1+ ; Copyright (c) Rich Hickey. All rights reserved.
2+ ; The use and distribution terms for this software are covered by the
3+ ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+ ; which can be found in the file epl-v10.html at the root of this distribution.
5+ ; By using this software in any fashion, you are agreeing to be bound by
6+ ; the terms of this license.
7+ ; You must not remove this notice, or any other, from this software.
8+
9+ (ns clojure.test-clojure.array-symbols
10+ (:use clojure.test)
11+ (:require [clojure.test-helper :as util]))
12+
13+ (set! *warn-on-reflection* true )
14+
15+ ; ; Easiest just to rewrite this whole thing
16+ ; (deftest test-array-symbols
17+ ; (is (= 'java.lang.String/1 (read-string "java.lang.String/1")))
18+ ; (is (= 'String/1 (read-string "String/1")))
19+ ; (is (= 'int/2 (read-string "int/2")))
20+ ; (testing "array symbol resolution"
21+ ; (are [str-repr klass] (= (Class/forName str-repr) klass)
22+ ; "[Z" (resolve 'boolean/1)
23+ ; "[B" (resolve 'byte/1)
24+ ; "[C" (resolve 'char/1)
25+ ; "[S" (resolve 'short/1)
26+ ; "[F" (resolve 'float/1)
27+ ; "[D" (resolve 'double/1)
28+ ; "[I" (resolve 'int/1)
29+ ; "[J" (resolve 'long/1)
30+ ; "[[J" (resolve 'long/2)
31+ ; "[Ljava.lang.Object;" (resolve 'Object/1)
32+ ; "[Ljava.lang.String;" (resolve 'String/1)
33+ ; "[[Ljava.lang.String;" (resolve 'String/2))
34+ ; (is (thrown? ClassNotFoundException (resolve 'ThisIsNotAClassThatCouldBeFound138/2)))
35+ ; (is (thrown? ClassNotFoundException (resolve 'foo.bar.ThisIsNotAClassThatCouldBeFound138/2))))
36+ ; (testing "array hints"
37+ ; (util/should-not-reflect
38+ ; (let [^long/1 a (long-array [1 2 3 4 99 100])]
39+ ; (java.util.Arrays/binarySearch a 99))))
40+ ; (testing "syntax quote"
41+ ; (is (= `byte/1 'byte/1))
42+ ; (is (= `byte/9 'byte/9))
43+ ; (is (= `java.util.UUID/1 'java.util.UUID/1))
44+ ; (is (= `String/1 'java.lang.String/1)))
45+ ; (testing "resolution"
46+ ; (is (= (eval 'long/1) (class (make-array Long/TYPE 0))))
47+ ; (is (= (resolve 'long/1) (class (make-array Long/TYPE 0))))
48+ ; (is (= (resolve 'String/1) (class (make-array String 0))))
49+ ; (is (= (resolve 'java.lang.String/1) (class (make-array String 0))))
50+ ; (is (= (resolve 'java.util.UUID/1) (class (make-array java.util.UUID 0))))
51+ ; (is (= (resolve 'String/2)
52+ ; (class (into-array (class (make-array String 0)) [(into-array String ["a" "b"])])))))
53+ ; (testing "value position"
54+ ; (is (= (class (make-array String 0)) String/1))
55+ ; (is (= [(class (make-array String 0))] [String/1])))
56+ ; (testing "printing"
57+ ; (is (= "long/1" (print-str long/1)))
58+ ; (is (= "byte/1" (print-str (class (make-array Byte/TYPE 0)))))
59+ ; (is (= "java.lang.String/2" (print-str String/2)))
60+ ; (is (= "[[java.lang.String/2]]" (print-str [[String/2]])))
61+ ; (is (= "java.util.UUID/4" (print-str java.util.UUID/4)))
62+ ; (is (= "[[[[[[[[[[Ljava.lang.Object;" (print-str (Class/forName "[[[[[[[[[[Ljava.lang.Object;"))))
63+ ; (is (= "java.lang.Object/9" (print-str (Class/forName "[[[[[[[[[Ljava.lang.Object;")))))
64+ ; (testing "error conditions"
65+ ; (is (thrown? Exception (read-string "String/1:")))
66+ ; (is (thrown? Exception (read-string "String/0")))
67+ ; (is (thrown? Exception (read-string "String/42")))
68+ ; (is (thrown? Exception (eval '(deftype Foo/2 [a
69+
70+ (defn make-array-of-dim [^Type element-type ^long dim]
71+ (if (<= dim 0 )
72+ element-type
73+ (make-array-of-dim (.MakeArrayType element-type) (dec dim))))
74+
75+
76+ (deftest test-array-symbols
77+ (is (= 'System.String/1 (read-string " System.String/1" )))
78+ (is (= 'String/1 (read-string " String/1" )))
79+ (is (= 'int/2 (read-string " int/2" )))
80+ (testing " array symbol resolution"
81+ (are [str-repr klass] (= (Type/GetType str-repr) klass)
82+ " System.Boolean[]" (resolve 'boolean/1 )
83+ " System.Byte[]" (resolve 'byte/1 )
84+ " System.Char[]" (resolve 'char/1 )
85+ " System.Int16[]" (resolve 'short/1 )
86+ " System.Single[]" (resolve 'float/1 )
87+ " System.Double[]" (resolve 'double/1 )
88+ " System.Int32[]" (resolve 'int/1 )
89+ " System.Int64[]" (resolve 'long/1 )
90+ " System.Int64[][]" (resolve 'long/2 )
91+ " System.Object[]" (resolve 'Object/1 )
92+ " System.String[]" (resolve 'String/1 )
93+ " System.String[][]" (resolve 'String/2 ))
94+ (is (thrown? clojure.lang.TypeNotFoundException (resolve 'ThisIsNotAClassThatCouldBeFound138/2 )))
95+ (is (thrown? clojure.lang.TypeNotFoundException (resolve 'foo.bar.ThisIsNotAClassThatCouldBeFound138/2 ))))
96+ (testing " array hints"
97+ (util/should-not-reflect
98+ (let [^long/1 a (long-array [1 2 3 4 99 100 ])]
99+ (System.Array/BinarySearch a 99 ))))
100+ (testing " syntax quote"
101+ (is (= `byte/1 'byte/1 ))
102+ (is (= `byte/9 'byte/9 ))
103+ (is (= `System.Guid/1 'System.Guid/1 ))
104+ (is (= `String/1 'String/1 )))
105+ (testing " resolution"
106+ (is (= (eval 'long/1 ) (class (make-array Int64 0 ))))
107+ (is (= (resolve 'long/1 ) (class (make-array Int64 0 ))))
108+ (is (= (resolve 'String/1 ) (class (make-array String 0 ))))
109+ (is (= (resolve 'System.String/1 ) (class (make-array String 0 ))))
110+ (is (= (resolve 'System.Guid/1 ) (class (make-array System.Guid 0 ))))
111+ (is (= (resolve 'String/2 )
112+ (class (into-array (class (make-array String 0 )) [(into-array String [" a" " b" ])])))))
113+ (testing " value position"
114+ (is (= (class (make-array String 0 )) String/1 ))
115+ (is (= [(class (make-array String 0 ))] [String/1 ])))
116+ (testing " printing"
117+ (is (= " long/1" (print-str long/1 )))
118+ (is (= " byte/1" (print-str (class (make-array Byte 0 )))))
119+ (is (= " System.String/2" (print-str String/2 )))
120+ (is (= " [[System.String/2]]" (print-str [[String/2 ]])))
121+ (is (= " System.Guid/4" (print-str System.Guid/4 )))
122+ (is (= " System.Object[][][][][][][][][][]" (print-str (make-array-of-dim Object 10 ))))
123+ (is (= " System.Object/9" (print-str (make-array-of-dim Object 9 )))))
124+ (testing " error conditions"
125+ (is (thrown? Exception (read-string " String/1:" )))
126+ (is (thrown? Exception (read-string " String/0" )))
127+ (is (thrown? Exception (read-string " String/42" )))
128+ (is (thrown? Exception (eval '(deftype Foo/2 [a]))))))
0 commit comments