test-as-array.scm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ; Test array memory order operations and array conversion.
  2. ; (c) Daniel Llorens - 2013
  3. ; This library is free software; you can redistribute it and/or modify it under
  4. ; the terms of the GNU General Public License as published by the Free
  5. ; Software Foundation; either version 3 of the License, or (at your option) any
  6. ; later version.
  7. (import (srfi srfi-1) (srfi srfi-26) (ploy as-array) (ploy slices) (ploy test)
  8. (ploy basic))
  9. ; ---------------------------------------------------------------------
  10. ; assignable?
  11. ; ---------------------------------------------------------------------
  12. (assert (assignable? (i. 3 3 3)))
  13. (assert (assignable? (i. 3 3)))
  14. (assert (assignable? (i. 3)))
  15. (assert (assignable? (i.)))
  16. (assert (assignable? (reverse. (i. 3) 0)))
  17. (assert (assignable? (reverse. (i. 3 3) 0)))
  18. (assert (assignable? (reverse. (i. 3 3) 1)))
  19. (assert (assignable? (reverse. (reverse. (i. 3 3) 0) 1)))
  20. (assert (assignable? (reverse. (reverse. (i. 3 3) 1) 0)))
  21. (assert (assignable? (reshape (i. 3 3) 3 3 1)))
  22. (assert (assignable? (reshape (i. 3 3) 3 3 2)))
  23. (assert (not (assignable? (reshape (i. 3 3) 2 3 3))))
  24. (assert (not (assignable? (reshape 3 4 4 4))))
  25. (assert (not (assignable? (reshape 0 3))))
  26. (assert (assignable? (reshape 0 1)))
  27. (assert (assignable? (reshape 0 0)))
  28. (assert (assignable? (as-array (reshape 0 3) #:order 'c)))
  29. (assert (not (assignable? (cant (i. 10) 3 1))))
  30. (assert (not (assignable? (cant (i. 10) 3 2))))
  31. (assert (assignable? (cant (i. 10) 3 3)))
  32. ; ---------------------------------------------------------------------
  33. ; as-array
  34. ; ---------------------------------------------------------------------
  35. (define (array->nested-vector A)
  36. (case (rank A)
  37. ((0) A)
  38. ((1) A)
  39. (else (array->nested-vector (explode 1 A)))))
  40. (define* (test-as-array-with-options #:key (post-test (lambda x #t)) (order #f))
  41. (let* ((test (lambda* (ref A msg #:key rank)
  42. (assert (equal? ($ ref) (arraylike-dimensions A #:rank rank))
  43. (format #f "~a, rank ~a: arraylike-dimensions" msg rank))
  44. (let ((B (as-array A #:order order #:rank rank)))
  45. (assert (equal? ref B) msg)
  46. (assert (or (and rank (zero? rank)) (post-test B))
  47. (format #f "~a, rank ~a: post-test failed" msg rank))))))
  48. ; common cases.
  49. (for-each
  50. (lambda (A msg)
  51. (let ((a (array->list A)))
  52. (test A a "list")
  53. (for-each (lambda (i) (test (list->array i a) a msg #:rank i))
  54. (iota (rank A) 1))))
  55. (list (i. 2) (i. 2 3) (i. 2 3 4))
  56. (list "list" "list-list" "list-list-list"))
  57. (for-each
  58. (lambda (A msg)
  59. (let ((a (array->nested-vector A))
  60. (B (array-copy A)))
  61. (test A a "vector")
  62. (for-each (lambda (i) (test (ply (verb array->nested-vector '() (- i)) A) a msg #:rank i))
  63. (iota (rank A) 1))
  64. (assert (array-equal? A B))))
  65. (list (i. 2) (i. 2 3) (i. 2 3 4))
  66. (list "vector" "vector-vector" "vector-vector-vector"))
  67. (test (i. 2 3) (array->list (explode 1 (i. 2 3))) "list-vector")
  68. (test (explode 1 (i. 2 3)) (array->list (explode 1 (i. 2 3))) "list-vector" #:rank 1)
  69. (test (i. 2 3) (list->array 1 (array->list (i. 2 3))) "vector-list")
  70. (test (list->array 1 (array->list (i. 2 3))) (list->array 1 (array->list (i. 2 3))) "vector-list" #:rank 1)
  71. (test (i. 2 3 4) (ply array->list (explode 1 (explode 1 (i. 2 3 4)))) "vector-list-vector")
  72. (test (i. 2 3 4) (array->list (explode 1 (list->array 2 (array->list (i. 2 3 4))))) "list-vector-list")
  73. (test (i. 2 3 4) (explode 2 (i. 2 3 4)) "vector-array2")
  74. (test (i. 2 3 4) (explode 1 (i. 2 3 4)) "array2-vector")
  75. (test (i. 2 3 4) (array->list (explode 2 (i. 2 3 4))) "list-array2")
  76. (test (i. 2 3 4) (ply array->list (explode 1 (i. 2 3 4))) "array2-list")
  77. ; corner cases.
  78. (test #() '() "empty list")
  79. (test #() #() "empty vector")
  80. (test (make-array 0 0 0) (make-array 0 0 0) "empty 0 0 2-array")
  81. (test (make-array 0 1 0) (make-array 0 1 0) "empty 1 0 2-array")
  82. (test (make-array 0 0 1) (make-array 0 0 1) "empty 0 1 2-array")
  83. ; make-shared-array to output of size 0 doesn't have the same shared-array-root as the original array. I'd say this is a bug in shared-array-root.
  84. ; atoms of empty arrays are assumed to be scalars.
  85. (test #() (from (explode 2 (i. 2 3 4)) (J 0)) "empty view of nonempty array")
  86. ; on non-empty arrays, we can tell the rank of the atom.
  87. (test (from (i. 2 3 4) (J 1)) (from (explode 2 (i. 2 3 4)) (J 1)) "empty view of nonempty array")
  88. ))
  89. (test-as-array-with-options)
  90. (test-as-array-with-options #:order 'fortran #:post-test fortran-order?)
  91. (test-as-array-with-options #:order 'c #:post-test c-order?)
  92. ; very typical cases.
  93. (let ((as-f64 (cut as-array <> #:type 'f64)))
  94. (T #2f64((1 2 3))
  95. (as-f64 #(#(1 2 3)))
  96. (as-f64 #2f64((1 2 3)))
  97. (as-f64 '((1 2 3)))
  98. (as-f64 #((1 2 3)))
  99. (as-f64 '(#(1 2 3)))
  100. (as-f64 #(#f64(1 2 3)))
  101. (as-f64 '(#f64(1 2 3)))))
  102. ; pass through cases.
  103. (define (test-pass-through msg pass? A . args)
  104. (assert (eqv? pass? (eq? (shared-array-root A) (shared-array-root (apply as-array A args))))
  105. (format #f "~a: expected ~a" msg (if pass? "success" "failure"))))
  106. (test-pass-through "c-order passthrough" #t (i. 2 3))
  107. (test-pass-through "c-order passthrough" #f (i. 2 3) #:type 'f64)
  108. (test-pass-through "c-order passthrough" #t (as-array (i. 2 3) #:type 'f64) #:type 'f64)
  109. (test-pass-through "c-order passthrough" #t (i. 2 3) #:order 'c)
  110. (test-pass-through "c-order passthrough" #t (transpose-array (i. 2 3) 1 0) #:order 'fortran)
  111. (test-pass-through "c-order passthrough" #t (i. 2 3))
  112. (test-pass-through "c-order passthrough" #t (transpose-array (i. 2 3) 1 0))
  113. (test-pass-through "c-order passthrough" #f (i. 2 3) #:order 'fortran)
  114. (test-pass-through "c-order passthrough" #f (transpose-array (i. 2 3) 1 0) #:order 'c)
  115. (test-pass-through "c-order passthrough" #f (i. 2 3) #:order 'c #:unique? #t)
  116. (test-pass-through "c-order passthrough" #f (transpose-array (i. 2 3) 1 0) #:order 'fortran #:unique? #t)
  117. (test-pass-through "c-order passthrough" #f (i. 2 3) #:unique? #t)
  118. (test-pass-through "c-order passthrough" #f (transpose-array (i. 2 3) 1 0) #:unique? #t)
  119. ; @TODO Check ra-large.H:is_c_order() against these.
  120. ;; (c:is_c_order? (as-array (i. 2 3) #:order 'fortran))
  121. ;; (c:is_c_order? (as-array (i. 2 3) #:order 'c))
  122. ; FIXED array contents should be redefined to return memory also with fortran-order array, or renamed.
  123. (array-contents (as-array (i. 2 3 4) #:order 'c))
  124. ; #(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23)
  125. (array-contents (as-array (i. 2 3 4) #:order 'fortran))
  126. ; #f