summaryrefslogtreecommitdiff
path: root/src/unicode_tests.zig
diff options
context:
space:
mode:
authorGravatar Sam Atman2025-06-01 14:08:25 -0400
committerGravatar Sam Atman2025-06-01 14:08:25 -0400
commit8f5209fa095c2ed9114ce102b2f9b2cc90d66b13 (patch)
tree4ec54815215a9a808be0ab9a2968159f144ba076 /src/unicode_tests.zig
parentDocument "fat_offset" in README (diff)
downloadzg-8f5209fa095c2ed9114ce102b2f9b2cc90d66b13.tar.gz
zg-8f5209fa095c2ed9114ce102b2f9b2cc90d66b13.tar.xz
zg-8f5209fa095c2ed9114ce102b2f9b2cc90d66b13.zip
Add graphemeAtIndex + iterate before and after
That completes the set. I do think it's possible to bum a few more cycles from the implementation, but, I'm not going to. It passes the acceptance suite and that's what it needs to do.
Diffstat (limited to 'src/unicode_tests.zig')
-rw-r--r--src/unicode_tests.zig69
1 files changed, 59 insertions, 10 deletions
diff --git a/src/unicode_tests.zig b/src/unicode_tests.zig
index c463dcc..ae177a9 100644
--- a/src/unicode_tests.zig
+++ b/src/unicode_tests.zig
@@ -162,20 +162,51 @@ test "Segmentation GraphemeIterator" {
162 bytes_index += cp_index; 162 bytes_index += cp_index;
163 } 163 }
164 164
165 const this_str = all_bytes.items;
166
165 { 167 {
166 var iter = graph.iterator(all_bytes.items); 168 var iter = graph.iterator(this_str);
167 169
168 // Check. 170 // Check.
169 for (want.items) |want_gc| { 171 for (want.items, 1..) |want_gc, idx| {
170 const got_gc = (iter.next()).?; 172 const got_gc = (iter.next()).?;
171 try std.testing.expectEqualStrings( 173 try std.testing.expectEqualStrings(
172 want_gc.bytes(all_bytes.items), 174 want_gc.bytes(this_str),
173 got_gc.bytes(all_bytes.items), 175 got_gc.bytes(this_str),
174 ); 176 );
177 for (got_gc.offset..got_gc.offset + got_gc.len) |i| {
178 const this_gc = graph.graphemeAtIndex(this_str, i);
179 std.testing.expectEqualSlices(
180 u8,
181 got_gc.bytes(this_str),
182 this_gc.bytes(this_str),
183 ) catch |err| {
184 debug.print("Wrong grapheme on line {d} #{d} offset {d}\n", .{ line_iter.line, idx, i });
185 return err;
186 };
187 }
188 var after_iter = graph.iterateAfterGrapheme(this_str, got_gc);
189 if (after_iter.next()) |next_gc| {
190 if (iter.peek()) |next_peek| {
191 std.testing.expectEqualSlices(
192 u8,
193 next_gc.bytes(this_str),
194 next_peek.bytes(this_str),
195 ) catch |err| {
196 debug.print("Peeks differ on line {d} #{d} \n", .{ line_iter.line, idx });
197 return err;
198 };
199 } else {
200 debug.print("Mismatch: peek missing, next found, line {d} #{d}\n", .{ line_iter.line, idx });
201 try testing.expect(false);
202 }
203 } else {
204 try testing.expectEqual(null, iter.peek());
205 }
175 } 206 }
176 } 207 }
177 { 208 {
178 var iter = graph.reverseIterator(all_bytes.items); 209 var iter = graph.reverseIterator(this_str);
179 210
180 // Check. 211 // Check.
181 var i: usize = want.items.len; 212 var i: usize = want.items.len;
@@ -190,8 +221,8 @@ test "Segmentation GraphemeIterator" {
190 return error.TestExpectedEqual; 221 return error.TestExpectedEqual;
191 }; 222 };
192 std.testing.expectEqualStrings( 223 std.testing.expectEqualStrings(
193 want_gc.bytes(all_bytes.items), 224 want_gc.bytes(this_str),
194 got_gc.bytes(all_bytes.items), 225 got_gc.bytes(this_str),
195 ) catch |err| { 226 ) catch |err| {
196 std.debug.print( 227 std.debug.print(
197 "line {d} grapheme {d}: expected {any} found {any}\n", 228 "line {d} grapheme {d}: expected {any} found {any}\n",
@@ -199,6 +230,24 @@ test "Segmentation GraphemeIterator" {
199 ); 230 );
200 return err; 231 return err;
201 }; 232 };
233 var before_iter = graph.iterateBeforeGrapheme(this_str, got_gc);
234 if (before_iter.prev()) |prev_gc| {
235 if (iter.peek()) |prev_peek| {
236 std.testing.expectEqualSlices(
237 u8,
238 prev_gc.bytes(this_str),
239 prev_peek.bytes(this_str),
240 ) catch |err| {
241 debug.print("Peeks differ on line {d} #{d} \n", .{ line_iter.line, i });
242 return err;
243 };
244 } else {
245 debug.print("Mismatch: peek missing, prev found, line {d} #{d}\n", .{ line_iter.line, i });
246 try testing.expect(false);
247 }
248 } else {
249 try testing.expectEqual(null, iter.peek());
250 }
202 } 251 }
203 } 252 }
204 } 253 }
@@ -287,7 +336,7 @@ test "Segmentation Word Iterator" {
287 } else { 336 } else {
288 try testing.expect(false); 337 try testing.expect(false);
289 } 338 }
290 var peek_iter = wb.iterateAfter(this_str, got_word); 339 var peek_iter = wb.iterateAfterWord(this_str, got_word);
291 const peek_1 = peek_iter.next(); 340 const peek_1 = peek_iter.next();
292 if (peek_1) |p1| { 341 if (peek_1) |p1| {
293 const peek_2 = iter.peek(); 342 const peek_2 = iter.peek();
@@ -313,7 +362,7 @@ test "Segmentation Word Iterator" {
313 got_word.bytes(this_str), 362 got_word.bytes(this_str),
314 this_word.bytes(this_str), 363 this_word.bytes(this_str),
315 ) catch |err| { 364 ) catch |err| {
316 debug.print("Wrong word on line {d} #{d} offset {d}\n", .{ line_iter.line, idx + 1, i }); 365 debug.print("Wrong word on line {d} #{d} offset {d}\n", .{ line_iter.line, idx, i });
317 return err; 366 return err;
318 }; 367 };
319 } 368 }
@@ -356,7 +405,7 @@ test "Segmentation Word Iterator" {
356 } else { 405 } else {
357 try testing.expect(false); 406 try testing.expect(false);
358 } 407 }
359 var peek_iter = wb.iterateBefore(this_str, got_word); 408 var peek_iter = wb.iterateBeforeWord(this_str, got_word);
360 const peek_1 = peek_iter.prev(); 409 const peek_1 = peek_iter.prev();
361 if (peek_1) |p1| { 410 if (peek_1) |p1| {
362 const peek_2 = r_iter.peek(); 411 const peek_2 = r_iter.peek();