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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2009-10-31 11:52:10 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20091031115210-crjd42sn6ezrj52c
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: memrchr.c,v 1.1.2.1 2008/03/20 21:42:39 joerg_wunsch Exp $      */
2
 
 
3
 
#ifndef __AVR__
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
9
 
#else
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__)
14
 
# else
15
 
   /* small AVR */
16
 
#  define PRINTFLN(args...)
17
 
# endif
18
 
# define EXIT   exit
19
 
#endif
20
 
 
21
 
#include <stdlib.h>
22
 
#include <string.h>
23
 
#include <stdio.h>
24
 
#include "progmem.h"
25
 
 
26
 
void Check (int line, const char *s, int c, size_t len, int expect)
27
 
{
28
 
    char t[320];
29
 
    char *p;
30
 
    
31
 
    if (len > sizeof(t))
32
 
        exit (1);
33
 
    memcpy_P (t, s, len);
34
 
    p = memrchr (t, c, len);
35
 
 
36
 
    if (expect < 0) {
37
 
        if (p) {
38
 
            PRINTFLN (line, "non zero pointer is returned");
39
 
            EXIT (line);
40
 
        }
41
 
    } else {
42
 
        if (p != t + expect) {
43
 
            PRINTFLN (line, "expect= %d  result= %d", expect, p - t);
44
 
            EXIT (line + 1000);
45
 
        }
46
 
    }
47
 
    if (memcmp_P (t, s, len)) {
48
 
        PRINTFLN (line, "string is changed");
49
 
        EXIT (line + 2000);
50
 
    }
51
 
}
52
 
 
53
 
#define CHECK(s, c, len, expect)        do {    \
54
 
    Check (__LINE__, PSTR(s), c, len, expect);  \
55
 
} while (0)
56
 
 
57
 
int main ()
58
 
{
59
 
    /* Not found        */
60
 
    CHECK ("", 0, 0, -1);
61
 
    CHECK ("", 255, 0, -1);
62
 
    CHECK ("ABCDEF", 'a', 6, -1);
63
 
    
64
 
    /* Found    */
65
 
    CHECK ("\000", 0, 1, 0);
66
 
    CHECK ("\001", 1, 1, 0);
67
 
    CHECK ("\377", 255, 1, 0);
68
 
    CHECK ("987654321", '7', 9, 2);
69
 
 
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);
74
 
 
75
 
    /* Last occurance   */
76
 
    CHECK ("abcdabcd", 'b', 8, 5);
77
 
    CHECK ("........", '.', 8, 7);
78
 
    
79
 
    /* 'c' converted to a char  */
80
 
    CHECK ("ABCDEF", 'A'+0x100, 6, 0);
81
 
    CHECK ("ABCDE\377", ~0, 6, 5);
82
 
    
83
 
    /* Very long string */
84
 
    CHECK ("................................................................"
85
 
           "................................................................"
86
 
           "................................................................"
87
 
           "...............................................................*"
88
 
           "................................................................",
89
 
           '*', 320, 255);
90
 
    CHECK ("................................................................"
91
 
           "................................................................"
92
 
           "................................................................"
93
 
           "................................................................"
94
 
           "*...............................................................",
95
 
           '*', 320, 256);
96
 
    CHECK ("................................................................"
97
 
           "................................................................"
98
 
           "................................................................"
99
 
           "................................................................"
100
 
           ".*..............................................................",
101
 
           '*', 320, 257);
102
 
 
103
 
    return 0;
104
 
}