summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorDavid Hook <dgh@bouncycastle.org>2021-06-03 13:07:09 +1000
committerDavid Hook <dgh@bouncycastle.org>2021-06-03 13:07:09 +1000
commit2618a7e99306d25d2f3cfb61afc9c4a7832683a3 (patch)
tree9e55fd73a2d1729c8fbb7d698738feea8854bb4c /crypto/test
parentgithub #172 - added blowfish init check on key size (diff)
parentNTZ for 0 should be 32/64 resp. (diff)
downloadBouncyCastle.NET-ed25519-2618a7e99306d25d2f3cfb61afc9c4a7832683a3.tar.xz
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/UnitTests.csproj2
-rw-r--r--crypto/test/src/util/utiltest/IntegersTest.cs36
-rw-r--r--crypto/test/src/util/utiltest/LongsTest.cs36
3 files changed, 74 insertions, 0 deletions
diff --git a/crypto/test/UnitTests.csproj b/crypto/test/UnitTests.csproj
index 2164d2949..58737f9ce 100644
--- a/crypto/test/UnitTests.csproj
+++ b/crypto/test/UnitTests.csproj
@@ -486,6 +486,8 @@
     <Compile Include="src\util\test\TestRandomBigInteger.cs" />
     <Compile Include="src\util\test\TestRandomData.cs" />
     <Compile Include="src\util\test\UncloseableStream.cs" />
+    <Compile Include="src\util\utiltest\IntegersTest.cs" />
+    <Compile Include="src\util\utiltest\LongsTest.cs" />
     <Compile Include="src\x509\test\TestCertificateGen.cs" />
   </ItemGroup>
   <ItemGroup>
diff --git a/crypto/test/src/util/utiltest/IntegersTest.cs b/crypto/test/src/util/utiltest/IntegersTest.cs
new file mode 100644
index 000000000..a661144db
--- /dev/null
+++ b/crypto/test/src/util/utiltest/IntegersTest.cs
@@ -0,0 +1,36 @@
+using System;
+
+using NUnit.Framework;
+
+namespace Org.BouncyCastle.Utilities.UtilTests
+{
+    [TestFixture]
+    public class IntegersTest
+    {
+        [Test]
+        public void TestNumberOfLeadingZeros()
+        {
+            for (int i = 0; i < 31; ++i)
+            {
+                Assert.AreEqual(i, Integers.NumberOfLeadingZeros((int)(0x80000000U >> i)));
+                Assert.AreEqual(i, Integers.NumberOfLeadingZeros((int)(0xFFFFFFFFU >> i)));
+            }
+
+            Assert.AreEqual(31, Integers.NumberOfLeadingZeros(1));
+            Assert.AreEqual(32, Integers.NumberOfLeadingZeros(0));
+        }
+
+        [Test]
+        public void TestNumberOfTrailingZeros()
+        {
+            for (int i = 0; i < 31; ++i)
+            {
+                Assert.AreEqual(i, Integers.NumberOfTrailingZeros(1 << i));
+                Assert.AreEqual(i, Integers.NumberOfTrailingZeros(-1 << i));
+            }
+
+            Assert.AreEqual(31, Integers.NumberOfTrailingZeros(int.MinValue));
+            Assert.AreEqual(32, Integers.NumberOfTrailingZeros(0));
+        }
+    }
+}
diff --git a/crypto/test/src/util/utiltest/LongsTest.cs b/crypto/test/src/util/utiltest/LongsTest.cs
new file mode 100644
index 000000000..b2ddb6656
--- /dev/null
+++ b/crypto/test/src/util/utiltest/LongsTest.cs
@@ -0,0 +1,36 @@
+using System;
+
+using NUnit.Framework;
+
+namespace Org.BouncyCastle.Utilities.UtilTests
+{
+    [TestFixture]
+    public class LongsTest
+    {
+        [Test]
+        public void TestNumberOfLeadingZeros()
+        {
+            for (int i = 0; i < 63; ++i)
+            {
+                Assert.AreEqual(i, Longs.NumberOfLeadingZeros((long)(0x8000000000000000UL >> i)));
+                Assert.AreEqual(i, Longs.NumberOfLeadingZeros((long)(0xFFFFFFFFFFFFFFFFUL >> i)));
+            }
+
+            Assert.AreEqual(63, Longs.NumberOfLeadingZeros(1L));
+            Assert.AreEqual(64, Longs.NumberOfLeadingZeros(0L));
+        }
+
+        [Test]
+        public void TestNumberOfTrailingZeros()
+        {
+            for (int i = 0; i < 63; ++i)
+            {
+                Assert.AreEqual(i, Longs.NumberOfTrailingZeros(1L << i));
+                Assert.AreEqual(i, Longs.NumberOfTrailingZeros(-1L << i));
+            }
+
+            Assert.AreEqual(63, Longs.NumberOfTrailingZeros(long.MinValue));
+            Assert.AreEqual(64, Longs.NumberOfTrailingZeros(0L));
+        }
+    }
+}