-
Notifications
You must be signed in to change notification settings - Fork 0
/
tppi_test.go
261 lines (254 loc) · 5.43 KB
/
tppi_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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// ॐ भूर्भुवः स्वः
// तत्स॑वि॒तुर्वरे॑ण्यं॒
// भर्गो॑ दे॒वस्य॑ धीमहि।
// धियो॒ यो नः॑ प्रचो॒दया॑त्॥
//
//
// बोसजी के द्वारा रचित टिप्पी अधिलेखन प्रकृया।
// ================================
//
// एक सरल संचार सहायक और संलग्न तंत्र।
//
// ~~~~~~~~~~~~~~~~~~~~~~~
// एक रचनात्मक भारतीय उत्पाद।
// ~~~~~~~~~~~~~~~~~~~~~~~
//
//
// Sources
// --------
// https://github.com/boseji/go-tppi
//
//
// License
// --------
//
// SPDX: Apache-2.0
//
// Copyright (C) 2024 Abhijit Bose (aka. Boseji). All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX short identifier: Apache-2.0
package tppi
import (
"reflect"
"strings"
"testing"
)
func Test_Assemble(t *testing.T) {
tests := []struct {
name string
args []string
wantS string
}{
{
name: "Basic test of Assembly",
args: []string{
"P1",
"P2",
},
wantS: "~|P1|P2|~",
},
{
name: "Assemble string containing special chars",
args: []string{
"P1|",
"P~2",
},
wantS: "~|P1\\x7C|P~2|~",
},
{
name: "Assemble string containing multiple special chars",
args: []string{
"Pa~rt1|",
"P|+art~2",
},
wantS: "~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
},
{
name: "Blank contents",
args: []string{},
wantS: "~||~",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Assemble(tt.args...)
if strings.Compare(got, tt.wantS) != 0 {
t.Errorf("error in values got %v want %v", got, tt.wantS)
}
})
}
}
func Test_Disassemble(t *testing.T) {
tests := []struct {
name string
args string
wantSa []string
}{
{
name: "Basic test of Assembly",
args: "~|P1|P2|~",
wantSa: []string{
"P1",
"P2",
},
},
{
name: "Assemble string containing special chars",
args: "~|P1\\x7C|P~2|~",
wantSa: []string{
"P1|",
"P~2",
},
},
{
name: "Assemble string containing multiple special chars",
args: "~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
wantSa: []string{
"Pa~rt1|",
"P|+art~2",
},
},
{
name: "Blank contents",
args: "",
wantSa: []string{""},
},
{
name: "Blank packet",
args: "~||~",
wantSa: []string{""},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Disassemble(tt.args)
if !reflect.DeepEqual(got, tt.wantSa) {
t.Errorf("error in values got %v want %v", got, tt.wantSa)
}
})
}
}
func Test_ValidPacket(t *testing.T) {
tests := []struct {
name string
arg string
wantErr bool
}{
{
name: "Basic Single Packet",
arg: "~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
},
{
name: "Dual Packets",
arg: "~|P1\\x7C|P~2|~+~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
},
{
name: "Error Single Packet1",
arg: "~Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
wantErr: true,
},
{
name: "Error Single Packet2",
arg: "~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|",
wantErr: true,
},
{
name: "Error Single Packet3",
arg: "~|Pa~rt1\\x7C|~P+\\x2Bart~2|~",
wantErr: true,
},
{
name: "Error Single Packet4",
arg: "~|Pa~rt1\\x7C+P\\x7C\\x2Bart~2|~",
wantErr: true,
},
{
name: "Error Single Packet5",
arg: "~|Pa~rt1\\x7C|~~|+~|P\\x7C\\x2Bart~2|~",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidPacket(tt.arg)
if (err != nil) != tt.wantErr {
t.Errorf("error ValidPacket() got %v want error %v",
err, tt.wantErr)
}
})
}
}
func Test_PacketJoin(t *testing.T) {
tests := []struct {
name string
args []string
wantS string
}{
{
name: "Join a single packet",
args: []string{
"~||~",
},
wantS: "~||~",
},
{
name: "Join two packet",
args: []string{
"~||~",
"~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
},
wantS: "~||~+~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := PacketJoin(tt.args...)
if strings.Compare(got, tt.wantS) != 0 {
t.Errorf("error in values got %v want %v", got, tt.wantS)
}
})
}
}
func Test_PacketSplit(t *testing.T) {
tests := []struct {
name string
arg string
wantSa []string
}{
{
name: "Join a single packet",
arg: "~||~",
wantSa: []string{
"~||~",
},
},
{
name: "Join two packet",
arg: "~||~+~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
wantSa: []string{
"~||~",
"~|Pa~rt1\\x7C|P\\x7C\\x2Bart~2|~",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := SplitPacket(tt.arg)
if !reflect.DeepEqual(got, tt.wantSa) {
t.Errorf("error in values got %v want %v", got, tt.wantSa)
}
})
}
}