Skip to content

Commit

Permalink
Convert union tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
okamsn committed Jul 30, 2023
1 parent ceb16bb commit 4ed15a9
Showing 1 changed file with 197 additions and 166 deletions.
363 changes: 197 additions & 166 deletions tests/tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -4168,170 +4168,200 @@ Using `start' and `end' in either order should give the same result."
(sum . summing)))

;;;;; Union
(ert-deftest union ()
;; TODO: `union' currently has predictable behavior due to the `:at' position,
;; but it might be worthwhile to remove that predictability for speed in
;; the future.
;;
;; (should (null (cl-set-difference
;; '(4 1 2 3)
;; (eval (quote (loopy (list i '((1 2) (2 3) (3 4)))
;; (union var i)))))))
;; (should (null (cl-set-difference
;; '(4 1 2 3)
;; (eval (quote (loopy (list i '((1 2) (2 3) (3 4)))
;; (unioning var i)
;; (finally-return var)))))))
;;
;; (should (null (cl-set-difference
;; '(4 2 (1 1) 3)
;; (eval (quote (loopy (list i '(((1 1) 2) ((1 1) 3) (3 4)))
;; (unioning var i :test #'equal)
;; (finally-return var))))
;; :test #'equal)))
;;
;; ;; The resulting list should only have one element whose `car' is `a'.
;; (should (= 1 (cl-count-if (lambda (x) (eq (car x) 'a))
;; (eval (quote (loopy (array i [((a . 1)) ((a . 2))])
;; (unioning var i :key #'car)
;; (finally-return var)))))))

(should (equal '(1 2 3 4)
(eval (quote (loopy (list i '((1 2) (2 3) (3 4)))
(union var i)
(finally-return var))))))

(should (equal '(1 2 3 4)
(eval (quote (loopy (list i '((1 2) (2 3) (3 4)))
(unioning var i)
(finally-return var))))))

(should (equal '((1 1) 2 3 4)
(eval (quote (loopy (list i '(((1 1) 2) ((1 1) 3) (3 4)))
(unioning var i :test #'equal)
(finally-return var))))))

(should (equal '((1 1) 2 3 4)
(eval (quote (let ((func #'equal ))
(loopy (list i '(((1 1) 2) ((1 1) 3) (3 4)))
(unioning var i :test func)
(finally-return var)))))))

(should (equal '((a . 1))
(eval (quote (loopy (array i [((a . 1)) ((a . 2))])
(unioning var i :key #'car)
(finally-return var))))))

(should (equal '((a . 1))
(eval (quote (let ((func #'car))
(loopy (array i [((a . 1)) ((a . 2))])
(unioning var i :key func)
(finally-return var))))))))

(ert-deftest union-destructuring ()
;; TODO: `union' currently has predictable behavior due to the `:at' position,
;; but it might be worthwhile to remove that predictability for speed in
;; the future.
;;
;; (should (null (cl-destructuring-bind (first second)
;; (eval (quote (loopy (array i [((1 2) (2 3))
;; ((1 2 3) (3 4))])
;; (union (var1 var2) i :test #'equal))))
;; (or (clsetdifference first '(1 2 3))
;; (clsetdifference second '(2 3 4))))))
(should (equal '((1 2 3) (2 3 4))
(eval (quote (loopy (array i [((1 2) (2 3))
((1 2 3) (3 4))])
(union (var1 var2) i :test #'=)
(finally-return var1 var2)))))))

(ert-deftest union-at ()
(should (equal '((1 2) (3 2) (1 1))
(eval (quote (loopy (list i '(((1 2) (3 2)) ((1 1) (4 2))))
(union i :at end :key #'cl-second))))))

(should (equal '((1 2) (3 2) (4 2))
(eval (quote (loopy (list i '(((1 2) (3 2)) ((1 1) (4 2))))
(union i :at end :key #'car))))))

(should (equal '((4 2) (1 2) (3 2))
(eval (quote (loopy (list i '(((1 2) (3 2)) ((1 1) (4 2))))
(union i :at start :key #'car))))))

(should (equal '((4 2) (1 2) (3 2))
(eval (quote (loopy (list i '(((1 2) (3 2)) ((1 1) (4 2))))
(union c i :at start :key #'car)
(finally-return c))))))

(should (equal '(1 2 3)
(eval (quote (loopy (list i '((1 2 3) (1 2 3)))
(union i :test #'equal :at start))))))
(loopy-deftest union
:result '(1 2 3 4)
:multi-body t
:body [((list i '((1 2) (2 3) (3 4)))
(_cmd var i)
(finally-return var))

(should (equal '(1 2 3)
(eval (quote (loopy (list i '((1 2 3) (1 2 3)))
(union c i :test #'equal :at start)
(finally-return c)))))))

(ert-deftest union-end-tracking ()
(should (equal '(1 2 3 4 5 6 7 8)
(eval (quote
(let ((l1 (list (list 1 2) (list 3 4) (list 5 6) (list 7 8))))
(loopy (flag split)
(list i l1)
(union i :at end)))))))

(should (equal '(1 2 3 4 5 6 7 8)
(let ((l1 (list (list 1 2) (list 3 4) (list 5 6) (list 7 8))))
(loopy (flag -split)
(list i l1)
(union i :at end)))))

(should (equal '(1 2 3 4 5 6)
(let ((l1 (list (list 1 2) (list 3 4) (list 4 3) (list 5 6))))
(loopy (list i l1)
(union coll i :at end)
(finally-return coll)))))

(should (equal '(1 2 3 4 5 6 7)
(let ((l1 (list (list 1 2) (list 3 4) (list 4 3) (list 5 6))))
(loopy (list i l1)
(union coll i :at end)
(union coll (mapcar #'1+ i) :at end)
(finally-return coll)))))

(should (equal '(5 6 3 4 1 2 11 12 13 14 15 16)
(loopy (list i '((1 2) (3 4) (5 6)))
(union i :at start)
(union (mapcar (lambda (x) (+ x 10)) i)
:at end))))

(should (equal '(5 6 3 4 1 2 11 12 13 14 15 16)
(loopy (list i '((1 2) (3 4) (5 6)))
(union (mapcar (lambda (x) (+ x 10)) i)
:at end)
(union i :at start)))))

(ert-deftest union-not-destructive ()
(let ((l1 (list (list 1 2) (list 3 4) (list 5 6) (list 7 8))))
(loopy (list i l1) (union coll i :at start))
(should (equal l1 '((1 2) (3 4) (5 6) (7 8)))))

(let ((l1 (list (list 1 2) (list 3 4) (list 5 6) (list 7 8))))
(loopy (list i l1) (union coll i :at end))
(should (equal l1 '((1 2) (3 4) (5 6) (7 8)))))

(let ((l1 (list (list 1 2) (list 3 4) (list 5 6) (list 7 8))))
(loopy (list i l1) (union i :at start))
(should (equal l1 '((1 2) (3 4) (5 6) (7 8)))))

(should (equal (let ((l1 (list (list 1 2) (list 3 4) (list 5 6) (list 7 8))))
(loopy (list i l1) (union i :at end))
l1)
'((1 2) (3 4) (5 6) (7 8))))

(let ((l1 (list (list 1 2) (list 3 4) (list 5 6) (list 7 8))))
(loopy (flag split) (list i l1) (union i :at end))
(should (equal l1 '((1 2) (3 4) (5 6) (7 8))))))
((list i '((1 2) (2 3) (3 4)))
(_cmd i))]
:repeat _cmd
:loopy ((_cmd . (union unioning)))
:iter-keyword ((list . list)
(_cmd . (union unioning)))
:iter-bare ((list . listing)
(_cmd . (unioning))))

(loopy-deftest union-:key
:result '((a . 1))
:multi-body t
:body [(loopy (array i [((a . 1)) ((a . 2))])
(union var i :key #'car)
(finally-return var))

(loopy (with (func #'car))
(array i [((a . 1)) ((a . 2))])
(union i :key func))]
:loopy t
:iter-keyword (union array)
:iter-bare ((union . unioning)
(array . arraying)))

(loopy-deftest union-:test
:result '((1 1) 2 3 4)
:multi-body t
:body [((list i '(((1 1) 2) ((1 1) 3) (3 4)))
(union var i :test #'equal)
(finally-return var))

((with (func #'equal))
(list i '(((1 1) 2) ((1 1) 3) (3 4)))
(union i :test func))]
:loopy t
:iter-keyword (union list)
:iter-bare ((union . unioning)
(list . listing)))

(loopy-deftest union-destructuring
:result '((1 2 3) (2 3 4))
:body ((array i [((1 2) (2 3))
((1 2 3) (3 4))])
(union (var1 var2) i :test #'=)
(finally-return var1 var2))
:loopy t
:iter-keyword (array union)
:iter-bare ((array . arraying)
(union . unioning)))

(loopy-deftest union-:at-end
:result '(1 2 3 4 5 6)
:multi-body t
:body [((list i '((1 2 3) (3 4 5 6)))
(union i :at end))

((list i '((1 2 3) (4 5 3 6)))
(union coll i :at end)
(finally-return coll))]
:loopy t
:iter-keyword (list union)
:iter-bare ((list . listing)
(union . unioning)))

(loopy-deftest union-:at-start/beginning
:result '(4 5 6 1 2 3)
:multi-body t
:body [((list i '((1 2 3) (3 4 5 6)))
(union i :at start))
((list i '((1 2 3) (3 4 5 6)))
(union coll i :at start)
(finally-return coll))
((list i '((1 2 3) (3 4 5 6)))
(union i :at beginning))
((list i '((1 2 3) (3 4 5 6)))
(union coll i :at beginning)
(finally-return coll))]
:loopy t
:iter-keyword (list union)
:iter-bare ((list . listing)
(union . unioning)))

(loopy-deftest union-end-tracking-accum-opt-end
:result '(4 5 6 1 2 3)
:body ((accum-opt (coll end))
(list i '((1 2 3) (3 4 5 6)))
(union coll i :at start)
(finally-return coll))
:loopy t
:iter-keyword (list union)
:iter-bare ((list . listing)
(union . unioning)))

(loopy-deftest union-end-tracking-accum-opt-start
:result '(1 2 3 4 6 7)
:body ((accum-opt (coll start))
(list i '((1 2 3 4) (3 6 4 7)))
(union coll i :at end)
(finally-return coll))
:loopy t
:iter-keyword (list union)
:iter-bare ((list . listing)
(union . unioning)))

(loopy-deftest union-end-tracking-start-end
:doc "Don't overlap. Just check that it combines well."
:result '(8 7 6 5 1 2 3 4 11 12 13 14 18 17 16 15)
:multi-body t
:body [((list i '((1 2 3 4) (8 7 6 5)))
(union coll i :at start)
(union coll (mapcar (lambda (x) (+ x 10)) i) :at end)
(finally-return coll))

(loopy (list i '((1 2 3 4) (8 7 6 5)))
(union coll (mapcar (lambda (x) (+ x 10)) i) :at end)
(union coll i :at start)
(finally-return coll))

((list i '((1 2 3 4) (8 7 6 5)))
(union i :at start)
(union (mapcar (lambda (x) (+ x 10)) i) :at end))

((list i '((1 2 3 4) (8 7 6 5)))
(union (mapcar (lambda (x) (+ x 10)) i) :at end)
(union i :at start))]
:loopy t
:iter-keyword (list union)
:iter-bare ((list . listing)
(union . unioning)))

(loopy-deftest union-end-tracking-end-twice
:result '(1 2 3 4 5 7 6 8)
:multi-body t
:body [((list i '((1 2 3 4) (7 4 6 3)))
(union coll i :at end)
(union coll (mapcar (lambda (x) (+ x 1)) i) :at end)
(finally-return coll))

((list i '((1 2 3 4) (7 4 6 3)))
(union i :at end)
(union (mapcar (lambda (x) (+ x 1)) i) :at end))]
:loopy t
:iter-keyword (list union)
:iter-bare ((list . listing)
(union . unioning)))

(loopy-deftest union-not-destructive
:doc "Check that `union' doesn't modify the list being unioned."
:result t
:multi-body t
:body [((with (l1 (list (list 1 2 3) (list 3 4 5) (list 5 6 7) (list 7 8 9))))
(listing i l1)
(union coll i :at start)
(finally-return (equal l1 '((1 2 3) (3 4 5) (5 6 7) (7 8 9)))))

((with (l1 (list (list 1 2 3) (list 3 4 5) (list 5 6 7) (list 7 8 9))))
(listing i l1)
(union coll i :at end)
(finally-return (equal l1 '((1 2 3) (3 4 5) (5 6 7) (7 8 9)))))

((with (l1 (list (list 1 2 3) (list 3 4 5) (list 5 6 7) (list 7 8 9))))
(listing i l1)
(union i :at start)
(finally-return (equal l1 '((1 2 3) (3 4 5) (5 6 7) (7 8 9)))))

((with (l1 (list (list 1 2 3) (list 3 4 5) (list 5 6 7) (list 7 8 9))))
(listing i l1)
(union i :at end)
(finally-return (equal l1 '((1 2 3) (3 4 5) (5 6 7) (7 8 9)))))]
:loopy t
:iter-keyword (listing union)
:iter-bare ((union . unioning)))

(loopy-deftest union-:test-:key
:result '((4 2) (1 2) (3 2))
:multi-body t
:body [((list i '(((1 2) (3 2)) ((1 1) (4 2))))
(union var i :at 'start :key #'car)
(finally-return var))

((list i '(((1 2) (3 2)) ((1 1) (4 2))))
(union i :at 'start :key #'car))]
:loopy t
:iter-keyword (list union)
:iter-bare ((list . listing)
(union . unioning)))

;;;;; Vconcat
(ert-deftest vconcat ()
Expand Down Expand Up @@ -4763,10 +4793,11 @@ Not multiple of 3: 7")))
(defun mu4e:other-path ()
"Return load-path for mu4e.
This assumes that you're on guix."
(loopy (with (regexp "Documents")
(base-dir (expand-file-name "~/")))
(list file (directory-files base-dir))
(expr full-path (expand-file-name file base-dir))))
(with-suppressed-warnings ((unused . (regexp base-dir)))
(loopy (with (regexp "Documents")
(base-dir (expand-file-name "~/")))
(list file (directory-files base-dir))
(set full-path (expand-file-name file base-dir)))))
(mu4e:other-path)
;; If an `nreverse' goes bad, then the function value of `mu4e:other-path'
;; might be changed (somehow), which causes an error.
Expand Down

0 comments on commit 4ed15a9

Please sign in to comment.