test-binary-search-tree.scm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. (import
  2. (except (rnrs base) error vector-map)
  3. (only (guile) lambda* λ error)
  4. ;; SRFI-64 for unit testing
  5. (srfi srfi-64)
  6. (binary-tree))
  7. (test-begin "tree-test")
  8. (test-group "tree-leaf?"
  9. (test-assert "tree-leaf returns #t for a leaf"
  10. (tree-leaf? (make-leaf 'something)))
  11. (test-assert "tree-leaf returns #f for a non-leaf - 01"
  12. (not (tree-leaf? (make-tree 'something
  13. (make-leaf 'something)
  14. (make-empty-tree)))))
  15. (test-assert "tree-leaf returns #f for a non-leaf - 02"
  16. (not (tree-leaf? (make-tree 'something
  17. (make-leaf 'something)
  18. (make-leaf 'something))))))
  19. (test-group "tree-member?"
  20. (test-assert "tree-member? returns #f for non-member"
  21. (let ([tree (make-tree 5
  22. (make-leaf 3)
  23. (make-leaf 7))])
  24. (not (tree-member? tree 4 <))))
  25. (test-assert "tree-member? returns #t for member at root node"
  26. (let ([tree (make-tree 5
  27. (make-leaf 3)
  28. (make-leaf 7))])
  29. (tree-member? tree 5 <)))
  30. (test-assert "tree-member? returns #t for member at leaf node left"
  31. (let ([tree (make-tree 5
  32. (make-leaf 3)
  33. (make-leaf 7))])
  34. (tree-member? tree 3 <)))
  35. (test-assert "tree-member? returns #t for member at leaf node right"
  36. (let ([tree (make-tree 5
  37. (make-leaf 3)
  38. (make-leaf 7))])
  39. (tree-member? tree 7 <)))
  40. (test-assert "tree-member? returns #f for any value if tree is empty"
  41. (not (tree-member? (make-empty-tree) 7 <))))
  42. (test-group "tree-insert"
  43. (test-assert "inserting an element into a tree makes it so that the element is in the tree"
  44. (tree-member? (tree-insert (make-empty-tree) 7 <) 7 <))
  45. (test-assert "inserting an element into a tree creates expected structure"
  46. (equal? (tree-insert (make-empty-tree) 7 <)
  47. (make-leaf 7)))
  48. (test-assert "inserting inserts at correct place in tree"
  49. (equal? (tree-insert (tree-insert (tree-insert (make-empty-tree) 5 <) 7 <) 3 <)
  50. (make-tree 5
  51. (make-leaf 3)
  52. (make-leaf 7)))))
  53. (test-group "make-complete-tree"
  54. (test-assert "make-complete-tree creates a tree of correct depth"
  55. (= (tree-depth (make-complete-tree "hello!" 2)) 2))
  56. (test-assert "make-complete-tree creates an empty tree for depth 0"
  57. (= (tree-depth (make-complete-tree "foo" 0)) 0))
  58. (test-assert "make-complete-tree creates a tree filled with the correct elements"
  59. (let ([elem 'myelem])
  60. (let iter ([tree° (make-complete-tree elem 3)])
  61. (cond
  62. [(tree-empty? tree°) #t]
  63. [(eq? (tree-value tree°) elem)
  64. (and (iter (tree-left tree°))
  65. (iter (tree-right tree°)))]
  66. [else #f]))))
  67. (test-assert "make-complete-tree creates a full binary tree"
  68. (let ([depth 2])
  69. (let iter ([tree° (make-complete-tree 'foo depth)]
  70. [depth° depth])
  71. (cond
  72. [(and (tree-empty? tree°) (= depth° 0)) #t]
  73. [else
  74. (cond
  75. [(tree-empty? tree°) #f]
  76. [else
  77. (and (iter (tree-left tree°) (- depth° 1))
  78. (iter (tree-right tree°) (- depth° 1)))])])))))
  79. (test-group "tree-depth"
  80. (test-assert "tree-depth gives zero for empty tree"
  81. (= (tree-depth (make-empty-tree))
  82. 0))
  83. (test-assert "tree-depth gives correct depth - 01"
  84. (= (tree-depth
  85. (make-tree 1
  86. (make-tree 1
  87. (make-leaf 1)
  88. (make-leaf 1))
  89. (make-tree 1
  90. (make-leaf 1)
  91. (make-leaf 1))))
  92. 3))
  93. (test-assert "tree-depth gives correct depth - 02"
  94. (= (tree-depth (make-leaf 1))
  95. 1)))
  96. (test-group "tree-size"
  97. (test-assert "tree-size gives correct result for empty tree"
  98. (= (tree-size (make-empty-tree))
  99. 0))
  100. (test-assert "tree-size gives correct result for non-empty tree"
  101. (= (tree-size
  102. (make-tree 1
  103. (make-tree 2
  104. (make-leaf 4)
  105. (make-leaf 5))
  106. (make-tree 3
  107. (make-leaf 6)
  108. (make-leaf 7))))
  109. 7))
  110. (test-assert "tree-size gives correct result for heavily unbalanced tree"
  111. (= (tree-size
  112. (make-tree
  113. 1
  114. (make-tree
  115. 2
  116. (make-tree
  117. 3
  118. (make-leaf 4)
  119. (make-empty-tree))
  120. (make-empty-tree))
  121. (make-empty-tree)))
  122. 4)))
  123. (test-group "tree-fold"
  124. (test-assert "tree-fold gives correct result for empty tree - 01"
  125. (eq? (tree-fold (λ (val acc) (or val acc))
  126. 'blub
  127. (make-empty-tree))
  128. 'blub))
  129. (test-assert "tree-fold gives correct result for empty tree - 02"
  130. (eq? (tree-fold (λ (val acc) (or val acc))
  131. #f
  132. (make-empty-tree))
  133. #f))
  134. (test-assert "tree-fold gives correct result for empty tree - 03"
  135. (eq? (tree-fold (λ (val acc) (and val acc))
  136. #t
  137. (make-empty-tree))
  138. #t))
  139. (test-assert "tree-fold gives correct result for non-empty tree - 01"
  140. (= (tree-fold (λ (val acc) (+ val acc))
  141. 0
  142. (make-tree 1
  143. (make-tree 1
  144. (make-tree 1
  145. (make-leaf 1)
  146. (make-leaf 1))
  147. (make-tree 1
  148. (make-leaf 1)
  149. (make-leaf 1)))
  150. (make-leaf 1)))
  151. 9))
  152. (test-assert "tree-fold gives correct result for non-empty tree - 02"
  153. (= (tree-fold +
  154. 0
  155. (make-tree 5
  156. (make-tree -3
  157. (make-tree 3
  158. (make-leaf 9)
  159. (make-leaf -6))
  160. (make-tree 1
  161. (make-leaf 0)
  162. (make-leaf 1)))
  163. (make-leaf 0)))
  164. 10)))
  165. (test-group "make-balanced-tree"
  166. (test-assert "make-balanced-tree creates an empty tree for size 0"
  167. (equal? (make-balanced-tree 'something 0)
  168. (make-empty-tree)))
  169. (test-assert "make-balanced-tree creates tree of correct size - 01"
  170. (= (tree-size (make-balanced-tree 'something 5))
  171. 5))
  172. (test-assert "make-balanced-tree creates tree of correct size - 02"
  173. (= (tree-size (make-balanced-tree 'something 100))
  174. 100))
  175. (test-assert "make-balanced-tree creates tree of correct depth - 01"
  176. (= (tree-depth (make-balanced-tree 'something 8))
  177. 4))
  178. (test-assert "make-balanced-tree creates tree of correct depth - 02"
  179. (= (tree-depth (make-balanced-tree 'something 1))
  180. 1))
  181. (test-assert "make-balanced-tree creates a tree filled with only the filling"
  182. (let ([val 'something])
  183. (tree-fold (λ (elem acc)
  184. (and (eq? elem val) acc))
  185. val
  186. (make-balanced-tree val 10)))))
  187. (test-end "tree-test")