|
| 1 | +; Test module system functionality |
| 2 | +; This test demonstrates multi-file module loading and interop |
| 3 | + |
| 4 | +; ============================================================================ |
| 5 | +; Load standard library and modules from separate files |
| 6 | +; ============================================================================ |
| 7 | +(load "lisp/stdlib.lisp") |
| 8 | +(load "lisp/modules/math.lisp") |
| 9 | +(load "lisp/modules/predicates.lisp") |
| 10 | +(load "lisp/modules/greetings.lisp") |
| 11 | + |
| 12 | +; ============================================================================ |
| 13 | +; Test 1: Basic qualified access |
| 14 | +; ============================================================================ |
| 15 | +(println "Test 1: Qualified access (module/function)") |
| 16 | + |
| 17 | +(if (= (math/square 5) 25) |
| 18 | + (println " math/square: passed") |
| 19 | + (println " math/square: FAILED")) |
| 20 | + |
| 21 | +(if (= (math/cube 3) 27) |
| 22 | + (println " math/cube: passed") |
| 23 | + (println " math/cube: FAILED")) |
| 24 | + |
| 25 | +(if (= (math/abs -42) 42) |
| 26 | + (println " math/abs: passed") |
| 27 | + (println " math/abs: FAILED")) |
| 28 | + |
| 29 | +(if (= (math/double 7) 14) |
| 30 | + (println " math/double: passed") |
| 31 | + (println " math/double: FAILED")) |
| 32 | + |
| 33 | +(if (= (math/negate 5) -5) |
| 34 | + (println " math/negate: passed") |
| 35 | + (println " math/negate: FAILED")) |
| 36 | + |
| 37 | +; ============================================================================ |
| 38 | +; Test 2: Selective import |
| 39 | +; ============================================================================ |
| 40 | +(println "") |
| 41 | +(println "Test 2: Selective import") |
| 42 | + |
| 43 | +(import math (square cube)) |
| 44 | + |
| 45 | +(if (= (square 6) 36) |
| 46 | + (println " imported square: passed") |
| 47 | + (println " imported square: FAILED")) |
| 48 | + |
| 49 | +(if (= (cube 4) 64) |
| 50 | + (println " imported cube: passed") |
| 51 | + (println " imported cube: FAILED")) |
| 52 | + |
| 53 | +; Original qualified access still works after import |
| 54 | +(if (= (math/abs -10) 10) |
| 55 | + (println " qualified still works: passed") |
| 56 | + (println " qualified still works: FAILED")) |
| 57 | + |
| 58 | +; ============================================================================ |
| 59 | +; Test 3: Predicates module |
| 60 | +; ============================================================================ |
| 61 | +(println "") |
| 62 | +(println "Test 3: Predicates module") |
| 63 | + |
| 64 | +(if (predicates/positive? 5) |
| 65 | + (println " predicates/positive?: passed") |
| 66 | + (println " predicates/positive?: FAILED")) |
| 67 | + |
| 68 | +(if (predicates/negative? -3) |
| 69 | + (println " predicates/negative?: passed") |
| 70 | + (println " predicates/negative?: FAILED")) |
| 71 | + |
| 72 | +(if (predicates/zero? 0) |
| 73 | + (println " predicates/zero?: passed") |
| 74 | + (println " predicates/zero?: FAILED")) |
| 75 | + |
| 76 | +(if (predicates/even? 4) |
| 77 | + (println " predicates/even?: passed") |
| 78 | + (println " predicates/even?: FAILED")) |
| 79 | + |
| 80 | +(if (predicates/odd? 7) |
| 81 | + (println " predicates/odd?: passed") |
| 82 | + (println " predicates/odd?: FAILED")) |
| 83 | + |
| 84 | +; ============================================================================ |
| 85 | +; Test 4: Import from multiple modules |
| 86 | +; ============================================================================ |
| 87 | +(println "") |
| 88 | +(println "Test 4: Import from multiple modules") |
| 89 | + |
| 90 | +(import predicates (even? positive?)) |
| 91 | + |
| 92 | +(if (even? 10) |
| 93 | + (println " imported even?: passed") |
| 94 | + (println " imported even?: FAILED")) |
| 95 | + |
| 96 | +(if (positive? 42) |
| 97 | + (println " imported positive?: passed") |
| 98 | + (println " imported positive?: FAILED")) |
| 99 | + |
| 100 | +; ============================================================================ |
| 101 | +; Test 5: Composing functions from different modules |
| 102 | +; ============================================================================ |
| 103 | +(println "") |
| 104 | +(println "Test 5: Composing module functions") |
| 105 | + |
| 106 | +; square of absolute value |
| 107 | +(def square-abs (fn (x) (math/square (math/abs x)))) |
| 108 | + |
| 109 | +(if (= (square-abs -5) 25) |
| 110 | + (println " square-abs: passed") |
| 111 | + (println " square-abs: FAILED")) |
| 112 | + |
| 113 | +; check if double is positive |
| 114 | +(def double-positive? (fn (x) (predicates/positive? (math/double x)))) |
| 115 | + |
| 116 | +(if (double-positive? 3) |
| 117 | + (println " double-positive?: passed") |
| 118 | + (println " double-positive?: FAILED")) |
| 119 | + |
| 120 | +; ============================================================================ |
| 121 | +; Test 6: Module values |
| 122 | +; ============================================================================ |
| 123 | +(println "") |
| 124 | +(println "Test 6: Module as value") |
| 125 | + |
| 126 | +; Modules are first-class values |
| 127 | +(def m math) |
| 128 | +(println (list " module value: " m)) |
| 129 | + |
| 130 | +; ============================================================================ |
| 131 | +; Test 7: Nested function calls with modules |
| 132 | +; ============================================================================ |
| 133 | +(println "") |
| 134 | +(println "Test 7: Nested calls") |
| 135 | + |
| 136 | +(if (= (math/square (math/abs -5)) 25) |
| 137 | + (println " nested calls: passed") |
| 138 | + (println " nested calls: FAILED")) |
| 139 | + |
| 140 | +(if (= (math/cube (math/double 2)) 64) ; cube(double(2)) = cube(4) = 64 |
| 141 | + (println " cube of double: passed") |
| 142 | + (println " cube of double: FAILED")) |
| 143 | + |
| 144 | +; ============================================================================ |
| 145 | +; Test 8: Using modules with higher-order functions (from stdlib) |
| 146 | +; ============================================================================ |
| 147 | +(println "") |
| 148 | +(println "Test 8: Modules with higher-order functions (using stdlib)") |
| 149 | + |
| 150 | +; Map with module function - check individual elements |
| 151 | +(def squares (map math/square (list 1 2 3 4 5))) |
| 152 | +(if (and (= (car squares) 1) |
| 153 | + (= (car (cdr squares)) 4) |
| 154 | + (= (car (cdr (cdr squares))) 9)) |
| 155 | + (println " map with math/square: passed") |
| 156 | + (println " map with math/square: FAILED")) |
| 157 | + |
| 158 | +; Filter with module predicate - check result |
| 159 | +(def evens (filter predicates/even? (list 1 2 3 4 5 6))) |
| 160 | +(if (and (= (car evens) 2) |
| 161 | + (= (car (cdr evens)) 4) |
| 162 | + (= (car (cdr (cdr evens))) 6)) |
| 163 | + (println " filter with even?: passed") |
| 164 | + (println " filter with even?: FAILED")) |
| 165 | + |
| 166 | +; Filter with positive? |
| 167 | +(def positives (filter predicates/positive? (list -2 -1 0 1 2))) |
| 168 | +(if (and (= (car positives) 1) |
| 169 | + (= (car (cdr positives)) 2)) |
| 170 | + (println " filter with positive?: passed") |
| 171 | + (println " filter with positive?: FAILED")) |
| 172 | + |
| 173 | +; Fold with module function |
| 174 | +(def sum-of-squares (fold (fn (acc x) (+ acc (math/square x))) 0 (list 1 2 3 4))) |
| 175 | +(if (= sum-of-squares 30) ; 1 + 4 + 9 + 16 = 30 |
| 176 | + (println " fold with math/square: passed") |
| 177 | + (println " fold with math/square: FAILED")) |
| 178 | + |
| 179 | +; ============================================================================ |
| 180 | +; Test 9: Greetings module (side effects) |
| 181 | +; ============================================================================ |
| 182 | +(println "") |
| 183 | +(println "Test 9: Greetings module") |
| 184 | +(greetings/hello "World") |
| 185 | +(greetings/goodbye "World") |
| 186 | +(greetings/welcome "Alice" "Lisp Land") |
| 187 | +(println " greetings module: passed (check output above)") |
| 188 | + |
| 189 | +; ============================================================================ |
| 190 | +; Summary |
| 191 | +; ============================================================================ |
| 192 | +(println "All module tests completed!") |
| 193 | +(println "This test loaded modules from separate files:") |
| 194 | +(println " - lisp/stdlib.lisp (map, filter, fold)") |
| 195 | +(println " - lisp/modules/math.lisp") |
| 196 | +(println " - lisp/modules/predicates.lisp") |
| 197 | +(println " - lisp/modules/greetings.lisp") |
| 198 | +(println "") |
| 199 | +(println "Note: Private functions are not accessible.") |
| 200 | +(println "Trying (math/internal-constant) would error:") |
| 201 | +(println " 'internal-constant' is not exported from module 'math'") |
0 commit comments