1
+ using NetBitOps ;
2
+ using NUnit . Framework ;
3
+ using System . Collections . Generic ;
4
+ using System . Linq ;
5
+
6
+ namespace BitOpsTest {
7
+ public class GetLastSetBitTests {
8
+ [ SetUp ]
9
+ public void Setup ( ) {
10
+ }
11
+
12
+ /*--------------------------------ULong--------------------------------*/
13
+ /// <summary>
14
+ /// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 64-bit ulong type.
15
+ /// </summary>
16
+ private static IEnumerable < ulong > TestULongSource =
17
+ Enumerable . Range ( 0 , sizeof ( ulong ) + 1 ) . Reverse ( ) . SelectMany (
18
+ index1 => Enumerable . Range ( 0 , sizeof ( ulong ) + 1 ) . Reverse ( ) . Select ( index2 => ( index1 != sizeof ( ulong ) ? unchecked ( ( ulong ) 0x80 ) << ( index1 * 8 ) : 0 ) | ( index2 != sizeof ( ulong ) ? unchecked ( ( ulong ) 0x1 ) << ( index2 * 8 ) : 0 ) )
19
+ ) ;
20
+
21
+ [ Test ]
22
+ [ Description ( "Test BitOps.GetLastSetBit for ulong type" ) ]
23
+ [ TestCaseSource ( nameof ( TestULongSource ) ) ]
24
+ public void ULongTest ( ulong n ) {
25
+ Assert . AreEqual ( TrueGetLastSetBit ( n ) , BitOps . GetLastSetBit ( n ) ) ;
26
+ }
27
+
28
+
29
+ /*--------------------------------UInt--------------------------------*/
30
+ /// <summary>
31
+ /// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 32-bit uint type.
32
+ /// </summary>
33
+ private static IEnumerable < uint > TestUIntegerSource =
34
+ Enumerable . Range ( 0 , sizeof ( uint ) + 1 ) . Reverse ( ) . SelectMany (
35
+ index1 => Enumerable . Range ( 0 , sizeof ( uint ) + 1 ) . Reverse ( ) . Select ( index2 => ( index1 != sizeof ( uint ) ? unchecked ( ( uint ) 0x80 ) << ( index1 * 8 ) : 0 ) | ( index2 != sizeof ( uint ) ? unchecked ( ( uint ) 0x1 ) << ( index2 * 8 ) : 0 ) )
36
+ ) ;
37
+
38
+ [ Test ]
39
+ [ Description ( "Test BitOps.GetLastSetBit for uint type" ) ]
40
+ [ TestCaseSource ( nameof ( TestUIntegerSource ) ) ]
41
+ public void UIntTest ( uint n ) {
42
+ Assert . AreEqual ( TrueGetLastSetBit ( n ) , BitOps . GetLastSetBit ( n ) ) ;
43
+ }
44
+
45
+
46
+ /*--------------------------------UShort--------------------------------*/
47
+ /// <summary>
48
+ /// All 0x80 and 0x1 (the last and the first bits of byte) combinations for each byte for 16-bit ushort type.
49
+ /// </summary>
50
+ private static IEnumerable < ushort > TestUShortSource =
51
+ Enumerable . Range ( 0 , sizeof ( ushort ) + 1 ) . Reverse ( ) . SelectMany (
52
+ index1 => Enumerable . Range ( 0 , sizeof ( ushort ) + 1 ) . Reverse ( ) . Select ( index2 => ( ushort ) ( ( index1 != sizeof ( ushort ) ? unchecked ( ( ushort ) 0x80 ) << ( index1 * 8 ) : 0 ) | ( index2 != sizeof ( ushort ) ? unchecked ( ( ushort ) 0x1 ) << ( index2 * 8 ) : 0 ) ) )
53
+ ) ;
54
+
55
+ [ Test ]
56
+ [ Description ( "Test BitOps.GetLastSetBit for ushort type" ) ]
57
+ [ TestCaseSource ( nameof ( TestUShortSource ) ) ]
58
+ public void UShortTest ( ushort n ) {
59
+ Assert . AreEqual ( TrueGetLastSetBit ( n ) , BitOps . GetLastSetBit ( n ) ) ;
60
+ }
61
+
62
+
63
+ /*--------------------------------Byte--------------------------------*/
64
+ /// <summary>
65
+ /// All 8-bit byte type values.
66
+ /// </summary>
67
+ private static IEnumerable < byte > TestByteSource = Enumerable . Range ( 0 , 2 << ( sizeof ( byte ) * 8 ) ) . Select ( n => ( byte ) n ) ;
68
+
69
+ [ Test ]
70
+ [ Description ( "Test BitOps.GetLastSetBit for byte type" ) ]
71
+ [ TestCaseSource ( nameof ( TestByteSource ) ) ]
72
+ public void ByteTest ( byte n ) {
73
+ Assert . AreEqual ( TrueGetLastSetBit ( n ) , BitOps . GetLastSetBit ( n ) ) ;
74
+ }
75
+
76
+ static int TrueGetLastSetBit ( ulong n ) {
77
+ int counter = - 1 ;
78
+ while ( n != 0 ) {
79
+ n >>= 1 ;
80
+ counter ++ ;
81
+ }
82
+ return counter ;
83
+ }
84
+
85
+ }
86
+ }
0 commit comments