Hi all,

I just discovered something weird and annoying: the (test-end) in
the core version of test.scm behaves differently from the test egg!
In the test egg, (test-begin) and (test-end) are an alternate,
non-nesting syntax for (test-group), but in the core version, (test-end)
behaves like (test-exit) from the test egg.

This means that there are several tests which didn't really get tested
all the way through, especially the tests taken from the numbers egg
and irregex.

The first of the attached patches fixes the tests.  This exposed an
issue with how flonums are serialized by the compiler (and also in
the repl), because the numbers-test-gauche now failed.
Try it: (print (expt 2.0 64.0)) will print 0.0 (but it won't be
equal to 0.0)  The second patch fixes this issue; the commit message
explains the gory details.

I don't know whether we should also fix the helper function in master;
the number->string conversion there uses a different approach so it
is not affected by this specific bug, but u32vectors will allow you to
put a value like (expt 2.0 64.0) in the vector without giving an error,
and the effect is that the value will be zero.  Actually, the code is
wildly incorrect anyway for 64-bit platforms as it doesn't verify that
the number fits an unsigned integer, but it verifies that the number
fits a machine word, which is an unsigned long on 64 bit platforms.

The fix for master would be pretty straightforward: just replace
C_[U]WORD_MAX and C_WORD_MIN by [U]INT_MAX and INT_MIN in
C_fits_in_int_p and C_fits_in_unsigned_int_p.  I don't know if there's
any code around that actually relies on this checking whether the
number fits a _word_, instead, though.

Cheers,
Peter
From 58dd295deed6d5d04f09638646d4f5be0ca0a123 Mon Sep 17 00:00:00 2001
From: Peter Bex <[email protected]>
Date: Mon, 16 Oct 2017 17:44:19 +0200
Subject: [PATCH 1/2] Fix test-end to not end the entire process but the
 current group only

This makes it consistent with the test egg, and ensures some more
tests actually get run, which were basically dead tests before.
---
 tests/environment-tests.scm     |  2 ++
 tests/executable-tests.scm      |  2 ++
 tests/functor-tests.scm         |  4 +++-
 tests/match-test.scm            |  1 +
 tests/module-tests-compiled.scm |  2 ++
 tests/module-tests.scm          |  1 +
 tests/numbers-test-ashinn.scm   |  2 ++
 tests/numbers-test-gauche.scm   |  4 ++--
 tests/numbers-test.scm          |  1 +
 tests/port-tests.scm            |  4 +++-
 tests/test-find-files.scm       |  2 ++
 tests/test-irregex.scm          |  5 +++--
 tests/test.scm                  | 31 +++++++++++++++++++++++++++++--
 13 files changed, 53 insertions(+), 8 deletions(-)

diff --git a/tests/environment-tests.scm b/tests/environment-tests.scm
index 61f50f03..ef45b47b 100644
--- a/tests/environment-tests.scm
+++ b/tests/environment-tests.scm
@@ -53,3 +53,5 @@
 (test-error (eval 'baz format-env))
 
 (test-end)
+
+(test-exit)
diff --git a/tests/executable-tests.scm b/tests/executable-tests.scm
index f1ebc82a..d98f121f 100644
--- a/tests/executable-tests.scm
+++ b/tests/executable-tests.scm
@@ -30,3 +30,5 @@
               (read-symbolic-link* program-path)))
 
 (test-end)
+
+(test-exit)
diff --git a/tests/functor-tests.scm b/tests/functor-tests.scm
index 7fa5531f..61193796 100644
--- a/tests/functor-tests.scm
+++ b/tests/functor-tests.scm
@@ -5,7 +5,7 @@
 
 
 (include "test.scm")
-(test-begin)
+(test-begin "functor tests")
 
 ;;
 
@@ -240,3 +240,5 @@
 ;;
 
 (test-end)
+
+(test-exit)
diff --git a/tests/match-test.scm b/tests/match-test.scm
index d3dd2437..20568bfa 100644
--- a/tests/match-test.scm
+++ b/tests/match-test.scm
@@ -116,3 +116,4 @@
 
 (test-end "match")
 
+(test-exit)
diff --git a/tests/module-tests-compiled.scm b/tests/module-tests-compiled.scm
index 66031acb..09b2e94c 100644
--- a/tests/module-tests-compiled.scm
+++ b/tests/module-tests-compiled.scm
@@ -41,3 +41,5 @@
 
 
 (test-end "modules")
+
+(test-exit)
diff --git a/tests/module-tests.scm b/tests/module-tests.scm
index 33a4b15e..bed2049f 100644
--- a/tests/module-tests.scm
+++ b/tests/module-tests.scm
@@ -374,3 +374,4 @@
 
 (test-end "modules")
 
