/test/clojure/test_clojure/data_structures.clj

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