plmn id handling improved
[ric-plt/submgr.git] / e2ap / pkg / conv / bcd_test.go
1 /*
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
20 package conv
21
22 import (
23         "encoding/hex"
24         "fmt"
25         "os"
26         "testing"
27 )
28
29 //-----------------------------------------------------------------------------
30 //
31 //-----------------------------------------------------------------------------
32 func TestMain(m *testing.M) {
33         code := m.Run()
34         os.Exit(code)
35 }
36
37 //-----------------------------------------------------------------------------
38 //
39 //-----------------------------------------------------------------------------
40
41 func 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
49         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
65         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
72 func 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
80         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
96         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
102 func 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
110         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
126         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 }
131
132 //-----------------------------------------------------------------------------
133 //
134 //-----------------------------------------------------------------------------
135 func 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
166 func 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
196 func 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 }