| 1 |
;;; |
| 2 |
;;; testing symbols and keywords |
| 3 |
;;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(test-start "symbols and keywords") |
| 7 |
|
| 8 |
;;---------------------------------------------------------------- |
| 9 |
(test-section "symbols") |
| 10 |
|
| 11 |
(test* "symbol->string" "foo" (symbol->string 'foo)) |
| 12 |
(test* "string->symbol" 'foo (string->symbol "foo") eq?) |
| 13 |
|
| 14 |
(test* "gensym" '(#t #t #f) |
| 15 |
(let1 s (gensym "ooo") |
| 16 |
(list (symbol? s) |
| 17 |
(string=? (substring (symbol->string s) 0 3) "ooo") |
| 18 |
(eq? s (string->symbol (symbol->string s)))))) |
| 19 |
|
| 20 |
(test* "interned?" #t (symbol-interned? (string->symbol "foofoo"))) |
| 21 |
(test* "interned?" #f (symbol-interned? (gensym "foofoo"))) |
| 22 |
|
| 23 |
(test* "symbol=?" '(#t #t #f #f) |
| 24 |
(list (symbol=? 'a 'a) |
| 25 |
(symbol=? 'a 'a 'a) |
| 26 |
(symbol=? 'a 'b) |
| 27 |
(symbol=? 'a 'a 'b))) |
| 28 |
(test* "symbol=?" (test-error) (symbol=? 'a 'a "a")) |
| 29 |
|
| 30 |
(test* "symbol reader" 'foo (read-from-string "foo")) |
| 31 |
(test* "symbol reader" 'foo (read-from-string "foo bar")) |
| 32 |
(test* "symbol reader escaped" 'foo (read-from-string "|foo|")) |
| 33 |
(test* "symbol reader escaped" 'foo (read-from-string "|foo|bar")) |
| 34 |
(test* "symbol reader escaped" '|foo bar| (read-from-string "|foo bar|")) |
| 35 |
|
| 36 |
;; NB: guard these with reader mode (legacy mode should read it differently) |
| 37 |
(test* "symbol reader hex escaped" 'abc (read-from-string "|a\\x62;c|")) |
| 38 |
(test* "symbol reader hex escaped" 'abc (read-from-string "|a\\x0062;c|")) |
| 39 |
|
| 40 |
(define (writer-test name expected) |
| 41 |
(test* (string-append "symbol writer: " name) |
| 42 |
expected |
| 43 |
(write-to-string (string->symbol name)))) |
| 44 |
|
| 45 |
(writer-test "foo" "foo") |
| 46 |
(writer-test "foo bar" "|foo bar|") |
| 47 |
(writer-test "" "||") |
| 48 |
(writer-test " " "| |") |
| 49 |
(writer-test "\r" "|\\x0d;|") |
| 50 |
(writer-test "+i" "|+i|") |
| 51 |
(writer-test "+in" "+in") |
| 52 |
(writer-test "+inf" "+inf") |
| 53 |
(writer-test "+inf." "+inf.") |
| 54 |
(writer-test "+inf.0" "|+inf.0|") |
| 55 |
(writer-test "-i" "|-i|") |
| 56 |
(writer-test "-nan.0" "|-nan.0|") |
| 57 |
(writer-test "@abc" "|@abc|") |
| 58 |
(writer-test "ab@c" "ab@c") |
| 59 |
(writer-test "." "|.|") |
| 60 |
(writer-test ".." "..") |
| 61 |
(writer-test "..." "...") |
| 62 |
(writer-test "+" "+") |
| 63 |
(writer-test "-" "-") |
| 64 |
(writer-test "->" "->") |
| 65 |
(writer-test "-0." "|-0.|") |
| 66 |
(writer-test "-1a" "|-1a|") |
| 67 |
(writer-test "-.0" "|-.0|") |
| 68 |
(writer-test "-.a" "-.a") |
| 69 |
|
| 70 |
(test* "symbol reader uninterned" "foo" |
| 71 |
(let1 s (read-from-string "#:foo") |
| 72 |
(and (symbol? s) (not (eq? 'foo s)) (symbol->string s)))) |
| 73 |
(test* "symbol reader uninterned" "+" |
| 74 |
(let1 s (read-from-string "#:+") |
| 75 |
(and (symbol? s) (not (eq? '+ s)) (symbol->string s)))) |
| 76 |
(test* "symbol reader uninterned" "-a" |
| 77 |
(let1 s (read-from-string "#:-a") |
| 78 |
(and (symbol? s) (not (eq? '-a s)) (symbol->string s)))) |
| 79 |
(test* "symbol reader uninterned" "foo bar" |
| 80 |
(let1 s (read-from-string "#:|foo bar|") |
| 81 |
(and (symbol? s) (not (eq? '|foo bar| s)) (symbol->string s)))) |
| 82 |
(test* "symbol reader uninterned" #f |
| 83 |
(eq? (read-from-string "#:foo") (read-from-string "#:foo"))) |
| 84 |
(test* "symbol reader uninterned invalid" (test-error) |
| 85 |
(read-from-string "#:")) |
| 86 |
(test* "symbol reader uninterned invalid" (test-error) |
| 87 |
(read-from-string "#: yoo")) |
| 88 |
(test* "symbol reader uninterned invalid" (test-error) |
| 89 |
(read-from-string "#:1234")) |
| 90 |
(test* "symbol reader uninterned invalid" (test-error) |
| 91 |
(read-from-string "#:+inf.0")) |
| 92 |
|
| 93 |
(test* "symbol writer uninterned" "#:foo" |
| 94 |
(write-to-string (string->uninterned-symbol "foo"))) |
| 95 |
(test* "symbol writer uninterned" "#:|foo bar|" |
| 96 |
(write-to-string (string->uninterned-symbol "foo bar"))) |
| 97 |
(test* "symbol writer uninterned" "(#0=#:foo #0#)" |
| 98 |
(write-to-string (let1 s (string->uninterned-symbol "foo") |
| 99 |
(list s s)) |
| 100 |
write/ss)) |
| 101 |
(test* "symbol writer uninterned" "(#:foo #:foo)" |
| 102 |
(write-to-string (list (string->uninterned-symbol "foo") |
| 103 |
(string->uninterned-symbol "foo")) |
| 104 |
write/ss)) |
| 105 |
|
| 106 |
(test* "prefix" 'bar (symbol-sans-prefix 'foo:bar 'foo:)) |
| 107 |
(test* "prefix" #f (symbol-sans-prefix 'foo:bar 'bar:)) |
| 108 |
|
| 109 |
(test* "symbol-append" 'ab:c45 (symbol-append 'ab ':c 45)) |
| 110 |
(test* "symbol-append" 'quux (symbol-append #t 'qu "ux")) |
| 111 |
(test* "symbol-append" #t |
| 112 |
(let1 x (symbol-append #f 'qu "ux") |
| 113 |
(and (not (symbol-interned? x)) |
| 114 |
(equal? (symbol->string x) "quux")))) |
| 115 |
(test* "symbol-append" '|| (symbol-append)) |
| 116 |
(test* "symbol-append" '|| (symbol-append #t)) |
| 117 |
|
| 118 |
;;---------------------------------------------------------------- |
| 119 |
(test-section "keywords") |
| 120 |
|
| 121 |
(test* "reader" #t (keyword? (read-from-string ":abc"))) |
| 122 |
(test* "reader" #t (keyword? (read-from-string ":"))) |
| 123 |
(test* "reader" #t (keyword? (read-from-string "::"))) |
| 124 |
(test* "reader" #t (keyword? (read-from-string ":|abc|"))) |
| 125 |
(test* "reader" #t (keyword? (read-from-string ":|a b c|"))) |
| 126 |
(test* "keyword?" #f (keyword? 'abc)) |
| 127 |
(test* "make-keyword" #t (keyword? (make-keyword 'abc))) |
| 128 |
(test* "keyword->string" "abc" (keyword->string (make-keyword 'abc))) |
| 129 |
(test* "keyword->string" "a b" (keyword->string (read-from-string ":|a b|"))) |
| 130 |
(test* "writer" ":abc" (write-to-string (make-keyword "abc"))) |
| 131 |
(test* "writer" |
| 132 |
(if (symbol? :x) "|:a b c|" ":|a b c|");transient during symbol-keyword integration |
| 133 |
(write-to-string (make-keyword "a b c"))) |
| 134 |
(test* "writer" ":" (write-to-string (make-keyword ""))) |
| 135 |
(test* "writer" "::" (write-to-string (make-keyword ":"))) |
| 136 |
(test* "writer" ":3" (write-to-string (make-keyword "3"))) |
| 137 |
(test* "writer" ":-i" (write-to-string (make-keyword "-i"))) |
| 138 |
(test* "eq?" #t (eq? :a :a)) |
| 139 |
(test* "eq?" #f (eq? :a 'a)) |
| 140 |
(test* "eqv?" #t (eqv? :a :a)) |
| 141 |
(test* "eqv?" #f (eqv? :a 'a)) |
| 142 |
|
| 143 |
;;---------------------------------------------------------------- |
| 144 |
(test-section "get-keyword") |
| 145 |
|
| 146 |
(define *key-value-list* '(:a 33 :b "foo" :c :d :d ook :a 99)) |
| 147 |
|
| 148 |
(test* "get-keyword" "foo" |
| 149 |
(get-keyword :b *key-value-list*)) |
| 150 |
(test* "get-keyword" 33 |
| 151 |
(get-keyword :a *key-value-list*)) |
| 152 |
(test* "get-keyword" 'ook |
| 153 |
(get-keyword :d *key-value-list*)) |
| 154 |
(test* "get-keyword" (test-error) |
| 155 |
(get-keyword :z *key-value-list*)) |
| 156 |
(test* "get-keyword" 88 |
| 157 |
(get-keyword :z *key-value-list* 88)) |
| 158 |
(test* "get-keyword" (test-error) |
| 159 |
(get-keyword :z (cdr *key-value-list*))) |
| 160 |
|
| 161 |
(test* "get-keyword*" "foo" |
| 162 |
(get-keyword* :b *key-value-list*)) |
| 163 |
(test* "get-keyword*" 33 |
| 164 |
(get-keyword* :a *key-value-list*)) |
| 165 |
(test* "get-keyword*" 'ook |
| 166 |
(get-keyword* :d *key-value-list*)) |
| 167 |
(test* "get-keyword*" 'ook |
| 168 |
(get-keyword* :d *key-value-list* (error "oops"))) |
| 169 |
(test* "get-keyword*" (test-error) |
| 170 |
(get-keyword* :z *key-value-list*)) |
| 171 |
(test* "get-keyword*" 88 |
| 172 |
(get-keyword* :z *key-value-list* 88)) |
| 173 |
(test* "get-keyword*" (test-error) |
| 174 |
(get-keyword* :z (cdr *key-value-list*))) |
| 175 |
|
| 176 |
;;---------------------------------------------------------------- |
| 177 |
(test-section "delete-keyword") |
| 178 |
|
| 179 |
(let () |
| 180 |
(define (tester source to-delete expected source-result) |
| 181 |
(define (non-destructive qfn fn arg0) |
| 182 |
(test* (format "~a ~s ~s" qfn arg0 source) expected (fn arg0 source))) |
| 183 |
(define (destructive qfn fn arg0) |
| 184 |
(test* (format "~a ~s ~s" qfn arg0 source) |
| 185 |
(list expected source-result) |
| 186 |
(let* ([orig (list-copy source)] |
| 187 |
[res (fn arg0 orig)]) |
| 188 |
(list res orig)))) |
| 189 |
|
| 190 |
(cond [(list? to-delete) |
| 191 |
(non-destructive 'delete-keywords delete-keywords to-delete) |
| 192 |
(destructive 'delete-keywords! delete-keywords! to-delete)] |
| 193 |
[else |
| 194 |
(non-destructive 'delete-keyword delete-keyword to-delete) |
| 195 |
(destructive 'delete-keyword! delete-keyword! to-delete) |
| 196 |
(non-destructive 'delete-keywords delete-keywords (list to-delete)) |
| 197 |
(destructive 'delete-keywords! delete-keywords! (list to-delete)) |
| 198 |
])) |
| 199 |
|
| 200 |
(tester '(:a 3 :b 5) :b '(:a 3) '(:a 3)) |
| 201 |
(tester '(:a 3 :b 5) :a '(:b 5) '(:a 3 :b 5)) |
| 202 |
(tester '(:a 3 :b 5) :c '(:a 3 :b 5) '(:a 3 :b 5)) |
| 203 |
(tester '(:a 3) :a '() '(:a 3)) |
| 204 |
(tester '() :a '() '()) |
| 205 |
(tester '(:a 3 :a 5) :a '() '(:a 3 :a 5)) |
| 206 |
(tester '(:a 3 :b 4 :b 5) :b '(:a 3) '(:a 3)) |
| 207 |
(tester '(:a 3 :b 4 :a 5) :a '(:b 4) '(:a 3 :b 4)) |
| 208 |
(tester '(:a 3 :b 4 :a 5) :b '(:a 3 :a 5) '(:a 3 :a 5)) |
| 209 |
|
| 210 |
(tester '(:a 3 :b 4) '(:a :b) '() '(:a 3 :b 4)) |
| 211 |
(tester '(:a 3 :b 4) '(:a :c) '(:b 4) '(:a 3 :b 4)) |
| 212 |
(tester '(:a 3 :b 4) '(:c :b) '(:a 3) '(:a 3)) |
| 213 |
(tester '(:a 3 :b 4) '() '(:a 3 :b 4) '(:a 3 :b 4)) |
| 214 |
(tester '(:a 3 :b 4 :c 5) '(:a :c) '(:b 4) '(:a 3 :b 4)) |
| 215 |
(tester '(:a 3 :b 4 :c 5) '(:b :c) '(:a 3) '(:a 3)) |
| 216 |
(tester '(:a 3 :b 4 :c 5) '(:a :b) '(:c 5) '(:a 3 :b 4 :c 5)) |
| 217 |
) |
| 218 |
|
| 219 |
(test-end) |