| 1 |
use glib::{CollationKey, FilenameCollationKey}; |
| 2 |
|
| 3 |
fn init() { |
| 4 |
use std::sync::Once; |
| 5 |
static ONCE: Once = Once::new(); |
| 6 |
|
| 7 |
// Make sure that all tests below are running with the system |
| 8 |
// locale and not the "C" locale. |
| 9 |
ONCE.call_once(|| unsafe { |
| 10 |
libc::setlocale(libc::LC_ALL, c"".as_ptr() as *const _); |
| 11 |
}); |
| 12 |
} |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn collate() { |
| 16 |
init(); |
| 17 |
|
| 18 |
let mut unsorted = vec![ |
| 19 |
String::from("bcd"), |
| 20 |
String::from("cde"), |
| 21 |
String::from("abc"), |
| 22 |
]; |
| 23 |
|
| 24 |
let sorted = vec![ |
| 25 |
String::from("abc"), |
| 26 |
String::from("bcd"), |
| 27 |
String::from("cde"), |
| 28 |
]; |
| 29 |
|
| 30 |
unsorted.sort_by(|s1, s2| CollationKey::from(&s1).cmp(&CollationKey::from(&s2))); |
| 31 |
|
| 32 |
assert_eq!(unsorted, sorted); |
| 33 |
} |
| 34 |
|
| 35 |
#[test] |
| 36 |
fn collate_non_ascii() { |
| 37 |
init(); |
| 38 |
|
| 39 |
let mut unsorted = vec![ |
| 40 |
String::from("猫の手も借りたい"), |
| 41 |
String::from("日本語は難しい"), |
| 42 |
String::from("ありがとう"), |
| 43 |
]; |
| 44 |
|
| 45 |
let sorted = vec![ |
| 46 |
String::from("ありがとう"), |
| 47 |
String::from("日本語は難しい"), |
| 48 |
String::from("猫の手も借りたい"), |
| 49 |
]; |
| 50 |
|
| 51 |
unsorted.sort_by(|s1, s2| CollationKey::from(&s1).cmp(&CollationKey::from(&s2))); |
| 52 |
|
| 53 |
assert_eq!(unsorted, sorted); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn collate_filenames() { |
| 58 |
init(); |
| 59 |
|
| 60 |
let mut unsorted = vec![ |
| 61 |
String::from("bcd.a"), |
| 62 |
String::from("cde.b"), |
| 63 |
String::from("abc.c"), |
| 64 |
]; |
| 65 |
|
| 66 |
let sorted = vec![ |
| 67 |
String::from("abc.c"), |
| 68 |
String::from("bcd.a"), |
| 69 |
String::from("cde.b"), |
| 70 |
]; |
| 71 |
|
| 72 |
unsorted |
| 73 |
.sort_by(|s1, s2| FilenameCollationKey::from(&s1).cmp(&FilenameCollationKey::from(&s2))); |
| 74 |
|
| 75 |
assert_eq!(unsorted, sorted); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn collate_filenames_non_ascii() { |
| 80 |
init(); |
| 81 |
|
| 82 |
let mut unsorted = vec![ |
| 83 |
String::from("猫の手も借りたい.foo"), |
| 84 |
String::from("日本語は難しい.bar"), |
| 85 |
String::from("ありがとう.baz"), |
| 86 |
]; |
| 87 |
|
| 88 |
let sorted = vec![ |
| 89 |
String::from("ありがとう.baz"), |
| 90 |
String::from("日本語は難しい.bar"), |
| 91 |
String::from("猫の手も借りたい.foo"), |
| 92 |
]; |
| 93 |
|
| 94 |
unsorted |
| 95 |
.sort_by(|s1, s2| FilenameCollationKey::from(&s1).cmp(&FilenameCollationKey::from(&s2))); |
| 96 |
|
| 97 |
assert_eq!(unsorted, sorted); |
| 98 |
} |