-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathValueByteTests.cs
More file actions
142 lines (114 loc) · 4.68 KB
/
ValueByteTests.cs
File metadata and controls
142 lines (114 loc) · 4.68 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
using BitStack;
using NUnit.Framework;
using UnityEngine;
/**
* Unit Tests designed to be ran by the Unity Test Runner which tests functionality
* related to the byte data type (unsigned byte, 8 bits)
*/
public static class ValueByteTests {
static readonly byte TEST_VALUE = 158; // 10011110
static readonly string TEST_VALUE_STR = "10011110";
static readonly string TEST_HEX = "9E";
static readonly int LOOP_COUNT = 8;
// the expected bit sequence in array form
static readonly int[] EXPTECTED_BITS = { 0, 1, 1, 1, 1, 0, 0, 1 };
[Test]
public static void Test_BitAt() {
for (int i = 0; i < LOOP_COUNT; i++) {
Debug.Assert(TEST_VALUE.BitAt(i) == EXPTECTED_BITS[i]);
}
}
[Test]
public static void Test_BitInvAt() {
for (int i = 0; i < LOOP_COUNT; i++) {
Debug.Assert(TEST_VALUE.BitInvAt(i) != (EXPTECTED_BITS[i]));
}
}
[Test]
public static void Test_Bool() {
byte posValue = 2;
byte zeroValue = 0;
Debug.Assert(posValue.Bool());
Debug.Assert(!zeroValue.Bool());
}
[Test]
public static void Test_SetBitAt() {
for (int i = 0; i < LOOP_COUNT; i++) {
Debug.Assert(TEST_VALUE.SetBitAt(i).BitAt(i) == 1, "Expected Bit Position(" + i + ") to be 1");
}
}
[Test]
public static void Test_UnsetBitAt() {
for (int i = 0; i < LOOP_COUNT; i++) {
Debug.Assert(TEST_VALUE.UnsetBitAt(i).BitAt(i) == 0, "Expected Bit Position(" + i + ") to be 0");
}
}
[Test]
public static void Test_SetBit() {
for (int i = 0; i < LOOP_COUNT; i++) {
Debug.Assert(TEST_VALUE.SetBit(i, 0).BitAt(i) == 0, "Expected Bit Position(" + i + ") to be 0");
Debug.Assert(TEST_VALUE.SetBit(i, 1).BitAt(i) == 1, "Expected Bit Position(" + i + ") to be 1");
}
}
[Test]
public static void Test_SetUnsetBit() {
for (int i = 0; i < LOOP_COUNT; i++) {
Debug.Assert(TEST_VALUE.SetBit(i, 0).SetBit(i, 1).BitAt(i) == 1, "Expected Bit Position(" + i + ") to be 1");
Debug.Assert(TEST_VALUE.SetBit(i, 1).SetBit(i, 0).BitAt(i) == 0, "Expected Bit Position(" + i + ") to be 0");
}
}
[Test]
public static void Test_ToggleBitAt() {
byte inv = TEST_VALUE;
var invTest = (byte) ~TEST_VALUE;
for (int i = 0; i < LOOP_COUNT; i++) {
inv = inv.ToggleBitAt(i);
}
Debug.Assert(inv == invTest, "Expected Toggle(" + inv + ") and InvTest(" + (invTest) + ") to Match.");
// invert the order to ensure
for (int i = 0; i < LOOP_COUNT; i++) {
inv = inv.ToggleBitAt(i);
}
Debug.Assert(inv == TEST_VALUE, "Expected Toggle(" + inv + ") and Test(" + TEST_VALUE + ") to Match.");
}
[Test]
public static void Test_PopCount() {
var popCount = TEST_VALUE.PopCount();
Debug.Assert(TEST_VALUE.PopCount() == 5, "Expected Value(" + popCount + ") and Test(5) + to Match");
}
[Test]
public static void Test_BitString() {
var testStr = TEST_VALUE.BitString();
Debug.Assert(testStr == TEST_VALUE_STR, "Expected String(" + testStr + ") and Test(" + TEST_VALUE_STR + ") to Match.");
}
[Test]
public static void Test_ByteFromBitString() {
var testByte = TEST_VALUE_STR.ByteFromBitString();
Debug.Assert(testByte == TEST_VALUE, "Expected Byte(" + testByte + ") and Test(" + TEST_VALUE + ") to Match.");
}
[Test]
public static void Test_HexString() {
var testHex = TEST_VALUE.HexString();
Debug.Assert(testHex == TEST_HEX, "Expected Hex(" + testHex + ") and Test(" + TEST_HEX + ") to Match.");
}
[Test]
public static void Test_IsPowerOfTwo() {
byte pow1 = 16;
byte pow2 = 64;
byte pow3 = 2;
byte nPow1 = 14;
byte nPow2 = 33;
byte nPow3 = 5;
Debug.Assert(pow1.IsPowerOfTwo(), "Expected Test(" + pow1 + ") To be Power of Two");
Debug.Assert(pow2.IsPowerOfTwo(), "Expected Test(" + pow2 + ") To be Power of Two");
Debug.Assert(pow3.IsPowerOfTwo(), "Expected Test(" + pow3 + ") To be Power of Two");
Debug.Assert(!nPow1.IsPowerOfTwo(), "Expected Test(" + nPow1 + ") To be Non Power of Two");
Debug.Assert(!nPow2.IsPowerOfTwo(), "Expected Test(" + nPow2 + ") To be Non Power of Two");
Debug.Assert(!nPow3.IsPowerOfTwo(), "Expected Test(" + nPow3 + ") To be Non Power of Two");
}
[Test]
public static void Test_GetByte() {
byte testValue = TEST_VALUE.ByteAt(0);
Debug.Assert(TEST_VALUE == testValue, "Expected Test(" + TEST_VALUE + ") To be equal to Value(" + testValue + ")");
}
}