+(test-exit)
diff --git a/tests/numbers-test-ashinn.scm b/tests/numbers-test-ashinn.scm
index 713cae0c..9fa886ff 100644
--- a/tests/numbers-test-ashinn.scm
+++ b/tests/numbers-test-ashinn.scm
@@ -144,3 +144,5 @@
   (test-assert (bit->boolean #x10000000000000000 64)))
 
 (test-end)
+
+(test-exit)
diff --git a/tests/numbers-test-gauche.scm b/tests/numbers-test-gauche.scm
index 61423410..c14f4b90 100644
--- a/tests/numbers-test-gauche.scm
+++ b/tests/numbers-test-gauche.scm
@@ -48,7 +48,7 @@
 
 ;; Gauche compat
 
-(import bitwise)
+(import bitwise (chicken port) (chicken format) (chicken string))
 
 (define (greatest-fixnum) most-positive-fixnum)
 (define (least-fixnum) most-negative-fixnum)
@@ -2118,4 +2118,4 @@
 
 (test-end)
 
-(test-end)
+(test-exit)
diff --git a/tests/numbers-test.scm b/tests/numbers-test.scm
index da3c70b3..58586676 100644
--- a/tests/numbers-test.scm
+++ b/tests/numbers-test.scm
@@ -1657,3 +1657,4 @@
 (test-end)
 
 ;(unless (zero? (test-failure-count)) (exit 1))
+(test-exit)
diff --git a/tests/port-tests.scm b/tests/port-tests.scm
index 1330b443..90ffcabf 100644
--- a/tests/port-tests.scm
+++ b/tests/port-tests.scm
@@ -2,7 +2,7 @@
         file flonum format io port posix srfi-4 tcp)
 
 (include "test.scm")
-(test-begin)
+(test-begin "ports")
 
 (define-syntax assert-error
   (syntax-rules ()
@@ -436,3 +436,5 @@ EOF
 ;;;
 
 (test-end)
+
+(test-exit)
diff --git a/tests/test-find-files.scm b/tests/test-find-files.scm
index 08866b74..f09c02a1 100644
--- a/tests/test-find-files.scm
+++ b/tests/test-find-files.scm
@@ -215,3 +215,5 @@
 
 (change-directory "..")
 (delete-directory "find-files-test-dir" #t)
+
+(test-exit)
diff --git a/tests/test-irregex.scm b/tests/test-irregex.scm
index b3e1520e..19218bd8 100644
--- a/tests/test-irregex.scm
+++ b/tests/test-irregex.scm
@@ -59,7 +59,7 @@
 	 splt)
 	(warning "invalid regex test line" line))))
 
-(test-begin)
+(test-begin "basic irregex tests")
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; basic irregex
@@ -511,7 +511,7 @@
 
 ;;; UTF-8 tests
 
-(test-begin)
+(test-begin "utf-8 tests")
 
 (test-assert (irregex-search "(?u:<..>)" "<漢字>"))
 (test-assert (irregex-search "(?u:<.*>)" "<漢字>"))
@@ -541,3 +541,4 @@
 
 (test-end)
 
+(test-exit)
diff --git a/tests/test.scm b/tests/test.scm
index 5c886f5d..fc67fc51 100644
--- a/tests/test.scm
+++ b/tests/test.scm
@@ -5,9 +5,13 @@
 (import (only chicken.string ->string))
 (import time) ; current-milliseconds
 
+(define *current-group-name* "")
 (define *pass* 0)
 (define *fail* 0)
 (define *start* 0)
