diff options
author | Simen Heggestøyl <simenheg@gmail.com> | 2016-09-08 19:18:40 +0200 |
---|---|---|
committer | Simen Heggestøyl <simenheg@gmail.com> | 2016-09-08 19:18:40 +0200 |
commit | e951aa145981cfcee48517dbc7faad89b24e4e96 (patch) | |
tree | 00183921fdd7ee62e07e0c87b914b1299869b630 /test | |
parent | ba5d32398ba42fcf14ad5242d95c68133981744f (diff) |
Add tests for ring.el
* test/lisp/emacs-lisp/ring-tests.el: New file with tests for ring.el.
Diffstat (limited to 'test')
-rw-r--r-- | test/lisp/emacs-lisp/ring-tests.el | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/test/lisp/emacs-lisp/ring-tests.el b/test/lisp/emacs-lisp/ring-tests.el new file mode 100644 index 0000000000..705bfe5cff --- /dev/null +++ b/test/lisp/emacs-lisp/ring-tests.el @@ -0,0 +1,204 @@ +;;; ring-tests.el --- Tests for ring.el -*- lexical-binding: t; -*- + +;; Copyright (C) 2016 Free Software Foundation, Inc. + +;; Author: Simen Heggestøyl <simenheg@gmail.com> +;; Keywords: + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. + +;;; Commentary: + +;; + +;;; Code: + +(require 'ert) +(require 'ring) + +(ert-deftest ring-tests-make-ring-ring-p () + (should (ring-p (make-ring 5)))) + +(ert-deftest ring-tests-insert-at-beginning () + (let ((ring (make-ring 5))) + (ring-insert-at-beginning ring 'foo) + (ring-insert-at-beginning ring 'bar) + (should (equal (ring-elements ring) '(foo bar))))) + +(ert-deftest ring-tests-plus1 () + (should (= (ring-plus1 0 5) 1)) + (should (= (ring-plus1 4 5) 0))) + +(ert-deftest ring-tests-minus1 () + (should (= (ring-minus1 0 5) 4)) + (should (= (ring-minus1 4 5) 3))) + +(ert-deftest ring-tests-length () + (let ((ring (make-ring 2))) + (should (= (ring-length ring) 0)) + (ring-insert ring 'a) + (should (= (ring-length ring) 1)) + (ring-insert ring 'b) + (should (= (ring-length ring) 2)) + (ring-insert ring 'c) + (should (= (ring-length ring) 2)))) + +(ert-deftest ring-tests-index () + (should (= (ring-index 0 0 3 3) 2)) + (should (= (ring-index 0 0 3 5) 2)) + (should (= (ring-index 0 2 3 3) 1)) + (should (= (ring-index 1 2 3 3) 0)) + (should (= (ring-index 2 2 3 3) 2))) + +(ert-deftest ring-tests-empty-p () + (let ((ring (make-ring 5))) + (should (ring-empty-p ring)) + (ring-insert ring 1) + (should-not (ring-empty-p ring)) + (ring-remove ring) + (should (ring-empty-p ring)))) + +(ert-deftest ring-tests-size () + (let ((ring (make-ring 2))) + (should (= (ring-size ring) 2)) + (ring-insert ring "a") + (ring-insert ring "b") + (ring-insert ring "c") + (should (= (ring-size ring) 2)) + (ring-extend ring 3) + (should (= (ring-size ring) 5)))) + +(ert-deftest ring-tests-copy () + (let ((ring1 (make-ring 3))) + (ring-insert ring1 1) + (ring-insert ring1 2) + (let ((ring2 (ring-copy ring1))) + (should-not (eq ring1 ring2)) + (should (= (ring-size ring1) (ring-size ring2))) + (should (equal (ring-elements ring1) (ring-elements ring2)))))) + +(ert-deftest ring-tests-insert () + (let ((ring (make-ring 2))) + (ring-insert ring :a) + (should (equal (ring-elements ring) '(:a))) + (ring-insert ring :b) + (should (equal (ring-elements ring) '(:b :a))) + (ring-insert ring :c) + (should (equal (ring-elements ring) '(:c :b))))) + +(ert-deftest ring-tests-remove () + (let ((ring (make-ring 2))) + (should-error (ring-remove ring)) + (ring-insert ring 'foo) + (ring-insert ring 'bar) + (should (eq (ring-remove ring) 'foo)) + (should (equal (ring-elements ring) '(bar))) + (ring-insert ring 'baz) + (should (eq (ring-remove ring 0) 'baz)) + (should (equal (ring-elements ring) '(bar))))) + +(ert-deftest ring-tests-ref () + (let ((ring (make-ring 2))) + (should-error (ring-ref ring 0)) + (ring-insert ring :a) + (should (eq (ring-ref ring 0) :a)) + (ring-insert ring :b) + (should (eq (ring-ref ring 0) :b)) + (should (eq (ring-ref ring 1) :a)) + (should (eq (ring-ref ring 2) :b)))) + +(ert-deftest ring-tests-elements () + (let ((ring (make-ring 5))) + (ring-insert ring 3) + (ring-insert ring 2) + (ring-insert ring 1) + (should (equal (ring-elements ring) '(1 2 3))))) + +(ert-deftest ring-tests-member () + (let ((ring (make-ring 3))) + (ring-insert ring "foo") + (ring-insert ring "bar") + (should (= (ring-member ring "foo") 1)) + (should (= (ring-member ring "bar") 0)) + (should-not (ring-member ring "baz")))) + +(ert-deftest ring-tests-next () + (let ((ring (make-ring 3))) + (ring-insert ring 'a) + (ring-insert ring 'b) + (should (eq (ring-next ring 'b) 'a)) + (should (eq (ring-next ring 'a) 'b)) + (should-error (ring-next ring 'c)))) + +(ert-deftest ring-tests-previous () + (let ((ring (make-ring 3))) + (ring-insert ring 'a) + (ring-insert ring 'b) + (should (eq (ring-previous ring 'b) 'a)) + (should (eq (ring-previous ring 'a) 'b)) + (should-error (ring-previous ring 'c)))) + +(ert-deftest ring-tests-extend () + (let ((ring (make-ring 2))) + (ring-insert ring 1) + (ring-insert ring 2) + (should (= (ring-size ring) 2)) + (should (= (ring-length ring) 2)) + (ring-extend ring 3) + (ring-insert ring 3) + (should (= (ring-size ring) 5)) + (should (equal (ring-elements ring) '(3 2 1))))) + +(ert-deftest ring-tests-insert () + (let ((ring (make-ring 2))) + (ring-insert+extend ring :a) + (ring-insert+extend ring :b) + (should (equal (ring-elements ring) '(:b :a))) + (ring-insert+extend ring :c) + (should (equal (ring-elements ring) '(:c :b))) + (ring-insert+extend ring :d t) + (should (equal (ring-elements ring) '(:d :c :b))))) + +(ert-deftest ring-tests-remove+insert+extend () + (let ((ring (make-ring 3))) + (ring-insert ring 1) + (ring-insert ring 1) + (ring-insert ring 2) + (ring-remove+insert+extend ring 1) + (should (equal (ring-elements ring) '(1 2))) + (ring-remove+insert+extend ring 0) + (should (equal (ring-elements ring) '(0 1 2))) + (ring-remove+insert+extend ring 3) + (should (equal (ring-elements ring) '(3 0 1))) + (ring-remove+insert+extend ring 4 t) + (should (equal (ring-elements ring) '(4 3 0 1))) + (ring-remove+insert+extend ring 1 t) + (should (equal (ring-elements ring) '(1 4 3 0))))) + +(ert-deftest ring-tests-convert-sequence-to-ring () + (let ((ring (ring-convert-sequence-to-ring '(a b c)))) + (should (equal (ring-elements ring) '(a b c)))) + (let ((ring (ring-convert-sequence-to-ring [1 2 3]))) + (should (equal (ring-elements ring) '(1 2 3)))) + (let ((ring (ring-convert-sequence-to-ring "abc"))) + (should (equal (ring-elements ring) '(?a ?b ?c)))) + (let ((ring (make-ring 2))) + (ring-insert ring :a) + (ring-insert ring :b) + (should + (equal (ring-elements (ring-convert-sequence-to-ring ring)) + (ring-elements ring))))) + +(provide 'ring-tests) +;;; ring-tests.el ends here |