|
| 1 | +; Test Standard Library Functions |
| 2 | +; Tests for logic operators, list functions, string functions, and I/O |
| 3 | + |
| 4 | +; ============================================ |
| 5 | +; Test short-circuiting logic operators |
| 6 | +; ============================================ |
| 7 | + |
| 8 | +(println "Testing logic operators...") |
| 9 | + |
| 10 | +; Test: and with no arguments |
| 11 | +(def test1 (and)) |
| 12 | +(if (= test1 true) |
| 13 | + (println "✓ (and) => true") |
| 14 | + (println "✗ (and) failed")) |
| 15 | + |
| 16 | +; Test: and with one argument |
| 17 | +(def test2 (and 42)) |
| 18 | +(if (= test2 42) |
| 19 | + (println "✓ (and 42) => 42") |
| 20 | + (println "✗ (and 42) failed")) |
| 21 | + |
| 22 | +; Test: and short-circuits on false |
| 23 | +(def test3 (and true false (/ 1 0))) ; Should not divide by zero |
| 24 | +(if (= test3 false) |
| 25 | + (println "✓ (and true false ...) short-circuits") |
| 26 | + (println "✗ (and) short-circuit failed")) |
| 27 | + |
| 28 | +; Test: and returns last value when all true |
| 29 | +(def test4 (and 1 2 3)) |
| 30 | +(if (= test4 3) |
| 31 | + (println "✓ (and 1 2 3) => 3") |
| 32 | + (println "✗ (and 1 2 3) failed")) |
| 33 | + |
| 34 | +; Test: or with no arguments |
| 35 | +(def test5 (or)) |
| 36 | +(if (= test5 false) |
| 37 | + (println "✓ (or) => false") |
| 38 | + (println "✗ (or) failed")) |
| 39 | + |
| 40 | +; Test: or with one argument |
| 41 | +(def test6 (or 42)) |
| 42 | +(if (= test6 42) |
| 43 | + (println "✓ (or 42) => 42") |
| 44 | + (println "✗ (or 42) failed")) |
| 45 | + |
| 46 | +; Test: or short-circuits on true |
| 47 | +(def test7 (or false true (/ 1 0))) ; Should not divide by zero |
| 48 | +(if (= test7 true) |
| 49 | + (println "✓ (or false true ...) short-circuits") |
| 50 | + (println "✗ (or) short-circuit failed")) |
| 51 | + |
| 52 | +; Test: or returns last value when all false |
| 53 | +(def test8 (or false nil false)) |
| 54 | +(if (= test8 false) |
| 55 | + (println "✓ (or false nil false) => false") |
| 56 | + (println "✗ (or false nil false) failed")) |
| 57 | + |
| 58 | +; ============================================ |
| 59 | +; Test list functions |
| 60 | +; ============================================ |
| 61 | + |
| 62 | +(println "\nTesting list functions...") |
| 63 | + |
| 64 | +; Test: nth on list |
| 65 | +(def lst (list 10 20 30 40 50)) |
| 66 | +(def test9 (nth lst 0)) |
| 67 | +(if (= test9 10) |
| 68 | + (println "✓ (nth lst 0) => 10") |
| 69 | + (println "✗ (nth lst 0) failed")) |
| 70 | + |
| 71 | +(def test10 (nth lst 2)) |
| 72 | +(if (= test10 30) |
| 73 | + (println "✓ (nth lst 2) => 30") |
| 74 | + (println "✗ (nth lst 2) failed")) |
| 75 | + |
| 76 | +; Test: append |
| 77 | +(def test11 (append (list 1 2) (list 3 4) (list 5 6))) |
| 78 | +(def test11-len (length test11)) |
| 79 | +(if (= test11-len 6) |
| 80 | + (println "✓ (append ...) => list of length 6") |
| 81 | + (println "✗ (append) failed")) |
| 82 | + |
| 83 | +; Test: reverse |
| 84 | +(def test12 (reverse (list 1 2 3 4 5))) |
| 85 | +(def test12-first (nth test12 0)) |
| 86 | +(if (= test12-first 5) |
| 87 | + (println "✓ (reverse (list 1 2 3 4 5)) => (5 4 3 2 1)") |
| 88 | + (println "✗ (reverse) failed")) |
| 89 | + |
| 90 | +; ============================================ |
| 91 | +; Test string functions |
| 92 | +; ============================================ |
| 93 | + |
| 94 | +(println "\nTesting string functions...") |
| 95 | + |
| 96 | +; Test: string-length |
| 97 | +(def test13 (string-length "hello")) |
| 98 | +(if (= test13 5) |
| 99 | + (println "✓ (string-length \"hello\") => 5") |
| 100 | + (println "✗ (string-length) failed")) |
| 101 | + |
| 102 | +; Test: string-append |
| 103 | +(def test14 (string-append "hello" " " "world")) |
| 104 | +(if (= test14 "hello world") |
| 105 | + (println "✓ (string-append ...) => \"hello world\"") |
| 106 | + (println "✗ (string-append) failed")) |
| 107 | + |
| 108 | +; Test: substring |
| 109 | +(def test15 (substring "hello world" 0 5)) |
| 110 | +(if (= test15 "hello") |
| 111 | + (println "✓ (substring \"hello world\" 0 5) => \"hello\"") |
| 112 | + (println "✗ (substring) failed")) |
| 113 | + |
| 114 | +(def test16 (substring "hello world" 6 11)) |
| 115 | +(if (= test16 "world") |
| 116 | + (println "✓ (substring \"hello world\" 6 11) => \"world\"") |
| 117 | + (println "✗ (substring) failed")) |
| 118 | + |
| 119 | +; Test: string->list |
| 120 | +(def test17 (string->list "abc")) |
| 121 | +(def test17-len (length test17)) |
| 122 | +(if (= test17-len 3) |
| 123 | + (println "✓ (string->list \"abc\") => list of length 3") |
| 124 | + (println "✗ (string->list) failed")) |
| 125 | + |
| 126 | +; Test: list->string |
| 127 | +(def test18 (list->string (list "h" "e" "l" "l" "o"))) |
| 128 | +(if (= test18 "hello") |
| 129 | + (println "✓ (list->string ...) => \"hello\"") |
| 130 | + (println "✗ (list->string) failed")) |
| 131 | + |
| 132 | +; Test: format |
| 133 | +(def test19 (format "Hello, ~a!" "world")) |
| 134 | +(if (= test19 "Hello, world!") |
| 135 | + (println "✓ (format \"Hello, ~a!\" \"world\") => \"Hello, world!\"") |
| 136 | + (println "✗ (format) failed")) |
| 137 | + |
| 138 | +(def test20 (format "The answer is ~a" 42)) |
| 139 | +(if (= test20 "The answer is 42") |
| 140 | + (println "✓ (format \"The answer is ~a\" 42) => \"The answer is 42\"") |
| 141 | + (println "✗ (format) failed")) |
| 142 | + |
| 143 | +; ============================================ |
| 144 | +; Test I/O functions |
| 145 | +; ============================================ |
| 146 | + |
| 147 | +(println "\nTesting I/O functions...") |
| 148 | + |
| 149 | +; Test: write-file and read-file |
| 150 | +(def test-content "Hello from Lisp VM!\nLine 2\nLine 3") |
| 151 | +(write-file "/tmp/lisp-test.txt" test-content) |
| 152 | +(def read-content (read-file "/tmp/lisp-test.txt")) |
| 153 | +(if (= read-content test-content) |
| 154 | + (println "✓ write-file and read-file work correctly") |
| 155 | + (println "✗ write-file or read-file failed")) |
| 156 | + |
| 157 | +(println "\nAll standard library tests completed!") |
0 commit comments