-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathValueSByteTests.cs
More file actions
140 lines (116 loc) · 4.78 KB
/
ValueSByteTests.cs
File metadata and controls
140 lines (116 loc) · 4.78 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
using BitStack;
using NUnit.Framework;
using UnityEngine;
/**
* Unit Tests designed to be ran by the Unity Test Runner which tests functionality
* related to the sbyte data type (signed byte, 8 bits)
*/
public static class ValueSByteTests {
static readonly sbyte TEST_VALUE = -87; // 10101001
static readonly string TEST_VALUE_STR = "10101001";
static readonly string TEST_HEX = "A9";
static readonly int LOOP_COUNT = 8;
// the expected bit sequence in array form
static readonly int[] EXPTECTED_BITS = { 1, 0, 0, 1, 0, 1, 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() {
sbyte posValue = 2;
sbyte zeroValue = 0;
sbyte negValue = -2;
Debug.Assert(posValue.Bool());
Debug.Assert(!zeroValue.Bool());
Debug.Assert(!negValue.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() {
sbyte inv = TEST_VALUE;
var invTest = (sbyte) ~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() == 4, "Expected Value(" + popCount + ") and Test(4) + 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_SByteFromBitString() {
var testByte = TEST_VALUE_STR.SByteFromBitString();
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() {
sbyte pow1 = 16;
sbyte pow2 = 64;
sbyte pow3 = 2;
sbyte nPow1 = 14;
sbyte nPow2 = 33;
sbyte 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(((byte) TEST_VALUE) == testValue, "Expected Test(" + ((byte) TEST_VALUE) + ") To be equal to Value(" + testValue + ")");
}
}