code wiki / (root) / str_test.nx

str_test.nx source

↩ module page · 59 lines · 1949 B

1// str_test.nx -- self-test for str.nx. 2 3 4import "str.nx" 5 6 7// ===== self-test =================================================== 8 9func main() -> i64 { 10 let hello: *str = str_from_cstr("hello") 11 let world: *str = str_from_cstr("world") 12 13 if str_len(hello) != 5 { return 1 } 14 if str_len(world) != 5 { return 2 } 15 if str_eq(hello, world) != 0 { return 3 } 16 if str_eq(hello, hello) != 1 { return 4 } 17 if str_eq_cstr(hello, "hello") != 1 { return 5 } 18 if str_eq_cstr(hello, "helloo") != 0 { return 6 } 19 20 let hi: *str = str_substr(hello, 0, 2) 21 if str_len(hi) != 2 { return 7 } 22 if str_eq_cstr(hi, "he") != 1 { return 8 } 23 24 // starts_with 25 if str_starts_with(hello, hi) != 1 { return 9 } 26 27 // index_of 28 if str_index_of(hello, 0x6C) != 2 { return 10 } // 'l' at index 2 29 if str_index_of(hello, 0x7A) != -1 { return 11 } // 'z' absent 30 31 // split_once 32 let greeting: *str = str_from_cstr("hello,world") 33 let rest_box_raw: *u8 = str_sys_mmap(16) 34 let rest_box: *i64 = rest_box_raw as *i64 35 let before: *str = str_split_once(greeting, 0x2C, rest_box) 36 if str_eq_cstr(before, "hello") != 1 { return 12 } 37 let after: *str = *rest_box as *str 38 if str_eq_cstr(after, "world") != 1 { return 13 } 39 40 // trim 41 let padded: *str = str_from_cstr(" trimmed ") 42 let trimmed: *str = str_trim(padded) 43 if str_eq_cstr(trimmed, "trimmed") != 1 { return 14 } 44 45 // parse_int 46 let numstr: *str = str_from_cstr("42 rest") 47 let consumed_raw: *u8 = str_sys_mmap(16) 48 let consumed: *i64 = consumed_raw as *i64 49 let parsed: i64 = str_parse_int(numstr, consumed) 50 if parsed != 42 { return 15 } 51 if *consumed != 2 { return 16 } 52 53 let neg: *str = str_from_cstr("-123") 54 let c2_raw: *u8 = str_sys_mmap(16) 55 let c2: *i64 = c2_raw as *i64 56 if str_parse_int(neg, c2) != -123 { return 17 } 57 58 return 0 59}