diff options
Diffstat (limited to 'hardregex-semantics.awk')
-rw-r--r-- | hardregex-semantics.awk | 233 |
1 files changed, 217 insertions, 16 deletions
diff --git a/hardregex-semantics.awk b/hardregex-semantics.awk index 84fcef93..fc8ba805 100644 --- a/hardregex-semantics.awk +++ b/hardregex-semantics.awk @@ -44,7 +44,7 @@ function simple_tests( fbre, numresult, strresult) else print "variable as numeric value: fail" - # Use as string value, should be string value of text + # Use as string value, should be string value of regexp text strresult = "<" fbre ">" if (strresult == "<fo+ba+r>") print "variable as string value: ok" @@ -75,21 +75,222 @@ function simple_tests( fbre, numresult, strresult) print "typeof variable after conversion: fail" } +function match_tests( fbre, fun) +{ + if (match("foobaaar", @/fo+ba+r/)) + print "match(constant): ok" + else + print "match(constant): fail" + + fbre = @/fo+ba+r/ + if (match("foobaaar", fbre)) + print "match(variable): ok" + else + print "match(variable): fail" + + fun = "match" + if (@fun("foobaaar", @/fo+ba+r/)) + print "match(constant) indirect: ok" + else + print "match(constant) indirect: fail" + + if (@fun("foobaaar", fbre)) + print "match(variable) indirect: ok" + else + print "match(variable) indirect: fail" +} + +function sub_tests( fbre, count, target, fun) +{ + target = "abc foobaar def foobar ghi" + count = sub(@/fo+ba+r/, "XX", target) + if (count == 1 && target == "abc XX def foobar ghi") + print "sub(constant): ok" + else + print "sub(constant): fail" + + fbre = @/fo+ba+r/ + target = "abc foobaar def foobar ghi" + count = sub(fbre, "XX", target) + if (count == 1 && target == "abc XX def foobar ghi") + print "sub(variable): ok" + else + print "sub(variable): fail" + + fun = "sub" + $0 = "abc foobaar def foobar ghi" + count = @fun(@/fo+ba+r/, "XX") + if (count == 1 && $0 == "abc XX def foobar ghi") + print "sub(constant) indirect: ok" + else + print "sub(constant) indirect: fail" + + $0 = "abc foobaar def foobar ghi" + count = @fun(fbre, "XX") + if (count == 1 && $0 == "abc XX def foobar ghi") + print "sub(variable) indirect: ok" + else + print "sub(variable) indirect: fail" +} + +function gsub_tests( fbre, count, target, fun) +{ + target = "abc foobaar def foobar ghi" + count = gsub(@/fo+ba+r/, "XX", target) + if (count == 2 && target == "abc XX def XX ghi") + print "gsub(constant): ok" + else + print "gsub(constant): fail" + + fbre = @/fo+ba+r/ + target = "abc foobaar def foobar ghi" + count = gsub(fbre, "XX", target) + if (count == 2 && target == "abc XX def XX ghi") + print "gsub(variable): ok" + else + print "gsub(variable): fail" + + fun = "gsub" + $0 = "abc foobaar def foobar ghi" + count = @fun(@/fo+ba+r/, "XX") + if (count == 2 && $0 == "abc XX def XX ghi") + print "gsub(constant) indirect: ok" + else + print "gsub(constant) indirect: fail" + + $0 = "abc foobaar def foobar ghi" + count = @fun(fbre, "XX") + if (count == 2 && $0 == "abc XX def XX ghi") + print "gsub(variable) indirect: ok" + else + print "gsub(variable) indirect: fail" +} + +function gensub_tests( fbre, result, target, fun) +{ + target = "abc foobaar def foobar ghi" + result = gensub(@/fo+ba+r/, "XX", "g", target) + if (result == "abc XX def XX ghi") + print "gensub(constant): ok" + else + print "gensub(constant): fail" + + fbre = @/fo+ba+r/ + target = "abc foobaar def foobar ghi" + result = gensub(fbre, "XX", "g", target) + if (result == "abc XX def XX ghi") + print "gensub(variable): ok" + else + print "gensub(variable): fail" + + fun = "gensub" + $0 = "abc foobaar def foobar ghi" + result = @fun(@/fo+ba+r/, "XX", "g") + if (result == "abc XX def XX ghi") + print "gensub(constant) indirect: ok" + else + print "gensub(constant) indirect: fail" + + $0 = "abc foobaar def foobar ghi" + result = @fun(fbre, "XX", "g") + if (result == "abc XX def XX ghi") + print "gensub(variable) indirect: ok" + else + print "gensub(variable) indirect: fail" + + result = @fun(@/fo+ba+r/, "XX", "g", target) + if (result == "abc XX def XX ghi") + print "gensub(constant) indirect 2: ok" + else + print "gensub(constant) indirect 2: fail" + + result = @fun(fbre, "XX", "g", target) + if (result == "abc XX def XX ghi") + print "gensub(variable) indirect 2: ok" + else + print "gensub(variable) indirect 2: fail" +} + +function split_tests( fbre, data, seps, fun, b1) +{ + delete data + delete seps + b1 = split("a:b:c:d", data, @/:/, seps) + if (b1 == 4 && data[1] == "a" && seps[1] == ":") + print "split(constant): ok" + else + print "split(constant): fail" + + delete data + delete seps + fbre = @/:/ + b1 = split("a:b:c:d", data, fbre, seps) + if (b1 == 4 && data[1] == "a" && seps[1] == ":") + print "split(variable): ok" + else + print "split(variable): fail" + + fun = "split" + delete data + delete seps + b1 = @fun("a:b:c:d", data, @/:/, seps) + if (b1 == 4 && data[1] == "a" && seps[1] == ":") + print "split(constant) indirect: ok" + else + print "split(constant) indirect: fail" + + delete data + delete seps + b1 = @fun("a:b:c:d", data, fbre, seps) + if (b1 == 4 && data[1] == "a" && seps[1] == ":") + print "split(variable) indirect: ok" + else + print "split(variable) indirect: fail" +} + +function patsplit_tests( fbre, data, seps, fun, b1) +{ + delete data + delete seps + b1 = patsplit("a:b:c:d", data, @/[a-z]+/, seps) + if (b1 == 4 && data[1] == "a" && seps[1] == ":") + print "patsplit(constant): ok" + else + print "patsplit(constant): fail" + + delete data + delete seps + fbre = @/[a-z]+/ + b1 = patsplit("a:b:c:d", data, fbre, seps) + if (b1 == 4 && data[1] == "a" && seps[1] == ":") + print "patsplit(variable): ok" + else + print "patsplit(variable): fail" + + fun = "patsplit" + delete data + delete seps + b1 = @fun("a:b:c:d", data, @/[a-z]+/, seps) + if (b1 == 4 && data[1] == "a" && seps[1] == ":") + print "patsplit(constant) indirect: ok" + else + print "patsplit(constant) indirect: fail" + + delete data + delete seps + b1 = @fun("a:b:c:d", data, fbre, seps) + if (b1 == 4 && data[1] == "a" && seps[1] == ":") + print "patsplit(variable) indirect: ok" + else + print "patsplit(variable) indirect: fail" +} + BEGIN { simple_tests() - - # use with match, constant - # use with match, variable - # use with sub, constant - # use with sub, variable - # use with gsub, constant - # use with gsub, variable - # use with gensub, constant - # use with gensub, variable - # use with split, constant - # use with split, variable - # use with patsplit, constant - # use with patsplit, variable - - # indirect call tests... + match_tests() + sub_tests() + gsub_tests() + gensub_tests() + split_tests() + patsplit_tests() } |