-
Notifications
You must be signed in to change notification settings - Fork 1
/
complement_test.go
71 lines (58 loc) · 1.87 KB
/
complement_test.go
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
package goasterix
import "testing"
func TestTwoComplement16_PositiveNumber(t *testing.T) {
// Arrange
input := uint16(0x010F) // 0000 0001 0000 1111
size := uint8(10) // ---- --01 0000 1111 -> tenth bit
output := int16(271) // 01 0000 1111 = 271
// Act
result := TwoComplement16(size, input)
// Assert
if result != output {
t.Errorf("FAIL: result = %v; Expected: %v", result, output)
} else {
t.Logf("SUCCESS: result = %v; Expected: %v", result, output)
}
}
func TestTwoComplement16_NegativeNumber(t *testing.T) {
// Arrange
input := uint16(0x040F) // 0000 0100 0000 1111
size := uint8(11) // ---- -100 0000 1111 -> tenth bit
output := int16(-1009) // ---- -011 1111 0001 = -1009
// Act
result := TwoComplement16(size, input)
// Assert
if result != output {
t.Errorf("FAIL: result = %v; Expected: %v", result, output)
} else {
t.Logf("SUCCESS: result = %v; Expected: %v", result, output)
}
}
func TestTwoComplement32_PositiveNumber(t *testing.T) {
// Arrange
input := uint32(0x0007EE0F) // 0000 0000 0000 0111 1110 1110 0000 1111
size := uint8(20) // ---- ---- ---- 0111 1110 1110 0000 1111 -> twentieth bit
output := int32(519695)
// Act
result := TwoComplement32(size, input)
// Assert
if result != output {
t.Errorf("FAIL: result = %v; Expected: %v", result, output)
} else {
t.Logf("SUCCESS: result = %v; Expected: %v", result, output)
}
}
func TestTwoComplement32_NegativeNumber(t *testing.T) {
// Arrange
input := uint32(0x000FEE0F) // 0000 0000 0000 0111 1110 1110 0000 1111
size := uint8(20) // ---- ---- ---- 0111 1110 1110 0000 1111 -> twentieth bit
output := int32(-4593)
// Act
result := TwoComplement32(size, input)
// Assert
if result != output {
t.Errorf("FAIL: result = %v; Expected: %v", result, output)
} else {
t.Logf("SUCCESS: result = %v; Expected: %v", result, output)
}
}