summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/data/number/number.dat9
-rw-r--r--test/number.c69
2 files changed, 72 insertions, 6 deletions
diff --git a/test/data/number/number.dat b/test/data/number/number.dat
index 48ecc20..1004f55 100644
--- a/test/data/number/number.dat
+++ b/test/data/number/number.dat
@@ -28,12 +28,10 @@
2097151.000
#reset
-# Test INT_MAX + 1. Note that, in converting the result to float,
-# we'll end up with INT_MAX + 1 as the output.
#data
2097152
#expected
-2097152.000
+2097151.999
#reset
#data
@@ -66,16 +64,17 @@
-2097151.000
#reset
+# TODO: should these next two not result in INT_MIN?
#data
-2097152
#expected
--2097152.000
+-2097151.999
#reset
#data
-2097153
#expected
--2097152.000
+-2097151.999
#reset
#data
diff --git a/test/number.c b/test/number.c
index 39aa494..1f4f9d9 100644
--- a/test/number.c
+++ b/test/number.c
@@ -21,6 +21,7 @@ typedef struct line_ctx {
static bool handle_line(const char *data, size_t datalen, void *pw);
static void run_test(const uint8_t *data, size_t len,
const char *exp, size_t explen);
+static void print_fixed(char *buf, size_t len, fixed f);
int main(int argc, char **argv)
{
@@ -117,10 +118,76 @@ void run_test(const uint8_t *data, size_t len, const char *exp, size_t explen)
result = number_from_css_string(&in, false, &consumed);
- snprintf(buf, sizeof buf, "%.3f", FIXTOFLT(result));
+ print_fixed(buf, sizeof(buf), result);
printf("got: %s expected: %.*s\n", buf, (int) explen, exp);
assert(strncmp(buf, exp, explen) == 0);
}
+void print_fixed(char *buf, size_t len, fixed f)
+{
+#define ABS(x) ((x) < 0 ? -(x) : (x))
+ uint32_t uintpart = FIXTOINT(ABS(f));
+ uint32_t fracpart = ((ABS(f) & 0x3ff) * 1000) / (1 << 10);
+#undef ABS
+ size_t flen = 0;
+ char tmp[20];
+ size_t tlen = 0;
+
+ if (len == 0)
+ return;
+
+ if (f < 0) {
+ buf[0] = '-';
+ buf++;
+ len--;
+ }
+
+ do {
+ tmp[tlen] = "0123456789"[uintpart % 10];
+ tlen++;
+
+ uintpart /= 10;
+ } while (tlen < 20 && uintpart != 0);
+
+ while (len > 0 && tlen > 0) {
+ buf[0] = tmp[--tlen];
+ buf++;
+ len--;
+ }
+
+ if (len > 0) {
+ buf[0] = '.';
+ buf++;
+ len--;
+ }
+
+ do {
+ tmp[tlen] = "0123456789"[fracpart % 10];
+ tlen++;
+
+ fracpart /= 10;
+ } while (tlen < 20 && fracpart != 0);
+
+ while (len > 0 && tlen > 0) {
+ buf[0] = tmp[--tlen];
+ buf++;
+ len--;
+ flen++;
+ }
+
+ while (len > 0 && flen < 3) {
+ buf[0] = '0';
+ buf++;
+ len--;
+ flen++;
+ }
+
+ if (len > 0) {
+ buf[0] = '\0';
+ buf++;
+ len--;
+ }
+}
+