From: Eric S. Raymond Date: Wed, 17 Sep 2014 21:58:33 +0000 (-0400) Subject: Unit test now checks real and bool cases. X-Git-Tag: 1.0~23 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=2d0f6a1d64a5d3a87d11afbb4ab373c576257295;p=microjson.git Unit test now checks real and bool cases. --- diff --git a/microjson.c b/microjson.c index 6472213..97af731 100644 --- a/microjson.c +++ b/microjson.c @@ -859,14 +859,14 @@ int json_read_array(const char *cp, const struct json_array_t *arr, cp = ep; break; case t_uinteger: - arr->arr.uintegers.store[offset] = (int)strtoul(cp, &ep, 0); + arr->arr.uintegers.store[offset] = (unsigned int)strtoul(cp, &ep, 0); if (ep == cp) return JSON_ERR_BADNUM; else cp = ep; break; case t_real: - arr->arr.real.store[offset] = (int)strtod(cp, &ep); + arr->arr.reals.store[offset] = strtod(cp, &ep); if (ep == cp) return JSON_ERR_BADNUM; else @@ -874,11 +874,11 @@ int json_read_array(const char *cp, const struct json_array_t *arr, break; case t_boolean: if (strncmp(cp, "true", 4) == 0) { - arr->arr.boolean.store[offset] = true; + arr->arr.booleans.store[offset] = true; cp += 4; } else if (strncmp(cp, "false", 5) == 0) { - arr->arr.boolean.store[offset] = false; + arr->arr.booleans.store[offset] = false; cp += 5; } break; diff --git a/microjson.h b/microjson.h index 1041e3d..47e2671 100644 --- a/microjson.h +++ b/microjson.h @@ -40,10 +40,10 @@ struct json_array_t { } uintegers; struct { double *store; - } real; + } reals; struct { bool *store; - } boolean; + } booleans; } arr; int *count, maxlen; }; diff --git a/test_microjson.c b/test_microjson.c index fd4358c..68e7d8b 100644 --- a/test_microjson.c +++ b/test_microjson.c @@ -606,7 +606,7 @@ static const struct json_attr_t json_attrs_8[] = { static const char *json_str9 = "{\"parts\":[]}"; -/* Case 10: Read array of integers in a member */ +/* Case 10: Read array of integers */ static const char *json_str10 = "[23,-17,5]"; static int intstore[4], intcount; @@ -615,12 +615,41 @@ static int intstore[4], intcount; static const struct json_array_t json_array_10 = { .element_type = t_integer, .arr.integers.store = intstore, - .arr.integers.storelen = sizeof(intstore), .count = &intcount, .maxlen = sizeof(intstore)/sizeof(intstore[0]), }; /*@+type@*/ +/* Case 11: Read array of booleans */ + +static const char *json_str11 = "[true,false,true]"; +static bool boolstore[4]; +static int boolcount; + +/*@-type@*/ +static const struct json_array_t json_array_11 = { + .element_type = t_boolean, + .arr.booleans.store = boolstore, + .count = &boolcount, + .maxlen = sizeof(boolstore)/sizeof(boolstore[0]), +}; +/*@+type@*/ + +/* Case 12: Read array of reals */ + +static const char *json_str12 = "[23.1,-17.2,5.3]"; +static double realstore[4]; +static int realcount; + +/*@-type@*/ +static const struct json_array_t json_array_12 = { + .element_type = t_real, + .arr.reals.store = realstore, + .count = &realcount, + .maxlen = sizeof(realstore)/sizeof(realstore[0]), +}; +/*@+type@*/ + /*@ +fullinitblock @*/ /* *INDENT-ON* */ @@ -727,13 +756,32 @@ static void jsontest(int i) case 10: status = json_read_array(json_str10, &json_array_10, NULL); - assert_integer("instore[0]", intstore[0], 23); - assert_integer("instore[1]", intstore[1], -17); - assert_integer("instore[2]", intstore[2], 5); - assert_integer("instore[3]", intstore[3], 0); + assert_integer("count", intcount, 3); + assert_integer("intstore[0]", intstore[0], 23); + assert_integer("intstore[1]", intstore[1], -17); + assert_integer("intstore[2]", intstore[2], 5); + assert_integer("intstore[3]", intstore[3], 0); + break; + + case 11: + status = json_read_array(json_str11, &json_array_11, NULL); + assert_integer("count", boolcount, 3); + assert_boolean("boolstore[0]", boolstore[0], true); + assert_boolean("boolstore[1]", boolstore[1], false); + assert_boolean("boolstore[2]", boolstore[2], true); + assert_boolean("boolstore[3]", boolstore[3], false); + break; + + case 12: + status = json_read_array(json_str12, &json_array_12, NULL); + assert_integer("count", realcount, 3); + assert_real("realstore[0]", realstore[0], 23.1); + assert_real("realstore[1]", realstore[1], -17.2); + assert_real("realstore[2]", realstore[2], 5.3); + assert_real("realstore[3]", realstore[3], 0); break; -#define MAXTEST 10 +#define MAXTEST 12 default: (int)fputs("Unknown test number\n", stderr);