-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from BartMassey-upstream/char
added char() implementations and tests
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::convert::TryFrom; | ||
use std::ops::RangeBounds; | ||
|
||
fn test_char_coverage<R>(n: usize, range: R) | ||
where | ||
R: Iterator<Item = char> + RangeBounds<char> + Clone, | ||
{ | ||
use std::collections::HashSet; | ||
|
||
let all: HashSet<char> = range.clone().collect(); | ||
let mut covered = HashSet::new(); | ||
for _ in 0..n { | ||
let c = fastrand::char(range.clone()); | ||
assert!(all.contains(&c)); | ||
covered.insert(c); | ||
} | ||
assert_eq!(covered, all); | ||
} | ||
|
||
#[test] | ||
fn test_char() { | ||
// ASCII control chars. | ||
let nul = 0u8 as char; | ||
let soh = 1u8 as char; | ||
let stx = 2u8 as char; | ||
// Some undefined Hangul Jamo codepoints just before | ||
// the surrogate area. | ||
let last_jamo = char::try_from(0xd7ffu32).unwrap(); | ||
let penultimate_jamo = char::try_from(last_jamo as u32 - 1).unwrap(); | ||
// Private-use codepoints just after the surrogate area. | ||
let first_private = char::try_from(0xe000u32).unwrap(); | ||
let second_private = char::try_from(first_private as u32 + 1).unwrap(); | ||
// Private-use codepoints at the end of Unicode space. | ||
let last_private = std::char::MAX; | ||
let penultimate_private = char::try_from(last_private as u32 - 1).unwrap(); | ||
|
||
test_char_coverage(100, nul..stx); | ||
test_char_coverage(100, nul..=soh); | ||
|
||
test_char_coverage(400, penultimate_jamo..second_private); | ||
test_char_coverage(400, penultimate_jamo..=second_private); | ||
|
||
test_char_coverage(100, penultimate_private..=last_private); | ||
} |