1
/* $Id: memcmp.c,v 1.1 2007/02/24 14:20:17 dmix Exp $ */
10
void Check (int line, const char *s1, const char *s2, size_t len, int expect)
15
if (len > sizeof(t1) || len > sizeof(t2))
17
memcpy_P (t1, s1, len ? len : 1);
18
memcpy_P (t2, s2, len ? len : 1);
19
i = memcmp (t1, t2, len);
23
/* Glibc memcmp() returns: -1/0/+1. */
24
if ( (i < 0 && expect < 0)
25
|| (i > 0 && expect > 0)
26
|| (i == 0 && expect == 0) )
28
printf ("\nLine %d: expect: %d, result: %d\n",
30
if (line > 255) line = 255;
37
#define CHECK(s1, s2, len, expect) do { \
38
Check (__LINE__, PSTR(s1), PSTR(s2), len, expect); \
45
CHECK ("A", "A", 0, 0);
46
CHECK ("B", "C", 0, 0);
49
CHECK ("\000", "\001", 1, -1);
50
CHECK ("\000", "\377", 1, -255);
51
CHECK ("\001", "\000", 1, 1);
52
CHECK ("\377", "\000", 1, 255);
55
CHECK ("\001", "\001", 1, 0);
56
CHECK ("\001", "\001", 2, 0);
57
CHECK ("1234\377", "1234\377", 1, 0);
58
CHECK ("1234\377+", "1234\377-", 5, 0);
60
/* Args are not equal. */
61
CHECK ("ABCDEF", "ABCDE\000", 6, 'F');
62
CHECK ("abcde\000", "abcdef", 6, -'f');
63
CHECK ("12345", "12245", 5, 1);
65
/* Case is important. */
66
CHECK ("xyz", "xYz", 3, 'y'-'Y');
67
CHECK ("QUICK", "qUICK", 5, 'Q'-'q');
69
/* Chars are unsigned */
70
CHECK ("\200", "\177", 1, 1);
71
CHECK ("\177", "\200", 1, -1);
72
CHECK ("\001", "\377", 1, -254);
73
CHECK ("\377", "\001", 1, 254);
75
/* '\0' is possible in the middle of array */
76
CHECK ("\000ABC\000D", "\000ABC\000C", 5, 0);
77
CHECK ("\000ABC\000D", "\000ABC\000C", 6, 1);