aboutsummaryrefslogtreecommitdiffstats
path: root/tests/decimal_test.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-10-06 20:40:56 +0200
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:59 +0100
commit9e59a63d9b5897d9ff6d0d9936a57c3167ea9a34 (patch)
treef54ecb581bef3e8476725e989b36e14ac556e0a2 /tests/decimal_test.c
parent5a0d9e5a66e0ad0b85db3740b479301128637ad5 (diff)
downloadserd-9e59a63d9b5897d9ff6d0d9936a57c3167ea9a34.tar.gz
serd-9e59a63d9b5897d9ff6d0d9936a57c3167ea9a34.tar.bz2
serd-9e59a63d9b5897d9ff6d0d9936a57c3167ea9a34.zip
Add precise decimal digit generation
Diffstat (limited to 'tests/decimal_test.c')
-rw-r--r--tests/decimal_test.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/tests/decimal_test.c b/tests/decimal_test.c
index b0361e2c..674ce5cb 100644
--- a/tests/decimal_test.c
+++ b/tests/decimal_test.c
@@ -1,5 +1,5 @@
/*
- Copyright 2011-2019 David Robillard <http://drobilla.net>
+ Copyright 2011-2020 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -18,7 +18,13 @@
#include "../src/decimal.h"
+#include "serd/serd.h"
+
#include <assert.h>
+#include <math.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
static void
test_count_digits(void)
@@ -48,8 +54,50 @@ test_count_digits(void)
assert(20 == serd_count_digits(18446744073709551615ull));
}
+static void
+check_precision(const double d,
+ const unsigned precision,
+ const unsigned frac_digits,
+ const char* expected)
+{
+ SerdNode* const node = serd_new_decimal(d, precision, frac_digits, NULL);
+ const char* str = serd_node_string(node);
+
+ if (strcmp(str, expected)) {
+ fprintf(stderr, "error: string is \"%s\"\n", str);
+ fprintf(stderr, "note: expected \"%s\"\n", expected);
+ assert(false);
+ }
+
+ serd_node_free(node);
+}
+
+static void
+test_precision(void)
+{
+ assert(serd_new_decimal((double)INFINITY, 17, 0, NULL) == NULL);
+ assert(serd_new_decimal((double)-INFINITY, 17, 0, NULL) == NULL);
+ assert(serd_new_decimal((double)NAN, 17, 0, NULL) == NULL);
+
+ check_precision(1.0000000001, 17, 8, "1.0");
+ check_precision(0.0000000001, 17, 10, "0.0000000001");
+ check_precision(0.0000000001, 17, 8, "0.0");
+
+ check_precision(12345.678900, 9, 5, "12345.6789");
+ check_precision(12345.678900, 8, 5, "12345.678");
+ check_precision(12345.678900, 5, 5, "12345.0");
+ check_precision(12345.678900, 3, 5, "12300.0");
+
+ check_precision(12345.678900, 9, 0, "12345.6789");
+ check_precision(12345.678900, 9, 5, "12345.6789");
+ check_precision(12345.678900, 9, 3, "12345.678");
+ check_precision(12345.678900, 9, 1, "12345.6");
+}
+
int
main(void)
{
test_count_digits();
+ test_precision();
+ return 0;
}