aboutsummaryrefslogtreecommitdiff
path: root/tests/tests_str.c
diff options
context:
space:
mode:
authorMartin Baƙinka <marun1@email.cz>2021-05-03 12:43:54 +0200
committerGitHub <noreply@github.com>2021-05-03 13:43:54 +0300
commit0c88e55fa8f132e8ac15426911d155f136078302 (patch)
tree03fc1b22c0582bd57ae3a14095d3d9c9f949f0c9 /tests/tests_str.c
parent4ea8f3ff12da61a5ceba690bf1e39d9385b42880 (diff)
downloadlibfixmath-0c88e55fa8f132e8ac15426911d155f136078302.tar.gz
fixed division, undefined behaviors and some improvements (#33)
* testing using ctest * emove old testing script * added github workflow CI * updated CI * added unit test for macros * F16() and F16C() are both rounding allways, so fix16_from_dbl should as well * added tests for strign operations, but these functions are in my opinion unreliable and tests are failing * removed old unittests * removed old unittests from cmake * problem with division using gcc * improved benchmark * clarification of problem with division * attempt to fix * fixed some undefined behaviors, fixed division
Diffstat (limited to 'tests/tests_str.c')
-rw-r--r--tests/tests_str.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/tests_str.c b/tests/tests_str.c
new file mode 100644
index 0000000..d893ffb
--- /dev/null
+++ b/tests/tests_str.c
@@ -0,0 +1,108 @@
+#include "tests_str.h"
+#include "tests.h"
+
+int test_str_to()
+{
+ char buf[13];
+ fix16_to_str(fix16_from_dbl(1234.5678), buf, 4);
+ ASSERT_EQ_STR(buf, "1234.5678");
+
+ fix16_to_str(fix16_from_dbl(-1234.5678), buf, 4);
+ ASSERT_EQ_STR(buf, "-1234.5678");
+
+ fix16_to_str(0, buf, 0);
+ ASSERT_EQ_STR(buf, "0");
+
+ fix16_to_str(fix16_from_dbl(0.9), buf, 0);
+ ASSERT_EQ_STR(buf, "1");
+
+ fix16_to_str(1, buf, 5);
+ ASSERT_EQ_STR(buf, "0.00002");
+
+ fix16_to_str(-1, buf, 5);
+ ASSERT_EQ_STR(buf, "-0.00002");
+
+ fix16_to_str(65535, buf, 5);
+ ASSERT_EQ_STR(buf, "0.99998");
+
+ fix16_to_str(65535, buf, 4);
+ ASSERT_EQ_STR(buf, "1.0000");
+
+ fix16_to_str(fix16_maximum, buf, 5);
+ ASSERT_EQ_STR(buf, "32767.99998");
+
+ fix16_to_str(fix16_minimum, buf, 5);
+ ASSERT_EQ_STR(buf, "-32768.00000");
+
+ return 0;
+}
+
+int test_str_from()
+{
+ ASSERT_EQ_INT(fix16_from_str("1234.5678"), fix16_from_dbl(1234.5678));
+ ASSERT_EQ_INT(fix16_from_str("-1234.5678"), fix16_from_dbl(-1234.5678));
+ ASSERT_EQ_INT(fix16_from_str(" +1234,56780 "),
+ fix16_from_dbl(1234.5678));
+
+ ASSERT_EQ_INT(fix16_from_str("0"), 0);
+ ASSERT_EQ_INT(fix16_from_str("1"), fix16_one);
+ ASSERT_EQ_INT(fix16_from_str("1.0"), fix16_one);
+ ASSERT_EQ_INT(fix16_from_str("1.0000000000"), fix16_one);
+
+ ASSERT_EQ_INT(fix16_from_str("0.00002"), 1);
+ ASSERT_EQ_INT(fix16_from_str("0.99998"), 65535);
+
+ ASSERT_EQ_INT(fix16_from_str("32767.99998"), fix16_maximum);
+ ASSERT_EQ_INT(fix16_from_str("-32768.00000"), fix16_minimum);
+
+ return 0;
+}
+
+int test_str_extended()
+{
+
+ fix16_t value = fix16_minimum;
+ char testbuf[13];
+ char goodbuf[13];
+
+ while (value < fix16_maximum)
+ {
+ double fvalue = fix16_to_dbl(value);
+
+ /* Turns out we have to jump through some hoops to round
+ doubles perfectly for printing:
+ http://stackoverflow.com/questions/994764/rounding-doubles-5-sprintf
+ */
+ fvalue = round(fvalue * 100000.) / 100000.;
+
+ snprintf(goodbuf, 13, "%0.5f", fvalue);
+ fix16_to_str(value, testbuf, 5);
+
+ if (strcmp(goodbuf, testbuf) != 0)
+ {
+ printf("Value (fix16_t)%d gave %s, should be %s\n", value, testbuf,
+ goodbuf);
+ return 1;
+ }
+
+ fix16_t roundtrip = fix16_from_str(testbuf);
+ if (roundtrip != value)
+ {
+ printf("Roundtrip failed: (fix16_t)%d -> %s -> (fix16_t)%d\n",
+ value, testbuf, roundtrip);
+ return 1;
+ }
+
+ value += 0x10001;
+ }
+
+ return 0;
+}
+
+int test_str()
+{
+ TEST(test_str_to());
+ TEST(test_str_from());
+ TEST(test_str_extended());
+ return 0;
+}