sort.test 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ;;;; sort.test --- tests Guile's sort functions -*- scheme -*-
  2. ;;;; Copyright (C) 2003, 2006, 2007, 2009, 2011 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (use-modules (test-suite lib))
  18. (define (randomize-vector! v n)
  19. (array-index-map! v (lambda (i) (random n)))
  20. v)
  21. (with-test-prefix "sort"
  22. (pass-if-exception "less function taking less than two arguments"
  23. exception:wrong-num-args
  24. (sort '(1 2) (lambda (x) #t)))
  25. (pass-if-exception "less function taking more than two arguments"
  26. exception:wrong-num-args
  27. (sort '(1 2) (lambda (x y z) z)))
  28. (pass-if "sort!"
  29. (let ((v (randomize-vector! (make-vector 1000) 1000)))
  30. (sorted? (sort! v <) <)))
  31. (pass-if "sort! of non-contigous vector"
  32. (let* ((a (make-array 0 1000 3))
  33. (v (make-shared-array a (lambda (i) (list i 0)) 1000)))
  34. (randomize-vector! v 1000)
  35. (sorted? (sort! v <) <)))
  36. (pass-if "sort! of negative-increment vector"
  37. (let* ((a (make-array 0 1000 3))
  38. (v (make-shared-array a (lambda (i) (list (- 999 i) 0)) 1000)))
  39. (randomize-vector! v 1000)
  40. (sorted? (sort! v <) <)))
  41. (pass-if "stable-sort!"
  42. (let ((v (randomize-vector! (make-vector 1000) 1000)))
  43. (sorted? (stable-sort! v <) <)))
  44. (pass-if "stable-sort! of non-contigous vector"
  45. (let* ((a (make-array 0 1000 3))
  46. (v (make-shared-array a (lambda (i) (list i 0)) 1000)))
  47. (randomize-vector! v 1000)
  48. (sorted? (stable-sort! v <) <)))
  49. (pass-if "stable-sort! of negative-increment vector"
  50. (let* ((a (make-array 0 1000 3))
  51. (v (make-shared-array a (lambda (i) (list (- 999 i) 0)) 1000)))
  52. (randomize-vector! v 1000)
  53. (sorted? (stable-sort! v <) <))))
  54. ;;;
  55. ;;; stable-sort
  56. ;;;
  57. (with-test-prefix "stable-sort"
  58. ;; in guile 1.8.0 and 1.8.1 this test failed, an empty list provoked a
  59. ;; wrong-type-arg exception (where it shouldn't)
  60. (pass-if "empty list"
  61. (eq? '() (stable-sort '() <)))
  62. ;; Ditto here, but up to 2.0.1 and 2.1.0 and invoking undefined
  63. ;; behavior (integer underflow) leading to crashes.
  64. (pass-if "empty vector"
  65. (equal? '#() (stable-sort '#() <))))