Blame src/general/string_memmem.c

nsz 62a26a
#ifndef _GNU_SOURCE
nsz 62a26a
#define _GNU_SOURCE 1
nsz 62a26a
#endif
nsz 62a26a
#include <string.h>
nsz 62a26a
#include "test.h"
nsz 62a26a
nsz 62a26a
#define N(s, sub) { \
nsz 62a26a
	char *p = s; \
nsz 62a26a
	char *q = memmem(p, strlen(p), sub, strlen(sub)); \
nsz 62a26a
	if (q) \
nsz 62a26a
		error("memmem(%s,%s) returned str+%d, wanted 0\n", #s, #sub, q-p); \
nsz 62a26a
}
nsz 62a26a
nsz 62a26a
#define T(s, sub, n) { \
nsz 62a26a
	char *p = s; \
nsz 62a26a
	char *q = memmem(p, strlen(p), sub, strlen(sub)); \
nsz 62a26a
	if (q == 0) \
nsz 62a26a
		error("memmem(%s,%s) returned 0, wanted str+%d\n", #s, #sub, n); \
nsz 62a26a
	else if (q - p != n) \
nsz 62a26a
		error("memmem(%s,%s) returned str+%d, wanted str+%d\n", #s, #sub, q-p, n); \
nsz 62a26a
}
nsz 62a26a
nsz 62a26a
int main(void)
nsz 62a26a
{
nsz 62a26a
	N("", "a")
nsz 62a26a
	N("a", "aa")
nsz 62a26a
	N("a", "b")
nsz 62a26a
	N("aa", "ab")
nsz 62a26a
	N("aa", "aaa")
nsz 62a26a
	N("abba", "aba")
nsz 62a26a
	N("abc abc", "abcd")
nsz 62a26a
	N("0-1-2-3-4-5-6-7-8-9", "-3-4-56-7-8-")
nsz 62a26a
	N("0-1-2-3-4-5-6-7-8-9", "-3-4-5+6-7-8-")
nsz 62a26a
	N("_ _ _\xff_ _ _", "_\x7f_")
nsz 62a26a
	N("_ _ _\x7f_ _ _", "_\xff_")
nsz 62a26a
nsz 62a26a
	T("", "", 0)
nsz 62a26a
	T("abcd", "", 0)
nsz 62a26a
	T("abcd", "a", 0)
nsz 62a26a
	T("abcd", "b", 1)
nsz 62a26a
	T("abcd", "c", 2)
nsz 62a26a
	T("abcd", "d", 3)
nsz 62a26a
	T("abcd", "ab", 0)
nsz 62a26a
	T("abcd", "bc", 1)
nsz 62a26a
	T("abcd", "cd", 2)
nsz 62a26a
	T("ababa", "baba", 1)
nsz 62a26a
	T("ababab", "babab", 1)
nsz 62a26a
	T("abababa", "bababa", 1)
nsz 62a26a
	T("abababab", "bababab", 1)
nsz 62a26a
	T("ababababa", "babababa", 1)
nsz 62a26a
	T("nanabanabanana", "aba", 3)
nsz 62a26a
	T("nanabanabanana", "ban", 4)
nsz 62a26a
	T("nanabanabanana", "anab", 1)
nsz 62a26a
	T("nanabanabanana", "banana", 8)
nsz 62a26a
	T("_ _\xff_ _", "_\xff_", 2)
nsz 62a26a
nsz 62a26a
	return test_status;
nsz 62a26a
}