Add missing zstd.h to coregrind Makefile.am noinst_HEADERS
[valgrind.git] / memcheck / tests / memmem.c
bloba4f804f7f25aca6e7d460ab5343ea376c177c7e3
1 #define _GNU_SOURCE
2 #include <assert.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include "../../config.h"
7 /* mallocs an mem block and fills it with A. A needs to be a zero
8 terminated string. The A string chars, minus the terminating zero
9 are copied into the returned mem block. */
10 static void *
11 create_mem (const char *a)
13 size_t len = strlen (a);
14 void *mem = malloc (len);
15 memcpy (mem, a, len);
16 return mem;
19 int
20 main ()
22 char *haystack;
23 char *needle;
25 haystack = create_mem ("a");
26 needle = create_mem ("a");
27 #if defined(VGO_darwin) || defined(VGO_solaris)
29 * macOS manpage says
30 * If big_len is smaller than little_len, if little_len is 0, if big_len is 0 or if
31 * little occurs nowhere in big, NULL is returned; otherwise a pointer to the first
32 * character of the first occurrence of little is returned.
34 assert (memmem (haystack, 0, needle, 0) == NULL);
35 assert (memmem (haystack, 1, needle, 0) == NULL);
36 #else
37 assert (memmem (haystack, 0, needle, 0) == haystack);
38 assert (memmem (haystack, 1, needle, 0) == haystack);
39 #endif
40 assert (memmem (haystack, 0, needle, 1) == NULL);
41 assert (memmem (haystack, 1, needle, 1) == haystack);
42 free (haystack);
43 free (needle);
45 haystack = create_mem ("abc");
46 needle = create_mem ("bc");
47 #if defined(VGO_darwin) || defined(VGO_solaris)
48 assert (memmem (haystack, 3, needle, 0) == NULL);
49 #else
50 assert (memmem (haystack, 3, needle, 0) == haystack);
51 #endif
52 assert (memmem (haystack, 3, needle, 2) == haystack + 1);
53 assert (memmem (haystack + 1, 2, needle, 2) == haystack + 1);
54 assert (memmem (haystack + 2, 1, needle, 2) == NULL);
55 free (haystack);
56 free (needle);
58 haystack = create_mem ("abcabcabc");
59 needle = create_mem ("bca");
60 assert (memmem (haystack, 9, needle, 3) == haystack + 1);
61 free (haystack);
62 free (needle);
64 haystack = create_mem ("abcabcabc");
65 needle = create_mem ("bcad");
66 assert (memmem (haystack, 9, needle, 4) == NULL);
67 free (haystack);
68 free (needle);
70 haystack = create_mem ("xxxxxxxxxxxxxxxxxABC");
71 needle = create_mem ("ABCD");
72 assert (memmem (haystack, 20, needle, 2) == haystack + 17);
73 assert (memmem (haystack + 3, 17, needle, 2) == haystack + 17);
74 assert (memmem (haystack + 15, 5, needle, 2) == haystack + 17);
75 assert (memmem (haystack, 20, needle, 3) == haystack + 17);
76 assert (memmem (haystack + 3, 17, needle, 3) == haystack + 17);
77 assert (memmem (haystack + 15, 5, needle, 3) == haystack + 17);
78 assert (memmem (haystack, 20, needle, 4) == NULL);
79 assert (memmem (haystack + 3, 5, needle, 4) == NULL);
80 assert (memmem (haystack + 15, 5, needle, 4) == NULL);
81 free (haystack);
82 free (needle);
84 return 0;