1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <stdio.h>
16 #include <string.h>
17 #include "test.h"
18
test_strlen_empty_string()19 void test_strlen_empty_string() {
20 if (strlen("") != 0) {
21 t_error("Empty string length should be 0\n");
22 }
23 }
24
test_strlen_with_spaces()25 void test_strlen_with_spaces() {
26 if (strlen(" ") != 1) {
27 t_error("Length of single space should be 1\n");
28 }
29 if (strlen(" ") != 2) {
30 t_error("Length of double space should be 2\n");
31 }
32 if (strlen(" a ") != 3) {
33 t_error("Length of ' a ' should be 3\n");
34 }
35 if (strlen(" a b ") != 5) {
36 t_error("Length of ' a b ' should be 5\n");
37 }
38 if (strlen(" ") != 4) {
39 t_error("Length of 4 spaces should be 4\n");
40 }
41 }
42
test_strlen_special_chars()43 void test_strlen_special_chars() {
44 if (strlen("!@#$%^&*()") != 10) {
45 t_error("Length of special chars string should be 10\n");
46 }
47 if (strlen("[]{}<>?|") != 8) {
48 t_error("Length of bracket string should be 8\n");
49 }
50 if (strlen("`~\\\"'") != 5) {
51 t_error("Length of escaped chars string should be 5\n");
52 }
53 }
54
test_strlen_escaped_chars()55 void test_strlen_escaped_chars() {
56 if (strlen("a\nb") != 3) {
57 t_error("Length of 'a\\nb' should be 3\n");
58 }
59 if (strlen("a\tb") != 3) {
60 t_error("Length of 'a\\tb' should be 3\n");
61 }
62 if (strlen("\n\n\n") != 3) {
63 t_error("Length of '\\n\\n\\n' should be 3\n");
64 }
65 if (strlen("line1\nline2") != 11) {
66 t_error("Length of multiline string should be 11\n");
67 }
68 if (strlen("tab\tseparated") != 13) {
69 t_error("Length of tab-separated string should be 13\n");
70 }
71 }
72
test_strlen_with_embedded_null()73 void test_strlen_with_embedded_null() {
74 const char str1[] = {'a', '\0', 'b', '\0'};
75 if (strlen(str1) != 1) {
76 t_error("Length of string with embedded null should be 1\n");
77 }
78
79 const char str2[] = {'h', 'e', '\0', 'l', 'l', 'o'};
80 if (strlen(str2) != 2) {
81 t_error("Length of string with early null should be 2\n");
82 }
83
84 const char str3[] = {'\0', 'x'};
85 if (strlen(str3) != 0) {
86 t_error("Length of null-first string should be 0\n");
87 }
88 }
89
test_strlen_long_string()90 void test_strlen_long_string() {
91 char str100[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
92 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\0";
93 if (strlen(str100) != 100) {
94 t_error("Length of 100-char string should be 100\n");
95 }
96 char str255[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
97 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
98 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
99 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
100 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
101 "aaaaa\0";
102 if (strlen(str255) != 255) {
103 t_error("Length of 255-char string should be 255 actually %d\n", strlen(str255));
104 }
105 }
106
test_strlen_char_arrays()107 void test_strlen_char_arrays() {
108 char s1[10] = "";
109 if (strlen(s1) != 0) {
110 t_error("Length of empty array should be 0\n");
111 }
112
113 char s2[10] = "hi";
114 if (strlen(s2) != 2) {
115 t_error("Length of 'hi' should be 2\n");
116 }
117
118 char s3[5] = {'x', 'y', 'z', 0, 'q'};
119 if (strlen(s3) != 3) {
120 t_error("Length of 'xyz' array should be 3\n");
121 }
122
123 char s4[] = {'h', 'e', 'l', 'l', 'o', 0};
124 if (strlen(s4) != 5) {
125 t_error("Length of 'hello' array should be 5\n");
126 }
127 }
128
test_strlen_boundary_manual()129 void test_strlen_boundary_manual() {
130 char s1[] = "a";
131 if (strlen(s1) != 1) {
132 t_error("Length of 'a' should be 1\n");
133 }
134
135 char s2[] = "aa";
136 if (strlen(s2) != 2) {
137 t_error("Length of 'aa' should be 2\n");
138 }
139
140 char s3[] = "aaa";
141 if (strlen(s3) != 3) {
142 t_error("Length of 'aaa' should be 3\n");
143 }
144
145 char s4[] = "aaaa";
146 if (strlen(s4) != 4) {
147 t_error("Length of 'aaaa' should be 4\n");
148 }
149
150 char s5[] = "aaaaa";
151 if (strlen(s5) != 5) {
152 t_error("Length of 'aaaaa' should be 5\n");
153 }
154
155 char s6[] = "aaaaaa";
156 if (strlen(s6) != 6) {
157 t_error("Length of 'aaaaaa' should be 6\n");
158 }
159
160 char s7[] = "aaaaaaa";
161 if (strlen(s7) != 7) {
162 t_error("Length of 'aaaaaaa' should be 7\n");
163 }
164
165 char s8[] = "aaaaaaaa";
166 if (strlen(s8) != 8) {
167 t_error("Length of 'aaaaaaaa' should be 8\n");
168 }
169
170 char s9[] = "aaaaaaaaa";
171 if (strlen(s9) != 9) {
172 t_error("Length of 'aaaaaaaaa' should be 9\n");
173 }
174
175 char s10[] = "aaaaaaaaaa";
176 if (strlen(s10) != 10) {
177 t_error("Length of 'aaaaaaaaaa' should be 10\n");
178 }
179 }
180
test_strlen_utf8()181 void test_strlen_utf8() {
182 if (strlen("你好") != 6) {
183 t_error("Length of Chinese '你好' should be 6\n");
184 }
185 if (strlen("世界") != 6) {
186 t_error("Length of Chinese '世界' should be 6\n");
187 }
188 if (strlen("你好世界") != 12) {
189 t_error("Length of Chinese '你好世界' should be 12\n");
190 }
191 }
192
test_strlen_misc_cases()193 void test_strlen_misc_cases() {
194 char s[] = "test\0hidden";
195 if (strlen(s) != 4) {
196 t_error("Length of 'test\\0hidden' should be 4\n");
197 }
198
199 const char *p = "pointer test";
200 if (strlen(p) != 12) {
201 t_error("Length of 'pointer test' should be 12\n");
202 }
203
204 char s2[] = {'t', 'e', 's', 't', 0};
205 if (strlen(s2) != 4) {
206 t_error("Length of 'test' array should be 4\n");
207 }
208
209 char s3[] = {0};
210 if (strlen(s3) != 0) {
211 t_error("Length of null array should be 0\n");
212 }
213 }
214
test_strlen_concatenated_literals()215 void test_strlen_concatenated_literals() {
216 if (strlen("hello" "world") != 10) {
217 t_error("Length of concatenated 'helloworld' should be 10\n");
218 }
219 if (strlen("foo" "bar" "baz") != 9) {
220 t_error("Length of concatenated 'foobarbaz' should be 9\n");
221 }
222 if (strlen("" "") != 0) {
223 t_error("Length of concatenated empty strings should be 0\n");
224 }
225 if (strlen("abc" "") != 3) {
226 t_error("Length of 'abc' concatenated with empty should be 3\n");
227 }
228 if (strlen("" "xyz") != 3) {
229 t_error("Length of empty concatenated with 'xyz' should be 3\n");
230 }
231 }
232
test_strlen_repeated_chars()233 void test_strlen_repeated_chars() {
234 char a[] = "aaaaaaaaaaaaaaaaaaaa";
235 if (strlen(a) != 20) {
236 t_error("Length of 20 'a's should be 20\n");
237 }
238
239 char b[] = "bbbbbbbbbbbbbbbbb";
240 if (strlen(b) != 17) {
241 t_error("Length of 17 'b's should be 17\n");
242 }
243
244 char c[] = "cccccccccccc";
245 if (strlen(c) != 12) {
246 t_error("Length of 12 'c's should be 12\n");
247 }
248 }
249
test_strlen_with_pointer_offsets()250 void test_strlen_with_pointer_offsets() {
251 char base[] = "abcdef";
252 if (strlen(base + 1) != 5) {
253 t_error("Length from offset 1 should be 5\n");
254 }
255 if (strlen(base + 2) != 4) {
256 t_error("Length from offset 2 should be 4\n");
257 }
258 if (strlen(base + 5) != 1) {
259 t_error("Length from offset 5 should be 1\n");
260 }
261 if (strlen(base + 6) != 0) {
262 t_error("Length from offset 6 should be 0\n");
263 }
264 }
265
test_strlen_static_consts()266 void test_strlen_static_consts() {
267 static const char *a = "const1";
268 if (strlen(a) != 6) {
269 t_error("Length of 'const1' should be 6\n");
270 }
271
272 static const char *b = "a slightly longer const string";
273 if (strlen(b) != strlen("a slightly longer const string")) {
274 t_error("Length of long const string mismatch\n");
275 }
276
277 static const char *c = "";
278 if (strlen(c) != 0) {
279 t_error("Length of empty const string should be 0\n");
280 }
281 }
282
test_strlen_stack_allocated()283 void test_strlen_stack_allocated() {
284 char a[6] = "hello";
285 if (strlen(a) != 5) {
286 t_error("Length of stack 'hello' should be 5\n");
287 }
288
289 char b[] = {'w', 'o', 'r', 'l', 'd', '\0'};
290 if (strlen(b) != 5) {
291 t_error("Length of stack 'world' should be 5\n");
292 }
293 }
294
test_strlen_with_backslash_sequences()295 void test_strlen_with_backslash_sequences() {
296 if (strlen("a\\b\\c") != 5) {
297 t_error("Length of 'a\\\\b\\\\c' should be 5\n");
298 }
299 if (strlen("line\\n") != 6) {
300 t_error("Length of 'line\\\\n' should be 6\n");
301 }
302 if (strlen("quote\\\"") != 7) {
303 t_error("Length of 'quote\\\\\"' should be 7\n");
304 }
305 }
306
test_strlen_inline_null_characters()307 void test_strlen_inline_null_characters() {
308 char a[] = {'A', 'B', 'C', '\0', 'D', 'E', '\0'};
309 if (strlen(a) != 3) {
310 t_error("Length of array with inline null should be 3\n");
311 }
312
313 char b[] = {'x', '\0', '\0', 'z'};
314 if (strlen(b) != 1) {
315 t_error("Length of array with early null should be 1\n");
316 }
317
318 char c[] = {'\0', 'a', 'b'};
319 if (strlen(c) != 0) {
320 t_error("Length of array with first null should be 0\n");
321 }
322 }
323
test_strlen_extended_ascii()324 void test_strlen_extended_ascii() {
325 char a[] = {127, 0};
326 if (strlen(a) != 1) {
327 t_error("Length of extended ASCII 127 should be 1\n");
328 }
329
330 char b[] = {(char)200, (char)201, 0};
331 if (strlen(b) != 2) {
332 t_error("Length of extended ASCII 200,201 should be 2\n");
333 }
334
335 char c[] = {(char)255, 0};
336 if (strlen(c) != 1) {
337 t_error("Length of extended ASCII 255 should be 1\n");
338 }
339 }
340
test_strlen_null_at_end_of_buffer()341 void test_strlen_null_at_end_of_buffer() {
342 char a[100];
343 memset(a, 'x', 99);
344 a[99] = '\0';
345 if (strlen(a) != 99) {
346 t_error("Length of 99 'x's should be 99\n");
347 }
348 }
349
test_strlen_partial_init_array()350 void test_strlen_partial_init_array() {
351 char s[10] = "abc";
352 if (strlen(s) != 3) {
353 t_error("Length of partially initialized 'abc' should be 3\n");
354 }
355
356 char t[20] = "abcdefgh";
357 if (strlen(t) != 8) {
358 t_error("Length of partially initialized 'abcdefgh' should be 8\n");
359 }
360 }
361
test_strlen_max_safe()362 void test_strlen_max_safe() {
363 char longbuf[256];
364 for (int i = 0; i < 255; ++i) {
365 longbuf[i] = 'a';
366 }
367 longbuf[255] = '\0';
368 if (strlen(longbuf) != 255) {
369 t_error("Length of 255 'a's should be 255\n");
370 }
371 }
372
test_strlen_manual_truncate()373 void test_strlen_manual_truncate() {
374 char s[] = "abcdefghij";
375 s[5] = '\0';
376 if (strlen(s) != 5) {
377 t_error("Length after truncate at 5 should be 5\n");
378 }
379
380 s[1] = '\0';
381 if (strlen(s) != 1) {
382 t_error("Length after truncate at 1 should be 1\n");
383 }
384 }
385
test_strlen_weird_printables()386 void test_strlen_weird_printables() {
387 char s1[] = "abc\tdef";
388 if (strlen(s1) != 7) {
389 t_error("Length of 'abc\\tdef' should be 7\n");
390 }
391
392 char s2[] = "abc\vdef";
393 if (strlen(s2) != 7) {
394 t_error("Length of 'abc\\vdef' should be 7\n");
395 }
396 }
397
test_strlen_whitespace_mix()398 void test_strlen_whitespace_mix() {
399 if (strlen(" ") != 4) {
400 t_error("Length of 4 spaces should be 4\n");
401 }
402 if (strlen(" \t \n ") != 5) {
403 t_error("Length of mixed whitespace should be 5\n");
404 }
405 if (strlen("\t\t\t") != 3) {
406 t_error("Length of 3 tabs should be 3\n");
407 }
408 }
409
test_strlen_reverse_embedded_null()410 void test_strlen_reverse_embedded_null() {
411 char s[] = {'z', 'y', 'x', '\0', 'w', 'v'};
412 if (strlen(s) != 3) {
413 t_error("Length with reverse embedded null should be 3\n");
414 }
415 }
416
test_strlen_manual_fill_buffer()417 void test_strlen_manual_fill_buffer() {
418 char s[6];
419 s[0] = 'a';
420 s[1] = 'b';
421 s[2] = 'c';
422 s[3] = 'd';
423 s[4] = '\0';
424 s[5] = 'x';
425 if (strlen(s) != 4) {
426 t_error("Length of manually filled buffer should be 4\n");
427 }
428 }
429
test_strlen_dynamic_init_short()430 void test_strlen_dynamic_init_short() {
431 char s[4] = {0};
432 s[0] = 'a';
433 s[1] = 'b';
434 s[2] = 'c';
435 s[3] = '\0';
436 if (strlen(s) != 3) {
437 t_error("Length of dynamically initialized buffer should be 3\n");
438 }
439 }
440
test_strlen_unicode_mix()441 void test_strlen_unicode_mix() {
442 if (strlen("a你b好c") != 9) {
443 t_error("Length of mixed Unicode string should be 9\n");
444 }
445 if (strlen("αβγ") != 6) {
446 t_error("Length of Greek letters should be 6\n");
447 }
448 }
449
test_strlen_very_long_string()450 void test_strlen_very_long_string() {
451 char very_long[1025];
452 memset(very_long, 'x', 1024);
453 very_long[1024] = '\0';
454 if (strlen(very_long) != 1024) {
455 t_error("Length of very long string should be 1024\n");
456 }
457 }
458
test_strlen_null_only()459 void test_strlen_null_only() {
460 char s[] = {'\0'};
461 if (strlen(s) != 0) {
462 t_error("Length of null-only string should be 0\n");
463 }
464 }
465
test_strlen_alternating_chars()466 void test_strlen_alternating_chars() {
467 char s[] = "a\0b\0c\0d";
468 if (strlen(s) != 1) {
469 t_error("Length of alternating chars string should be 1\n");
470 }
471 }
472
test_strlen_all_control_chars()473 void test_strlen_all_control_chars() {
474 char s[] = {1, 2, 3, 4, 5, 0};
475 if (strlen(s) != 5) {
476 t_error("Length of control chars string should be 5\n");
477 }
478 }
479
test_strlen_hex_chars()480 void test_strlen_hex_chars() {
481 char s[] = "\x41\x42\x43\x00\x44";
482 if (strlen(s) != 3) {
483 t_error("Length of hex string should be 3\n");
484 }
485 }
486
test_strlen_octal_chars()487 void test_strlen_octal_chars() {
488 char s[] = "\101\102\103\000\104";
489 if (strlen(s) != 3) {
490 t_error("Length of octal string should be 3\n");
491 }
492 }
493
test_strlen_one_past_null()494 void test_strlen_one_past_null() {
495 char s[] = "abc\0d";
496 if (strlen(s+4) != 1) {
497 t_error("Length past null should be 1\n");
498 }
499 }
500
test_strlen_empty_after_null()501 void test_strlen_empty_after_null() {
502 char s[] = "\0abc";
503 if (strlen(s+1) != 3) {
504 t_error("Length after null should be 3\n");
505 }
506 }
507
main()508 int main() {
509 test_strlen_empty_string();
510 test_strlen_with_spaces();
511 test_strlen_special_chars();
512 test_strlen_escaped_chars();
513 test_strlen_with_embedded_null();
514 test_strlen_long_string();
515 test_strlen_char_arrays();
516 test_strlen_boundary_manual();
517 test_strlen_utf8();
518 test_strlen_misc_cases();
519 test_strlen_concatenated_literals();
520 test_strlen_repeated_chars();
521 test_strlen_with_pointer_offsets();
522 test_strlen_static_consts();
523 test_strlen_stack_allocated();
524 test_strlen_with_backslash_sequences();
525 test_strlen_inline_null_characters();
526 test_strlen_extended_ascii();
527 test_strlen_null_at_end_of_buffer();
528 test_strlen_partial_init_array();
529 test_strlen_max_safe();
530 test_strlen_manual_truncate();
531 test_strlen_weird_printables();
532 test_strlen_whitespace_mix();
533 test_strlen_reverse_embedded_null();
534 test_strlen_manual_fill_buffer();
535 test_strlen_dynamic_init_short();
536 test_strlen_unicode_mix();
537 test_strlen_very_long_string();
538 test_strlen_null_only();
539 test_strlen_alternating_chars();
540 test_strlen_all_control_chars();
541 test_strlen_hex_chars();
542 test_strlen_octal_chars();
543 test_strlen_one_past_null();
544 test_strlen_empty_after_null();
545 return 0;
546 }