forked from sergiss/epc-tds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
337 lines (317 loc) · 12.5 KB
/
test.js
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* EPC Tag Data Standard
* 2021 Sergio S. - https://github.com/sergiss/epc-tds
*/
'use strict'
var tds = require("./index.js");
const ITERATIONS = 10000;
function test(t, n) {
let startTime = new Date().getTime();
t(n);
return new Date().getTime() - startTime;
}
let time;
function sgcn96Test(n) {
let epc, sgcn, tmp;
for(let i = 0; i < n; ++i) {
sgcn = tds.Utils.randomEan(13) + Math.floor(Math.random() * tds.Utils.getMaxValue(16));
epc = new tds.Sgcn96().setFilter(3).setPartition(6).setSgcn(sgcn);
epc = new tds.Sgcn96(epc.toHexString());
if(sgcn !== epc.getSgcn()) {
console.log(epc.toIdURI());
throw Error(`Sgcn96, expected GDTI: ${sgcn}, current: ${epc.getSgcn()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Sgcn96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
}
time = test(sgcn96Test, ITERATIONS);
console.log("Test Sgcn96 time: " + time);
function gdti96Test(n) {
let epc, gdti, tmp;
for(let i = 0; i < n; ++i) {
gdti = tds.Utils.randomEan(13) + Math.floor(Math.random() * tds.Utils.getMaxValue(16));
epc = new tds.Gdti96().setFilter(3).setPartition(6).setGdti(gdti);
epc = new tds.Gdti96(epc.toHexString());
if(gdti !== epc.getGdti()) {
console.log(epc.toIdURI());
throw Error(`Gdti96, expected GDTI: ${gdti}, current: ${epc.getGdti()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Gdti96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
}
time = test(gdti96Test, ITERATIONS);
console.log("Test Gdti96 time: " + time);
function gdti174Test(n) {
let epc, gdti, tmp;
for(let i = 0; i < n; ++i) {
gdti = tds.Utils.randomEan(13) + Math.floor(Math.random() * tds.Utils.getMaxValue(16));
epc = new tds.Gdti174().setFilter(3).setPartition(6).setGdti(gdti);
epc = new tds.Gdti174(epc.toHexString());
if(gdti !== epc.getGdti()) {
console.log(epc.toIdURI());
throw Error(`Gdti174, expected GDTI: ${gdti}, current: ${epc.getGdti()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Gdti174, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
}
time = test(gdti174Test, ITERATIONS);
console.log("Test Gdti174 time: " + time);
function cpi96Test(n) {
let epc, cpi, tmp;
for(let i = 0; i < n; ++i) {
cpi = String(Math.floor(Math.random() * 999999)).padStart(6, '0') + Math.floor(Math.random() * tds.Utils.getMaxValue(16));
epc = new tds.Cpi96().setFilter(3).setPartition(6).setCpi(cpi);
epc.setSerial(Math.floor(Math.random() * tds.Cpi96.MAX_SERIAL));
epc = new tds.Cpi96(epc.toHexString());
if(cpi !== epc.getCpi()) {
console.log(epc.toIdURI());
throw Error(`Cpi96, expected CPI: ${cpi}, current: ${epc.getCpi()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Cpi96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
}
time = test(cpi96Test, ITERATIONS);
console.log("Test Cpi96 time: " + time);
function gsrn96Test(n) {
let epc, gsrn, tmp;
for(let i = 0; i < n; ++i) {
gsrn = tds.Utils.randomEan(18);
epc = new tds.Gsrn96().setFilter(3).setPartition(6).setGsrn(gsrn);
epc = new tds.Gsrn96(epc.toHexString());
if(gsrn !== epc.getGsrn()) {
console.log(epc.toIdURI());
throw Error(`Gsrn96, expected GIAI: ${gsrn}, current: ${epc.getGsrn()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Gsrn96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
}
time = test(gsrn96Test, ITERATIONS);
console.log("Test Gsrn96 time: " + time);
function giai96Test(n) {
let epc, giai, tmp;
for(let i = 0; i < n; ++i) {
giai = String(Math.floor(Math.random() * 999999)).padStart(6, '0') + Math.floor(Math.random() * tds.Utils.getMaxValue(16));
epc = new tds.Giai96().setFilter(3).setPartition(6).setGiai(giai);
epc = new tds.Giai96(epc.toHexString());
if(giai !== epc.getGiai()) {
console.log(epc.toIdURI());
throw Error(`Giai96, expected GIAI: ${giai}, current: ${epc.getGiai()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Giai96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
}
time = test(giai96Test, ITERATIONS);
console.log("Test Giai96 time: " + time);
function giai202Test(n) {
let epc, giai, tmp;
for(let i = 0; i < n; ++i) {
giai = String(Math.floor(Math.random() * 999999)).padStart(6, '0') + Math.floor(Math.random() * tds.Utils.getMaxValue(16));
epc = new tds.Giai202().setFilter(3).setPartition(6).setGiai(giai);
epc = new tds.Giai202(epc.toHexString());
if(giai !== epc.getGiai()) {
console.log(giai)
console.log(epc.toHexString())
console.log(epc.toIdURI());
throw Error(`Giai202, expected GIAI: ${giai}, current: ${epc.getGiai()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Giai202, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
}
time = test(giai202Test, ITERATIONS);
console.log("Test Giai202 time: " + time);
function grai170Test(n) {
let epc, grai, tmp;
for(let i = 0; i < n; ++i) {
grai = tds.Utils.randomEan(13) + Math.floor(Math.random() * tds.Utils.getMaxValue(16));
epc = new tds.Grai170().setFilter(3).setPartition(6).setGrai(grai);
epc = new tds.Grai170(epc.toHexString());
if(grai !== epc.getGrai()) {
console.log(epc.toIdURI());
throw Error(`Grai170, expected GRAI: ${grai}, current: ${epc.getGrai()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Grai170, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
//console.log(epc.getGtin());
}
time = test(grai170Test, ITERATIONS);
console.log("Test Grai170 time: " + time);
function grai96Test(n) {
let epc, grai, tmp;
for(let i = 0; i < n; ++i) {
grai = tds.Utils.randomEan(13) + Math.floor(Math.random() * tds.Utils.getMaxValue(16));
epc = new tds.Grai96().setFilter(3).setPartition(6).setGrai(grai);
epc = new tds.Grai96(epc.toHexString());
if(grai !== epc.getGrai()) {
console.log(epc.toIdURI());
throw Error(`Grai96, expected GRAI: ${grai}, current: ${epc.getGrai()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Grai96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
//console.log(epc.getGtin());
}
time = test(grai96Test, ITERATIONS);
console.log("Test Grai96 time: " + time);
function sscc96Test(n) {
let epc, ean, tmp;
for(let i = 0; i < n; ++i) {
ean = tds.Utils.randomEan(18);
epc = new tds.Sscc96().setFilter(3).setPartition(6).setSscc(ean);
epc = new tds.Sscc96(epc.toHexString());
if(ean !== epc.getSscc()) {
throw Error(`Sscc96, expected SSCC: ${ean}, current: ${epc.getSscc()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Sscc96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
//console.log(epc.getGtin());
}
time = test(sscc96Test, ITERATIONS);
console.log("Test Sscc96 time: " + time);
function sgtin96Test(n) {
let epc, gtin, tmp;
for(let i = 0; i < n; ++i) {
gtin = tds.Utils.randomEan(14);
epc = new tds.Sgtin96().setFilter(3).setPartition(6).setGtin(gtin).setSerial(Math.floor(Math.random() * tds.Sgtin96.MAX_SERIAL));
epc = new tds.Sgtin96(epc.toHexString());
if(gtin !== epc.getGtin()) {
throw Error(`Sgtin96, expected GTIN: ${gtin}, current: ${epc.getGtin()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Sgtin96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
//console.log(epc.getGtin());
}
time = test(sgtin96Test, ITERATIONS);
console.log("Test Sgtin96 time: " + time);
function sgtin198Test(n) {
let epc, gtin, tmp;
for(let i = 0; i < n; ++i) {
gtin = tds.Utils.randomEan(14);
epc = new tds.Sgtin198().setFilter(3).setPartition(6).setGtin(gtin).setSerial(tds.Utils.randomHex(tds.Sgtin198.MAX_SERIAL_LEN));
epc = new tds.Sgtin198(epc.toHexString());
if(gtin !== epc.getGtin()) {
throw Error(`Sgtin198, expected GTIN: ${gtin}, current: ${epc.getGtin()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Sgtin198, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
//console.log(epc.getGtin());
}
time = test(sgtin198Test, ITERATIONS);
console.log("Test Sgtin198 time: " + time);
function sgln96Test(n) {
let epc, ean, tmp;
for(let i = 0; i < n; ++i) {
ean = tds.Utils.randomEan(13);
epc = new tds.Sgln96().setFilter(3).setPartition(6).setGln(ean).setExtension(Math.floor(Math.random() * tds.Sgln96.MAX_EXTENSION));
epc = new tds.Sgln96(epc.toHexString());
if(ean !== epc.getGln()) {
throw Error(`Sgln96, expected GLN: ${ean}, current: ${epc.getGln()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Sgln96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
//console.log(epc.getGtin());
}
time = test(sgln96Test, ITERATIONS);
console.log("Test Sgln96 time: " + time);
function sgln195Test(n) {
let epc, ean, tmp;
for(let i = 0; i < n; ++i) {
ean = tds.Utils.randomEan(13);
epc = new tds.Sgln195().setFilter(3).setPartition(6).setGln(ean).setExtension(tds.Utils.randomHex(tds.Sgln195.MAX_EXTENSION_LEN));
epc = new tds.Sgln195(epc.toHexString());
if(ean !== epc.getGln()) {
throw Error(`Sgln195, expected GLN: ${ean}, current: ${epc.getGln()}`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Sgln195, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
//console.log(epc.getGtin());
}
time = test(sgln195Test, ITERATIONS);
console.log("Test Sgln195 time: " + time);
function gid96Test(n) {
let epc, tmp;
for(let i = 0; i < n; ++i) {
let manager = Math.floor(Math.random() * tds.Gid96.MAX_MANAGER);
let clazz = Math.floor(Math.random() * tds.Gid96.MAX_CLASS);
let serial = Math.floor(Math.random() * tds.Gid96.MAX_SERIAL);
epc = new tds.Gid96().setManager(manager).setClass (clazz).setSerial (serial);
epc = new tds.Gid96(epc.toHexString());
if(manager !== epc.getManager() || clazz !== epc.getClass() || serial != epc.getSerial()) {
throw Error(`Gid96, expected: [${manager}, ${clazz}, ${serial}] current: [${epc.getManager()}, ${epc.getClass()}, ${epc.getSerial()}]`);
}
const uri = epc.toTagURI();
tmp = tds.fromTagURI(uri);
if(uri !== tmp.toTagURI()) {
throw Error(`Gid96, expected TAG URI: ${uri}, current: ${tmp.toTagURI()}`);
}
}
//console.log(epc.toHexString())
}
time = test(gid96Test, ITERATIONS);
console.log("Test Gid96 time: " + time);
console.log("*** Test completed successfully! ***");