1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
/* $Id: strchr.c,v 1.1 2007/02/22 13:45:11 dmix Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "progmem.h"
void Check (int line, const char *s, int c, int expect)
{
char t[300], *p;
strcpy_P (t, s);
p = strchr (t, c);
if (expect == -1 && !p)
return;
if (t + expect == p)
return;
#ifndef __AVR__
printf ("\nLine %d: expect: %d, result: %d\n",
line, expect, (p ? p-t : -1));
#endif
exit (line);
}
#define CHECK(s, c, expect) do { \
Check (__LINE__, PSTR(s), c, expect); \
} while (0)
int main ()
{
/* Not found */
CHECK ("", 1, -1);
CHECK ("", 255, -1);
CHECK ("ABCDEF", 'a', -1);
/* Found */
CHECK ("\001", 1, 0);
CHECK ("\377", 255, 0);
CHECK ("987654321", '7', 2);
/* '\0' is a part of string */
CHECK ("12345", 0, 5);
CHECK ("", 0, 0);
/* First occurance */
CHECK ("abcdabcd", 'b', 1);
/* 'c' converted to a char */
CHECK ("ABCDEF", 'A'+0x100, 0);
CHECK ("ABCDE\377", ~0, 5);
/* Very long string */
CHECK ("................................................................"
"................................................................"
"................................................................"
"................................................................"
"*...............", '*', 256);
return 0;
}
|