Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/src/windowing/winit/fonts/str_index_map_tests.rs
StratoSDK / crates / strato-ui-renderer / src / windowing / winit / fonts / str_index_map_tests.rs
1use super::*;
2 
3#[test]
4fn test_str_index_map_get_byte_index() {
5 let text = "ab😈■d";
6 let str_index_map = StrIndexMap::new(text);
7 
8 assert_eq!(str_index_map.byte_index(0), Some(0));
9 assert_eq!(str_index_map.byte_index(1), Some(1));
10 assert_eq!(str_index_map.byte_index(2), Some(2));
11 
12 // The character at index 2 (😈) is 4 bytes, which means the character at index 3 (■) starts at
13 // byte index 6.
14 assert_eq!(str_index_map.byte_index(3), Some(6));
15 // The character at index 3 (■) is 3 bytes, which means the character at index 4 (d) starts at
16 // byte index 9.
17 assert_eq!(str_index_map.byte_index(4), Some(9));
18 
19 // The backing string only has 5 characters. Ensure we return None in the case a character index
20 // that isn't included in the string is passed.
21 assert_eq!(str_index_map.byte_index(5), None);
22}
23 
24#[test]
25fn test_str_index_map_get_char_index() {
26 let text = "ab😈■d";
27 let str_index_map = StrIndexMap::new(text);
28 
29 assert_eq!(str_index_map.char_index(0), Some(0));
30 assert_eq!(str_index_map.char_index(1), Some(1));
31 assert_eq!(str_index_map.char_index(2), Some(2));
32 
33 // The character at index 2 (😈) is 4 bytes, which means the character at index 3 (■) starts at
34 // byte index 6.
35 assert_eq!(str_index_map.char_index(6), Some(3));
36 // The character at index 3 (■) is 3 bytes, which means the character at index 4 (d) starts at
37 // byte index 9.
38 assert_eq!(str_index_map.char_index(9), Some(4));
39 
40 // The backing string only has 10 bytes. Ensure we return None in the case a byte index
41 // that isn't included in the string is passed.
42 assert_eq!(str_index_map.char_index(10), None);
43 
44 // Byte index 3 is not a char boundary, so we should return None.
45 assert_eq!(str_index_map.char_index(3), None);
46}
47