+(define *total-pass* 0)
+(define *total-fail* 0)
+(define *total-start* 0)
 (define *fail-token* (gensym))
 
 (define (run-test name thunk expect eq pass-msg fail-msg)
@@ -30,9 +34,15 @@
       (else (display (car ls)) (lp (cdr ls))))))
 
 (define (test-begin . o)
+  (set! *current-group-name* (if (null? o) "<unnamed>" (car o)))
+  (print "== " *current-group-name* " ==")
+  (set! *total-pass* (+ *total-pass* *pass*))
+  (set! *total-fail* (+ *total-fail* *fail*))
   (set! *pass* 0)
   (set! *fail* 0)
-  (set! *start* (current-milliseconds)))
+  (set! *start* (current-milliseconds))
+  (when (= 0 *total-start*)
+    (set! *total-start* (current-milliseconds))))
 
 (define (format-float n prec)
   (let* ((str (number->string n))
@@ -68,8 +78,25 @@
 	   "%) tests passed")
     (print "  " *fail* " ("
 	   (format-percent *fail* total)
+	   "%) tests failed"))
+    (print "-- " *current-group-name* " --\n\n"))
+
+(define (test-exit . o)
+  (print " TOTALS: ")
+  (set! *total-pass* (+ *total-pass* *pass*)) ; should be 0
+  (set! *total-fail* (+ *total-fail* *fail*)) ; should be 0
+  (let ((end (current-milliseconds))
+        (total (+ *total-pass* *total-fail*)))
+    (print "  " total " tests completed in "
+	   (format-float (exact->inexact (/ (- end *total-start*) 1000)) 3)
+	   " seconds")
+    (print "  " *total-pass* " ("
+	   (format-percent *total-pass* total)
+	   "%) tests passed")
+    (print "  " *total-fail* " ("
+	   (format-percent *total-fail* total)
 	   "%) tests failed")
-    (exit (if (zero? *fail*) 0 1))))
+    (exit (if (zero? *total-fail*) 0 1))))
 
 (define (run-equal name thunk expect eq)
   (run-test name thunk expect eq
-- 
2.11.0

From 493e9dd8c6f49a4738e4434152728b4a465f9ec4 Mon Sep 17 00:00:00 2001
From: Peter Bex <[email protected]>
Date: Mon, 16 Oct 2017 20:35:33 +0200
Subject: [PATCH 2/2] Fix flonum to string conversion on extreme edge cases on
 64-bit archs

This bug was exposed by the previous commit which restored the tests
to work as intended, because the serialization of (expt 2.0 64)
resulted in "0.0" in compiled code, causing the exact/inexact
comparison tests between the bignum (expt 2 64) and the flonum
(expt 2.0 64.0) to fail.

This representation fails because C_fits_in_unsigned_int_p will
incorrectly return C_SCHEME_TRUE on a 64-bit architecture due to how
we perform the casting: Because UINT_MAX is 2^64-1, it will be
normalized by the floating-point representation to the nearest
floating-point value that can represent it: 2^64.
Thus, (double)2^64 <= UINT_MAX is true.  When we then cast the
double to an unsigned word, it will result in zero due to overflow.

The fix here is to simply check if the log2 of the number is less than
the number of bits in an UWORD, rather than trying a direct value
comparison.  As a bonus, this allows us to finally get rid of the
obsolete C_fits_in_unsigned_int_p() inline helper function.
---
 chicken.h | 14 --------------
 runtime.c |  4 ++--
 2 files changed, 2 insertions(+), 16 deletions(-)

diff --git a/chicken.h b/chicken.h
index 9e2af974..45571d45 100644
--- a/chicken.h
+++ b/chicken.h
@@ -2420,20 +2420,6 @@ inline static C_word C_i_bignump(C_word x)
 }
 
 
-
-/* XXX TODO OBSOLETE (but still used by C_flonum_to_string) */
-inline static C_word C_fits_in_unsigned_int_p(C_word x)
-{
-  double n, m;
-
-  if(x & C_FIXNUM_BIT) return C_SCHEME_TRUE;
-  if(C_truep(C_i_bignump(x))) return C_mk_bool(C_bignum_size(x) == 1);
-
-  n = C_flonum_magnitude(x);
-  return C_mk_bool(C_modf(n, &m) == 0.0 && n >= 0 && n <= C_UWORD_MAX);
-}
-
-
 inline static double C_c_double(C_word x)
 {
   if(x & C_FIXNUM_BIT) return (double)C_unfix(x);
diff --git a/runtime.c b/runtime.c
index e1970cd6..2997b1a5 100644
--- a/runtime.c
+++ b/runtime.c
@@ -10432,7 +10432,7 @@ void C_ccall C_fixnum_to_string(C_word c, C_word *av)
 void C_ccall C_flonum_to_string(C_word c, C_word *av)
 {
   C_char *p;
-  double f;
+  double f, m;
   C_word *a,
     /* self = av[ 0 ] */
     k = av[ 1 ],
@@ -10450,7 +10450,7 @@ void C_ccall C_flonum_to_string(C_word c, C_word *av)
     barf(C_BAD_ARGUMENT_TYPE_BAD_BASE_ERROR, "number->string", C_fix(radix));
   }
 
-  if(C_fits_in_unsigned_int_p(num) == C_SCHEME_TRUE) { /* Use fast int code */
+  if(C_modf(f, &m) == 0.0 && f >= 0 && log2(f) < C_WORD_SIZE) { /* Use fast int code */
     if(f < 0) {
       p = to_n_nary((C_uword)-f, radix, 1, 1);
     } else {
-- 
2.11.0

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to