summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/58_index
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-05-30 15:49:18 +0200
committerJo-Philipp Wich <jo@mein.io>2022-05-30 16:02:13 +0200
commitda3f089e94203dd1e7c86f457d9faf4c2293d5d8 (patch)
tree79f8b047c2a5dd5b80097dbf2263bcc46774f46c /tests/custom/03_stdlib/58_index
parent081871e18db544e2c834a516950e1c16c9bc4d2c (diff)
lib: rework uc_index() implementation
- Fix segfault on passing string haystack with non-string needle argument - Perform strict equality tests against array haystacks - Make string searches binary safe - Improve left index string search performance - Improve right index array search performance - Add missing test coverage for index() and rindex() Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/03_stdlib/58_index')
-rw-r--r--tests/custom/03_stdlib/58_index48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/custom/03_stdlib/58_index b/tests/custom/03_stdlib/58_index
new file mode 100644
index 0000000..30c5146
--- /dev/null
+++ b/tests/custom/03_stdlib/58_index
@@ -0,0 +1,48 @@
+The `index()` function locates an element within a given array or a substring
+position within a given string, depending on the type of arguments given.
+
+Returns `null` if the given haystack argument is neither an array nor a string,
+returns `-1` if the element was not found within the array or the substring was
+not found within the string.
+
+Returns the first found index position in all other cases.
+
+-- Testcase --
+{%
+ let o = {};
+
+ printf("%.J\n", [
+ index([ 1, 2, "abc", 3, "abc", 1, 2 ], "abc"), // should return 2
+ index([ 1, 2, 3 ], 4), // should return -1
+ index([ [], {} ], {}), // should return -1 (strict equality)
+ index([ [], o ], o), // should return 1 (strict equality)
+
+ index("foobarfoobarfoobar", "arf"), // should return 4
+ index("test", "hello"), // should return -1
+ index("test", "test"), // should return 0 (needle = haystack length special case)
+ index("test", ""), // should return 0 (zero length needle special case)
+ index("", ""), // should return 0 (zero length special case)
+ index("foo\0foo\0foo", "o\0f"), // should return 2 (binary safe)
+
+ index({ test: true }, true), // should return null
+ index(1234, 3), // should return null
+ ]);
+%}
+-- End --
+
+-- Expect stdout --
+[
+ 2,
+ -1,
+ -1,
+ 1,
+ 4,
+ -1,
+ 0,
+ 0,
+ 0,
+ 2,
+ null,
+ null
+]
+-- End --