njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 1 | // This module does unit testing of m_libcbase. |
| 2 | |
| 3 | #include <assert.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #include "coregrind/m_libcbase.c" |
| 8 | |
| 9 | |
| 10 | void test_isXYZ(void) |
| 11 | { |
| 12 | assert( VG_(isspace)(' ') ); |
| 13 | assert( VG_(isspace)('\n') ); |
| 14 | assert( VG_(isspace)('\t') ); |
| 15 | assert( ! VG_(isspace)('3') ); |
| 16 | assert( ! VG_(isspace)('x') ); |
| 17 | |
| 18 | assert( VG_(isdigit)('0') ); |
| 19 | assert( VG_(isdigit)('1') ); |
| 20 | assert( VG_(isdigit)('5') ); |
| 21 | assert( VG_(isdigit)('9') ); |
| 22 | assert( ! VG_(isdigit)('a') ); |
| 23 | assert( ! VG_(isdigit)('!') ); |
| 24 | } |
| 25 | |
| 26 | void test_is_XYZ_digit() |
| 27 | { |
| 28 | Long x; |
| 29 | |
| 30 | assert( is_dec_digit('0', &x) && 0 == x ); |
| 31 | assert( is_dec_digit('1', &x) && 1 == x ); |
| 32 | assert( is_dec_digit('9', &x) && 9 == x ); |
| 33 | |
| 34 | assert( is_hex_digit('0', &x) && 0 == x ); |
| 35 | assert( is_hex_digit('1', &x) && 1 == x ); |
| 36 | assert( is_hex_digit('9', &x) && 9 == x ); |
| 37 | assert( is_hex_digit('a', &x) && 10 == x ); |
| 38 | assert( is_hex_digit('f', &x) && 15 == x ); |
| 39 | assert( is_hex_digit('A', &x) && 10 == x ); |
| 40 | assert( is_hex_digit('F', &x) && 15 == x ); |
| 41 | } |
| 42 | |
| 43 | void test_strtoll(void) |
| 44 | { |
| 45 | // For VG_(strtoll*)() |
| 46 | typedef struct { |
| 47 | Char* str; // The string to convert. |
| 48 | Long res; // The result. |
| 49 | Char endptr_val; // The char one past the end of the converted text. |
| 50 | } StrtollInputs; |
| 51 | |
| 52 | // VG_(strtoll10)() |
| 53 | { |
| 54 | StrtollInputs a[] = { |
| 55 | // If there's no number at the head of the string, return 0, and |
| 56 | // make 'endptr' point to the start of the string. |
| 57 | { str : "", res : 0, endptr_val : '\0' }, |
| 58 | { str : " \n\t", res : 0, endptr_val : ' ' }, |
| 59 | { str : "one", res : 0, endptr_val : 'o' }, |
| 60 | { str : "\ntwo", res : 0, endptr_val : '\n' }, |
| 61 | |
| 62 | // Successful conversion. Leading whitespace is ignored. A single |
| 63 | // '-' or '+' is accepted. |
| 64 | { str : "0", res : 0, endptr_val : '\0' }, |
| 65 | { str : "+0", res : 0, endptr_val : '\0' }, |
| 66 | { str : "-0", res : 0, endptr_val : '\0' }, |
| 67 | { str : "1", res : 1, endptr_val : '\0' }, |
| 68 | { str : "+1", res : 1, endptr_val : '\0' }, |
| 69 | { str : "-1", res : -1, endptr_val : '\0' }, |
| 70 | { str : "12", res : 12, endptr_val : '\0' }, |
| 71 | { str : "-567", res : -567, endptr_val : '\0' }, |
| 72 | { str : "1234567", res : 1234567, endptr_val : '\0' }, |
| 73 | { str : "007", res : 7, endptr_val : '\0' }, |
| 74 | { str : " +42", res : 42, endptr_val : '\0' }, |
| 75 | { str : "\n\t\r\v -56", res : -56, endptr_val : '\0' }, |
| 76 | { str : "123xyz", res : 123, endptr_val : 'x' }, |
| 77 | { str : " -123abc", res : -123, endptr_val : 'a' }, |
| 78 | |
| 79 | // Whitespace after the +/- is not allowed; conversion fails. |
| 80 | { str : "+ 1", res : 0, endptr_val : '+' }, |
| 81 | { str : "-\n1", res : 0, endptr_val : '-' }, |
| 82 | }; |
| 83 | |
| 84 | // Nb: We test the results against strtoll() as well. |
| 85 | int i; |
| 86 | for (i = 0; i < (sizeof(a) / sizeof(StrtollInputs)); i++) { |
| 87 | Char* endptr1; |
| 88 | char* endptr2; |
| 89 | Long res1 = VG_(strtoll10)(a[i].str, &endptr1); |
| 90 | long long res2 = strtoll (a[i].str, &endptr2, 10); |
| 91 | //printf("res1 = %lld, *endptr1 = '%c'\n", res1, *endptr1); |
| 92 | //printf("res2 = %lld, *endptr2 = '%c'\n", res2, *endptr2); |
| 93 | assert(a[i].res == res1 && a[i].endptr_val == *endptr1); |
| 94 | assert(res2 == res1 && *endptr2 == *endptr1); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // VG_(strtoll16)() |
| 99 | { |
| 100 | StrtollInputs a[] = { |
| 101 | // If there's no number at the head of the string, return 0, and |
| 102 | // make 'endptr' point to the start of the string. |
| 103 | { str : "", res : 0, endptr_val : '\0' }, |
| 104 | { str : " \n\t", res : 0, endptr_val : ' ' }, |
| 105 | { str : "one", res : 0, endptr_val : 'o' }, |
| 106 | { str : "\ntwo", res : 0, endptr_val : '\n' }, |
| 107 | |
| 108 | // Successful conversion. Leading whitespace is ignored. A single |
| 109 | // '-' or '+' is accepted. "0X" and "0x" are also allowed at the |
| 110 | // front, but if no digits follow, just the "0" is converted. |
| 111 | { str : "0", res : 0, endptr_val : '\0' }, |
| 112 | { str : "0x0", res : 0, endptr_val : '\0' }, |
| 113 | { str : "0X0", res : 0, endptr_val : '\0' }, |
| 114 | { str : "0x", res : 0, endptr_val : 'x' }, |
| 115 | { str : "0Xg", res : 0, endptr_val : 'X' }, |
| 116 | { str : "0", res : 0, endptr_val : '\0' }, |
| 117 | { str : "+0", res : 0, endptr_val : '\0' }, |
| 118 | { str : "-0", res : 0, endptr_val : '\0' }, |
| 119 | { str : "1", res : 1, endptr_val : '\0' }, |
| 120 | { str : "+1", res : 1, endptr_val : '\0' }, |
| 121 | { str : "-1", res : -1, endptr_val : '\0' }, |
| 122 | { str : "1a", res : 26, endptr_val : '\0' }, |
| 123 | { str : "-5F7", res : -1527, endptr_val : '\0' }, |
| 124 | { str : "0x1234567", res : 19088743, endptr_val : '\0' }, |
| 125 | { str : "007", res : 7, endptr_val : '\0' }, |
| 126 | { str : "0X00ABCD", res : 43981, endptr_val : '\0' }, |
| 127 | { str : " +AbC", res : 2748, endptr_val : '\0' }, |
| 128 | { str : " -0xAbC", res : -2748, endptr_val : '\0' }, |
| 129 | { str : " -0xxx", res : 0, endptr_val : 'x' }, |
| 130 | { str : "\n\t\r\v -56", res : -86, endptr_val : '\0' }, |
| 131 | { str : "123xyz", res : 291, endptr_val : 'x' }, |
| 132 | { str : " -123defghi", res : -1195503, endptr_val : 'g' }, |
| 133 | |
| 134 | // Whitespace after the +/- is not allowed; conversion fails. |
| 135 | { str : "+ 1", res : 0, endptr_val : '+' }, |
| 136 | { str : "-\n0x1", res : 0, endptr_val : '-' }, |
| 137 | }; |
| 138 | |
| 139 | // Nb: We test the results against strtoll() as well. |
| 140 | int i; |
| 141 | for (i = 0; i < (sizeof(a) / sizeof(StrtollInputs)); i++) { |
| 142 | Char* endptr1; |
| 143 | char* endptr2; |
| 144 | Long res1 = VG_(strtoll16)(a[i].str, &endptr1); |
| 145 | long long res2 = strtoll (a[i].str, &endptr2, 16); |
| 146 | //printf(" res1 = %lld, *endptr1 = '%c'\n", res1, *endptr1); |
| 147 | //printf(" res2 = %lld, *endptr2 = '%c'\n", res2, *endptr2); |
| 148 | assert(a[i].res == res1 && a[i].endptr_val == *endptr1); |
| 149 | assert(res2 == res1 && *endptr2 == *endptr1); |
| 150 | } |
| 151 | } |
| 152 | // VG_(strtod)() |
| 153 | { |
| 154 | StrtollInputs a[] = { |
| 155 | // If there's no number at the head of the string, return 0, and |
| 156 | // make 'endptr' point to the start of the string. |
| 157 | { str : "", res : 0, endptr_val : '\0' }, |
| 158 | { str : " \n\t", res : 0, endptr_val : ' ' }, |
| 159 | { str : "one", res : 0, endptr_val : 'o' }, |
| 160 | { str : "\ntwo", res : 0, endptr_val : '\n' }, |
| 161 | |
| 162 | // Successful conversion. Leading whitespace is ignored. A single |
| 163 | // '-' or '+' is accepted. "0X" and "0x" are also allowed at the |
| 164 | // front, but if no digits follow, just the "0" is converted. |
| 165 | { str : "0", res : 0, endptr_val : '\0' }, |
| 166 | { str : "0", res : 0, endptr_val : '\0' }, |
| 167 | { str : "+0", res : 0, endptr_val : '\0' }, |
| 168 | { str : "-0", res : 0, endptr_val : '\0' }, |
| 169 | { str : "1", res : 1, endptr_val : '\0' }, |
| 170 | { str : "+1", res : 1, endptr_val : '\0' }, |
| 171 | { str : "-1", res : -1, endptr_val : '\0' }, |
| 172 | { str : "1a", res : 26, endptr_val : '\0' }, |
| 173 | { str : "-5F7", res : -1527, endptr_val : '\0' }, |
| 174 | { str : "0x1234567", res : 19088743, endptr_val : '\0' }, |
| 175 | { str : "007", res : 7, endptr_val : '\0' }, |
| 176 | { str : "0X00ABCD", res : 43981, endptr_val : '\0' }, |
| 177 | { str : " +AbC", res : 2748, endptr_val : '\0' }, |
| 178 | { str : " -0xAbC", res : -2748, endptr_val : '\0' }, |
| 179 | { str : " -0xxx", res : 0, endptr_val : 'x' }, |
| 180 | { str : "\n\t\r\v -56", res : -86, endptr_val : '\0' }, |
| 181 | { str : "123xyz", res : 291, endptr_val : 'x' }, |
| 182 | { str : " -123defghi", res : -1195503, endptr_val : 'g' }, |
| 183 | |
| 184 | // Whitespace after the +/- is not allowed; conversion fails. |
| 185 | { str : "+ 1", res : 0, endptr_val : '+' }, |
| 186 | { str : "-\n0x1", res : 0, endptr_val : '-' }, |
| 187 | }; |
| 188 | |
| 189 | // Nb: We test the results against strtoll() as well. |
| 190 | int i; |
| 191 | for (i = 0; i < (sizeof(a) / sizeof(StrtollInputs)); i++) { |
| 192 | Char* endptr1; |
| 193 | char* endptr2; |
| 194 | Long res1 = VG_(strtoll16)(a[i].str, &endptr1); |
| 195 | long long res2 = strtoll (a[i].str, &endptr2, 16); |
| 196 | //printf(" res1 = %lld, *endptr1 = '%c'\n", res1, *endptr1); |
| 197 | //printf(" res2 = %lld, *endptr2 = '%c'\n", res2, *endptr2); |
| 198 | assert(a[i].res == res1 && a[i].endptr_val == *endptr1); |
| 199 | assert(res2 == res1 && *endptr2 == *endptr1); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | int main(void) |
| 205 | { |
| 206 | //-------------------------------------------------------------------- |
| 207 | // Macros in pub_tool_libcbase.h |
| 208 | //-------------------------------------------------------------------- |
| 209 | // XXX: todo |
| 210 | |
| 211 | //-------------------------------------------------------------------- |
| 212 | // Char functions |
| 213 | //-------------------------------------------------------------------- |
| 214 | test_isXYZ(); |
| 215 | |
| 216 | //-------------------------------------------------------------------- |
| 217 | // String-to-number functions |
| 218 | //-------------------------------------------------------------------- |
| 219 | test_is_XYZ_digit(); |
| 220 | test_strtoll(); |
| 221 | |
| 222 | //-------------------------------------------------------------------- |
| 223 | // String functions |
| 224 | //-------------------------------------------------------------------- |
| 225 | // XXX: todo |
| 226 | |
| 227 | //-------------------------------------------------------------------- |
| 228 | // Mem functions |
| 229 | //-------------------------------------------------------------------- |
| 230 | // XXX: todo |
| 231 | |
| 232 | //-------------------------------------------------------------------- |
| 233 | // Miscellaneous functions |
| 234 | //-------------------------------------------------------------------- |
| 235 | // XXX: todo |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |