/test/clojure/test_clojure/data_structures.clj

https://github.com/abimaran/clojure · Clojure · 830 lines · 562 code · 159 blank · 109 comment · 49 complexity · ec6141e9ef9125d8d43f10616d47803c MD5 · raw file

  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. ; Author: Frantisek Sodomka
  9. (ns clojure.test-clojure.data-structures
  10. (:use clojure.test))
  11. ;; *** Helper functions ***
  12. (defn diff [s1 s2]
  13. (seq (reduce disj (set s1) (set s2))))
  14. ;; *** General ***
  15. (defstruct equality-struct :a :b)
  16. (deftest test-equality
  17. ; nil is not equal to any other value
  18. (are [x] (not (= nil x))
  19. true false
  20. 0 0.0
  21. \space
  22. "" #""
  23. () [] #{} {}
  24. (lazy-seq nil) ; SVN 1292: fixed (= (lazy-seq nil) nil)
  25. (lazy-seq ())
  26. (lazy-seq [])
  27. (lazy-seq {})
  28. (lazy-seq #{})
  29. (lazy-seq "")
  30. (lazy-seq (into-array []))
  31. (new Object) )
  32. ; numbers equality across types (see tests below - NOT IMPLEMENTED YET)
  33. ; ratios
  34. (is (= 1/2 0.5))
  35. (is (= 1/1000 0.001))
  36. (is (not= 2/3 0.6666666666666666))
  37. ; vectors equal other seqs by items equality
  38. (are [x y] (= x y)
  39. '() [] ; regression fixed in r1208; was not equal
  40. '(1) [1]
  41. '(1 2) [1 2]
  42. [] '() ; same again, but vectors first
  43. [1] '(1)
  44. [1 2] '(1 2) )
  45. (is (not= [1 2] '(2 1))) ; order of items matters
  46. ; list and vector vs. set and map
  47. (are [x y] (not= x y)
  48. ; only () equals []
  49. () #{}
  50. () {}
  51. [] #{}
  52. [] {}
  53. #{} {}
  54. ; only '(1) equals [1]
  55. '(1) #{1}
  56. [1] #{1} )
  57. ; sorted-map, hash-map and array-map - classes differ, but content is equal
  58. ;; TODO: reimplement all-are with new do-template?
  59. ;; (all-are (not= (class _1) (class _2))
  60. ;; (sorted-map :a 1)
  61. ;; (hash-map :a 1)
  62. ;; (array-map :a 1))
  63. ;; (all-are (= _1 _2)
  64. ;; (sorted-map)
  65. ;; (hash-map)
  66. ;; (array-map))
  67. ;; (all-are (= _1 _2)
  68. ;; (sorted-map :a 1)
  69. ;; (hash-map :a 1)
  70. ;; (array-map :a 1))
  71. ;; (all-are (= _1 _2)
  72. ;; (sorted-map :a 1 :z 3 :c 2)
  73. ;; (hash-map :a 1 :z 3 :c 2)
  74. ;; (array-map :a 1 :z 3 :c 2))
  75. ; struct-map vs. sorted-map, hash-map and array-map
  76. (are [x] (and (not= (class (struct equality-struct 1 2)) (class x))
  77. (= (struct equality-struct 1 2) x))
  78. (sorted-map-by compare :a 1 :b 2)
  79. (sorted-map :a 1 :b 2)
  80. (hash-map :a 1 :b 2)
  81. (array-map :a 1 :b 2))
  82. ; sorted-set vs. hash-set
  83. (is (not= (class (sorted-set 1)) (class (hash-set 1))))
  84. (are [x y] (= x y)
  85. (sorted-set-by <) (hash-set)
  86. (sorted-set-by < 1) (hash-set 1)
  87. (sorted-set-by < 3 2 1) (hash-set 3 2 1)
  88. (sorted-set) (hash-set)
  89. (sorted-set 1) (hash-set 1)
  90. (sorted-set 3 2 1) (hash-set 3 2 1) ))
  91. ;; *** Collections ***
  92. (deftest test-count
  93. (are [x y] (= x y)
  94. (count nil) 0
  95. (count ()) 0
  96. (count '(1)) 1
  97. (count '(1 2 3)) 3
  98. (count []) 0
  99. (count [1]) 1
  100. (count [1 2 3]) 3
  101. (count #{}) 0
  102. (count #{1}) 1
  103. (count #{1 2 3}) 3
  104. (count {}) 0
  105. (count {:a 1}) 1
  106. (count {:a 1 :b 2 :c 3}) 3
  107. (count "") 0
  108. (count "a") 1
  109. (count "abc") 3
  110. (count (into-array [])) 0
  111. (count (into-array [1])) 1
  112. (count (into-array [1 2 3])) 3
  113. (count (java.util.ArrayList. [])) 0
  114. (count (java.util.ArrayList. [1])) 1
  115. (count (java.util.ArrayList. [1 2 3])) 3
  116. (count (java.util.HashMap. {})) 0
  117. (count (java.util.HashMap. {:a 1})) 1
  118. (count (java.util.HashMap. {:a 1 :b 2 :c 3})) 3 )
  119. ; different types
  120. (are [x] (= (count [x]) 1)
  121. nil true false
  122. 0 0.0 "" \space
  123. () [] #{} {} ))
  124. (deftest test-conj
  125. ; doesn't work on strings or arrays
  126. (is (thrown? ClassCastException (conj "" \a)))
  127. (is (thrown? ClassCastException (conj (into-array []) 1)))
  128. (are [x y] (= x y)
  129. (conj nil 1) '(1)
  130. (conj nil 3 2 1) '(1 2 3)
  131. (conj nil nil) '(nil)
  132. (conj nil nil nil) '(nil nil)
  133. (conj nil nil nil 1) '(1 nil nil)
  134. ; list -> conj puts the item at the front of the list
  135. (conj () 1) '(1)
  136. (conj () 1 2) '(2 1)
  137. (conj '(2 3) 1) '(1 2 3)
  138. (conj '(2 3) 1 4 3) '(3 4 1 2 3)
  139. (conj () nil) '(nil)
  140. (conj () ()) '(())
  141. ; vector -> conj puts the item at the end of the vector
  142. (conj [] 1) [1]
  143. (conj [] 1 2) [1 2]
  144. (conj [2 3] 1) [2 3 1]
  145. (conj [2 3] 1 4 3) [2 3 1 4 3]
  146. (conj [] nil) [nil]
  147. (conj [] []) [[]]
  148. ; map -> conj expects another (possibly single entry) map as the item,
  149. ; and returns a new map which is the old map plus the entries
  150. ; from the new, which may overwrite entries of the old.
  151. ; conj also accepts a MapEntry or a vector of two items (key and value).
  152. (conj {} {}) {}
  153. (conj {} {:a 1}) {:a 1}
  154. (conj {} {:a 1 :b 2}) {:a 1 :b 2}
  155. (conj {} {:a 1 :b 2} {:c 3}) {:a 1 :b 2 :c 3}
  156. (conj {} {:a 1 :b 2} {:a 3 :c 4}) {:a 3 :b 2 :c 4}
  157. (conj {:a 1} {:a 7}) {:a 7}
  158. (conj {:a 1} {:b 2}) {:a 1 :b 2}
  159. (conj {:a 1} {:a 7 :b 2}) {:a 7 :b 2}
  160. (conj {:a 1} {:a 7 :b 2} {:c 3}) {:a 7 :b 2 :c 3}
  161. (conj {:a 1} {:a 7 :b 2} {:b 4 :c 5}) {:a 7 :b 4 :c 5}
  162. (conj {} (first {:a 1})) {:a 1} ; MapEntry
  163. (conj {:a 1} (first {:b 2})) {:a 1 :b 2}
  164. (conj {:a 1} (first {:a 7})) {:a 7}
  165. (conj {:a 1} (first {:b 2}) (first {:a 5})) {:a 5 :b 2}
  166. (conj {} [:a 1]) {:a 1} ; vector
  167. (conj {:a 1} [:b 2]) {:a 1 :b 2}
  168. (conj {:a 1} [:a 7]) {:a 7}
  169. (conj {:a 1} [:b 2] [:a 5]) {:a 5 :b 2}
  170. (conj {} {nil {}}) {nil {}}
  171. (conj {} {{} nil}) {{} nil}
  172. (conj {} {{} {}}) {{} {}}
  173. ; set
  174. (conj #{} 1) #{1}
  175. (conj #{} 1 2 3) #{1 2 3}
  176. (conj #{2 3} 1) #{3 1 2}
  177. (conj #{3 2} 1) #{1 2 3}
  178. (conj #{2 3} 2) #{2 3}
  179. (conj #{2 3} 2 3) #{2 3}
  180. (conj #{2 3} 4 1 2 3) #{1 2 3 4}
  181. (conj #{} nil) #{nil}
  182. (conj #{} #{}) #{#{}} ))
  183. ;; *** Lists and Vectors ***
  184. (deftest test-peek
  185. ; doesn't work for sets and maps
  186. (is (thrown? ClassCastException (peek #{1})))
  187. (is (thrown? ClassCastException (peek {:a 1})))
  188. (are [x y] (= x y)
  189. (peek nil) nil
  190. ; list = first
  191. (peek ()) nil
  192. (peek '(1)) 1
  193. (peek '(1 2 3)) 1
  194. (peek '(nil)) nil ; special cases
  195. (peek '(1 nil)) 1
  196. (peek '(nil 2)) nil
  197. (peek '(())) ()
  198. (peek '(() nil)) ()
  199. (peek '(() 2 nil)) ()
  200. ; vector = last
  201. (peek []) nil
  202. (peek [1]) 1
  203. (peek [1 2 3]) 3
  204. (peek [nil]) nil ; special cases
  205. (peek [1 nil]) nil
  206. (peek [nil 2]) 2
  207. (peek [[]]) []
  208. (peek [[] nil]) nil
  209. (peek [[] 2 nil]) nil ))
  210. (deftest test-pop
  211. ; doesn't work for sets and maps
  212. (is (thrown? ClassCastException (pop #{1})))
  213. (is (thrown? ClassCastException (pop #{:a 1})))
  214. ; collection cannot be empty
  215. (is (thrown? IllegalStateException (pop ())))
  216. (is (thrown? IllegalStateException (pop [])))
  217. (are [x y] (= x y)
  218. (pop nil) nil
  219. ; list - pop first
  220. (pop '(1)) ()
  221. (pop '(1 2 3)) '(2 3)
  222. (pop '(nil)) ()
  223. (pop '(1 nil)) '(nil)
  224. (pop '(nil 2)) '(2)
  225. (pop '(())) ()
  226. (pop '(() nil)) '(nil)
  227. (pop '(() 2 nil)) '(2 nil)
  228. ; vector - pop last
  229. (pop [1]) []
  230. (pop [1 2 3]) [1 2]
  231. (pop [nil]) []
  232. (pop [1 nil]) [1]
  233. (pop [nil 2]) [nil]
  234. (pop [[]]) []
  235. (pop [[] nil]) [[]]
  236. (pop [[] 2 nil]) [[] 2] ))
  237. ;; *** Lists (IPersistentList) ***
  238. (deftest test-list
  239. (are [x] (list? x)
  240. ()
  241. '()
  242. (list)
  243. (list 1 2 3) )
  244. ; order is important
  245. (are [x y] (not (= x y))
  246. (list 1 2) (list 2 1)
  247. (list 3 1 2) (list 1 2 3) )
  248. (are [x y] (= x y)
  249. '() ()
  250. (list) '()
  251. (list 1) '(1)
  252. (list 1 2) '(1 2)
  253. ; nesting
  254. (list 1 (list 2 3) (list 3 (list 4 5 (list 6 (list 7)))))
  255. '(1 (2 3) (3 (4 5 (6 (7)))))
  256. ; different data structures
  257. (list true false nil)
  258. '(true false nil)
  259. (list 1 2.5 2/3 "ab" \x 'cd :kw)
  260. '(1 2.5 2/3 "ab" \x cd :kw)
  261. (list (list 1 2) [3 4] {:a 1 :b 2} #{:c :d})
  262. '((1 2) [3 4] {:a 1 :b 2} #{:c :d})
  263. ; evaluation
  264. (list (+ 1 2) [(+ 2 3) 'a] (list (* 2 3) 8))
  265. '(3 [5 a] (6 8))
  266. ; special cases
  267. (list nil) '(nil)
  268. (list 1 nil) '(1 nil)
  269. (list nil 2) '(nil 2)
  270. (list ()) '(())
  271. (list 1 ()) '(1 ())
  272. (list () 2) '(() 2) ))
  273. ;; *** Maps (IPersistentMap) ***
  274. (deftest test-find
  275. (are [x y] (= x y)
  276. (find {} :a) nil
  277. (find {:a 1} :a) [:a 1]
  278. (find {:a 1} :b) nil
  279. (find {:a 1 :b 2} :a) [:a 1]
  280. (find {:a 1 :b 2} :b) [:b 2]
  281. (find {:a 1 :b 2} :c) nil
  282. (find {} nil) nil
  283. (find {:a 1} nil) nil
  284. (find {:a 1 :b 2} nil) nil ))
  285. (deftest test-contains?
  286. ; contains? is designed to work preferably on maps and sets
  287. (are [x y] (= x y)
  288. (contains? {} :a) false
  289. (contains? {} nil) false
  290. (contains? {:a 1} :a) true
  291. (contains? {:a 1} :b) false
  292. (contains? {:a 1} nil) false
  293. (contains? {:a 1 :b 2} :a) true
  294. (contains? {:a 1 :b 2} :b) true
  295. (contains? {:a 1 :b 2} :c) false
  296. (contains? {:a 1 :b 2} nil) false
  297. ; sets
  298. (contains? #{} 1) false
  299. (contains? #{} nil) false
  300. (contains? #{1} 1) true
  301. (contains? #{1} 2) false
  302. (contains? #{1} nil) false
  303. (contains? #{1 2 3} 1) true
  304. (contains? #{1 2 3} 3) true
  305. (contains? #{1 2 3} 10) false
  306. (contains? #{1 2 3} nil) false)
  307. ; numerically indexed collections (e.g. vectors and Java arrays)
  308. ; => test if the numeric key is WITHIN THE RANGE OF INDEXES
  309. (are [x y] (= x y)
  310. (contains? [] 0) false
  311. (contains? [] -1) false
  312. (contains? [] 1) false
  313. (contains? [1] 0) true
  314. (contains? [1] -1) false
  315. (contains? [1] 1) false
  316. (contains? [1 2 3] 0) true
  317. (contains? [1 2 3] 2) true
  318. (contains? [1 2 3] 3) false
  319. (contains? [1 2 3] -1) false
  320. ; arrays
  321. (contains? (into-array []) 0) false
  322. (contains? (into-array []) -1) false
  323. (contains? (into-array []) 1) false
  324. (contains? (into-array [1]) 0) true
  325. (contains? (into-array [1]) -1) false
  326. (contains? (into-array [1]) 1) false
  327. (contains? (into-array [1 2 3]) 0) true
  328. (contains? (into-array [1 2 3]) 2) true
  329. (contains? (into-array [1 2 3]) 3) false
  330. (contains? (into-array [1 2 3]) -1) false)
  331. ; 'contains?' operates constant or logarithmic time,
  332. ; it WILL NOT perform a linear search for a value.
  333. (are [x] (= x false)
  334. (contains? '(1 2 3) 0)
  335. (contains? '(1 2 3) 1)
  336. (contains? '(1 2 3) 3)
  337. (contains? '(1 2 3) 10)
  338. (contains? '(1 2 3) nil)
  339. (contains? '(1 2 3) ()) ))
  340. (deftest test-keys
  341. (are [x y] (= x y) ; other than map data structures
  342. (keys ()) nil
  343. (keys []) nil
  344. (keys #{}) nil
  345. (keys "") nil )
  346. (are [x y] (= x y)
  347. ; (class {:a 1}) => clojure.lang.PersistentArrayMap
  348. (keys {}) nil
  349. (keys {:a 1}) '(:a)
  350. (diff (keys {:a 1 :b 2}) '(:a :b)) nil ; (keys {:a 1 :b 2}) '(:a :b)
  351. ; (class (sorted-map :a 1)) => clojure.lang.PersistentTreeMap
  352. (keys (sorted-map)) nil
  353. (keys (sorted-map :a 1)) '(:a)
  354. (diff (keys (sorted-map :a 1 :b 2)) '(:a :b)) nil ; (keys (sorted-map :a 1 :b 2)) '(:a :b)
  355. ; (class (hash-map :a 1)) => clojure.lang.PersistentHashMap
  356. (keys (hash-map)) nil
  357. (keys (hash-map :a 1)) '(:a)
  358. (diff (keys (hash-map :a 1 :b 2)) '(:a :b)) nil )) ; (keys (hash-map :a 1 :b 2)) '(:a :b)
  359. (deftest test-vals
  360. (are [x y] (= x y) ; other than map data structures
  361. (vals ()) nil
  362. (vals []) nil
  363. (vals #{}) nil
  364. (vals "") nil )
  365. (are [x y] (= x y)
  366. ; (class {:a 1}) => clojure.lang.PersistentArrayMap
  367. (vals {}) nil
  368. (vals {:a 1}) '(1)
  369. (diff (vals {:a 1 :b 2}) '(1 2)) nil ; (vals {:a 1 :b 2}) '(1 2)
  370. ; (class (sorted-map :a 1)) => clojure.lang.PersistentTreeMap
  371. (vals (sorted-map)) nil
  372. (vals (sorted-map :a 1)) '(1)
  373. (diff (vals (sorted-map :a 1 :b 2)) '(1 2)) nil ; (vals (sorted-map :a 1 :b 2)) '(1 2)
  374. ; (class (hash-map :a 1)) => clojure.lang.PersistentHashMap
  375. (vals (hash-map)) nil
  376. (vals (hash-map :a 1)) '(1)
  377. (diff (vals (hash-map :a 1 :b 2)) '(1 2)) nil )) ; (vals (hash-map :a 1 :b 2)) '(1 2)
  378. (deftest test-key
  379. (are [x] (= (key (first (hash-map x :value))) x)
  380. nil
  381. false true
  382. 0 42
  383. 0.0 3.14
  384. 2/3
  385. 0M 1M
  386. \c
  387. "" "abc"
  388. 'sym
  389. :kw
  390. () '(1 2)
  391. [] [1 2]
  392. {} {:a 1 :b 2}
  393. #{} #{1 2} ))
  394. (deftest test-val
  395. (are [x] (= (val (first (hash-map :key x))) x)
  396. nil
  397. false true
  398. 0 42
  399. 0.0 3.14
  400. 2/3
  401. 0M 1M
  402. \c
  403. "" "abc"
  404. 'sym
  405. :kw
  406. () '(1 2)
  407. [] [1 2]
  408. {} {:a 1 :b 2}
  409. #{} #{1 2} ))
  410. (deftest test-get
  411. (let [m {:a 1, :b 2, :c {:d 3, :e 4}, :f nil, :g false, nil {:h 5}}]
  412. (is (thrown? IllegalArgumentException (get-in {:a 1} 5)))
  413. (are [x y] (= x y)
  414. (get m :a) 1
  415. (get m :e) nil
  416. (get m :e 0) 0
  417. (get m :b 0) 2
  418. (get m :f 0) nil
  419. (get-in m [:c :e]) 4
  420. (get-in m '(:c :e)) 4
  421. (get-in m [:c :x]) nil
  422. (get-in m [:f]) nil
  423. (get-in m [:g]) false
  424. (get-in m [:h]) nil
  425. (get-in m []) m
  426. (get-in m nil) m
  427. (get-in m [:c :e] 0) 4
  428. (get-in m '(:c :e) 0) 4
  429. (get-in m [:c :x] 0) 0
  430. (get-in m [:b] 0) 2
  431. (get-in m [:f] 0) nil
  432. (get-in m [:g] 0) false
  433. (get-in m [:h] 0) 0
  434. (get-in m [:x :y] {:y 1}) {:y 1}
  435. (get-in m [] 0) m
  436. (get-in m nil 0) m)))
  437. ;; *** Sets ***
  438. (deftest test-hash-set
  439. (are [x] (set? x)
  440. #{}
  441. #{1 2}
  442. (hash-set)
  443. (hash-set 1 2) )
  444. ; order isn't important
  445. (are [x y] (= x y)
  446. #{1 2} #{2 1}
  447. #{3 1 2} #{1 2 3}
  448. (hash-set 1 2) (hash-set 2 1)
  449. (hash-set 3 1 2) (hash-set 1 2 3) )
  450. (are [x y] (= x y)
  451. ; equal classes
  452. (class #{}) (class (hash-set))
  453. (class #{1 2}) (class (hash-set 1 2))
  454. ; creating
  455. (hash-set) #{}
  456. (hash-set 1) #{1}
  457. (hash-set 1 2) #{1 2}
  458. ; nesting
  459. (hash-set 1 (hash-set 2 3) (hash-set 3 (hash-set 4 5 (hash-set 6 (hash-set 7)))))
  460. #{1 #{2 3} #{3 #{4 5 #{6 #{7}}}}}
  461. ; different data structures
  462. (hash-set true false nil)
  463. #{true false nil}
  464. (hash-set 1 2.5 2/3 "ab" \x 'cd :kw)
  465. #{1 2.5 2/3 "ab" \x 'cd :kw}
  466. (hash-set (list 1 2) [3 4] {:a 1 :b 2} #{:c :d})
  467. #{'(1 2) [3 4] {:a 1 :b 2} #{:c :d}}
  468. ; evaluation
  469. (hash-set (+ 1 2) [(+ 2 3) :a] (hash-set (* 2 3) 8))
  470. #{3 [5 :a] #{6 8}}
  471. ; special cases
  472. (hash-set nil) #{nil}
  473. (hash-set 1 nil) #{1 nil}
  474. (hash-set nil 2) #{nil 2}
  475. (hash-set #{}) #{#{}}
  476. (hash-set 1 #{}) #{1 #{}}
  477. (hash-set #{} 2) #{#{} 2} ))
  478. (deftest test-sorted-set
  479. ; only compatible types can be used
  480. (is (thrown? ClassCastException (sorted-set 1 "a")))
  481. (is (thrown? ClassCastException (sorted-set '(1 2) [3 4])))
  482. ; creates set?
  483. (are [x] (set? x)
  484. (sorted-set)
  485. (sorted-set 1 2) )
  486. ; equal and unique
  487. (are [x] (and (= (sorted-set x) #{x})
  488. (= (sorted-set x x) (sorted-set x)))
  489. nil
  490. false true
  491. 0 42
  492. 0.0 3.14
  493. 2/3
  494. 0M 1M
  495. \c
  496. "" "abc"
  497. 'sym
  498. :kw
  499. () ; '(1 2)
  500. [] [1 2]
  501. {} ; {:a 1 :b 2}
  502. #{} ; #{1 2}
  503. )
  504. ; cannot be cast to java.lang.Comparable
  505. (is (thrown? ClassCastException (sorted-set '(1 2) '(1 2))))
  506. (is (thrown? ClassCastException (sorted-set {:a 1 :b 2} {:a 1 :b 2})))
  507. (is (thrown? ClassCastException (sorted-set #{1 2} #{1 2})))
  508. (are [x y] (= x y)
  509. ; generating
  510. (sorted-set) #{}
  511. (sorted-set 1) #{1}
  512. (sorted-set 1 2) #{1 2}
  513. ; sorting
  514. (seq (sorted-set 5 4 3 2 1)) '(1 2 3 4 5)
  515. ; special cases
  516. (sorted-set nil) #{nil}
  517. (sorted-set 1 nil) #{nil 1}
  518. (sorted-set nil 2) #{nil 2}
  519. (sorted-set #{}) #{#{}} ))
  520. (deftest test-sorted-set-by
  521. ; only compatible types can be used
  522. ; NB: not a ClassCastException, but a RuntimeException is thrown,
  523. ; requires discussion on whether this should be symmetric with test-sorted-set
  524. (is (thrown? Exception (sorted-set-by < 1 "a")))
  525. (is (thrown? Exception (sorted-set-by < '(1 2) [3 4])))
  526. ; creates set?
  527. (are [x] (set? x)
  528. (sorted-set-by <)
  529. (sorted-set-by < 1 2) )
  530. ; equal and unique
  531. (are [x] (and (= (sorted-set-by compare x) #{x})
  532. (= (sorted-set-by compare x x) (sorted-set-by compare x)))
  533. nil
  534. false true
  535. 0 42
  536. 0.0 3.14
  537. 2/3
  538. 0M 1M
  539. \c
  540. "" "abc"
  541. 'sym
  542. :kw
  543. () ; '(1 2)
  544. [] [1 2]
  545. {} ; {:a 1 :b 2}
  546. #{} ; #{1 2}
  547. )
  548. ; cannot be cast to java.lang.Comparable
  549. ; NB: not a ClassCastException, but a RuntimeException is thrown,
  550. ; requires discussion on whether this should be symmetric with test-sorted-set
  551. (is (thrown? Exception (sorted-set-by compare '(1 2) '(1 2))))
  552. (is (thrown? Exception (sorted-set-by compare {:a 1 :b 2} {:a 1 :b 2})))
  553. (is (thrown? Exception (sorted-set-by compare #{1 2} #{1 2})))
  554. (are [x y] (= x y)
  555. ; generating
  556. (sorted-set-by >) #{}
  557. (sorted-set-by > 1) #{1}
  558. (sorted-set-by > 1 2) #{1 2}
  559. ; sorting
  560. (seq (sorted-set-by < 5 4 3 2 1)) '(1 2 3 4 5)
  561. ; special cases
  562. (sorted-set-by compare nil) #{nil}
  563. (sorted-set-by compare 1 nil) #{nil 1}
  564. (sorted-set-by compare nil 2) #{nil 2}
  565. (sorted-set-by compare #{}) #{#{}} ))
  566. (deftest test-set
  567. ; set?
  568. (are [x] (set? (set x))
  569. () '(1 2)
  570. [] [1 2]
  571. #{} #{1 2}
  572. {} {:a 1 :b 2}
  573. (into-array []) (into-array [1 2])
  574. "" "abc" )
  575. ; unique
  576. (are [x] (= (set [x x]) #{x})
  577. nil
  578. false true
  579. 0 42
  580. 0.0 3.14
  581. 2/3
  582. 0M 1M
  583. \c
  584. "" "abc"
  585. 'sym
  586. :kw
  587. () '(1 2)
  588. [] [1 2]
  589. {} {:a 1 :b 2}
  590. #{} #{1 2} )
  591. ; conversion
  592. (are [x y] (= (set x) y)
  593. () #{}
  594. '(1 2) #{1 2}
  595. [] #{}
  596. [1 2] #{1 2}
  597. #{} #{} ; identity
  598. #{1 2} #{1 2} ; identity
  599. {} #{}
  600. {:a 1 :b 2} #{[:a 1] [:b 2]}
  601. (into-array []) #{}
  602. (into-array [1 2]) #{1 2}
  603. "" #{}
  604. "abc" #{\a \b \c} ))
  605. (deftest test-disj
  606. ; doesn't work on lists, vectors or maps
  607. (is (thrown? ClassCastException (disj '(1 2) 1)))
  608. (is (thrown? ClassCastException (disj [1 2] 1)))
  609. (is (thrown? ClassCastException (disj {:a 1} :a)))
  610. ; identity
  611. (are [x] (= (disj x) x)
  612. nil
  613. #{}
  614. #{1 2 3}
  615. ; different data types
  616. #{nil
  617. false true
  618. 0 42
  619. 0.0 3.14
  620. 2/3
  621. 0M 1M
  622. \c
  623. "" "abc"
  624. 'sym
  625. :kw
  626. [] [1 2]
  627. {} {:a 1 :b 2}
  628. #{} #{1 2}} )
  629. ; type identity
  630. (are [x] (= (class (disj x)) (class x))
  631. (hash-set)
  632. (hash-set 1 2)
  633. (sorted-set)
  634. (sorted-set 1 2) )
  635. (are [x y] (= x y)
  636. (disj nil :a) nil
  637. (disj nil :a :b) nil
  638. (disj #{} :a) #{}
  639. (disj #{} :a :b) #{}
  640. (disj #{:a} :a) #{}
  641. (disj #{:a} :a :b) #{}
  642. (disj #{:a} :c) #{:a}
  643. (disj #{:a :b :c :d} :a) #{:b :c :d}
  644. (disj #{:a :b :c :d} :a :d) #{:b :c}
  645. (disj #{:a :b :c :d} :a :b :c) #{:d}
  646. (disj #{:a :b :c :d} :d :a :c :b) #{}
  647. (disj #{nil} :a) #{nil}
  648. (disj #{nil} #{}) #{nil}
  649. (disj #{nil} nil) #{}
  650. (disj #{#{}} nil) #{#{}}
  651. (disj #{#{}} #{}) #{}
  652. (disj #{#{nil}} #{nil}) #{} ))
  653. ;; *** Queues ***
  654. (deftest test-queues
  655. (let [EMPTY clojure.lang.PersistentQueue/EMPTY]
  656. (are [x y] (= x y)
  657. EMPTY EMPTY
  658. (into EMPTY (range 50)) (into EMPTY (range 50))
  659. (range 5) (into EMPTY (range 5))
  660. (range 1 6) (-> EMPTY
  661. (into (range 6))
  662. pop))
  663. (are [x y] (not= x y)
  664. (range 5) (into EMPTY (range 6))
  665. (range 6) (into EMPTY (range 5))
  666. (range 0 6) (-> EMPTY
  667. (into (range 6))
  668. pop)
  669. (range 1 6) (-> EMPTY
  670. (into (range 7))
  671. pop))))