diff options
author | Olaf Bergmann <bergmann@tzi.org> | 2015-04-24 17:18:09 +0200 |
---|---|---|
committer | Olaf Bergmann <bergmann@tzi.org> | 2015-04-24 17:18:09 +0200 |
commit | 6359753af8d173f77f24281ea8009ae083892713 (patch) | |
tree | 02d95f095807f4da89a3d54bbc952f8883fad1f8 | |
parent | 33034139f07dbc26ff4ef861bd9100b5b03f67bb (diff) | |
download | cn-cbor-6359753af8d173f77f24281ea8009ae083892713.tar.gz |
Handle float test cases when CBOR_NO_FLOAT is set
When support for floats is switched off, some parser tests do not
show the expected results. This modification makes some tests check
for the expected error code CN_CBOR_ERR_FLOAT_NOT_SUPPORTED and
switches off a few other.
-rw-r--r-- | test/cbor_test.c | 38 | ||||
-rw-r--r-- | test/test.c | 1 |
2 files changed, 34 insertions, 5 deletions
diff --git a/test/cbor_test.c b/test/cbor_test.c index 4918a87..43f73a4 100644 --- a/test/cbor_test.c +++ b/test/cbor_test.c @@ -60,6 +60,7 @@ CTEST(cbor, error) ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING], "CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING"); ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_INVALID_PARAMETER], "CN_CBOR_ERR_INVALID_PARAMETER"); ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_OUT_OF_MEMORY], "CN_CBOR_ERR_OUT_OF_MEMORY"); + ASSERT_STR(cn_cbor_error_str[CN_CBOR_ERR_FLOAT_NOT_SUPPORTED], "CN_CBOR_ERR_FLOAT_NOT_SUPPORTED"); } CTEST(cbor, parse) @@ -92,6 +93,7 @@ CTEST(cbor, parse) "f6", // null "f7", // undefined "f8ff", // simple(255) +#ifndef CBOR_NO_FLOAT "f93c00", // 1.0 "f9bc00", // -1.0 "f903ff", // 6.097555160522461e-05 @@ -101,6 +103,7 @@ CTEST(cbor, parse) "fa47800000", // 65536.0 "fb3ff199999999999a", // 1.1 "f97e00", // NaN +#endif /* CBOR_NO_FLOAT */ "5f42010243030405ff", // (_ h'0102', h'030405') "7f61616161ff", // (_ "a", "a") "9fff", // [_ ] @@ -133,7 +136,7 @@ CTEST(cbor, parse) CTEST(cbor, parse_normalize) { cn_cbor_errback err; - char *tests[] = { + char *basic_tests[] = { "00", "00", // 0 "1800", "00", "1818", "1818", @@ -146,6 +149,8 @@ CTEST(cbor, parse_normalize) "c600", "c600", // 6(0) (undefined tag) "d80600", "c600", "d9000600", "c600", + }; + char *float_tests[] = { "fb3ff0000000000000", "f93c00", // 1.0 "fbbff0000000000000", "f9bc00", // -1.0 "fb40f86a0000000000", "fa47c35000", // 100000.0 @@ -160,12 +165,12 @@ CTEST(cbor, parse_normalize) unsigned char encoded[1024]; ssize_t enc_sz; - for (i=0; i<sizeof(tests)/sizeof(char*); ) { - ASSERT_TRUE(parse_hex(tests[i++], &b)); - ASSERT_TRUE(parse_hex(tests[i++], &b2)); + for (i=0; i<sizeof(basic_tests)/sizeof(char*); i+=2) { + ASSERT_TRUE(parse_hex(basic_tests[i], &b)); + ASSERT_TRUE(parse_hex(basic_tests[i+1], &b2)); err.err = CN_CBOR_NO_ERROR; cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err); - CTEST_LOG("%s: %s", tests[i], cn_cbor_error_str[err.err]); + CTEST_LOG("%s: %s", basic_tests[i], cn_cbor_error_str[err.err]); ASSERT_EQUAL(err.err, CN_CBOR_NO_ERROR); ASSERT_NOT_NULL(cb); @@ -175,6 +180,27 @@ CTEST(cbor, parse_normalize) free(b2.ptr); cn_cbor_free(cb CONTEXT_NULL); } + + for (i=0; i<sizeof(float_tests)/sizeof(char*); i+=2) { + ASSERT_TRUE(parse_hex(float_tests[i], &b)); + ASSERT_TRUE(parse_hex(float_tests[i+1], &b2)); + err.err = CN_CBOR_NO_ERROR; + cb = cn_cbor_decode(b.ptr, b.sz CONTEXT_NULL, &err); + CTEST_LOG("%s: %s", float_tests[i], cn_cbor_error_str[err.err]); +#ifndef CBOR_NO_FLOAT + ASSERT_EQUAL(err.err, CN_CBOR_NO_ERROR); + ASSERT_NOT_NULL(cb); +#else /* CBOR_NO_FLOAT */ + ASSERT_EQUAL(err.err, CN_CBOR_ERR_FLOAT_NOT_SUPPORTED); + ASSERT_NULL(cb); +#endif /* CBOR_NO_FLOAT */ + + /* enc_sz = cbor_encoder_write(encoded, 0, sizeof(encoded), cb); */ + /* ASSERT_DATA(b2.ptr, b2.sz, encoded, enc_sz); */ + free(b.ptr); + free(b2.ptr); + cn_cbor_free(cb CONTEXT_NULL); + } } typedef struct _cbor_failure @@ -217,6 +243,7 @@ CTEST(cbor, fail) // Decoder loses float size information CTEST(cbor, float) { +#ifndef CBOR_NO_FLOAT cn_cbor_errback err; char *tests[] = { "f90001", // 5.960464477539063e-08 @@ -243,6 +270,7 @@ CTEST(cbor, float) free(b.ptr); cn_cbor_free(cb CONTEXT_NULL); } +#endif /* CBOR_NO_FLOAT */ } CTEST(cbor, getset) diff --git a/test/test.c b/test/test.c index 42c6cf6..263f9fe 100644 --- a/test/test.c +++ b/test/test.c @@ -98,6 +98,7 @@ const char *err_name[] = { "CN_CBOR_ERR_RESERVED_AI", "CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING", "CN_CBOR_ERR_OUT_OF_MEMORY", + "CN_CBOR_ERR_FLOAT_NOT_SUPPORTED", }; static void cn_cbor_decode_test(const unsigned char *buf, int len) { |