2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
32 func TestMain(m *testing.M) {
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 func TestBcdEven(t *testing.T) {
43 bcd := NewBcd("0123456789??????")
44 bcdbuf := bcd.Encode("123456")
46 t.Errorf("TestBcdEven: bcd Encode failed")
49 if bcdbuf[0] != 0x12 {
50 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x12 got 0x%x", bcdbuf[0])
53 if bcdbuf[1] != 0x34 {
54 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x34 got 0x%x", bcdbuf[1])
57 if bcdbuf[2] != 0x56 {
58 t.Errorf("TestBcdEven: bcdbuf[2] expected 0x56 got 0x%x", bcdbuf[2])
61 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
62 hex.Encode(hexdata, bcdbuf)
63 fmt.Printf("TestBcdEven: 123456 encoded data [%s]\n", string(hexdata))
65 bcdstr := bcd.Decode(bcdbuf)
66 if bcdstr != string("123456") {
67 t.Errorf("TestBcdEven: bcd Decode failed: got %s expect %s", bcdstr, string("123456"))
72 func TestBcdUnEven1(t *testing.T) {
74 bcd := NewBcd("0123456789??????")
75 bcdbuf := bcd.Encode("12345")
77 t.Errorf("TestBcdUnEven1: bcd Encode failed")
80 if bcdbuf[0] != 0x12 {
81 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x12 got 0x%x", bcdbuf[0])
84 if bcdbuf[1] != 0x34 {
85 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x34 got 0x%x", bcdbuf[1])
88 if bcdbuf[2] != 0x5f {
89 t.Errorf("TestBcdEven: bcdbuf[2] expected 0x5f got 0x%x", bcdbuf[2])
92 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
93 hex.Encode(hexdata, bcdbuf)
94 fmt.Printf("TestBcdUnEven1: 12345 encoded data [%s]\n", string(hexdata))
96 bcdstr := bcd.Decode(bcdbuf)
97 if bcdstr != string("12345?") {
98 t.Errorf("TestBcdUnEven1: bcd Decode failed: got %s expect %s", bcdstr, string("12345?"))
102 func TestBcdUnEven2(t *testing.T) {
104 bcd := NewBcd("0123456789?????f")
105 bcdbuf := bcd.Encode("12345f")
106 if len(bcdbuf) == 0 {
107 t.Errorf("TestBcdUnEven2: bcd Encode failed")
110 if bcdbuf[0] != 0x12 {
111 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x12 got 0x%x", bcdbuf[0])
114 if bcdbuf[1] != 0x34 {
115 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x34 got 0x%x", bcdbuf[1])
118 if bcdbuf[2] != 0x5f {
119 t.Errorf("TestBcdEven: bcdbuf[2] expected 0x5f got 0x%x", bcdbuf[2])
122 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
123 hex.Encode(hexdata, bcdbuf)
124 fmt.Printf("TestBcdUnEven2: 12345f encoded data [%s]\n", string(hexdata))
126 bcdstr := bcd.Decode(bcdbuf)
127 if bcdstr != string("12345f") {
128 t.Errorf("TestBcdUnEven2: bcd Decode failed: got %s expect %s", bcdstr, string("12345f"))
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
135 func TestTbcdEven(t *testing.T) {
137 bcd := NewTbcd("0123456789??????")
138 bcdbuf := bcd.Encode("123456")
139 if len(bcdbuf) == 0 {
140 t.Errorf("TestTbcdEven: bcd Encode failed")
143 if bcdbuf[0] != 0x21 {
144 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x21 got 0x%x", bcdbuf[0])
147 if bcdbuf[1] != 0x43 {
148 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x34 got 0x%x", bcdbuf[1])
151 if bcdbuf[2] != 0x65 {
152 t.Errorf("TestBcdEven: bcdbuf[2] expected 0x65 got 0x%x", bcdbuf[2])
155 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
156 hex.Encode(hexdata, bcdbuf)
157 fmt.Printf("TestTbcdEven: 123456 encoded data [%s]\n", string(hexdata))
159 bcdstr := bcd.Decode(bcdbuf)
160 if bcdstr != string("123456") {
161 t.Errorf("TestTbcdEven: bcd Decode failed: got %s expect %s", bcdstr, string("123456"))
166 func TestTbcdUnEven1(t *testing.T) {
168 bcd := NewTbcd("0123456789??????")
169 bcdbuf := bcd.Encode("12345")
170 if len(bcdbuf) == 0 {
171 t.Errorf("TestTbcdUnEven1: bcd Encode failed")
174 if bcdbuf[0] != 0x21 {
175 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x21 got 0x%x", bcdbuf[0])
178 if bcdbuf[1] != 0x43 {
179 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x43 got 0x%x", bcdbuf[1])
182 if bcdbuf[2] != 0xf5 {
183 t.Errorf("TestBcdEven: bcdbuf[2] expected 0xf5 got 0x%x", bcdbuf[2])
186 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
187 hex.Encode(hexdata, bcdbuf)
188 fmt.Printf("TestTbcdUnEven1: 12345 encoded data [%s]\n", string(hexdata))
190 bcdstr := bcd.Decode(bcdbuf)
191 if bcdstr != string("12345?") {
192 t.Errorf("TestTbcdUnEven1: bcd Decode failed: got %s expect %s", bcdstr, string("12345?"))
196 func TestTbcdUnEven2(t *testing.T) {
198 bcd := NewTbcd("0123456789?????f")
199 bcdbuf := bcd.Encode("12345f")
200 if len(bcdbuf) == 0 {
201 t.Errorf("TestTbcdUnEven2: bcd Encode failed")
204 if bcdbuf[0] != 0x21 {
205 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x21 got 0x%x", bcdbuf[0])
208 if bcdbuf[1] != 0x43 {
209 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x43 got 0x%x", bcdbuf[1])
212 if bcdbuf[2] != 0xf5 {
213 t.Errorf("TestBcdEven: bcdbuf[2] expected 0xf5 got 0x%x", bcdbuf[2])
216 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
217 hex.Encode(hexdata, bcdbuf)
218 fmt.Printf("TestTbcdUnEven2: 12345f encoded data [%s]\n", string(hexdata))
220 bcdstr := bcd.Decode(bcdbuf)
221 if bcdstr != string("12345f") {
222 t.Errorf("TestTbcdUnEven2: bcd Decode failed: got %s expect %s", bcdstr, string("12345f"))