A game about forced loneliness, made by TACStudios
1using System;
2using NUnit.Framework;
3using Unity.Collections;
4using Unity.Collections.Tests;
5
6internal class BitFieldTests : CollectionsTestFixture
7{
8 [Test]
9 public void BitField32_Get_Set()
10 {
11 var test = new BitField32();
12
13 uint bits;
14
15 bits = test.GetBits(0, 32);
16 Assert.AreEqual(0x0, bits);
17
18 test.SetBits(0, true);
19 bits = test.GetBits(0, 32);
20 Assert.AreEqual(0x1, bits);
21
22 test.SetBits(0, true, 32);
23 bits = test.GetBits(0, 32);
24 Assert.AreEqual(0xffffffff, bits);
25 Assert.IsTrue(test.TestAll(0, 32));
26
27 test.SetBits(0, false, 32);
28 bits = test.GetBits(0, 32);
29 Assert.AreEqual(0x0, bits);
30
31 test.SetBits(15, true, 7);
32 Assert.IsTrue(test.TestAll(15, 7));
33 test.SetBits(3, true, 3);
34 Assert.IsTrue(test.TestAll(3, 3));
35 bits = test.GetBits(0, 32);
36 Assert.AreEqual(0x3f8038, bits);
37 bits = test.GetBits(0, 15);
38 Assert.AreEqual(0x38, bits);
39 Assert.IsFalse(test.TestNone(0, 32));
40 Assert.IsFalse(test.TestAll(0, 32));
41 Assert.IsTrue(test.TestAny(0, 32));
42 }
43
44 [Test]
45 public void BitField32_Count_Leading_Trailing()
46 {
47 var test = new BitField32();
48
49 Assert.AreEqual(0, test.CountBits());
50 Assert.AreEqual(32, test.CountLeadingZeros());
51 Assert.AreEqual(32, test.CountTrailingZeros());
52
53 test.SetBits(31, true);
54 Assert.AreEqual(1, test.CountBits());
55 Assert.AreEqual(0, test.CountLeadingZeros());
56 Assert.AreEqual(31, test.CountTrailingZeros());
57
58 test.SetBits(0, true);
59 Assert.AreEqual(2, test.CountBits());
60 Assert.AreEqual(0, test.CountLeadingZeros());
61 Assert.AreEqual(0, test.CountTrailingZeros());
62
63 test.SetBits(31, false);
64 Assert.AreEqual(1, test.CountBits());
65 Assert.AreEqual(31, test.CountLeadingZeros());
66 Assert.AreEqual(0, test.CountTrailingZeros());
67 }
68
69 [Test]
70 [TestRequiresDotsDebugOrCollectionChecks]
71 public void BitField32_Throws()
72 {
73 var test = new BitField32();
74
75 for (byte i = 0; i < 32; ++i)
76 {
77 Assert.DoesNotThrow(() => { test.GetBits(i, (byte)(32 - i)); });
78 }
79
80 Assert.Throws<ArgumentException>(() => { test.GetBits(0, 33); });
81 Assert.Throws<ArgumentException>(() => { test.GetBits(1, 32); });
82 }
83
84 [Test]
85 public void BitField64_Get_Set()
86 {
87 var test = new BitField64();
88
89 ulong bits;
90
91 bits = test.GetBits(0, 64);
92 Assert.AreEqual(0x0, bits);
93
94 test.SetBits(0, true);
95 bits = test.GetBits(0, 64);
96 Assert.AreEqual(0x1, bits);
97
98 test.SetBits(0, true, 64);
99 bits = test.GetBits(0, 64);
100 Assert.AreEqual(0xfffffffffffffffful, bits);
101 Assert.IsTrue(test.TestAll(0, 64));
102
103 test.SetBits(0, false, 64);
104 bits = test.GetBits(0, 64);
105 Assert.AreEqual(0x0ul, bits);
106
107 test.SetBits(15, true, 7);
108 Assert.IsTrue(test.TestAll(15, 7));
109 test.SetBits(3, true, 3);
110 Assert.IsTrue(test.TestAll(3, 3));
111 bits = test.GetBits(0, 32);
112 Assert.AreEqual(0x3f8038, bits);
113 bits = test.GetBits(0, 15);
114 Assert.AreEqual(0x38, bits);
115 Assert.IsFalse(test.TestNone(0, 64));
116 Assert.IsFalse(test.TestAll(0, 64));
117 Assert.IsTrue(test.TestAny(0, 64));
118 }
119
120 [Test]
121 [TestRequiresDotsDebugOrCollectionChecks]
122 public void BitField64_Throws()
123 {
124 var test = new BitField64();
125
126 for (byte i = 0; i < 64; ++i)
127 {
128 Assert.DoesNotThrow(() => { test.GetBits(i, (byte)(64 - i)); });
129 }
130
131 Assert.Throws<ArgumentException>(() => { test.GetBits(0, 65); });
132 Assert.Throws<ArgumentException>(() => { test.GetBits(1, 64); });
133 }
134
135 [Test]
136 public void BitField64_Count_Leading_Trailing()
137 {
138 var test = new BitField64();
139
140 Assert.AreEqual(0, test.CountBits());
141 Assert.AreEqual(64, test.CountLeadingZeros());
142 Assert.AreEqual(64, test.CountTrailingZeros());
143
144 test.SetBits(63, true);
145 Assert.AreEqual(1, test.CountBits());
146 Assert.AreEqual(0, test.CountLeadingZeros());
147 Assert.AreEqual(63, test.CountTrailingZeros());
148
149 test.SetBits(0, true);
150 Assert.AreEqual(2, test.CountBits());
151 Assert.AreEqual(0, test.CountLeadingZeros());
152 Assert.AreEqual(0, test.CountTrailingZeros());
153
154 test.SetBits(63, false);
155 Assert.AreEqual(1, test.CountBits());
156 Assert.AreEqual(63, test.CountLeadingZeros());
157 Assert.AreEqual(0, test.CountTrailingZeros());
158 }
159}