-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathBitStackTests.cs
152 lines (134 loc) · 4.84 KB
/
BitStackTests.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
namespace System.Text.Json.Tests
{
public static partial class BitStackTests
{
private static readonly Random s_random = new Random(42);
[Fact]
public static void DefaultBitStack()
{
BitStack bitStack = default;
Assert.Equal(0, bitStack.CurrentDepth);
}
[Fact]
public static void SetResetFirstBit()
{
BitStack bitStack = default;
Assert.Equal(0, bitStack.CurrentDepth);
bitStack.SetFirstBit();
Assert.Equal(1, bitStack.CurrentDepth);
Assert.False(bitStack.Pop());
Assert.Equal(0, bitStack.CurrentDepth);
bitStack = default;
Assert.Equal(0, bitStack.CurrentDepth);
bitStack.ResetFirstBit();
Assert.Equal(1, bitStack.CurrentDepth);
Assert.False(bitStack.Pop());
Assert.Equal(0, bitStack.CurrentDepth);
bitStack = default;
Assert.Equal(0, bitStack.CurrentDepth);
bitStack.SetFirstBit();
Assert.Equal(1, bitStack.CurrentDepth);
Assert.False(bitStack.Pop());
Assert.Equal(0, bitStack.CurrentDepth);
bitStack.ResetFirstBit();
Assert.Equal(1, bitStack.CurrentDepth);
Assert.False(bitStack.Pop());
Assert.Equal(0, bitStack.CurrentDepth);
}
[Theory]
[InlineData(32)]
[InlineData(64)]
[InlineData(256)]
public static void BitStackPushPop(int bitLength)
{
BitStack bitStack = default;
Assert.Equal(0, bitStack.CurrentDepth);
var values = new bool[bitLength];
for (int i = 0; i < bitLength; i++)
{
values[i] = s_random.NextDouble() >= 0.5;
}
for (int i = 0; i < bitLength; i++)
{
if (values[i])
{
bitStack.PushTrue();
}
else
{
bitStack.PushFalse();
}
Assert.Equal(i + 1, bitStack.CurrentDepth);
}
// Loop backwards when popping.
for (int i = bitLength - 1; i > 0; i--)
{
// We need the value at the top *after* popping off the last one.
Assert.Equal(values[i - 1], bitStack.Pop());
Assert.Equal(i, bitStack.CurrentDepth);
}
}
[Theory]
[OuterLoop]
[InlineData(3_200_000)]
[InlineData(int.MaxValue / 32 + 1)] // 67_108_864
public static void BitStackPushPopLarge(int bitLength)
{
BitStack bitStack = default;
Assert.Equal(0, bitStack.CurrentDepth);
var values = new bool[bitLength];
for (int i = 0; i < bitLength; i++)
{
values[i] = s_random.NextDouble() >= 0.5;
}
const int IterationCapacity = 1_600_000;
int expectedDepth = 0;
// Only set and compare the first and last few (otherwise, the test takes too long)
for (int i = 0; i < IterationCapacity; i++)
{
if (values[i])
{
bitStack.PushTrue();
}
else
{
bitStack.PushFalse();
}
expectedDepth++;
Assert.Equal(expectedDepth, bitStack.CurrentDepth);
}
for (int i = bitLength - IterationCapacity; i < bitLength; i++)
{
if (values[i])
{
bitStack.PushTrue();
}
else
{
bitStack.PushFalse();
}
expectedDepth++;
Assert.Equal(expectedDepth, bitStack.CurrentDepth);
}
Assert.Equal(IterationCapacity * 2, expectedDepth);
// Loop backwards when popping.
for (int i = bitLength - 1; i >= bitLength - IterationCapacity; i--)
{
// We need the value at the top *after* popping off the last one.
Assert.Equal(values[i - 1], bitStack.Pop());
expectedDepth--;
Assert.Equal(expectedDepth, bitStack.CurrentDepth);
}
for (int i = IterationCapacity - 1; i > 0; i--)
{
// We need the value at the top *after* popping off the last one.
Assert.Equal(values[i - 1], bitStack.Pop());
expectedDepth--;
Assert.Equal(expectedDepth, bitStack.CurrentDepth);
}
}
}
}