scanner_test+test.ha (4333B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use bytes; 5 use encoding::utf8; 6 use io; 7 use memio; 8 use strings; 9 10 @test fn read_byte() void = { 11 let buf = memio::fixed([1, 3, 3, 7]); 12 13 assert(read_byte(&buf) as u8 == 1); 14 assert(read_byte(&buf) as u8 == 3); 15 assert(read_byte(&buf) as u8 == 3); 16 assert(read_byte(&buf) as u8 == 7); 17 assert(read_byte(&buf) is io::EOF); 18 }; 19 20 @test fn read_tok() void = { 21 let buf = memio::fixed([1, 3, 4, 5, 3, 7]); 22 23 let tok = read_tok(&buf, 4) as []u8; 24 defer free(tok); 25 assert(bytes::equal(tok, [1, 3])); 26 27 let tok = read_tok(&buf, 7) as []u8; 28 defer free(tok); 29 assert(bytes::equal(tok, [5, 3])); 30 31 assert(read_tok(&buf, 1) is io::EOF); 32 }; 33 34 @test fn read_line() void = { 35 let helloworld = strings::toutf8("hello\nworld"); 36 let buf = memio::fixed(helloworld); 37 38 let line = read_line(&buf) as []u8; 39 defer free(line); 40 assert(bytes::equal(line, strings::toutf8("hello"))); 41 42 let line = read_line(&buf) as []u8; 43 defer free(line); 44 assert(bytes::equal(line, strings::toutf8("world"))); 45 46 assert(read_line(&buf) is io::EOF); 47 }; 48 49 @test fn read_rune() void = { 50 let in = memio::fixed([ 51 0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81, 52 0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0x00, 53 ]); 54 55 const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [ 56 'こ', 'ん', 'に', 'ち', 'は', '\0', io::EOF, 57 ]; 58 for (let i = 0z; i < len(expected); i += 1) { 59 let want = expected[i]; 60 61 match (read_rune(&in)) { 62 case let r: rune => 63 assert(want is rune && want as rune == r); 64 case io::EOF => 65 assert(want is io::EOF); 66 case => 67 abort(); 68 }; 69 }; 70 }; 71 72 @test fn scan_rune() void = { 73 let in = memio::fixed(strings::toutf8("hello")); 74 let scanner = newscanner(&in, 32); 75 76 const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [ 77 'h', 'e', 'l', 'l', 'o', io::EOF, 78 ]; 79 for (let i = 0z; i < len(expected); i += 1) { 80 let want = expected[i]; 81 82 match (scan_rune(&scanner)) { 83 case let r: rune => 84 assert(want is rune && want as rune == r); 85 case io::EOF => 86 assert(want is io::EOF); 87 case => 88 abort(); 89 }; 90 }; 91 }; 92 93 @test fn scan_rune_cutoff() void = { 94 let in = memio::fixed([ 95 'a', 0xE3, 96 ]); 97 let scanner = newscanner(&in, 32); 98 99 const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [ 100 'a', utf8::invalid, 101 ]; 102 for (let i = 0z; i < len(expected); i += 1) { 103 let want = expected[i]; 104 105 match (scan_rune(&scanner)) { 106 case let r: rune => 107 assert(want is rune && want as rune == r); 108 case io::EOF => 109 assert(want is io::EOF); 110 case utf8::invalid => 111 assert(want is utf8::invalid); 112 case => 113 abort(); 114 }; 115 }; 116 }; 117 118 @test fn scan_byte() void = { 119 let in = memio::fixed([1, 2, 3]); 120 let scanner = newscanner(&in, 3); 121 122 assert(scan_byte(&scanner) as u8 == 1); 123 assert(scan_byte(&scanner) as u8 == 2); 124 assert(scan_byte(&scanner) as u8 == 3); 125 assert(scan_byte(&scanner) is io::EOF); 126 }; 127 128 @test fn scan_read() void = { 129 const expected: [_]u8 = [ 130 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 131 ]; 132 let in = memio::fixed(expected); 133 134 let scanner = newscanner(&in, 2); 135 defer finish(&scanner); 136 let result = io::drain(&scanner)!; 137 defer free(result); 138 assert(bytes::equal(expected, result)); 139 }; 140 141 @test fn scan_unread() void = { 142 const expected: str = " I will not repeat \nDone!\n"; 143 let in = memio::fixed(strings::toutf8(expected)); 144 145 let scanner = newscanner(&in, 32); 146 defer finish(&scanner); 147 148 let b = scan_byte(&scanner) as u8; 149 unread(&scanner, [b]); 150 151 let b = scan_rune(&scanner) as rune; 152 unread(&scanner, utf8::encoderune(b)); 153 154 let l = scan_line(&scanner)! as const str; 155 assert(l == " I will not repeat "); 156 157 unread(&scanner, strings::toutf8("\n")); 158 unread(&scanner, strings::toutf8(l)); 159 let l = scan_line(&scanner)! as const str; 160 assert(l == " I will not repeat "); 161 162 unread(&scanner, strings::toutf8("\n")); 163 unread(&scanner, strings::toutf8(strings::trim(l))); 164 let l = scan_line(&scanner)! as const str; 165 assert(l == "I will not repeat"); 166 167 unread(&scanner, strings::toutf8("See?\n")); 168 let l = scan_line(&scanner)! as const str; 169 assert(l == "See?"); 170 171 let b = scan_rune(&scanner) as rune; 172 unreadrune(&scanner, b); 173 unreadrune(&scanner, ' '); 174 unread(&scanner, strings::toutf8("I'm")); 175 let l = scan_line(&scanner)! as const str; 176 assert(l == "I'm Done!"); 177 178 assert(scan_line(&scanner) is io::EOF); 179 };