-
-
Notifications
You must be signed in to change notification settings - Fork 616
Expand file tree
/
Copy pathtests.rs
More file actions
555 lines (447 loc) · 15.4 KB
/
tests.rs
File metadata and controls
555 lines (447 loc) · 15.4 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#![allow(clippy::redundant_clone)]
use std::hash::{BuildHasher, BuildHasherDefault, Hash};
use crate::{
CodePoint, CommonJsStringBuilder, JsStr, JsString, JsStringKind, Latin1JsStringBuilder,
StaticJsStrings, StaticString, Utf16JsStringBuilder,
};
use rustc_hash::FxHasher;
fn hash_value<T: Hash>(value: &T) -> u64 {
BuildHasherDefault::<FxHasher>::default().hash_one(value)
}
const fn ascii_to_utf16<const LEN: usize>(ascii: &[u8; LEN]) -> [u16; LEN] {
let mut array = [0; LEN];
let mut i = 0;
while i < LEN {
array[i] = ascii[i] as u16;
i += 1;
}
array
}
#[test]
fn empty() {
let s = StaticJsStrings::EMPTY_STRING;
assert_eq!(&s, &[]);
}
#[test]
fn refcount() {
let x = JsString::from("Hello world");
assert_eq!(x.refcount(), Some(1));
{
let y = x.clone();
assert_eq!(x.refcount(), Some(2));
assert_eq!(y.refcount(), Some(2));
{
let z = y.clone();
assert_eq!(x.refcount(), Some(3));
assert_eq!(y.refcount(), Some(3));
assert_eq!(z.refcount(), Some(3));
}
assert_eq!(x.refcount(), Some(2));
assert_eq!(y.refcount(), Some(2));
}
assert_eq!(x.refcount(), Some(1));
}
#[test]
fn static_refcount() {
let x = StaticJsStrings::EMPTY_STRING;
assert_eq!(x.refcount(), None);
{
let y = x.clone();
assert_eq!(x.refcount(), None);
assert_eq!(y.refcount(), None);
};
assert_eq!(x.refcount(), None);
}
#[test]
fn ptr_eq() {
let x = JsString::from("Hello");
let y = x.clone();
assert!(!x.is_static());
assert_eq!(x.ptr.addr(), y.ptr.addr());
let z = JsString::from("Hello");
assert_ne!(x.ptr.addr(), z.ptr.addr());
assert_ne!(y.ptr.addr(), z.ptr.addr());
}
#[test]
fn static_ptr_eq() {
let x = StaticJsStrings::EMPTY_STRING;
let y = x.clone();
assert!(x.is_static());
assert_eq!(x.ptr.addr(), y.ptr.addr());
let z = StaticJsStrings::EMPTY_STRING;
assert_eq!(x.ptr.addr(), z.ptr.addr());
assert_eq!(y.ptr.addr(), z.ptr.addr());
}
#[test]
fn as_str() {
const HELLO: &[u16] = &ascii_to_utf16(b"Hello");
let x = JsString::from(HELLO);
assert_eq!(&x, HELLO);
}
#[test]
fn hash() {
const HELLOWORLD: JsStr<'_> = JsStr::latin1("Hello World!".as_bytes());
let x = JsString::from(HELLOWORLD);
assert_eq!(x.as_str(), HELLOWORLD);
assert!(HELLOWORLD.is_latin1());
assert!(x.as_str().is_latin1());
let s_hash = hash_value(&HELLOWORLD);
let x_hash = hash_value(&x);
assert_eq!(s_hash, x_hash);
}
#[test]
fn concat() {
const Y: &[u16] = &ascii_to_utf16(b", ");
const W: &[u16] = &ascii_to_utf16(b"!");
let x = JsString::from("hello");
let z = JsString::from("world");
let xy = JsString::concat(x.as_str(), JsString::from(Y).as_str()).unwrap();
assert_eq!(&xy, &ascii_to_utf16(b"hello, "));
assert_eq!(xy.refcount(), Some(1));
let xyz = JsString::concat(xy.as_str(), z.as_str()).unwrap();
assert_eq!(&xyz, &ascii_to_utf16(b"hello, world"));
assert_eq!(xyz.refcount(), Some(1));
let xyzw = JsString::concat(xyz.as_str(), JsString::from(W).as_str()).unwrap();
assert_eq!(&xyzw, &ascii_to_utf16(b"hello, world!"));
assert_eq!(xyzw.refcount(), Some(1));
}
#[test]
fn trim_start_non_ascii_to_ascii() {
let s = "\u{2029}abc";
let x = JsString::from(s);
let y = x.trim_start();
assert_eq!(&y, s.trim_start());
}
#[test]
fn conversion_to_known_static_js_string() {
const JS_STR_U8: &JsStr<'_> = &JsStr::latin1("length".as_bytes());
const JS_STR_U16: &JsStr<'_> = &JsStr::utf16(&ascii_to_utf16(b"length"));
assert!(JS_STR_U8.is_latin1());
assert!(!JS_STR_U16.is_latin1());
assert_eq!(JS_STR_U8, JS_STR_U8);
assert_eq!(JS_STR_U16, JS_STR_U16);
assert_eq!(JS_STR_U8, JS_STR_U16);
assert_eq!(JS_STR_U16, JS_STR_U8);
assert_eq!(hash_value(JS_STR_U8), hash_value(JS_STR_U16));
let string = StaticJsStrings::get_string(JS_STR_U8);
assert!(string.is_some());
assert!(string.unwrap().as_str().is_latin1());
let string = StaticJsStrings::get_string(JS_STR_U16);
assert!(string.is_some());
assert!(string.unwrap().as_str().is_latin1());
}
#[test]
fn to_std_string_escaped() {
assert_eq!(
JsString::from("Hello, \u{1D49E} world!").to_std_string_escaped(),
"Hello, \u{1D49E} world!"
);
assert_eq!(
JsString::from("Hello, world!").to_std_string_escaped(),
"Hello, world!"
);
// 15 should not be escaped.
let unpaired_surrogates: [u16; 3] = [0xDC58, 0xD83C, 0x0015];
assert_eq!(
JsString::from(&unpaired_surrogates).to_std_string_escaped(),
"\\uDC58\\uD83C\u{15}"
);
}
#[test]
fn from_static_js_string() {
static STATIC_HELLO_WORLD: StaticString =
StaticString::new(JsStr::latin1("hello world".as_bytes()));
static STATIC_EMOJIS: StaticString = StaticString::new(JsStr::utf16(&[
0xD83C, 0xDFB9, 0xD83C, 0xDFB6, 0xD83C, 0xDFB5,
])); // 🎹🎶🎵
let latin1 = JsString::from_static(&STATIC_HELLO_WORLD);
let utf16 = JsString::from_static(&STATIC_EMOJIS);
// content compare
assert_eq!(latin1, "hello world");
assert_eq!(utf16, "🎹🎶🎵");
// refcount check
let clone = latin1.clone();
assert_eq!(clone, latin1);
let clone = utf16.clone();
assert_eq!(clone, utf16);
assert!(latin1.refcount().is_none());
assert!(utf16.refcount().is_none());
// `is_latin1` check
assert!(latin1.as_str().is_latin1());
assert!(!utf16.as_str().is_latin1());
}
#[test]
fn compare_static_and_dynamic_js_string() {
static STATIC_HELLO_WORLD: StaticString =
StaticString::new(JsStr::latin1("hello world".as_bytes()));
static STATIC_EMOJIS: StaticString = StaticString::new(JsStr::utf16(&[
0xD83C, 0xDFB9, 0xD83C, 0xDFB6, 0xD83C, 0xDFB5,
])); // 🎹🎶🎵
let static_latin1 = JsString::from_static(&STATIC_HELLO_WORLD);
let static_utf16 = JsString::from_static(&STATIC_EMOJIS);
let dynamic_latin1 = JsString::from(JsStr::latin1("hello world".as_bytes()));
let dynamic_utf16 = JsString::from(&[0xD83C, 0xDFB9, 0xD83C, 0xDFB6, 0xD83C, 0xDFB5]);
// content compare
assert_eq!(static_latin1, dynamic_latin1);
assert_eq!(static_utf16, dynamic_utf16);
// length check
assert_eq!(static_latin1.len(), dynamic_latin1.len());
assert_eq!(static_utf16.len(), dynamic_utf16.len());
// `is_static` check
assert!(static_latin1.is_static());
assert!(static_utf16.is_static());
assert!(!dynamic_latin1.is_static());
assert!(!dynamic_utf16.is_static());
}
#[test]
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::undocumented_unsafe_blocks)]
fn js_string_builder() {
let s = "2024年5月21日";
let utf16 = s.encode_utf16().collect::<Vec<_>>();
let s_utf16 = utf16.as_slice();
let ascii = "Lorem ipsum dolor sit amet";
let s_ascii = ascii.as_bytes();
let latin1_as_utf8_literal = "Déjà vu";
let s_latin1_literal: &[u8] = &[
b'D', 0xE9, /* é */
b'j', 0xE0, /* à */
b' ', b'v', b'u',
];
// latin1 builder -- test
// push ascii
let mut builder = Latin1JsStringBuilder::new();
for &code in s_ascii {
builder.push(code);
}
let s_builder = builder.build().unwrap_or_default();
assert_eq!(s_builder, ascii);
// push latin1
let mut builder = Latin1JsStringBuilder::new();
for &code in s_latin1_literal {
builder.push(code);
}
let s_builder = unsafe { builder.build_as_latin1() };
assert_eq!(
s_builder.to_std_string().unwrap_or_default(),
latin1_as_utf8_literal
);
// from_iter ascii
let s_builder = s_ascii
.iter()
.copied()
.collect::<Latin1JsStringBuilder>()
.build()
.unwrap_or_default();
assert_eq!(s_builder.to_std_string().unwrap_or_default(), ascii);
// from_iter latin1
let s_builder = unsafe {
s_latin1_literal
.iter()
.copied()
.collect::<Latin1JsStringBuilder>()
.build_as_latin1()
};
assert_eq!(
s_builder.to_std_string().unwrap_or_default(),
latin1_as_utf8_literal
);
// extend_from_slice ascii
let mut builder = Latin1JsStringBuilder::new();
builder.extend_from_slice(s_ascii);
let s_builder = builder.build().unwrap_or_default();
assert_eq!(s_builder.to_std_string().unwrap_or_default(), ascii);
// extend_from_slice latin1
let mut builder = Latin1JsStringBuilder::new();
builder.extend_from_slice(s_latin1_literal);
let s_builder = unsafe { builder.build_as_latin1() };
assert_eq!(
s_builder.to_std_string().unwrap_or_default(),
latin1_as_utf8_literal
);
// build from utf16 encoded string
let s_builder = s
.as_bytes()
.iter()
.copied()
.collect::<Latin1JsStringBuilder>()
.build();
assert_eq!(None, s_builder);
let s_builder = s_utf16
.iter()
.copied()
.map(|v| v as u8)
.collect::<Latin1JsStringBuilder>()
.build();
assert_eq!(None, s_builder);
// utf16 builder -- test
// push
let mut builder = Utf16JsStringBuilder::new();
for &code in s_utf16 {
builder.push(code);
}
let s_builder = builder.build();
assert_eq!(s_builder.to_std_string().unwrap_or_default(), s);
// from_iter
let s_builder = s_utf16
.iter()
.copied()
.collect::<Utf16JsStringBuilder>()
.build();
assert_eq!(s_builder.to_std_string().unwrap_or_default(), s);
// extend_from_slice
let mut builder = Utf16JsStringBuilder::new();
builder.extend_from_slice(s_utf16);
let s_builder = builder.build();
assert_eq!(s_builder.to_std_string().unwrap_or_default(), s);
}
#[test]
fn clone_builder() {
// latin1 builder -- test
let origin = Latin1JsStringBuilder::from(&b"0123456789"[..]);
let empty_origin = Latin1JsStringBuilder::new();
// clone == origin
let cloned = origin.clone();
assert_eq!(origin, cloned);
// clone_from == origin
let mut cloned_from = Latin1JsStringBuilder::new();
cloned_from.clone_from(&origin);
assert_eq!(origin, cloned_from);
// clone == origin(empty)
let cloned = empty_origin.clone();
assert_eq!(empty_origin, cloned);
// clone_from == origin(empty)
cloned_from.clone_from(&empty_origin);
assert!(cloned_from.capacity() > 0); // Should not be reallocated so the capacity is preserved.
assert_eq!(empty_origin, cloned_from);
// clone_from(empty) == origin(empty)
let mut cloned_from = Latin1JsStringBuilder::new();
cloned_from.clone_from(&empty_origin);
assert!(cloned_from.capacity() == 0);
assert_eq!(empty_origin, cloned_from);
// utf16 builder -- test
let s = "2024年5月21日";
let origin = Utf16JsStringBuilder::from(s.encode_utf16().collect::<Vec<_>>().as_slice());
let empty_origin = Utf16JsStringBuilder::new();
// clone == origin
let cloned = origin.clone();
assert_eq!(origin, cloned);
// clone_from == origin(empty)
let mut cloned_from = Utf16JsStringBuilder::new();
cloned_from.clone_from(&origin);
assert_eq!(origin, cloned_from);
// clone == origin(empty)
let cloned = empty_origin.clone();
assert_eq!(empty_origin, cloned);
// clone_from == origin(empty)
cloned_from.clone_from(&empty_origin);
assert!(cloned_from.capacity() > 0); // should not be reallocated so the capacity is preserved.
assert_eq!(empty_origin, cloned_from);
// clone_from(empty) == origin(empty)
let mut cloned_from = Utf16JsStringBuilder::new();
cloned_from.clone_from(&empty_origin);
assert!(cloned_from.capacity() == 0);
assert_eq!(empty_origin, cloned_from);
}
#[test]
fn common_js_string_builder() {
let utf16 = "2024年5月21日".encode_utf16().collect::<Vec<_>>();
let s_utf16 = utf16.as_slice();
let s = "Lorem ipsum dolor sit amet";
let js_str_utf16 = JsStr::utf16(s_utf16);
let js_str_ascii = JsStr::latin1(s.as_bytes());
let latin1_bytes = [
b'D', 0xE9, /* é */
b'j', 0xE0, /* à */
b' ', b'v', b'u',
];
let ch = '🎹';
let mut builder = CommonJsStringBuilder::with_capacity(10);
builder += ch;
builder += s;
builder += js_str_utf16;
builder += js_str_ascii;
builder += ch;
assert_eq!(builder.len(), 5);
let js_string = builder.build_from_utf16();
assert_eq!(
js_string,
"🎹Lorem ipsum dolor sit amet2024年5月21日Lorem ipsum dolor sit amet🎹"
);
let mut builder = CommonJsStringBuilder::new();
for b in latin1_bytes {
builder += b;
}
builder += s_utf16;
builder += ch;
let js_string = builder.build();
assert_eq!(
js_string.to_std_string().unwrap_or_default(),
"Déjà vu2024年5月21日🎹"
);
}
#[test]
fn code_points_optimization() {
// Test Latin1 optimization with extended Latin1 characters
let latin1_str = JsStr::latin1(b"Caf\xe9 na\xefve"); // "Café naïve" in Latin1 encoding
let latin1_points: Vec<CodePoint> = latin1_str.code_points().collect();
let expected_latin1: Vec<CodePoint> = "Café naïve".chars().map(CodePoint::Unicode).collect();
assert_eq!(latin1_points, expected_latin1);
// Test UTF-16 behavior unchanged (including non-ASCII)
let utf16_str = JsStr::utf16(&[
0x0043, 0x0061, 0x0066, 0x00E9, // "Café"
0x0020, // space
0x006E, 0x0061, 0x00EF, 0x0076, 0x0065, // "naïve"
]);
let utf16_points: Vec<CodePoint> = utf16_str.code_points().collect();
assert_eq!(latin1_points, utf16_points); // Same result for same content
}
#[test]
fn slice() {
let sliced = {
let base_str = JsString::from("Hello World");
assert_eq!(base_str.kind(), JsStringKind::Latin1Sequence);
base_str.slice(1, 5)
};
assert_eq!(sliced, JsString::from("ello"));
assert_eq!(sliced.kind(), JsStringKind::Slice);
let sliced2 = sliced.slice(1, 3);
drop(sliced);
assert_eq!(sliced2, JsString::from("ll"));
assert_eq!(sliced2.kind(), JsStringKind::Slice);
let sliced3 = sliced2.slice(0, 2);
drop(sliced2);
assert_eq!(sliced3, JsString::from("ll"));
assert_eq!(sliced3.kind(), JsStringKind::Slice);
let sliced4 = sliced3.slice(0, 2);
drop(sliced3);
assert_eq!(sliced4, JsString::from("ll"));
assert_eq!(sliced4.kind(), JsStringKind::Slice);
let sliced4 = sliced4.slice(0, 2);
assert_eq!(sliced4, JsString::from("ll"));
assert_eq!(sliced4.kind(), JsStringKind::Slice);
let sliced5 = sliced4.slice(1, 1);
assert_eq!(sliced5, JsString::from(""));
assert_eq!(sliced5.kind(), JsStringKind::Static);
assert_eq!(sliced5.slice(4, 4), StaticJsStrings::EMPTY_STRING);
}
#[test]
fn split() {
let base_str = JsString::from("Hello World");
assert_eq!(base_str.kind(), JsStringKind::Latin1Sequence);
let str1 = base_str.slice(0, 5);
let str2 = base_str.slice(6, base_str.len());
assert_eq!(str1, JsString::from("Hello"));
assert_eq!(str2, JsString::from("World"));
let str3 = str1.clone();
drop(str1);
assert_eq!(str3, JsString::from("Hello"));
drop(base_str);
assert_eq!(str3, JsString::from("Hello"));
}
#[test]
fn trim() {
// Very basic test for trimming. The extensive testing is done by `boa_engine`.
let base_str = JsString::from(" \u{000B} Hello World \t ");
assert_eq!(base_str.trim(), JsString::from("Hello World"));
}