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 | |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 7 | #include "pub_tool_vki.h" |
| 8 | #include "m_libcbase.c" |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 9 | |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 10 | #define CHECK(x) \ |
| 11 | if (!x) { fprintf(stderr, "failure: %s:%d\n", __FILE__, __LINE__); } |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 12 | |
bart | 24d9863 | 2009-02-26 13:21:50 +0000 | [diff] [blame^] | 13 | |
| 14 | #if ! defined(VKI_PAGE_SIZE) |
| 15 | unsigned long VKI_PAGE_SIZE = VKI_MAX_PAGE_SIZE; |
| 16 | #endif |
| 17 | |
| 18 | |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 19 | void test_VG_STREQ(void) |
| 20 | { |
| 21 | CHECK( ! VG_STREQ(NULL, NULL) ); // Nb: strcmp() considers these equal |
| 22 | CHECK( ! VG_STREQ(NULL, "ab") ); // Nb: strcmp() seg faults on this |
| 23 | CHECK( ! VG_STREQ("ab", NULL) ); // Nb: strcmp() seg faults on this |
| 24 | CHECK( ! VG_STREQ("", "a") ); |
| 25 | CHECK( ! VG_STREQ("a", "") ); |
| 26 | CHECK( ! VG_STREQ("abc", "abcd")); |
| 27 | CHECK( ! VG_STREQ("abcd", "abc") ); |
| 28 | CHECK( ! VG_STREQ("Abcd", "abcd")); |
| 29 | CHECK( ! VG_STREQ("abcd", "Abcd")); |
| 30 | |
| 31 | CHECK( VG_STREQ("", "") ); |
| 32 | CHECK( VG_STREQ("a", "a") ); |
| 33 | CHECK( VG_STREQ("abcd", "abcd") ); |
| 34 | } |
| 35 | |
| 36 | void test_VG_STREQN(void) |
| 37 | { |
| 38 | CHECK( ! VG_STREQN(0, NULL, NULL) ); |
| 39 | CHECK( ! VG_STREQN(5, NULL, NULL) ); |
| 40 | CHECK( ! VG_STREQN(0, NULL, "ab") ); |
| 41 | CHECK( ! VG_STREQN(5, NULL, "ab") ); |
| 42 | CHECK( ! VG_STREQN(0, "ab", NULL) ); |
| 43 | CHECK( ! VG_STREQN(1, "", "a") ); |
| 44 | CHECK( ! VG_STREQN(1, "a", "") ); |
| 45 | CHECK( ! VG_STREQN(4, "abc", "abcd")); |
| 46 | CHECK( ! VG_STREQN(4, "abcd", "abc") ); |
| 47 | CHECK( ! VG_STREQN(1, "Abcd", "abcd")); |
| 48 | CHECK( ! VG_STREQN(4, "Abcd", "abcd")); |
| 49 | CHECK( ! VG_STREQN(4, "abcd", "abce")); |
| 50 | CHECK( ! VG_STREQN(9, "abcd", "abce")); |
| 51 | |
| 52 | CHECK( VG_STREQN(0, "", "") ); |
| 53 | CHECK( VG_STREQN(1, "", "") ); |
| 54 | CHECK( VG_STREQN(0, "a", "a") ); |
| 55 | CHECK( VG_STREQN(1, "a", "a") ); |
| 56 | CHECK( VG_STREQN(2, "a", "a") ); |
| 57 | CHECK( VG_STREQN(9, "a", "a") ); |
| 58 | CHECK( VG_STREQN(1, "ab", "ac")); |
| 59 | CHECK( VG_STREQN(3, "abcd", "abce")); |
| 60 | } |
| 61 | |
njn | 29a100c | 2009-02-26 04:02:03 +0000 | [diff] [blame] | 62 | void test_VG_IS_XYZ_ALIGNED(void) |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 63 | { |
| 64 | CHECK( VG_IS_2_ALIGNED(0x0) ); |
| 65 | CHECK( ! VG_IS_2_ALIGNED(0x1) ); |
| 66 | CHECK( VG_IS_2_ALIGNED(0x2) ); |
| 67 | CHECK( ! VG_IS_2_ALIGNED(0x3) ); |
| 68 | CHECK( VG_IS_2_ALIGNED(0x4) ); |
| 69 | CHECK( ! VG_IS_2_ALIGNED(0x5) ); |
| 70 | CHECK( VG_IS_2_ALIGNED(0x6) ); |
| 71 | CHECK( ! VG_IS_2_ALIGNED(0x7) ); |
| 72 | CHECK( VG_IS_2_ALIGNED(0x8) ); |
| 73 | CHECK( ! VG_IS_2_ALIGNED(0x9) ); |
| 74 | CHECK( VG_IS_2_ALIGNED(0xa) ); |
| 75 | CHECK( ! VG_IS_2_ALIGNED(0xb) ); |
| 76 | CHECK( VG_IS_2_ALIGNED(0xc) ); |
| 77 | CHECK( ! VG_IS_2_ALIGNED(0xd) ); |
| 78 | CHECK( VG_IS_2_ALIGNED(0xe) ); |
| 79 | CHECK( ! VG_IS_2_ALIGNED(0xf) ); |
| 80 | |
| 81 | CHECK( VG_IS_4_ALIGNED(0x0) ); |
| 82 | CHECK( ! VG_IS_4_ALIGNED(0x1) ); |
| 83 | CHECK( ! VG_IS_4_ALIGNED(0x2) ); |
| 84 | CHECK( ! VG_IS_4_ALIGNED(0x3) ); |
| 85 | CHECK( VG_IS_4_ALIGNED(0x4) ); |
| 86 | CHECK( ! VG_IS_4_ALIGNED(0x5) ); |
| 87 | CHECK( ! VG_IS_4_ALIGNED(0x6) ); |
| 88 | CHECK( ! VG_IS_4_ALIGNED(0x7) ); |
| 89 | CHECK( VG_IS_4_ALIGNED(0x8) ); |
| 90 | CHECK( ! VG_IS_4_ALIGNED(0x9) ); |
| 91 | CHECK( ! VG_IS_4_ALIGNED(0xa) ); |
| 92 | CHECK( ! VG_IS_4_ALIGNED(0xb) ); |
| 93 | CHECK( VG_IS_4_ALIGNED(0xc) ); |
| 94 | CHECK( ! VG_IS_4_ALIGNED(0xd) ); |
| 95 | CHECK( ! VG_IS_4_ALIGNED(0xe) ); |
| 96 | CHECK( ! VG_IS_4_ALIGNED(0xf) ); |
| 97 | |
| 98 | CHECK( VG_IS_8_ALIGNED(0x0) ); |
| 99 | CHECK( ! VG_IS_8_ALIGNED(0x1) ); |
| 100 | CHECK( ! VG_IS_8_ALIGNED(0x2) ); |
| 101 | CHECK( ! VG_IS_8_ALIGNED(0x3) ); |
| 102 | CHECK( ! VG_IS_8_ALIGNED(0x4) ); |
| 103 | CHECK( ! VG_IS_8_ALIGNED(0x5) ); |
| 104 | CHECK( ! VG_IS_8_ALIGNED(0x6) ); |
| 105 | CHECK( ! VG_IS_8_ALIGNED(0x7) ); |
| 106 | CHECK( VG_IS_8_ALIGNED(0x8) ); |
| 107 | CHECK( ! VG_IS_8_ALIGNED(0x9) ); |
| 108 | CHECK( ! VG_IS_8_ALIGNED(0xa) ); |
| 109 | CHECK( ! VG_IS_8_ALIGNED(0xb) ); |
| 110 | CHECK( ! VG_IS_8_ALIGNED(0xc) ); |
| 111 | CHECK( ! VG_IS_8_ALIGNED(0xd) ); |
| 112 | CHECK( ! VG_IS_8_ALIGNED(0xe) ); |
| 113 | CHECK( ! VG_IS_8_ALIGNED(0xf) ); |
| 114 | |
| 115 | CHECK( VG_IS_16_ALIGNED(0x0) ); |
| 116 | CHECK( ! VG_IS_16_ALIGNED(0x1) ); |
| 117 | CHECK( ! VG_IS_16_ALIGNED(0x2) ); |
| 118 | CHECK( ! VG_IS_16_ALIGNED(0x3) ); |
| 119 | CHECK( ! VG_IS_16_ALIGNED(0x4) ); |
| 120 | CHECK( ! VG_IS_16_ALIGNED(0x5) ); |
| 121 | CHECK( ! VG_IS_16_ALIGNED(0x6) ); |
| 122 | CHECK( ! VG_IS_16_ALIGNED(0x7) ); |
| 123 | CHECK( ! VG_IS_16_ALIGNED(0x8) ); |
| 124 | CHECK( ! VG_IS_16_ALIGNED(0x9) ); |
| 125 | CHECK( ! VG_IS_16_ALIGNED(0xa) ); |
| 126 | CHECK( ! VG_IS_16_ALIGNED(0xb) ); |
| 127 | CHECK( ! VG_IS_16_ALIGNED(0xc) ); |
| 128 | CHECK( ! VG_IS_16_ALIGNED(0xd) ); |
| 129 | CHECK( ! VG_IS_16_ALIGNED(0xe) ); |
| 130 | CHECK( ! VG_IS_16_ALIGNED(0xf) ); |
| 131 | |
| 132 | CHECK( VG_IS_WORD_ALIGNED(0x0) ); |
| 133 | CHECK( ! VG_IS_WORD_ALIGNED(0x1) ); |
| 134 | CHECK( ! VG_IS_WORD_ALIGNED(0x2) ); |
| 135 | CHECK( ! VG_IS_WORD_ALIGNED(0x3) ); |
| 136 | // 0x4 case below |
| 137 | CHECK( ! VG_IS_WORD_ALIGNED(0x5) ); |
| 138 | CHECK( ! VG_IS_WORD_ALIGNED(0x6) ); |
| 139 | CHECK( ! VG_IS_WORD_ALIGNED(0x7) ); |
| 140 | CHECK( VG_IS_WORD_ALIGNED(0x8) ); |
| 141 | CHECK( ! VG_IS_WORD_ALIGNED(0x9) ); |
| 142 | CHECK( ! VG_IS_WORD_ALIGNED(0xa) ); |
| 143 | CHECK( ! VG_IS_WORD_ALIGNED(0xb) ); |
| 144 | // 0xc case below |
| 145 | CHECK( ! VG_IS_WORD_ALIGNED(0xd) ); |
| 146 | CHECK( ! VG_IS_WORD_ALIGNED(0xe) ); |
| 147 | CHECK( ! VG_IS_WORD_ALIGNED(0xf) ); |
| 148 | if (4 == sizeof(void*)) { |
| 149 | CHECK( VG_IS_WORD_ALIGNED(0x4) ); |
| 150 | CHECK( VG_IS_WORD_ALIGNED(0xc) ); |
| 151 | } else if (8 == sizeof(void*)) { |
| 152 | CHECK( ! VG_IS_WORD_ALIGNED(0x4) ); |
| 153 | CHECK( ! VG_IS_WORD_ALIGNED(0xc) ); |
| 154 | } else { |
| 155 | assert(0); |
| 156 | } |
| 157 | |
| 158 | CHECK( VG_IS_PAGE_ALIGNED(0x0) ); |
| 159 | CHECK( ! VG_IS_PAGE_ALIGNED(0x1) ); |
| 160 | CHECK( ! VG_IS_PAGE_ALIGNED(0x2) ); |
| 161 | CHECK( ! VG_IS_PAGE_ALIGNED(0x3) ); |
| 162 | CHECK( ! VG_IS_PAGE_ALIGNED(0x4) ); |
| 163 | CHECK( ! VG_IS_PAGE_ALIGNED(VKI_PAGE_SIZE-1) ); |
| 164 | CHECK( VG_IS_PAGE_ALIGNED(VKI_PAGE_SIZE ) ); |
| 165 | CHECK( ! VG_IS_PAGE_ALIGNED(VKI_PAGE_SIZE+1) ); |
| 166 | } |
| 167 | |
| 168 | void test_VG_ROUND_et_al() |
| 169 | { |
| 170 | CHECK( 0 == VG_ROUNDDN(0, 1) ); |
| 171 | CHECK( 1 == VG_ROUNDDN(1, 1) ); |
| 172 | CHECK( 2 == VG_ROUNDDN(2, 1) ); |
| 173 | CHECK( 3 == VG_ROUNDDN(3, 1) ); |
| 174 | CHECK( 4 == VG_ROUNDDN(4, 1) ); |
| 175 | CHECK( 5 == VG_ROUNDDN(5, 1) ); |
| 176 | CHECK( 6 == VG_ROUNDDN(6, 1) ); |
| 177 | CHECK( 7 == VG_ROUNDDN(7, 1) ); |
| 178 | |
| 179 | CHECK( 0 == VG_ROUNDUP(0, 1) ); |
| 180 | CHECK( 1 == VG_ROUNDUP(1, 1) ); |
| 181 | CHECK( 2 == VG_ROUNDUP(2, 1) ); |
| 182 | CHECK( 3 == VG_ROUNDUP(3, 1) ); |
| 183 | CHECK( 4 == VG_ROUNDUP(4, 1) ); |
| 184 | CHECK( 5 == VG_ROUNDUP(5, 1) ); |
| 185 | CHECK( 6 == VG_ROUNDUP(6, 1) ); |
| 186 | CHECK( 7 == VG_ROUNDUP(7, 1) ); |
| 187 | |
| 188 | CHECK( 0 == VG_ROUNDDN(0, 2) ); |
| 189 | CHECK( 0 == VG_ROUNDDN(1, 2) ); |
| 190 | CHECK( 2 == VG_ROUNDDN(2, 2) ); |
| 191 | CHECK( 2 == VG_ROUNDDN(3, 2) ); |
| 192 | CHECK( 4 == VG_ROUNDDN(4, 2) ); |
| 193 | CHECK( 4 == VG_ROUNDDN(5, 2) ); |
| 194 | CHECK( 6 == VG_ROUNDDN(6, 2) ); |
| 195 | CHECK( 6 == VG_ROUNDDN(7, 2) ); |
| 196 | |
| 197 | CHECK( 0 == VG_ROUNDUP(0, 2) ); |
| 198 | CHECK( 2 == VG_ROUNDUP(1, 2) ); |
| 199 | CHECK( 2 == VG_ROUNDUP(2, 2) ); |
| 200 | CHECK( 4 == VG_ROUNDUP(3, 2) ); |
| 201 | CHECK( 4 == VG_ROUNDUP(4, 2) ); |
| 202 | CHECK( 6 == VG_ROUNDUP(5, 2) ); |
| 203 | CHECK( 6 == VG_ROUNDUP(6, 2) ); |
| 204 | CHECK( 8 == VG_ROUNDUP(7, 2) ); |
| 205 | |
| 206 | CHECK( 0 == VG_ROUNDDN(0, 4) ); |
| 207 | CHECK( 0 == VG_ROUNDDN(1, 4) ); |
| 208 | CHECK( 0 == VG_ROUNDDN(2, 4) ); |
| 209 | CHECK( 0 == VG_ROUNDDN(3, 4) ); |
| 210 | CHECK( 4 == VG_ROUNDDN(4, 4) ); |
| 211 | CHECK( 4 == VG_ROUNDDN(5, 4) ); |
| 212 | CHECK( 4 == VG_ROUNDDN(6, 4) ); |
| 213 | CHECK( 4 == VG_ROUNDDN(7, 4) ); |
| 214 | |
| 215 | CHECK( 0 == VG_ROUNDUP(0, 4) ); |
| 216 | CHECK( 4 == VG_ROUNDUP(1, 4) ); |
| 217 | CHECK( 4 == VG_ROUNDUP(2, 4) ); |
| 218 | CHECK( 4 == VG_ROUNDUP(3, 4) ); |
| 219 | CHECK( 4 == VG_ROUNDUP(4, 4) ); |
| 220 | CHECK( 8 == VG_ROUNDUP(5, 4) ); |
| 221 | CHECK( 8 == VG_ROUNDUP(6, 4) ); |
| 222 | CHECK( 8 == VG_ROUNDUP(7, 4) ); |
| 223 | |
| 224 | CHECK( 0 == VG_ROUNDDN(0, 8) ); |
| 225 | CHECK( 0 == VG_ROUNDDN(1, 8) ); |
| 226 | CHECK( 0 == VG_ROUNDDN(2, 8) ); |
| 227 | CHECK( 0 == VG_ROUNDDN(3, 8) ); |
| 228 | CHECK( 0 == VG_ROUNDDN(4, 8) ); |
| 229 | CHECK( 0 == VG_ROUNDDN(5, 8) ); |
| 230 | CHECK( 0 == VG_ROUNDDN(6, 8) ); |
| 231 | CHECK( 0 == VG_ROUNDDN(7, 8) ); |
| 232 | |
| 233 | CHECK( 0 == VG_ROUNDUP(0, 8) ); |
| 234 | CHECK( 8 == VG_ROUNDUP(1, 8) ); |
| 235 | CHECK( 8 == VG_ROUNDUP(2, 8) ); |
| 236 | CHECK( 8 == VG_ROUNDUP(3, 8) ); |
| 237 | CHECK( 8 == VG_ROUNDUP(4, 8) ); |
| 238 | CHECK( 8 == VG_ROUNDUP(5, 8) ); |
| 239 | CHECK( 8 == VG_ROUNDUP(6, 8) ); |
| 240 | CHECK( 8 == VG_ROUNDUP(7, 8) ); |
| 241 | |
| 242 | CHECK( 0 == VG_PGROUNDDN(0) ); |
| 243 | CHECK( 0 == VG_PGROUNDDN(1) ); |
| 244 | CHECK( 0 == VG_PGROUNDDN(2) ); |
| 245 | CHECK( 0 == VG_PGROUNDDN(3) ); |
| 246 | CHECK( 0 == VG_PGROUNDDN(4) ); |
| 247 | CHECK( 0 == VG_PGROUNDDN(VKI_PAGE_SIZE-1) ); |
| 248 | CHECK( VKI_PAGE_SIZE == VG_PGROUNDDN(VKI_PAGE_SIZE ) ); |
| 249 | CHECK( VKI_PAGE_SIZE == VG_PGROUNDDN(VKI_PAGE_SIZE+1) ); |
| 250 | |
| 251 | CHECK( 0 == VG_PGROUNDUP(0) ); |
| 252 | CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(1) ); |
| 253 | CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(2) ); |
| 254 | CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(3) ); |
| 255 | CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(4) ); |
| 256 | CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(VKI_PAGE_SIZE-1) ); |
| 257 | CHECK( VKI_PAGE_SIZE == VG_PGROUNDUP(VKI_PAGE_SIZE ) ); |
| 258 | CHECK( VKI_PAGE_SIZE*2 == VG_PGROUNDUP(VKI_PAGE_SIZE+1) ); |
| 259 | } |
| 260 | |
| 261 | void test_isspace(void) |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 262 | { |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 263 | CHECK( VG_(isspace)(' ') ); |
| 264 | CHECK( VG_(isspace)('\n') ); |
| 265 | CHECK( VG_(isspace)('\t') ); |
| 266 | CHECK( ! VG_(isspace)('3') ); |
| 267 | CHECK( ! VG_(isspace)('x') ); |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 268 | } |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 269 | |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 270 | void test_isdigit(void) |
| 271 | { |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 272 | CHECK( VG_(isdigit)('0') ); |
| 273 | CHECK( VG_(isdigit)('1') ); |
| 274 | CHECK( VG_(isdigit)('5') ); |
| 275 | CHECK( VG_(isdigit)('9') ); |
| 276 | CHECK( ! VG_(isdigit)('a') ); |
| 277 | CHECK( ! VG_(isdigit)('!') ); |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 278 | } |
| 279 | |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 280 | void test_is_dec_digit() |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 281 | { |
| 282 | Long x; |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 283 | CHECK( is_dec_digit('0', &x) && 0 == x ); |
| 284 | CHECK( is_dec_digit('1', &x) && 1 == x ); |
| 285 | CHECK( is_dec_digit('9', &x) && 9 == x ); |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 286 | } |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 287 | |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 288 | void test_is_hex_digit() |
| 289 | { |
| 290 | Long x; |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 291 | CHECK( is_hex_digit('0', &x) && 0 == x ); |
| 292 | CHECK( is_hex_digit('1', &x) && 1 == x ); |
| 293 | CHECK( is_hex_digit('9', &x) && 9 == x ); |
| 294 | CHECK( is_hex_digit('a', &x) && 10 == x ); |
| 295 | CHECK( is_hex_digit('f', &x) && 15 == x ); |
| 296 | CHECK( is_hex_digit('A', &x) && 10 == x ); |
| 297 | CHECK( is_hex_digit('F', &x) && 15 == x ); |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 298 | } |
| 299 | |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 300 | void test_strtoll_and_strtod(void) |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 301 | { |
| 302 | // For VG_(strtoll*)() |
| 303 | typedef struct { |
| 304 | Char* str; // The string to convert. |
| 305 | Long res; // The result. |
| 306 | Char endptr_val; // The char one past the end of the converted text. |
| 307 | } StrtollInputs; |
| 308 | |
| 309 | // VG_(strtoll10)() |
| 310 | { |
| 311 | StrtollInputs a[] = { |
| 312 | // If there's no number at the head of the string, return 0, and |
| 313 | // make 'endptr' point to the start of the string. |
| 314 | { str : "", res : 0, endptr_val : '\0' }, |
| 315 | { str : " \n\t", res : 0, endptr_val : ' ' }, |
| 316 | { str : "one", res : 0, endptr_val : 'o' }, |
| 317 | { str : "\ntwo", res : 0, endptr_val : '\n' }, |
| 318 | |
| 319 | // Successful conversion. Leading whitespace is ignored. A single |
| 320 | // '-' or '+' is accepted. |
| 321 | { str : "0", res : 0, endptr_val : '\0' }, |
| 322 | { str : "+0", res : 0, endptr_val : '\0' }, |
| 323 | { str : "-0", res : 0, endptr_val : '\0' }, |
| 324 | { str : "1", res : 1, endptr_val : '\0' }, |
| 325 | { str : "+1", res : 1, endptr_val : '\0' }, |
| 326 | { str : "-1", res : -1, endptr_val : '\0' }, |
| 327 | { str : "12", res : 12, endptr_val : '\0' }, |
| 328 | { str : "-567", res : -567, endptr_val : '\0' }, |
| 329 | { str : "1234567", res : 1234567, endptr_val : '\0' }, |
| 330 | { str : "007", res : 7, endptr_val : '\0' }, |
| 331 | { str : " +42", res : 42, endptr_val : '\0' }, |
| 332 | { str : "\n\t\r\v -56", res : -56, endptr_val : '\0' }, |
| 333 | { str : "123xyz", res : 123, endptr_val : 'x' }, |
| 334 | { str : " -123abc", res : -123, endptr_val : 'a' }, |
| 335 | |
| 336 | // Whitespace after the +/- is not allowed; conversion fails. |
| 337 | { str : "+ 1", res : 0, endptr_val : '+' }, |
| 338 | { str : "-\n1", res : 0, endptr_val : '-' }, |
| 339 | }; |
| 340 | |
| 341 | // Nb: We test the results against strtoll() as well. |
| 342 | int i; |
| 343 | for (i = 0; i < (sizeof(a) / sizeof(StrtollInputs)); i++) { |
| 344 | Char* endptr1; |
| 345 | char* endptr2; |
| 346 | Long res1 = VG_(strtoll10)(a[i].str, &endptr1); |
| 347 | long long res2 = strtoll (a[i].str, &endptr2, 10); |
| 348 | //printf("res1 = %lld, *endptr1 = '%c'\n", res1, *endptr1); |
| 349 | //printf("res2 = %lld, *endptr2 = '%c'\n", res2, *endptr2); |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 350 | CHECK(a[i].res == res1 && a[i].endptr_val == *endptr1); |
| 351 | CHECK(res2 == res1 && *endptr2 == *endptr1); |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | // VG_(strtoll16)() |
| 356 | { |
| 357 | StrtollInputs a[] = { |
| 358 | // If there's no number at the head of the string, return 0, and |
| 359 | // make 'endptr' point to the start of the string. |
| 360 | { str : "", res : 0, endptr_val : '\0' }, |
| 361 | { str : " \n\t", res : 0, endptr_val : ' ' }, |
| 362 | { str : "one", res : 0, endptr_val : 'o' }, |
| 363 | { str : "\ntwo", res : 0, endptr_val : '\n' }, |
| 364 | |
| 365 | // Successful conversion. Leading whitespace is ignored. A single |
| 366 | // '-' or '+' is accepted. "0X" and "0x" are also allowed at the |
| 367 | // front, but if no digits follow, just the "0" is converted. |
| 368 | { str : "0", res : 0, endptr_val : '\0' }, |
| 369 | { str : "0x0", res : 0, endptr_val : '\0' }, |
| 370 | { str : "0X0", res : 0, endptr_val : '\0' }, |
| 371 | { str : "0x", res : 0, endptr_val : 'x' }, |
| 372 | { str : "0Xg", res : 0, endptr_val : 'X' }, |
| 373 | { str : "0", res : 0, endptr_val : '\0' }, |
| 374 | { str : "+0", res : 0, endptr_val : '\0' }, |
| 375 | { str : "-0", res : 0, endptr_val : '\0' }, |
| 376 | { str : "1", res : 1, endptr_val : '\0' }, |
| 377 | { str : "+1", res : 1, endptr_val : '\0' }, |
| 378 | { str : "-1", res : -1, endptr_val : '\0' }, |
| 379 | { str : "1a", res : 26, endptr_val : '\0' }, |
| 380 | { str : "-5F7", res : -1527, endptr_val : '\0' }, |
| 381 | { str : "0x1234567", res : 19088743, endptr_val : '\0' }, |
| 382 | { str : "007", res : 7, endptr_val : '\0' }, |
| 383 | { str : "0X00ABCD", res : 43981, endptr_val : '\0' }, |
| 384 | { str : " +AbC", res : 2748, endptr_val : '\0' }, |
| 385 | { str : " -0xAbC", res : -2748, endptr_val : '\0' }, |
| 386 | { str : " -0xxx", res : 0, endptr_val : 'x' }, |
| 387 | { str : "\n\t\r\v -56", res : -86, endptr_val : '\0' }, |
| 388 | { str : "123xyz", res : 291, endptr_val : 'x' }, |
| 389 | { str : " -123defghi", res : -1195503, endptr_val : 'g' }, |
| 390 | |
| 391 | // Whitespace after the +/- is not allowed; conversion fails. |
| 392 | { str : "+ 1", res : 0, endptr_val : '+' }, |
| 393 | { str : "-\n0x1", res : 0, endptr_val : '-' }, |
| 394 | }; |
| 395 | |
| 396 | // Nb: We test the results against strtoll() as well. |
| 397 | int i; |
| 398 | for (i = 0; i < (sizeof(a) / sizeof(StrtollInputs)); i++) { |
| 399 | Char* endptr1; |
| 400 | char* endptr2; |
| 401 | Long res1 = VG_(strtoll16)(a[i].str, &endptr1); |
| 402 | long long res2 = strtoll (a[i].str, &endptr2, 16); |
| 403 | //printf(" res1 = %lld, *endptr1 = '%c'\n", res1, *endptr1); |
| 404 | //printf(" res2 = %lld, *endptr2 = '%c'\n", res2, *endptr2); |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 405 | CHECK(a[i].res == res1 && a[i].endptr_val == *endptr1); |
| 406 | CHECK(res2 == res1 && *endptr2 == *endptr1); |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 407 | } |
| 408 | } |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 409 | |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 410 | // VG_(strtod)() |
njn | 2a5a76b | 2009-02-23 04:16:56 +0000 | [diff] [blame] | 411 | // XXX: todo |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 412 | } |
| 413 | |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 414 | void test_log2(void) |
| 415 | { |
| 416 | CHECK( -1 == VG_(log2)(0) ); |
| 417 | CHECK( 0 == VG_(log2)(1) ); |
| 418 | CHECK( 1 == VG_(log2)(2) ); |
| 419 | CHECK( -1 == VG_(log2)(3) ); |
| 420 | CHECK( 2 == VG_(log2)(4) ); |
| 421 | CHECK( -1 == VG_(log2)(5) ); |
| 422 | CHECK( -1 == VG_(log2)(6) ); |
| 423 | CHECK( -1 == VG_(log2)(7) ); |
| 424 | CHECK( 3 == VG_(log2)(8) ); |
| 425 | |
| 426 | CHECK( -1 == VG_(log2)( 15) ); |
| 427 | CHECK( 4 == VG_(log2)( 16) ); |
| 428 | CHECK( -1 == VG_(log2)( 17) ); |
| 429 | |
| 430 | CHECK( -1 == VG_(log2)( 63) ); |
| 431 | CHECK( 6 == VG_(log2)( 64) ); |
| 432 | CHECK( -1 == VG_(log2)( 65) ); |
| 433 | |
| 434 | CHECK( -1 == VG_(log2)(255) ); |
| 435 | CHECK( 8 == VG_(log2)(256) ); |
| 436 | CHECK( -1 == VG_(log2)(257) ); |
| 437 | |
| 438 | CHECK( -1 == VG_(log2)(65535) ); |
| 439 | CHECK( 16 == VG_(log2)(65536) ); |
| 440 | CHECK( -1 == VG_(log2)(65537) ); |
| 441 | |
| 442 | CHECK( -1 == VG_(log2)(16777215) ); |
| 443 | CHECK( 24 == VG_(log2)(16777216) ); |
| 444 | CHECK( -1 == VG_(log2)(16777217) ); |
| 445 | |
| 446 | CHECK( -1 == VG_(log2)(2147483647U) ); |
| 447 | CHECK( 31 == VG_(log2)(2147483648U) ); |
| 448 | CHECK( -1 == VG_(log2)(2147483649U) ); |
| 449 | |
| 450 | CHECK( -1 == VG_(log2)(4294967295U) ); // Max UInt |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void test_random(void) |
| 454 | { |
| 455 | // Hmm, it's really hard to unit test a pseudo-random number generator. |
| 456 | // So no testing here, sorry. |
| 457 | } |
| 458 | |
| 459 | //----------------------------------------------------------------------- |
| 460 | // main |
| 461 | //----------------------------------------------------------------------- |
| 462 | |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 463 | int main(void) |
| 464 | { |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 465 | // Nb: the order of the tests is based on the order of the code in |
| 466 | // m_libcbase.c, except that macros defined in pub_tool_libcbase.h are |
| 467 | // tested first. |
| 468 | |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 469 | //-------------------------------------------------------------------- |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 470 | // pub_tool_libcbase.h macros |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 471 | //-------------------------------------------------------------------- |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 472 | test_VG_STREQ(); |
| 473 | test_VG_STREQN(); |
njn | 29a100c | 2009-02-26 04:02:03 +0000 | [diff] [blame] | 474 | test_VG_IS_XYZ_ALIGNED(); |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 475 | test_VG_ROUND_et_al(); |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 476 | |
| 477 | //-------------------------------------------------------------------- |
| 478 | // Char functions |
| 479 | //-------------------------------------------------------------------- |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 480 | test_isspace(); |
| 481 | test_isdigit(); |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 482 | |
| 483 | //-------------------------------------------------------------------- |
| 484 | // String-to-number functions |
| 485 | //-------------------------------------------------------------------- |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 486 | test_is_dec_digit(); |
| 487 | test_is_hex_digit(); |
| 488 | test_strtoll_and_strtod(); |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 489 | |
| 490 | //-------------------------------------------------------------------- |
| 491 | // String functions |
| 492 | //-------------------------------------------------------------------- |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 493 | // XXX: more todo: VG_(str_*) |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 494 | |
| 495 | //-------------------------------------------------------------------- |
| 496 | // Mem functions |
| 497 | //-------------------------------------------------------------------- |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 498 | // XXX: todo: VG_(mem*) |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 499 | |
| 500 | //-------------------------------------------------------------------- |
| 501 | // Miscellaneous functions |
| 502 | //-------------------------------------------------------------------- |
njn | 132fdb0 | 2009-02-26 03:52:35 +0000 | [diff] [blame] | 503 | // XXX: todo: VG_(ssort) |
| 504 | test_log2(); |
| 505 | test_random(); |
njn | 221666f | 2009-02-20 06:37:52 +0000 | [diff] [blame] | 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |