~ubuntu-branches/ubuntu/raring/avr-libc/raring-proposed

« back to all changes in this revision

Viewing changes to tests/simulate/string/strrev.c

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2009-10-31 11:52:10 UTC
  • mfrom: (1.1.8 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091031115210-x0mlijnegkce86fk
Tags: 1:1.6.7-1
* New upstream relese (closes: #544030)
* Added lintian overrides (closes: #553265)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: strrev.c,v 1.1 2007/03/01 13:10:55 dmix Exp $   */
2
 
 
3
 
#ifndef __AVR__
4
 
# include <stdio.h>
5
 
#endif
6
 
#include <stdlib.h>
7
 
#include <string.h>
8
 
#include "progmem.h"
9
 
 
10
 
#ifndef __AVR__         /* strrev() is't a standart function    */
11
 
char * strrev (char *s)
12
 
{
13
 
    char *p1, *p2;
14
 
    
15
 
    for (p2 = s; *p2; ) p2++;
16
 
    p1 = s;
17
 
    while (p1 < p2) {
18
 
        char c1 = *p1;
19
 
        char c2 = *--p2;
20
 
        *p1++ = c2;
21
 
        *p2 = c1;
22
 
    }
23
 
    return s;
24
 
}
25
 
#endif
26
 
 
27
 
void Check (int line, const char *s, const char *expect)
28
 
{
29
 
    char t[300];
30
 
    char *p;
31
 
    int code = 0;
32
 
    int i;
33
 
 
34
 
    for (i = 2; i; i--) {
35
 
        if (strlen_P(s) > sizeof(t) - 1)
36
 
            exit (1);
37
 
        strcpy_P (t, s);
38
 
        p = strrev (t);
39
 
        if (p != t) {
40
 
            code = line + 1000;
41
 
            break;
42
 
        }
43
 
        if (strcmp_P (t, expect)) {
44
 
            code = line;
45
 
            break;
46
 
        }
47
 
        p = (char *)s;          /* change strings       */
48
 
        s = expect;
49
 
        expect = p;
50
 
    }
51
 
    if (!code)
52
 
        return;
53
 
#if   !defined(__AVR__)
54
 
    printf ("\nLine %d: expect: \"%s\""
55
 
            "\n         result: \"%s\"\n",
56
 
            line, expect, t);
57
 
    if (code > 255) code = 255;
58
 
#elif   defined(DEBUG)
59
 
    exit ((int)t);
60
 
#endif
61
 
    exit (code);
62
 
}
63
 
 
64
 
#define CHECK(s, expect)        do {            \
65
 
    Check (__LINE__, PSTR(s), PSTR(expect));    \
66
 
} while (0)
67
 
 
68
 
int main ()
69
 
{
70
 
    /* Empty string.    */
71
 
    CHECK ("", "");
72
 
    
73
 
    /* 1 char long      */
74
 
    CHECK ("a", "a");
75
 
    CHECK ("\001", "\001");
76
 
    CHECK ("\377", "\377");
77
 
    
78
 
    /* 2 chars long     */
79
 
    CHECK ("01", "10");
80
 
    CHECK ("**", "**");
81
 
    
82
 
    /* 3 and more chars long    */
83
 
    CHECK ("abc", "cba");
84
 
    CHECK ("qwer", "rewq");
85
 
    CHECK ("12345", "54321");
86
 
    CHECK ("[];'./", "/.';][");
87
 
    CHECK ("\001\177\200\201\377", "\377\201\200\177\001");
88
 
    CHECK ("The quick brown fox jumps over the lazy dog.",
89
 
           ".god yzal eht revo spmuj xof nworb kciuq ehT");
90
 
 
91
 
    /* Very long string.        */
92
 
    CHECK ("1..............................................................2"
93
 
           "3..............................................................4"
94
 
           "5..............................................................6"
95
 
           "7..............................................................",
96
 
           "..............................................................7"
97
 
           "6..............................................................5"
98
 
           "4..............................................................3"
99
 
           "2..............................................................1");
100
 
 
101
 
    CHECK ("1..............................................................2"
102
 
           "3..............................................................4"
103
 
           "5..............................................................6"
104
 
           "7..............................................................8",
105
 
           "8..............................................................7"
106
 
           "6..............................................................5"
107
 
           "4..............................................................3"
108
 
           "2..............................................................1");
109
 
 
110
 
    CHECK ("1..............................................................2"
111
 
           "3..............................................................4"
112
 
           "5..............................................................6"
113
 
           "7..............................................................89",
114
 
           "98..............................................................7"
115
 
           "6..............................................................5"
116
 
           "4..............................................................3"
117
 
           "2..............................................................1");
118
 
    
119
 
    return 0;
120
 
}