[Git][NTPsec/ntpsec][master] tests/ieee754io: test 32 bit floats too.
Gary E. Miller
gitlab at mg.gitlab.com
Tue Apr 4 23:50:52 UTC 2017
Gary E. Miller pushed to branch master at NTPsec / ntpsec
Commits:
85c39bf2 by Gary E. Miller at 2017-04-04T16:47:41-07:00
tests/ieee754io: test 32 bit floats too.
- - - - -
1 changed file:
- tests/libparse/ieee754io.c
Changes:
=====================================
tests/libparse/ieee754io.c
=====================================
--- a/tests/libparse/ieee754io.c
+++ b/tests/libparse/ieee754io.c
@@ -7,6 +7,14 @@
#include "unity.h"
#include "unity_fixture.h"
+/*
+ * Tests for libparse/ieee754io.c, in -lparse
+ *
+ * It would be nice to use C floats and doubles here, but
+ * the byte order is not the same as trimble or meinberg,
+ * and C floats and doubles need not be IEEE 754
+ */
+
TEST_GROUP(ieee754io);
TEST_SETUP(ieee754io) {}
@@ -21,83 +29,224 @@ static offsets_t native_off = { 0, 1, 2, 3, 4, 5, 6, 7 };
/*
unsigned char *p = (void *)&fp;
printf("fp: %d %d %d %d %d %d %d %d\n",
- p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
+ p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
*/
+TEST(ieee754io, test_zero32) {
+ int ret;
+ unsigned char one[4] = { 0, 0, 0, 0};
+ unsigned char *bp = &one[0];
+ l_fp fp;
+
+ ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+}
+
+TEST(ieee754io, test_one32) {
+ int ret;
+ unsigned char one[4] = { 0x3f, 0x80, 0, 0};
+ unsigned char *bp = &one[0];
+ l_fp fp;
+
+ ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_INT64( 1L << 32, (long)fp );
+}
+
+TEST(ieee754io, test_negone32) {
+ int ret;
+ unsigned char negone[4] = { 0xbf, 0x80, 0, 0};
+ unsigned char *bp = &negone[0];
+ l_fp fp;
+
+ ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_INT64( -(1L << 32), (long)fp );
+}
+
+TEST(ieee754io, test_nan32) {
+ int ret;
+ unsigned char buf[4] = { 0x7f, 0x80, 0, 0};
+ unsigned char *bp = &buf[0];
+ l_fp fp;
+
+ /* +INF */
+ ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_POSINFINITY == ret);
+ /* not IEEE754, but check for 0 anyway */
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+
+ /* -INF */
+ buf[0] = 0xff;
+ bp = &buf[0];
+ ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_NEGINFINITY == ret);
+ /* not IEEE754, but check for 0 anyway */
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+
+ /* NAN */
+ buf[0] = 0x7f;
+ buf[1] = 0xf8;
+ bp = &buf[0];
+ ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_NAN == ret);
+ /* not IEEE754, but check for 0 anyway */
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+}
+
+TEST(ieee754io, test_max32) {
+ int ret;
+ /* not enough precision in a float to get max l_fp */
+ unsigned char buf[4] = { 0x7f, 127, 255, 255};
+ /* not enough precision in a float to get negative max l_fp */
+ unsigned char buf_n[4] = { 0xff, 127, 255, 255};
+ unsigned char *bp = &buf[0];
+ l_fp fp;
+
+ /* max that fits */
+ ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_UINT64( (unsigned long)0xFFFFFF00UL, fp );
+
+ /* negative max that fits */
+ bp = &buf_n[0];
+ ret = fetch_ieee754( &bp, IEEE_SINGLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_UINT64( (unsigned long)0xFFFFFFFF00000100UL, fp );
+}
+
+
TEST(ieee754io, test_zero64) {
int ret;
unsigned char one[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
- unsigned char *bp = &one[0];
+ unsigned char *bp = &one[0];
l_fp fp;
- ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
- TEST_ASSERT( IEEE_OK == ret);
- TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
}
TEST(ieee754io, test_one64) {
int ret;
unsigned char one[8] = { 0x3f, 0xf0, 0, 0, 0, 0, 0, 0 };
- unsigned char *bp = &one[0];
+ unsigned char *bp = &one[0];
l_fp fp;
- ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
- TEST_ASSERT( IEEE_OK == ret);
- TEST_ASSERT_EQUAL_INT64( 1L << 32, (long)fp );
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_INT64( 1L << 32, (long)fp );
}
TEST(ieee754io, test_negone64) {
int ret;
unsigned char negone[8] = { 0xbf, 0xf0, 0, 0, 0, 0, 0, 0 };
- unsigned char *bp = &negone[0];
+ unsigned char *bp = &negone[0];
l_fp fp;
- ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
- TEST_ASSERT( IEEE_OK == ret);
- TEST_ASSERT_EQUAL_INT64( -(1L << 32), (long)fp );
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_INT64( -(1L << 32), (long)fp );
}
TEST(ieee754io, test_nan64) {
int ret;
unsigned char buf[8] = { 0x7f, 0xF0, 0, 0, 0, 0, 0, 0 };
- unsigned char *bp = &buf[0];
+ unsigned char *bp = &buf[0];
l_fp fp;
/* +INF */
- ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
- TEST_ASSERT( IEEE_POSINFINITY == ret);
+ TEST_ASSERT( IEEE_POSINFINITY == ret);
/* not IEEE754, but check for 0 anyway */
- TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
/* -INF */
buf[0] = 0xff;
- bp = &buf[0];
- ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+ bp = &buf[0];
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_NEGINFINITY == ret);
+ /* not IEEE754, but check for 0 anyway */
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+
+ /* +OVERFLOW */
+ buf[0] = 0x41;
+ buf[1] = 0xff;
+ bp = &buf[0];
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
- printf("\n%d\n", ret);
- TEST_ASSERT( IEEE_NEGINFINITY == ret);
+ TEST_ASSERT( IEEE_POSOVERFLOW == ret);
/* not IEEE754, but check for 0 anyway */
- TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+
+ /* -OVERFLOW */
+ buf[0] = 0xC1;
+ buf[1] = 0xff;
+ bp = &buf[0];
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_NEGOVERFLOW == ret);
+ /* not IEEE754, but check for 0 anyway */
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
/* NAN */
buf[0] = 0x7f;
buf[1] = 0xf8;
- bp = &buf[0];
- ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+ bp = &buf[0];
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
- printf("\n%d\n", ret);
- TEST_ASSERT( IEEE_NAN == ret);
+ TEST_ASSERT( IEEE_NAN == ret);
/* not IEEE754, but check for 0 anyway */
- TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+ TEST_ASSERT_EQUAL_INT64( 0, (long)fp );
+}
+
+TEST(ieee754io, test_max64) {
+ int ret;
+ /* not enough precision in a double to get max l_fp */
+ unsigned char buf[8] = { 65, 239, 255, 255, 255, 255, 255, 255 };
+ /* not enough precision in a double to get negative max l_fp */
+ unsigned char buf_n[8] = { 65, 239, 255, 255, 255, 255, 255, 254 };
+ unsigned char *bp = &buf[0];
+ l_fp fp;
+
+ /* max that fits */
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_UINT64( (unsigned long)0xFFFFFFFFFFFFF800UL, fp );
+
+ /* negative max that fits */
+ bp = &buf_n[0];
+ ret = fetch_ieee754( &bp, IEEE_DOUBLE, &fp, native_off);
+
+ TEST_ASSERT( IEEE_OK == ret);
+ TEST_ASSERT_EQUAL_UINT64( (unsigned long)0xFFFFFFFFFFFFF000UL, fp );
}
TEST_GROUP_RUNNER(ieee754io) {
- RUN_TEST_CASE(ieee754io, test_zero64);
- RUN_TEST_CASE(ieee754io, test_one64);
- RUN_TEST_CASE(ieee754io, test_negone64);
- RUN_TEST_CASE(ieee754io, test_nan64);
+ RUN_TEST_CASE(ieee754io, test_zero32);
+ RUN_TEST_CASE(ieee754io, test_one32);
+ RUN_TEST_CASE(ieee754io, test_negone32);
+ RUN_TEST_CASE(ieee754io, test_nan32);
+ RUN_TEST_CASE(ieee754io, test_max32);
+
+ RUN_TEST_CASE(ieee754io, test_zero64);
+ RUN_TEST_CASE(ieee754io, test_one64);
+ RUN_TEST_CASE(ieee754io, test_negone64);
+ RUN_TEST_CASE(ieee754io, test_nan64);
+ RUN_TEST_CASE(ieee754io, test_max64);
}
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/85c39bf21bf0522512bc15379be9f14ab47f7a50
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170404/2d3a2328/attachment.html>
More information about the vc
mailing list