arrays.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. ;;;; unif.test --- tests guile's uniform arrays -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright 2004, 2006, 2009, 2010 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 test-arrays)
  19. #:use-module ((system base compile) #:select (compile))
  20. #:use-module (test-suite lib)
  21. #:use-module (srfi srfi-4)
  22. #:use-module (srfi srfi-4 gnu))
  23. ;;;
  24. ;;; array?
  25. ;;;
  26. (define exception:wrong-num-indices
  27. (cons 'misc-error "^wrong number of indices.*"))
  28. (define exception:length-non-negative
  29. (cons 'read-error ".*array length must be non-negative.*"))
  30. (with-test-prefix "sanity"
  31. ;; At the current time of writing, bignums have a tc7 that is one bit
  32. ;; away from strings. It used to be that the vector implementation
  33. ;; registered for strings had the TYP7S mask, not the TYP7 mask,
  34. ;; making the system think that bignums were vectors. Doh!
  35. (pass-if (not (uniform-vector? 12345678901234567890123456789))))
  36. (with-test-prefix "array?"
  37. (let ((bool (make-typed-array 'b #t '(5 6)))
  38. (char (make-typed-array 'a #\a '(5 6)))
  39. (byte (make-typed-array 'u8 0 '(5 6)))
  40. (short (make-typed-array 's16 0 '(5 6)))
  41. (ulong (make-typed-array 'u32 0 '(5 6)))
  42. (long (make-typed-array 's32 0 '(5 6)))
  43. (longlong (make-typed-array 's64 0 '(5 6)))
  44. (float (make-typed-array 'f32 0 '(5 6)))
  45. (double (make-typed-array 'f64 0 '(5 6)))
  46. (complex (make-typed-array 'c64 0 '(5 6)))
  47. (scm (make-typed-array #t 0 '(5 6))))
  48. (with-test-prefix "is bool"
  49. (pass-if (eq? #t (typed-array? bool 'b)))
  50. (pass-if (eq? #f (typed-array? char 'b)))
  51. (pass-if (eq? #f (typed-array? byte 'b)))
  52. (pass-if (eq? #f (typed-array? short 'b)))
  53. (pass-if (eq? #f (typed-array? ulong 'b)))
  54. (pass-if (eq? #f (typed-array? long 'b)))
  55. (pass-if (eq? #f (typed-array? longlong 'b)))
  56. (pass-if (eq? #f (typed-array? float 'b)))
  57. (pass-if (eq? #f (typed-array? double 'b)))
  58. (pass-if (eq? #f (typed-array? complex 'b)))
  59. (pass-if (eq? #f (typed-array? scm 'b))))
  60. (with-test-prefix "is char"
  61. (pass-if (eq? #f (typed-array? bool 'a)))
  62. (pass-if (eq? #t (typed-array? char 'a)))
  63. (pass-if (eq? #f (typed-array? byte 'a)))
  64. (pass-if (eq? #f (typed-array? short 'a)))
  65. (pass-if (eq? #f (typed-array? ulong 'a)))
  66. (pass-if (eq? #f (typed-array? long 'a)))
  67. (pass-if (eq? #f (typed-array? longlong 'a)))
  68. (pass-if (eq? #f (typed-array? float 'a)))
  69. (pass-if (eq? #f (typed-array? double 'a)))
  70. (pass-if (eq? #f (typed-array? complex 'a)))
  71. (pass-if (eq? #f (typed-array? scm 'a))))
  72. (with-test-prefix "is byte"
  73. (pass-if (eq? #f (typed-array? bool 'u8)))
  74. (pass-if (eq? #f (typed-array? char 'u8)))
  75. (pass-if (eq? #t (typed-array? byte 'u8)))
  76. (pass-if (eq? #f (typed-array? short 'u8)))
  77. (pass-if (eq? #f (typed-array? ulong 'u8)))
  78. (pass-if (eq? #f (typed-array? long 'u8)))
  79. (pass-if (eq? #f (typed-array? longlong 'u8)))
  80. (pass-if (eq? #f (typed-array? float 'u8)))
  81. (pass-if (eq? #f (typed-array? double 'u8)))
  82. (pass-if (eq? #f (typed-array? complex 'u8)))
  83. (pass-if (eq? #f (typed-array? scm 'u8))))
  84. (with-test-prefix "is short"
  85. (pass-if (eq? #f (typed-array? bool 's16)))
  86. (pass-if (eq? #f (typed-array? char 's16)))
  87. (pass-if (eq? #f (typed-array? byte 's16)))
  88. (pass-if (eq? #t (typed-array? short 's16)))
  89. (pass-if (eq? #f (typed-array? ulong 's16)))
  90. (pass-if (eq? #f (typed-array? long 's16)))
  91. (pass-if (eq? #f (typed-array? longlong 's16)))
  92. (pass-if (eq? #f (typed-array? float 's16)))
  93. (pass-if (eq? #f (typed-array? double 's16)))
  94. (pass-if (eq? #f (typed-array? complex 's16)))
  95. (pass-if (eq? #f (typed-array? scm 's16))))
  96. (with-test-prefix "is ulong"
  97. (pass-if (eq? #f (typed-array? bool 'u32)))
  98. (pass-if (eq? #f (typed-array? char 'u32)))
  99. (pass-if (eq? #f (typed-array? byte 'u32)))
  100. (pass-if (eq? #f (typed-array? short 'u32)))
  101. (pass-if (eq? #t (typed-array? ulong 'u32)))
  102. (pass-if (eq? #f (typed-array? long 'u32)))
  103. (pass-if (eq? #f (typed-array? longlong 'u32)))
  104. (pass-if (eq? #f (typed-array? float 'u32)))
  105. (pass-if (eq? #f (typed-array? double 'u32)))
  106. (pass-if (eq? #f (typed-array? complex 'u32)))
  107. (pass-if (eq? #f (typed-array? scm 'u32))))
  108. (with-test-prefix "is long"
  109. (pass-if (eq? #f (typed-array? bool 's32)))
  110. (pass-if (eq? #f (typed-array? char 's32)))
  111. (pass-if (eq? #f (typed-array? byte 's32)))
  112. (pass-if (eq? #f (typed-array? short 's32)))
  113. (pass-if (eq? #f (typed-array? ulong 's32)))
  114. (pass-if (eq? #t (typed-array? long 's32)))
  115. (pass-if (eq? #f (typed-array? longlong 's32)))
  116. (pass-if (eq? #f (typed-array? float 's32)))
  117. (pass-if (eq? #f (typed-array? double 's32)))
  118. (pass-if (eq? #f (typed-array? complex 's32)))
  119. (pass-if (eq? #f (typed-array? scm 's32))))
  120. (with-test-prefix "is long long"
  121. (pass-if (eq? #f (typed-array? bool 's64)))
  122. (pass-if (eq? #f (typed-array? char 's64)))
  123. (pass-if (eq? #f (typed-array? byte 's64)))
  124. (pass-if (eq? #f (typed-array? short 's64)))
  125. (pass-if (eq? #f (typed-array? ulong 's64)))
  126. (pass-if (eq? #f (typed-array? long 's64)))
  127. (pass-if (eq? #t (typed-array? longlong 's64)))
  128. (pass-if (eq? #f (typed-array? float 's64)))
  129. (pass-if (eq? #f (typed-array? double 's64)))
  130. (pass-if (eq? #f (typed-array? complex 's64)))
  131. (pass-if (eq? #f (typed-array? scm 's64))))
  132. (with-test-prefix "is float"
  133. (pass-if (eq? #f (typed-array? bool 'f32)))
  134. (pass-if (eq? #f (typed-array? char 'f32)))
  135. (pass-if (eq? #f (typed-array? byte 'f32)))
  136. (pass-if (eq? #f (typed-array? short 'f32)))
  137. (pass-if (eq? #f (typed-array? ulong 'f32)))
  138. (pass-if (eq? #f (typed-array? long 'f32)))
  139. (pass-if (eq? #f (typed-array? longlong 'f32)))
  140. (pass-if (eq? #t (typed-array? float 'f32)))
  141. (pass-if (eq? #f (typed-array? double 'f32)))
  142. (pass-if (eq? #f (typed-array? complex 'f32)))
  143. (pass-if (eq? #f (typed-array? scm 'f32))))
  144. (with-test-prefix "is double"
  145. (pass-if (eq? #f (typed-array? bool 'f64)))
  146. (pass-if (eq? #f (typed-array? char 'f64)))
  147. (pass-if (eq? #f (typed-array? byte 'f64)))
  148. (pass-if (eq? #f (typed-array? short 'f64)))
  149. (pass-if (eq? #f (typed-array? ulong 'f64)))
  150. (pass-if (eq? #f (typed-array? long 'f64)))
  151. (pass-if (eq? #f (typed-array? longlong 'f64)))
  152. (pass-if (eq? #f (typed-array? float 'f64)))
  153. (pass-if (eq? #t (typed-array? double 'f64)))
  154. (pass-if (eq? #f (typed-array? complex 'f64)))
  155. (pass-if (eq? #f (typed-array? scm 'f64))))
  156. (with-test-prefix "is complex"
  157. (pass-if (eq? #f (typed-array? bool 'c64)))
  158. (pass-if (eq? #f (typed-array? char 'c64)))
  159. (pass-if (eq? #f (typed-array? byte 'c64)))
  160. (pass-if (eq? #f (typed-array? short 'c64)))
  161. (pass-if (eq? #f (typed-array? ulong 'c64)))
  162. (pass-if (eq? #f (typed-array? long 'c64)))
  163. (pass-if (eq? #f (typed-array? longlong 'c64)))
  164. (pass-if (eq? #f (typed-array? float 'c64)))
  165. (pass-if (eq? #f (typed-array? double 'c64)))
  166. (pass-if (eq? #t (typed-array? complex 'c64)))
  167. (pass-if (eq? #f (typed-array? scm 'c64))))
  168. (with-test-prefix "is scm"
  169. (pass-if (eq? #f (typed-array? bool #t)))
  170. (pass-if (eq? #f (typed-array? char #t)))
  171. (pass-if (eq? #f (typed-array? byte #t)))
  172. (pass-if (eq? #f (typed-array? short #t)))
  173. (pass-if (eq? #f (typed-array? ulong #t)))
  174. (pass-if (eq? #f (typed-array? long #t)))
  175. (pass-if (eq? #f (typed-array? longlong #t)))
  176. (pass-if (eq? #f (typed-array? float #t)))
  177. (pass-if (eq? #f (typed-array? double #t)))
  178. (pass-if (eq? #f (typed-array? complex #t)))
  179. (pass-if (eq? #t (typed-array? scm #t))))))
  180. ;;;
  181. ;;; array-equal?
  182. ;;;
  183. (with-test-prefix "array-equal?"
  184. (pass-if "#s16(...)"
  185. (array-equal? #s16(1 2 3) #s16(1 2 3))))
  186. ;;;
  187. ;;; array->list
  188. ;;;
  189. (with-test-prefix "array->list"
  190. (pass-if (equal? (array->list #s16(1 2 3)) '(1 2 3)))
  191. (pass-if (equal? (array->list #(1 2 3)) '(1 2 3)))
  192. (pass-if (equal? (array->list #2((1 2) (3 4) (5 6))) '((1 2) (3 4) (5 6))))
  193. (pass-if (equal? (array->list #()) '())))
  194. ;;;
  195. ;;; array-fill!
  196. ;;;
  197. (with-test-prefix "array-fill!"
  198. (with-test-prefix "bool"
  199. (let ((a (make-bitvector 1 #t)))
  200. (pass-if "#f" (array-fill! a #f) #t)
  201. (pass-if "#t" (array-fill! a #t) #t)))
  202. (with-test-prefix "char"
  203. (let ((a (make-string 1 #\a)))
  204. (pass-if "x" (array-fill! a #\x) #t)))
  205. (with-test-prefix "byte"
  206. (let ((a (make-s8vector 1 0)))
  207. (pass-if "0" (array-fill! a 0) #t)
  208. (pass-if "127" (array-fill! a 127) #t)
  209. (pass-if "-128" (array-fill! a -128) #t)
  210. (pass-if-exception "128" exception:out-of-range
  211. (array-fill! a 128))
  212. (pass-if-exception "-129" exception:out-of-range
  213. (array-fill! a -129))
  214. (pass-if-exception "symbol" exception:wrong-type-arg
  215. (array-fill! a 'symbol))))
  216. (with-test-prefix "short"
  217. (let ((a (make-s16vector 1 0)))
  218. (pass-if "0" (array-fill! a 0) #t)
  219. (pass-if "123" (array-fill! a 123) #t)
  220. (pass-if "-123" (array-fill! a -123) #t)))
  221. (with-test-prefix "ulong"
  222. (let ((a (make-u32vector 1 1)))
  223. (pass-if "0" (array-fill! a 0) #t)
  224. (pass-if "123" (array-fill! a 123) #t)
  225. (pass-if-exception "-123" exception:out-of-range
  226. (array-fill! a -123) #t)))
  227. (with-test-prefix "long"
  228. (let ((a (make-s32vector 1 -1)))
  229. (pass-if "0" (array-fill! a 0) #t)
  230. (pass-if "123" (array-fill! a 123) #t)
  231. (pass-if "-123" (array-fill! a -123) #t)))
  232. (with-test-prefix "float"
  233. (let ((a (make-f32vector 1 1.0)))
  234. (pass-if "0.0" (array-fill! a 0) #t)
  235. (pass-if "123.0" (array-fill! a 123.0) #t)
  236. (pass-if "-123.0" (array-fill! a -123.0) #t)
  237. (pass-if "0" (array-fill! a 0) #t)
  238. (pass-if "123" (array-fill! a 123) #t)
  239. (pass-if "-123" (array-fill! a -123) #t)
  240. (pass-if "5/8" (array-fill! a 5/8) #t)))
  241. (with-test-prefix "double"
  242. (let ((a (make-f64vector 1 1/3)))
  243. (pass-if "0.0" (array-fill! a 0) #t)
  244. (pass-if "123.0" (array-fill! a 123.0) #t)
  245. (pass-if "-123.0" (array-fill! a -123.0) #t)
  246. (pass-if "0" (array-fill! a 0) #t)
  247. (pass-if "123" (array-fill! a 123) #t)
  248. (pass-if "-123" (array-fill! a -123) #t)
  249. (pass-if "5/8" (array-fill! a 5/8) #t))))
  250. ;;;
  251. ;;; array-in-bounds?
  252. ;;;
  253. (with-test-prefix "array-in-bounds?"
  254. (pass-if (let ((a (make-array #f '(425 425))))
  255. (eq? #f (array-in-bounds? a 0)))))
  256. ;;;
  257. ;;; array-prototype
  258. ;;;
  259. (with-test-prefix "array-type"
  260. (with-test-prefix "on make-foo-vector"
  261. (pass-if "bool"
  262. (eq? 'b (array-type (make-bitvector 1))))
  263. (pass-if "char"
  264. (eq? 'a (array-type (make-string 1))))
  265. (pass-if "byte"
  266. (eq? 'u8 (array-type (make-u8vector 1))))
  267. (pass-if "short"
  268. (eq? 's16 (array-type (make-s16vector 1))))
  269. (pass-if "ulong"
  270. (eq? 'u32 (array-type (make-u32vector 1))))
  271. (pass-if "long"
  272. (eq? 's32 (array-type (make-s32vector 1))))
  273. (pass-if "long long"
  274. (eq? 's64 (array-type (make-s64vector 1))))
  275. (pass-if "float"
  276. (eq? 'f32 (array-type (make-f32vector 1))))
  277. (pass-if "double"
  278. (eq? 'f64 (array-type (make-f64vector 1))))
  279. (pass-if "complex"
  280. (eq? 'c64 (array-type (make-c64vector 1))))
  281. (pass-if "scm"
  282. (eq? #t (array-type (make-vector 1)))))
  283. (with-test-prefix "on make-typed-array"
  284. (let ((types '(b a u8 s8 u16 s16 u32 s32 u64 u64 f32 f64 c32 c64)))
  285. (for-each (lambda (type)
  286. (pass-if (symbol->string type)
  287. (eq? type
  288. (array-type (make-typed-array type
  289. *unspecified*
  290. '(5 6))))))
  291. types))))
  292. ;;;
  293. ;;; array-set!
  294. ;;;
  295. (with-test-prefix "array-set!"
  296. (with-test-prefix "bitvector"
  297. ;; in Guile 1.8.0 a bug in bitvector_set() caused a segv in array-set!
  298. ;; on a bitvector like the following
  299. (let ((a (make-bitvector 1)))
  300. (pass-if "one elem set #t"
  301. (begin
  302. (array-set! a #t 0)
  303. (eq? #t (array-ref a 0))))
  304. (pass-if "one elem set #f"
  305. (begin
  306. (array-set! a #f 0)
  307. (eq? #f (array-ref a 0))))))
  308. (with-test-prefix "byte"
  309. (let ((a (make-s8vector 1)))
  310. (pass-if "-128"
  311. (begin (array-set! a -128 0) #t))
  312. (pass-if "0"
  313. (begin (array-set! a 0 0) #t))
  314. (pass-if "127"
  315. (begin (array-set! a 127 0) #t))
  316. (pass-if-exception "-129" exception:out-of-range
  317. (begin (array-set! a -129 0) #t))
  318. (pass-if-exception "128" exception:out-of-range
  319. (begin (array-set! a 128 0) #t))))
  320. (with-test-prefix "short"
  321. (let ((a (make-s16vector 1)))
  322. ;; true if n can be array-set! into a
  323. (define (fits? n)
  324. (false-if-exception (begin (array-set! a n 0) #t)))
  325. (with-test-prefix "store/fetch"
  326. ;; Check array-ref gives back what was put with array-set!.
  327. ;; In Guile 1.6.4 and earlier, array-set! only demanded an inum and
  328. ;; would silently truncate to a short.
  329. (do ((n 1 (1+ (* 2 n)))) ;; n=2^k-1
  330. ((not (fits? n)))
  331. (array-set! a n 0)
  332. (pass-if n
  333. (= n (array-ref a 0))))
  334. (do ((n -1 (* 2 n))) ;; -n=2^k
  335. ((not (fits? n)))
  336. (array-set! a n 0)
  337. (pass-if n
  338. (= n (array-ref a 0))))))))
  339. ;;;
  340. ;;; array-set!
  341. ;;;
  342. (with-test-prefix "array-set!"
  343. (with-test-prefix "one dim"
  344. (let ((a (make-array #f '(3 5))))
  345. (pass-if "start"
  346. (array-set! a 'y 3)
  347. #t)
  348. (pass-if "end"
  349. (array-set! a 'y 5)
  350. #t)
  351. (pass-if-exception "start-1" exception:out-of-range
  352. (array-set! a 'y 2))
  353. (pass-if-exception "end+1" exception:out-of-range
  354. (array-set! a 'y 6))
  355. (pass-if-exception "two indexes" exception:out-of-range
  356. (array-set! a 'y 6 7))))
  357. (with-test-prefix "two dim"
  358. (let ((a (make-array #f '(3 5) '(7 9))))
  359. (pass-if "start"
  360. (array-set! a 'y 3 7)
  361. #t)
  362. (pass-if "end"
  363. (array-set! a 'y 5 9)
  364. #t)
  365. (pass-if-exception "start i-1" exception:out-of-range
  366. (array-set! a 'y 2 7))
  367. (pass-if-exception "end i+1" exception:out-of-range
  368. (array-set! a 'y 6 9))
  369. (pass-if-exception "one index" exception:wrong-num-indices
  370. (array-set! a 'y 4))
  371. (pass-if-exception "three indexes" exception:wrong-num-indices
  372. (array-set! a 'y 4 8 0)))))
  373. ;;;
  374. ;;; make-shared-array
  375. ;;;
  376. (define exception:mapping-out-of-range
  377. (cons 'misc-error "^mapping out of range")) ;; per scm_make_shared_array
  378. (with-test-prefix "make-shared-array"
  379. ;; this failed in guile 1.8.0
  380. (pass-if "vector unchanged"
  381. (let* ((a (make-array #f '(0 7)))
  382. (s (make-shared-array a list '(0 7))))
  383. (array-equal? a s)))
  384. (pass-if-exception "vector, high too big" exception:mapping-out-of-range
  385. (let* ((a (make-array #f '(0 7))))
  386. (make-shared-array a list '(0 8))))
  387. (pass-if-exception "vector, low too big" exception:out-of-range
  388. (let* ((a (make-array #f '(0 7))))
  389. (make-shared-array a list '(-1 7))))
  390. (pass-if "truncate columns"
  391. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
  392. #2((a b) (d e) (g h))))
  393. (pass-if "pick one column"
  394. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  395. (lambda (i) (list i 2))
  396. '(0 2))
  397. #(c f i)))
  398. (pass-if "diagonal"
  399. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  400. (lambda (i) (list i i))
  401. '(0 2))
  402. #(a e i)))
  403. ;; this failed in guile 1.8.0
  404. (pass-if "2 dims from 1 dim"
  405. (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
  406. (lambda (i j) (list (+ (* i 3) j)))
  407. 4 3)
  408. #2((a b c) (d e f) (g h i) (j k l))))
  409. (pass-if "reverse columns"
  410. (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
  411. (lambda (i j) (list i (- 2 j)))
  412. 3 3)
  413. #2((c b a) (f e d) (i h g))))
  414. (pass-if "fixed offset, 0 based becomes 1 based"
  415. (let* ((x #2((a b c) (d e f) (g h i)))
  416. (y (make-shared-array x
  417. (lambda (i j) (list (1- i) (1- j)))
  418. '(1 3) '(1 3))))
  419. (and (eq? (array-ref x 0 0) 'a)
  420. (eq? (array-ref y 1 1) 'a))))
  421. ;; this failed in guile 1.8.0
  422. (pass-if "stride every third element"
  423. (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
  424. (lambda (i) (list (* i 3)))
  425. 4)
  426. #1(a d g j)))
  427. (pass-if "shared of shared"
  428. (let* ((a #2((1 2 3) (4 5 6) (7 8 9)))
  429. (s1 (make-shared-array a (lambda (i) (list i 1)) 3))
  430. (s2 (make-shared-array s1 list '(1 2))))
  431. (and (eqv? 5 (array-ref s2 1))
  432. (eqv? 8 (array-ref s2 2))))))
  433. ;;;
  434. ;;; uniform-vector-ref
  435. ;;;
  436. (with-test-prefix "uniform-vector-ref"
  437. (with-test-prefix "byte"
  438. (let ((a (make-s8vector 1)))
  439. (pass-if "0"
  440. (begin
  441. (array-set! a 0 0)
  442. (= 0 (uniform-vector-ref a 0))))
  443. (pass-if "127"
  444. (begin
  445. (array-set! a 127 0)
  446. (= 127 (uniform-vector-ref a 0))))
  447. (pass-if "-128"
  448. (begin
  449. (array-set! a -128 0)
  450. (= -128 (uniform-vector-ref a 0)))))))
  451. ;;;
  452. ;;; syntax
  453. ;;;
  454. (with-test-prefix "syntax"
  455. (pass-if "rank and lower bounds"
  456. ;; uniform u32 array of rank 2 with index ranges 2..3 and 7..8.
  457. (let ((a '#2u32@2@7((1 2) (3 4))))
  458. (and (array? a)
  459. (typed-array? a 'u32)
  460. (= (array-rank a) 2)
  461. (let loop ((bounds '((2 7) (2 8) (3 7) (3 8)))
  462. (result #t))
  463. (if (null? bounds)
  464. result
  465. (and result
  466. (loop (cdr bounds)
  467. (apply array-in-bounds? a (car bounds)))))))))
  468. (pass-if "negative lower bound"
  469. (let ((a '#1@-3(a b)))
  470. (and (array? a)
  471. (= (array-rank a) 1)
  472. (array-in-bounds? a -3) (array-in-bounds? a -2)
  473. (eq? 'a (array-ref a -3))
  474. (eq? 'b (array-ref a -2)))))
  475. (pass-if-exception "negative length" exception:length-non-negative
  476. (with-input-from-string "'#1:-3(#t #t)" read))
  477. (pass-if "bitvector is self-evaluating"
  478. (equal? (compile (bitvector)) (bitvector))))
  479. ;;;
  480. ;;; equal? with vector and one-dimensional array
  481. ;;;
  482. (with-test-prefix "equal?"
  483. (pass-if "array and non-array"
  484. (not (equal? #2f64((0 1) (2 3)) 100)))
  485. (pass-if "empty vectors of different types"
  486. (not (equal? #s32() #f64())))
  487. (pass-if "empty arrays of different types"
  488. (not (equal? #2s32() #2f64())))
  489. (pass-if "empty arrays of the same type"
  490. (equal? #s32() #s32()))
  491. (pass-if "identical uniform vectors of the same type"
  492. (equal? #s32(1) #s32(1)))
  493. (pass-if "nonidentical uniform vectors of the same type"
  494. (not (equal? #s32(1) #s32(-1))))
  495. (pass-if "identical uniform vectors of different types"
  496. (not (equal? #s32(1) #s64(1))))
  497. (pass-if "nonidentical uniform vectors of different types"
  498. (not (equal? #s32(1) #s64(-1))))
  499. (pass-if "vector and one-dimensional array"
  500. (equal? (make-shared-array #2((a b c) (d e f) (g h i))
  501. (lambda (i) (list i i))
  502. '(0 2))
  503. #(a e i))))