1
exercism fetch scheme list-ops

list-ops-test.scm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
;; Load SRFI-64 lightweight testing specification
(use-modules (srfi srfi-64))
(use-modules (srfi srfi-1))

;; Suppress log file output. To write logs, comment out the following line:
(module-define! (resolve-module '(srfi srfi-64)) 'test-log-to-file #f)

;; Require list-ops impl
(add-to-load-path (dirname (current-filename)))
(use-modules (list-ops))

;;; Begin test suite
(test-begin "list-ops-test")

(test-eqv "length of empty list"
           0
           (my-length '()))

(test-eqv "length of normal list"
           4
           (my-length '(1 3 5 7)))

(test-eqv "length of huge list"
          1000000
          (my-length (list-tabulate 1000000 values)))

(test-equal "reverse of empty list"
            '()
            (my-reverse '()))

(test-equal "reverse of normal list"
            '(7 5 3 1)
            (my-reverse '(1 3 5 7)))

(test-equal "reverse of huge list"
            (list-tabulate 1000000 (lambda (x) (- 999999 x)))
            (my-reverse (list-tabulate 1000000 values)))

(define (inc x) (+ 1 x))

(test-equal "map of empty list"
            '()
            (my-map inc '()))

(test-equal "map of normal list"
            '(2 3 4 5)
            (my-map inc '(1 2 3 4)))

(test-equal "map of huge list"
            (list-tabulate 1000000 (lambda (x) (+ x 1)))
            (my-map inc (list-tabulate 1000000 values)))

(test-equal "filter of empty list"
            '()
            (my-filter odd? '()))

(test-equal "filter of normal list"
            '(1 3)
            (my-filter odd? '(1 2 3 4)))

(test-equal "filter of huge list"
            (filter odd? (list-tabulate 1000000 values))
            (my-filter odd? (list-tabulate 1000000 values)))

(test-eqv "fold of empty list"
          0
          (my-fold + 0 '()))

(test-eqv "fold of normal list"
          7
          (my-fold + -3 '(1 2 3 4)))

(test-eqv "fold of huge list"
          (fold + 0 (list-tabulate 1000000 values))
          (my-fold + 0 (list-tabulate 1000000 values)))

(test-eqv "fold with non-commutative function"
          0
          (my-fold (lambda (x acc) (- acc x))
                   10
                   '(1 2 3 4)))

(test-equal "append of empty lists"
            '()
            (my-append '() '()))

(test-equal "append of empty and non-empty list"
            '(1 2 3 4)
            (my-append '() '(1 2 3 4)))

(test-equal "append of non-empty and empty list"
            '(1 2 3 4)
            (my-append '(1 2 3 4) '()))

(test-equal "append of non-empty lists"
            '(1 2 3 4 5)
            (my-append '(1 2 3) '(4 5)))

(test-equal "append of huge lists"
            (list-tabulate 2000000 values)
            (my-append (list-tabulate 1000000 values)
                       (list-tabulate 1000000 (lambda (x) (+ x 1000000)))))

(test-equal "concatenate of empty list of lists"
            '()
            (my-concatenate '()))

(test-equal "concatenate of normal list of lists"
            '(1 2 3 4 5 6)
            (my-concatenate '((1 2) (3) () (4 5 6))))

(test-equal "concatenate of huge list of small lists"
            (list-tabulate 1000000 values)
            (my-concatenate (list-tabulate 1000000 list)))

(test-equal "concatenate of small list of huge lists"
            (list-tabulate 1000000 values)
            (my-concatenate
             (list-tabulate 10 (lambda (i)
                                 (list-tabulate 100000
                                                (lambda (j) (+ (* 100000 i) j)))))))

(test-end "list-ops-test")
;;; End test suite