test.scm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. ; -*- mode: scheme; coding: utf-8 -*-
  2. ; Replacement for Guile C-based array system - Tests
  3. ; (c) Daniel Llorens - 2016-2018
  4. ; This library is free software; you can redistribute it and/or modify it under
  5. ; the terms of the GNU General Public License as published by the Free
  6. ; Software Foundation; either version 3 of the License, or (at your option) any
  7. ; later version.
  8. ; Run with $GUILE -L mod -s test.scm
  9. (import (newra newra) (newra test) (newra tools) (newra read)
  10. (srfi :64) (srfi :26) (srfi :8) (only (srfi :1) fold iota)
  11. (ice-9 match) (only (rnrs base) vector-map))
  12. (define (throws-exception? k thunk)
  13. (catch #t
  14. (lambda () (thunk) #f)
  15. (lambda args (if (eq? k (car args)) args #f))))
  16. (define (ra->string ra) (call-with-output-string (cut display ra <>)))
  17. (define (string->ra s) (call-with-input-string s read))
  18. (set! test-log-to-file #t)
  19. (test-begin "newra")
  20. ; -----------------------
  21. ; auxiliary functions
  22. ; -----------------------
  23. (test-equal
  24. 6 ((@@ (newra newra) vector-fold) (lambda (a c) (+ c (car a))) 0 #((1) (2) (3))))
  25. (test-equal
  26. #(2 3) ((@@ (newra newra) vector-clip) #(1 2 3 4) 1 3))
  27. ; loop-fun from (newra test) FIXME may become ra-index-map!
  28. (define ra0 (make-ra-root (make-dim 1) (c-dims)))
  29. (define ra1 (make-ra-root (make-dim (* 2)) (c-dims 2)))
  30. (define ra2 (make-ra-root (make-dim (* 2 3)) (c-dims 2 3)))
  31. (define ra3 (make-ra-root (make-dim (* 2 3 4)) (c-dims 2 3 4)))
  32. (test-equal "0"
  33. (with-output-to-string (lambda () (%ra-loop (vector-map dim-len (ra-dims ra0)) 0 () (display (ra0))))))
  34. (test-equal "01"
  35. (with-output-to-string (lambda () (%ra-loop (vector-map dim-len (ra-dims ra1)) 1 (i) (display (+ i))))))
  36. (test-equal "012123"
  37. (with-output-to-string (lambda () (%ra-loop (vector-map dim-len (ra-dims ra2)) 2 (i j) (display (+ i j))))))
  38. (test-equal "012312342345123423453456"
  39. (with-output-to-string (lambda () (%ra-loop (vector-map dim-len (ra-dims ra3)) 3 (i j k) (display (+ i j k))))))
  40. (define a0 (ra->array (as-ra ra0 #:type #t)))
  41. (define a1 (ra->array (as-ra ra1 #:type #t)))
  42. (define a2 (ra->array (as-ra ra2 #:type #t)))
  43. (define a3 (ra->array (as-ra ra3 #:type #t)))
  44. (test-equal "(0)"
  45. (call-with-output-string (lambda (o) (ra-loop ra0 (lambda () (format o "(~a)" (ra0)))))))
  46. (test-equal "(0)(1)"
  47. (call-with-output-string (lambda (o) (ra-loop ra1 (lambda (i) (format o "(~a)" (ra1 i)))))))
  48. (test-equal "(0)(1)(2)(3)(4)(5)"
  49. (call-with-output-string (lambda (o) (ra-loop ra2 (lambda (i j) (format o "(~a)" (ra2 i j)))))))
  50. (test-equal "(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)(12)(13)(14)(15)(16)(17)(18)(19)(20)(21)(22)(23)"
  51. (call-with-output-string (lambda (o) (ra-loop ra3 (lambda (i j k) (format o "(~a)" (ra3 i j k)))))))
  52. (test-equal "(0)"
  53. (call-with-output-string (lambda (o) (array-loop a0 (lambda () (format o "(~a)" (array-ref a0)))))))
  54. (test-equal "(0)(1)"
  55. (call-with-output-string (lambda (o) (array-loop a1 (lambda (i) (format o "(~a)" (array-ref a1 i)))))))
  56. (test-equal "(0)(1)(2)(3)(4)(5)"
  57. (call-with-output-string (lambda (o) (array-loop a2 (lambda (i j) (format o "(~a)" (array-ref a2 i j)))))))
  58. (test-equal "(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)(12)(13)(14)(15)(16)(17)(18)(19)(20)(21)(22)(23)"
  59. (call-with-output-string (lambda (o) (array-loop a3 (lambda (i j k) (format o "(~a)" (array-ref a3 i j k)))))))
  60. ; -----------------------
  61. ; array->ra, ra->array, printers
  62. ; -----------------------
  63. (let* ((ra0 (array->ra #(1 2 3)))
  64. (ra1 (array->ra #@1(1 2 3)))
  65. (ra2 (array->ra #2((1 2) (3 4))))
  66. (ra3 (array->ra #2@1@1((1 2) (3 4))))
  67. (ra4 (array->ra #0(99))))
  68. (test-equal (ra->string ra0) "#%1:3(1 2 3)")
  69. (test-equal (ra->string ra1) "#%1@1:3(1 2 3)")
  70. (test-equal (ra->string ra2) "#%2:2:2((1 2) (3 4))")
  71. (test-equal (ra->string ra3) "#%2@1:2@1:2((1 2) (3 4))")
  72. (test-equal (ra->string ra4) "#%0(99)")
  73. (test-equal #(1 2 3) (ra->array ra0))
  74. (test-equal #@1(1 2 3) (ra->array ra1))
  75. (test-equal #2((1 2) (3 4)) (ra->array ra2))
  76. (test-equal #2@1@1((1 2) (3 4)) (ra->array ra3))
  77. (test-equal #0(99) (ra->array ra4))
  78. ; dead axes
  79. (test-equal "#%2:d:10((0 1 2 3 4 5 6 7 8 9))" (ra->string (ra-transpose (ra-i 10) 1))))
  80. ; -----------------------
  81. ; ra-iota, ra-i
  82. ; -----------------------
  83. (test-equal "#%3:2:3:4(((0 1 2 3) (4 5 6 7) (8 9 10 11)) ((12 13 14 15) (16 17 18 19) (20 21 22 23)))"
  84. (ra->string (ra-i 2 3 4)))
  85. (test-equal "#%3:2:0:4(() ())"
  86. (ra->string (ra-i 2 0 4)))
  87. (test-equal "#%0(0)"
  88. (ra->string (ra-i)))
  89. (test-equal "#%1:4(9 8 7 6)"
  90. (ra->string (ra-iota 4 9 -1)))
  91. (test-equal "#%1:4(9 10 11 12)"
  92. (ra->string (ra-iota 4 9)))
  93. (test-equal "#%1:0()"
  94. (ra->string (ra-iota 0)))
  95. ; -----------------------
  96. ; make-ra-shared
  97. ; -----------------------
  98. (let* ((ra2 (array->ra #2((1 2) (3 4))))
  99. (ra3 (array->ra #2@1@1((1 2) (3 4))))
  100. (ra4 (array->ra #0(99))))
  101. (test-equal "#%0(99)" (ra->string (make-ra-shared ra4 (lambda () '()))))
  102. (test-equal "#%1:2(1 4)" (ra->string (make-ra-shared ra2 (lambda (i) (list i i)) 2)))
  103. (test-equal "#%1:2(1 3)" (ra->string (make-ra-shared ra2 (lambda (i) (list i 0)) 2)))
  104. (test-equal "#%1:2(2 4)" (ra->string (make-ra-shared ra2 (lambda (i) (list i 1)) 2)))
  105. (test-equal "#%1:2(1 2)" (ra->string (make-ra-shared ra2 (lambda (j) (list 0 j)) 2)))
  106. (test-equal "#%1:2(3 4)" (ra->string (make-ra-shared ra2 (lambda (j) (list 1 j)) 2)))
  107. (test-equal "#%1@1:2(1 4)" (ra->string (make-ra-shared ra3 (lambda (i) (list i i)) '(1 2))))
  108. (test-equal "#%1@1:2(1 3)" (ra->string (make-ra-shared ra3 (lambda (i) (list i 1)) '(1 2))))
  109. (test-equal "#%1@1:2(2 4)" (ra->string (make-ra-shared ra3 (lambda (i) (list i 2)) '(1 2))))
  110. (test-equal "#%1@1:2(1 2)" (ra->string (make-ra-shared ra3 (lambda (j) (list 1 j)) '(1 2))))
  111. (test-equal "#%1@1:2(3 4)" (ra->string (make-ra-shared ra3 (lambda (j) (list 2 j)) '(1 2)))))
  112. ; -----------------------
  113. ; make-ra-new, make-ra
  114. ; -----------------------
  115. (let* ((ra5 (make-ra-new #t 0 (c-dims '(1 3) '(1 2)))))
  116. (array-index-map! (ra-root ra5) (lambda i i))
  117. (test-equal (ra->string ra5) "#%2@1:3@1:2(((0) (1)) ((2) (3)) ((4) (5)))")
  118. (test-equal 3 (ra-length ra5)))
  119. (let* ((ra5 (make-typed-ra 's64 0 '(1 3) '(1 2))))
  120. (array-index-map! (ra-root ra5) (lambda i (car i)))
  121. (test-equal (ra->string ra5) "#%2s64@1:3@1:2((0 1) (2 3) (4 5))")
  122. (test-equal 3 (ra-length ra5)))
  123. ; -----------------------
  124. ; make-ra-root
  125. ; -----------------------
  126. (define ra6 (make-ra-root #(1 2 3 4 5 6) (c-dims '(1 2) '(1 3))))
  127. (test-equal (ra->string ra6) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  128. (define ra7a (make-ra-root (make-dim 6 1) (c-dims '(1 2) '(1 3))))
  129. (test-equal (ra->string ra7a) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  130. (define ra7b (make-ra-raw #(1 4 2 5 3 6) -3 `#(,(make-dim 2 1 1) ,(make-dim 3 1 2))))
  131. (test-equal (ra->string ra7b) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  132. (test-equal 2 (ra-length ra6))
  133. (test-equal 2 (ra-length ra7a))
  134. (test-equal 2 (ra-length ra7b))
  135. ; -----------------------
  136. ; ra-slice, ra-ref, ra-cell
  137. ; -----------------------
  138. (define ra8 (make-ra-root (make-dim 6 1) (c-dims '(1 2) '(1 3))))
  139. (test-equal 2 (ra-length ra8))
  140. (test-equal (ra->string (ra-cell ra8)) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  141. (test-equal (ra->string (ra-cell ra8 1)) "#%1@1:3(1 2 3)")
  142. (test-equal (ra->string (ra-cell ra8 2)) "#%1@1:3(4 5 6)")
  143. (test-equal 5 (ra-cell ra8 2 2))
  144. ; applicable!
  145. (test-equal (ra->string (ra8)) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  146. (test-equal (ra->string (ra8 1)) "#%1@1:3(1 2 3)")
  147. (test-equal (ra->string (ra8 2)) "#%1@1:3(4 5 6)")
  148. (test-equal 5 (ra8 2 2))
  149. (test-equal (ra->string (ra-slice ra8)) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  150. (test-equal (ra->string (ra-slice ra8 1)) "#%1@1:3(1 2 3)")
  151. (test-equal (ra->string (ra-slice ra8 2)) "#%1@1:3(4 5 6)")
  152. (test-equal (ra->string (ra-slice ra8 2 2)) "#%0(5)")
  153. (test-equal 5 (ra-ref (ra-slice ra8 2 2)))
  154. (test-assert (throws-exception? 'bad-number-of-indices (lambda () (ra-ref ra8))))
  155. (test-assert (throws-exception? 'bad-number-of-indices (lambda () (ra-ref ra8 1))))
  156. (test-assert (throws-exception? 'dim-check-out-of-range (lambda () (ra-ref ra8 0 0))))
  157. (test-assert (throws-exception? 'dim-check-out-of-range (lambda () (ra-ref ra8 3 1))))
  158. (test-equal 1 (ra-ref ra8 1 1))
  159. (test-equal 2 (ra-ref ra8 1 2))
  160. (test-equal 3 (ra-ref ra8 1 3))
  161. (test-equal 4 (ra-ref ra8 2 1))
  162. (test-equal 5 (ra-ref ra8 2 2))
  163. (test-equal 6 (ra-ref ra8 2 3))
  164. ; -----------------------
  165. ; ra-transpose (see below for ra-transpose with dead axes)
  166. ; -----------------------
  167. (test-equal (ra->string (ra-transpose ra7a 1 0)) "#%2@1:3@1:2((1 4) (2 5) (3 6))")
  168. (test-equal (ra->string (ra-transpose ra7b 1 0)) "#%2@1:3@1:2((1 4) (2 5) (3 6))")
  169. (test-equal (ra->string (ra-transpose ra7a)) "#%2@1:3@1:2((1 4) (2 5) (3 6))")
  170. (test-equal (ra->string (ra-transpose ra7b)) "#%2@1:3@1:2((1 4) (2 5) (3 6))")
  171. (test-equal (ra->string (ra-transpose ra7a 0 1)) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  172. (test-equal (ra->string (ra-transpose ra7b 0 1)) "#%2@1:2@1:3((1 2 3) (4 5 6))")
  173. (test-equal (ra->string (ra-transpose ra7a 0 0)) "#%1@1:2(1 5)")
  174. (test-equal (ra->string (ra-transpose ra7b 0 0)) "#%1@1:2(1 5)")
  175. (test-assert (throws-exception? 'bad-number-of-axes (lambda () (ra-transpose (ra-iota 3) 0 1))))
  176. ; -----------------------
  177. ; ra-reverse
  178. ; -----------------------
  179. (let ((ra (ra-i 2 3 2)))
  180. (test-assert (ra-equal? ra (ra-reverse ra)))
  181. (test-equal "#%3:2:3:2(((6 7) (8 9) (10 11)) ((0 1) (2 3) (4 5)))" (ra->string (ra-reverse ra 0)))
  182. (test-equal "#%3:2:3:2(((4 5) (2 3) (0 1)) ((10 11) (8 9) (6 7)))" (ra->string (ra-reverse ra 1)))
  183. (test-equal "#%3:2:3:2(((1 0) (3 2) (5 4)) ((7 6) (9 8) (11 10)))" (ra->string (ra-reverse ra 2)))
  184. (test-equal "#%3:2:3:2(((5 4) (3 2) (1 0)) ((11 10) (9 8) (7 6)))" (ra->string (ra-reverse ra 1 2)))
  185. (test-equal "#%3:2:3:2(((7 6) (9 8) (11 10)) ((1 0) (3 2) (5 4)))" (ra->string (ra-reverse ra 2 0)))
  186. (test-assert (ra-equal? (ra-reverse ra 2 0) (ra-reverse ra 0 2)))
  187. (test-assert (ra-equal? (ra-reverse ra 2 0 1) (ra-reverse (ra-reverse (ra-reverse ra 0) 1) 2)))
  188. (test-eq (ra-root ra) (ra-root (ra-reverse ra 2 0))))
  189. (let ((ra (make-ra-root #(1 2 3 4) (vector (make-dim 4 -1)))))
  190. (test-equal "#%1@-1:4(4 3 2 1)" (ra->string (ra-reverse ra 0))))
  191. ; -----------------------
  192. ; ra-slice-for-each
  193. ; -----------------------
  194. (define ra-empty0 (make-ra-root (make-dim 6 1) (c-dims '(1 0) '(2 1))))
  195. (define ra-empty1 (make-ra-root (make-dim 6 1) (c-dims '(1 1) '(2 1))))
  196. (define ra-empty2 (make-ra-root (make-dim 6 1) (c-dims '(1 0) '(2 2))))
  197. (test-equal 0 (ra-length ra-empty0))
  198. (test-equal 1 (ra-length ra-empty1))
  199. (test-equal 0 (ra-length ra-empty2))
  200. (for-each
  201. (lambda (ra-slice-for-each)
  202. (test-begin (procedure-name ra-slice-for-each))
  203. (test-equal "#%2@1:0@2:0()\n"
  204. (call-with-output-string
  205. (lambda (s) (ra-slice-for-each 0 (lambda (o) (format s "~a\n" o)) ra-empty0))))
  206. (test-equal "#%2@1:1@2:0(())\n"
  207. (call-with-output-string
  208. (lambda (s) (ra-slice-for-each 0 (lambda (o) (format s "~a\n" o)) ra-empty1))))
  209. (test-equal "#%2@1:0@2:1()\n"
  210. (call-with-output-string
  211. (lambda (s) (ra-slice-for-each 0 (lambda (o) (format s "~a\n" o)) ra-empty2))))
  212. (test-equal "#%2@1:2@1:3((1 2 3) (4 5 6))\n"
  213. (call-with-output-string
  214. (lambda (s) (ra-slice-for-each 0 (lambda (o) (format s "~a\n" o)) ra6))))
  215. (test-equal "#%1@1:3(1 2 3)\n#%1@1:3(4 5 6)\n"
  216. (call-with-output-string
  217. (lambda (s) (ra-slice-for-each 1 (lambda (o) (format s "~a\n" o)) ra6))))
  218. (test-equal "1\n2\n3\n4\n5\n6\n"
  219. (call-with-output-string
  220. (lambda (s) (ra-slice-for-each 2 (lambda (o) (format s "~a\n" (ra-ref o))) ra6))))
  221. (test-equal "2\n4\n6\n8\n10\n12\n"
  222. (call-with-output-string
  223. (lambda (s) (ra-slice-for-each 2 (lambda (a b) (format s "~a\n" (+ (ra-ref a) (ra-ref b)))) ra6 ra7a))))
  224. (test-equal "2\n4\n6\n8\n10\n12\n"
  225. (call-with-output-string
  226. (lambda (s) (ra-slice-for-each 2 (lambda (a b) (format s "~a\n" (+ (ra-ref a) (ra-ref b)))) ra6 ra7b))))
  227. (test-equal "(#%2@1:2@1:3((1 2 3) (4 5 6)) #%2@1:2@1:3((1 2 3) (4 5 6)) #%2@1:2@1:3((1 2 3) (4 5 6)))\n"
  228. (call-with-output-string
  229. (lambda (s) (ra-slice-for-each 0 (lambda x (format s "~a\n" x)) ra6 ra7a ra7b))))
  230. (test-equal "(#%1@1:3(1 2 3) #%1@1:3(1 2 3) #%1@1:3(1 2 3))\n(#%1@1:3(4 5 6) #%1@1:3(4 5 6) #%1@1:3(4 5 6))\n"
  231. (call-with-output-string
  232. (lambda (s) (ra-slice-for-each 1 (lambda x (format s "~a\n" x)) ra6 ra7a ra7b))))
  233. (test-equal "(#%0(1) #%0(1) #%0(1))\n(#%0(2) #%0(2) #%0(2))\n(#%0(3) #%0(3) #%0(3))\n(#%0(4) #%0(4) #%0(4))\n(#%0(5) #%0(5) #%0(5))\n(#%0(6) #%0(6) #%0(6))\n"
  234. (call-with-output-string
  235. (lambda (s) (ra-slice-for-each 2 (lambda x (format s "~a\n" x)) ra6 ra7a ra7b))))
  236. (test-end (procedure-name ra-slice-for-each)))
  237. (list ra-slice-for-each-1 ra-slice-for-each-2
  238. ra-slice-for-each-3 ra-slice-for-each-4))
  239. ; -----------------------
  240. ; ra-offset - from guile/test-suite/tests/arrays.test
  241. ; -----------------------
  242. (test-begin "ra-offset")
  243. ; plain vector
  244. (test-equal 0 (ra-offset (make-ra 0 4)))
  245. ; plain array rank 2
  246. (test-equal 0 (ra-offset (make-ra 0 4 4)))
  247. ; row of rank-2 array, I
  248. (test-equal 0 (ra-offset ((make-ra 0 5 3) 0)))
  249. ; row of rank-2 array, II
  250. (test-equal 4 (ra-offset ((make-ra 0 6 4) 1)))
  251. ; col of rank-2 array, I
  252. (test-equal 0 (ra-offset ((ra-transpose (make-ra 0 5 3) 1 0) 0)))
  253. ; col of rank-2 array, II
  254. (test-equal 1 (ra-offset ((ra-transpose (make-ra 0 6 4) 1 0) 1)))
  255. ; of inverted array; shared-array-root's doc is unclear as 'first' is unclear. But we do the same.
  256. (test-equal 2 (shared-array-offset (make-shared-array #(1 2 3) (lambda (i) (list (- 2 i))) 3)))
  257. (test-equal 2 (ra-offset (make-ra-shared (array->ra #(1 2 3)) (lambda (i) (list (- 2 i))) 3)))
  258. (test-end "ra-offset")
  259. ; -----------------------
  260. ; ra-slice-for-each compatibility - from guile/test-suite/tests/array-map.test
  261. ; -----------------------
  262. (for-each
  263. (match-lambda
  264. ((name ra-slice-for-each list->ra list->typed-ra make-ra ra-copy!
  265. make-ra-shared array->ra ra->array ra-set! ra-ref)
  266. (let ((test-name (format #f "~a" name)))
  267. (test-begin test-name)
  268. ; 1 argument frame rank 1
  269. (test-equal
  270. #2((1 3 9) (2 7 8))
  271. (ra->array
  272. (let* ((a (list->ra 2 '((9 1 3) (7 8 2)))))
  273. (ra-slice-for-each 1 (lambda (a) (sort! (ra->array a) <)) a)
  274. a)))
  275. ; 1 argument frame rank 1, non-zero base indices
  276. (test-equal
  277. #2@1@1((1 3 9) (2 7 8))
  278. (ra->array
  279. (let* ((a (make-ra *unspecified* '(1 2) '(1 3)))
  280. (b (array->ra #2@1@1((9 1 3) (7 8 2)))))
  281. (ra-copy! a b)
  282. (ra-slice-for-each 1 (lambda (a) (sort! (ra->array a) <)) a)
  283. a)))
  284. ; 2 arguments frame rank 1
  285. (test-equal
  286. #f64(8 -1)
  287. (ra->array
  288. (let* ((x (list->typed-ra 'f64 2 '((9 1) (7 8))))
  289. (y (array->ra (f64vector 99 99))))
  290. (ra-slice-for-each 1 (lambda (y x) (ra-set! y (- (ra-ref x 0) (ra-ref x 1)))) y x)
  291. y)))
  292. ; regression: zero-sized frame loop without unrolling
  293. (test-equal
  294. 99
  295. (let* ((x 99)
  296. (o (make-ra 0. 0 3 2)))
  297. (ra-slice-for-each 2
  298. (lambda (o a0 a1)
  299. (set! x 0))
  300. o
  301. (make-ra-shared (make-ra 1. 0 1) (const '(0 0)) 0 3)
  302. (make-ra 2. 0 3))
  303. x))
  304. (test-end (format #f "~a" name)))))
  305. `(("oldra" ,array-slice-for-each ,list->array ,list->typed-array
  306. ,make-array ,(lambda (a b) (array-copy! b a)) ,make-shared-array
  307. ,(lambda (x) x) ,(lambda (x) x) ,array-set! ,array-ref)
  308. ("newra" ,ra-slice-for-each ,list->ra ,list->typed-ra
  309. ,make-ra ,ra-copy! ,make-ra-shared
  310. ,array->ra ,ra->array ,ra-set! ,ra-ref)
  311. ))
  312. ; -----------------------
  313. ; setter
  314. ; -----------------------
  315. (define ra9 (make-ra-root (make-vector 6) (c-dims '(-1 0) '(1 3))))
  316. (set! (ra9 -1 1) 99)
  317. (set! (ra9 -1 2) 77)
  318. (set! (ra9 -1 3) 88)
  319. (set! (ra9 0 1) 33)
  320. (set! (ra9 0 2) 11)
  321. (set! (ra9 0 3) 22)
  322. (test-equal (ra->string ra9) "#%2@-1:2@1:3((99 77 88) (33 11 22))")
  323. (test-equal 2 (ra-length ra9))
  324. (test-equal #2@-1:2@1:3((99 77 88) (33 44 22)) (ra->array (set! (ra9 0 2) 44)))
  325. ; -----------------------
  326. ; test through the ra-map! interface, also ra-copy!, ra-fill!, ra-equal?
  327. ; -----------------------
  328. (define ra11 (make-ra-new #t 0 (c-dims 10)))
  329. (define ra12 (make-ra-root (make-dim 10) (c-dims 10)))
  330. (define ra13 (make-ra-root (make-dim 10 10 -1) (c-dims 10)))
  331. (test-equal 10 (ra-length ra11))
  332. (test-equal 10 (ra-length ra12))
  333. (test-equal 10 (ra-length ra13))
  334. (test-equal "#%1:10(0 1 2 3 4 5 6 7 8 9)" (ra->string (ra-copy! ra11 ra12)))
  335. (let ((ra5a (make-ra-new #t 0 (c-dims '(1 2) '(1 3))))
  336. (ra6a (make-ra-root (vector-copy #(1 2 3 4 5 6)) (c-dims '(1 2) '(1 3)))))
  337. (test-equal "#%2@1:2@1:3((1 2 3) (4 5 6))" (ra->string (ra-copy! ra5a ra6a)))
  338. (test-assert (ra-equal? ra6a ra5a))
  339. (test-assert (ra-equal? ra6a ra5a ra6a))
  340. (set! (ra5a 1 1) 99)
  341. (test-assert (not (ra-equal? ra5a ra6a)))
  342. (set! (ra6a 1 1) 99)
  343. (test-assert (ra-equal? ra5a ra6a))
  344. (test-equal "#%2@1:2@1:3((x x x) (x x x))" (ra->string (ra-fill! ra5a 'x)))
  345. (test-equal "#%2@1:2@1:3((x x x) (x x x))" (ra->string ra5a))
  346. (test-assert (not (ra-equal? ra6a ra5a)))
  347. (test-assert (not (ra-equal? (string->ra "#%(1 2 3)") (string->ra "#%(1 2 3 4)"))))
  348. (test-assert (not (ra-equal? (string->ra "#%1f64(1 2 3)") (string->ra "#%(1 2 3)"))))
  349. (test-assert (ra-equal? (make-ra-root #(99 99 99 99) (c-dims '(1 2) '(1 2)))
  350. (ra-fill! (make-ra-root (vector 1 2 3 4) (c-dims '(1 2) '(1 2))) 99))))
  351. (test-begin "ra-for-each")
  352. (test-equal "(10 0 10)(9 1 9)(8 2 8)(7 3 7)(6 4 6)(5 5 5)(4 6 4)(3 7 3)(2 8 2)(1 9 1)"
  353. (call-with-output-string (lambda (s) (ra-for-each (lambda x (display x s)) ra13 ra12 ra13))))
  354. (test-end "ra-for-each")
  355. (ra-map! ra11 (lambda () (random 100)))
  356. (for-each
  357. (match-lambda
  358. ((ra-map! name)
  359. (test-begin (string-append "3 args - " name))
  360. (test-equal "#%1:10(-10 -8 -6 -4 -2 0 2 4 6 8)"
  361. (call-with-output-string (cute display (ra-map! ra11 - ra12 ra13) <>)))
  362. (test-end (string-append "3 args - " name))))
  363. `((,(cut ra-map*! ra-slice-for-each-1 <...>) "fe1")
  364. (,(cut ra-map*! ra-slice-for-each-2 <...>) "fe2")
  365. (,(cut ra-map*! ra-slice-for-each-3 <...>) "fe3")
  366. (,(cut ra-map*! ra-slice-for-each-4 <...>) "fe4")
  367. (,ra-map! "ra-map!")))
  368. ; test with enough args to hit the arglist version.
  369. (for-each
  370. (match-lambda
  371. ((ra-map! name)
  372. (test-begin (string-append "4 args - " name))
  373. (test-equal "#%1:10(-10 -9 -8 -7 -6 -5 -4 -3 -2 -1)"
  374. (call-with-output-string (cute display (ra-map! ra11 - ra12 ra13 ra12) <>)))
  375. (test-end (string-append "4 args - " name))))
  376. `((,(cut ra-map*! ra-slice-for-each-1 <...>) "fe1")
  377. (,(cut ra-map*! ra-slice-for-each-2 <...>) "fe2")
  378. (,(cut ra-map*! ra-slice-for-each-3 <...>) "fe3")
  379. (,(cut ra-map*! ra-slice-for-each-4 <...>) "fe4")
  380. (,ra-map! "ra-map!")))
  381. (test-begin "ra-for-each")
  382. (test-equal "(10 0 10 0)(9 1 9 1)(8 2 8 2)(7 3 7 3)(6 4 6 4)(5 5 5 5)(4 6 4 6)(3 7 3 7)(2 8 2 8)(1 9 1 9)"
  383. (call-with-output-string (lambda (s) (ra-for-each (lambda x (display x s)) ra13 ra12 ra13 ra12))))
  384. (test-end "ra-for-each")
  385. ; -----------------------
  386. ; ra->list
  387. ; -----------------------
  388. (test-equal '(1 2 3) (ra->list (string->ra "#%(1 2 3)")))
  389. (test-equal '(1 2 3) (ra->list (string->ra "#%@1(1 2 3)")))
  390. (test-equal '((1 2) (3 4)) (ra->list (string->ra "#%2((1 2) (3 4))")))
  391. (test-equal '((1 2) (3 4)) (ra->list (string->ra "#%2@1@1((1 2) (3 4))")))
  392. (test-equal '(((1 2 3) (3 4 5)) ((4 5 6) (6 7 8))) (ra->list (string->ra "#%3@1@1@-1(((1 2 3) (3 4 5)) ((4 5 6) (6 7 8)))")))
  393. (test-equal 99 (ra->list (string->ra "#%0(99)")))
  394. ; -----------------------
  395. ; reader
  396. ; -----------------------
  397. (for-each
  398. (lambda (str)
  399. (test-equal (format #f "#%3~a:2:2:2(((1 2) (3 4)) ((5 6) (7 8)))" str)
  400. (ra->string (string->ra (format #f "#%3~a(((1 2) (3 4)) ((5 6) (7 8)))" str))))
  401. (test-equal (format #f "#%3~a:2:2:2(((1 2) (3 4)) ((5 6) (7 8)))" str)
  402. (ra->string (string->ra (format #f "#%3~a[((1 2) [3 4]) [[5 6] (7 8)]]" str))))
  403. (test-equal (format #f "#%2~a:2:2((1 2) (3 4))" str)
  404. (ra->string (string->ra (format #f "#%2~a((1 2) (3 4))" str))))
  405. (test-equal (format #f "#%1~a:4(1 2 3 4)" str)
  406. (ra->string (string->ra (format #f "#%1~a(1 2 3 4)" str))))
  407. (test-equal (format #f "#%1~a:12(1 2 3 4 5 6 7 8 9 10 11 12)" str)
  408. (ra->string (string->ra (format #f "#%1~a(1 2 3 4 5 6 7 8 9 10 11 12)" str))))
  409. (test-equal (format #f "#%2~a@1:2@1:2((1 2) (3 4))" str)
  410. (ra->string (string->ra (format #f "#%2~a@1@1((1 2) (3 4))" str))))
  411. (test-equal (format #f "#%2~a@1:2@1:2((1 2) (3 4))" str)
  412. (ra->string (string->ra (format #f "#%2~a@1@1:2((1 2) (3 4))" str))))
  413. (test-equal (format #f "#%2~a@-1:2@1:2((1 2) (3 4))" str)
  414. (ra->string (string->ra (format #f "#%2~a@-1@1((1 2) (3 4))" str)))))
  415. '("s32" ""))
  416. (test-equal "#%0(#(1 2 3))" (ra->string (string->ra "#%0(#(1 2 3))")))
  417. (test-equal "#%1f64:3(1.0 2.0 3.0)" (ra->string (string->ra "#%1f64(1 2 3)")))
  418. (test-equal "#%0(#%1:3(1 2 3))" (ra->string (string->ra "#%0[#%[1 2 3]]")))
  419. (test-equal "#%1f64:3(1.0 2.0 3.0)" (ra->string (string->ra "#%1f64[1 2 3]")))
  420. ; -----------------------
  421. ; list->ra, list->typed-ra
  422. ; -----------------------
  423. (test-equal "#%0(99)" (ra->string (list->ra 0 99)))
  424. (test-equal "#%1:3(1 2 3)" (ra->string (list->ra 1 '(1 2 3))))
  425. (test-equal "#%0((1 2 3))" (ra->string (list->ra 0 '(1 2 3))))
  426. (test-equal "#%2:2:2((1 2) (3 4))" (ra->string (list->ra 2 '((1 2) (3 4)))))
  427. (test-equal "#%2:2:3((1 2 3) (4 5 6))" (ra->string (list->ra 2 '((1 2 3) (4 5 6)))))
  428. (test-equal "#%2:3:2((1 2) (3 4) (5 6))" (ra->string (list->ra 2 '((1 2) (3 4) (5 6)))))
  429. (test-equal "#%1:2((1 2) (3 4))" (ra->string (list->ra 1 '((1 2) (3 4)))))
  430. (test-equal "#%0(((1 2) (3 4)))" (ra->string (list->ra 0 '((1 2) (3 4)))))
  431. (test-equal "#%0f64(99.0)" (ra->string (list->typed-ra 'f64 0 99)))
  432. (test-equal "#%1f64:3(1.0 2.0 3.0)" (ra->string (list->typed-ra 'f64 1 '(1 2 3))))
  433. (test-equal "#%2f64:2:2((1.0 2.0) (3.0 4.0))" (ra->string (list->typed-ra 'f64 2 '((1 2) (3 4)))))
  434. (test-equal "#%1f64@1:3(1.0 2.0 3.0)" (ra->string (list->typed-ra 'f64 '(1) '(1 2 3))))
  435. (test-equal "#%1f64@1:3(1.0 2.0 3.0)" (ra->string (list->typed-ra 'f64 '((1 3)) '(1 2 3))))
  436. (test-equal "#%1f64@-1:3(1.0 2.0 3.0)" (ra->string (list->ra 'f64 '(-1) '(1 2 3))))
  437. (test-equal "#%1f64@-1:3(1.0 2.0 3.0)" (ra->string (list->ra 'f64 '((-1 1)) '(1 2 3))))
  438. ; -----------------------
  439. ; ra-index-map!
  440. ; -----------------------
  441. (test-equal "#%2:2:3(((0 0) (0 1) (0 2)) ((1 0) (1 1) (1 2)))"
  442. (ra->string (let ((x (make-ra 0 2 3))) (ra-index-map! x (lambda x x)))))
  443. (test-equal "#%0(99)"
  444. (ra->string (let ((x (make-ra 0))) (ra-index-map! x (lambda x 99)))))
  445. ; -----------------------
  446. ; ra-dimensions, ra-shape
  447. ; -----------------------
  448. (test-equal '((-1 3) (0 4))
  449. (ra-shape (make-ra 'foo '(-1 3) 5)))
  450. (test-equal '((-1 3) 5)
  451. (ra-dimensions (make-ra 'foo '(-1 3) 5)))
  452. ; -----------------------
  453. ; raw prefix match
  454. ; -----------------------
  455. (let ((ra0 (ra-i 2 3))
  456. (ra1 (ra-i 2)))
  457. (test-equal
  458. "#%2:2:3((0 1 2) (3 4 5))-#%1:2(0 1)|"
  459. (call-with-output-string
  460. (lambda (o) (ra-slice-for-each 0 (lambda (ra0 ra1) (format o "~a-~a|" ra0 ra1)) ra0 ra1))))
  461. (test-equal
  462. "#%1:3(0 1 2)-#%0(0)|#%1:3(3 4 5)-#%0(1)|"
  463. (call-with-output-string
  464. (lambda (o) (ra-slice-for-each 1 (lambda (ra0 ra1) (format o "~a-~a|" ra0 ra1)) ra0 ra1))))
  465. (test-equal
  466. "#%0(0)-#%0(0)|#%0(1)-#%0(0)|#%0(2)-#%0(0)|#%0(3)-#%0(1)|#%0(4)-#%0(1)|#%0(5)-#%0(1)|"
  467. (call-with-output-string
  468. (lambda (o) (ra-slice-for-each 2 (lambda (ra0 ra1) (format o "~a-~a|" ra0 ra1)) ra0 ra1))))
  469. (test-assert
  470. (throws-exception? 'unset-len-for-dim
  471. (lambda () (ra-slice-for-each 3 (lambda (ra0 ra1) 0) ra0 ra1)))))
  472. ; -----------------------
  473. ; prefix match on item (not cell) variants
  474. ; -----------------------
  475. (test-assert
  476. (ra-equal?
  477. (string->ra "#%1:2(-2 -5)")
  478. (let ((ra0 (ra-copy #t (ra-i 2 3)))
  479. (ra1 (ra-copy #t (ra-i 2))))
  480. (ra-map! ra1 - ra0))))
  481. (test-assert
  482. (ra-equal?
  483. (string->ra "#%2:2:3((0 0 0) (-1 -1 -1))")
  484. (let ((ra0 (ra-copy #t (ra-i 2 3)))
  485. (ra1 (ra-copy #t (ra-i 2))))
  486. (ra-map! ra0 - ra1))))
  487. (test-assert
  488. (ra-equal?
  489. (string->ra "#%3:4:3:2(((0 0) (1 1) (2 2)) ((3 3) (4 4) (5 5)) ((6 6) (7 7) (8 8)) ((9 9) (10 10) (11 11)))")
  490. (ra-copy! (make-ra 99 4 3 2) (ra-i 4 3))))
  491. ; -----------------------
  492. ; ra-fold ra-fold*
  493. ; -----------------------
  494. ; these are slow :-/
  495. ; numpy (1.16.2) - time(np.sum(np.arange(1e8))) ~ 0.15 s
  496. ; octave (4.4.1) - a = time(); b=sum(0:999999999); time()-a ~ 3.1 s
  497. ; newra - ,time (ra-fold + 0 (ra-iota #e1e8)) ~ 4.7 s
  498. (test-equal (* #e1e6 (- #e1e6 1) 1/2) (ra-fold + 0 (ra-iota #e1e6)))
  499. (test-equal (* #e1e6 (- #e1e6 1) 1/2) (ra-fold + 0 (ra-i 100 100 100)))
  500. ; -----------------------
  501. ; ra-any ra-every
  502. ; -----------------------
  503. (test-assert (ra-every positive? (list->ra 1 '(1 2 3 4 5))))
  504. (test-equal -3 (ra-any (lambda (x) (and (negative? x) x)) (list->ra 1 '(1 2 -3 4 5))))
  505. (test-equal #(0 2) (ra-any (lambda (a i j) (and (zero? a) (vector i j)))
  506. (list->ra 2 '((3 4 0) (1 2 9)))
  507. (ra-iota #f)
  508. (ra-transpose (ra-iota #f) 1)))
  509. ; -----------------------
  510. ; inf dims I - (make-dim #f) = (make-dim #f 0 1)
  511. ; -----------------------
  512. (test-assert (ra-equal? (ra-i 3) (make-ra-root (make-dim #f) (c-dims 3))))
  513. ; inf index vector
  514. (test-assert (not (dim-len (make-dim #f))))
  515. ; make an array out of inf index vector
  516. (test-assert (ra-equal? (ra-i 3) (make-ra-root (make-dim #f) (c-dims 3))))
  517. ; make an inf array
  518. (let ((rainf (make-ra-root (make-dim #f) (c-dims #f))))
  519. (throws-exception? 'unset-len-for-dim (lambda () (ra-for-each pk rainf)))
  520. (test-equal #(10 8 6) (ra->array (ra-map! (make-ra #f 3) - (ra-iota 3 10 -1) rainf)))
  521. (test-equal #(-10 -8 -6) (ra->array (ra-map! (make-ra #f 3) - rainf (ra-iota 3 10 -1)))))
  522. (let ((rainf (ra-i #f 3 4)))
  523. (test-equal #(1204 1205 1206 1207) (ra->array (ra-copy #t (rainf 100 1))))
  524. (test-equal '(#f 3 4) (ra-dimensions rainf)))
  525. ; -----------------------
  526. ; inf dims II - (make-dim #f 0 0) = dead axis
  527. ; -----------------------
  528. (test-equal
  529. #2((11 12 13) (21 22 23))
  530. (ra->array
  531. (ra-map! (make-ra #f 2 3) +
  532. (make-ra-root #(10 20) (vector (make-dim 2 0 1) (make-dim #f 0 0)))
  533. (make-ra-root #(1 2 3) (vector (make-dim #f 0 0) (make-dim 3 0 1))))))
  534. ; using generalized transpose
  535. (test-equal
  536. #2((11 12 13) (21 22 23))
  537. (ra->array
  538. (ra-map! (make-ra #f 2 3) +
  539. (ra-iota 2 10 10)
  540. (ra-transpose (ra-iota 3 1 1) 1))))
  541. ; copying arrays with dead axes
  542. (let ((ra (ra-copy #t (ra-transpose (ra-i 3) 1))))
  543. (test-equal "#%2:d:3((0 1 2))" (ra->string ra))
  544. (test-equal #(0 1 2) (ra-root ra)))
  545. ; -----------------------
  546. ; the end.
  547. ; -----------------------
  548. (test-end "newra")
  549. (exit (test-runner-fail-count (test-runner-current)))