| Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 1 | |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "config.h" |
| 6 | |
| 7 | #include "json_inttypes.h" |
| 8 | #include "json_util.h" |
| 9 | |
| 10 | void checkit(const char *buf) |
| 11 | { |
| 12 | int64_t cint64 = -666; |
| 13 | |
| 14 | int retval = json_parse_int64(buf, &cint64); |
| 15 | printf("buf=%s parseit=%d, value=%" PRId64 " \n", buf, retval, cint64); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * This test calls json_parse_int64 with a variety of different strings. |
| 20 | * It's purpose is to ensure that the results are consistent across all |
| 21 | * different environments that it might be executed in. |
| 22 | * |
| 23 | * This always exits with a 0 exit value. The output should be compared |
| 24 | * against previously saved expected output. |
| 25 | */ |
| 26 | int main() |
| 27 | { |
| 28 | char buf[100]; |
| 29 | |
| 30 | checkit("x"); |
| 31 | |
| ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 32 | checkit("0"); |
| 33 | checkit("-0"); |
| 34 | |
| 35 | checkit("00000000"); |
| 36 | checkit("-00000000"); |
| 37 | |
| Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 38 | checkit("1"); |
| 39 | |
| 40 | strcpy(buf, "2147483647"); // aka INT32_MAX |
| 41 | checkit(buf); |
| 42 | |
| 43 | strcpy(buf, "-1"); |
| 44 | checkit(buf); |
| 45 | |
| 46 | strcpy(buf, " -1"); |
| 47 | checkit(buf); |
| 48 | |
| 49 | strcpy(buf, "00001234"); |
| 50 | checkit(buf); |
| 51 | |
| 52 | strcpy(buf, "0001234x"); |
| 53 | checkit(buf); |
| 54 | |
| 55 | strcpy(buf, "-00001234"); |
| 56 | checkit(buf); |
| 57 | |
| 58 | strcpy(buf, "-00001234x"); |
| 59 | checkit(buf); |
| 60 | |
| 61 | strcpy(buf, "4294967295"); // aka UINT32_MAX |
| 62 | |
| 63 | sprintf(buf, "4294967296"); // aka UINT32_MAX + 1 |
| 64 | |
| 65 | strcpy(buf, "21474836470"); // INT32_MAX * 10 |
| 66 | checkit(buf); |
| 67 | |
| 68 | strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch |
| 69 | checkit(buf); |
| 70 | |
| 71 | strcpy(buf, "-2147483647"); // INT32_MIN + 1 |
| 72 | checkit(buf); |
| 73 | |
| 74 | strcpy(buf, "-2147483648"); // INT32_MIN |
| 75 | checkit(buf); |
| 76 | |
| 77 | strcpy(buf, "-2147483649"); // INT32_MIN - 1 |
| 78 | checkit(buf); |
| 79 | |
| 80 | strcpy(buf, "-21474836480"); // INT32_MIN * 10 |
| 81 | checkit(buf); |
| 82 | |
| Eric Haszlakiewicz | 77c6239 | 2012-07-29 12:13:54 -0500 | [diff] [blame] | 83 | strcpy(buf, "9223372036854775806"); // INT64_MAX - 1 |
| 84 | checkit(buf); |
| 85 | |
| Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 86 | strcpy(buf, "9223372036854775807"); // INT64_MAX |
| 87 | checkit(buf); |
| 88 | |
| 89 | strcpy(buf, "9223372036854775808"); // INT64_MAX + 1 |
| 90 | checkit(buf); |
| 91 | |
| 92 | strcpy(buf, "-9223372036854775808"); // INT64_MIN |
| 93 | checkit(buf); |
| 94 | |
| 95 | strcpy(buf, "-9223372036854775809"); // INT64_MIN - 1 |
| 96 | checkit(buf); |
| 97 | |
| Eric Haszlakiewicz | 77c6239 | 2012-07-29 12:13:54 -0500 | [diff] [blame] | 98 | strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1 |
| 99 | checkit(buf); |
| 100 | |
| Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 101 | strcpy(buf, "18446744073709551615"); // UINT64_MAX |
| 102 | checkit(buf); |
| 103 | |
| 104 | strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1 |
| 105 | checkit(buf); |
| 106 | |
| 107 | strcpy(buf, "-18446744073709551616"); // -UINT64_MAX |
| 108 | checkit(buf); |
| 109 | |
| Eric Haszlakiewicz | 77c6239 | 2012-07-29 12:13:54 -0500 | [diff] [blame] | 110 | // Ensure we can still parse valid numbers after parsing out of range ones. |
| 111 | strcpy(buf, "123"); |
| 112 | checkit(buf); |
| 113 | |
| Michael Clark | c4dceae | 2010-10-06 16:39:20 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |