summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/stringto01/stringto_test_template.h
blob: 1b951f72bb5369b5ab796e48ee1272494cdfa98a (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 *  COPYRIGHT (c) 1989-2009.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#if defined(STRING_TO_POINTER)
  #define GOOD_VALUE        0x123
  #define GOOD_VALUE_STRING "0x123"
  #define BAD_VALUE_STRING  "xxx"
#elif defined(STRING_TO_INTEGER)
  #define GOOD_VALUE        123
  #define GOOD_VALUE_STRING "123"
  #define BAD_VALUE_STRING  "YYY"
#elif defined(STRING_TO_FLOAT)
  #define GOOD_VALUE        1.23
  #define GOOD_VALUE_STRING "1.23"
  #define BAD_VALUE_STRING  "zzz"
#else
  #error "what type are we testing?"
#endif

void TEST_STRING_TO_NAME(void)
{
  TEST_STRING_TO_TYPE  value;
  rtems_status_code    status;
  char                *endptr;

  puts( "\nTesting " STRING_TO_NAME_METHOD_STRING );

  /* Null pointer for return value */
  puts(
    STRING_TO_NAME_METHOD_STRING
    " - NULL return value - RTEMS_INVALID_ADDRESS"
  );
  #if defined(STRING_TO_INTEGER)
    status = STRING_TO_NAME_METHOD( GOOD_VALUE_STRING, NULL, &endptr, 10 );
  #elif defined(STRING_TO_POINTER) || defined(STRING_TO_FLOAT)
    status = STRING_TO_NAME_METHOD( GOOD_VALUE_STRING, NULL, &endptr );
  #endif
  rtems_test_assert( status == RTEMS_INVALID_ADDRESS );

  /* Basic conversion works for return value, return end pointer */
  puts(
    STRING_TO_NAME_METHOD_STRING " - " GOOD_VALUE_STRING
    " NULL endptr return value - RTEMS_SUCCESSFUL"
  );
  #if defined(STRING_TO_INTEGER)
    status = STRING_TO_NAME_METHOD( GOOD_VALUE_STRING, &value, NULL, 10 );
  #elif defined(STRING_TO_POINTER) || defined(STRING_TO_FLOAT)
    status = STRING_TO_NAME_METHOD( GOOD_VALUE_STRING, &value, NULL );
  #endif
  rtems_test_assert( status == RTEMS_SUCCESSFUL );
  rtems_test_assert( value == (TEST_STRING_TO_TYPE)GOOD_VALUE );

  #if defined(STRING_TO_MAX)
    /* Basic conversion works for return value */
    endptr = NULL;
    puts(
      STRING_TO_NAME_METHOD_STRING " - MAXIMUM VALUE"
      " w/endptr return value - RTEMS_SUCCESSFUL"
    );
    #if defined(STRING_TO_INTEGER)
      status = STRING_TO_NAME_METHOD(
        STRING_TO_MAX_STRING, &value, &endptr, 10 );
    #elif defined(STRING_TO_POINTER) || defined(STRING_TO_FLOAT)
      status = STRING_TO_NAME_METHOD( STRING_TO_MAX_STRING, &value, &endptr );
    #endif
    rtems_test_assert( status == RTEMS_SUCCESSFUL );
    rtems_test_assert( endptr );
    rtems_test_assert( value == (TEST_STRING_TO_TYPE)STRING_TO_MAX );
  #endif

  /* Basic conversion works for return value */
  endptr = NULL;
  puts(
    STRING_TO_NAME_METHOD_STRING " - " GOOD_VALUE_STRING
    " w/endptr return value - RTEMS_SUCCESSFUL"
  );
  #if defined(STRING_TO_INTEGER)
    status = STRING_TO_NAME_METHOD( GOOD_VALUE_STRING, &value, &endptr, 10 );
  #elif defined(STRING_TO_POINTER) || defined(STRING_TO_FLOAT)
    status = STRING_TO_NAME_METHOD( GOOD_VALUE_STRING, &value, &endptr );
  #endif
  rtems_test_assert( status == RTEMS_SUCCESSFUL );
  rtems_test_assert( endptr );
  rtems_test_assert( value == (TEST_STRING_TO_TYPE)GOOD_VALUE );

  /* Bad conversion works for return value */
  endptr = NULL;
  puts(
    STRING_TO_NAME_METHOD_STRING " - " BAD_VALUE_STRING
    " w/endptr return value - RTEMS_NOT_DEFINED"
  );
  #if defined(STRING_TO_INTEGER)
    status = STRING_TO_NAME_METHOD( BAD_VALUE_STRING, &value, &endptr, 10 );
  #elif defined(STRING_TO_POINTER) || defined(STRING_TO_FLOAT)
    status = STRING_TO_NAME_METHOD( BAD_VALUE_STRING, &value, &endptr );
  #endif
  rtems_test_assert( status == RTEMS_NOT_DEFINED );
  rtems_test_assert( endptr );

  /* Conversion of empty string */
  endptr = NULL;
  value = 0;
  puts(
    STRING_TO_NAME_METHOD_STRING
    " - empty string - w/endptr return value - RTEMS_NOT_DEFINED"
  );
  #if defined(STRING_TO_INTEGER)
    status = STRING_TO_NAME_METHOD( "", &value, &endptr, 10 );
  #elif defined(STRING_TO_POINTER) || defined(STRING_TO_FLOAT)
    status = STRING_TO_NAME_METHOD( "", &value, &endptr );
  #endif
  rtems_test_assert( status == RTEMS_NOT_DEFINED );
  rtems_test_assert( endptr );
  rtems_test_assert( value == (TEST_STRING_TO_TYPE)0 );

  /* Conversion of number that is too large */
  #if defined(TEST_TOO_LARGE_STRING)
    endptr = NULL;
    value = 0;
    puts(
    STRING_TO_NAME_METHOD_STRING " - overflow - RTEMS_INVALID_NUMBER" );
    #if defined(STRING_TO_INTEGER)
      status = STRING_TO_NAME_METHOD(
                 TEST_TOO_LARGE_STRING, &value, &endptr, 10 );
    #elif defined(STRING_TO_POINTER) || defined(STRING_TO_FLOAT)
      status = STRING_TO_NAME_METHOD( TEST_TOO_LARGE_STRING, &value, &endptr );
    #endif
    if ( status != RTEMS_INVALID_NUMBER )
      printf( "ERROR = %s\n", rtems_status_text(status) );
    rtems_test_assert( status == RTEMS_INVALID_NUMBER );
    rtems_test_assert( endptr );
  #endif


  /* Conversion of number that is too large for unsigned char */
  #if defined(TEST_TOO_LARGE_FOR_UCHAR)
    endptr = NULL;
    value = 0;
    puts(
    STRING_TO_NAME_METHOD_STRING " - overflow - RTEMS_INVALID_NUMBER" );
    #if defined(STRING_TO_INTEGER)
      status = STRING_TO_NAME_METHOD(
                 TEST_TOO_LARGE_FOR_UCHAR, &value, &endptr, 10 );
    #endif
    if ( status != RTEMS_INVALID_NUMBER )
      printf( "ERROR = %s\n", rtems_status_text(status) );
    rtems_test_assert( status == RTEMS_INVALID_NUMBER );
    rtems_test_assert( endptr );
  #endif

  /* Conversion of number that is too small */
  #if defined(TEST_TOO_SMALL_STRING)
    endptr = NULL;
    value = 0;
    puts( STRING_TO_NAME_METHOD_STRING "- RTEMS_INVALID_NUMBER" );
    #if defined(STRING_TO_INTEGER)
      status = STRING_TO_NAME_METHOD(
                 TEST_TOO_SMALL_STRING, &value, &endptr, 10 );
    #elif defined(STRING_TO_POINTER) || defined(STRING_TO_FLOAT)
      status = STRING_TO_NAME_METHOD( TEST_TOO_SMALL_STRING, &value, &endptr );
    #endif
    rtems_test_assert( status == RTEMS_INVALID_NUMBER );
    rtems_test_assert( endptr );
  #endif
}

/* Now undefined everything that instantiates this */
#undef TEST_STRING_TO_TYPE
#undef TEST_STRING_TO_NAME
#undef STRING_TO_NAME_METHOD
#undef STRING_TO_NAME_METHOD_STRING
#undef STRING_TO_INTEGER
#undef STRING_TO_POINTER
#undef STRING_TO_FLOAT
#undef STRING_TO_MAX
#undef STRING_TO_MAX_STRING
#undef GOOD_VALUE
#undef GOOD_VALUE_STRING
#undef BAD_VALUE_STRING
#undef TEST_TOO_LARGE_STRING
#undef TEST_TOO_SMALL_STRING
#undef TEST_TOO_LARGE_FOR_UCHAR