xref: /reactos/modules/rostests/apitests/crt/strlen.c (revision ccef43f3)
1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Test for strlen
5  * PROGRAMMER:      Timo Kreuzer <timo.kreuzer@reactos.org>
6  */
7 
8 #include <apitest.h>
9 
10 #include <stdio.h>
11 #include <tchar.h>
12 #include <pseh/pseh2.h>
13 #include <ntstatus.h>
14 typedef _Return_type_success_(return >= 0) long NTSTATUS, *PNTSTATUS;
15 
16 #ifdef __GNUC__
17 #pragma GCC diagnostic ignored "-Wnonnull"
18 
19 size_t
20 GCC_builtin_strlen(const char *str)
21 {
22     return __builtin_strlen(str);
23 }
24 #endif
25 
26 #define EFLAGS_DF 0x400L
27 
28 typedef size_t (*PFN_STRLEN)(const char *);
29 
30 void
31 Test_strlen(PFN_STRLEN pstrlen)
32 {
33     size_t len;
34 #if defined(_M_IX86) || defined(_M_AMD64)
35     volatile uintptr_t eflags;
36     char *teststr = "a\0bcdefghijk";
37 #endif
38 
39     /* basic parameter tests */
40     StartSeh()
41         len = pstrlen(NULL);
42     EndSeh(STATUS_ACCESS_VIOLATION);
43     (void)len;
44 
45     ok_int((int)pstrlen("test"), 4);
46 
47 #if defined(_M_IX86) || defined(_M_AMD64)
48     eflags = __readeflags();
49     __writeeflags(eflags | EFLAGS_DF);
50     len = pstrlen(teststr + 4);
51 
52 #ifdef _M_AMD64
53     ok((__readeflags() & EFLAGS_DF) != 0, "Direction flag in ELFAGS was changed.\n");
54 #else
55     ok((__readeflags() & EFLAGS_DF) == 0, "Direction flag in ELFAGS was not changed.\n");
56 #endif
57     __writeeflags(eflags);
58 
59     /* Only test this for the exported versions, intrinsics might do it
60        differently. It's up to us to not do fishy stuff! Also crtdll does
61        not do it like this. */
62 #ifndef TEST_CRTDLL
63     if (pstrlen == strlen)
64     {
65         ok(len == 8, "Should not have gone backwards (got len %i)", (int)len);
66     }
67 #endif // TEST_CRTDLL
68 #endif
69 }
70 
71 START_TEST(strlen)
72 {
73     Test_strlen(strlen);
74 #ifdef __GNUC__
75     Test_strlen(GCC_builtin_strlen);
76 #endif // __GNUC__
77 }
78