1
/* $Id: memrchr.c,v 1.1.2.1 2008/03/20 21:42:39 joerg_wunsch Exp $ */
4
# define _GNU_SOURCE /* to include memrchr() */
5
# define PRINTFLN(line, fmt, ...) \
6
printf("\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
7
# define EXIT(code) exit ((code) < 255 ? (code) : 255)
8
# define memcmp_P memcmp
10
# if defined(__AVR_ATmega128__)
11
/* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
12
# define PRINTFLN(line, fmt, ...) \
13
sprintf ((char *)0x2000, "\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
16
# define PRINTFLN(args...)
26
void Check (int line, const char *s, int c, size_t len, int expect)
34
p = memrchr (t, c, len);
38
PRINTFLN (line, "non zero pointer is returned");
42
if (p != t + expect) {
43
PRINTFLN (line, "expect= %d result= %d", expect, p - t);
47
if (memcmp_P (t, s, len)) {
48
PRINTFLN (line, "string is changed");
53
#define CHECK(s, c, len, expect) do { \
54
Check (__LINE__, PSTR(s), c, len, expect); \
61
CHECK ("", 255, 0, -1);
62
CHECK ("ABCDEF", 'a', 6, -1);
65
CHECK ("\000", 0, 1, 0);
66
CHECK ("\001", 1, 1, 0);
67
CHECK ("\377", 255, 1, 0);
68
CHECK ("987654321", '7', 9, 2);
70
/* '\0' has't a special sense */
71
CHECK ("12345", 0, 6, 5);
72
CHECK (".\000.", 0, 3, 1);
73
CHECK ("\000a\000b", 'b', 4, 3);
76
CHECK ("abcdabcd", 'b', 8, 5);
77
CHECK ("........", '.', 8, 7);
79
/* 'c' converted to a char */
80
CHECK ("ABCDEF", 'A'+0x100, 6, 0);
81
CHECK ("ABCDE\377", ~0, 6, 5);
83
/* Very long string */
84
CHECK ("................................................................"
85
"................................................................"
86
"................................................................"
87
"...............................................................*"
88
"................................................................",
90
CHECK ("................................................................"
91
"................................................................"
92
"................................................................"
93
"................................................................"
94
"*...............................................................",
96
CHECK ("................................................................"
97
"................................................................"
98
"................................................................"
99
"................................................................"
100
".*..............................................................",