summaryrefslogtreecommitdiff
path: root/test_json.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2009-07-11 16:19:47 +0000
committerEric S. Raymond <esr@thyrsus.com>2009-07-11 16:19:47 +0000
commit6150cf71e8beb21e29ed4048f04806c9197734e1 (patch)
tree430f5a402a247b36d16ca34ebc0d98c7c9fbd14d /test_json.c
parentc60d911ef627fcbdd26dfce051b683d9f1280659 (diff)
downloadgpsd-6150cf71e8beb21e29ed4048f04806c9197734e1.tar.gz
JSON parser now verified to handle booleans.
Diffstat (limited to 'test_json.c')
-rw-r--r--test_json.c39
1 files changed, 26 insertions, 13 deletions
diff --git a/test_json.c b/test_json.c
index 9441cc5f..16508130 100644
--- a/test_json.c
+++ b/test_json.c
@@ -14,17 +14,24 @@
exit(1); \
}
-#define ASSERT_STRING(fld, val) \
+#define ASSERT_STRING(attr, fld, val) \
if (strcmp(fld, val)) \
{\
- (void)fprintf(stderr, "string attribute eval failed, value = %s.\n", fld);\
+ (void)fprintf(stderr, "'%s' attribute eval failed, value = %s.\n", attr, fld); \
exit(1);\
}
-#define ASSERT_INTEGER(fld, val) \
+#define ASSERT_INTEGER(attr, fld, val) \
if (fld != val)\
{\
- (void)fprintf(stderr, "integer attribute eval failed, value = %d.\n", fld);\
+ (void)fprintf(stderr, "'%s' attribute eval failed, value = %d.\n", attr, fld); \
+ exit(1);\
+ }
+
+#define ASSERT_BOOLEAN(attr, fld, val) \
+ if (fld != val)\
+ {\
+ (void)fprintf(stderr, "'%s' attribute eval failed, value = %s.\n", attr, fld ? "true" : "false"); \
exit(1);\
}
@@ -32,20 +39,22 @@
* Floating point comparisons are iffy, but at least if any of these fail
* the output will make it clear whether it was a precision issue
*/
-#define ASSERT_REAL(fld, val) \
+#define ASSERT_REAL(attr, fld, val) \
if (fld != val)\
{\
- (void)fprintf(stderr, "real attribute eval failed, value = %f.\n", fld);\
+ (void)fprintf(stderr, "'%s' attribute eval failed, value = %f.\n", attr, fld); \
exit(1);\
}
const char *json_str1 = "{\"device\":\"GPS#1\",\"tag\":\"MID2\",\
\"time\":1119197561.890,\"lon\":46.498203637,\"lat\":7.568074350,\
- \"alt\":1327.780,\"eph\":21.000,\"epv\":124.484,\"mode\":3}";
+ \"alt\":1327.780,\"eph\":21.000,\"epv\":124.484,\"mode\":3,\
+ \"flag1\":true,\"flag2\":false}";
static char buf1[JSON_VAL_MAX+1];
static char buf2[JSON_VAL_MAX+1];
static struct gps_fix_t fix;
+bool flag1, flag2;
const struct json_attr_t json_attrs_1[] = {
{"device", string, .addr.string = buf1},
@@ -57,6 +66,8 @@ const struct json_attr_t json_attrs_1[] = {
{"eph", real, .addr.real = &fix.eph, .dflt.real = 0},
{"epv", real, .addr.real = &fix.epv, .dflt.real = 0},
{"mode", integer, .addr.integer = &fix.mode, .dflt.integer = -1},
+ {"flag1", boolean, .addr.boolean = &flag1,},
+ {"flag2", boolean, .addr.boolean = &flag2,},
{NULL},
};
@@ -68,12 +79,14 @@ int main(int argc, char *argv[])
status = parse_json(json_str1, json_attrs_1);
ASSERT_CASE(1, status);
- ASSERT_STRING(buf1, "GPS#1");
- ASSERT_STRING(buf2, "MID2");
- ASSERT_INTEGER(fix.mode, 3);
- ASSERT_REAL(fix.time, 1119197561.890);
- ASSERT_REAL(fix.longitude, 46.498203637);
- ASSERT_REAL(fix.latitude, 7.568074350);
+ ASSERT_STRING("device", buf1, "GPS#1");
+ ASSERT_STRING("tag", buf2, "MID2");
+ ASSERT_INTEGER("mode", fix.mode, 3);
+ ASSERT_REAL("time", fix.time, 1119197561.890);
+ ASSERT_REAL("lon", fix.longitude, 46.498203637);
+ ASSERT_REAL("lat", fix.latitude, 7.568074350);
+ ASSERT_BOOLEAN("flag1", flag1, true);
+ ASSERT_BOOLEAN("flag2", flag2, false);
(void)fprintf(stderr, "succeeded.\n");
exit(0);