* test-suite/tests/sort.test: Added. Both tests in that file did
fail (one even with a segfault) with CVS guile before the recent
changes to libguile/sort.c.
* libguile/sort.c: Replaced hand-made trampline code by the new
official mechanism from eval.c. This fixes a segfault in the new
test file test-suite/tests/sort.test.
(quicksort, compare_function, scm_restricted_vector_sort_x,
scm_sorted_p, scm_merge, scm_merge_list_x, scm_merge_x,
scm_merge_list_step, scm_sort_x, scm_sort, scm_merge_vector_x,
scm_merge_vector_step, scm_stable_sort_x, scm_stable_sort,
scm_sort_list_x, scm_sort_list): Use trampoline mechanism from
eval.c.
(subr2less, lsubrless, closureless, applyless, scm_cmp_function,
cmp_fun_t): Removed.
(compare_function): Added.
* libguile/sort.c (quicksort, SWAP, stack_node): Replaced pointer
arithmetics with index arithmetics. Changed quicksort to work on
an array of SCM values instead of an array of characters. Avoid
bytewise copying of SCM elements. Avoid allocating memory on the
stack with alloca. Fixed some comments.
2003-04-22 23:32:40 +00:00
|
|
|
;;;; sort.test --- tests Guile's sort functions -*- scheme -*-
|
2011-06-09 22:11:02 +02:00
|
|
|
;;;; Copyright (C) 2003, 2006, 2007, 2009, 2011 Free Software Foundation, Inc.
|
* test-suite/tests/sort.test: Added. Both tests in that file did
fail (one even with a segfault) with CVS guile before the recent
changes to libguile/sort.c.
* libguile/sort.c: Replaced hand-made trampline code by the new
official mechanism from eval.c. This fixes a segfault in the new
test file test-suite/tests/sort.test.
(quicksort, compare_function, scm_restricted_vector_sort_x,
scm_sorted_p, scm_merge, scm_merge_list_x, scm_merge_x,
scm_merge_list_step, scm_sort_x, scm_sort, scm_merge_vector_x,
scm_merge_vector_step, scm_stable_sort_x, scm_stable_sort,
scm_sort_list_x, scm_sort_list): Use trampoline mechanism from
eval.c.
(subr2less, lsubrless, closureless, applyless, scm_cmp_function,
cmp_fun_t): Removed.
(compare_function): Added.
* libguile/sort.c (quicksort, SWAP, stack_node): Replaced pointer
arithmetics with index arithmetics. Changed quicksort to work on
an array of SCM values instead of an array of characters. Avoid
bytewise copying of SCM elements. Avoid allocating memory on the
stack with alloca. Fixed some comments.
2003-04-22 23:32:40 +00:00
|
|
|
;;;;
|
2009-06-17 00:22:09 +01:00
|
|
|
;;;; This library is free software; you can redistribute it and/or
|
|
|
|
|
;;;; modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
;;;; License as published by the Free Software Foundation; either
|
|
|
|
|
;;;; version 3 of the License, or (at your option) any later version.
|
* test-suite/tests/sort.test: Added. Both tests in that file did
fail (one even with a segfault) with CVS guile before the recent
changes to libguile/sort.c.
* libguile/sort.c: Replaced hand-made trampline code by the new
official mechanism from eval.c. This fixes a segfault in the new
test file test-suite/tests/sort.test.
(quicksort, compare_function, scm_restricted_vector_sort_x,
scm_sorted_p, scm_merge, scm_merge_list_x, scm_merge_x,
scm_merge_list_step, scm_sort_x, scm_sort, scm_merge_vector_x,
scm_merge_vector_step, scm_stable_sort_x, scm_stable_sort,
scm_sort_list_x, scm_sort_list): Use trampoline mechanism from
eval.c.
(subr2less, lsubrless, closureless, applyless, scm_cmp_function,
cmp_fun_t): Removed.
(compare_function): Added.
* libguile/sort.c (quicksort, SWAP, stack_node): Replaced pointer
arithmetics with index arithmetics. Changed quicksort to work on
an array of SCM values instead of an array of characters. Avoid
bytewise copying of SCM elements. Avoid allocating memory on the
stack with alloca. Fixed some comments.
2003-04-22 23:32:40 +00:00
|
|
|
;;;;
|
2009-06-17 00:22:09 +01:00
|
|
|
;;;; This library is distributed in the hope that it will be useful,
|
* test-suite/tests/sort.test: Added. Both tests in that file did
fail (one even with a segfault) with CVS guile before the recent
changes to libguile/sort.c.
* libguile/sort.c: Replaced hand-made trampline code by the new
official mechanism from eval.c. This fixes a segfault in the new
test file test-suite/tests/sort.test.
(quicksort, compare_function, scm_restricted_vector_sort_x,
scm_sorted_p, scm_merge, scm_merge_list_x, scm_merge_x,
scm_merge_list_step, scm_sort_x, scm_sort, scm_merge_vector_x,
scm_merge_vector_step, scm_stable_sort_x, scm_stable_sort,
scm_sort_list_x, scm_sort_list): Use trampoline mechanism from
eval.c.
(subr2less, lsubrless, closureless, applyless, scm_cmp_function,
cmp_fun_t): Removed.
(compare_function): Added.
* libguile/sort.c (quicksort, SWAP, stack_node): Replaced pointer
arithmetics with index arithmetics. Changed quicksort to work on
an array of SCM values instead of an array of characters. Avoid
bytewise copying of SCM elements. Avoid allocating memory on the
stack with alloca. Fixed some comments.
2003-04-22 23:32:40 +00:00
|
|
|
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2009-06-17 00:22:09 +01:00
|
|
|
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
;;;; Lesser General Public License for more details.
|
* test-suite/tests/sort.test: Added. Both tests in that file did
fail (one even with a segfault) with CVS guile before the recent
changes to libguile/sort.c.
* libguile/sort.c: Replaced hand-made trampline code by the new
official mechanism from eval.c. This fixes a segfault in the new
test file test-suite/tests/sort.test.
(quicksort, compare_function, scm_restricted_vector_sort_x,
scm_sorted_p, scm_merge, scm_merge_list_x, scm_merge_x,
scm_merge_list_step, scm_sort_x, scm_sort, scm_merge_vector_x,
scm_merge_vector_step, scm_stable_sort_x, scm_stable_sort,
scm_sort_list_x, scm_sort_list): Use trampoline mechanism from
eval.c.
(subr2less, lsubrless, closureless, applyless, scm_cmp_function,
cmp_fun_t): Removed.
(compare_function): Added.
* libguile/sort.c (quicksort, SWAP, stack_node): Replaced pointer
arithmetics with index arithmetics. Changed quicksort to work on
an array of SCM values instead of an array of characters. Avoid
bytewise copying of SCM elements. Avoid allocating memory on the
stack with alloca. Fixed some comments.
2003-04-22 23:32:40 +00:00
|
|
|
;;;;
|
2009-06-17 00:22:09 +01:00
|
|
|
;;;; You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
;;;; License along with this library; if not, write to the Free Software
|
|
|
|
|
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
* test-suite/tests/sort.test: Added. Both tests in that file did
fail (one even with a segfault) with CVS guile before the recent
changes to libguile/sort.c.
* libguile/sort.c: Replaced hand-made trampline code by the new
official mechanism from eval.c. This fixes a segfault in the new
test file test-suite/tests/sort.test.
(quicksort, compare_function, scm_restricted_vector_sort_x,
scm_sorted_p, scm_merge, scm_merge_list_x, scm_merge_x,
scm_merge_list_step, scm_sort_x, scm_sort, scm_merge_vector_x,
scm_merge_vector_step, scm_stable_sort_x, scm_stable_sort,
scm_sort_list_x, scm_sort_list): Use trampoline mechanism from
eval.c.
(subr2less, lsubrless, closureless, applyless, scm_cmp_function,
cmp_fun_t): Removed.
(compare_function): Added.
* libguile/sort.c (quicksort, SWAP, stack_node): Replaced pointer
arithmetics with index arithmetics. Changed quicksort to work on
an array of SCM values instead of an array of characters. Avoid
bytewise copying of SCM elements. Avoid allocating memory on the
stack with alloca. Fixed some comments.
2003-04-22 23:32:40 +00:00
|
|
|
|
|
|
|
|
(use-modules (test-suite lib))
|
|
|
|
|
|
2005-01-02 21:03:12 +00:00
|
|
|
(define (randomize-vector! v n)
|
|
|
|
|
(array-index-map! v (lambda (i) (random n)))
|
|
|
|
|
v)
|
|
|
|
|
|
* test-suite/tests/sort.test: Added. Both tests in that file did
fail (one even with a segfault) with CVS guile before the recent
changes to libguile/sort.c.
* libguile/sort.c: Replaced hand-made trampline code by the new
official mechanism from eval.c. This fixes a segfault in the new
test file test-suite/tests/sort.test.
(quicksort, compare_function, scm_restricted_vector_sort_x,
scm_sorted_p, scm_merge, scm_merge_list_x, scm_merge_x,
scm_merge_list_step, scm_sort_x, scm_sort, scm_merge_vector_x,
scm_merge_vector_step, scm_stable_sort_x, scm_stable_sort,
scm_sort_list_x, scm_sort_list): Use trampoline mechanism from
eval.c.
(subr2less, lsubrless, closureless, applyless, scm_cmp_function,
cmp_fun_t): Removed.
(compare_function): Added.
* libguile/sort.c (quicksort, SWAP, stack_node): Replaced pointer
arithmetics with index arithmetics. Changed quicksort to work on
an array of SCM values instead of an array of characters. Avoid
bytewise copying of SCM elements. Avoid allocating memory on the
stack with alloca. Fixed some comments.
2003-04-22 23:32:40 +00:00
|
|
|
(with-test-prefix "sort"
|
|
|
|
|
|
|
|
|
|
(pass-if-exception "less function taking less than two arguments"
|
2009-08-21 00:38:48 +02:00
|
|
|
exception:wrong-num-args
|
* test-suite/tests/sort.test: Added. Both tests in that file did
fail (one even with a segfault) with CVS guile before the recent
changes to libguile/sort.c.
* libguile/sort.c: Replaced hand-made trampline code by the new
official mechanism from eval.c. This fixes a segfault in the new
test file test-suite/tests/sort.test.
(quicksort, compare_function, scm_restricted_vector_sort_x,
scm_sorted_p, scm_merge, scm_merge_list_x, scm_merge_x,
scm_merge_list_step, scm_sort_x, scm_sort, scm_merge_vector_x,
scm_merge_vector_step, scm_stable_sort_x, scm_stable_sort,
scm_sort_list_x, scm_sort_list): Use trampoline mechanism from
eval.c.
(subr2less, lsubrless, closureless, applyless, scm_cmp_function,
cmp_fun_t): Removed.
(compare_function): Added.
* libguile/sort.c (quicksort, SWAP, stack_node): Replaced pointer
arithmetics with index arithmetics. Changed quicksort to work on
an array of SCM values instead of an array of characters. Avoid
bytewise copying of SCM elements. Avoid allocating memory on the
stack with alloca. Fixed some comments.
2003-04-22 23:32:40 +00:00
|
|
|
(sort '(1 2) (lambda (x) #t)))
|
|
|
|
|
|
|
|
|
|
(pass-if-exception "less function taking more than two arguments"
|
2009-08-21 00:38:48 +02:00
|
|
|
exception:wrong-num-args
|
2005-01-02 21:03:12 +00:00
|
|
|
(sort '(1 2) (lambda (x y z) z)))
|
|
|
|
|
|
|
|
|
|
(pass-if "sort!"
|
|
|
|
|
(let ((v (randomize-vector! (make-vector 1000) 1000)))
|
|
|
|
|
(sorted? (sort! v <) <)))
|
|
|
|
|
|
|
|
|
|
(pass-if "sort! of non-contigous vector"
|
|
|
|
|
(let* ((a (make-array 0 1000 3))
|
|
|
|
|
(v (make-shared-array a (lambda (i) (list i 0)) 1000)))
|
|
|
|
|
(randomize-vector! v 1000)
|
|
|
|
|
(sorted? (sort! v <) <)))
|
|
|
|
|
|
|
|
|
|
(pass-if "sort! of negative-increment vector"
|
|
|
|
|
(let* ((a (make-array 0 1000 3))
|
|
|
|
|
(v (make-shared-array a (lambda (i) (list (- 999 i) 0)) 1000)))
|
|
|
|
|
(randomize-vector! v 1000)
|
|
|
|
|
(sorted? (sort! v <) <)))
|
|
|
|
|
|
|
|
|
|
(pass-if "stable-sort!"
|
|
|
|
|
(let ((v (randomize-vector! (make-vector 1000) 1000)))
|
|
|
|
|
(sorted? (stable-sort! v <) <)))
|
|
|
|
|
|
|
|
|
|
(pass-if "stable-sort! of non-contigous vector"
|
|
|
|
|
(let* ((a (make-array 0 1000 3))
|
|
|
|
|
(v (make-shared-array a (lambda (i) (list i 0)) 1000)))
|
|
|
|
|
(randomize-vector! v 1000)
|
|
|
|
|
(sorted? (stable-sort! v <) <)))
|
|
|
|
|
|
|
|
|
|
(pass-if "stable-sort! of negative-increment vector"
|
|
|
|
|
(let* ((a (make-array 0 1000 3))
|
|
|
|
|
(v (make-shared-array a (lambda (i) (list (- 999 i) 0)) 1000)))
|
|
|
|
|
(randomize-vector! v 1000)
|
|
|
|
|
(sorted? (stable-sort! v <) <))))
|
2007-03-07 23:00:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; stable-sort
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "stable-sort"
|
|
|
|
|
|
|
|
|
|
;; in guile 1.8.0 and 1.8.1 this test failed, an empty list provoked a
|
|
|
|
|
;; wrong-type-arg exception (where it shouldn't)
|
|
|
|
|
(pass-if "empty list"
|
2011-06-09 22:11:02 +02:00
|
|
|
(eq? '() (stable-sort '() <)))
|
|
|
|
|
|
|
|
|
|
;; Ditto here, but up to 2.0.1 and 2.1.0 and invoking undefined
|
|
|
|
|
;; behavior (integer underflow) leading to crashes.
|
|
|
|
|
(pass-if "empty vector"
|
|
|
|
|
(equal? '#() (stable-sort '#() <))))
|
2007-03-07 23:00:22 +00:00
|
|
|
|