test-from.scm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ; Tests for (from) and (amend!) from (ploy ploy).
  2. ; (c) Daniel Llorens - 2012-2014
  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 (ploy ploy) (ploy slices) (ploy basic) (srfi srfi-9) (ice-9 match)
  8. (srfi srfi-1) (srfi srfi-11) (ploy test) (srfi srfi-26) (srfi srfi-8))
  9. (define (complement-list k l)
  10. (let loop ((i 0) (l l) (r '()))
  11. (cond ((= i k) r)
  12. ((null? l) (append! r (iota (- k i) i)))
  13. ((< i (car l)) (loop (+ (car l) 1) (cdr l) (append! r (iota (- (car l) i) i))))
  14. ((= i (car l)) (loop (+ i 1) (cdr l) r))
  15. (else (error "bad arguments")))))
  16. ; further generalization of array-fill! / array-set! / array-copy! / array-amend!.
  17. (define (subrank-copy*! b z . i)
  18. (let ((li (length i)))
  19. (cond ((= (rank b) li)
  20. (apply array-set! b z i))
  21. ((zero? (rank z))
  22. (array-fill! (apply array-from b i) z))
  23. ; this is the general case. The others are needed b/c ply & array-from pass x not #0(x) for rank-0 arrays.
  24. (else
  25. (ply (verb (cut array-amend! <> <>) #f (rank z) '_)
  26. (apply array-from b i) z)))))
  27. ; a variant that doesn't ply the write argument.
  28. (define (subrank-copy! b z . i)
  29. (let ((li (length i)))
  30. (if (= (rank b) li)
  31. ; this case b/c (array-from) will return x not #0(x).
  32. (apply array-set! b z i)
  33. (apply array-amend! b (extend-left z (drop ($ b) li)) i))))
  34. ; ----------------
  35. ; just rank-1 indices.
  36. ; ----------------
  37. (define A (i. 4))
  38. (amend! A #(a b c d))
  39. (T-msg "bad amend! 0-0" A #(a b c d))
  40. (define A (i. 4))
  41. (amend! A #(a b c) #(3 0 2))
  42. (T-msg "bad amend! 0-1" A #(b 1 c a))
  43. (define A (i. 2 2))
  44. (amend! A #2((a b) (c d)))
  45. (T-msg "bad amend! 0-2" A #2((a b) (c d)))
  46. (define A (i. 4 4))
  47. (amend! A #2((a b) (c d)) #(0 1) #(0 1))
  48. (T-msg "bad amend! 0-3" A #2((a b 2 3) (c d 6 7) (8 9 10 11) (12 13 14 15)))
  49. (define A (i. 4 4))
  50. (amend! A #2((a b) (c d)) #(1 3) #(0 2))
  51. (T-msg "bad amend! 0-4" A #2((0 1 2 3) (a 5 b 7) (8 9 10 11) (c 13 d 15)))
  52. (define A (i. 2 2 2))
  53. (amend! A #3(((a b) (c d)) ((e f) (g h))) #(1 0) #(1 0) #(1 0))
  54. (T-msg "bad amend! 0-5" A (reverse. (reverse. (reverse. #3(((a b) (c d)) ((e f) (g h))) 0) 1) 2))
  55. ; ----------------
  56. ; rank-1 and rank-0 indices.
  57. ; ----------------
  58. (define A (i. 4 4))
  59. (amend! A #(a b c d) #(3 2 0 1) 1)
  60. (T-msg "bad amend! 1-0" A #2((0 c 2 3) (4 d 6 7) (8 b 10 11) (12 a 14 15)))
  61. (define A (i. 2 2))
  62. (amend! A #2((a)) #(0) #(1))
  63. (T-msg "bad amend! 1-1" A #2((0 a) (2 3)))
  64. (define A (i. 2 2))
  65. (amend! A #(a) 0 #(1))
  66. (T-msg "bad amend! 1-2" A #2((0 a) (2 3)))
  67. (define A (i. 2 2))
  68. (amend! A #(a) #(0) 1)
  69. (T-msg "bad amend! 1-3" A #2((0 a) (2 3)))
  70. (define A (i. 2 2))
  71. (amend! A 'a 0 1)
  72. (T-msg "bad amend! 1-4" A #2((0 a) (2 3)))
  73. (define A (i. 2 2))
  74. (amend! A #0(a) 0 1)
  75. (T-msg "bad amend! 1-5" A #2((0 a) (2 3)))
  76. ; ----------------
  77. ; using shortcuts.
  78. ; ----------------
  79. (define A (i. 2 2))
  80. (amend! A #2((a b)) #(0) #t)
  81. (T-msg "bad amend! 2-0" A #2((a b) (2 3)))
  82. (define A (i. 2 2))
  83. (amend! A #2((a) (b)) #t #(0))
  84. (T-msg "bad amend! 2-1" A #2((a 1) (b 3)))
  85. (define A (i. 2 2))
  86. (amend! A #(a b) 0 #t)
  87. (T-msg "bad amend! 2-2" A #2((a b) (2 3)))
  88. (define A (i. 2 2))
  89. (amend! A #(a b) #t 0)
  90. (T-msg "bad amend! 2-3" A #2((a 1) (b 3)))
  91. ; -------------------
  92. ; indices with rank>0
  93. ; -------------------
  94. (define A (i. 4))
  95. (from A #2((3 1) (2 0)))
  96. (amend! A #2((a b) (c d)) #2((3 1) (2 0)))
  97. (T-msg "bad amend! 3-0" A #(d b c a))
  98. ; -------------------
  99. ; shortcuts and indices with rank>0. This uses backward != forward.
  100. ; -------------------
  101. ; see (5-6).
  102. (define A (i. 1 4))
  103. (from A #t #2((3 1) (2 0)))
  104. (amend! A #3(((a b) (c d))) #t #2((3 1) (2 0)))
  105. (T-msg "bad amend! 4-0" A #2((d b c a)))
  106. ; -------------------
  107. ; subrank-copy!
  108. ; -------------------
  109. (define A (i. 2 3))
  110. (subrank-copy! A #(a b c))
  111. (T-msg "bad subrank-copy! 0" A #2((a b c) (a b c)))
  112. (define A (i. 2 3))
  113. (subrank-copy! A 'a)
  114. (T-msg "bad subrank-copy! 1" A #2((a a a) (a a a)))
  115. (define A (i. 2 3))
  116. (subrank-copy*! A #0(a))
  117. (T-msg "bad subrank-copy! 2" A #2((#0(a) #0(a) #0(a)) (#0(a) #0(a) #0(a)))) ; @TODO Unsettled (see 4).
  118. (define A (i. 2 3))
  119. (subrank-copy! A #0(a))
  120. (T-msg "bad subrank-copy! 3" A #2((a a a) (a a a))) ; @TODO Unsettled (see 3)
  121. (define A (i. 2 3))
  122. (subrank-copy! A #(a b c) 1)
  123. (T-msg "bad subrank-copy! 4" A #2((0 1 2) (a b c)))
  124. (define A (i. 2 3))
  125. (subrank-copy! A 'a 1)
  126. (T-msg "bad subrank-copy! 5" A #2((0 1 2) (a a a)))
  127. (define A (i. 2 3))
  128. (subrank-copy! A 'a 1 1)
  129. (T-msg "bad subrank-copy! 6" A #2((0 1 2) (3 a 5)))
  130. (define A (i. 2 3))
  131. (subrank-copy! A #(a b) 1 1)
  132. (T-msg "bad subrank-copy! 7" A #2((0 1 2) (3 #(a b) 5)))
  133. ; -------------------
  134. ; fill has rank below shape remainder.
  135. ; -------------------
  136. (define A (i. 4))
  137. (amend! A 'a)
  138. (T-msg "bad amend! 5-0" A #(a a a a))
  139. (define A (i. 4 4))
  140. (amend! A 'a)
  141. (T-msg "bad amend! 5-1" A (reshape 'a 4 4))
  142. (define A (i. 4 4))
  143. (amend! A #(a b c d))
  144. (T-msg "bad amend! 5-2" A (reshape #(a b c d) 4 4))
  145. (define A (i. 4 4))
  146. (amend! A #2((a b c) (d e f) (g h i) (j k l)) #t (J 3))
  147. (T-msg "bad amend! 5-3" A #2((a b c 3) (d e f 7) (g h i 11) (j k l 15)))
  148. (define A (i. 4 4))
  149. (amend! A #(a b c) #t (J 3))
  150. (T-msg "bad amend! 5-4" A #2((a b c 3) (a b c 7) (a b c 11) (a b c 15)))
  151. ; Like (3-0), but extending to expected-z-shape.
  152. (define A (i. 1 4))
  153. (from A #t #2((3 1) (2 0)))
  154. (amend! A #2((a b) (c d)) #t #2((3 1) (2 0)))
  155. (T-msg "bad amend! 5-6" A #2((d b c a)))
  156. ; -------------------
  157. ; regression tests for amend!
  158. ; -------------------
  159. ; These work.
  160. (T #(a b) (amend! (make-array 0 2) #(a b) #(0 1)))
  161. (T #(a b) (amend! (make-array 0 2) #(a b)))
  162. (T #(a a) (amend! (make-array 0 2) 'a))
  163. (T #2((a b) (a b)) (amend! (make-array 0 2 2) #(a b)))
  164. (T #2((a b) (a b)) (amend! (make-array 0 2 2) #(a b) #(0 1)))
  165. ; These go through to prefix-amend!, but still require extend-left.
  166. (T #(a a) (amend! (make-array 0 2) 'a #(0 1)))
  167. (T #(9 9 9) (amend! (make-array 9 3) 'a #()))
  168. (T #2((a 0) (a 0)) (amend! (make-array 0 2 2) 'a #(0 1) #(0)))