bytestr 0.3.1

A utility provides a cheaply cloneable and sliceable immutable string.
Documentation
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
use crate::ByteStr;
use alloc::{borrow::Cow, format, string::String, vec, vec::Vec};

#[test]
fn test_new() {
    let bs = ByteStr::new();
    assert_eq!(bs.as_str(), "");
    assert!(bs.is_empty());
}

#[test]
fn test_from_static() {
    let bs = ByteStr::from_static("hello");
    assert_eq!(bs.as_str(), "hello");
    assert_eq!(bs.len(), 5);
}

#[test]
fn test_from_utf8_valid() {
    let bytes = b"hello world".to_vec();
    let bs = ByteStr::from_utf8(bytes).unwrap();
    assert_eq!(bs.as_str(), "hello world");
}

#[test]
fn test_from_utf8_invalid() {
    let invalid_bytes = vec![0xff, 0xfe, 0xfd];
    let result = ByteStr::from_utf8(invalid_bytes);
    assert!(result.is_err());
}

#[test]
fn test_from_string() {
    let s = String::from("test string");
    let bs = ByteStr::from(s);
    assert_eq!(bs.as_str(), "test string");
}

#[test]
fn test_from_str() {
    let bs = ByteStr::from("another test");
    assert_eq!(bs.as_str(), "another test");
}

#[test]
fn test_clone() {
    let bs1 = ByteStr::from("clone test");
    let bs2 = bs1.clone();
    assert_eq!(bs1, bs2);
    assert_eq!(bs1.as_str(), bs2.as_str());
}

#[test]
fn test_truncate() {
    let mut bs = ByteStr::from("hello world");
    bs.truncate(5);
    assert_eq!(bs.as_str(), "hello");
}

#[test]
fn test_truncate_utf8_boundary() {
    let mut bs = ByteStr::from("hello δΈ–η•Œ");
    bs.truncate(6);
    assert_eq!(bs.as_str(), "hello ");
}

#[test]
#[should_panic(expected = "assertion failed: self.deref().is_char_boundary(len)")]
fn test_truncate_invalid_boundary() {
    let mut bs = ByteStr::from("hello δΈ–η•Œ");
    // This should panic because it's not on a UTF-8 boundary
    bs.truncate(7);
}

#[test]
fn test_slice_ref() {
    let bs = ByteStr::from("hello world");
    let original_str = bs.as_str();
    let world_slice = &original_str[6..11]; // "world"
    let slice = bs.slice_ref(world_slice);
    assert_eq!(slice.as_str(), "world");
}

#[test]
fn test_clear() {
    let mut bs = ByteStr::from("clear me");
    assert!(!bs.is_empty());
    bs.clear();
    assert!(bs.is_empty());
    assert_eq!(bs.as_str(), "");
}

#[test]
fn test_into_bytes() {
    let bs = ByteStr::from("test bytes");
    let bytes = bs.into_bytes();
    assert_eq!(bytes.as_ref(), b"test bytes");
}

#[test]
fn test_debug_display() {
    let bs = ByteStr::from("debug test");
    assert_eq!(format!("{bs:?}"), "\"debug test\"");
    assert_eq!(format!("{bs}"), "debug test");
}

#[test]
fn test_equality_with_str() {
    let bs = ByteStr::from("equality test");
    assert_eq!(bs, "equality test");
    assert_eq!("equality test", bs);
}

#[test]
fn test_equality_with_string() {
    let bs = ByteStr::from("string test");
    let s = String::from("string test");
    assert_eq!(bs, s);
    assert_eq!(s, bs);
}

#[test]
fn test_equality_with_cow() {
    let bs = ByteStr::from("cow test");
    let cow_borrowed = Cow::Borrowed("cow test");
    let cow_owned = Cow::Owned(String::from("cow test"));

    assert_eq!(bs, cow_borrowed);
    assert_eq!(cow_borrowed, bs);
    assert_eq!(bs, cow_owned);
    assert_eq!(cow_owned, bs);
}

#[test]
fn test_as_ref_str() {
    let bs = ByteStr::from("as_ref test");
    let s: &str = bs.as_ref();
    assert_eq!(s, "as_ref test");
}

#[test]
fn test_as_ref_bytes() {
    let bs = ByteStr::from("bytes test");
    let bytes: &[u8] = bs.as_ref();
    assert_eq!(bytes, b"bytes test");
}

#[test]
fn test_borrow() {
    use core::borrow::Borrow;
    let bs = ByteStr::from("borrow test");
    let s: &str = bs.borrow();
    assert_eq!(s, "borrow test");
}

