1 /* Test strncmp builtin expansion for compilation and proper execution. */
2 /* { dg-do run { target *-*-linux* *-*-gnu* } } */
3 /* { dg-options "-O2" } */
4 /* { dg-require-effective-target ptr32plus } */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/mman.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12
13 int lib_memcmp(const void *a, const void *b, size_t n) asm("memcmp");
14 int lib_strncmp(const char *a, const char *b, size_t n) asm("strncmp");
15
test_driver_strncmp(void (test_strncmp)(const char *,const char *,int),void (test_memcmp)(const void *,const void *,int),size_t sz)16 static void test_driver_strncmp (void (test_strncmp)(const char *, const char *, int),
17 void (test_memcmp)(const void *, const void *, int),
18 size_t sz)
19 {
20 long pgsz = sysconf(_SC_PAGESIZE);
21 char buf1[sz+1];
22 char *buf2;
23 #if _POSIX_C_SOURCE >= 200112L
24 if ( posix_memalign ((void **)&buf2, pgsz, 2*pgsz) ) abort ();
25 #else
26 if ( !(buf2 = valloc(2*pgsz))) abort ();
27 #endif
28 char *p2;
29 int r,i,e;
30
31 r = mprotect (buf2+pgsz,pgsz,PROT_NONE);
32 if (r < 0) abort();
33
34 memset(buf1,'A',sz);
35 for(i=10; i>=0; i--) {
36 p2 = buf2+pgsz-sz-i;
37 memset(p2,'A',sz);
38 e = lib_strncmp(buf1,p2,sz);
39 (*test_strncmp)(buf1,p2,e);
40 e = lib_memcmp(buf1,p2,sz);
41 (*test_memcmp)(buf1,p2,e);
42 }
43 free(buf2);
44 }
45
46 #define RUN_TEST(SZ) test_driver_strncmp (test_strncmp_ ## SZ, test_memcmp_ ## SZ, SZ);
47
48 #define DEF_TEST(SZ) \
49 __attribute__((noinline)) \
50 void test_strncmp_ ## SZ (const char *str1, const char *str2, int expect) \
51 { \
52 int r; \
53 r = strncmp(str1,str2,SZ); \
54 if ( r < 0 && !(expect < 0) ) abort(); \
55 if ( r > 0 && !(expect > 0) ) abort(); \
56 if ( r == 0 && !(expect == 0) ) abort(); \
57 } \
58 __attribute__((noinline)) \
59 void test_memcmp_ ## SZ (const void *p1, const void *p2, int expect) \
60 { \
61 int r; \
62 r = memcmp(p1,p2,SZ); \
63 if ( r < 0 && !(expect < 0) ) abort(); \
64 if ( r > 0 && !(expect > 0) ) abort(); \
65 if ( r == 0 && !(expect == 0) ) abort(); \
66 }
67
68 DEF_TEST(1)
69 DEF_TEST(2)
70 DEF_TEST(3)
71 DEF_TEST(4)
72 DEF_TEST(5)
73 DEF_TEST(6)
74 DEF_TEST(7)
75 DEF_TEST(8)
76 DEF_TEST(9)
77 DEF_TEST(10)
78 DEF_TEST(11)
79 DEF_TEST(12)
80 DEF_TEST(13)
81 DEF_TEST(14)
82 DEF_TEST(15)
83 DEF_TEST(16)
84 DEF_TEST(32)
85 DEF_TEST(64)
86 DEF_TEST(65)
87 DEF_TEST(66)
88 DEF_TEST(67)
89 DEF_TEST(68)
90 DEF_TEST(69)
91 DEF_TEST(70)
92 DEF_TEST(71)
93
94 int
main(int argc,char ** argv)95 main(int argc, char **argv)
96 {
97 RUN_TEST(1) ;
98 RUN_TEST(2) ;
99 RUN_TEST(3) ;
100 RUN_TEST(4) ;
101 RUN_TEST(5) ;
102 RUN_TEST(6) ;
103 RUN_TEST(7) ;
104 RUN_TEST(8) ;
105 RUN_TEST(9) ;
106 RUN_TEST(10);
107 RUN_TEST(11);
108 RUN_TEST(12);
109 RUN_TEST(13);
110 RUN_TEST(14);
111 RUN_TEST(15);
112 RUN_TEST(16);
113 RUN_TEST(32);
114 RUN_TEST(64);
115 RUN_TEST(65);
116 RUN_TEST(66);
117 RUN_TEST(67);
118 RUN_TEST(68);
119 RUN_TEST(69);
120 RUN_TEST(70);
121 RUN_TEST(71);
122 return 0;
123 }
124