1 /* Test strncmp builtin expansion for compilation and proper execution. */
2 /* { dg-do run { target *-*-linux* *-*-gnu* *-*-uclinux* } } */
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 mprotect (buf2+pgsz,pgsz,PROT_READ|PROT_WRITE);
44 free(buf2);
45 }
46
47 #define RUN_TEST(SZ) test_driver_strncmp (test_strncmp_ ## SZ, test_memcmp_ ## SZ, SZ);
48
49 #define DEF_TEST(SZ) \
50 __attribute__((noinline)) \
51 void test_strncmp_ ## SZ (const char *str1, const char *str2, int expect) \
52 { \
53 int r; \
54 r = strncmp(str1,str2,SZ); \
55 if ( r < 0 && !(expect < 0) ) abort(); \
56 if ( r > 0 && !(expect > 0) ) abort(); \
57 if ( r == 0 && !(expect == 0) ) abort(); \
58 } \
59 __attribute__((noinline)) \
60 void test_memcmp_ ## SZ (const void *p1, const void *p2, int expect) \
61 { \
62 int r; \
63 r = memcmp(p1,p2,SZ); \
64 if ( r < 0 && !(expect < 0) ) abort(); \
65 if ( r > 0 && !(expect > 0) ) abort(); \
66 if ( r == 0 && !(expect == 0) ) abort(); \
67 }
68
69 DEF_TEST(1)
70 DEF_TEST(2)
71 DEF_TEST(3)
72 DEF_TEST(4)
73 DEF_TEST(5)
74 DEF_TEST(6)
75 DEF_TEST(7)
76 DEF_TEST(8)
77 DEF_TEST(9)
78 DEF_TEST(10)
79 DEF_TEST(11)
80 DEF_TEST(12)
81 DEF_TEST(13)
82 DEF_TEST(14)
83 DEF_TEST(15)
84 DEF_TEST(16)
85 DEF_TEST(32)
86 DEF_TEST(64)
87 DEF_TEST(65)
88 DEF_TEST(66)
89 DEF_TEST(67)
90 DEF_TEST(68)
91 DEF_TEST(69)
92 DEF_TEST(70)
93 DEF_TEST(71)
94
95 int
main(int argc,char ** argv)96 main(int argc, char **argv)
97 {
98 RUN_TEST(1) ;
99 RUN_TEST(2) ;
100 RUN_TEST(3) ;
101 RUN_TEST(4) ;
102 RUN_TEST(5) ;
103 RUN_TEST(6) ;
104 RUN_TEST(7) ;
105 RUN_TEST(8) ;
106 RUN_TEST(9) ;
107 RUN_TEST(10);
108 RUN_TEST(11);
109 RUN_TEST(12);
110 RUN_TEST(13);
111 RUN_TEST(14);
112 RUN_TEST(15);
113 RUN_TEST(16);
114 RUN_TEST(32);
115 RUN_TEST(64);
116 RUN_TEST(65);
117 RUN_TEST(66);
118 RUN_TEST(67);
119 RUN_TEST(68);
120 RUN_TEST(69);
121 RUN_TEST(70);
122 RUN_TEST(71);
123 return 0;
124 }
125