1
+ /*
2
+ * Copyright (c) 2024-2025, Arm ltd
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * * Redistributions of source code must retain the above copyright notice,
8
+ * this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * * Neither the name of Intel Corporation nor the names of its contributors
13
+ * may be used to endorse or promote products derived from this software
14
+ * without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
+ * POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+
29
+ #include < cstdlib>
30
+ #include < cstring>
31
+ #include < iostream>
32
+
33
+ #include " hs_compile.h"
34
+ #include " hs_runtime.h"
35
+
36
+ #include " hwlm/hwlm_literal.h"
37
+ #include " hwlm/noodle_build.h"
38
+ #include " hwlm/noodle_engine.h"
39
+ #include " hwlm/noodle_internal.h"
40
+
41
+ const char *buf1 = " azertyuioperty" ;
42
+ int buf1_len = 14 ;
43
+ const char *buf2 = " AZERTYUIOPERTY" ;
44
+ int buf2_len = 14 ;
45
+
46
+ typedef struct context {
47
+ /* array of indices in the string where we expect match to be reported */
48
+ size_t *expected_array;
49
+ size_t array_size;
50
+ /* counter of matches hapenning at a position in expected_array */
51
+ size_t number_matched;
52
+ /* counter of matches hapenning at a position NOT in expected_array */
53
+ size_t number_wrong;
54
+ } context_t ;
55
+
56
+ int callback (unsigned int id, unsigned long long start,
57
+ unsigned long long end_offset, unsigned int flags,
58
+ void *raw_context) {
59
+ (void )id;
60
+ (void )start;
61
+ (void )flags;
62
+ context_t *context = (context_t *)raw_context;
63
+ bool matched = false ;
64
+ // Check if the match is expected
65
+ for (size_t i = 0 ; i < context->array_size ; i++) {
66
+ if (end_offset == context->expected_array [i]) {
67
+ matched = true ;
68
+ }
69
+ }
70
+ // Tally the right counter wether the match was expected or not
71
+ if (matched) {
72
+ context->number_matched += 1 ;
73
+ } else {
74
+ context->number_wrong += 1 ;
75
+ printf (" unplanned match at index %llu\n " , end_offset);
76
+ }
77
+
78
+ return CB_CONTINUE_MATCHING;
79
+ }
80
+
81
+ int test_noodle () {
82
+ const char *pattern = " ert" ;
83
+ hs_short_literal_compiled_pattern_t noodle_database;
84
+
85
+ hs_error_t ret =
86
+ hs_compile_short_literal_search (pattern, 3 , &noodle_database);
87
+ if (ret != HS_SUCCESS) {
88
+ printf (" Fail to build the pattern\n " );
89
+ return 1 ;
90
+ }
91
+
92
+ size_t expected_array[2 ] = {4 , 12 };
93
+ context_t context = {&(expected_array[0 ]), 2 , 0 , 0 };
94
+ ret = hs_short_literal_search (&noodle_database, buf1, buf1_len, nullptr ,
95
+ callback, &context);
96
+ if (ret != HS_SUCCESS) {
97
+ printf (" Fail to run noodle\n " );
98
+ return 1 ;
99
+ }
100
+ if (context.number_matched != context.array_size ) {
101
+ printf (" 1- missed some matches. Expected: %lu, got %lu\n " ,
102
+ context.array_size , context.number_matched );
103
+ }
104
+
105
+ expected_array[0 ] = 8 ;
106
+ context = {&(expected_array[0 ]), 1 , 0 , 0 };
107
+ ret = hs_short_literal_search (&noodle_database, buf1 + 4 , buf1_len - 4 ,
108
+ nullptr , callback, &context);
109
+ if (ret != HS_SUCCESS) {
110
+ printf (" Fail to run noodle\n " );
111
+ return 1 ;
112
+ }
113
+ if (context.number_matched != context.array_size ) {
114
+ printf (" 2- missed some matches. Expected: %lu, got %lu\n " ,
115
+ context.array_size , context.number_matched );
116
+ }
117
+
118
+ pattern = " ERT" ;
119
+ ret = hs_compile_short_literal_search (pattern, 3 , &noodle_database);
120
+ if (ret != HS_SUCCESS) {
121
+ printf (" Fail to build the pattern\n " );
122
+ return 1 ;
123
+ }
124
+
125
+ expected_array[0 ] = 4 ;
126
+ context = {&(expected_array[0 ]), 2 , 0 , 0 };
127
+ ret = hs_short_literal_search (&noodle_database, buf2, buf2_len, nullptr ,
128
+ callback, &context);
129
+ if (ret != HS_SUCCESS) {
130
+ printf (" Fail to run noodle\n " );
131
+ return 1 ;
132
+ }
133
+ if (context.number_matched != context.array_size ) {
134
+ printf (" 3- missed some matches. Expected: %lu, got %lu\n " ,
135
+ context.array_size , context.number_matched );
136
+ }
137
+
138
+ expected_array[0 ] = 8 ;
139
+ context = {&(expected_array[0 ]), 1 , 0 , 0 };
140
+ ret = hs_short_literal_search (&noodle_database, buf2 + 4 , buf2_len - 4 ,
141
+ nullptr , callback, &context);
142
+ if (ret != HS_SUCCESS) {
143
+ printf (" Fail to run noodle\n " );
144
+ return 1 ;
145
+ }
146
+ if (context.number_matched != context.array_size ) {
147
+ printf (" 4- missed some matches. Expected: %lu, got %lu\n " ,
148
+ context.array_size , context.number_matched );
149
+ }
150
+
151
+ return 0 ;
152
+ }
153
+
154
+ int main () {
155
+ // test_plain_noodle();
156
+ if (!test_noodle ()) {
157
+ printf (" all test passed\n " );
158
+ }
159
+
160
+ return 0 ;
161
+ }
0 commit comments