blob: fa2c61b68ffb09f1b86d32ce9f63eaf5daf20493 [file] [log] [blame]
Juha Hyttinenff8dccd2019-12-10 14:34:07 +02001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
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==================================================================================
18*/
19
20package conv
21
22import (
Juha Hyttinen14f82732020-02-27 13:49:06 +020023 "encoding/hex"
24 "fmt"
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020025 "os"
26 "testing"
27)
28
Juha Hyttinen14f82732020-02-27 13:49:06 +020029//-----------------------------------------------------------------------------
30//
31//-----------------------------------------------------------------------------
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020032func TestMain(m *testing.M) {
33 code := m.Run()
34 os.Exit(code)
35}
36
Juha Hyttinen14f82732020-02-27 13:49:06 +020037//-----------------------------------------------------------------------------
38//
39//-----------------------------------------------------------------------------
40
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020041func TestBcdEven(t *testing.T) {
42
43 bcd := NewBcd("0123456789??????")
44 bcdbuf := bcd.Encode("123456")
45 if len(bcdbuf) == 0 {
46 t.Errorf("TestBcdEven: bcd Encode failed")
47 }
48
Juha Hyttinen14f82732020-02-27 13:49:06 +020049 if bcdbuf[0] != 0x12 {
50 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x12 got 0x%x", bcdbuf[0])
51 }
52
53 if bcdbuf[1] != 0x34 {
54 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x34 got 0x%x", bcdbuf[1])
55 }
56
57 if bcdbuf[2] != 0x56 {
58 t.Errorf("TestBcdEven: bcdbuf[2] expected 0x56 got 0x%x", bcdbuf[2])
59 }
60
61 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
62 hex.Encode(hexdata, bcdbuf)
63 fmt.Printf("TestBcdEven: 123456 encoded data [%s]\n", string(hexdata))
64
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020065 bcdstr := bcd.Decode(bcdbuf)
66 if bcdstr != string("123456") {
67 t.Errorf("TestBcdEven: bcd Decode failed: got %s expect %s", bcdstr, string("123456"))
68 }
69
70}
71
72func TestBcdUnEven1(t *testing.T) {
73
74 bcd := NewBcd("0123456789??????")
75 bcdbuf := bcd.Encode("12345")
76 if len(bcdbuf) == 0 {
77 t.Errorf("TestBcdUnEven1: bcd Encode failed")
78 }
79
Juha Hyttinen14f82732020-02-27 13:49:06 +020080 if bcdbuf[0] != 0x12 {
81 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x12 got 0x%x", bcdbuf[0])
82 }
83
84 if bcdbuf[1] != 0x34 {
85 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x34 got 0x%x", bcdbuf[1])
86 }
87
88 if bcdbuf[2] != 0x5f {
89 t.Errorf("TestBcdEven: bcdbuf[2] expected 0x5f got 0x%x", bcdbuf[2])
90 }
91
92 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
93 hex.Encode(hexdata, bcdbuf)
94 fmt.Printf("TestBcdUnEven1: 12345 encoded data [%s]\n", string(hexdata))
95
Juha Hyttinenff8dccd2019-12-10 14:34:07 +020096 bcdstr := bcd.Decode(bcdbuf)
97 if bcdstr != string("12345?") {
98 t.Errorf("TestBcdUnEven1: bcd Decode failed: got %s expect %s", bcdstr, string("12345?"))
99 }
100}
101
102func TestBcdUnEven2(t *testing.T) {
103
104 bcd := NewBcd("0123456789?????f")
105 bcdbuf := bcd.Encode("12345f")
106 if len(bcdbuf) == 0 {
107 t.Errorf("TestBcdUnEven2: bcd Encode failed")
108 }
109
Juha Hyttinen14f82732020-02-27 13:49:06 +0200110 if bcdbuf[0] != 0x12 {
111 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x12 got 0x%x", bcdbuf[0])
112 }
113
114 if bcdbuf[1] != 0x34 {
115 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x34 got 0x%x", bcdbuf[1])
116 }
117
118 if bcdbuf[2] != 0x5f {
119 t.Errorf("TestBcdEven: bcdbuf[2] expected 0x5f got 0x%x", bcdbuf[2])
120 }
121
122 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
123 hex.Encode(hexdata, bcdbuf)
124 fmt.Printf("TestBcdUnEven2: 12345f encoded data [%s]\n", string(hexdata))
125
Juha Hyttinenff8dccd2019-12-10 14:34:07 +0200126 bcdstr := bcd.Decode(bcdbuf)
127 if bcdstr != string("12345f") {
128 t.Errorf("TestBcdUnEven2: bcd Decode failed: got %s expect %s", bcdstr, string("12345f"))
129 }
130}
Juha Hyttinen14f82732020-02-27 13:49:06 +0200131
132//-----------------------------------------------------------------------------
133//
134//-----------------------------------------------------------------------------
135func TestTbcdEven(t *testing.T) {
136
137 bcd := NewTbcd("0123456789??????")
138 bcdbuf := bcd.Encode("123456")
139 if len(bcdbuf) == 0 {
140 t.Errorf("TestTbcdEven: bcd Encode failed")
141 }
142
143 if bcdbuf[0] != 0x21 {
144 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x21 got 0x%x", bcdbuf[0])
145 }
146
147 if bcdbuf[1] != 0x43 {
148 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x34 got 0x%x", bcdbuf[1])
149 }
150
151 if bcdbuf[2] != 0x65 {
152 t.Errorf("TestBcdEven: bcdbuf[2] expected 0x65 got 0x%x", bcdbuf[2])
153 }
154
155 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
156 hex.Encode(hexdata, bcdbuf)
157 fmt.Printf("TestTbcdEven: 123456 encoded data [%s]\n", string(hexdata))
158
159 bcdstr := bcd.Decode(bcdbuf)
160 if bcdstr != string("123456") {
161 t.Errorf("TestTbcdEven: bcd Decode failed: got %s expect %s", bcdstr, string("123456"))
162 }
163
164}
165
166func TestTbcdUnEven1(t *testing.T) {
167
168 bcd := NewTbcd("0123456789??????")
169 bcdbuf := bcd.Encode("12345")
170 if len(bcdbuf) == 0 {
171 t.Errorf("TestTbcdUnEven1: bcd Encode failed")
172 }
173
174 if bcdbuf[0] != 0x21 {
175 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x21 got 0x%x", bcdbuf[0])
176 }
177
178 if bcdbuf[1] != 0x43 {
179 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x43 got 0x%x", bcdbuf[1])
180 }
181
182 if bcdbuf[2] != 0xf5 {
183 t.Errorf("TestBcdEven: bcdbuf[2] expected 0xf5 got 0x%x", bcdbuf[2])
184 }
185
186 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
187 hex.Encode(hexdata, bcdbuf)
188 fmt.Printf("TestTbcdUnEven1: 12345 encoded data [%s]\n", string(hexdata))
189
190 bcdstr := bcd.Decode(bcdbuf)
191 if bcdstr != string("12345?") {
192 t.Errorf("TestTbcdUnEven1: bcd Decode failed: got %s expect %s", bcdstr, string("12345?"))
193 }
194}
195
196func TestTbcdUnEven2(t *testing.T) {
197
198 bcd := NewTbcd("0123456789?????f")
199 bcdbuf := bcd.Encode("12345f")
200 if len(bcdbuf) == 0 {
201 t.Errorf("TestTbcdUnEven2: bcd Encode failed")
202 }
203
204 if bcdbuf[0] != 0x21 {
205 t.Errorf("TestBcdEven: bcdbuf[0] expected 0x21 got 0x%x", bcdbuf[0])
206 }
207
208 if bcdbuf[1] != 0x43 {
209 t.Errorf("TestBcdEven: bcdbuf[1] expected 0x43 got 0x%x", bcdbuf[1])
210 }
211
212 if bcdbuf[2] != 0xf5 {
213 t.Errorf("TestBcdEven: bcdbuf[2] expected 0xf5 got 0x%x", bcdbuf[2])
214 }
215
216 hexdata := make([]byte, hex.EncodedLen(len(bcdbuf)))
217 hex.Encode(hexdata, bcdbuf)
218 fmt.Printf("TestTbcdUnEven2: 12345f encoded data [%s]\n", string(hexdata))
219
220 bcdstr := bcd.Decode(bcdbuf)
221 if bcdstr != string("12345f") {
222 t.Errorf("TestTbcdUnEven2: bcd Decode failed: got %s expect %s", bcdstr, string("12345f"))
223 }
224}