-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
165 lines (155 loc) · 4.26 KB
/
test.ts
File metadata and controls
165 lines (155 loc) · 4.26 KB
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
import { assertEquals, assertNotEquals } from "https://deno.land/std/testing/asserts.ts";
//import { BinaryToJSON } from "https://deno.land/x/binary_to_json@v0.1.0/mod.ts";
import { BinaryToJSON } from "./binary_to_json.ts";
Deno.test(
"constructor",
function(): void {
const buffer = new ArrayBuffer(4);
const arry = new Uint8Array(buffer);
arry[0] = 0x48;
arry[1] = 0x65;
arry[2] = 0xAC;
arry[3] = 0x6C;
const b2j = new BinaryToJSON();
assertNotEquals(null, b2j);
},
);
Deno.test(
"convert",
function():void {
const file = Deno.openSync("sample.dat");
const buffer = Deno.readAllSync(file);
Deno.close(file.rid);
const format = JSON.parse(Deno.readTextFileSync("sample_format.json"));
const b2j = new BinaryToJSON();
const data: any = b2j.convert(buffer, format);
console.log(data);
assertEquals(0x0A, data['state']);
assertEquals(0x01, data['infos'][0]['type']);
assertEquals(0x00, data['dat'][0]['type']);
},
);
Deno.test(
"convert repeat",
function():void {
const file = Deno.openSync("sample2.dat");
const buffer = Deno.readAllSync(file);
Deno.close(file.rid);
const format = JSON.parse(Deno.readTextFileSync("sample_format2.json"));
const b2j = new BinaryToJSON();
const data: any = b2j.convert(buffer, format);
console.log(data);
assertEquals(0x01, data['dat1'][0]['v']);
assertEquals(0x01, data['dat2'][0]['v']);
},
);
Deno.test(
"little endian",
function(): void {
const buffer = new ArrayBuffer(4);
const arry = new Uint8Array(buffer);
arry[0] = 0x0D;
arry[1] = 0x0C;
arry[2] = 0x0B;
arry[3] = 0x0A;
const format = [{"dat":4}];
const b2j = new BinaryToJSON();
const data: any = b2j.convert(arry, format, true);
console.log("0x" + data['dat'].toString(16));
assertEquals(0x0A0B0C0D, data['dat']);
},
);
Deno.test(
"big endian",
function(): void {
const buffer = new ArrayBuffer(5);
const arry = new Uint8Array(buffer);
arry[0] = 0x00;
arry[1] = 0x01;
arry[2] = 0x02;
arry[3] = 0x03;
const format = [{"dat":4}];
const b2j = new BinaryToJSON();
const data: any = b2j.convert(arry, format);
console.log("0x" + data['dat'].toString(16));
assertEquals(0x00010203, data['dat']);
},
);
Deno.test(
"convert array null",
function(): void {
const arry = new Uint8Array();
const format = [{"dat":4}];
const b2j = new BinaryToJSON();
const data: any = b2j.convert(arry, format);
console.log(data);
assertEquals({}, data);
},
);
Deno.test(
"read 5bytes",
function(): void {
const buffer = new ArrayBuffer(5);
const arry = new Uint8Array(buffer);
arry[0] = 0x00;
arry[1] = 0x01;
arry[2] = 0x02;
arry[3] = 0x03;
arry[4] = 0x04;
const format = [{"dat":5}];
const b2j = new BinaryToJSON();
const data: any = b2j.convert(arry, format);
console.log("0x" + data['dat'].toString(16));
assertEquals(0x0001020304, data['dat']);
},
);
Deno.test(
"read 8bytes",
function(): void {
const buffer = new ArrayBuffer(8);
const arry = new Uint8Array(buffer);
arry[0] = 0x00;
arry[1] = 0x01;
arry[2] = 0x02;
arry[3] = 0x03;
arry[4] = 0x04;
arry[5] = 0x05;
arry[6] = 0x06;
arry[7] = 0x07;
const format = [{"dat":8}];
const b2j = new BinaryToJSON();
const data: any = b2j.convert(arry, format);
console.log("0x" + data['dat'].toString(16));
assertEquals(0x0001020304050607, data['dat']);
},
);
Deno.test(
"complex dat",
function(): void {
/*
const file = Deno.openSync("XXX.DAT");
const buffer = Deno.readAllSync(file);
Deno.close(file.rid);
const format = JSON.parse(Deno.readTextFileSync("prct_format.json"));
const b2j = new BinaryToJSON();
const data: any = b2j.convert(buffer, format);
//console.log(data);
*/
},
);
Deno.test(
"skip bytes",
function(): void {
const buffer = new ArrayBuffer(4);
const arry = new Uint8Array(buffer);
arry[0] = 0x0D;
arry[1] = 0x0C;
arry[2] = 0x0B;
arry[3] = 0x0A;
const format = [{"__skip":2},{"dat":2}];
const b2j = new BinaryToJSON();
const data: any = b2j.convert(arry, format);
console.log("0x" + data['dat'].toString(16));
assertEquals(0x0B0A, data['dat']);
},
);