Debian Codegraph @

third_party/rust/arraystring/tests/string_parity.rs

raw
1 use arraystring::{error::Error, prelude::*, utils::is_char_boundary, utils::is_inside_boundary};
2 use std::panic::{catch_unwind, AssertUnwindSafe, RefUnwindSafe};
3 use std::{fmt::Debug, iter::FromIterator};
4
5 fn unwind<R, F>(func: F) -> Result<R, ()>
6 where
7 F: FnOnce() -> R,
8 {
9 catch_unwind(AssertUnwindSafe(func)).map_err(|_| ())
10 }
11
12 static STRINGS: [&'static str; 8] = [
13 "🤔🤔🤔🤔🤔🤔🤔",
14 "ABCDEFGHIJKLMNOPQRSASHUDAHSDIUASH ",
15 "iejueueheuheuheu 0",
16 "",
17 "1",
18 "ab",
19 " ",
20 " 899saH(8hadhaiuhsidnkandu",
21 ];
22
23 #[test]
24 fn try_from_str() {
25 assert(String::from, MaxString::try_from_str);
26 }
27
28 #[test]
29 fn from_str_truncate() {
30 assert(String::from, MaxString::from_str_truncate);
31 }
32
33 #[test]
34 fn from_str_unchecked() {
35 assert(String::from, |s| unsafe {
36 MaxString::from_str_unchecked(s)
37 });
38 }
39
40 #[test]
41 fn try_from_chars() {
42 assert(
43 |s| String::from_iter(s.chars()),
44 |s| MaxString::try_from_chars(s.chars()),
45 );
46 }
47
48 #[test]
49 fn from_chars() {
50 assert(
51 |s| String::from_iter(s.chars()),
52 |s| MaxString::from_chars(s.chars()),
53 );
54 }
55
56 #[test]
57 fn from_chars_unchecked() {
58 assert(
59 |s| String::from_iter(s.chars()),
60 |s| unsafe { MaxString::from_chars_unchecked(s.chars()) },
61 );
62 }
63
64 #[test]
65 fn try_from_iter() {
66 assert(
67 |s| String::from_iter(vec![s]),
68 |s| MaxString::try_from_iterator(vec![s]),
69 );
70 }
71
72 #[test]
73 fn from_iter() {
74 assert(
75 |s| String::from_iter(vec![s]),
76 |s| MaxString::from_iterator(vec![s]),
77 );
78 }
79
80 #[test]
81 fn from_iter_unchecked() {
82 assert(
83 |s| String::from_iter(vec![s]),
84 |s| unsafe { MaxString::from_iterator_unchecked(vec![s]) },
85 );
86 }
87
88 #[test]
89 fn try_from_utf8() {
90 assert(
91 |s| String::from_utf8(s.as_bytes().to_vec()),
92 |s| MaxString::try_from_utf8(s.as_bytes()),
93 );
94 }
95
96 #[test]
97 fn from_utf8() {
98 assert(
99 |s| String::from_utf8(s.as_bytes().to_vec()),
100 |s| MaxString::from_utf8(s.as_bytes()),
101 );
102 }
103
104 #[inline]
105 fn invalidate_utf8(buf: &mut [u8]) -> &mut [u8] {
106 if buf.len() >= 4 {
107 buf[0] = 0;
108 buf[1] = 159;
109 buf[2] = 146;
110 buf[3] = 150;
111 }
112 buf
113 }
114
115 #[test]
116 fn try_from_utf8_invalid() {
117 unsafe {
118 assert(
119 |s| String::from_utf8(invalidate_utf8(s.to_owned().as_bytes_mut()).to_vec()),
120 |s| MaxString::try_from_utf8(invalidate_utf8(s.to_owned().as_bytes_mut())),
121 );
122 }
123 }
124
125 #[test]
126 fn from_utf8_invalid() {
127 unsafe {
128 assert(
129 |s| String::from_utf8(invalidate_utf8(s.to_owned().as_bytes_mut()).to_vec()),
130 |s| MaxString::from_utf8(invalidate_utf8(s.to_owned().as_bytes_mut())),
131 );
132 }
133 }
134
135 #[test]
136 fn try_from_utf16() {
137 let utf16 = |s: &str| s.encode_utf16().collect::<Vec<_>>();
138 assert(
139 |s| String::from_utf16(&utf16(s)),
140 |s| MaxString::try_from_utf16(&utf16(s)),
141 );
142 }
143
144 #[test]
145 fn from_utf16() {
146 let utf16 = |s: &str| s.encode_utf16().collect::<Vec<_>>();
147 assert(
148 |s| String::from_utf16(&utf16(s)),
149 |s| MaxString::from_utf16(&utf16(s)),
150 );
151 }
152
153 #[test]
154 fn from_utf16_lossy() {
155 let utf16 = |s: &str| s.encode_utf16().collect::<Vec<_>>();
156 assert(
157 |s| String::from_utf16(&utf16(s)),
158 |s| MaxString::from_utf16(&utf16(s)),
159 );
160 }
161
162 fn invalidate_utf16(buf: &mut [u16]) -> &mut [u16] {
163 if buf.len() >= 7 {
164 buf[0] = 0xD834;
165 buf[1] = 0xDD1E;
166 buf[2] = 0x006D;
167 buf[3] = 0x0075;
168 buf[4] = 0xD800;
169 buf[5] = 0x0069;
170 buf[6] = 0x0063;
171 }
172 buf
173 }
174
175 #[test]
176 fn try_from_utf16_invalid() {
177 let utf16 = |s: &str| s.encode_utf16().collect::<Vec<_>>();
178 assert(
179 |s| String::from_utf16(invalidate_utf16(&mut utf16(s))),
180 |s| MaxString::try_from_utf16(invalidate_utf16(&mut utf16(s))),
181 );
182 }
183
184 #[test]
185 fn from_utf16_invalid() {
186 let utf16 = |s: &str| s.encode_utf16().collect::<Vec<_>>();
187 assert(
188 |s| String::from_utf16(invalidate_utf16(&mut utf16(s))),
189 |s| MaxString::from_utf16(invalidate_utf16(&mut utf16(s))),
190 );
191 }
192
193 #[test]
194 fn from_utf16_lossy_invalid() {
195 let utf16 = |s: &str| s.encode_utf16().collect::<Vec<_>>();
196 assert(
197 |s| String::from_utf16(invalidate_utf16(&mut utf16(s))),
198 |s| MaxString::from_utf16(invalidate_utf16(&mut utf16(s))),
199 );
200 }
201
202 #[test]
203 fn from_utf8_unchecked() {
204 unsafe {
205 assert(
206 |s| String::from_utf8_unchecked(s.as_bytes().to_vec()),
207 |s| MaxString::from_utf8_unchecked(s.as_bytes()),
208 );
209 }
210 }
211
212 #[test]
213 fn try_push_str() {
214 assert(
215 |s| {
216 let mut st = String::from(s);
217 st.push_str(s);
218 st
219 },
220 |s| {
221 let mut ms = MaxString::try_from_str(s).unwrap();
222 ms.try_push_str(s).map(|()| ms)
223 },
224 );
225 }
226
227 #[test]
228 fn push_str() {
229 assert(
230 |s| {
231 let mut st = String::from(s);
232 st.push_str(s);
233 st
234 },
235 |s| {
236 let mut ms = MaxString::try_from_str(s).unwrap();
237 ms.push_str(s);
238 ms
239 },
240 );
241 }
242
243 #[test]
244 fn add_str() {
245 assert(
246 |s| String::from(s) + s,
247 |s| MaxString::try_from_str(s).unwrap() + s,
248 );
249 }
250
251 #[test]
252 fn push_str_unchecked() {
253 assert(
254 |s| {
255 let mut st = String::from(s);
256 st.push_str(s);
257 st
258 },
259 |s| {
260 let mut ms = MaxString::try_from_str(s).unwrap();
261 unsafe { ms.push_str_unchecked(s) };
262 ms
263 },
264 );
265 }
266
267 #[test]
268 fn push() {
269 assert(
270 |s| {
271 let mut s = String::from(s);
272 s.push('🤔');
273 s
274 },
275 |s| {
276 let mut ms = MaxString::try_from_str(s).unwrap();
277 ms.try_push('🤔').map(|()| ms)
278 },
279 );
280 }
281
282 #[test]
283 fn push_unchecked() {
284 assert(
285 |s| {
286 let mut s = String::from(s);
287 s.push('🤔');
288 s
289 },
290 |s| {
291 let mut ms = MaxString::try_from_str(s).unwrap();
292 unsafe { ms.push_unchecked('🤔') };
293 ms
294 },
295 );
296 }
297
298 #[test]
299 fn truncate() {
300 assert(
301 |s| {
302 unwind(move || {
303 let mut s = String::from(s);
304 s.truncate(2);
305 s
306 })
307 },
308 |s| {
309 let mut ms = MaxString::try_from_str(s).unwrap();
310 ms.truncate(2).map(|()| ms)
311 },
312 );
313 }
314
315 #[test]
316 fn pop() {
317 assert(
318 |s| {
319 let mut s = String::from(s);
320 let old = s.pop();
321 (s, old)
322 },
323 |s| {
324 let mut ms = MaxString::try_from_str(s).unwrap();
325 let old = ms.pop();
326 (ms, old)
327 },
328 );
329 }
330
331 #[test]
332 fn trim() {
333 assert(
334 |s| String::from(s).trim().to_owned(),
335 |s| {
336 let mut ms = MaxString::try_from_str(s).unwrap();
337 ms.trim();
338 ms
339 },
340 );
341 }
342
343 #[test]
344 fn remove() {
345 assert(
346 |s| {
347 unwind(move || {
348 let mut s = String::from(s);
349 let removed = s.remove(2);
350 (removed, s)
351 })
352 },
353 |s| {
354 let mut ms = MaxString::try_from_str(s).unwrap();
355 ms.remove(2).map(|r| (r, ms))
356 },
357 );
358 }
359
360 #[test]
361 fn retain() {
362 assert(
363 |s| {
364 let mut s = String::from(s);
365 s.retain(|c| c == 'a');
366 s
367 },
368 |s| {
369 let mut ms = MaxString::try_from_str(s).unwrap();
370 ms.retain(|c| c == 'a');
371 ms
372 },
373 );
374 }
375
376 #[test]
377 fn try_insert() {
378 assert(
379 |s| {
380 unwind(move || {
381 let mut s = String::from(s);
382 s.insert(2, 'a');
383 s
384 })
385 },
386 |s| {
387 let mut ms = MaxString::try_from_str(s).unwrap();
388 ms.try_insert(2, 'a').map(|()| ms)
389 },
390 );
391 }
392
393 #[test]
394 fn insert_unchecked() {
395 assert(
396 |s| {
397 unwind(move || {
398 let mut s = String::from(s);
399 s.insert(2, 'a');
400 s
401 })
402 },
403 |s| {
404 let mut ms = MaxString::try_from_str(s).unwrap();
405 is_inside_boundary(2u8, ms.len())
406 .map_err(Error::from)
407 .and_then(|()| Ok(is_char_boundary(&ms, 2)?))
408 .map(|()| unsafe { ms.insert_unchecked(2, 'a') })
409 .map(|()| ms)
410 },
411 );
412 }
413
414 #[test]
415 fn try_insert_str() {
416 assert(
417 |s| {
418 unwind(move || {
419 let mut st = String::from(s);
420 st.insert_str(2, s);
421 st
422 })
423 },
424 |s| {
425 let mut ms = MaxString::try_from_str(s).unwrap();
426 ms.try_insert_str(2, s).map(|()| ms)
427 },
428 );
429 }
430
431 #[test]
432 fn insert_str() {
433 assert(
434 |s| {
435 unwind(move || {
436 let mut st = String::from(s);
437 st.insert_str(2, s);
438 (st, ())
439 })
440 },
441 |s| {
442 let mut ms = MaxString::try_from_str(s).unwrap();
443 let res = ms.insert_str(2, s);
444 res.map(|()| (ms, ()))
445 },
446 );
447 }
448
449 #[test]
450 fn insert_str_unchecked() {
451 assert(
452 |s| {
453 unwind(move || {
454 let mut st = String::from(s);
455 st.insert_str(2, s);
456 st
457 })
458 },
459 |s| {
460 let mut ms = MaxString::try_from_str(s).unwrap();
461 is_inside_boundary(2u8, ms.len())
462 .map_err(Error::from)
463 .and_then(|()| Ok(is_char_boundary(&ms, 2)?))
464 .map(|()| unsafe { ms.insert_str_unchecked(2, s) })
465 .map(|()| ms)
466 },
467 );
468 }
469
470 #[test]
471 fn clear() {
472 assert(
473 |s| {
474 let mut st = String::from(s);
475 st.clear();
476 st
477 },
478 |s| {
479 let mut ms = MaxString::try_from_str(s).unwrap();
480 ms.clear();
481 ms
482 },
483 );
484 }
485
486 #[test]
487 fn split_off() {
488 assert(
489 |s| {
490 unwind(move || {
491 let mut st = String::from(s);
492 let split = st.split_off(2);
493 (st, split)
494 })
495 },
496 |s| {
497 let mut ms = MaxString::try_from_str(s).unwrap();
498 ms.split_off(2).map(|s| (ms, s))
499 },
500 );
501 }
502
503 #[test]
504 fn drain() {
505 assert(
506 |s| {
507 unwind(move || {
508 let mut st = String::from(s);
509 let drained: String = st.drain(..2).collect();
510 (st, drained)
511 })
512 },
513 |s| {
514 let mut ms = MaxString::try_from_str(s).unwrap();
515 let drained = ms.drain(..2).map(|d| d.collect::<String>());
516 drained.map(|d| (ms, d))
517 },
518 );
519 }
520
521 #[test]
522 fn replace_range() {
523 assert(
524 |s| {
525 unwind(move || {
526 let mut st = String::from(s);
527 st.replace_range(..2, s);
528 (st, ())
529 })
530 },
531 |s| {
532 let mut ms = MaxString::try_from_str(s).unwrap();
533 ms.replace_range(..2, s).map(|()| (ms, ()))
534 },
535 );
536 }
537
538 #[test]
539 fn len() {
540 assert(
541 |s| {
542 let st = String::from(s);
543 st.len().to_string()
544 },
545 |s| {
546 let ms = MaxString::try_from_str(s).unwrap();
547 ms.len().to_string()
548 },
549 );
550 }
551
552 #[test]
553 fn is_empty() {
554 assert(
555 |s| {
556 let st = String::from(s);
557 st.is_empty().to_string()
558 },
559 |s| {
560 let ms = MaxString::try_from_str(s).unwrap();
561 ms.is_empty().to_string()
562 },
563 );
564 }
565
566 #[test]
567 fn new() {
568 assert_eq!(String::new().as_str(), MaxString::new().as_str());
569 }
570
571 // Internal hackery to make the function `assert` possible
572
573 trait Normalize<EQ: PartialEq> {
574 fn normalize(&self) -> EQ;
575 }
576
577 impl Normalize<Result<String, ()>> for () {
578 fn normalize(&self) -> Result<String, ()> {
579 Ok("".to_owned())
580 }
581 }
582
583 impl Normalize<Result<String, ()>> for MaxString {
584 fn normalize(&self) -> Result<String, ()> {
585 Ok(self.as_str().to_owned())
586 }
587 }
588
589 impl Normalize<Result<String, ()>> for String {
590 fn normalize(&self) -> Result<String, ()> {
591 Ok(self.as_str().to_owned())
592 }
593 }
594
595 impl<'a> Normalize<Result<String, ()>> for &'a str {
596 fn normalize(&self) -> Result<String, ()> {
597 Ok(self.to_string())
598 }
599 }
600
601 impl Normalize<Result<String, ()>> for char {
602 fn normalize(&self) -> Result<String, ()> {
603 Ok(self.to_string())
604 }
605 }
606
607 impl<N: Normalize<Result<String, ()>>> Normalize<Result<String, ()>> for Option<N> {
608 fn normalize(&self) -> Result<String, ()> {
609 self.as_ref().ok_or(()).and_then(|n| n.normalize())
610 }
611 }
612
613 impl<E, K: PartialEq, N: Normalize<Result<K, ()>>> Normalize<Result<K, ()>> for Result<N, E> {
614 fn normalize(&self) -> Result<K, ()> {
615 self.as_ref().map_err(|_| ()).and_then(|n| n.normalize())
616 }
617 }
618
619 impl<K: PartialEq, L: PartialEq, M: Normalize<K>, N: Normalize<L>> Normalize<Result<(K, L), ()>>
620 for (M, N)
621 {
622 fn normalize(&self) -> Result<(K, L), ()> {
623 Ok((self.0.normalize(), self.1.normalize()))
624 }
625 }
626
627 impl<J, K, L, M, N, O> Normalize<Result<(J, K, L), ()>> for (M, N, O)
628 where
629 J: PartialEq,
630 K: PartialEq,
631 L: PartialEq,
632 M: Normalize<J>,
633 N: Normalize<K>,
634 O: Normalize<L>,
635 {
636 fn normalize(&self) -> Result<(J, K, L), ()> {
637 Ok((self.0.normalize(), self.1.normalize(), self.2.normalize()))
638 }
639 }
640
641 fn assert<Q, F, G, T, U>(f: F, g: G)
642 where
643 Q: PartialEq + Debug,
644 T: Normalize<Q>,
645 U: Normalize<Q>,
646 F: Fn(&'static str) -> T + RefUnwindSafe,
647 G: Fn(&'static str) -> U + RefUnwindSafe,
648 {
649 let _ = env_logger::try_init();
650 for string in STRINGS.into_iter() {
651 let f = f(string).normalize();
652 let g = g(string).normalize();
653 assert_eq!(f, g);
654 }
655 }