• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/json-c/tests/test_parse_int64.c

  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 void checkit_uint(const char *buf)
 19 {
 20         uint64_t cuint64 = 666;
 21 
 22         int retval = json_parse_uint64(buf, &cuint64);
 23         printf("buf=%s parseit=%d, value=%" PRIu64 " \n", buf, retval, cuint64);
 24 }
 25 
 26 /**
 27  * This test calls json_parse_int64 and json_parse_int64 with a variety
 28  * of different strings. It's purpose is to ensure that the results are
 29  * consistent across all different environments that it might be executed in.
 30  *
 31  * This always exits with a 0 exit value.  The output should be compared
 32  * against previously saved expected output.
 33  */
 34 int main()
 35 {
 36         char buf[100];
 37 
 38         printf("==========json_parse_int64() test===========\n");
 39         checkit("x");
 40 
 41         checkit("");
 42         checkit("-0");
 43 
 44         checkit("00000000");
 45         checkit("-00000000");
 46 
 47         checkit("1");
 48 
 49         strcpy(buf, "2147483647"); // aka INT32_MAX
 50         checkit(buf);
 51 
 52         strcpy(buf, "-1");
 53         checkit(buf);
 54 
 55         strcpy(buf, "   -1");
 56         checkit(buf);
 57 
 58         strcpy(buf, "00001234");
 59         checkit(buf);
 60 
 61         strcpy(buf, "0001234x");
 62         checkit(buf);
 63 
 64         strcpy(buf, "-00001234");
 65         checkit(buf);
 66 
 67         strcpy(buf, "-00001234x");
 68         checkit(buf);
 69 
 70         strcpy(buf, "4294967295"); // aka UINT32_MAX
 71         checkit(buf);
 72 
 73         strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
 74         checkit(buf);
 75 
 76         strcpy(buf, "21474836470"); // INT32_MAX * 10
 77         checkit(buf);
 78 
 79         strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
 80         checkit(buf);
 81 
 82         strcpy(buf, "-2147483647"); // INT32_MIN + 1
 83         checkit(buf);
 84 
 85         strcpy(buf, "-2147483648"); // INT32_MIN
 86         checkit(buf);
 87 
 88         strcpy(buf, "-2147483649"); // INT32_MIN - 1
 89         checkit(buf);
 90 
 91         strcpy(buf, "-21474836480"); // INT32_MIN * 10
 92         checkit(buf);
 93 
 94         strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
 95         checkit(buf);
 96 
 97         strcpy(buf, "9223372036854775807"); // INT64_MAX
 98         checkit(buf);
 99 
100         strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
101         checkit(buf);
102 
103         strcpy(buf, "-9223372036854775808"); // INT64_MIN
104         checkit(buf);
105 
106         strcpy(buf, "-9223372036854775809"); // INT64_MIN - 1
107         checkit(buf);
108 
109         strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
110         checkit(buf);
111 
112         strcpy(buf, "18446744073709551615"); // UINT64_MAX
113         checkit(buf);
114 
115         strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
116         checkit(buf);
117 
118         strcpy(buf, "-18446744073709551616"); // -UINT64_MAX
119         checkit(buf);
120 
121         // Ensure we can still parse valid numbers after parsing out of range ones.
122         strcpy(buf, "123");
123         checkit(buf);
124 
125         printf("\n==========json_parse_uint64() test===========\n");
126         checkit_uint("x");
127 
128         checkit_uint("");
129         checkit_uint("-0");
130 
131         checkit_uint("00000000");
132         checkit_uint("-00000000");
133 
134         checkit_uint("1");
135 
136         strcpy(buf, "2147483647"); // aka INT32_MAX
137         checkit_uint(buf);
138 
139         strcpy(buf, "-1");
140         checkit_uint(buf);
141 
142         strcpy(buf, "-9223372036854775808");
143         checkit_uint(buf);
144 
145         strcpy(buf, "   1");
146         checkit_uint(buf);
147 
148         strcpy(buf, "00001234");
149         checkit_uint(buf);
150 
151         strcpy(buf, "0001234x");
152         checkit_uint(buf);
153 
154         strcpy(buf, "4294967295"); // aka UINT32_MAX
155         checkit_uint(buf);
156 
157         strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
158         checkit_uint(buf);
159 
160         strcpy(buf, "21474836470"); // INT32_MAX * 10
161         checkit_uint(buf);
162 
163         strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
164         checkit_uint(buf);
165 
166         strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
167         checkit_uint(buf);
168 
169         strcpy(buf, "9223372036854775807"); // INT64_MAX
170         checkit_uint(buf);
171 
172         strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
173         checkit_uint(buf);
174 
175         strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
176         checkit_uint(buf);
177 
178         strcpy(buf, "18446744073709551615"); // UINT64_MAX
179         checkit_uint(buf);
180 
181         strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
182         checkit_uint(buf);
183 
184         // Ensure we can still parse valid numbers after parsing out of range ones.
185         strcpy(buf, "123");
186         checkit_uint(buf);
187 
188         return 0;
189 }
190 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt