-
Notifications
You must be signed in to change notification settings - Fork 21
/
string-view_test.ts
192 lines (165 loc) · 7.68 KB
/
string-view_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
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
import { StringView } from "../string-view.ts";
import { assertEquals } from "./test_deps.ts";
const { test } = Deno;
const Encoder = new TextEncoder();
const randomString = "qwertyasфы←😀ячсм";
test("[StringView.decode] decodes encoded string", () => {
const view = new StringView(Encoder.encode(randomString).buffer);
assertEquals(StringView.decode(view), randomString);
assertEquals(StringView.decode(view, 1, 3), "wer");
assertEquals(StringView.decode(view, 2, 6), "ertyas");
assertEquals(StringView.decode(view, 6, 21), "asфы←😀ячсм");
});
test("[StringView.decode] decodes long encoded strings with TextDecoder", () => {
const string = randomString.repeat(15);
const view = new StringView(Encoder.encode(string).buffer);
assertEquals(StringView.decode(view), string);
assertEquals(StringView.decode(view, 6, 399), string.slice(6));
});
test("[StringView.encode] encodes a string", () => {
const view = new StringView(new ArrayBuffer(30));
const written = StringView.encode(randomString, view);
assertEquals(StringView.decode(view, 0, written), randomString);
});
test("[StringView.encode] handles 3-byte strings", () => {
const view = new StringView(new ArrayBuffer(10));
const written = StringView.encode("—", view);
assertEquals(StringView.decode(view, 0, written), "—");
});
test("[StringView.encode] handles bad strings", () => {
const view = new StringView(new ArrayBuffer(10));
const written = StringView.encode("—", view);
assertEquals(StringView.decode(view, 0, written), "—");
});
test("[StringView#size] returns the amount UTF characters in the view", () => {
assertEquals(StringView.from("asdf").size, 4);
assertEquals(StringView.from("фыва").size, 4);
assertEquals(StringView.from("😀😀fooа😀←").size, 8);
});
test("[StringView.from] creates a StringView from a string", () => {
const stringView = StringView.from("foo");
assertEquals(stringView instanceof StringView, true);
assertEquals(stringView.byteLength, 3);
assertEquals(stringView.toString(), "foo");
});
test("[StringView.getLength] returns the size in bytes of a given string", () => {
assertEquals(StringView.getLength("asdf"), 4);
assertEquals(StringView.getLength("—"), 3);
assertEquals(StringView.getLength("фыва"), 8);
assertEquals(StringView.getLength("😀😀fooа😀←"), 20);
});
test("[StringView.encode] encodes a string into a limited size buffer", () => {
const view = new StringView(new ArrayBuffer(30));
const written = StringView.encode(randomString, view, 0, 8);
assertEquals(StringView.decode(view, 0, written), "qwertyas");
});
test("[StringView.encode] encodes long strings with TextEncoder", () => {
const string = randomString.repeat(15);
const view = new StringView(new ArrayBuffer(405));
const written = StringView.encode(string, view);
assertEquals(StringView.decode(view, 0, written), string);
});
test("[StringView.encode] encodes a long string into smaller buffer using TextEncoder", () => {
const string = randomString.repeat(15);
const view = new StringView(new ArrayBuffer(216));
const written = StringView.encode(string, view, 0, 216);
assertEquals(StringView.decode(view, 0, written), randomString.repeat(8));
});
test("[StringView#character] iterates over the characters in the StringView", () => {
const stringView = StringView.from("asфы←😀");
const chars = [];
for (const character of stringView.characters()) {
chars.push(character);
}
assertEquals(chars, ["a", "s", "ф", "ы", "←", "😀"]);
});
test("[StringView#charAt] returns a new string consisting of the single UTF character", () => {
const stringView = StringView.from("asфы←😀");
const chars = [];
for (let i = 0; i < stringView.size; i++) {
chars[i] = stringView.charAt(i);
}
assertEquals(chars, ["a", "s", "ф", "ы", "←", "😀"]);
});
test("[StringView#charAt] returns an empty string if the character is not found", () => {
const invalidString = StringView.from("");
assertEquals(invalidString.charAt(), "");
assertEquals(invalidString.charAt(1), "");
assertEquals(invalidString.charAt(10), "");
});
test("[StringView#replace] replaces a pattern with a replacement", () => {
const stringView = StringView.from("Vimessaid");
stringView
.replace(Encoder.encode("s"), Encoder.encode("x"))
.replace(Encoder.encode("d"), Encoder.encode("y"));
assertEquals(stringView.toString(), "Vimexxaiy");
});
test("[StringView#reverse] reverses the characters of the StringView in-place", () => {
const stringView = StringView.from("fooа😀←ц");
assertEquals(stringView.reverse().toString(), "ц←😀аoof");
});
test("[StringView#includes] checks whether the provided encoded sequence is found inside the view", () => {
const stringView = StringView.from("Vimesi");
assertEquals(stringView.includes(Encoder.encode("im")), true);
assertEquals(stringView.includes(Encoder.encode("x")), false);
});
test("[StringView#indexOf] returns the index of the first occurrence of the specified value", () => {
const stringView = StringView.from("Vimesi");
assertEquals(stringView.indexOf(Encoder.encode("im")), 1);
assertEquals(stringView.indexOf(Encoder.encode("Vi")), 0);
assertEquals(stringView.indexOf(Encoder.encode("Vimes")), 0);
assertEquals(stringView.indexOf(Encoder.encode("x")), -1);
assertEquals(stringView.indexOf(Encoder.encode("Vix")), -1);
assertEquals(stringView.indexOf(Encoder.encode("s")), 4);
assertEquals(stringView.indexOf(Encoder.encode("i")), 1);
assertEquals(stringView.indexOf(Encoder.encode("i"), 2), 5);
const array = new Uint8Array(
new Array(300).fill(0).map(() => (Math.random() * 128) | 0),
);
const longString = new StringView(array.buffer);
longString.setUint8(0, 97);
assertEquals(longString.byteLength, 300);
assertEquals(longString.indexOf(Encoder.encode("ё")), -1);
assertEquals(longString.indexOf(Encoder.encode("a")), 0);
});
test("[StringView#set] encodes a given string into the StringView", () => {
const stringView = new StringView(new ArrayBuffer(6));
stringView.set("qwerty");
assertEquals(stringView.toString(), "qwerty");
stringView.set("qwerty12");
assertEquals(stringView.toString(), "qwerty");
stringView.set("1234");
assertEquals(stringView.toString(), "1234");
});
test("[StringView#substring] returns a new string containing the specified part of the given string", () => {
const stringView = StringView.from(randomString);
assertEquals(stringView.substring(0, 6), "qwerty");
assertEquals(stringView.substring(6, 11), "asфы←");
assertEquals(stringView.substring(6, 10), "asфы");
assertEquals(stringView.substring(6, 13), "asфы←😀я");
assertEquals(stringView.substring(10, 12), "←😀");
});
test("[StringView#substring] returns an empty string if characters are not found", () => {
const invalidString = new StringView(
new Uint8Array([128, 155, 134]).buffer,
);
assertEquals(invalidString.substring(), "");
});
test("[StringView#toString] returns a string representation of the StringView", () => {
const stringView = StringView.from("foo");
assertEquals(stringView.toString(), "foo");
});
test("[StringView#toJSON] returns a string representation of the StringView", () => {
const stringView = StringView.from("foo");
assertEquals(JSON.stringify(stringView), '"foo"');
});
test("[StringView#trim] returns a StringView without trailing zeros", () => {
const stringView = new StringView(new ArrayBuffer(10));
assertEquals(stringView.byteLength, 10);
stringView.set("foo");
assertEquals(stringView.trim().byteLength, 3);
assertEquals(StringView.from("bar").trim().byteLength, 3);
});
test("[StringView.initialize] initializes a view class", () => {
assertEquals(StringView.initialize(), StringView);
});