/extra/quadtrees/quadtrees-tests.factor

http://github.com/abeaumont/factor · Factor · 241 lines · 216 code · 24 blank · 1 comment · 0 complexity · 2ff6195ec48525dd9d6a55dfca454bae MD5 · raw file

  1. ! (c) 2009 Joe Groff, see BSD license
  2. USING: accessors assocs kernel tools.test quadtrees math.rectangles sorting ;
  3. IN: quadtrees.tests
  4. : unit-bounds ( -- rect ) { -1.0 -1.0 } { 2.0 2.0 } <rect> ;
  5. : value>>key ( assoc value key -- assoc )
  6. pick set-at ; inline
  7. : delete>>key ( assoc key -- assoc )
  8. over delete-at ; inline
  9. [ T{ quadtree f T{ rect f { -1.0 -1.0 } { 2.0 2.0 } } { 0.0 -0.25 } "a" f f f f t } ]
  10. [
  11. unit-bounds <quadtree>
  12. "a" { 0.0 -0.25 } value>>key
  13. ] unit-test
  14. [ T{ quadtree f T{ rect f { -1.0 -1.0 } { 2.0 2.0 } } { 0.0 -0.25 } "b" f f f f t } ]
  15. [
  16. unit-bounds <quadtree>
  17. "a" { 0.0 -0.25 } value>>key
  18. "b" { 0.0 -0.25 } value>>key
  19. ] unit-test
  20. [ T{ quadtree f T{ rect f { -1.0 -1.0 } { 2.0 2.0 } } f f
  21. T{ quadtree f T{ rect f { -1.0 -1.0 } { 1.0 1.0 } } { -0.5 -0.75 } "c" f f f f t }
  22. T{ quadtree f T{ rect f { 0.0 -1.0 } { 1.0 1.0 } } { 0.0 -0.25 } "a" f f f f t }
  23. T{ quadtree f T{ rect f { -1.0 0.0 } { 1.0 1.0 } } f f f f f f t }
  24. T{ quadtree f T{ rect f { 0.0 0.0 } { 1.0 1.0 } } { 0.25 0.25 } "b" f f f f t }
  25. f
  26. } ] [
  27. unit-bounds <quadtree>
  28. "a" { 0.0 -0.25 } value>>key
  29. "b" { 0.25 0.25 } value>>key
  30. "c" { -0.5 -0.75 } value>>key
  31. ] unit-test
  32. [ T{ quadtree f T{ rect f { -1.0 -1.0 } { 2.0 2.0 } } f f
  33. T{ quadtree f T{ rect f { -1.0 -1.0 } { 1.0 1.0 } } { -0.5 -0.75 } "c" f f f f t }
  34. T{ quadtree f T{ rect f { 0.0 -1.0 } { 1.0 1.0 } } { 0.0 -0.25 } "a" f f f f t }
  35. T{ quadtree f T{ rect f { -1.0 0.0 } { 1.0 1.0 } } f f f f f f t }
  36. T{ quadtree f T{ rect f { 0.0 0.0 } { 1.0 1.0 } } f f
  37. T{ quadtree f T{ rect f { 0.0 0.0 } { 0.5 0.5 } } { 0.25 0.25 } "b" f f f f t }
  38. T{ quadtree f T{ rect f { 0.5 0.0 } { 0.5 0.5 } } { 0.75 0.25 } "d" f f f f t }
  39. T{ quadtree f T{ rect f { 0.0 0.5 } { 0.5 0.5 } } f f f f f f t }
  40. T{ quadtree f T{ rect f { 0.5 0.5 } { 0.5 0.5 } } f f f f f f t }
  41. }
  42. f
  43. } ] [
  44. unit-bounds <quadtree>
  45. "a" { 0.0 -0.25 } value>>key
  46. "b" { 0.25 0.25 } value>>key
  47. "c" { -0.5 -0.75 } value>>key
  48. "d" { 0.75 0.25 } value>>key
  49. ] unit-test
  50. [ "b" t ] [
  51. unit-bounds <quadtree>
  52. "a" { 0.0 -0.25 } value>>key
  53. "b" { 0.25 0.25 } value>>key
  54. "c" { -0.5 -0.75 } value>>key
  55. "d" { 0.75 0.25 } value>>key
  56. { 0.25 0.25 } swap at*
  57. ] unit-test
  58. [ f f ] [
  59. unit-bounds <quadtree>
  60. "a" { 0.0 -0.25 } value>>key
  61. "b" { 0.25 0.25 } value>>key
  62. "c" { -0.5 -0.75 } value>>key
  63. "d" { 0.75 0.25 } value>>key
  64. { 1.0 1.0 } swap at*
  65. ] unit-test
  66. [ { "a" "c" } ] [
  67. unit-bounds <quadtree>
  68. "a" { 0.0 -0.25 } value>>key
  69. "b" { 0.25 0.25 } value>>key
  70. "c" { -0.5 -0.75 } value>>key
  71. "d" { 0.75 0.25 } value>>key
  72. { -0.6 -0.8 } { 0.8 1.0 } <rect> swap in-rect natural-sort
  73. ] unit-test
  74. [ T{ quadtree f T{ rect f { -1.0 -1.0 } { 2.0 2.0 } } f f
  75. T{ quadtree f T{ rect f { -1.0 -1.0 } { 1.0 1.0 } } { -0.5 -0.75 } "c" f f f f t }
  76. T{ quadtree f T{ rect f { 0.0 -1.0 } { 1.0 1.0 } } { 0.0 -0.25 } "a" f f f f t }
  77. T{ quadtree f T{ rect f { -1.0 0.0 } { 1.0 1.0 } } f f f f f f t }
  78. T{ quadtree f T{ rect f { 0.0 0.0 } { 1.0 1.0 } } { 0.75 0.25 } "d" f f f f t }
  79. f
  80. } ] [
  81. unit-bounds <quadtree>
  82. "a" { 0.0 -0.25 } value>>key
  83. "b" { 0.25 0.25 } value>>key
  84. "c" { -0.5 -0.75 } value>>key
  85. "d" { 0.75 0.25 } value>>key
  86. { 0.25 0.25 } delete>>key
  87. prune-quadtree
  88. ] unit-test
  89. [ T{ quadtree f T{ rect f { -1.0 -1.0 } { 2.0 2.0 } } f f
  90. T{ quadtree f T{ rect f { -1.0 -1.0 } { 1.0 1.0 } } { -0.5 -0.75 } "c" f f f f t }
  91. T{ quadtree f T{ rect f { 0.0 -1.0 } { 1.0 1.0 } } { 0.0 -0.25 } "a" f f f f t }
  92. T{ quadtree f T{ rect f { -1.0 0.0 } { 1.0 1.0 } } f f f f f f t }
  93. T{ quadtree f T{ rect f { 0.0 0.0 } { 1.0 1.0 } } f f f f f f t }
  94. f
  95. } ] [
  96. unit-bounds <quadtree>
  97. "a" { 0.0 -0.25 } value>>key
  98. "b" { 0.25 0.25 } value>>key
  99. "c" { -0.5 -0.75 } value>>key
  100. "d" { 0.75 0.25 } value>>key
  101. { 0.25 0.25 } delete>>key
  102. { 0.75 0.25 } delete>>key
  103. prune-quadtree
  104. ] unit-test
  105. [ T{ quadtree f T{ rect f { -1.0 -1.0 } { 2.0 2.0 } } f f
  106. T{ quadtree f T{ rect f { -1.0 -1.0 } { 1.0 1.0 } } f f
  107. T{ quadtree f T{ rect f { -1.0 -1.0 } { 0.5 0.5 } } { -0.75 -0.75 } "b" f f f f t }
  108. T{ quadtree f T{ rect f { -0.5 -1.0 } { 0.5 0.5 } } f f f f f f t }
  109. T{ quadtree f T{ rect f { -1.0 -0.5 } { 0.5 0.5 } } f f f f f f t }
  110. T{ quadtree f T{ rect f { -0.5 -0.5 } { 0.5 0.5 } } { -0.25 -0.25 } "a" f f f f t }
  111. f
  112. }
  113. T{ quadtree f T{ rect f { 0.0 -1.0 } { 1.0 1.0 } } f f
  114. T{ quadtree f T{ rect f { 0.0 -1.0 } { 0.5 0.5 } } f f f f f f t }
  115. T{ quadtree f T{ rect f { 0.5 -1.0 } { 0.5 0.5 } } { 0.75 -0.75 } "f" f f f f t }
  116. T{ quadtree f T{ rect f { 0.0 -0.5 } { 0.5 0.5 } } { 0.25 -0.25 } "e" f f f f t }
  117. T{ quadtree f T{ rect f { 0.5 -0.5 } { 0.5 0.5 } } f f f f f f t }
  118. f
  119. }
  120. T{ quadtree f T{ rect f { -1.0 0.0 } { 1.0 1.0 } } f f
  121. T{ quadtree f T{ rect f { -1.0 0.0 } { 0.5 0.5 } } f f f f f f t }
  122. T{ quadtree f T{ rect f { -0.5 0.0 } { 0.5 0.5 } } { -0.25 0.25 } "c" f f f f t }
  123. T{ quadtree f T{ rect f { -1.0 0.5 } { 0.5 0.5 } } { -0.75 0.75 } "d" f f f f t }
  124. T{ quadtree f T{ rect f { -0.5 0.5 } { 0.5 0.5 } } f f f f f f t }
  125. f
  126. }
  127. T{ quadtree f T{ rect f { 0.0 0.0 } { 1.0 1.0 } } f f
  128. T{ quadtree f T{ rect f { 0.0 0.0 } { 0.5 0.5 } } { 0.25 0.25 } "g" f f f f t }
  129. T{ quadtree f T{ rect f { 0.5 0.0 } { 0.5 0.5 } } f f f f f f t }
  130. T{ quadtree f T{ rect f { 0.0 0.5 } { 0.5 0.5 } } f f f f f f t }
  131. T{ quadtree f T{ rect f { 0.5 0.5 } { 0.5 0.5 } } { 0.75 0.75 } "h" f f f f t }
  132. f
  133. }
  134. f
  135. } ] [
  136. unit-bounds <quadtree>
  137. "a" { -0.25 -0.25 } value>>key
  138. "b" { -0.75 -0.75 } value>>key
  139. "c" { -0.25 0.25 } value>>key
  140. "d" { -0.75 0.75 } value>>key
  141. "e" { 0.25 -0.25 } value>>key
  142. "f" { 0.75 -0.75 } value>>key
  143. "g" { 0.25 0.25 } value>>key
  144. "h" { 0.75 0.75 } value>>key
  145. prune-quadtree
  146. ] unit-test
  147. [ 8 ] [
  148. unit-bounds <quadtree>
  149. "a" { -0.25 -0.25 } value>>key
  150. "b" { -0.75 -0.75 } value>>key
  151. "c" { -0.25 0.25 } value>>key
  152. "d" { -0.75 0.75 } value>>key
  153. "e" { 0.25 -0.25 } value>>key
  154. "f" { 0.75 -0.75 } value>>key
  155. "g" { 0.25 0.25 } value>>key
  156. "h" { 0.75 0.75 } value>>key
  157. assoc-size
  158. ] unit-test
  159. [ {
  160. { { -0.75 -0.75 } "b" }
  161. { { -0.75 0.75 } "d" }
  162. { { -0.25 -0.25 } "a" }
  163. { { -0.25 0.25 } "c" }
  164. { { 0.25 -0.25 } "e" }
  165. { { 0.25 0.25 } "g" }
  166. { { 0.75 -0.75 } "f" }
  167. { { 0.75 0.75 } "h" }
  168. } ] [
  169. unit-bounds <quadtree>
  170. "a" { -0.25 -0.25 } value>>key
  171. "b" { -0.75 -0.75 } value>>key
  172. "c" { -0.25 0.25 } value>>key
  173. "d" { -0.75 0.75 } value>>key
  174. "e" { 0.25 -0.25 } value>>key
  175. "f" { 0.75 -0.75 } value>>key
  176. "g" { 0.25 0.25 } value>>key
  177. "h" { 0.75 0.75 } value>>key
  178. >alist natural-sort
  179. ] unit-test
  180. TUPLE: pointy-thing center ;
  181. [ {
  182. T{ pointy-thing f { 0 0 } }
  183. T{ pointy-thing f { 1 0 } }
  184. T{ pointy-thing f { 0 1 } }
  185. T{ pointy-thing f { 1 1 } }
  186. T{ pointy-thing f { 2 0 } }
  187. T{ pointy-thing f { 3 0 } }
  188. T{ pointy-thing f { 2 1 } }
  189. T{ pointy-thing f { 3 1 } }
  190. T{ pointy-thing f { 0 2 } }
  191. T{ pointy-thing f { 1 2 } }
  192. T{ pointy-thing f { 0 3 } }
  193. T{ pointy-thing f { 1 3 } }
  194. T{ pointy-thing f { 2 2 } }
  195. T{ pointy-thing f { 3 2 } }
  196. T{ pointy-thing f { 2 3 } }
  197. T{ pointy-thing f { 3 3 } }
  198. } ] [
  199. {
  200. T{ pointy-thing f { 3 1 } }
  201. T{ pointy-thing f { 2 3 } }
  202. T{ pointy-thing f { 3 2 } }
  203. T{ pointy-thing f { 0 1 } }
  204. T{ pointy-thing f { 2 2 } }
  205. T{ pointy-thing f { 1 1 } }
  206. T{ pointy-thing f { 3 0 } }
  207. T{ pointy-thing f { 3 3 } }
  208. T{ pointy-thing f { 1 3 } }
  209. T{ pointy-thing f { 2 1 } }
  210. T{ pointy-thing f { 0 0 } }
  211. T{ pointy-thing f { 2 0 } }
  212. T{ pointy-thing f { 1 0 } }
  213. T{ pointy-thing f { 0 2 } }
  214. T{ pointy-thing f { 1 2 } }
  215. T{ pointy-thing f { 0 3 } }
  216. } [ center>> ] swizzle
  217. ] unit-test