diff options
| author | Loic Guegan <manzerberdes@gmx.com> | 2019-02-24 10:30:57 +0100 |
|---|---|---|
| committer | Loic Guegan <manzerberdes@gmx.com> | 2019-02-24 10:30:57 +0100 |
| commit | 5725987c8dfd55d4ee0282f0a37779e06052f3c6 (patch) | |
| tree | d985acea2fdb3149804ea630c06662d5a8d0796c /test | |
| parent | d0f6e2ff9cdf4c35e99b54fa765aca7f46ed6a24 (diff) | |
Re-organize code
Diffstat (limited to 'test')
| -rw-r--r-- | test/test.lisp | 8 | ||||
| -rw-r--r-- | test/union-find.lisp | 68 | ||||
| -rw-r--r-- | test/union-find/test-quick-find.lisp | 45 |
3 files changed, 121 insertions, 0 deletions
diff --git a/test/test.lisp b/test/test.lisp new file mode 100644 index 0000000..f672045 --- /dev/null +++ b/test/test.lisp @@ -0,0 +1,8 @@ +(in-package :com.lisp-algo.test) + +(defun do-tests () + "Configure lisp-unit and run all tests." + (setq *print-errors* t) ; Details tests locations when running tests + (setq *print-summary* t) ; Details on tests + (setq *print-failures* t) ; Details tests locations when failures + (run-tests :all :com.lisp-algo.test)) diff --git a/test/union-find.lisp b/test/union-find.lisp new file mode 100644 index 0000000..4bfa61d --- /dev/null +++ b/test/union-find.lisp @@ -0,0 +1,68 @@ +(in-package :com.lisp-algo.test) + + +;;; Utils +(defun get-row (array &optional (row-id 0)) + (let* ((row-size (array-dimension array 1)) ; Deduce row size from array + (row (make-array row-size :fill-pointer 0))) ; Initialize a new vector (which will contain the row row-id from array) + ;; Fill row with the right values of array + (do ((cur-id 0 (+ cur-id 1))) + ((>= cur-id row-size) row) + (vector-push (row-major-aref array (+ cur-id (* row-size row-id ))) row)))) + + +;;; Test create network +(define-test create-network-test () + ;; ----- Length tests + (dotimes (nw-size 1000) + (assert-equal nw-size (length (qf-create-network nw-size))) ; Quick-Find + (assert-equal nw-size (length (qu-create-network nw-size))) ; Quick-Union + ;; Weighted-Quick-Union + (assert-equal 10 (length (get-row (wqu-create-network 10) 0))) + (assert-equal 10 (length (get-row (wqu-create-network 10) 1))) + ;; Weighted-Quick-Union with Path Compression + (assert-equal 10 (length (get-row (wqupc-create-network 10) 0))) + (assert-equal 10 (length (get-row (wqupc-create-network 10) 1)))) + ;; ----- Value tests + (assert-equalp #(0 1 2 3 4) (qf-create-network 5)) ; Quick-Find + (assert-equalp #(0 1 2 3 4) (qu-create-network 5)) ; Quick-Union + ;; Weighted-Quick-Union + (assert-true (equalp #(0 1 2 3 4 5 6 7 8 9) (get-row (wqu-create-network 10) 0))) + (assert-true (equalp (make-array 10 :initial-element 1) (get-row (wqu-create-network 10) 1))) + ;; Weighted-Quick-Union with Path Compression + (assert-true (equalp #(0 1 2 3 4 5 6 7 8 9) (get-row (wqupc-create-network 10) 0))) + (assert-true (equalp (make-array 10 :initial-element 1) (get-row (wqupc-create-network 10) 1)))) + + +;; (define-test test-union_ +;; (let ((nw (create-network 10))) +;; (setf nw (union_ nw 1 2)) +;; (setf nw (union_ nw 0 5)) +;; (assert-equal (aref nw 1) (aref nw 2)) +;; (assert-equal (aref nw 0) (aref nw 5)) +;; (assert-false (equal (aref nw 0) (aref nw 8))) +;; (assert-false (equal (aref nw 0) (aref nw 2))))) + +;; (define-test test-connected +;; (let ((nw (create-network 10))) +;; (setf nw (union_ nw 1 2)) +;; (setf nw (union_ nw 0 5)) +;; (assert-true (connected nw 1 2)) +;; (assert-true (connected nw 0 5)) +;; (assert-false (connected nw 0 8)) +;; (assert-false (connected nw 0 2)))) + +;; (define-test test-nunion__ +;; (let ((nw (create-network 10))) +;; (nunion_ nw 1 2) +;; (nunion_ nw 0 5) +;; (assert-equal (aref nw 1) (aref nw 2)) +;; (assert-equal (aref nw 0) (aref nw 5)) +;; (assert-false (equal (aref nw 0) (aref nw 8))) +;; (assert-false (equal (aref nw 0) (aref nw 2))))) + +;; ;; Run all tests +;; (setq *print-summary* t) ; Details tests locations when running tests +;; (run-tests :all) + + diff --git a/test/union-find/test-quick-find.lisp b/test/union-find/test-quick-find.lisp new file mode 100644 index 0000000..f173ea6 --- /dev/null +++ b/test/union-find/test-quick-find.lisp @@ -0,0 +1,45 @@ +(load "../lisp-unit.lisp") +(defpackage :test-quick-find + (:use :common-lisp + :lisp-unit)) + +(in-package :test-quick-find) +(load "../../src/union-find/quick-find.lisp") + +;;; Define tests +(define-test test-create-network + (assert-equal 10 (length (create-network 10))) + (assert-equalp #(0 1 2 3 4) (create-network 5))) + +(define-test test-union_ + (let ((nw (create-network 10))) + (setf nw (union_ nw 1 2)) + (setf nw (union_ nw 0 5)) + (assert-equal (aref nw 1) (aref nw 2)) + (assert-equal (aref nw 0) (aref nw 5)) + (assert-false (equal (aref nw 0) (aref nw 8))) + (assert-false (equal (aref nw 0) (aref nw 2))))) + +(define-test test-connected + (let ((nw (create-network 10))) + (setf nw (union_ nw 1 2)) + (setf nw (union_ nw 0 5)) + (assert-true (connected nw 1 2)) + (assert-true (connected nw 0 5)) + (assert-false (connected nw 0 8)) + (assert-false (connected nw 0 2)))) + +(define-test test-nunion__ + (let ((nw (create-network 10))) + (nunion_ nw 1 2) + (nunion_ nw 0 5) + (assert-equal (aref nw 1) (aref nw 2)) + (assert-equal (aref nw 0) (aref nw 5)) + (assert-false (equal (aref nw 0) (aref nw 8))) + (assert-false (equal (aref nw 0) (aref nw 2))))) + +;; Run all tests +(setq *print-summary* t) ; Details tests locations when running tests +(run-tests :all) + + |