#[test]
fn test_deref() {
    let bs = ByteStr::from("deref test");
    assert_eq!(bs.len(), 10);
    assert!(bs.starts_with("deref"));
    assert!(bs.ends_with("test"));
}

#[test]
fn test_from_str_trait() {
    use core::str::FromStr;
    let bs = ByteStr::from_str("fromstr test").unwrap();
    assert_eq!(bs.as_str(), "fromstr test");
}

#[test]
fn test_default() {
    let bs = ByteStr::default();
    assert_eq!(bs, ByteStr::new());
    assert!(bs.is_empty());
}

#[test]
fn test_ord_and_partial_ord() {
    let bs1 = ByteStr::from("apple");
    let bs2 = ByteStr::from("banana");
    let bs3 = ByteStr::from("apple");

    assert!(bs1 < bs2);
    assert!(bs2 > bs1);
    assert_eq!(bs1, bs3);
    assert!(bs1 <= bs3);
    assert!(bs1 >= bs3);
}

#[test]
fn test_hash() {
    use alloc::collections::BTreeSet;

    let mut set = BTreeSet::new();
    let bs1 = ByteStr::from("hash test");
    let bs2 = ByteStr::from("hash test");
    let bs3 = ByteStr::from("different");

    set.insert(bs1);
    assert!(!set.insert(bs2)); // Should return false as it's already in the set
    assert!(set.insert(bs3)); // Should return true as it's different
}

#[test]
fn test_empty_operations() {
    let mut bs = ByteStr::new();
    assert!(bs.is_empty());
    assert_eq!(bs.len(), 0);
    assert_eq!(bs.as_str(), "");

    bs.clear();
    assert!(bs.is_empty());
}

#[test]
fn test_unicode_support() {
    let bs = ByteStr::from("Hello, δΈ–η•Œ! πŸ¦€");
    assert_eq!(bs.as_str(), "Hello, δΈ–η•Œ! πŸ¦€");
    assert!(bs.contains("δΈ–η•Œ"));
    assert!(bs.contains("πŸ¦€"));
}

#[test]
fn test_slice_ref_edge_cases() {
    let bs = ByteStr::from("hello");
    let original_str = bs.as_str();

    // Test slicing the entire string
    let full_slice = bs.slice_ref(original_str);
    assert_eq!(full_slice.as_str(), "hello");

    // Test slicing first character
    let first_char = bs.slice_ref(&original_str[0..1]);
    assert_eq!(first_char.as_str(), "h");

    // Test slicing last character
    let last_char = bs.slice_ref(&original_str[4..5]);
    assert_eq!(last_char.as_str(), "o");
}

// ============================================================================
// SAFETY TESTS - Testing unsafe code blocks for memory safety and correctness
// ============================================================================

#[test]
fn test_from_utf8_unchecked_safety() {
    // Test that from_utf8_unchecked with valid UTF-8 works correctly
    let valid_bytes = bytes::Bytes::from("Hello, δΈ–η•Œ! πŸ¦€");
    let bs = unsafe { ByteStr::from_utf8_unchecked(valid_bytes) };
    assert_eq!(bs.as_str(), "Hello, δΈ–η•Œ! πŸ¦€");

    // Test with empty bytes
    let empty_bytes = bytes::Bytes::new();
    let empty_bs = unsafe { ByteStr::from_utf8_unchecked(empty_bytes) };
    assert_eq!(empty_bs.as_str(), "");
    assert!(empty_bs.is_empty());
}

#[test]
fn test_from_static_safety() {
    // Test from_static with various static strings
    let bs1 = ByteStr::from_static("");
    assert_eq!(bs1.as_str(), "");

    let bs2 = ByteStr::from_static("ASCII only");
    assert_eq!(bs2.as_str(), "ASCII only");

    let bs3 = ByteStr::from_static("Unicode: δΈ–η•Œ πŸ¦€");
    assert_eq!(bs3.as_str(), "Unicode: δΈ–η•Œ πŸ¦€");

    // Verify that from_static strings can be used safely
    assert!(bs3.contains("δΈ–η•Œ"));
    assert!(bs3.contains("πŸ¦€"));
}

