1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
;;; ring-tests.el --- Tests for ring.el -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2025 Free Software Foundation, Inc.
;; Author: Simen Heggestøyl <simenheg@gmail.com>
;; Keywords:
;; This file is part of GNU Emacs.
;; 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 <https://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-resize/grow ()
(let ((ring (make-ring 3)))
(ring-insert ring 1)
(ring-insert ring 2)
(ring-insert ring 3)
(ring-resize ring 5)
(should (= (ring-size ring) 5))
(should (equal (ring-elements ring) '(3 2 1)))))
(ert-deftest ring-resize/grow-empty ()
(let ((ring (make-ring 3)))
(ring-resize ring 5)
(should (= (ring-size ring) 5))
(should (equal (ring-elements ring) '()))))
(ert-deftest ring-resize/grow-wrapped-ring ()
(let ((ring (make-ring 3)))
(ring-insert ring 1)
(ring-insert ring 2)
(ring-insert ring 3)
(ring-insert ring 4)
(ring-insert ring 5)
(ring-resize ring 5)
(should (= (ring-size ring) 5))
(should (equal (ring-elements ring) '(5 4 3)))))
(ert-deftest ring-resize/shrink ()
(let ((ring (make-ring 5)))
(ring-insert ring 1)
(ring-insert ring 2)
(ring-insert ring 3)
(ring-insert ring 4)
(ring-insert ring 5)
(ring-resize ring 3)
(should (= (ring-size ring) 3))
(should (equal (ring-elements ring) '(5 4 3)))))
(ert-deftest ring-tests-insert-2 ()
(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
|