aboutsummaryrefslogtreecommitdiff
path: root/example/ndpiReader.c
diff options
context:
space:
mode:
authorVladimir Gavrilov <105977161+0xA50C1A1@users.noreply.github.com>2024-05-10 23:43:59 +0300
committerGitHub <noreply@github.com>2024-05-10 22:43:59 +0200
commita813121e0a7021cdbfd64630960b330a23b1a4d2 (patch)
tree7021b987d621b6940ebccc6738967654c399d477 /example/ndpiReader.c
parent4b4b358562a80b546d10f779dba8c56c5d0c6502 (diff)
`ndpi_strnstr()` optimization (#2433)
Diffstat (limited to 'example/ndpiReader.c')
-rw-r--r--example/ndpiReader.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c
index 60d13f919..7a326f71b 100644
--- a/example/ndpiReader.c
+++ b/example/ndpiReader.c
@@ -5658,6 +5658,43 @@ void strlcpyUnitTest() {
/* *********************************************** */
+void strnstrUnitTest(void) {
+ /* Test 1: null string */
+ assert(ndpi_strnstr(NULL, "find", 10) == NULL);
+ assert(ndpi_strnstr("string", NULL, 10) == NULL);
+
+ /* Test 2: empty substring */
+ assert(strcmp(ndpi_strnstr("string", "", 6), "string") == 0);
+
+ /* Test 3: single character substring */
+ assert(strcmp(ndpi_strnstr("string", "r", 6), "ring") == 0);
+ assert(ndpi_strnstr("string", "x", 6) == NULL);
+
+ /* Test 4: multiple character substring */
+ assert(strcmp(ndpi_strnstr("string", "ing", 6), "ing") == 0);
+ assert(ndpi_strnstr("string", "xyz", 6) == NULL);
+
+ /* Test 5: substring equal to the beginning of the string */
+ assert(strcmp(ndpi_strnstr("string", "str", 3), "string") == 0);
+
+ /* Test 6: substring at the end of the string */
+ assert(strcmp(ndpi_strnstr("string", "ing", 6), "ing") == 0);
+
+ /* Test 7: substring in the middle of the string */
+ assert(strcmp(ndpi_strnstr("hello world", "lo wo", 11), "lo world") == 0);
+
+ /* Test 8: repeated characters in the string */
+ assert(strcmp(ndpi_strnstr("aaaaaa", "aaa", 6), "aaaaaa") == 0);
+
+ /* Test 9: empty string and slen 0 */
+ assert(ndpi_strnstr("", "find", 0) == NULL);
+
+ /* Test 10: substring equal to the string */
+ assert(strcmp(ndpi_strnstr("string", "string", 6), "string") == 0);
+}
+
+/* *********************************************** */
+
void filterUnitTest() {
ndpi_filter* f = ndpi_filter_alloc();
u_int32_t v, i;
@@ -6024,6 +6061,7 @@ int main(int argc, char **argv) {
compressedBitmapUnitTest();
strtonumUnitTest();
strlcpyUnitTest();
+ strnstrUnitTest();
#endif
}