aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2015-04-14 15:06:41 +0300
committerArnold D. Robbins <arnold@skeeve.com>2015-04-14 15:06:41 +0300
commitaca30f7d82ec4fa002c6ab5ea4a2d9d77d28c2cd (patch)
tree49a3c25bc4460088e6dcc23f40bebe044ab16e67
parentc187863b95bed5f750b08df898fdfb611a4bdb54 (diff)
downloadegawk-aca30f7d82ec4fa002c6ab5ea4a2d9d77d28c2cd.tar.gz
egawk-aca30f7d82ec4fa002c6ab5ea4a2d9d77d28c2cd.tar.bz2
egawk-aca30f7d82ec4fa002c6ab5ea4a2d9d77d28c2cd.zip
Add more tests, make them work. Almost done...
-rw-r--r--ChangeLog4
-rw-r--r--builtin.c12
-rw-r--r--hardregex-semantics.awk233
3 files changed, 229 insertions, 20 deletions
diff --git a/ChangeLog b/ChangeLog
index 4349a973..606f54b5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,10 @@
Unrelated:
* builtin.c (call_sub): Fix for indirect gensub, 3 args now works.
+ Unrelated:
+ * builtin.c (call_sub, call_match, call_split_func): Allow for
+ regex to be Node_hardregex.
+
2015-04-13 Arnold D. Robbins <arnold@skeeve.com>
* regcomp.c (analyze): Prevent malloc(0).
diff --git a/builtin.c b/builtin.c
index db62cb57..12a79177 100644
--- a/builtin.c
+++ b/builtin.c
@@ -3106,7 +3106,8 @@ call_sub(const char *name, int nargs)
* push replace
* push $0
*/
- regex = make_regnode(Node_regex, regex);
+ if (regex->type != Node_hardregex)
+ regex = make_regnode(Node_regex, regex);
PUSH(regex);
PUSH(replace);
lhs = r_get_field(zero, (Func_ptr *) 0, true);
@@ -3130,7 +3131,8 @@ call_sub(const char *name, int nargs)
* nargs++
* }
*/
- regex = make_regnode(Node_regex, regex);
+ if (regex->type != Node_hardregex)
+ regex = make_regnode(Node_regex, regex);
PUSH(regex);
PUSH(replace);
PUSH(glob_flag);
@@ -3167,7 +3169,8 @@ call_match(int nargs)
/* Don't need to pop the string just to push it back ... */
- regex = make_regnode(Node_regex, regex);
+ if (regex->type != Node_hardregex)
+ regex = make_regnode(Node_regex, regex);
PUSH(regex);
if (array)
@@ -3195,7 +3198,8 @@ call_split_func(const char *name, int nargs)
if (nargs >= 3) {
regex = POP_STRING();
- regex = make_regnode(Node_regex, regex);
+ if (regex->type != Node_hardregex)
+ regex = make_regnode(Node_regex, regex);
} else {
if (name[0] == 's') {
regex = make_regnode(Node_regex, FS_node->var_value);
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()
}