]>
Commit | Line | Data |
---|---|---|
3c0e092e XL |
1 | // Copyright 2017 The UNIC Project Developers. |
2 | // | |
3 | // See the COPYRIGHT file at the top-level directory of this distribution. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | use std::{char, u32, vec}; | |
12 | ||
13 | use unic_char_range::CharRange; | |
14 | ||
15 | fn all_chars() -> vec::IntoIter<char> { | |
16 | (u32::MIN..u32::MAX) | |
17 | .take_while(|&u| u <= char::MAX as u32) | |
18 | .filter_map(char::from_u32) | |
19 | .collect::<Vec<_>>() | |
20 | .into_iter() | |
21 | } | |
22 | ||
23 | #[test] | |
24 | fn test_iter_all_chars() { | |
25 | assert!(CharRange::all().iter().eq(all_chars())) | |
26 | } | |
27 | ||
28 | #[test] | |
29 | fn test_iter_all_chars_rev() { | |
30 | assert!(CharRange::all().iter().rev().eq(all_chars().rev())) | |
31 | } | |
32 | ||
33 | #[test] | |
34 | fn test_iter_all_chars_mixed_next_back() { | |
35 | let mut custom = CharRange::all().iter(); | |
36 | let mut simple = all_chars(); | |
37 | while let Some(custom_char) = custom.next() { | |
38 | assert_eq!(Some(custom_char), simple.next()); | |
39 | assert_eq!(custom.next_back(), simple.next_back()); | |
40 | } | |
41 | assert_eq!(None, simple.next()); | |
42 | } | |
43 | ||
44 | #[test] | |
45 | fn test_iter_all_chars_into_iter() { | |
46 | for _ch in CharRange::all() { | |
47 | // nothing | |
48 | } | |
49 | } | |
50 | ||
51 | #[test] | |
52 | fn test_iter_fused() { | |
53 | let mut iter = CharRange::all().iter(); | |
54 | let mut fused = all_chars().fuse(); | |
55 | assert!(iter.by_ref().eq(fused.by_ref())); | |
56 | for _ in 0..100 { | |
57 | assert_eq!(iter.next(), fused.next()); | |
58 | } | |
59 | } | |
60 | ||
61 | #[test] | |
62 | fn test_iter_exact_trusted_len() { | |
63 | fn assert_presents_right_len<I: ExactSizeIterator>(iter: &I, len: usize) { | |
64 | assert_eq!(iter.len(), len); | |
65 | assert_eq!(iter.size_hint(), (len, Some(len))); | |
66 | } | |
67 | ||
68 | let mut iter = CharRange::all().iter(); | |
69 | let mut predicted_length = iter.len(); | |
70 | assert_eq!(predicted_length, all_chars().len()); | |
71 | ||
72 | while let Some(_) = iter.next() { | |
73 | predicted_length -= 1; | |
74 | assert_presents_right_len(&iter, predicted_length); | |
75 | } | |
76 | ||
77 | assert_presents_right_len(&iter, 0); | |
78 | } |