]> thyrsus.com Git - microjson.git/commitdiff
Unit test now checks real and bool cases.
authorEric S. Raymond <esr@thyrsus.com>
Wed, 17 Sep 2014 21:58:33 +0000 (17:58 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Wed, 17 Sep 2014 21:58:33 +0000 (17:58 -0400)
microjson.c
microjson.h
test_microjson.c

index 6472213cbc66030d5c390e721179a758d49112ab..97af731891a29e839f0fb4de1594f78bc351f17b 100644 (file)
@@ -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;
index 1041e3df77091ae9fbffafd69c0dabc9d3e322af..47e2671aa1c7e43180c3387085d8854a8fec9c1e 100644 (file)
@@ -40,10 +40,10 @@ struct json_array_t {
        } uintegers;
        struct {
            double *store;
-       } real;
+       } reals;
        struct {
            bool *store;
-       } boolean;
+       } booleans;
     } arr;
     int *count, maxlen;
 };
index fd4358ce628716876c7e5e26ad29049f518d7814..68e7d8b1b34b46da0195cb9fb30b565ce05e3f6a 100644 (file)
@@ -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);