-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBitStringTests.cs
More file actions
199 lines (181 loc) · 5.41 KB
/
BitStringTests.cs
File metadata and controls
199 lines (181 loc) · 5.41 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections;
using Xunit;
using Platform.Random;
namespace Platform.Collections.Tests
{
public static class BitStringTests
{
[Fact]
public static void BitGetSetTest()
{
const int n = 250;
var bitArray = new BitArray(n);
var bitString = new BitString(n);
for (var i = 0; i < n; i++)
{
var value = RandomHelpers.Default.NextBoolean();
bitArray.Set(i, value);
bitString.Set(i, value);
Assert.Equal(value, bitArray.Get(i));
Assert.Equal(value, bitString.Get(i));
}
}
[Fact]
public static void BitStringArrayConstructorTest()
{
const int n = 128;
var array = new long[] { unchecked((long)0x1234567890ABCDEF), unchecked((long)0xFEDCBA0987654321) };
var bitString = new BitString(array, n);
var normalBitString = new BitString(n);
for (var i = 0L; i < BitString.GetWordsCountFromIndex(n); i++)
{
for (var j = 0; j < 64 && (i * 64 + j) < n; j++)
{
var bitIndex = i * 64 + j;
var expectedValue = (array[i] & (1L << j)) != 0;
normalBitString.Set(bitIndex, expectedValue);
Assert.Equal(expectedValue, bitString.Get(bitIndex));
}
}
Assert.Equal(normalBitString.CountSetBits(), bitString.CountSetBits());
}
[Fact]
public static void CreateWithRandomBitsTest()
{
const int n = 1000;
var bitString1 = BitStringExtensions.CreateWithRandomBits(n);
var bitString2 = BitStringExtensions.CreateWithRandomBits(n);
Assert.Equal(n, bitString1.Length);
Assert.Equal(n, bitString2.Length);
Assert.False(bitString1.Equals(bitString2));
}
[Fact]
public static void BitVectorNotTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.VectorNot();
w.Not();
});
}
[Fact]
public static void BitParallelNotTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.ParallelNot();
w.Not();
});
}
[Fact]
public static void BitParallelVectorNotTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.ParallelVectorNot();
w.Not();
});
}
[Fact]
public static void BitVectorAndTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.VectorAnd(y);
w.And(v);
});
}
[Fact]
public static void BitParallelAndTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.ParallelAnd(y);
w.And(v);
});
}
[Fact]
public static void BitParallelVectorAndTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.ParallelVectorAnd(y);
w.And(v);
});
}
[Fact]
public static void BitVectorOrTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.VectorOr(y);
w.Or(v);
});
}
[Fact]
public static void BitParallelOrTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.ParallelOr(y);
w.Or(v);
});
}
[Fact]
public static void BitParallelVectorOrTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.ParallelVectorOr(y);
w.Or(v);
});
}
[Fact]
public static void BitVectorXorTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.VectorXor(y);
w.Xor(v);
});
}
[Fact]
public static void BitParallelXorTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.ParallelXor(y);
w.Xor(v);
});
}
[Fact]
public static void BitParallelVectorXorTest()
{
TestToOperationsWithSameMeaning((x, y, w, v) =>
{
x.ParallelVectorXor(y);
w.Xor(v);
});
}
private static void TestToOperationsWithSameMeaning(Action<BitString, BitString, BitString, BitString> test)
{
const int n = 5654;
var x = new BitString(n);
var y = new BitString(n);
while (x.Equals(y))
{
x.SetRandomBits();
y.SetRandomBits();
}
var w = new BitString(x);
var v = new BitString(y);
Assert.False(x.Equals(y));
Assert.False(w.Equals(v));
Assert.True(x.Equals(w));
Assert.True(y.Equals(v));
test(x, y, w, v);
Assert.True(x.Equals(w));
}
}
}