test-list-utils.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. (import
  2. ;; unit tests
  3. (srfi srfi-64)
  4. ;; module to test
  5. (list-utils))
  6. (test-begin "list-utils-test")
  7. (test-group
  8. "list-prefixq-test"
  9. (test-assert "list-prefixq-00"
  10. (list-prefix? '(a b c) '(a b)))
  11. (test-assert "list-prefixq-01"
  12. (list-prefix? '(a b c) '(a b c)))
  13. (test-assert "list-prefixq-02"
  14. (not (list-prefix? '(a b c) '(a b c d))))
  15. (test-assert "list-prefixq-03"
  16. (list-prefix? '(a b c) '(a)))
  17. (test-assert "list-prefixq-04"
  18. (not (list-prefix? '(a b c) '(b))))
  19. (test-assert "list-prefixq-05"
  20. (list-prefix? '(a b c) '()))
  21. (test-assert "list-prefixq-06"
  22. (list-prefix? '() '())))
  23. (test-group
  24. "make-multiple-recursive-list-remover"
  25. (test-equal "make-multiple-recursive-list-remover-00"
  26. '(a b (c) (d))
  27. ((make-multiple-recursive-list-remover equal?)
  28. '(a b (c) (d (e f))) '(e f)))
  29. (test-equal "make-multiple-recursive-list-remover-01"
  30. '(a b (c) (d (e f)))
  31. ((make-multiple-recursive-list-remover equal?)
  32. '(a b (c) (e f) (d (e (e f) f))) '(e f)))
  33. ;; Define both bindings to have the same value, but they
  34. ;; are different objects, so eq? should be #f.
  35. (let ([num1 10]
  36. [num2 10])
  37. (test-equal "make-multiple-recursive-list-remover-02"
  38. ;; The number must not be removed.
  39. `(a b (c) (d (,num1)))
  40. ((make-multiple-recursive-list-remover eq?)
  41. ;; Put different bindings in the lists.
  42. `(a b (c) (d (,num1))) `(,num2))))
  43. ;; Define both bindings to have the same value, but they
  44. ;; are different objects, so eq? should be #f.
  45. (let ([num1 10]
  46. [num2 10])
  47. (test-equal "make-multiple-recursive-list-remover-03"
  48. ;; The number must not be removed, because eqv? does
  49. ;; not deal with lists.
  50. `(a b (c) (d (,num1)))
  51. ((make-multiple-recursive-list-remover eqv?)
  52. ;; Put different bindings in the lists.
  53. `(a b (c) (d (,num1))) `(,num2))))
  54. ;; Define both bindings to have the same value, but they
  55. ;; are different objects, so eq? should be #f.
  56. (test-equal "make-multiple-recursive-list-remover-04"
  57. ;; The number must be removed. eqv? can deal with
  58. ;; numbers.
  59. '(a b (c) (d))
  60. ((make-multiple-recursive-list-remover eqv?)
  61. ;; Put different bindings in the lists.
  62. '(a b (c) (d 10)) 10))
  63. ;; Define both bindings to have the same value, but they
  64. ;; are different objects, so eq? should be #f.
  65. (let ([num1 10]
  66. [num2 10])
  67. (test-equal "make-multiple-recursive-list-remover-05"
  68. ;; The number must be removed.
  69. `(a b (c) (d))
  70. ((make-multiple-recursive-list-remover equal?)
  71. ;; Put different bindings in the lists.
  72. `(a b (c) (d (,num1))) `(,num2)))))
  73. (test-group
  74. "make-multiple-list-remover"
  75. (test-equal "make-multiple-list-remover-00"
  76. ;; cannot remove in sublist
  77. '(a b (c) (d (e f)))
  78. ((make-multiple-list-remover equal?)
  79. '(a b (c) (d (e f))) '(e f)))
  80. (test-equal "make-multiple-list-remover-01"
  81. ;; can remove in top level
  82. '(a b (c) (d))
  83. ((make-multiple-list-remover equal?)
  84. '(a b (c) (d) (e f)) '(e f)))
  85. (test-equal "make-multiple-list-remover-02"
  86. ;; can remove in top level, but remains in sublist
  87. '(a b (c) (d (e f)))
  88. ((make-multiple-list-remover equal?)
  89. '(a b (c) (d (e f)) (e f)) '(e f)))
  90. ;; Define both bindings to have the same value, but they
  91. ;; are different objects, so eq? should be #f.
  92. (let ([a '(9 10)]
  93. ;; We must specify b not as literal, but a list created using the list
  94. ;; constructor, because Guile puts equal? lists defined in the same top
  95. ;; level expression into the same place in the store, which makes them
  96. ;; actually eq?.
  97. [b (list 9 10)])
  98. (test-equal "make-multiple-list-remover-03"
  99. `(1 2 (3) (4) ,a)
  100. ((make-multiple-list-remover eq?)
  101. `(1 2 (3) (4) ,a) b)))
  102. ;; Define both bindings to have the same value, but they
  103. ;; are different objects, so eq? should be #f.
  104. (let ([num1 10]
  105. [num2 10])
  106. (test-equal "make-multiple-list-remover-04"
  107. ((make-multiple-recursive-list-remover eqv?)
  108. ;; Put different bindings in the lists.
  109. `(a b (c) (d (,num1))) `(,num2))
  110. ;; The number must not be removed, because eqv? does
  111. ;; not deal with lists.
  112. `(a b (c) (d (,num1)))))
  113. (test-equal "make-multiple-list-remover-05"
  114. ;; removes on top level, but not in sub list
  115. '(a b (c) (d 10))
  116. ((make-multiple-list-remover eqv?)
  117. '(a b (c) (d 10)) 10))
  118. (let ([num1 10] [num2 10])
  119. (test-equal "make-multiple-list-remover-06"
  120. ((make-multiple-list-remover equal?)
  121. ;; Put different bindings in the lists.
  122. `(a b (c) (d (,num1))) `(,num2))
  123. ;; The number must be removed.
  124. `(a b (c) (d (,num1))))))
  125. (test-group
  126. "map-to-all-except-last-test"
  127. (test-equal "applies procedure to all but the last element -- 00"
  128. '(2 3 4 4)
  129. (map-to-all-except-last
  130. (λ (elem)
  131. (+ elem 1))
  132. '(1 2 3 4))))
  133. (test-end "list-utils-test")