1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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));
}
[Test]
public void TestPopCount()
{
Random random = new Random();
for (int pos = 0; pos <= 24; ++pos)
{
int seed = Integers.RotateLeft(random.Next(0xFFFFFF) << 8, pos);
ImplTestPopCountRange(seed, pos, 0xFF);
}
}
private static void ImplTestPopCountRange(int seed, int pos, int count)
{
for (int i = 0; i < count; ++i)
{
int n = seed + (i << pos);
int expected = SimpleBitCount(n);
Assert.AreEqual(expected, Integers.PopCount(n));
Assert.AreEqual(expected, Integers.PopCount((uint)n));
}
}
private static int SimpleBitCount(int n)
{
int count = 0;
for (int i = 0; i < 32; ++i)
{
count += (n >> i) & 1;
}
return count;
}
}
}
|