Author: jmb
Date: Sat Jan 17 02:59:41 2009
New Revision: 6111

URL: http://source.netsurf-browser.org?rev=6111&view=rev
Log:
Use our own printing routine, rather than relying on snprintf to do the right 
thing wrt rounding.
Update test data in line with this. That said, it would appear that values 
<=-(2^21) get truncated to -2097151.999. This probably wants investigating.

Modified:
    trunk/libcss/test/data/number/number.dat
    trunk/libcss/test/number.c

Modified: trunk/libcss/test/data/number/number.dat
URL: 
http://source.netsurf-browser.org/trunk/libcss/test/data/number/number.dat?rev=6111&r1=6110&r2=6111&view=diff
==============================================================================
--- trunk/libcss/test/data/number/number.dat (original)
+++ trunk/libcss/test/data/number/number.dat Sat Jan 17 02:59:41 2009
@@ -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

Modified: trunk/libcss/test/number.c
URL: 
http://source.netsurf-browser.org/trunk/libcss/test/number.c?rev=6111&r1=6110&r2=6111&view=diff
==============================================================================
--- trunk/libcss/test/number.c (original)
+++ trunk/libcss/test/number.c Sat Jan 17 02:59:41 2009
@@ -21,6 +21,7 @@
 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 @@
 
        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--;
+       }
+}
+


_______________________________________________
netsurf-commits mailing list
[email protected]
http://vlists.pepperfish.net/cgi-bin/mailman/listinfo/netsurf-commits-netsurf-browser.org

Reply via email to