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