list-utils-test.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. (use-modules
  2. ;; SRFI 64 for unit testing facilities
  3. (srfi srfi-64)
  4. ;; utils - the code to be tested
  5. (utils list))
  6. (test-begin "list-utils-test")
  7. (test-group
  8. "map-asterisk"
  9. (test-equal "map*-1"
  10. '(1 4 (9 16) 25 36)
  11. (map* (lambda (n) (* n n))
  12. '(1 2 (3 4) 5 6)))
  13. (test-equal "map*-2"
  14. '("1" "4" ("9" "16") "25" "36")
  15. (map* (lambda (num)
  16. (number->string (* num num)))
  17. '(1 2 (3 4) 5 6))))
  18. (test-group
  19. "stringify*"
  20. (test-equal "stringify*-1"
  21. '("1" "2" ("3" "4") "5" "6")
  22. (stringify* '(1 2 (3 4) 5 6)))
  23. (test-equal "stringify*-2"
  24. '("1" "3.2" ("3" "#t") "5" "#f")
  25. (stringify* '(1 3.2 (3 #t) 5 #f))))
  26. (test-group
  27. "apply-multiple"
  28. (test-equal "apply-multiple-1"
  29. 8
  30. (apply-multiple
  31. (list (lambda (e) (+ e 1))
  32. (lambda (e) (* e 2)))
  33. 3))
  34. (test-equal "apply-multiple-2"
  35. 5
  36. (apply-multiple
  37. (list (lambda (e) (+ e 1))
  38. (lambda (e) (* e 2))
  39. (lambda (e) (- e 5)))
  40. 4)))
  41. (test-group
  42. "list-reduce"
  43. (test-equal "list-reduce-1"
  44. 120
  45. (list-reduce '(1 2 3 4 5) * 1))
  46. (test-equal "list-reduce-2"
  47. 5
  48. (list-reduce '(1 5 3 4 4)
  49. (lambda (a b) (max a b))
  50. 4)))
  51. (test-group
  52. "fisher-yates-shuffle"
  53. (test-assert "fisher-yates-shuffle keeps all elements of the list"
  54. ;; NOTE: This test tests code, which makes use of random integers. Although
  55. ;; unlikely, it could succeed an arbitrary number of times, before it fails,
  56. ;; if there is a bug in the implementation of the tested procedure.
  57. ;; FUTURE TODO: Perhaps it would be best to work with a random seed here,
  58. ;; which makes the results of the fisher-yates-shuffle deterministic.
  59. (let* ([elems-in-list '(1 2 3 4)]
  60. [shuffled-list (fisher-yates-shuffle elems-in-list)])
  61. (let loop ([elems-to-find elems-in-list])
  62. (cond
  63. [(null? elems-to-find) #t]
  64. [else
  65. (if (member (car elems-to-find) shuffled-list)
  66. (loop (cdr elems-to-find))
  67. #f)]))))
  68. (test-assert "fisher-yates-shuffle results in list of same length"
  69. (let ([list-to-shuffle '(1 2 3 4 5)])
  70. (= (length list-to-shuffle)
  71. (length (fisher-yates-shuffle '(1 2 3 4 5)))))))
  72. (test-group
  73. "accumulate"
  74. (test-equal "accumulate sum"
  75. 10
  76. (accumulate + 0 '(1 2 3 4)))
  77. (test-equal "accumulate list"
  78. '(1 2 3 4)
  79. (accumulate cons '() '(1 2 3 4))))
  80. (test-group
  81. "fold-right"
  82. (test-equal "fold-right"
  83. (/ 3 8)
  84. (fold-right / 1 '(1 2 3 4))))
  85. (test-group
  86. "fold-left"
  87. (test-equal "fold-left"
  88. (/ 8 3)
  89. (fold-left / 1 '(1 2 3 4))))
  90. (test-group
  91. "flatten"
  92. (test-equal "flatten with nested list should give flattened list"
  93. '(1 2 3 4 5 6)
  94. (flatten '((1) ((2 3 4) 5) 6)))
  95. (test-equal "flatten with flat list should not change the list"
  96. '(1 2 3 4 5 6)
  97. (flatten '(1 2 3 4 5 6))))
  98. (test-group
  99. "split-into-chunks-of-size-n"
  100. (test-equal "split-into-chunks-of-size-n splits and last sublist contains remaining"
  101. '((1 2 3) (4 5 6) (7))
  102. (split-into-chunks-of-size-n '(1 2 3 4 5 6 7) 3))
  103. (test-equal "split-into-chunks-of-size-n splits correctly if list length is divisable by n"
  104. '((1 2) (3 4) (5 6))
  105. (split-into-chunks-of-size-n '(1 2 3 4 5 6) 2))
  106. (test-equal "split-into-chunks-of-size-n does nothing if n greater than or equal to list length -- 1"
  107. '((1 2 3 4 5 6))
  108. (split-into-chunks-of-size-n '(1 2 3 4 5 6) 6))
  109. (test-equal "split-into-chunks-of-size-n does nothing if n greater than or equal to list length -- 2"
  110. '((1 2 3 4 5 6))
  111. (split-into-chunks-of-size-n '(1 2 3 4 5 6) 7)))
  112. (test-group
  113. "count"
  114. (test-equal "count with predicate that should never be true"
  115. 0
  116. (count (lambda (elem)
  117. (= 1 2))
  118. '(1 2 3 4 5)))
  119. (test-equal "count even numbers"
  120. 2
  121. (count (lambda (elem)
  122. (= (remainder elem 2) 0))
  123. '(1 2 3 4 5)))
  124. (test-equal "count with tautology predicate"
  125. 5
  126. (count (lambda (elem)
  127. (= 1 1))
  128. '(1 2 3 4 5))))
  129. (test-group
  130. "list-range"
  131. (test-equal "list-range of empty list is empty list"
  132. '()
  133. (list-range '() 0 4))
  134. (test-equal "list-range of complete list"
  135. '(1 3 5 2 4)
  136. (list-range '(1 3 5 2 4) 0 5))
  137. (test-equal "list-range usual case -- 1"
  138. '(3)
  139. (list-range '(1 3 5 2 4) 1 2))
  140. (test-equal "list-range usual case -- 2"
  141. '(3 5)
  142. (list-range '(1 3 5 2 4) 1 3)))
  143. (test-group
  144. "take-up-to"
  145. (test-equal "take-up-to with sufficient elements"
  146. '(1 2 3)
  147. (take-up-to 3 '(1 2 3 4 5 6 7)))
  148. (test-equal "take-up-to with insufficient elements"
  149. '(1 2 3 4)
  150. (take-up-to 6 '(1 2 3 4))))
  151. (test-group
  152. "drop-up-to"
  153. (test-equal "drop-up-to with sufficient elements"
  154. '(4 5 6 7)
  155. (drop-up-to 3 '(1 2 3 4 5 6 7)))
  156. (test-equal "drop-up-to with insufficient elements should give empty list"
  157. '()
  158. (drop-up-to 6 '(1 2 3 4))))
  159. (test-group
  160. "range"
  161. (test-equal "range with no step"
  162. '(0 1 2 3 4 5 6 7 8 9)
  163. (range 0 10))
  164. (test-equal "range with step 2"
  165. '(0 2 4 6 8)
  166. (range 0 10 2)))
  167. (test-end "list-utils-test")