deno.land / std@0.91.0 / encoding / hex_test.ts

نووسراو ببینە
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
// Ported from Go// https://github.com/golang/go/blob/go1.12.5/src/encoding/hex/hex.go// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertThrows } from "../testing/asserts.ts";
import { decode, decodedLen, decodeString, encode, encodedLen, encodeToString, errInvalidByte, errLength,} from "./hex.ts";
function toByte(s: string): number { return new TextEncoder().encode(s)[0];}
const testCases = [ // encoded(hex) / decoded(Uint8Array) ["", []], ["0001020304050607", [0, 1, 2, 3, 4, 5, 6, 7]], ["08090a0b0c0d0e0f", [8, 9, 10, 11, 12, 13, 14, 15]], ["f0f1f2f3f4f5f6f7", [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7]], ["f8f9fafbfcfdfeff", [0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]], ["67", Array.from(new TextEncoder().encode("g"))], ["e3a1", [0xe3, 0xa1]],];
const errCases = [ // encoded(hex) / error ["0", errLength()], ["zd4aa", errInvalidByte(toByte("z"))], ["d4aaz", errInvalidByte(toByte("z"))], ["30313", errLength()], ["0g", errInvalidByte(new TextEncoder().encode("g")[0])], ["00gg", errInvalidByte(new TextEncoder().encode("g")[0])], ["0\x01", errInvalidByte(new TextEncoder().encode("\x01")[0])], ["ffeed", errLength()],];
Deno.test({ name: "[encoding.hex] encodedLen", fn(): void { assertEquals(encodedLen(0), 0); assertEquals(encodedLen(1), 2); assertEquals(encodedLen(2), 4); assertEquals(encodedLen(3), 6); assertEquals(encodedLen(4), 8); },});
Deno.test({ name: "[encoding.hex] encode", fn(): void { { const srcStr = "abc"; const src = new TextEncoder().encode(srcStr); const dest = encode(src); assertEquals(src, new Uint8Array([97, 98, 99])); assertEquals(dest.length, 6); }
for (const [enc, dec] of testCases) { const src = new Uint8Array(dec as number[]); const dest = encode(src); assertEquals(dest.length, src.length * 2); assertEquals(new TextDecoder().decode(dest), enc); } },});
Deno.test({ name: "[encoding.hex] encodeToString", fn(): void { for (const [enc, dec] of testCases) { assertEquals(encodeToString(new Uint8Array(dec as number[])), enc); } },});
Deno.test({ name: "[encoding.hex] decodedLen", fn(): void { assertEquals(decodedLen(0), 0); assertEquals(decodedLen(2), 1); assertEquals(decodedLen(4), 2); assertEquals(decodedLen(6), 3); assertEquals(decodedLen(8), 4); },});
Deno.test({ name: "[encoding.hex] decode", fn(): void { // Case for decoding uppercase hex characters, since // Encode always uses lowercase. const extraTestcase = [ ["F8F9FAFBFCFDFEFF", [0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff]], ];
const cases = testCases.concat(extraTestcase);
for (const [enc, dec] of cases) { const src = new TextEncoder().encode(enc as string); const dest = decode(src); assertEquals(Array.from(dest), Array.from(dec as number[])); } },});
Deno.test({ name: "[encoding.hex] decodeString", fn(): void { for (const [enc, dec] of testCases) { const dst = decodeString(enc as string);
assertEquals(dec, Array.from(dst)); } },});
Deno.test({ name: "[encoding.hex] decode error", fn(): void { for (const [input, expectedErr] of errCases) { assertThrows( () => decode(new TextEncoder().encode(input as string)), Error, (expectedErr as Error).message, ); } },});
Deno.test({ name: "[encoding.hex] decodeString error", fn(): void { for (const [input, expectedErr] of errCases) { assertThrows( (): void => { decodeString(input as string); }, Error, (expectedErr as Error).message, ); } },});
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