sort.test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 of vector"
  29. (let* ((v (randomize-vector! (make-vector 1000) 1000))
  30. (w (vector-copy v)))
  31. (and (sorted? (sort v <) <)
  32. (equal? w v))))
  33. (pass-if "sort of typed array"
  34. (let* ((v (randomize-vector! (make-typed-array 'f64 *unspecified* 99) 99))
  35. (w (make-typed-array 'f64 *unspecified* 99)))
  36. (array-copy! v w)
  37. (and (sorted? (sort v <) <)
  38. (equal? w v))))
  39. (pass-if "sort! of vector"
  40. (let ((v (randomize-vector! (make-vector 1000) 1000)))
  41. (sorted? (sort! v <) <)))
  42. (pass-if "sort! of typed array"
  43. (let ((v (randomize-vector! (make-typed-array 'f64 *unspecified* 99) 99)))
  44. (sorted? (sort! v <) <)))
  45. (pass-if "sort! of non-contigous vector"
  46. (let* ((a (make-array 0 1000 3))
  47. (v (make-shared-array a (lambda (i) (list i 0)) 1000)))
  48. (randomize-vector! v 1000)
  49. (sorted? (sort! v <) <)))
  50. (pass-if "sort! of non-contigous typed array"
  51. (let* ((a (make-typed-array 'f64 0 99 3))
  52. (v (make-shared-array a (lambda (i) (list i 0)) 99)))
  53. (randomize-vector! v 99)
  54. (sorted? (sort! v <) <)))
  55. (pass-if "sort! of negative-increment vector"
  56. (let* ((a (make-array 0 1000 3))
  57. (v (make-shared-array a (lambda (i) (list (- 999 i) 0)) 1000)))
  58. (randomize-vector! v 1000)
  59. (sorted? (sort! v <) <)))
  60. (pass-if "sort! of negative-increment typed array"
  61. (let* ((a (make-typed-array 'f64 0 99 3))
  62. (v (make-shared-array a (lambda (i) (list (- 98 i) 0)) 99)))
  63. (randomize-vector! v 99)
  64. (sorted? (sort! v <) <)))
  65. (pass-if "stable-sort!"
  66. (let ((v (randomize-vector! (make-vector 1000) 1000)))
  67. (sorted? (stable-sort! v <) <)))
  68. (pass-if "stable-sort! of non-contigous vector"
  69. (let* ((a (make-array 0 1000 3))
  70. (v (make-shared-array a (lambda (i) (list i 0)) 1000)))
  71. (randomize-vector! v 1000)
  72. (sorted? (stable-sort! v <) <)))
  73. (pass-if "stable-sort! of negative-increment vector"
  74. (let* ((a (make-array 0 1000 3))
  75. (v (make-shared-array a (lambda (i) (list (- 999 i) 0)) 1000)))
  76. (randomize-vector! v 1000)
  77. (sorted? (stable-sort! v <) <))))
  78. ;;;
  79. ;;; stable-sort
  80. ;;;
  81. (with-test-prefix "stable-sort"
  82. ;; in guile 1.8.0 and 1.8.1 this test failed, an empty list provoked a
  83. ;; wrong-type-arg exception (where it shouldn't)
  84. (pass-if "empty list"
  85. (eq? '() (stable-sort '() <)))
  86. ;; Ditto here, but up to 2.0.1 and 2.1.0 and invoking undefined
  87. ;; behavior (integer underflow) leading to crashes.
  88. (pass-if "empty vector"
  89. (equal? '#() (stable-sort '#() <))))