#[test]
fn test_as_str_safety() {
    // Test that as_str() always returns valid UTF-8
    let test_cases = [
        "",
        "ASCII",
        "δΈ–η•Œ",
        "πŸ¦€",
        "Mixed: ASCII δΈ–η•Œ πŸ¦€",
        "Very long string with various characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 δΈ–η•Œ πŸ¦€πŸ¦€πŸ¦€",
    ];

    for case in &test_cases {
        let bs = ByteStr::from(*case);
        let retrieved = bs.as_str();
        assert_eq!(retrieved, *case);

        // Verify that the returned &str is valid UTF-8
        assert!(core::str::from_utf8(retrieved.as_bytes()).is_ok());
    }
}

#[test]
fn test_as_bytes_mut_safety() {
    let mut bs = ByteStr::from("test string");

    // Test that we can safely access the mutable bytes
    unsafe {
        let bytes_mut = bs.as_bytes_mut();
        // Just check that we can read from it safely
        assert_eq!(bytes_mut.len(), 11);

        // The bytes should represent the same string
        assert_eq!(bytes_mut.as_ref(), b"test string");
    }

    // After the unsafe block, the ByteStr should still be valid
    assert_eq!(bs.as_str(), "test string");
}

#[test]
fn test_truncate_safety_with_utf8_boundaries() {
    // Test truncate with various UTF-8 boundary conditions
    let mut bs1 = ByteStr::from("Hello, δΈ–η•Œ!");

    // Truncate at ASCII boundary
    bs1.truncate(7); // "Hello, "
    assert_eq!(bs1.as_str(), "Hello, ");

    // Test with emoji
    let mut bs2 = ByteStr::from("πŸ¦€πŸ¦€πŸ¦€");
    bs2.truncate(4); // One crab emoji (4 bytes)
    assert_eq!(bs2.as_str(), "πŸ¦€");

    // Test truncating to empty
    let mut bs3 = ByteStr::from("test");
    bs3.truncate(0);
    assert_eq!(bs3.as_str(), "");
    assert!(bs3.is_empty());
}

#[test]
fn test_slice_ref_safety() {
    let original = "Hello, δΈ–η•Œ! πŸ¦€";
    let bs = ByteStr::from(original);
    let original_str = bs.as_str();

    // Test various safe slices
    let test_slices = [
        &original_str[0..0],                  // Empty slice at start
        &original_str[0..5],                  // "Hello"
        &original_str[7..10],                 // "δΈ–"
        &original_str[10..13],                // "η•Œ"
        &original_str[15..19],                // "πŸ¦€"
        &original_str[0..original_str.len()], // Full string
    ];

    for slice in &test_slices {
        let sliced_bs = bs.slice_ref(slice);
        assert_eq!(sliced_bs.as_str(), *slice);

        // Verify the sliced ByteStr is still valid UTF-8
        assert!(core::str::from_utf8(sliced_bs.as_str().as_bytes()).is_ok());
    }
}

#[test]
fn test_memory_layout_consistency() {
    // Test that ByteStr maintains consistent memory layout
    let original_string = "Memory safety test πŸ”’";
    let bs = ByteStr::from(original_string);

    // The string content should be identical
    assert_eq!(bs.as_str(), original_string);

    // The byte representation should be identical
    assert_eq!(bs.as_str().as_bytes(), original_string.as_bytes());

    // Test after cloning
    let cloned = bs.clone();
    assert_eq!(cloned.as_str(), original_string);
    assert_eq!(cloned.as_str().as_bytes(), original_string.as_bytes());

    // Test after conversion back to bytes
    let bytes = bs.into_bytes();
    assert_eq!(bytes.as_ref(), original_string.as_bytes());
}

#[test]
fn test_utf8_validation_consistency() {
    // Test that all paths through the API maintain UTF-8 validity
    let test_strings = [
        "",
        "a",
        "Hello",
        "δΈ–",
        "δΈ–η•Œ",
        "πŸ¦€",
        "πŸ¦€πŸ¦€πŸ¦€",
        "Mixed: Hello δΈ–η•Œ πŸ¦€!",
    ];

    for test_str in &test_strings {
        // Test from_utf8 path
        let bs1 = ByteStr::from_utf8(test_str.as_bytes()).unwrap();
        assert_eq!(bs1.as_str(), *test_str);

        // Test from string path
        let bs2 = ByteStr::from(*test_str);
        assert_eq!(bs2.as_str(), *test_str);

        // Test from static path
        let bs3 = ByteStr::from_static(test_str);
        assert_eq!(bs3.as_str(), *test_str);

        // All should be equal
        assert_eq!(bs1, bs2);
        assert_eq!(bs2, bs3);
        assert_eq!(bs1, bs3);
    }
}

