| 1 |
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution and at |
| 3 |
// http://rust-lang.org/COPYRIGHT. |
| 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 super::char::is_combining_mark; |
| 12 |
use super::UnicodeNormalization; |
| 13 |
use core::char; |
| 14 |
|
| 15 |
#[cfg(not(feature = "std"))] |
| 16 |
use alloc::string::{String, ToString}; |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn test_nfd() { |
| 20 |
macro_rules! t { |
| 21 |
($input: expr, $expected: expr) => { |
| 22 |
assert_eq!($input.nfd().to_string(), $expected); |
| 23 |
// A dummy iterator that is not std::str::Chars directly; |
| 24 |
// note that `id_func` is used to ensure `Clone` implementation |
| 25 |
assert_eq!( |
| 26 |
$input.chars().map(|c| c).nfd().collect::<String>(), |
| 27 |
$expected |
| 28 |
); |
| 29 |
}; |
| 30 |
} |
| 31 |
t!("abc", "abc"); |
| 32 |
t!("\u{1e0b}\u{1c4}", "d\u{307}\u{1c4}"); |
| 33 |
t!("\u{2026}", "\u{2026}"); |
| 34 |
t!("\u{2126}", "\u{3a9}"); |
| 35 |
t!("\u{1e0b}\u{323}", "d\u{323}\u{307}"); |
| 36 |
t!("\u{1e0d}\u{307}", "d\u{323}\u{307}"); |
| 37 |
t!("a\u{301}", "a\u{301}"); |
| 38 |
t!("\u{301}a", "\u{301}a"); |
| 39 |
t!("\u{d4db}", "\u{1111}\u{1171}\u{11b6}"); |
| 40 |
t!("\u{ac1c}", "\u{1100}\u{1162}"); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn test_nfkd() { |
| 45 |
macro_rules! t { |
| 46 |
($input: expr, $expected: expr) => { |
| 47 |
assert_eq!($input.nfkd().to_string(), $expected); |
| 48 |
}; |
| 49 |
} |
| 50 |
t!("abc", "abc"); |
| 51 |
t!("\u{1e0b}\u{1c4}", "d\u{307}DZ\u{30c}"); |
| 52 |
t!("\u{2026}", "..."); |
| 53 |
t!("\u{2126}", "\u{3a9}"); |
| 54 |
t!("\u{1e0b}\u{323}", "d\u{323}\u{307}"); |
| 55 |
t!("\u{1e0d}\u{307}", "d\u{323}\u{307}"); |
| 56 |
t!("a\u{301}", "a\u{301}"); |
| 57 |
t!("\u{301}a", "\u{301}a"); |
| 58 |
t!("\u{d4db}", "\u{1111}\u{1171}\u{11b6}"); |
| 59 |
t!("\u{ac1c}", "\u{1100}\u{1162}"); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn test_nfc() { |
| 64 |
macro_rules! t { |
| 65 |
($input: expr, $expected: expr) => { |
| 66 |
assert_eq!($input.nfc().to_string(), $expected); |
| 67 |
}; |
| 68 |
} |
| 69 |
t!("abc", "abc"); |
| 70 |
t!("\u{1e0b}\u{1c4}", "\u{1e0b}\u{1c4}"); |
| 71 |
t!("\u{2026}", "\u{2026}"); |
| 72 |
t!("\u{2126}", "\u{3a9}"); |
| 73 |
t!("\u{1e0b}\u{323}", "\u{1e0d}\u{307}"); |
| 74 |
t!("\u{1e0d}\u{307}", "\u{1e0d}\u{307}"); |
| 75 |
t!("a\u{301}", "\u{e1}"); |
| 76 |
t!("\u{301}a", "\u{301}a"); |
| 77 |
t!("\u{d4db}", "\u{d4db}"); |
| 78 |
t!("\u{ac1c}", "\u{ac1c}"); |
| 79 |
t!( |
| 80 |
"a\u{300}\u{305}\u{315}\u{5ae}b", |
| 81 |
"\u{e0}\u{5ae}\u{305}\u{315}b" |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn test_nfkc() { |
| 87 |
macro_rules! t { |
| 88 |
($input: expr, $expected: expr) => { |
| 89 |
assert_eq!($input.nfkc().to_string(), $expected); |
| 90 |
}; |
| 91 |
} |
| 92 |
t!("abc", "abc"); |
| 93 |
t!("\u{1e0b}\u{1c4}", "\u{1e0b}D\u{17d}"); |
| 94 |
t!("\u{2026}", "..."); |
| 95 |
t!("\u{2126}", "\u{3a9}"); |
| 96 |
t!("\u{1e0b}\u{323}", "\u{1e0d}\u{307}"); |
| 97 |
t!("\u{1e0d}\u{307}", "\u{1e0d}\u{307}"); |
| 98 |
t!("a\u{301}", "\u{e1}"); |
| 99 |
t!("\u{301}a", "\u{301}a"); |
| 100 |
t!("\u{d4db}", "\u{d4db}"); |
| 101 |
t!("\u{ac1c}", "\u{ac1c}"); |
| 102 |
t!( |
| 103 |
"a\u{300}\u{305}\u{315}\u{5ae}b", |
| 104 |
"\u{e0}\u{5ae}\u{305}\u{315}b" |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn test_normalize_char() { |
| 110 |
assert_eq!('\u{2126}'.nfd().to_string(), "\u{3a9}") |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn test_is_combining_mark_ascii() { |
| 115 |
for cp in 0..0x7f { |
| 116 |
assert!(!is_combining_mark(char::from_u32(cp).unwrap())); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn test_is_combining_mark_misc() { |
| 122 |
// https://github.com/unicode-rs/unicode-normalization/issues/16 |
| 123 |
// U+11C3A BHAIKSUKI VOWEL SIGN O |
| 124 |
// Category: Mark, Nonspacing [Mn] |
| 125 |
assert!(is_combining_mark('\u{11C3A}')); |
| 126 |
|
| 127 |
// U+11C3F BHAIKSUKI SIGN VIRAMA |
| 128 |
// Category: Mark, Nonspacing [Mn] |
| 129 |
assert!(is_combining_mark('\u{11C3F}')); |
| 130 |
} |