vectors.test 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;;;; vectors.test --- test suite for Guile's vector functions -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2003, 2006, 2010, 2011 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-suite vectors)
  19. :use-module (test-suite lib))
  20. ;; FIXME: As soon as guile supports immutable vectors, this has to be
  21. ;; replaced with the appropriate error type and message.
  22. (define exception:immutable-vector
  23. (cons 'some-error-type "^trying to modify an immutable vector"))
  24. (with-test-prefix "vector-set!"
  25. (expect-fail-exception "vector constant"
  26. exception:immutable-vector
  27. (vector-set! '#(1 2 3) 0 4)))
  28. (with-test-prefix "vector->list"
  29. (pass-if "simple vector"
  30. (equal? '(1 2 3) (vector->list #(1 2 3))))
  31. (pass-if "string vector 1"
  32. (equal? '("abc" "def" "ghi") (vector->list #("abc" "def" "ghi"))))
  33. (pass-if "string-vector 2"
  34. (equal? '("abc\u0100" "def\u0101" "ghi\u0102")
  35. (vector->list #("abc\u0100" "def\u0101" "ghi\u0102"))))
  36. (pass-if "shared array"
  37. (let ((b (make-shared-array #(1) (lambda (x) '(0)) 2)))
  38. (equal? b (list->vector (vector->list b))))))
  39. (with-test-prefix "make-vector"
  40. (pass-if "null"
  41. (equal? #() (make-vector 0)))
  42. (pass-if "fill with num"
  43. (equal? #(1 1 1) (make-vector 3 1)))
  44. (pass-if "fill with string"
  45. (equal? #("abc" "abc" "abc") (make-vector 3 "abc")))
  46. (pass-if "fill with string 2"
  47. (equal? #("ab\u0100" "ab\u0100" "ab\u0100")
  48. (make-vector 3 "ab\u0100"))))
  49. (with-test-prefix "vector-move-left!"
  50. (pass-if-exception "before start" exception:out-of-range
  51. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  52. (b (vector 10 20 30 40 50 60 70 80 90)))
  53. (vector-move-left! a 3 5 b -1)))
  54. (pass-if "beginning"
  55. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  56. (b (vector 10 20 30 40 50 60 70 80 90)))
  57. (vector-move-left! a 3 5 b 0)
  58. (equal? b #(4 5 30 40 50 60 70 80 90))))
  59. (pass-if "middle"
  60. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  61. (b (vector 10 20 30 40 50 60 70 80 90)))
  62. (vector-move-left! a 3 5 b 2)
  63. (equal? b #(10 20 4 5 50 60 70 80 90))))
  64. (pass-if "overlap -"
  65. (let ((a (vector 1 2 3 4 5 6 7 8 9)))
  66. (vector-move-left! a 3 5 a 2)
  67. (equal? a #(1 2 4 5 5 6 7 8 9))))
  68. (pass-if "overlap +"
  69. (let ((a (vector 1 2 3 4 5 6 7 8 9)))
  70. (vector-move-left! a 3 5 a 4)
  71. (equal? a #(1 2 3 4 4 4 7 8 9))))
  72. (pass-if "end"
  73. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  74. (b (vector 10 20 30 40 50 60 70 80 90)))
  75. (vector-move-left! a 3 5 b 7)
  76. (equal? b #(10 20 30 40 50 60 70 4 5))))
  77. (pass-if "whole thing"
  78. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  79. (b (vector 10 20 30 40 50 60 70 80 90)))
  80. (vector-move-left! a 0 9 b 0)
  81. (equal? b #(1 2 3 4 5 6 7 8 9))))
  82. (pass-if-exception "past end" exception:out-of-range
  83. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  84. (b (vector 10 20 30 40 50 60 70 80 90)))
  85. (vector-move-left! a 3 5 b 8))))
  86. (with-test-prefix "vector-move-right!"
  87. (pass-if-exception "before start" exception:out-of-range
  88. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  89. (b (vector 10 20 30 40 50 60 70 80 90)))
  90. (vector-move-right! a 3 5 b -1)))
  91. (pass-if "beginning"
  92. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  93. (b (vector 10 20 30 40 50 60 70 80 90)))
  94. (vector-move-right! a 3 5 b 0)
  95. (equal? b #(4 5 30 40 50 60 70 80 90))))
  96. (pass-if "middle"
  97. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  98. (b (vector 10 20 30 40 50 60 70 80 90)))
  99. (vector-move-right! a 3 5 b 2)
  100. (equal? b #(10 20 4 5 50 60 70 80 90))))
  101. (pass-if "overlap -"
  102. (let ((a (vector 1 2 3 4 5 6 7 8 9)))
  103. (vector-move-right! a 3 5 a 2)
  104. (equal? a #(1 2 5 5 5 6 7 8 9))))
  105. (pass-if "overlap +"
  106. (let ((a (vector 1 2 3 4 5 6 7 8 9)))
  107. (vector-move-right! a 3 5 a 4)
  108. (equal? a #(1 2 3 4 4 5 7 8 9))))
  109. (pass-if "end"
  110. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  111. (b (vector 10 20 30 40 50 60 70 80 90)))
  112. (vector-move-right! a 3 5 b 7)
  113. (equal? b #(10 20 30 40 50 60 70 4 5))))
  114. (pass-if "whole thing"
  115. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  116. (b (vector 10 20 30 40 50 60 70 80 90)))
  117. (vector-move-right! a 0 9 b 0)
  118. (equal? b #(1 2 3 4 5 6 7 8 9))))
  119. (pass-if-exception "past end" exception:out-of-range
  120. (let ((a (vector 1 2 3 4 5 6 7 8 9))
  121. (b (vector 10 20 30 40 50 60 70 80 90)))
  122. (vector-move-right! a 3 5 b 8))))