#[test]
fn test_concurrent_access_safety() {
    extern crate std;
    use alloc::sync::Arc;
    use std::thread;

    let test_string = "Concurrent access test πŸš€";
    let expected_len = test_string.len(); // Get the actual byte length
    let bs = Arc::new(ByteStr::from(test_string));
    let mut handles = vec![];

    // Spawn multiple threads that read from the same ByteStr
    for i in 0..10 {
        let bs_clone = Arc::clone(&bs);
        let handle = thread::spawn(move || {
            for _ in 0..100 {
                // These operations should be safe to perform concurrently
                assert_eq!(bs_clone.len(), expected_len);
                assert!(bs_clone.contains("Concurrent"));
                assert!(bs_clone.contains("πŸš€"));
                assert_eq!(bs_clone.as_str(), "Concurrent access test πŸš€");

                // Test slicing
                let original_str = bs_clone.as_str();
                let slice = bs_clone.slice_ref(&original_str[0..10]);
                assert_eq!(slice.as_str(), "Concurrent");
            }
            i
        });
        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }
}

#[test]
fn test_zero_copy_guarantees() {
    // Test that operations that should be zero-copy actually are
    let original = "Zero copy test";
    let bs = ByteStr::from(original);

    // Cloning should not change the content
    let cloned = bs.clone();
    assert_eq!(bs.as_str(), cloned.as_str());

    // Slicing should not change the content
    let original_str = bs.as_str();
    let full_slice = bs.slice_ref(original_str);
    assert_eq!(bs.as_str(), full_slice.as_str());

    // Converting to bytes and back (conceptually)
    let bytes = bs.clone().into_bytes();
    let bs_from_bytes = ByteStr::from_utf8(bytes).unwrap();
    assert_eq!(bs.as_str(), bs_from_bytes.as_str());
}

#[test]
fn test_index_trait() {
    let bs = ByteStr::from("Hello, world!");

    // Test different range types
    assert_eq!(&bs[..], "Hello, world!"); // RangeFull
    assert_eq!(&bs[0..5], "Hello"); // Range
    assert_eq!(&bs[7..], "world!"); // RangeFrom
    assert_eq!(&bs[..5], "Hello"); // RangeTo
    assert_eq!(&bs[..=4], "Hello"); // RangeToInclusive
}

#[test]
fn test_index_trait_unicode() {
    let bs = ByteStr::from("Hello, δΈ–η•Œ!");

    // Test with Unicode strings
    assert_eq!(&bs[..], "Hello, δΈ–η•Œ!");
    assert_eq!(&bs[0..7], "Hello, ");
    assert_eq!(&bs[7..10], "δΈ–");
    assert_eq!(&bs[10..13], "η•Œ");
}

#[test]
#[should_panic(expected = "byte index 8 is not a char boundary")]
fn test_index_trait_panic_on_invalid_boundary() {
    let bs = ByteStr::from("Hello, δΈ–η•Œ!");
    // This should panic because 8 is not on a UTF-8 boundary
    let _ = &bs[8..];
}

#[test]
fn test_capacity() {
    let bs = ByteStr::from("Hello, world!");

    // Capacity should be at least as large as length
    assert!(bs.capacity() >= bs.len());

    let empty = ByteStr::new();
    assert_eq!(empty.capacity(), 0);
}

// UTF-16 related tests
#[test]
fn test_from_utf16_valid_ascii() {
    let utf16: Vec<u16> = "Hello, world!".encode_utf16().collect();
    let bs = ByteStr::from_utf16(&utf16).unwrap();
    assert_eq!(bs.as_str(), "Hello, world!");
}

#[test]
fn test_from_utf16_valid_unicode() {
    let utf16: Vec<u16> = "Hello, δΈ–η•Œ! πŸ¦€".encode_utf16().collect();
    let bs = ByteStr::from_utf16(&utf16).unwrap();
    assert_eq!(bs.as_str(), "Hello, δΈ–η•Œ! πŸ¦€");
}

#[test]
fn test_from_utf16_empty() {
    let empty_utf16: Vec<u16> = vec![];
    let bs = ByteStr::from_utf16(&empty_utf16).unwrap();
    assert!(bs.is_empty());
    assert_eq!(bs.as_str(), "");
}

#[test]
fn test_from_utf16_invalid() {
    // High surrogate without low surrogate
    let invalid_utf16 = vec![0xD800, 0x0041]; // High surrogate + 'A'
    let result = ByteStr::from_utf16(&invalid_utf16);
    assert!(result.is_err());
}

