sort.test 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;;; sort.test --- tests Guile's sort functions -*- scheme -*-
  2. ;;;; Copyright (C) 2003, 2006, 2007 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program 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
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;;;; Boston, MA 02110-1301 USA
  18. (use-modules (test-suite lib))
  19. (define (randomize-vector! v n)
  20. (array-index-map! v (lambda (i) (random n)))
  21. v)
  22. (with-test-prefix "sort"
  23. (pass-if-exception "less function taking less than two arguments"
  24. exception:wrong-type-arg
  25. (sort '(1 2) (lambda (x) #t)))
  26. (pass-if-exception "less function taking more than two arguments"
  27. exception:wrong-type-arg
  28. (sort '(1 2) (lambda (x y z) z)))
  29. (pass-if "sort!"
  30. (let ((v (randomize-vector! (make-vector 1000) 1000)))
  31. (sorted? (sort! v <) <)))
  32. (pass-if "sort! of non-contigous vector"
  33. (let* ((a (make-array 0 1000 3))
  34. (v (make-shared-array a (lambda (i) (list i 0)) 1000)))
  35. (randomize-vector! v 1000)
  36. (sorted? (sort! v <) <)))
  37. (pass-if "sort! of negative-increment vector"
  38. (let* ((a (make-array 0 1000 3))
  39. (v (make-shared-array a (lambda (i) (list (- 999 i) 0)) 1000)))
  40. (randomize-vector! v 1000)
  41. (sorted? (sort! v <) <)))
  42. (pass-if "stable-sort!"
  43. (let ((v (randomize-vector! (make-vector 1000) 1000)))
  44. (sorted? (stable-sort! v <) <)))
  45. (pass-if "stable-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? (stable-sort! v <) <)))
  50. (pass-if "stable-sort! of negative-increment vector"
  51. (let* ((a (make-array 0 1000 3))
  52. (v (make-shared-array a (lambda (i) (list (- 999 i) 0)) 1000)))
  53. (randomize-vector! v 1000)
  54. (sorted? (stable-sort! v <) <))))
  55. ;;;
  56. ;;; stable-sort
  57. ;;;
  58. (with-test-prefix "stable-sort"
  59. ;; in guile 1.8.0 and 1.8.1 this test failed, an empty list provoked a
  60. ;; wrong-type-arg exception (where it shouldn't)
  61. (pass-if "empty list"
  62. (eq? '() (stable-sort '() <))))