arrays.test 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. ;;;; arrays.test --- tests guile's uniform arrays -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2004, 2006, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Free
  4. ;;;; Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-arrays)
  20. #:use-module ((system base compile) #:select (compile))
  21. #:use-module (test-suite lib)
  22. #:use-module (srfi srfi-4)
  23. #:use-module (srfi srfi-4 gnu))
  24. ;;;
  25. ;;; array?
  26. ;;;
  27. (define exception:wrong-num-indices
  28. (cons 'misc-error "^wrong number of indices.*"))
  29. (define exception:length-non-negative
  30. (cons 'read-error ".*array length must be non-negative.*"))
  31. (with-test-prefix "array?"
  32. (let ((bool (make-typed-array 'b #t '(5 6)))
  33. (char (make-typed-array 'a #\a '(5 6)))
  34. (byte (make-typed-array 'u8 0 '(5 6)))
  35. (short (make-typed-array 's16 0 '(5 6)))
  36. (ulong (make-typed-array 'u32 0 '(5 6)))
  37. (long (make-typed-array 's32 0 '(5 6)))
  38. (longlong (make-typed-array 's64 0 '(5 6)))
  39. (float (make-typed-array 'f32 0 '(5 6)))
  40. (double (make-typed-array 'f64 0 '(5 6)))
  41. (complex (make-typed-array 'c64 0 '(5 6)))
  42. (scm (make-typed-array #t 0 '(5 6))))
  43. (with-test-prefix "is bool"
  44. (pass-if (eq? #t (typed-array? bool 'b)))
  45. (pass-if (eq? #f (typed-array? char 'b)))
  46. (pass-if (eq? #f (typed-array? byte 'b)))
  47. (pass-if (eq? #f (typed-array? short 'b)))
  48. (pass-if (eq? #f (typed-array? ulong 'b)))
  49. (pass-if (eq? #f (typed-array? long 'b)))
  50. (pass-if (eq? #f (typed-array? longlong 'b)))
  51. (pass-if (eq? #f (typed-array? float 'b)))
  52. (pass-if (eq? #f (typed-array? double 'b)))
  53. (pass-if (eq? #f (typed-array? complex 'b)))
  54. (pass-if (eq? #f (typed-array? scm 'b))))
  55. (with-test-prefix "is char"
  56. (pass-if (eq? #f (typed-array? bool 'a)))
  57. (pass-if (eq? #t (typed-array? char 'a)))
  58. (pass-if (eq? #f (typed-array? byte 'a)))
  59. (pass-if (eq? #f (typed-array? short 'a)))
  60. (pass-if (eq? #f (typed-array? ulong 'a)))
  61. (pass-if (eq? #f (typed-array? long 'a)))
  62. (pass-if (eq? #f (typed-array? longlong 'a)))
  63. (pass-if (eq? #f (typed-array? float 'a)))
  64. (pass-if (eq? #f (typed-array? double 'a)))
  65. (pass-if (eq? #f (typed-array? complex 'a)))
  66. (pass-if (eq? #f (typed-array? scm 'a))))
  67. (with-test-prefix "is byte"
  68. (pass-if (eq? #f (typed-array? bool 'u8)))
  69. (pass-if (eq? #f (typed-array? char 'u8)))
  70. (pass-if (eq? #t (typed-array? byte 'u8)))
  71. (pass-if (eq? #f (typed-array? short 'u8)))
  72. (pass-if (eq? #f (typed-array? ulong 'u8)))
  73. (pass-if (eq? #f (typed-array? long 'u8)))
  74. (pass-if (eq? #f (typed-array? longlong 'u8)))
  75. (pass-if (eq? #f (typed-array? float 'u8)))
  76. (pass-if (eq? #f (typed-array? double 'u8)))
  77. (pass-if (eq? #f (typed-array? complex 'u8)))
  78. (pass-if (eq? #f (typed-array? scm 'u8))))
  79. (with-test-prefix "is short"
  80. (pass-if (eq? #f (typed-array? bool 's16)))
  81. (pass-if (eq? #f (typed-array? char 's16)))
  82. (pass-if (eq? #f (typed-array? byte 's16)))
  83. (pass-if (eq? #t (typed-array? short 's16)))
  84. (pass-if (eq? #f (typed-array? ulong 's16)))
  85. (pass-if (eq? #f (typed-array? long 's16)))
  86. (pass-if (eq? #f (typed-array? longlong 's16)))
  87. (pass-if (eq? #f (typed-array? float 's16)))
  88. (pass-if (eq? #f (typed-array? double 's16)))
  89. (pass-if (eq? #f (typed-array? complex 's16)))
  90. (pass-if (eq? #f (typed-array? scm 's16))))
  91. (with-test-prefix "is ulong"
  92. (pass-if (eq? #f (typed-array? bool 'u32)))
  93. (pass-if (eq? #f (typed-array? char 'u32)))
  94. (pass-if (eq? #f (typed-array? byte 'u32)))
  95. (pass-if (eq? #f (typed-array? short 'u32)))
  96. (pass-if (eq? #t (typed-array? ulong 'u32)))
  97. (pass-if (eq? #f (typed-array? long 'u32)))
  98. (pass-if (eq? #f (typed-array? longlong 'u32)))
  99. (pass-if (eq? #f (typed-array? float 'u32)))
  100. (pass-if (eq? #f (typed-array? double 'u32)))
  101. (pass-if (eq? #f (typed-array? complex 'u32)))
  102. (pass-if (eq? #f (typed-array? scm 'u32))))
  103. (with-test-prefix "is long"
  104. (pass-if (eq? #f (typed-array? bool 's32)))
  105. (pass-if (eq? #f (typed-array? char 's32)))
  106. (pass-if (eq? #f (typed-array? byte 's32)))
  107. (pass-if (eq? #f (typed-array? short 's32)))
  108. (pass-if (eq? #f (typed-array? ulong 's32)))
  109. (pass-if (eq? #t (typed-array? long 's32)))
  110. (pass-if (eq? #f (typed-array? longlong 's32)))
  111. (pass-if (eq? #f (typed-array? float 's32)))
  112. (pass-if (eq? #f (typed-array? double 's32)))
  113. (pass-if (eq? #f (typed-array? complex 's32)))
  114. (pass-if (eq? #f (typed-array? scm 's32))))
  115. (with-test-prefix "is long long"
  116. (pass-if (eq? #f (typed-array? bool 's64)))
  117. (pass-if (eq? #f (typed-array? char 's64)))
  118. (pass-if (eq? #f (typed-array? byte 's64)))
  119. (pass-if (eq? #f (typed-array? short 's64)))
  120. (pass-if (eq? #f (typed-array? ulong 's64)))
  121. (pass-if (eq? #f (typed-array? long 's64)))
  122. (pass-if (eq? #t (typed-array? longlong 's64)))
  123. (pass-if (eq? #f (typed-array? float 's64)))
  124. (pass-if (eq? #f (typed-array? double 's64)))
  125. (pass-if (eq? #f (typed-array? complex 's64)))
  126. (pass-if (eq? #f (typed-array? scm 's64))))
  127. (with-test-prefix "is float"
  128. (pass-if (eq? #f (typed-array? bool 'f32)))
  129. (pass-if (eq? #f (typed-array? char 'f32)))
  130. (pass-if (eq? #f (typed-array? byte 'f32)))
  131. (pass-if (eq? #f (typed-array? short 'f32)))
  132. (pass-if (eq? #f (typed-array? ulong 'f32)))
  133. (pass-if (eq? #f (typed-array? long 'f32)))
  134. (pass-if (eq? #f (typed-array? longlong 'f32)))
  135. (pass-if (eq? #t (typed-array? float 'f32)))
  136. (pass-if (eq? #f (typed-array? double 'f32)))
  137. (pass-if (eq? #f (typed-array? complex 'f32)))
  138. (pass-if (eq? #f (typed-array? scm 'f32))))
  139. (with-test-prefix "is double"
  140. (pass-if (eq? #f (typed-array? bool 'f64)))
  141. (pass-if (eq? #f (typed-array? char 'f64)))
  142. (pass-if (eq? #f (typed-array? byte 'f64)))
  143. (pass-if (eq? #f (typed-array? short 'f64)))
  144. (pass-if (eq? #f (typed-array? ulong 'f64)))
  145. (pass-if (eq? #f (typed-array? long 'f64)))
  146. (pass-if (eq? #f (typed-array? longlong 'f64)))
  147. (pass-if (eq? #f (typed-array? float 'f64)))
  148. (pass-if (eq? #t (typed-array? double 'f64)))
  149. (pass-if (eq? #f (typed-array? complex 'f64)))
  150. (pass-if (eq? #f (typed-array? scm 'f64))))
  151. (with-test-prefix "is complex"
  152. (pass-if (eq? #f (typed-array? bool 'c64)))
  153. (pass-if (eq? #f (typed-array? char 'c64)))
  154. (pass-if (eq? #f (typed-array? byte 'c64)))
  155. (pass-if (eq? #f (typed-array? short 'c64)))
  156. (pass-if (eq? #f (typed-array? ulong 'c64)))
  157. (pass-if (eq? #f (typed-array? long 'c64)))
  158. (pass-if (eq? #f (typed-array? longlong 'c64)))
  159. (pass-if (eq? #f (typed-array? float 'c64)))
  160. (pass-if (eq? #f (typed-array? double 'c64)))
  161. (pass-if (eq? #t (typed-array? complex 'c64)))
  162. (pass-if (eq? #f (typed-array? scm 'c64))))
  163. (with-test-prefix "is scm"
  164. (pass-if (eq? #f (typed-array? bool #t)))
  165. (pass-if (eq? #f (typed-array? char #t)))
  166. (pass-if (eq? #f (typed-array? byte #t)))
  167. (pass-if (eq? #f (typed-array? short #t)))
  168. (pass-if (eq? #f (typed-array? ulong #t)))
  169. (pass-if (eq? #f (typed-array? long #t)))
  170. (pass-if (eq? #f (typed-array? longlong #t)))
  171. (pass-if (eq? #f (typed-array? float #t)))
  172. (pass-if (eq? #f (typed-array? double #t)))
  173. (pass-if (eq? #f (typed-array? complex #t)))
  174. (pass-if (eq? #t (typed-array? scm #t))))
  175. (with-test-prefix "typed-array? returns #f"
  176. (pass-if (eq? #f (typed-array? '(1 2 3) 'c64)))
  177. (pass-if (eq? #f (typed-array? '(1 2 3) #t)))
  178. (pass-if (eq? #f (typed-array? 99 'c64)))
  179. (pass-if (eq? #f (typed-array? 99 #t))))))
  180. ;;;
  181. ;;; array-equal?
  182. ;;;
  183. (with-test-prefix/c&e "array-equal?"
  184. (pass-if "#s16(...)"
  185. (array-equal? #s16(1 2 3) #s16(1 2 3))))
  186. ;;;
  187. ;;; make-shared-array
  188. ;;;
  189. (define exception:mapping-out-of-range
  190. (cons 'misc-error "^mapping out of range")) ;; per scm_make_shared_array
  191. (with-test-prefix/c&e "make-shared-array"
  192. ;; this failed in guile 1.8.0
  193. (pass-if "vector unchanged"
  194. (let* ((a (make-array #f '(0 7)))
  195. (s (make-shared-array a list '(0 7))))
  196. (array-equal? a s)))
  197. (pass-if-exception "vector, high too big" exception:mapping-out-of-range
  198. (let* ((a (make-array #f '(0 7))))
  199. (make-shared-array a list '(0 8))))
  200. (pass-if-exception "vector, low too big" exception:out-of-range
  201. (let* ((a (make-array #f '(0 7))))
  202. (make-shared-array a list '(-1 7))))
  203. (pass-if "truncate columns"
  204. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
  205. #2((a b) (d e) (g h))))
  206. (pass-if "pick one column"
  207. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  208. (lambda (i) (list i 2))
  209. '(0 2))
  210. #(c f i)))
  211. (pass-if "diagonal"
  212. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  213. (lambda (i) (list i i))
  214. '(0 2))
  215. #(a e i)))
  216. ;; this failed in guile 1.8.0
  217. (pass-if "2 dims from 1 dim"
  218. (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
  219. (lambda (i j) (list (+ (* i 3) j)))
  220. 4 3)
  221. #2((a b c) (d e f) (g h i) (j k l))))
  222. (pass-if "reverse columns"
  223. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  224. (lambda (i j) (list i (- 2 j)))
  225. 3 3)
  226. #2((c b a) (f e d) (i h g))))
  227. (pass-if "fixed offset, 0 based becomes 1 based"
  228. (let* ((x #2((a b c) (d e f) (g h i)))
  229. (y (make-shared-array x
  230. (lambda (i j) (list (1- i) (1- j)))
  231. '(1 3) '(1 3))))
  232. (and (eq? (array-ref x 0 0) 'a)
  233. (eq? (array-ref y 1 1) 'a))))
  234. ;; this failed in guile 1.8.0
  235. (pass-if "stride every third element"
  236. (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
  237. (lambda (i) (list (* i 3)))
  238. 4)
  239. #1(a d g j)))
  240. (pass-if "shared of shared"
  241. (let* ((a #2((1 2 3) (4 5 6) (7 8 9)))
  242. (s1 (make-shared-array a (lambda (i) (list i 1)) 3))
  243. (s2 (make-shared-array s1 list '(1 2))))
  244. (and (eqv? 5 (array-ref s2 1))
  245. (eqv? 8 (array-ref s2 2))))))
  246. ;;;
  247. ;;; array-contents
  248. ;;;
  249. (define (every-two x) (make-shared-array x (lambda (i) (list (* i 2))) 2))
  250. (with-test-prefix/c&e "array-contents"
  251. (pass-if "simple vector"
  252. (let* ((a (make-array 0 4)))
  253. (eq? a (array-contents a))))
  254. (pass-if "offset vector"
  255. (let* ((a (make-array 0 '(1 4))))
  256. (array-copy! #(1 2 3 4) (array-contents a))
  257. (array-equal? #1@1(1 2 3 4) a)))
  258. (pass-if "offset vector, strict"
  259. (let* ((a (make-array 0 '(1 4))))
  260. (array-copy! #(1 2 3 4) (array-contents a #t))
  261. (array-equal? #1@1(1 2 3 4) a)))
  262. (pass-if "stepped vector"
  263. (let* ((a (make-array 0 4)))
  264. (array-copy! #(99 66) (array-contents (every-two a)))
  265. (array-equal? #(99 0 66 0) a)))
  266. ;; this failed in 2.0.9.
  267. (pass-if "stepped vector, strict"
  268. (let* ((a (make-array 0 4)))
  269. (not (array-contents (every-two a) #t))))
  270. (pass-if "plain rank 2 array"
  271. (let* ((a (make-array 0 2 2)))
  272. (array-copy! #(1 2 3 4) (array-contents a #t))
  273. (array-equal? #2((1 2) (3 4)) a)))
  274. (pass-if "offset rank 2 array"
  275. (let* ((a (make-array 0 '(1 2) '(1 2))))
  276. (array-copy! #(1 2 3 4) (array-contents a #t))
  277. (array-equal? #2@1@1((1 2) (3 4)) a)))
  278. (pass-if "transposed rank 2 array"
  279. (let* ((a (make-array 0 4 4)))
  280. (not (array-contents (transpose-array a 1 0) #t))))
  281. ;; This is a consequence of (array-contents? a #t) => #t.
  282. (pass-if "empty array"
  283. (let ((a (make-typed-array 'f64 2 0 0)))
  284. (f64vector? (array-contents a))))
  285. (pass-if "broadcast vector I"
  286. (let* ((a (make-array 0 4))
  287. (b (make-shared-array a (lambda (i j k) (list k)) 1 1 4)))
  288. (array-copy! #(1 2 3 4) (array-contents b #t))
  289. (array-equal? #(1 2 3 4) a)))
  290. (pass-if "broadcast vector II"
  291. (let* ((a (make-array 0 4))
  292. (b (make-shared-array a (lambda (i j k) (list k)) 2 1 4)))
  293. (not (array-contents b))))
  294. ;; FIXME maybe this should be allowed.
  295. ;; (pass-if "broadcast vector -> empty"
  296. ;; (let* ((a (make-array 0 4))
  297. ;; (b (make-shared-array a (lambda (i j k) (list k)) 0 1 4)))
  298. ;; (if #f #f)))
  299. (pass-if "broadcast 2-rank I"
  300. (let* ((a #2((1 2 3) (4 5 6)))
  301. (b (make-shared-array a (lambda (i j) (list 0 j)) 2 3)))
  302. (not (array-contents b))))
  303. (pass-if "broadcast 2-rank II"
  304. (let* ((a #2((1 2 3) (4 5 6)))
  305. (b (make-shared-array a (lambda (i j) (list i 0)) 2 3)))
  306. (not (array-contents b))))
  307. (pass-if "literal array"
  308. (not (not (array-contents #2((1 2 3) (4 5 6)))))))
  309. ;;;
  310. ;;; shared-array-root
  311. ;;;
  312. (define amap1 (lambda (i) (list (* 2 i))))
  313. (define amap2 (lambda (i j) (list (+ 1 (* 2 i)) (+ 1 (* 2 j)))))
  314. (with-test-prefix/c&e "shared-array-root"
  315. (pass-if "plain vector"
  316. (let* ((a (make-vector 4 0))
  317. (b (make-shared-array a amap1 2)))
  318. (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
  319. (pass-if "plain array rank 2"
  320. (let* ((a (make-array 0 4 4))
  321. (b (make-shared-array a amap2 2 2)))
  322. (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
  323. (pass-if "uniform array rank 2"
  324. (let* ((a (make-typed-array 'c64 0 4 4))
  325. (b (make-shared-array a amap2 2 2)))
  326. (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
  327. (pass-if "bit array rank 2"
  328. (let* ((a (make-typed-array 'b #f 4 4))
  329. (b (make-shared-array a amap2 2 2)))
  330. (eq? (shared-array-root a) (shared-array-root b) (array-contents a)))))
  331. ;;;
  332. ;;; transpose-array
  333. ;;;
  334. ; see strings.test.
  335. (define exception:wrong-type-arg
  336. (cons #t "Wrong type"))
  337. (with-test-prefix/c&e "transpose-array"
  338. (pass-if-exception "non array argument" exception:wrong-type-arg
  339. (transpose-array 99))
  340. (pass-if "rank 0"
  341. (let* ((a #0(99))
  342. (b (transpose-array a)))
  343. (and (array-equal? a b)
  344. (eq? (shared-array-root a) (shared-array-root b)))))
  345. (pass-if "rank 1"
  346. (let* ((a #(1 2 3))
  347. (b (transpose-array a 0)))
  348. (and (array-equal? a b)
  349. (eq? (shared-array-root a) (shared-array-root b)))))
  350. (pass-if "rank 2"
  351. (let* ((a #2((1 2 3) (4 5 6)))
  352. (b (transpose-array a 1 0))
  353. (c (transpose-array a 0 1)))
  354. (and (array-equal? b #2((1 4) (2 5) (3 6)))
  355. (array-equal? c a)
  356. (eq? (shared-array-root a)
  357. (shared-array-root b)
  358. (shared-array-root c)))))
  359. ; rank > 2 is needed to check against the inverted axis index logic.
  360. (pass-if "rank 3"
  361. (let* ((a #3(((0 1 2 3) (4 5 6 7) (8 9 10 11))
  362. ((12 13 14 15) (16 17 18 19) (20 21 22 23))))
  363. (b (transpose-array a 1 2 0)))
  364. (and (array-equal? b #3(((0 4 8) (12 16 20)) ((1 5 9) (13 17 21))
  365. ((2 6 10) (14 18 22)) ((3 7 11) (15 19 23))))
  366. (eq? (shared-array-root a)
  367. (shared-array-root b))))))
  368. ;;;
  369. ;;; array->list
  370. ;;;
  371. (with-test-prefix/c&e "array->list"
  372. (pass-if-equal "uniform vector" '(1 2 3) (array->list #s16(1 2 3)))
  373. (pass-if-equal "vector" '(1 2 3) (array->list #(1 2 3)))
  374. (pass-if-equal "rank 2 array" '((1 2) (3 4) (5 6)) (array->list #2((1 2) (3 4) (5 6))))
  375. (pass-if-equal "empty vector" '() (array->list #()))
  376. (pass-if-equal "http://bugs.gnu.org/12465 - ok"
  377. '(3 4)
  378. (let* ((a #2((1 2) (3 4)))
  379. (b (make-shared-array a (lambda (j) (list 1 j)) 2)))
  380. (array->list b)))
  381. (pass-if-equal "http://bugs.gnu.org/12465 - bad"
  382. '(2 4)
  383. (let* ((a #2((1 2) (3 4)))
  384. (b (make-shared-array a (lambda (i) (list i 1)) 2)))
  385. (array->list b))))
  386. ;;;
  387. ;;; array-fill!
  388. ;;;
  389. (with-test-prefix "array-fill!"
  390. (with-test-prefix "bool"
  391. (let ((a (make-bitvector 1 #t)))
  392. (pass-if "#f" (array-fill! a #f) #t)
  393. (pass-if "#t" (array-fill! a #t) #t)))
  394. (with-test-prefix "char"
  395. (let ((a (make-string 1 #\a)))
  396. (pass-if "x" (array-fill! a #\x) #t)))
  397. (with-test-prefix "byte"
  398. (let ((a (make-s8vector 1 0)))
  399. (pass-if "0" (array-fill! a 0) #t)
  400. (pass-if "127" (array-fill! a 127) #t)
  401. (pass-if "-128" (array-fill! a -128) #t)
  402. (pass-if-exception "128" exception:out-of-range
  403. (array-fill! a 128))
  404. (pass-if-exception "-129" exception:out-of-range
  405. (array-fill! a -129))
  406. (pass-if-exception "symbol" exception:wrong-type-arg
  407. (array-fill! a 'symbol))))
  408. (with-test-prefix "short"
  409. (let ((a (make-s16vector 1 0)))
  410. (pass-if "0" (array-fill! a 0) #t)
  411. (pass-if "123" (array-fill! a 123) #t)
  412. (pass-if "-123" (array-fill! a -123) #t)))
  413. (with-test-prefix "ulong"
  414. (let ((a (make-u32vector 1 1)))
  415. (pass-if "0" (array-fill! a 0) #t)
  416. (pass-if "123" (array-fill! a 123) #t)
  417. (pass-if-exception "-123" exception:out-of-range
  418. (array-fill! a -123) #t)))
  419. (with-test-prefix "long"
  420. (let ((a (make-s32vector 1 -1)))
  421. (pass-if "0" (array-fill! a 0) #t)
  422. (pass-if "123" (array-fill! a 123) #t)
  423. (pass-if "-123" (array-fill! a -123) #t)))
  424. (with-test-prefix "float"
  425. (let ((a (make-f32vector 1 1.0)))
  426. (pass-if "0.0" (array-fill! a 0) #t)
  427. (pass-if "123.0" (array-fill! a 123.0) #t)
  428. (pass-if "-123.0" (array-fill! a -123.0) #t)
  429. (pass-if "0" (array-fill! a 0) #t)
  430. (pass-if "123" (array-fill! a 123) #t)
  431. (pass-if "-123" (array-fill! a -123) #t)
  432. (pass-if "5/8" (array-fill! a 5/8) #t)))
  433. (with-test-prefix "double"
  434. (let ((a (make-f64vector 1 1/3)))
  435. (pass-if "0.0" (array-fill! a 0) #t)
  436. (pass-if "123.0" (array-fill! a 123.0) #t)
  437. (pass-if "-123.0" (array-fill! a -123.0) #t)
  438. (pass-if "0" (array-fill! a 0) #t)
  439. (pass-if "123" (array-fill! a 123) #t)
  440. (pass-if "-123" (array-fill! a -123) #t)
  441. (pass-if "5/8" (array-fill! a 5/8) #t)))
  442. (with-test-prefix "noncompact"
  443. (let* ((a (make-array 0 3 3))
  444. (b (make-shared-array a (lambda (i) (list i i)) 3)))
  445. (array-fill! b 9)
  446. (pass-if
  447. (and (equal? b #(9 9 9))
  448. (equal? a #2((9 0 0) (0 9 0) (0 0 9))))))))
  449. ;;;
  450. ;;; array-in-bounds?
  451. ;;;
  452. (with-test-prefix/c&e "array-in-bounds?"
  453. (pass-if (let ((a (make-array #f '(425 425))))
  454. (eq? #f (array-in-bounds? a 0)))))
  455. ;;;
  456. ;;; array-prototype
  457. ;;;
  458. (with-test-prefix "array-type"
  459. (with-test-prefix/c&e "on make-foo-vector"
  460. (pass-if "bool"
  461. (eq? 'b (array-type (make-bitvector 1))))
  462. (pass-if "char"
  463. (eq? 'a (array-type (make-string 1))))
  464. (pass-if "byte"
  465. (eq? 'u8 (array-type (make-u8vector 1))))
  466. (pass-if "short"
  467. (eq? 's16 (array-type (make-s16vector 1))))
  468. (pass-if "ulong"
  469. (eq? 'u32 (array-type (make-u32vector 1))))
  470. (pass-if "long"
  471. (eq? 's32 (array-type (make-s32vector 1))))
  472. (pass-if "long long"
  473. (eq? 's64 (array-type (make-s64vector 1))))
  474. (pass-if "float"
  475. (eq? 'f32 (array-type (make-f32vector 1))))
  476. (pass-if "double"
  477. (eq? 'f64 (array-type (make-f64vector 1))))
  478. (pass-if "complex"
  479. (eq? 'c64 (array-type (make-c64vector 1))))
  480. (pass-if "scm"
  481. (eq? #t (array-type (make-vector 1)))))
  482. (with-test-prefix "on make-typed-array"
  483. (let ((types '(b a u8 s8 u16 s16 u32 s32 u64 u64 f32 f64 c32 c64)))
  484. (for-each (lambda (type)
  485. (pass-if (symbol->string type)
  486. (eq? type
  487. (array-type (make-typed-array type
  488. *unspecified*
  489. '(5 6))))))
  490. types))))
  491. ;;;
  492. ;;; array-set!
  493. ;;;
  494. (with-test-prefix "array-set!"
  495. (with-test-prefix "bitvector"
  496. ;; in Guile 1.8.0 a bug in bitvector_set() caused a segv in array-set!
  497. ;; on a bitvector like the following
  498. (let ((a (make-bitvector 1)))
  499. (pass-if "one elem set #t"
  500. (begin
  501. (array-set! a #t 0)
  502. (eq? #t (array-ref a 0))))
  503. (pass-if "one elem set #f"
  504. (begin
  505. (array-set! a #f 0)
  506. (eq? #f (array-ref a 0))))))
  507. (with-test-prefix "byte"
  508. (let ((a (make-s8vector 1)))
  509. (pass-if "-128"
  510. (begin (array-set! a -128 0) #t))
  511. (pass-if "0"
  512. (begin (array-set! a 0 0) #t))
  513. (pass-if "127"
  514. (begin (array-set! a 127 0) #t))
  515. (pass-if-exception "-129" exception:out-of-range
  516. (begin (array-set! a -129 0) #t))
  517. (pass-if-exception "128" exception:out-of-range
  518. (begin (array-set! a 128 0) #t))))
  519. (with-test-prefix "short"
  520. (let ((a (make-s16vector 1)))
  521. ;; true if n can be array-set! into a
  522. (define (fits? n)
  523. (false-if-exception (begin (array-set! a n 0) #t)))
  524. (with-test-prefix "store/fetch"
  525. ;; Check array-ref gives back what was put with array-set!.
  526. ;; In Guile 1.6.4 and earlier, array-set! only demanded an inum and
  527. ;; would silently truncate to a short.
  528. (do ((n 1 (1+ (* 2 n)))) ;; n=2^k-1
  529. ((not (fits? n)))
  530. (array-set! a n 0)
  531. (pass-if n
  532. (= n (array-ref a 0))))
  533. (do ((n -1 (* 2 n))) ;; -n=2^k
  534. ((not (fits? n)))
  535. (array-set! a n 0)
  536. (pass-if n
  537. (= n (array-ref a 0))))))))
  538. ;;;
  539. ;;; array-set!
  540. ;;;
  541. (with-test-prefix "array-set!"
  542. (with-test-prefix "one dim"
  543. (let ((a (make-array #f '(3 5))))
  544. (pass-if "start"
  545. (array-set! a 'y 3)
  546. #t)
  547. (pass-if "end"
  548. (array-set! a 'y 5)
  549. #t)
  550. (pass-if-exception "start-1" exception:out-of-range
  551. (array-set! a 'y 2))
  552. (pass-if-exception "end+1" exception:out-of-range
  553. (array-set! a 'y 6))
  554. (pass-if-exception "two indexes" exception:wrong-num-indices
  555. (array-set! a 'y 6 7))))
  556. (with-test-prefix "two dim"
  557. (let ((a (make-array #f '(3 5) '(7 9))))
  558. (pass-if "start"
  559. (array-set! a 'y 3 7)
  560. #t)
  561. (pass-if "end"
  562. (array-set! a 'y 5 9)
  563. #t)
  564. (pass-if-exception "start i-1" exception:out-of-range
  565. (array-set! a 'y 2 7))
  566. (pass-if-exception "end i+1" exception:out-of-range
  567. (array-set! a 'y 6 9))
  568. (pass-if-exception "one index" exception:wrong-num-indices
  569. (array-set! a 'y 4))
  570. (pass-if-exception "three indexes" exception:wrong-num-indices
  571. (array-set! a 'y 4 8 0)))))
  572. ;;;
  573. ;;; uniform-vector
  574. ;;;
  575. (with-test-prefix "typed arrays"
  576. (with-test-prefix "array-ref byte"
  577. (let ((a (make-s8vector 1)))
  578. (pass-if "0"
  579. (begin
  580. (array-set! a 0 0)
  581. (= 0 (array-ref a 0))))
  582. (pass-if "127"
  583. (begin
  584. (array-set! a 127 0)
  585. (= 127 (array-ref a 0))))
  586. (pass-if "-128"
  587. (begin
  588. (array-set! a -128 0)
  589. (= -128 (array-ref a 0))))))
  590. (with-test-prefix "shared with rank 1 equality"
  591. (let ((a #f64(1 2 3 4)))
  592. (pass-if "change offset"
  593. (let ((b (make-shared-array a (lambda (i) (list (+ i 1))) 3)))
  594. (and (eq? (array-type b) (array-type a))
  595. (= 3 (array-length b))
  596. (array-equal? b #f64(2 3 4)))))
  597. (pass-if "change stride"
  598. (let ((c (make-shared-array a (lambda (i) (list (* i 2))) 2)))
  599. (and (eq? (array-type c) (array-type a))
  600. (= 2 (array-length c))
  601. (array-equal? c #f64(1 3))))))))
  602. ;;;
  603. ;;; syntax
  604. ;;;
  605. (with-test-prefix/c&e "syntax"
  606. (pass-if "rank and lower bounds"
  607. ;; uniform u32 array of rank 2 with index ranges 2..3 and 7..8.
  608. (let ((a '#2u32@2@7((1 2) (3 4))))
  609. (and (array? a)
  610. (typed-array? a 'u32)
  611. (= (array-rank a) 2)
  612. (let loop ((bounds '((2 7) (2 8) (3 7) (3 8)))
  613. (result #t))
  614. (if (null? bounds)
  615. result
  616. (and result
  617. (loop (cdr bounds)
  618. (apply array-in-bounds? a (car bounds)))))))))
  619. (pass-if "negative lower bound"
  620. (let ((a '#1@-3(a b)))
  621. (and (array? a)
  622. (= (array-rank a) 1)
  623. (array-in-bounds? a -3) (array-in-bounds? a -2)
  624. (eq? 'a (array-ref a -3))
  625. (eq? 'b (array-ref a -2)))))
  626. (pass-if-exception "negative length" exception:length-non-negative
  627. (with-input-from-string "'#1:-3(#t #t)" read))
  628. (pass-if "bitvector is self-evaluating"
  629. (equal? (compile (bitvector)) (bitvector)))
  630. ; this failed in 2.0.9.
  631. (pass-if "typed arrays that are not uniform arrays"
  632. (let ((a #2b((#t #f) (#f #t)))
  633. (b (make-typed-array 'b #f 2 2)))
  634. (array-set! b #t 0 0)
  635. (array-set! b #t 1 1)
  636. (array-equal? a b))))
  637. ;;;
  638. ;;; equal? with vector and one-dimensional array
  639. ;;;
  640. (with-test-prefix/c&e "equal?"
  641. (pass-if "array and non-array"
  642. (not (equal? #2f64((0 1) (2 3)) 100)))
  643. (pass-if "empty vectors of different types"
  644. (not (equal? #s32() #f64())))
  645. (pass-if "empty arrays of different types"
  646. (not (equal? #2s32() #2f64())))
  647. (pass-if "empty arrays of the same type"
  648. (equal? #s32() #s32()))
  649. (pass-if "identical uniform vectors of the same type"
  650. (equal? #s32(1) #s32(1)))
  651. (pass-if "nonidentical uniform vectors of the same type"
  652. (not (equal? #s32(1) #s32(-1))))
  653. (pass-if "identical uniform vectors of different types"
  654. (not (equal? #s32(1) #s64(1))))
  655. (pass-if "nonidentical uniform vectors of different types"
  656. (not (equal? #s32(1) #s64(-1))))
  657. (pass-if "vector and one-dimensional array"
  658. (equal? (make-shared-array #2((a b c) (d e f) (g h i))
  659. (lambda (i) (list i i))
  660. '(0 2))
  661. #(a e i))))
  662. ;;;
  663. ;;; slices as generalized vectors
  664. ;;;
  665. (define (array-row a i)
  666. (make-shared-array a (lambda (j) (list i j))
  667. (cadr (array-dimensions a))))
  668. (with-test-prefix/c&e "generalized vector slices"
  669. (pass-if (equal? (array-row #2u32((0 1) (2 3)) 1)
  670. #u32(2 3)))
  671. (pass-if (equal? (array-ref (array-row #2u32((0 1) (2 3)) 1) 0)
  672. 2)))
  673. ;;;
  674. ;;; printing arrays
  675. ;;;
  676. (with-test-prefix/c&e "printing arrays"
  677. (pass-if-equal "writing 1D arrays that aren't vectors"
  678. "#1(b c)"
  679. (format #f "~a" (make-shared-array #(a b c)
  680. (lambda (i) (list (+ i 1)))
  681. 2))))