#[test]
fn test_from_utf16_invalid_lone_high_surrogate() {
    // Lone high surrogate at end
    let invalid_utf16 = vec![0x0041, 0xD800]; // 'A' + lone high surrogate
    let result = ByteStr::from_utf16(&invalid_utf16);
    assert!(result.is_err());
}

#[test]
fn test_from_utf16_invalid_lone_low_surrogate() {
    // Lone low surrogate
    let invalid_utf16 = vec![0xDC00, 0x0041]; // Lone low surrogate + 'A'
    let result = ByteStr::from_utf16(&invalid_utf16);
    assert!(result.is_err());
}

#[test]
fn test_from_utf16_surrogate_pairs() {
    // Valid surrogate pair (U+1F980 - crab emoji πŸ¦€)
    let utf16 = vec![0xD83E, 0xDD80]; // High surrogate + low surrogate for πŸ¦€
    let bs = ByteStr::from_utf16(&utf16).unwrap();
    assert_eq!(bs.as_str(), "πŸ¦€");
}

#[test]
fn test_from_utf16_lossy_valid() {
    let utf16: Vec<u16> = "Hello, world!".encode_utf16().collect();
    let bs = ByteStr::from_utf16_lossy(&utf16);
    assert_eq!(bs.as_str(), "Hello, world!");
}

#[test]
fn test_from_utf16_lossy_invalid() {
    // High surrogate without low surrogate - should be replaced with replacement char
    let invalid_utf16 = vec![0xD800, 0x0041]; // High surrogate + 'A'
    let bs = ByteStr::from_utf16_lossy(&invalid_utf16);

    // Should contain replacement character and 'A'
    assert!(bs.as_str().contains('\u{FFFD}'));
    assert!(bs.as_str().contains('A'));
}

#[test]
fn test_from_utf16_lossy_empty() {
    let empty_utf16: Vec<u16> = vec![];
    let bs = ByteStr::from_utf16_lossy(&empty_utf16);
    assert!(bs.is_empty());
    assert_eq!(bs.as_str(), "");
}

#[test]
fn test_from_utf16_lossy_multiple_invalid() {
    // Multiple invalid sequences
    let invalid_utf16 = vec![0xD800, 0xD800, 0x0041]; // Two high surrogates + 'A'
    let bs = ByteStr::from_utf16_lossy(&invalid_utf16);

    // Should contain replacement characters and 'A'
    let replacement_count = bs.as_str().matches('\u{FFFD}').count();
    assert!(replacement_count >= 1);
    assert!(bs.as_str().contains('A'));
}

#[test]
fn test_from_utf16_roundtrip() {
    let original = "Hello, δΈ–η•Œ! πŸ¦€ Testing UTF-16 roundtrip";
    let utf16: Vec<u16> = original.encode_utf16().collect();
    let bs = ByteStr::from_utf16(&utf16).unwrap();

    assert_eq!(bs.as_str(), original);

    // Test roundtrip consistency
    let utf16_again: Vec<u16> = bs.as_str().encode_utf16().collect();
    assert_eq!(utf16, utf16_again);
}

#[test]
fn test_from_utf16_edge_cases() {
    // Test BMP characters (Basic Multilingual Plane)
    let bmp_chars = "ABCΞ±Ξ²Ξ³δΈ­ζ–‡ν•œκΈ€";
    let utf16: Vec<u16> = bmp_chars.encode_utf16().collect();
    let bs = ByteStr::from_utf16(&utf16).unwrap();
    assert_eq!(bs.as_str(), bmp_chars);

    // Test supplementary characters (outside BMP)
    let supplementary = "πŸŒπŸŽ΅πŸŽ¨πŸš€";
    let utf16_supp: Vec<u16> = supplementary.encode_utf16().collect();
    let bs_supp = ByteStr::from_utf16(&utf16_supp).unwrap();
    assert_eq!(bs_supp.as_str(), supplementary);
}

#[test]
fn test_from_utf16_consistency() {
    // Test that from_utf16 and from_utf16_lossy produce the same result for valid input
    let test_strings = [
        "Hello, world!",
        "δΈ–η•Œ",
        "πŸ¦€πŸŒπŸŽ΅",
        "",
        "Mixed: Hello δΈ–η•Œ πŸ¦€",
    ];

    for test_str in &test_strings {
        let utf16: Vec<u16> = test_str.encode_utf16().collect();
        let bs_strict = ByteStr::from_utf16(&utf16).unwrap();
        let bs_lossy = ByteStr::from_utf16_lossy(&utf16);

        assert_eq!(bs_strict.as_str(), bs_lossy.as_str());
        assert_eq!(bs_strict.as_str(), *test_str);
    }
}