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
|
package ipmi
import (
"testing"
)
func Test_onesComplement(t *testing.T) {
// https://en.wikipedia.org/wiki/Ones%27_complement
tests := []struct {
name string
input uint32
bitSize uint8
expected int32
}{
{"test1", 127, 8, 127},
{"test2", 126, 8, 126},
{"test3", 2, 8, 2},
{"test4", 1, 8, 1},
{"test5", 0, 8, 0},
{"test6", 255, 8, -0},
{"test7", 254, 8, -1},
{"test8", 253, 8, -2},
{"test9", 129, 8, -126},
{"test10", 128, 8, -127},
}
for _, test := range tests {
got := onesComplement(test.input, test.bitSize)
if got != test.expected {
t.Errorf("test %s not matched, got: %d, expected: %d", test.name, got, test.expected)
}
}
}
func Test_twosComplement(t *testing.T) {
// https://en.wikipedia.org/wiki/Two%27s_complement
tests := []struct {
name string
input uint32
bitSize uint8
expected int32
}{
{"test1", 0, 8, 0},
{"test2", 1, 8, 1},
{"test3", 2, 8, 2},
{"test4", 126, 8, 126},
{"test5", 127, 8, 127},
{"test6", 128, 8, -128},
{"test7", 129, 8, -127},
{"test8", 130, 8, -126},
{"test9", 254, 8, -2},
{"test10", 255, 8, -1},
}
for _, test := range tests {
got := twosComplement(test.input, test.bitSize)
if got != test.expected {
t.Errorf("test %s not matched, got: %d, expected: %d", test.name, got, test.expected)
}
}
}
func Test_onesComplementEncode(t *testing.T) {
// https://en.wikipedia.org/wiki/Ones%27_complement
tests := []struct {
name string
expected uint32
bitSize uint8
input int32
}{
{"test1", 127, 8, 127},
{"test2", 126, 8, 126},
{"test3", 2, 8, 2},
{"test4", 1, 8, 1},
{"test5", 0, 8, 0},
// {"test6", 255, 8, -0},
{"test7", 254, 8, -1},
{"test8", 253, 8, -2},
{"test9", 129, 8, -126},
{"test10", 128, 8, -127},
}
for _, test := range tests {
got := onesComplementEncode(test.input, test.bitSize)
if got != test.expected {
t.Errorf("test %s not matched, got: %d, expected: %d", test.name, got, test.expected)
}
}
}
func Test_twosComplementEncode(t *testing.T) {
// https://en.wikipedia.org/wiki/Two%27s_complement
tests := []struct {
name string
expected uint32
bitSize uint8
input int32
}{
{"test1", 0, 8, 0},
{"test2", 1, 8, 1},
{"test3", 2, 8, 2},
{"test4", 126, 8, 126},
{"test5", 127, 8, 127},
{"test6", 128, 8, -128},
{"test7", 129, 8, -127},
{"test8", 130, 8, -126},
{"test9", 254, 8, -2},
{"test10", 255, 8, -1},
}
for _, test := range tests {
got := twosComplementEncode(test.input, test.bitSize)
if got != test.expected {
t.Errorf("test %s not matched, got: %d, expected: %d", test.name, got, test.expected)
}
}
}
func Test_unpackBytes(t *testing.T) {
tests := []struct {
name string
expected []byte
input []uint8
offset int
length int
}{
{"test1", []byte{0x03, 0x04}, []uint8{0x01, 0x02, 0x03, 0x04}, 2, 2},
{"test2", []byte{0x04, 0x05, 0x06, 0x07}, []uint8{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 3, 4},
{"test3", []byte{}, []uint8{}, 0, 0},
{"test4", []byte{0x01}, []uint8{0x01}, 0, 1},
}
for _, tt := range tests {
got, _, err := unpackBytes(tt.input, tt.offset, tt.length)
if err != nil {
t.Errorf("test (%s) unpackBytes failed, err: %s", tt.name, err)
return
}
if !isByteSliceEqual(got, tt.expected) {
t.Errorf("test %s not matched, got: %v, expected: %v", tt.name, got, tt.expected)
return
}
}
}
|