From bf63e9b09369e10ccee1a929be7e413fb81fa956 Mon Sep 17 00:00:00 2001 From: Ycros <18012+ycros@users.noreply.github.com> Date: Wed, 4 Dec 2024 14:35:53 +1100 Subject: [PATCH] Add test for regex preallocated captures. --- .../core/text/regex/test_core_text_regex.odin | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/core/text/regex/test_core_text_regex.odin b/tests/core/text/regex/test_core_text_regex.odin index dfc9224a84b..3e714540639 100644 --- a/tests/core/text/regex/test_core_text_regex.odin +++ b/tests/core/text/regex/test_core_text_regex.odin @@ -1043,3 +1043,40 @@ test_us_phone_number :: proc(t: ^testing.T) { EXPR :: `^[2-9]\d{2}-\d{3}-\d{4}$` check_expression(t, EXPR, "650-253-0001", "650-253-0001") } + +@test +test_preallocated_capture :: proc(t: ^testing.T) { + capture := regex.preallocate_capture() + defer regex.destroy(capture) + + for pos in capture.pos { + testing.expect_value(t, pos, [2]int{0, 0}) + } + for group in capture.groups { + testing.expect_value(t, group, "") + } + + rex, parse_err := regex.create(`f(o)ob(ar)`) + if !testing.expect_value(t, parse_err, nil) { + return + } + defer regex.destroy(rex) + + num_groups, success := regex.match_with_preallocated_capture(rex, "foobar", &capture) + testing.expect_value(t, num_groups, 3) + testing.expect_value(t, success, true) + + testing.expect_value(t, capture.pos[0], [2]int{0, 6}) + testing.expect_value(t, capture.pos[1], [2]int{1, 2}) + testing.expect_value(t, capture.pos[2], [2]int{4, 6}) + for pos in capture.pos[3:] { + testing.expect_value(t, pos, [2]int{0, 0}) + } + + testing.expect_value(t, capture.groups[0], "foobar") + testing.expect_value(t, capture.groups[1], "o") + testing.expect_value(t, capture.groups[2], "ar") + for groups in capture.groups[3:] { + testing.expect_value(t, groups, "") + } +}