arrays.test 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. (define (array-row a i)
  25. (make-shared-array a (lambda (j) (list i j))
  26. (cadr (array-dimensions a))))
  27. (define (array-col a j)
  28. (make-shared-array a (lambda (i) (list i j))
  29. (car (array-dimensions a))))
  30. (define exception:wrong-num-indices
  31. (cons 'misc-error "^wrong number of indices.*"))
  32. (define exception:length-non-negative
  33. (cons 'read-error ".*array length must be non-negative.*"))
  34. (define exception:wrong-type-arg
  35. (cons #t "Wrong type"))
  36. (define exception:mapping-out-of-range
  37. (cons 'misc-error "^mapping out of range")) ;; per scm_make_shared_array
  38. ;;;
  39. ;;; array?
  40. ;;;
  41. (with-test-prefix "array?"
  42. (let ((bool (make-typed-array 'b #t '(5 6)))
  43. (char (make-typed-array 'a #\a '(5 6)))
  44. (byte (make-typed-array 'u8 0 '(5 6)))
  45. (short (make-typed-array 's16 0 '(5 6)))
  46. (ulong (make-typed-array 'u32 0 '(5 6)))
  47. (long (make-typed-array 's32 0 '(5 6)))
  48. (longlong (make-typed-array 's64 0 '(5 6)))
  49. (float (make-typed-array 'f32 0 '(5 6)))
  50. (double (make-typed-array 'f64 0 '(5 6)))
  51. (complex (make-typed-array 'c64 0 '(5 6)))
  52. (scm (make-typed-array #t 0 '(5 6))))
  53. (with-test-prefix "is bool"
  54. (pass-if (eq? #t (typed-array? bool 'b)))
  55. (pass-if (eq? #f (typed-array? char 'b)))
  56. (pass-if (eq? #f (typed-array? byte 'b)))
  57. (pass-if (eq? #f (typed-array? short 'b)))
  58. (pass-if (eq? #f (typed-array? ulong 'b)))
  59. (pass-if (eq? #f (typed-array? long 'b)))
  60. (pass-if (eq? #f (typed-array? longlong 'b)))
  61. (pass-if (eq? #f (typed-array? float 'b)))
  62. (pass-if (eq? #f (typed-array? double 'b)))
  63. (pass-if (eq? #f (typed-array? complex 'b)))
  64. (pass-if (eq? #f (typed-array? scm 'b))))
  65. (with-test-prefix "is char"
  66. (pass-if (eq? #f (typed-array? bool 'a)))
  67. (pass-if (eq? #t (typed-array? char 'a)))
  68. (pass-if (eq? #f (typed-array? byte 'a)))
  69. (pass-if (eq? #f (typed-array? short 'a)))
  70. (pass-if (eq? #f (typed-array? ulong 'a)))
  71. (pass-if (eq? #f (typed-array? long 'a)))
  72. (pass-if (eq? #f (typed-array? longlong 'a)))
  73. (pass-if (eq? #f (typed-array? float 'a)))
  74. (pass-if (eq? #f (typed-array? double 'a)))
  75. (pass-if (eq? #f (typed-array? complex 'a)))
  76. (pass-if (eq? #f (typed-array? scm 'a))))
  77. (with-test-prefix "is byte"
  78. (pass-if (eq? #f (typed-array? bool 'u8)))
  79. (pass-if (eq? #f (typed-array? char 'u8)))
  80. (pass-if (eq? #t (typed-array? byte 'u8)))
  81. (pass-if (eq? #f (typed-array? short 'u8)))
  82. (pass-if (eq? #f (typed-array? ulong 'u8)))
  83. (pass-if (eq? #f (typed-array? long 'u8)))
  84. (pass-if (eq? #f (typed-array? longlong 'u8)))
  85. (pass-if (eq? #f (typed-array? float 'u8)))
  86. (pass-if (eq? #f (typed-array? double 'u8)))
  87. (pass-if (eq? #f (typed-array? complex 'u8)))
  88. (pass-if (eq? #f (typed-array? scm 'u8))))
  89. (with-test-prefix "is short"
  90. (pass-if (eq? #f (typed-array? bool 's16)))
  91. (pass-if (eq? #f (typed-array? char 's16)))
  92. (pass-if (eq? #f (typed-array? byte 's16)))
  93. (pass-if (eq? #t (typed-array? short 's16)))
  94. (pass-if (eq? #f (typed-array? ulong 's16)))
  95. (pass-if (eq? #f (typed-array? long 's16)))
  96. (pass-if (eq? #f (typed-array? longlong 's16)))
  97. (pass-if (eq? #f (typed-array? float 's16)))
  98. (pass-if (eq? #f (typed-array? double 's16)))
  99. (pass-if (eq? #f (typed-array? complex 's16)))
  100. (pass-if (eq? #f (typed-array? scm 's16))))
  101. (with-test-prefix "is ulong"
  102. (pass-if (eq? #f (typed-array? bool 'u32)))
  103. (pass-if (eq? #f (typed-array? char 'u32)))
  104. (pass-if (eq? #f (typed-array? byte 'u32)))
  105. (pass-if (eq? #f (typed-array? short 'u32)))
  106. (pass-if (eq? #t (typed-array? ulong 'u32)))
  107. (pass-if (eq? #f (typed-array? long 'u32)))
  108. (pass-if (eq? #f (typed-array? longlong 'u32)))
  109. (pass-if (eq? #f (typed-array? float 'u32)))
  110. (pass-if (eq? #f (typed-array? double 'u32)))
  111. (pass-if (eq? #f (typed-array? complex 'u32)))
  112. (pass-if (eq? #f (typed-array? scm 'u32))))
  113. (with-test-prefix "is long"
  114. (pass-if (eq? #f (typed-array? bool 's32)))
  115. (pass-if (eq? #f (typed-array? char 's32)))
  116. (pass-if (eq? #f (typed-array? byte 's32)))
  117. (pass-if (eq? #f (typed-array? short 's32)))
  118. (pass-if (eq? #f (typed-array? ulong 's32)))
  119. (pass-if (eq? #t (typed-array? long 's32)))
  120. (pass-if (eq? #f (typed-array? longlong 's32)))
  121. (pass-if (eq? #f (typed-array? float 's32)))
  122. (pass-if (eq? #f (typed-array? double 's32)))
  123. (pass-if (eq? #f (typed-array? complex 's32)))
  124. (pass-if (eq? #f (typed-array? scm 's32))))
  125. (with-test-prefix "is long long"
  126. (pass-if (eq? #f (typed-array? bool 's64)))
  127. (pass-if (eq? #f (typed-array? char 's64)))
  128. (pass-if (eq? #f (typed-array? byte 's64)))
  129. (pass-if (eq? #f (typed-array? short 's64)))
  130. (pass-if (eq? #f (typed-array? ulong 's64)))
  131. (pass-if (eq? #f (typed-array? long 's64)))
  132. (pass-if (eq? #t (typed-array? longlong 's64)))
  133. (pass-if (eq? #f (typed-array? float 's64)))
  134. (pass-if (eq? #f (typed-array? double 's64)))
  135. (pass-if (eq? #f (typed-array? complex 's64)))
  136. (pass-if (eq? #f (typed-array? scm 's64))))
  137. (with-test-prefix "is float"
  138. (pass-if (eq? #f (typed-array? bool 'f32)))
  139. (pass-if (eq? #f (typed-array? char 'f32)))
  140. (pass-if (eq? #f (typed-array? byte 'f32)))
  141. (pass-if (eq? #f (typed-array? short 'f32)))
  142. (pass-if (eq? #f (typed-array? ulong 'f32)))
  143. (pass-if (eq? #f (typed-array? long 'f32)))
  144. (pass-if (eq? #f (typed-array? longlong 'f32)))
  145. (pass-if (eq? #t (typed-array? float 'f32)))
  146. (pass-if (eq? #f (typed-array? double 'f32)))
  147. (pass-if (eq? #f (typed-array? complex 'f32)))
  148. (pass-if (eq? #f (typed-array? scm 'f32))))
  149. (with-test-prefix "is double"
  150. (pass-if (eq? #f (typed-array? bool 'f64)))
  151. (pass-if (eq? #f (typed-array? char 'f64)))
  152. (pass-if (eq? #f (typed-array? byte 'f64)))
  153. (pass-if (eq? #f (typed-array? short 'f64)))
  154. (pass-if (eq? #f (typed-array? ulong 'f64)))
  155. (pass-if (eq? #f (typed-array? long 'f64)))
  156. (pass-if (eq? #f (typed-array? longlong 'f64)))
  157. (pass-if (eq? #f (typed-array? float 'f64)))
  158. (pass-if (eq? #t (typed-array? double 'f64)))
  159. (pass-if (eq? #f (typed-array? complex 'f64)))
  160. (pass-if (eq? #f (typed-array? scm 'f64))))
  161. (with-test-prefix "is complex"
  162. (pass-if (eq? #f (typed-array? bool 'c64)))
  163. (pass-if (eq? #f (typed-array? char 'c64)))
  164. (pass-if (eq? #f (typed-array? byte 'c64)))
  165. (pass-if (eq? #f (typed-array? short 'c64)))
  166. (pass-if (eq? #f (typed-array? ulong 'c64)))
  167. (pass-if (eq? #f (typed-array? long 'c64)))
  168. (pass-if (eq? #f (typed-array? longlong 'c64)))
  169. (pass-if (eq? #f (typed-array? float 'c64)))
  170. (pass-if (eq? #f (typed-array? double 'c64)))
  171. (pass-if (eq? #t (typed-array? complex 'c64)))
  172. (pass-if (eq? #f (typed-array? scm 'c64))))
  173. (with-test-prefix "is scm"
  174. (pass-if (eq? #f (typed-array? bool #t)))
  175. (pass-if (eq? #f (typed-array? char #t)))
  176. (pass-if (eq? #f (typed-array? byte #t)))
  177. (pass-if (eq? #f (typed-array? short #t)))
  178. (pass-if (eq? #f (typed-array? ulong #t)))
  179. (pass-if (eq? #f (typed-array? long #t)))
  180. (pass-if (eq? #f (typed-array? longlong #t)))
  181. (pass-if (eq? #f (typed-array? float #t)))
  182. (pass-if (eq? #f (typed-array? double #t)))
  183. (pass-if (eq? #f (typed-array? complex #t)))
  184. (pass-if (eq? #t (typed-array? scm #t))))
  185. (with-test-prefix "typed-array? returns #f"
  186. (pass-if (eq? #f (typed-array? '(1 2 3) 'c64)))
  187. (pass-if (eq? #f (typed-array? '(1 2 3) #t)))
  188. (pass-if (eq? #f (typed-array? 99 'c64)))
  189. (pass-if (eq? #f (typed-array? 99 #t))))))
  190. ;;;
  191. ;;; array-equal?
  192. ;;;
  193. (with-test-prefix/c&e "array-equal?"
  194. (pass-if "#s16(...)"
  195. (array-equal? #s16(1 2 3) #s16(1 2 3)))
  196. (pass-if "#0f64(...)"
  197. (array-equal? #0f64(99) (make-typed-array 'f64 99)))
  198. (pass-if "#0(...)"
  199. (array-equal? #0(99) (make-array 99))))
  200. ;;;
  201. ;;; make-shared-array
  202. ;;;
  203. (with-test-prefix/c&e "make-shared-array"
  204. ;; this failed in guile 1.8.0
  205. (pass-if "vector unchanged"
  206. (let* ((a (make-array #f '(0 7)))
  207. (s (make-shared-array a list '(0 7))))
  208. (array-equal? a s)))
  209. (pass-if-exception "vector, high too big" exception:mapping-out-of-range
  210. (let* ((a (make-array #f '(0 7))))
  211. (make-shared-array a list '(0 8))))
  212. (pass-if-exception "vector, low too big" exception:out-of-range
  213. (let* ((a (make-array #f '(0 7))))
  214. (make-shared-array a list '(-1 7))))
  215. (pass-if "truncate columns"
  216. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
  217. #2((a b) (d e) (g h))))
  218. (pass-if "pick one column"
  219. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  220. (lambda (i) (list i 2))
  221. '(0 2))
  222. #(c f i)))
  223. (pass-if "diagonal"
  224. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  225. (lambda (i) (list i i))
  226. '(0 2))
  227. #(a e i)))
  228. ;; this failed in guile 1.8.0
  229. (pass-if "2 dims from 1 dim"
  230. (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
  231. (lambda (i j) (list (+ (* i 3) j)))
  232. 4 3)
  233. #2((a b c) (d e f) (g h i) (j k l))))
  234. (pass-if "reverse columns"
  235. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  236. (lambda (i j) (list i (- 2 j)))
  237. 3 3)
  238. #2((c b a) (f e d) (i h g))))
  239. (pass-if "fixed offset, 0 based becomes 1 based"
  240. (let* ((x #2((a b c) (d e f) (g h i)))
  241. (y (make-shared-array x
  242. (lambda (i j) (list (1- i) (1- j)))
  243. '(1 3) '(1 3))))
  244. (and (eq? (array-ref x 0 0) 'a)
  245. (eq? (array-ref y 1 1) 'a))))
  246. ;; this failed in guile 1.8.0
  247. (pass-if "stride every third element"
  248. (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
  249. (lambda (i) (list (* i 3)))
  250. 4)
  251. #1(a d g j)))
  252. (pass-if "shared of shared"
  253. (let* ((a #2((1 2 3) (4 5 6) (7 8 9)))
  254. (s1 (make-shared-array a (lambda (i) (list i 1)) 3))
  255. (s2 (make-shared-array s1 list '(1 2))))
  256. (and (eqv? 5 (array-ref s2 1))
  257. (eqv? 8 (array-ref s2 2))))))
  258. ;;;
  259. ;;; array-slice
  260. ;;;
  261. (with-test-prefix/c&e "array-slice"
  262. (pass-if "vector I"
  263. (let ((v (vector 1 2 3)))
  264. (array-fill! (array-slice v 1) 'a)
  265. (array-equal? v #(1 a 3))))
  266. (pass-if "vector II"
  267. (let ((v (vector 1 2 3)))
  268. (array-copy! #(a b c) (array-slice v))
  269. (array-equal? v #(a b c))))
  270. (pass-if "array I"
  271. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  272. (array-fill! (array-slice a 1 1) 'a)
  273. (array-equal? a #2((1 2 3) (4 a 6)))))
  274. (pass-if "array II"
  275. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  276. (array-copy! #(a b c) (array-slice a 1))
  277. (array-equal? a #2((1 2 3) (a b c)))))
  278. (pass-if "array III"
  279. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  280. (array-copy! #2((a b c) (x y z)) (array-slice a))
  281. (array-equal? a #2((a b c) (x y z)))))
  282. (pass-if "rank 0 array"
  283. (let ((a (make-array 77)))
  284. (array-fill! (array-slice a) 'a)
  285. (array-equal? a #0(a)))))
  286. ;;;
  287. ;;; array-cell-ref
  288. ;;;
  289. (with-test-prefix/c&e "array-cell-ref"
  290. (pass-if "vector I"
  291. (let ((v (vector 1 2 3)))
  292. (equal? 2 (array-cell-ref v 1))))
  293. (pass-if "vector II"
  294. (let ((v (vector 1 2 3)))
  295. (array-copy! #(a b c) (array-cell-ref v))
  296. (array-equal? v #(a b c))))
  297. (pass-if "array I"
  298. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  299. (equal? 5 (array-cell-ref a 1 1))))
  300. (pass-if "array II"
  301. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  302. (array-copy! #(a b c) (array-cell-ref a 1))
  303. (array-equal? a #2((1 2 3) (a b c)))))
  304. (pass-if "array III"
  305. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  306. (array-copy! #2((a b c) (x y z)) (array-cell-ref a))
  307. (array-equal? a #2((a b c) (x y z)))))
  308. (pass-if "rank 0 array"
  309. (let ((a (make-array 77)))
  310. (equal? (array-cell-ref a) 77))))
  311. ;;;
  312. ;;; array-cell-set!
  313. ;;;
  314. (with-test-prefix/c&e "array-cell-set!"
  315. (pass-if "vector I"
  316. (let ((v (vector 1 2 3)))
  317. (and (eq? v (array-cell-set! v 'x 1))
  318. (array-equal? v #(1 x 3)))))
  319. (pass-if "vector II"
  320. (let ((v (vector 1 2 3)))
  321. (and (eq? v (array-cell-set! (array-cell-ref v) #(a b c)))
  322. (array-equal? v #(a b c)))))
  323. (pass-if "array I"
  324. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  325. (and (eq? a (array-cell-set! a 'x 1 1))
  326. (array-equal? a #2((1 2 3) (4 x 6))))))
  327. (pass-if "array II"
  328. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  329. (and (eq? a (array-cell-set! a #(a b c) 1))
  330. (array-equal? a #2((1 2 3) (a b c))))))
  331. (pass-if "array III"
  332. (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
  333. (and (eq? a (array-cell-set! a #2((a b c) (x y z))))
  334. (array-equal? a #2((a b c) (x y z))))))
  335. (pass-if "rank 0 array"
  336. (let ((a (make-array 77)))
  337. (and (eq? a (array-cell-set! a 99))
  338. (array-equal? a #0(99))))))
  339. ;;;
  340. ;;; array-contents
  341. ;;;
  342. (define (every-two x) (make-shared-array x (lambda (i) (list (* i 2))) 2))
  343. (with-test-prefix/c&e "array-contents"
  344. (pass-if "0-rank array"
  345. (let ((a (make-vector 1 77)))
  346. (and
  347. (eq? a (array-contents (make-shared-array a (const '(0)))))
  348. (eq? a (array-contents (make-shared-array a (const '(0))) #t)))))
  349. (pass-if "simple vector"
  350. (let* ((a (make-array 0 4)))
  351. (eq? a (array-contents a))))
  352. (pass-if "offset vector"
  353. (let* ((a (make-array 0 '(1 4))))
  354. (array-copy! #(1 2 3 4) (array-contents a))
  355. (array-equal? #1@1(1 2 3 4) a)))
  356. (pass-if "offset vector, strict"
  357. (let* ((a (make-array 0 '(1 4))))
  358. (array-copy! #(1 2 3 4) (array-contents a #t))
  359. (array-equal? #1@1(1 2 3 4) a)))
  360. (pass-if "stepped vector"
  361. (let* ((a (make-array 0 4)))
  362. (array-copy! #(99 66) (array-contents (every-two a)))
  363. (array-equal? #(99 0 66 0) a)))
  364. ;; this failed in 2.0.9.
  365. (pass-if "stepped vector, strict"
  366. (let* ((a (make-array 0 4)))
  367. (not (array-contents (every-two a) #t))))
  368. (pass-if "plain rank 2 array"
  369. (let* ((a (make-array 0 2 2)))
  370. (array-copy! #(1 2 3 4) (array-contents a #t))
  371. (array-equal? #2((1 2) (3 4)) a)))
  372. (pass-if "offset rank 2 array"
  373. (let* ((a (make-array 0 '(1 2) '(1 2))))
  374. (array-copy! #(1 2 3 4) (array-contents a #t))
  375. (array-equal? #2@1@1((1 2) (3 4)) a)))
  376. (pass-if "transposed rank 2 array"
  377. (let* ((a (make-array 0 4 4)))
  378. (not (array-contents (transpose-array a 1 0) #t))))
  379. ;; This is a consequence of (array-contents? a #t) => #t.
  380. (pass-if "empty array"
  381. (let ((a (make-typed-array 'f64 2 0 0)))
  382. (f64vector? (array-contents a))))
  383. (pass-if "broadcast vector I"
  384. (let* ((a (make-array 0 4))
  385. (b (make-shared-array a (lambda (i j k) (list k)) 1 1 4)))
  386. (array-copy! #(1 2 3 4) (array-contents b #t))
  387. (array-equal? #(1 2 3 4) a)))
  388. (pass-if "broadcast vector II"
  389. (let* ((a (make-array 0 4))
  390. (b (make-shared-array a (lambda (i j k) (list k)) 2 1 4)))
  391. (not (array-contents b))))
  392. ;; FIXME maybe this should be allowed.
  393. ;; (pass-if "broadcast vector -> empty"
  394. ;; (let* ((a (make-array 0 4))
  395. ;; (b (make-shared-array a (lambda (i j k) (list k)) 0 1 4)))
  396. ;; (if #f #f)))
  397. (pass-if "broadcast 2-rank I"
  398. (let* ((a #2((1 2 3) (4 5 6)))
  399. (b (make-shared-array a (lambda (i j) (list 0 j)) 2 3)))
  400. (not (array-contents b))))
  401. (pass-if "broadcast 2-rank II"
  402. (let* ((a #2((1 2 3) (4 5 6)))
  403. (b (make-shared-array a (lambda (i j) (list i 0)) 2 3)))
  404. (not (array-contents b))))
  405. (pass-if "literal array"
  406. (not (not (array-contents #2((1 2 3) (4 5 6)))))))
  407. ;;;
  408. ;;; shared-array-root
  409. ;;;
  410. (define amap1 (lambda (i) (list (* 2 i))))
  411. (define amap2 (lambda (i j) (list (+ 1 (* 2 i)) (+ 1 (* 2 j)))))
  412. (with-test-prefix/c&e "shared-array-root"
  413. (pass-if "plain vector"
  414. (let* ((a (make-vector 4 0))
  415. (b (make-shared-array a amap1 2)))
  416. (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
  417. (pass-if "plain array rank 2"
  418. (let* ((a (make-array 0 4 4))
  419. (b (make-shared-array a amap2 2 2)))
  420. (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
  421. (pass-if "uniform array rank 2"
  422. (let* ((a (make-typed-array 'c64 0 4 4))
  423. (b (make-shared-array a amap2 2 2)))
  424. (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
  425. (pass-if "bit array rank 2"
  426. (let* ((a (make-typed-array 'b #f 4 4))
  427. (b (make-shared-array a amap2 2 2)))
  428. (eq? (shared-array-root a) (shared-array-root b) (array-contents a)))))
  429. ;;;
  430. ;;; shared-array-offset
  431. ;;;
  432. (with-test-prefix/c&e "shared-array-offset"
  433. (pass-if "plain vector"
  434. (zero? (shared-array-offset (make-vector 4 0))))
  435. (pass-if "plain array rank 2"
  436. (zero? (shared-array-offset (make-array 0 4 4))))
  437. (pass-if "row of rank-2 array, I"
  438. (= 0 (shared-array-offset (array-row (make-array 0 5 3) 0))))
  439. (pass-if "row of rank-2 array, II"
  440. (= 4 (shared-array-offset (array-row (make-array 0 6 4) 1))))
  441. (pass-if "col of rank-2 array, I"
  442. (= 0 (shared-array-offset (array-col (make-array 0 5 3) 0))))
  443. (pass-if "col of rank-2 array, II"
  444. (= 1 (shared-array-offset (array-col (make-array 0 6 4) 1)))))
  445. ;;;
  446. ;;; shared-array-increments
  447. ;;;
  448. (with-test-prefix "shared-array-increments"
  449. (pass-if "plain vector"
  450. (equal? '(1) (shared-array-increments (make-vector 4 0))))
  451. (pass-if "plain array rank 2"
  452. (equal? '(4 1) (shared-array-increments (make-array 0 3 4))))
  453. (pass-if "plain array rank 3"
  454. (equal? '(20 5 1) (shared-array-increments (make-array 0 3 4 5))))
  455. (pass-if "row of rank-2 array"
  456. (equal? '(1) (shared-array-increments (array-row (make-array 0 5 3) 0))))
  457. (pass-if "col of rank-2 array"
  458. (equal? '(3) (shared-array-increments (array-col (make-array 0 5 3) 0)))))
  459. ;;;
  460. ;;; transpose-array
  461. ;;;
  462. ; see strings.test.
  463. (with-test-prefix/c&e "transpose-array"
  464. (pass-if-exception "non array argument" exception:wrong-type-arg
  465. (transpose-array 99))
  466. (pass-if "rank 0"
  467. (let* ((a #0(99))
  468. (b (transpose-array a)))
  469. (and (array-equal? a b)
  470. (eq? (shared-array-root a) (shared-array-root b)))))
  471. (pass-if "rank 1"
  472. (let* ((a #(1 2 3))
  473. (b (transpose-array a 0)))
  474. (and (array-equal? a b)
  475. (eq? (shared-array-root a) (shared-array-root b)))))
  476. (pass-if "rank 2"
  477. (let* ((a #2((1 2 3) (4 5 6)))
  478. (b (transpose-array a 1 0))
  479. (c (transpose-array a 0 1)))
  480. (and (array-equal? b #2((1 4) (2 5) (3 6)))
  481. (array-equal? c a)
  482. (eq? (shared-array-root a)
  483. (shared-array-root b)
  484. (shared-array-root c)))))
  485. ; rank > 2 is needed to check against the inverted axis index logic.
  486. (pass-if "rank 3"
  487. (let* ((a #3(((0 1 2 3) (4 5 6 7) (8 9 10 11))
  488. ((12 13 14 15) (16 17 18 19) (20 21 22 23))))
  489. (b (transpose-array a 1 2 0)))
  490. (and (array-equal? b #3(((0 4 8) (12 16 20)) ((1 5 9) (13 17 21))
  491. ((2 6 10) (14 18 22)) ((3 7 11) (15 19 23))))
  492. (eq? (shared-array-root a)
  493. (shared-array-root b))))))
  494. ;;;
  495. ;;; array->list
  496. ;;;
  497. (with-test-prefix/c&e "array->list"
  498. (pass-if-equal "uniform vector" '(1 2 3) (array->list #s16(1 2 3)))
  499. (pass-if-equal "vector" '(1 2 3) (array->list #(1 2 3)))
  500. (pass-if-equal "rank 2 array" '((1 2) (3 4) (5 6)) (array->list #2((1 2) (3 4) (5 6))))
  501. (pass-if-equal "empty vector" '() (array->list #()))
  502. (pass-if-equal "http://bugs.gnu.org/12465 - ok"
  503. '(3 4)
  504. (let* ((a #2((1 2) (3 4)))
  505. (b (make-shared-array a (lambda (j) (list 1 j)) 2)))
  506. (array->list b)))
  507. (pass-if-equal "http://bugs.gnu.org/12465 - bad"
  508. '(2 4)
  509. (let* ((a #2((1 2) (3 4)))
  510. (b (make-shared-array a (lambda (i) (list i 1)) 2)))
  511. (array->list b))))
  512. ;;;
  513. ;;; array-fill!
  514. ;;;
  515. (with-test-prefix "array-fill!"
  516. (with-test-prefix "bool"
  517. (let ((a (make-bitvector 1 #t)))
  518. (pass-if "#f" (array-fill! a #f) #t)
  519. (pass-if "#t" (array-fill! a #t) #t)))
  520. (with-test-prefix "char"
  521. (let ((a (make-string 1 #\a)))
  522. (pass-if "x" (array-fill! a #\x) #t)))
  523. (with-test-prefix "byte"
  524. (let ((a (make-s8vector 1 0)))
  525. (pass-if "0" (array-fill! a 0) #t)
  526. (pass-if "127" (array-fill! a 127) #t)
  527. (pass-if "-128" (array-fill! a -128) #t)
  528. (pass-if-exception "128" exception:out-of-range
  529. (array-fill! a 128))
  530. (pass-if-exception "-129" exception:out-of-range
  531. (array-fill! a -129))
  532. (pass-if-exception "symbol" exception:wrong-type-arg
  533. (array-fill! a 'symbol))))
  534. (with-test-prefix "short"
  535. (let ((a (make-s16vector 1 0)))
  536. (pass-if "0" (array-fill! a 0) #t)
  537. (pass-if "123" (array-fill! a 123) #t)
  538. (pass-if "-123" (array-fill! a -123) #t)))
  539. (with-test-prefix "ulong"
  540. (let ((a (make-u32vector 1 1)))
  541. (pass-if "0" (array-fill! a 0) #t)
  542. (pass-if "123" (array-fill! a 123) #t)
  543. (pass-if-exception "-123" exception:out-of-range
  544. (array-fill! a -123) #t)))
  545. (with-test-prefix "long"
  546. (let ((a (make-s32vector 1 -1)))
  547. (pass-if "0" (array-fill! a 0) #t)
  548. (pass-if "123" (array-fill! a 123) #t)
  549. (pass-if "-123" (array-fill! a -123) #t)))
  550. (with-test-prefix "float"
  551. (let ((a (make-f32vector 1 1.0)))
  552. (pass-if "0.0" (array-fill! a 0) #t)
  553. (pass-if "123.0" (array-fill! a 123.0) #t)
  554. (pass-if "-123.0" (array-fill! a -123.0) #t)
  555. (pass-if "0" (array-fill! a 0) #t)
  556. (pass-if "123" (array-fill! a 123) #t)
  557. (pass-if "-123" (array-fill! a -123) #t)
  558. (pass-if "5/8" (array-fill! a 5/8) #t)))
  559. (with-test-prefix "double"
  560. (let ((a (make-f64vector 1 1/3)))
  561. (pass-if "0.0" (array-fill! a 0) #t)
  562. (pass-if "123.0" (array-fill! a 123.0) #t)
  563. (pass-if "-123.0" (array-fill! a -123.0) #t)
  564. (pass-if "0" (array-fill! a 0) #t)
  565. (pass-if "123" (array-fill! a 123) #t)
  566. (pass-if "-123" (array-fill! a -123) #t)
  567. (pass-if "5/8" (array-fill! a 5/8) #t)))
  568. (with-test-prefix "noncompact"
  569. (let* ((a (make-array 0 3 3))
  570. (b (make-shared-array a (lambda (i) (list i i)) 3)))
  571. (array-fill! b 9)
  572. (pass-if
  573. (and (equal? b #(9 9 9))
  574. (equal? a #2((9 0 0) (0 9 0) (0 0 9))))))))
  575. ;;;
  576. ;;; array-in-bounds?
  577. ;;;
  578. (with-test-prefix "array-in-bounds?"
  579. (pass-if (let ((a (make-array #f '(425 425))))
  580. (eq? #f (array-in-bounds? a 0)))))
  581. ;;;
  582. ;;; array-prototype
  583. ;;;
  584. (with-test-prefix "array-type"
  585. (with-test-prefix "on make-foo-vector"
  586. (pass-if "bool"
  587. (eq? 'b (array-type (make-bitvector 1))))
  588. (pass-if "char"
  589. (eq? 'a (array-type (make-string 1))))
  590. (pass-if "byte"
  591. (eq? 'u8 (array-type (make-u8vector 1))))
  592. (pass-if "short"
  593. (eq? 's16 (array-type (make-s16vector 1))))
  594. (pass-if "ulong"
  595. (eq? 'u32 (array-type (make-u32vector 1))))
  596. (pass-if "long"
  597. (eq? 's32 (array-type (make-s32vector 1))))
  598. (pass-if "long long"
  599. (eq? 's64 (array-type (make-s64vector 1))))
  600. (pass-if "float"
  601. (eq? 'f32 (array-type (make-f32vector 1))))
  602. (pass-if "double"
  603. (eq? 'f64 (array-type (make-f64vector 1))))
  604. (pass-if "complex"
  605. (eq? 'c64 (array-type (make-c64vector 1))))
  606. (pass-if "scm"
  607. (eq? #t (array-type (make-vector 1)))))
  608. (with-test-prefix "on make-typed-array"
  609. (let ((types '(b a u8 s8 u16 s16 u32 s32 u64 u64 f32 f64 c32 c64)))
  610. (for-each (lambda (type)
  611. (pass-if (symbol->string type)
  612. (eq? type
  613. (array-type (make-typed-array type
  614. *unspecified*
  615. '(5 6))))))
  616. types))))
  617. ;;;
  618. ;;; array-set!
  619. ;;;
  620. (with-test-prefix "array-set!"
  621. (with-test-prefix "bitvector"
  622. ;; in Guile 1.8.0 a bug in bitvector_set() caused a segv in array-set!
  623. ;; on a bitvector like the following
  624. (let ((a (make-bitvector 1)))
  625. (pass-if "one elem set #t"
  626. (begin
  627. (array-set! a #t 0)
  628. (eq? #t (array-ref a 0))))
  629. (pass-if "one elem set #f"
  630. (begin
  631. (array-set! a #f 0)
  632. (eq? #f (array-ref a 0))))))
  633. (with-test-prefix "byte"
  634. (let ((a (make-s8vector 1)))
  635. (pass-if "-128"
  636. (begin (array-set! a -128 0) #t))
  637. (pass-if "0"
  638. (begin (array-set! a 0 0) #t))
  639. (pass-if "127"
  640. (begin (array-set! a 127 0) #t))
  641. (pass-if-exception "-129" exception:out-of-range
  642. (begin (array-set! a -129 0) #t))
  643. (pass-if-exception "128" exception:out-of-range
  644. (begin (array-set! a 128 0) #t))))
  645. (with-test-prefix "short"
  646. (let ((a (make-s16vector 1)))
  647. ;; true if n can be array-set! into a
  648. (define (fits? n)
  649. (false-if-exception (begin (array-set! a n 0) #t)))
  650. (with-test-prefix "store/fetch"
  651. ;; Check array-ref gives back what was put with array-set!.
  652. ;; In Guile 1.6.4 and earlier, array-set! only demanded an inum and
  653. ;; would silently truncate to a short.
  654. (do ((n 1 (1+ (* 2 n)))) ;; n=2^k-1
  655. ((not (fits? n)))
  656. (array-set! a n 0)
  657. (pass-if n
  658. (= n (array-ref a 0))))
  659. (do ((n -1 (* 2 n))) ;; -n=2^k
  660. ((not (fits? n)))
  661. (array-set! a n 0)
  662. (pass-if n
  663. (= n (array-ref a 0))))))))
  664. ;;;
  665. ;;; array-set!
  666. ;;;
  667. (with-test-prefix "array-set!"
  668. (with-test-prefix "one dim"
  669. (let ((a (make-array #f '(3 5))))
  670. (pass-if "start"
  671. (array-set! a 'y 3)
  672. #t)
  673. (pass-if "end"
  674. (array-set! a 'y 5)
  675. #t)
  676. (pass-if-exception "start-1" exception:out-of-range
  677. (array-set! a 'y 2))
  678. (pass-if-exception "end+1" exception:out-of-range
  679. (array-set! a 'y 6))
  680. (pass-if-exception "two indexes" exception:wrong-num-indices
  681. (array-set! a 'y 6 7))))
  682. (with-test-prefix "two dim"
  683. (let ((a (make-array #f '(3 5) '(7 9))))
  684. (pass-if "start"
  685. (array-set! a 'y 3 7)
  686. #t)
  687. (pass-if "end"
  688. (array-set! a 'y 5 9)
  689. #t)
  690. (pass-if-exception "start i-1" exception:out-of-range
  691. (array-set! a 'y 2 7))
  692. (pass-if-exception "end i+1" exception:out-of-range
  693. (array-set! a 'y 6 9))
  694. (pass-if-exception "one index" exception:wrong-num-indices
  695. (array-set! a 'y 4))
  696. (pass-if-exception "three indexes" exception:wrong-num-indices
  697. (array-set! a 'y 4 8 0)))))
  698. ;;;
  699. ;;; uniform-vector
  700. ;;;
  701. (with-test-prefix "typed arrays"
  702. (with-test-prefix "array-ref byte"
  703. (let ((a (make-s8vector 1)))
  704. (pass-if "0"
  705. (begin
  706. (array-set! a 0 0)
  707. (= 0 (array-ref a 0))))
  708. (pass-if "127"
  709. (begin
  710. (array-set! a 127 0)
  711. (= 127 (array-ref a 0))))
  712. (pass-if "-128"
  713. (begin
  714. (array-set! a -128 0)
  715. (= -128 (array-ref a 0))))))
  716. (with-test-prefix "shared with rank 1 equality"
  717. (let ((a #f64(1 2 3 4)))
  718. (pass-if "change offset"
  719. (let ((b (make-shared-array a (lambda (i) (list (+ i 1))) 3)))
  720. (and (eq? (array-type b) (array-type a))
  721. (= 3 (array-length b))
  722. (array-equal? b #f64(2 3 4)))))
  723. (pass-if "change stride"
  724. (let ((c (make-shared-array a (lambda (i) (list (* i 2))) 2)))
  725. (and (eq? (array-type c) (array-type a))
  726. (= 2 (array-length c))
  727. (array-equal? c #f64(1 3))))))))
  728. ;;;
  729. ;;; syntax
  730. ;;;
  731. (with-test-prefix/c&e "syntax"
  732. (pass-if "rank and lower bounds"
  733. ;; uniform u32 array of rank 2 with index ranges 2..3 and 7..8.
  734. (let ((a '#2u32@2@7((1 2) (3 4))))
  735. (and (array? a)
  736. (typed-array? a 'u32)
  737. (= (array-rank a) 2)
  738. (let loop ((bounds '((2 7) (2 8) (3 7) (3 8)))
  739. (result #t))
  740. (if (null? bounds)
  741. result
  742. (and result
  743. (loop (cdr bounds)
  744. (apply array-in-bounds? a (car bounds)))))))))
  745. (pass-if "negative lower bound"
  746. (let ((a '#1@-3(a b)))
  747. (and (array? a)
  748. (= (array-rank a) 1)
  749. (array-in-bounds? a -3) (array-in-bounds? a -2)
  750. (eq? 'a (array-ref a -3))
  751. (eq? 'b (array-ref a -2)))))
  752. (pass-if-exception "negative length" exception:length-non-negative
  753. (with-input-from-string "'#1:-3(#t #t)" read))
  754. (pass-if "bitvector is self-evaluating"
  755. (equal? (compile (bitvector)) (bitvector)))
  756. ; this failed in 2.0.9.
  757. (pass-if "typed arrays that are not uniform arrays"
  758. (let ((a #2b((#t #f) (#f #t)))
  759. (b (make-typed-array 'b #f 2 2)))
  760. (array-set! b #t 0 0)
  761. (array-set! b #t 1 1)
  762. (array-equal? a b))))
  763. ;;;
  764. ;;; equal? with vector and one-dimensional array
  765. ;;;
  766. (with-test-prefix/c&e "equal?"
  767. (pass-if "array and non-array"
  768. (not (equal? #2f64((0 1) (2 3)) 100)))
  769. (pass-if "empty vectors of different types"
  770. (not (equal? #s32() #f64())))
  771. (pass-if "empty arrays of different types"
  772. (not (equal? #2s32() #2f64())))
  773. (pass-if "empty arrays of the same type"
  774. (equal? #s32() #s32()))
  775. (pass-if "identical uniform vectors of the same type"
  776. (equal? #s32(1) #s32(1)))
  777. (pass-if "nonidentical uniform vectors of the same type"
  778. (not (equal? #s32(1) #s32(-1))))
  779. (pass-if "identical uniform vectors of different types"
  780. (not (equal? #s32(1) #s64(1))))
  781. (pass-if "nonidentical uniform vectors of different types"
  782. (not (equal? #s32(1) #s64(-1))))
  783. (pass-if "vector and one-dimensional array"
  784. (equal? (make-shared-array #2((a b c) (d e f) (g h i))
  785. (lambda (i) (list i i))
  786. '(0 2))
  787. #(a e i))))
  788. ;;;
  789. ;;; slices as generalized vectors
  790. ;;;
  791. (with-test-prefix/c&e "generalized vector slices"
  792. (pass-if (equal? (array-row #2u32((0 1) (2 3)) 1)
  793. #u32(2 3)))
  794. (pass-if (equal? (array-ref (array-row #2u32((0 1) (2 3)) 1) 0)
  795. 2)))
  796. ;;;
  797. ;;; printing arrays
  798. ;;;
  799. (with-test-prefix/c&e "printing arrays"
  800. (pass-if-equal "writing 1D arrays that aren't vectors"
  801. "#1(b c)"
  802. (format #f "~a" (make-shared-array #(a b c)
  803. (lambda (i) (list (+ i 1)))
  804. 2)))
  805. (pass-if-equal "0-array"
  806. "#0(9)"
  807. (format #f "~a" (make-array 9)))
  808. (pass-if-equal "2-array"
  809. "#2f64((0.0 1.0) (2.0 3.0))"
  810. (format #f "~a" #2f64((0 1) (2 3))))
  811. (pass-if-equal "empty 3-array"
  812. "#3()"
  813. (format #f "~a" (make-array 1 0 0 0)))
  814. (pass-if-equal "empty 3-array with last nonempty dim."
  815. "#3:0:0:1()"
  816. (format #f "~a" (make-array 1 0 0 1)))
  817. (pass-if-equal "empty typed 3-array with last nonempty dim."
  818. "#3f64:0:0:1()"
  819. (format #f "~a" (make-typed-array 'f64 1 0 0 1)))
  820. (pass-if-equal "empty 3-array with middle nonempty dim."
  821. "#3:0:1:0()"
  822. (format #f "~a" (make-array 1 0 1 0)))
  823. (pass-if-equal "empty 3-array with first nonempty dim."
  824. "#3(())"
  825. (format #f "~a" (make-array 1 1 0 0)))
  826. (pass-if-equal "3-array with non-zero lower bounds"
  827. "#3@1@0@1(((1 1 1) (1 1 1)) ((1 1 1) (1 1 1)))"
  828. (format #f "~a" (make-array 1 '(1 2) '(0 1) '(1 3))))
  829. (pass-if-equal "3-array with non-zero-lower bounds and last nonempty dim."
  830. "#3@0:0@0:0@1:3()"
  831. (format #f "~a" (make-array 1 0 0 '(1 3))))
  832. (pass-if-equal "3-array with non-zero-lower bounds and middle nonempty dim."
  833. "#3@0:0@1:3@0:0()"
  834. (format #f "~a" (make-array 1 0 '(1 3) 0)))
  835. (pass-if-equal "3-array with non-zero-lower bounds and first nonempty dim."
  836. "#3@1@0@0(() () ())"
  837. (format #f "~a" (make-array 1 '(1 3) 0 0)))
  838. (pass-if-equal "3-array with singleton dim case I"
  839. "#3@1@1@-1(((1 1 1)))"
  840. (format #f "~a" (make-array 1 '(1 1) '(1 1) '(-1 1))))
  841. (pass-if-equal "3-array with singleton dim case II"
  842. "#3@-1@1@1(((1) (1) (1)))"
  843. (format #f "~a" (make-array 1 '(-1 -1) '(1 3) '(1 1))))
  844. (pass-if-equal "3-array with singleton dim case III"
  845. "#3@1@-1@1(((1)) ((1)) ((1)))"
  846. (format #f "~a" (make-array 1 '(1 3) '(-1 -1) '(1 1)))))
  847. ;;;
  848. ;;; reading arrays
  849. ;;;
  850. (with-test-prefix/c&e "reading arrays"
  851. (pass-if "empty 3-array with first nonempty dim I"
  852. (array-equal? (make-array 1 1 0 0)
  853. (call-with-input-string "#3(())" read)))
  854. (pass-if "empty 3-array with first nonempty dim II"
  855. (array-equal? (make-array 1 1 0 0)
  856. #3(())))
  857. (pass-if "empty 3-array with middle nonempty dim I"
  858. (array-equal? (make-array 1 0 1 0)
  859. (call-with-input-string "#3:0:1:0()" read)))
  860. (pass-if "empty 3-array with middle nonempty dim II"
  861. (array-equal? (make-array 1 0 1 0)
  862. #3:0:1:0()))
  863. (pass-if "empty typed 3-array with middle nonempty dim I"
  864. (array-equal? (make-typed-array 'f64 1 0 1 0)
  865. (call-with-input-string "#3f64:0:1:0()" read)))
  866. (pass-if "empty typed 3-array with middle nonempty dim II"
  867. (array-equal? (make-typed-array 'f64 1 0 1 0)
  868. #3f64:0:1:0()))
  869. (pass-if "array with specified size I"
  870. (array-equal? #f64(1 2 3)
  871. (call-with-input-string "#f64:3(1 2 3)" read)))
  872. (pass-if "array with specified size II"
  873. (array-equal? #f64(1 2 3)
  874. #f64:3(1 2 3))))