File: strrev.c

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (120 lines) | stat: -rw-r--r-- 3,548 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* $Id: strrev.c,v 1.1 2007/03/01 13:10:55 dmix Exp $	*/

#ifndef __AVR__
# include <stdio.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "progmem.h"

#ifndef	__AVR__		/* strrev() is't a standart function	*/
char * strrev (char *s)
{
    char *p1, *p2;
    
    for (p2 = s; *p2; ) p2++;
    p1 = s;
    while (p1 < p2) {
	char c1 = *p1;
	char c2 = *--p2;
	*p1++ = c2;
	*p2 = c1;
    }
    return s;
}
#endif

void Check (int line, const char *s, const char *expect)
{
    char t[300];
    char *p;
    int code = 0;
    int i;

    for (i = 2; i; i--) {
	if (strlen_P(s) > sizeof(t) - 1)
	    exit (1);
        strcpy_P (t, s);
	p = strrev (t);
        if (p != t) {
	    code = line + 1000;
	    break;
	}
        if (strcmp_P (t, expect)) {
	    code = line;
	    break;
	}
	p = (char *)s;		/* change strings	*/
	s = expect;
	expect = p;
    }
    if (!code)
	return;
#if   !defined(__AVR__)
    printf ("\nLine %d: expect: \"%s\""
	    "\n         result: \"%s\"\n",
	    line, expect, t);
    if (code > 255) code = 255;
#elif	defined(DEBUG)
    exit ((int)t);
#endif
    exit (code);
}

#define CHECK(s, expect)	do {		\
    Check (__LINE__, PSTR(s), PSTR(expect));	\
} while (0)

int main ()
{
    /* Empty string.	*/
    CHECK ("", "");
    
    /* 1 char long	*/
    CHECK ("a", "a");
    CHECK ("\001", "\001");
    CHECK ("\377", "\377");
    
    /* 2 chars long	*/
    CHECK ("01", "10");
    CHECK ("**", "**");
    
    /* 3 and more chars long	*/
    CHECK ("abc", "cba");
    CHECK ("qwer", "rewq");
    CHECK ("12345", "54321");
    CHECK ("[];'./", "/.';][");
    CHECK ("\001\177\200\201\377", "\377\201\200\177\001");
    CHECK ("The quick brown fox jumps over the lazy dog.",
	   ".god yzal eht revo spmuj xof nworb kciuq ehT");

    /* Very long string.	*/
    CHECK ("1..............................................................2"
	   "3..............................................................4"
	   "5..............................................................6"
	   "7..............................................................",
	   "..............................................................7"
	   "6..............................................................5"
	   "4..............................................................3"
	   "2..............................................................1");

    CHECK ("1..............................................................2"
	   "3..............................................................4"
	   "5..............................................................6"
	   "7..............................................................8",
	   "8..............................................................7"
	   "6..............................................................5"
	   "4..............................................................3"
	   "2..............................................................1");

    CHECK ("1..............................................................2"
	   "3..............................................................4"
	   "5..............................................................6"
	   "7..............................................................89",
	   "98..............................................................7"
	   "6..............................................................5"
	   "4..............................................................3"
	   "2..............................................................1");
    
    return 0;
}