tests.c: add minmea_scan("f") overflow tests

This commit is contained in:
Kosma Moczek 2014-04-27 17:05:21 +02:00
parent a5f67aa500
commit 334e63d599

39
tests.c
View File

@ -126,6 +126,10 @@ START_TEST(test_minmea_scan_f)
ck_assert(minmea_scan("", "f", &f) == true); ck_assert(minmea_scan("", "f", &f) == true);
ck_assert_int_eq(f.scale, 0); ck_assert_int_eq(f.scale, 0);
ck_assert(minmea_scan("42", "f", &f) == true);
ck_assert_int_eq(f.value, 42);
ck_assert_int_eq(f.scale, 1);
ck_assert(minmea_scan("15.345", "f", &f) == true); ck_assert(minmea_scan("15.345", "f", &f) == true);
ck_assert_int_eq(f.value, 15345); ck_assert_int_eq(f.value, 15345);
ck_assert_int_eq(f.scale, 1000); ck_assert_int_eq(f.scale, 1000);
@ -134,13 +138,8 @@ START_TEST(test_minmea_scan_f)
ck_assert_int_eq(f.value, -123); ck_assert_int_eq(f.value, -123);
ck_assert_int_eq(f.scale, 100); ck_assert_int_eq(f.scale, 100);
/* some GPS units have absurdly big precision. handle whatever int handles. */ /* the guaranteed range is 32 bits which translates to +-180 degrees
ck_assert(minmea_scan("5106.94091", "f", &f) == true); * with 5 decimal digits. make sure we support that. */
ck_assert_int_eq(f.value, 510694091);
ck_assert_int_eq(f.scale, 100000);
/* for now we support +-180 degrees with 5 decimal digits; anything
* more will overflow. */
ck_assert(minmea_scan("18000.00000", "f", &f) == true); ck_assert(minmea_scan("18000.00000", "f", &f) == true);
ck_assert_int_eq(f.value, 1800000000); ck_assert_int_eq(f.value, 1800000000);
ck_assert_int_eq(f.scale, 100000); ck_assert_int_eq(f.scale, 100000);
@ -148,6 +147,32 @@ START_TEST(test_minmea_scan_f)
ck_assert_int_eq(f.value, -1800000000); ck_assert_int_eq(f.value, -1800000000);
ck_assert_int_eq(f.scale, 100000); ck_assert_int_eq(f.scale, 100000);
if (sizeof(int_least32_t) == 4) {
/* fits in 32 bits */
ck_assert(minmea_scan("2147483647", "f", &f) == true);
ck_assert_int_eq(f.value, 2147483647);
ck_assert_int_eq(f.scale, 1);
/* doesn't fit, truncate precision */
ck_assert(minmea_scan("2147483.648", "f", &f) == true);
ck_assert_int_eq(f.value, 214748364);
ck_assert_int_eq(f.scale, 100);
/* doesn't fit, bail out */
ck_assert(minmea_scan("2147483648", "f", &f) == false);
} else if (sizeof(int_least32_t) == 8) {
/* fits in 64 bits */
ck_assert(minmea_scan("9223372036854775807", "f", &f) == true);
ck_assert_int_eq(f.value, 9223372036854775807);
ck_assert_int_eq(f.scale, 1);
/* doesn't fit, truncate precision */
ck_assert(minmea_scan("9223372036854775.808", "f", &f) == true);
ck_assert_int_eq(f.value, 922337203685477580);
ck_assert_int_eq(f.scale, 100);
/* doesn't fit, bail out */
ck_assert(minmea_scan("9223372036854775808", "f", &f) == false);
} else {
ck_abort_msg("your platform is esoteric. please fix this unit test.");
}
/* optional f.values */ /* optional f.values */
ck_assert(minmea_scan("foo", "_;f", &f) == true); ck_assert(minmea_scan("foo", "_;f", &f) == true);
ck_assert_int_eq(f.scale, 0); ck_assert_int_eq(f.scale, 0);