-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathValueByteArrayTests.cs
191 lines (138 loc) · 5.83 KB
/
ValueByteArrayTests.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
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
using System;
using BitStack;
using NUnit.Framework;
using UnityEngine;
public static class ValueByteArrayTests {
const int BITS = 8;
const int BYTES = BITS / 8;
static readonly byte[] TEST_VALUE_ARRAY = { 128, 196, 213, 254, 96 };
// looper values
static readonly int LOOP_COUNT = BITS * TEST_VALUE_ARRAY.Length;
static readonly int LOOP_COUNT_BYTES = BYTES * TEST_VALUE_ARRAY.Length;
// the bit sequence of the above values as a linear array of 1 and 0
static readonly int[] EXPTECTED_BITS = CalcBits(TEST_VALUE_ARRAY);
// inverted variant of current bytes
static readonly byte[] TEST_BYTES = CalcBytes(TEST_VALUE_ARRAY, true);
// the expected byte sequence of the current values
static readonly byte[] EXPECTED_BYTES = CalcBytes(TEST_VALUE_ARRAY, false);
// NOTE -> This is Tested elsewhere and is assumed correct
static int[] CalcBits(byte[] value) {
int[] bits = new int[BITS * value.Length];
int index = 0;
for (int i = 0; i < value.Length; i++) {
for (int j = 0; j < BITS; j++) {
bits[index] = value[i].BitAt(j);
index++;
}
}
return bits;
}
// NOTE -> This is Tested elsewhere and is assumed correct
static byte[] CalcBytes(byte[] value, bool invert) {
byte[] bytes = new byte[BYTES * value.Length];
int index = 0;
for (int i = 0; i < value.Length; i++) {
for (int j = 0; j < BYTES; j++) {
// flip/invert the byte
if (invert) {
bytes[index] = (byte) ~(value[i].ByteAt(j));
} else {
bytes[index] = value[i].ByteAt(j);
}
index++;
}
}
return bytes;
}
// NOTE -> This is Tested elsewhere and is assumed correct
static byte[] GetTestArray() {
byte[] copyArray = new byte[TEST_VALUE_ARRAY.Length];
Array.Copy(TEST_VALUE_ARRAY, copyArray, TEST_VALUE_ARRAY.Length);
return copyArray;
}
[Test]
public static void Test_BitAt() {
byte[] TEST_VALUE = GetTestArray();
for (int i = 0; i < LOOP_COUNT; i++) {
int bit = TEST_VALUE.BitAt(i);
Debug.Assert(bit == EXPTECTED_BITS[i], "Expected Bit(" + bit + ") at Index(" + i + ") to be " + EXPTECTED_BITS[i]);
}
}
[Test]
public static void Test_BitInvAt() {
byte[] TEST_VALUE = GetTestArray();
for (int i = 0; i < LOOP_COUNT; i++) {
int bit = TEST_VALUE.BitInvAt(i);
Debug.Assert(TEST_VALUE.BitInvAt(i) != EXPTECTED_BITS[i], "Expected Bit(" + bit + ") at Index(" + i + ") not to be " + EXPTECTED_BITS[i]);
}
}
[Test]
public static void Test_SetBitAt() {
byte[] TEST_VALUE = GetTestArray();
for (int i = 0; i < LOOP_COUNT; i++) {
TEST_VALUE.SetBitAt(i);
Debug.Assert(TEST_VALUE.BitAt(i) == 1, "Expected Bit Position(" + i + ") to be 1");
}
}
[Test]
public static void Test_UnsetBitAt() {
byte[] TEST_VALUE = GetTestArray();
for (int i = 0; i < LOOP_COUNT; i++) {
TEST_VALUE.UnsetBitAt(i);
Debug.Assert(TEST_VALUE.BitAt(i) == 0, "Expected Bit Position(" + i + ") to be 0");
}
}
[Test]
public static void Test_SetBit() {
byte[] TEST_VALUE = GetTestArray();
for (int i = 0; i < LOOP_COUNT; i++) {
TEST_VALUE.SetBit(i, 0);
Debug.Assert(TEST_VALUE.BitAt(i) == 0, "Expected Bit Position(" + i + ") to be 0");
TEST_VALUE.SetBit(i, 1);
Debug.Assert(TEST_VALUE.BitAt(i) == 1, "Expected Bit Position(" + i + ") to be 1");
}
}
[Test]
public static void Test_SetUnsetBit() {
byte[] TEST_VALUE = GetTestArray();
for (int i = 0; i < LOOP_COUNT; i++) {
TEST_VALUE.SetBit(i, 0);
TEST_VALUE.SetBit(i, 1);
Debug.Assert(TEST_VALUE.BitAt(i) == 1, "Expected Bit Position(" + i + ") to be 1");
TEST_VALUE.SetBit(i, 1);
TEST_VALUE.SetBit(i, 0);
Debug.Assert(TEST_VALUE.BitAt(i) == 0, "Expected Bit Position(" + i + ") to be 0");
}
}
[Test]
public static void Test_ToggleBitAt() {
byte[] TEST_VALUE = GetTestArray();
for (int i = 0; i < LOOP_COUNT; i++) {
TEST_VALUE.ToggleBitAt(i);
int inv = TEST_VALUE.BitAt(i);
Debug.Assert(TEST_VALUE.BitAt(i) != EXPTECTED_BITS[i], "Expected Toggle(" + inv + ") and InvTest(" + EXPTECTED_BITS[i] + ") to not Match.");
}
for (int i = 0; i < LOOP_COUNT; i++) {
TEST_VALUE.ToggleBitAt(i);
int inv = TEST_VALUE.BitAt(i);
Debug.Assert(TEST_VALUE.BitAt(i) == EXPTECTED_BITS[i], "Expected Toggle(" + inv + ") and InvTest(" + EXPTECTED_BITS[i] + ") to Match.");
}
}
[Test]
public static void Test_SetByteAt_ByteAt() {
byte[] TEST_VALUE = GetTestArray();
for (int i = 0; i < LOOP_COUNT_BYTES; i++) {
TEST_VALUE.SetByteAt(TEST_BYTES[i], i);
byte value = TEST_VALUE.ByteAt(i);
Debug.Assert(value == TEST_BYTES[i], "Expected Byte(" + value + ") and Test(" + TEST_BYTES[i] + ") to Match.");
TEST_VALUE.SetByteAt(EXPECTED_BYTES[i], i);
value = TEST_VALUE.ByteAt(i);
Debug.Assert(value == EXPECTED_BYTES[i], "Expected Byte(" + value + ") and Test(" + EXPECTED_BYTES[i] + ") to Match.");
}
// in the end, all values should match the original since we have inverted operations
// in the last test
for (int i = 0; i < TEST_VALUE.Length; i++) {
Debug.Assert(TEST_VALUE[i] == TEST_VALUE_ARRAY[i], "Expected Value(" + TEST_VALUE[i] + ") and Test(" + TEST_VALUE_ARRAY[i] + ") to Match.");
}
}
}