1;;; data-tests.el --- tests for src/data.c -*- lexical-binding:t -*- 2 3;; Copyright (C) 2013-2021 Free Software Foundation, Inc. 4 5;; This file is part of GNU Emacs. 6 7;; GNU Emacs is free software: you can redistribute it and/or modify 8;; it under the terms of the GNU General Public License as published by 9;; the Free Software Foundation, either version 3 of the License, or 10;; (at your option) any later version. 11 12;; GNU Emacs is distributed in the hope that it will be useful, 13;; but WITHOUT ANY WARRANTY; without even the implied warranty of 14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15;; GNU General Public License for more details. 16 17;; You should have received a copy of the GNU General Public License 18;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 19 20;;; Commentary: 21 22;;; Code: 23 24(require 'cl-lib) 25 26(defconst data-tests--float-greater-than-fixnums (+ 1.0 most-positive-fixnum) 27 "A floating-point value that is greater than all fixnums. 28It is also as small as conveniently possible, to make the tests sharper. 29Adding 1.0 to `most-positive-fixnum' should suffice on all 30practical Emacs platforms, since the result is a power of 2 and 31this is exactly representable and is greater than 32`most-positive-fixnum', which is just less than a power of 2.") 33 34(ert-deftest data-tests-= () 35 (should-error (=)) 36 (should (= 1)) 37 (should (= 2 2)) 38 (should (= 9 9 9 9 9 9 9 9 9)) 39 (should (= most-negative-fixnum (float most-negative-fixnum))) 40 (should-not (= most-positive-fixnum data-tests--float-greater-than-fixnums)) 41 (should-not (apply #'= '(3 8 3))) 42 (should-error (= 9 9 'foo)) 43 ;; Short circuits before getting to bad arg 44 (should-not (= 9 8 'foo))) 45 46(ert-deftest data-tests-< () 47 (should-error (<)) 48 (should (< 1)) 49 (should (< 2 3)) 50 (should (< -6 -1 0 2 3 4 8 9 999)) 51 (should (< 0.5 most-positive-fixnum data-tests--float-greater-than-fixnums)) 52 (should-not (apply #'< '(3 8 3))) 53 (should-error (< 9 10 'foo)) 54 ;; Short circuits before getting to bad arg 55 (should-not (< 9 8 'foo))) 56 57(ert-deftest data-tests-> () 58 (should-error (>)) 59 (should (> 1)) 60 (should (> 3 2)) 61 (should (> 6 1 0 -2 -3 -4 -8 -9 -999)) 62 (should (> data-tests--float-greater-than-fixnums most-positive-fixnum 0.5)) 63 (should-not (apply #'> '(3 8 3))) 64 (should-error (> 9 8 'foo)) 65 ;; Short circuits before getting to bad arg 66 (should-not (> 8 9 'foo))) 67 68(ert-deftest data-tests-<= () 69 (should-error (<=)) 70 (should (<= 1)) 71 (should (<= 2 3)) 72 (should (<= -6 -1 -1 0 0 0 2 3 4 8 999)) 73 (should (<= 0.5 most-positive-fixnum data-tests--float-greater-than-fixnums)) 74 (should-not (apply #'<= '(3 8 3 3))) 75 (should-error (<= 9 10 'foo)) 76 ;; Short circuits before getting to bad arg 77 (should-not (<= 9 8 'foo))) 78 79(ert-deftest data-tests->= () 80 (should-error (>=)) 81 (should (>= 1)) 82 (should (>= 3 2)) 83 (should (>= 666 1 0 0 -2 -3 -3 -3 -4 -8 -8 -9 -999)) 84 (should (>= data-tests--float-greater-than-fixnums most-positive-fixnum)) 85 (should-not (apply #'>= '(3 8 3))) 86 (should-error (>= 9 8 'foo)) 87 ;; Short circuits before getting to bad arg 88 (should-not (>= 8 9 'foo))) 89 90(ert-deftest data-tests-max () 91 (should-error (max)) 92 (should (= 1 (max 1))) 93 (should (= 3 (max 3 2))) 94 (should (= 666 (max 666 1 0 0 -2 -3 -3 -3 -4 -8 -8 -9 -999))) 95 (should (= (1+ most-negative-fixnum) 96 (max (float most-negative-fixnum) (1+ most-negative-fixnum)))) 97 (should (= 8 (apply #'max '(3 8 3)))) 98 (should-error (max 9 8 'foo)) 99 (should-error (max (make-marker))) 100 (should (eql 1 (max (point-min-marker) 1)))) 101 102(ert-deftest data-tests-min () 103 (should-error (min)) 104 (should (= 1 (min 1))) 105 (should (= 2 (min 3 2))) 106 (should (= -999 (min 666 1 0 0 -2 -3 -3 -3 -4 -8 -8 -9 -999))) 107 (should (= most-positive-fixnum 108 (min data-tests--float-greater-than-fixnums most-positive-fixnum))) 109 (should (= 3 (apply #'min '(3 8 3)))) 110 (should-error (min 9 8 'foo)) 111 (should-error (min (make-marker))) 112 (should (eql 1 (min (point-min-marker) 1))) 113 (should (isnan (min 0.0e+NaN))) 114 (should (isnan (min 0.0e+NaN 1 2))) 115 (should (isnan (min 1.0 0.0e+NaN))) 116 (should (isnan (min 1.0 0.0e+NaN 1.1))) 117 (should (isnan (min 1.0 0.0e+NaN 1.1 (1+ most-positive-fixnum)))) 118 (should (isnan (max 1.0 0.0e+NaN 1.1 (1+ most-positive-fixnum))))) 119 120(defun data-tests-popcnt (byte) 121 "Calculate the Hamming weight of BYTE." 122 (if (< byte 0) 123 (setq byte (lognot byte))) 124 (if (zerop byte) 125 0 126 (+ (logand byte 1) (data-tests-popcnt (ash byte -1))))) 127 128(ert-deftest data-tests-logcount () 129 (should (cl-loop for n in (number-sequence -255 255) 130 always (= (logcount n) (data-tests-popcnt n)))) 131 ;; https://oeis.org/A000120 132 (should (= 11 (logcount 9727))) 133 (should (= 8 (logcount 9999)))) 134 135;; Bool vector tests. Compactly represent bool vectors as hex 136;; strings. 137 138(ert-deftest bool-vector-count-population-all-0-nil () 139 (cl-loop for sz in '(0 45 1 64 9 344) 140 do (let* ((bv (make-bool-vector sz nil))) 141 (should 142 (zerop 143 (bool-vector-count-population bv)))))) 144 145(ert-deftest bool-vector-count-population-all-1-t () 146 (cl-loop for sz in '(0 45 1 64 9 344) 147 do (let* ((bv (make-bool-vector sz t))) 148 (should 149 (eql 150 (bool-vector-count-population bv) 151 sz))))) 152 153(ert-deftest bool-vector-count-population-1-nil () 154 (let* ((bv (make-bool-vector 45 nil))) 155 (aset bv 40 t) 156 (aset bv 0 t) 157 (should 158 (eql 159 (bool-vector-count-population bv) 160 2)))) 161 162(ert-deftest bool-vector-count-population-1-t () 163 (let* ((bv (make-bool-vector 45 t))) 164 (aset bv 40 nil) 165 (aset bv 0 nil) 166 (should 167 (eql 168 (bool-vector-count-population bv) 169 43)))) 170 171(defun mock-bool-vector-count-consecutive (a b i) 172 (cl-loop for i from i below (length a) 173 while (eq (aref a i) b) 174 sum 1)) 175 176(defun test-bool-vector-bv-from-hex-string (desc) 177 (let (bv nibbles) 178 (dolist (c (string-to-list desc)) 179 (push (string-to-number 180 (char-to-string c) 181 16) 182 nibbles)) 183 (setf bv (make-bool-vector (* 4 (length nibbles)) nil)) 184 (let ((i 0)) 185 (dolist (n (nreverse nibbles)) 186 (dotimes (_ 4) 187 (aset bv i (> (logand 1 n) 0)) 188 (cl-incf i) 189 (setf n (ash n -1))))) 190 bv)) 191 192(defun test-bool-vector-to-hex-string (bv) 193 (let (nibbles (v (cl-coerce bv 'list))) 194 (while v 195 (push (logior 196 (ash (if (nth 0 v) 1 0) 0) 197 (ash (if (nth 1 v) 1 0) 1) 198 (ash (if (nth 2 v) 1 0) 2) 199 (ash (if (nth 3 v) 1 0) 3)) 200 nibbles) 201 (setf v (nthcdr 4 v))) 202 (mapconcat (lambda (n) (format "%X" n)) 203 (nreverse nibbles) 204 ""))) 205 206(defun test-bool-vector-count-consecutive-tc (desc) 207 "Run a test case for `bool-vector-count-consecutive'. 208DESC is a string describing the test. It is a sequence of 209hexadecimal digits describing the bool vector. We exhaustively 210test all counts at all possible positions in the vector by 211comparing the subr with a much slower Lisp implementation." 212 (let ((bv (test-bool-vector-bv-from-hex-string desc))) 213 (cl-loop 214 for lf in '(nil t) 215 do (cl-loop 216 for pos from 0 upto (length bv) 217 for cnt = (mock-bool-vector-count-consecutive bv lf pos) 218 for rcnt = (bool-vector-count-consecutive bv lf pos) 219 unless (eql cnt rcnt) 220 do (error "FAILED testcase %S %3S %3S %3S" 221 pos lf cnt rcnt))))) 222 223(defconst bool-vector-test-vectors 224'("" 225 "0" 226 "F" 227 "0F" 228 "F0" 229 "00000000000000000000000000000FFFFF0000000" 230 "44a50234053fba3340000023444a50234053fba33400000234" 231 "12341234123456123412346001234123412345612341234600" 232 "44a50234053fba33400000234" 233 "1234123412345612341234600" 234 "44a50234053fba33400000234" 235 "1234123412345612341234600" 236 "44a502340" 237 "123412341" 238 "0000000000000000000000000" 239 "FFFFFFFFFFFFFFFF1")) 240 241(ert-deftest bool-vector-count-consecutive () 242 (mapc #'test-bool-vector-count-consecutive-tc 243 bool-vector-test-vectors)) 244 245(defun test-bool-vector-apply-mock-op (mock a b c) 246 "Compute (slowly) the correct result of a bool-vector set operation." 247 (let (changed) 248 (cl-assert (eql (length b) (length c))) 249 (unless a 250 (setf a (make-bool-vector (length b) nil)) 251 (setf changed t)) 252 253 (cl-loop for i below (length b) 254 for mockr = (funcall mock 255 (if (aref b i) 1 0) 256 (if (aref c i) 1 0)) 257 for r = (not (= 0 mockr)) 258 do (progn 259 (unless (eq (aref a i) r) 260 (setf changed t)) 261 (setf (aref a i) r))) 262 (if changed a))) 263 264(defun test-bool-vector-binop (mock real) 265 "Test a binary set operation." 266 (cl-loop for s1 in bool-vector-test-vectors 267 for bv1 = (test-bool-vector-bv-from-hex-string s1) 268 for vecs2 = (cl-remove-if-not 269 (lambda (x) (eql (length x) (length s1))) 270 bool-vector-test-vectors) 271 do (cl-loop for s2 in vecs2 272 for bv2 = (test-bool-vector-bv-from-hex-string s2) 273 for mock-result = (test-bool-vector-apply-mock-op 274 mock nil bv1 bv2) 275 for real-result = (funcall real bv1 bv2) 276 do (progn 277 (should (equal mock-result real-result)))))) 278 279(ert-deftest bool-vector-intersection-op () 280 (test-bool-vector-binop 281 #'logand 282 #'bool-vector-intersection)) 283 284(ert-deftest bool-vector-union-op () 285 (test-bool-vector-binop 286 #'logior 287 #'bool-vector-union)) 288 289(ert-deftest bool-vector-xor-op () 290 (test-bool-vector-binop 291 #'logxor 292 #'bool-vector-exclusive-or)) 293 294(ert-deftest bool-vector-set-difference-op () 295 (test-bool-vector-binop 296 (lambda (a b) (logand a (lognot b))) 297 #'bool-vector-set-difference)) 298 299(ert-deftest bool-vector-change-detection () 300 (let* ((vc1 (test-bool-vector-bv-from-hex-string "abcdef")) 301 (vc2 (test-bool-vector-bv-from-hex-string "012345")) 302 (vc3 (make-bool-vector (length vc1) nil)) 303 (c1 (bool-vector-union vc1 vc2 vc3)) 304 (c2 (bool-vector-union vc1 vc2 vc3))) 305 (should (equal c1 (test-bool-vector-apply-mock-op 306 #'logior 307 nil 308 vc1 vc2))) 309 (should (not c2)))) 310 311(ert-deftest bool-vector-not () 312 (let* ((v1 (test-bool-vector-bv-from-hex-string "FFFF3")) 313 (v2 (test-bool-vector-bv-from-hex-string "0000C")) 314 (v3 (bool-vector-not v1))) 315 (should (equal v2 v3)))) 316 317;; Tests for variable bindings 318 319(defvar binding-test-buffer-A (get-buffer-create "A")) 320(defvar binding-test-buffer-B (get-buffer-create "B")) 321 322(defvar binding-test-always-local 'always) 323(make-variable-buffer-local 'binding-test-always-local) 324 325(defvar binding-test-some-local 'some) 326(with-current-buffer binding-test-buffer-A 327 (setq-local binding-test-some-local 'local)) 328 329(ert-deftest binding-test-manual () 330 "A test case from the elisp manual." 331 (with-current-buffer binding-test-buffer-A 332 (let ((binding-test-some-local 'something-else)) 333 (should (eq binding-test-some-local 'something-else)) 334 (set-buffer binding-test-buffer-B) 335 (should (eq binding-test-some-local 'some))) 336 (should (eq binding-test-some-local 'some)) 337 (set-buffer binding-test-buffer-A) 338 (should (eq binding-test-some-local 'local)))) 339 340(ert-deftest binding-test-setq-default () 341 "Test that a `setq-default' has no effect when there is a local binding." 342 (with-current-buffer binding-test-buffer-B 343 ;; This variable is not local in this buffer. 344 (let ((binding-test-some-local 'something-else)) 345 (setq-default binding-test-some-local 'new-default)) 346 (should (eq binding-test-some-local 'some)))) 347 348(ert-deftest data-tests--let-buffer-local () 349 (let ((blvar (make-symbol "blvar"))) 350 (set-default blvar nil) 351 (make-variable-buffer-local blvar) 352 353 (dolist (var (list blvar 'left-margin)) 354 (let ((def (default-value var))) 355 (with-temp-buffer 356 (should (equal def (symbol-value var))) 357 (cl-progv (list var) (list 42) 358 (should (equal (symbol-value var) 42)) 359 (should (equal (default-value var) (symbol-value var))) 360 (set var 123) 361 (should (not (local-variable-p var))) 362 (should (equal (symbol-value var) 123)) 363 (should (equal (default-value var) (symbol-value var)))) ;bug#44733 364 (should (equal (symbol-value var) def)) 365 (should (equal (default-value var) (symbol-value var)))) 366 (should (equal (default-value var) def)))))) 367 368(ert-deftest data-tests--let-buffer-local-no-unwind-other-buffers () 369 "Test that a let-binding for a buffer-local unwinds only current-buffer." 370 (let ((blvar (make-symbol "blvar"))) 371 (set-default blvar 0) 372 (make-variable-buffer-local blvar) 373 (dolist (var (list blvar 'left-margin)) 374 (let* ((def (default-value var)) 375 (newdef (+ def 1)) 376 (otherbuf (generate-new-buffer "otherbuf"))) 377 (with-temp-buffer 378 (cl-progv (list var) (list newdef) 379 (with-current-buffer otherbuf 380 (set var 123) 381 (should (local-variable-p var)) 382 (should (equal (symbol-value var) 123)) 383 (should (equal (default-value var) newdef)))) 384 (with-current-buffer otherbuf 385 (should (local-variable-p var)) 386 (should (equal (symbol-value var) 123)) 387 (should (equal (default-value var) def))) 388 ))))) 389 390(ert-deftest binding-test-makunbound () 391 "Tests of makunbound, from the manual." 392 (with-current-buffer binding-test-buffer-B 393 (should (boundp 'binding-test-some-local)) 394 (let ((binding-test-some-local 'outer)) 395 (let ((binding-test-some-local 'inner)) 396 (makunbound 'binding-test-some-local) 397 (should (not (boundp 'binding-test-some-local)))) 398 (should (and (boundp 'binding-test-some-local) 399 (eq binding-test-some-local 'outer)))))) 400 401(ert-deftest binding-test-defvar-bool () 402 "Test DEFVAR_BOOL." 403 (let ((display-hourglass 5)) 404 (should (eq display-hourglass t)))) 405 406(ert-deftest binding-test-defvar-int () 407 "Test DEFVAR_INT." 408 (should-error (setq gc-cons-threshold 5.0) :type 'wrong-type-argument)) 409 410(ert-deftest binding-test-set-constant-t () 411 "Test setting the constant t." 412 (with-no-warnings (should-error (setq t 'bob) :type 'setting-constant))) 413 414(ert-deftest binding-test-set-constant-nil () 415 "Test setting the constant nil." 416 (with-no-warnings (should-error (setq nil 'bob) :type 'setting-constant))) 417 418(ert-deftest binding-test-set-constant-keyword () 419 "Test setting a keyword constant." 420 (with-no-warnings (should-error (setq :keyword 'bob) :type 'setting-constant))) 421 422(ert-deftest binding-test-set-constant-itself () 423 "Test setting a keyword to itself." 424 (with-no-warnings (should (setq :keyword :keyword)))) 425 426(ert-deftest data-tests--set-default-per-buffer () 427 :expected-result t ;; Not fixed yet! 428 ;; FIXME: Performance tests are inherently unreliable. 429 ;; Using wall-clock time makes it even worse, so don't bother unless 430 ;; we have the primitive to measure cpu-time. 431 (skip-unless (fboundp 'current-cpu-time)) 432 ;; Test performance of set-default on DEFVAR_PER_BUFFER variables. 433 ;; More specifically, test the problem seen in bug#41029 where setting 434 ;; the default value of a variable takes time proportional to the 435 ;; number of buffers. 436 (when (fboundp 'current-cpu-time) ; silence byte-compiler 437 (let* ((fun #'error) 438 (test (lambda () 439 (with-temp-buffer 440 (let ((st (car (current-cpu-time)))) 441 (dotimes (_ 1000) 442 (let ((case-fold-search 'data-test)) 443 ;; Use an indirection through a mutable var 444 ;; to try and make sure the byte-compiler 445 ;; doesn't optimize away the let bindings. 446 (funcall fun))) 447 ;; FIXME: Handle the wraparound, if any. 448 (- (car (current-cpu-time)) st))))) 449 (_ (setq fun #'ignore)) 450 (time1 (funcall test)) 451 (bufs (mapcar (lambda (_) (generate-new-buffer " data-test")) 452 (make-list 1000 nil))) 453 (time2 (funcall test))) 454 (mapc #'kill-buffer bufs) 455 ;; Don't divide one time by the other since they may be 0. 456 (should (< time2 (* time1 5)))))) 457 458;; More tests to write - 459;; kill-local-variable 460;; defconst; can modify 461;; defvar and defconst modify the local binding [ doesn't matter for us ] 462;; various kinds of special internal forwarding objects 463;; a couple examples in manual, not enough 464;; variable aliases 465 466;; Tests for watchpoints 467 468(ert-deftest data-tests-variable-watchers () 469 (defvar data-tests-var 0) 470 (let* ((watch-data nil) 471 (collect-watch-data 472 (lambda (&rest args) (push args watch-data)))) 473 (cl-flet ((should-have-watch-data (data) 474 (should (equal (pop watch-data) data)) 475 (should (null watch-data)))) 476 (add-variable-watcher 'data-tests-var collect-watch-data) 477 (setq data-tests-var 1) 478 (should-have-watch-data '(data-tests-var 1 set nil)) 479 (let ((data-tests-var 2)) 480 (should-have-watch-data '(data-tests-var 2 let nil)) 481 (setq data-tests-var 3) 482 (should-have-watch-data '(data-tests-var 3 set nil))) 483 (should-have-watch-data '(data-tests-var 1 unlet nil)) 484 ;; `setq-default' on non-local variable is same as `setq'. 485 (setq-default data-tests-var 4) 486 (should-have-watch-data '(data-tests-var 4 set nil)) 487 (makunbound 'data-tests-var) 488 (should-have-watch-data '(data-tests-var nil makunbound nil)) 489 (setq data-tests-var 5) 490 (should-have-watch-data '(data-tests-var 5 set nil)) 491 (remove-variable-watcher 'data-tests-var collect-watch-data) 492 (setq data-tests-var 6) 493 (should (null watch-data))))) 494 495(ert-deftest data-tests-varalias-watchers () 496 (defvar data-tests-var0 0) 497 (defvar data-tests-var1 0) 498 (defvar data-tests-var2 0) 499 (defvar data-tests-var3 0) 500 (let* ((watch-data nil) 501 (collect-watch-data 502 (lambda (&rest args) (push args watch-data)))) 503 (cl-flet ((should-have-watch-data (data) 504 (should (equal (pop watch-data) data)) 505 (should (null watch-data)))) 506 ;; Watch var0, then alias it. 507 (add-variable-watcher 'data-tests-var0 collect-watch-data) 508 (defvar data-tests-var0-alias) 509 (defvaralias 'data-tests-var0-alias 'data-tests-var0) 510 (setq data-tests-var0 1) 511 (should-have-watch-data '(data-tests-var0 1 set nil)) 512 (setq data-tests-var0-alias 2) 513 (should-have-watch-data '(data-tests-var0 2 set nil)) 514 ;; Alias var1, then watch var1-alias. 515 (defvar data-tests-var1-alias) 516 (defvaralias 'data-tests-var1-alias 'data-tests-var1) 517 (add-variable-watcher 'data-tests-var1-alias collect-watch-data) 518 (setq data-tests-var1 1) 519 (should-have-watch-data '(data-tests-var1 1 set nil)) 520 (setq data-tests-var1-alias 2) 521 (should-have-watch-data '(data-tests-var1 2 set nil)) 522 ;; Alias var2, then watch it. 523 (defvar data-tests-var2-alias) 524 (defvaralias 'data-tests-var2-alias 'data-tests-var2) 525 (add-variable-watcher 'data-tests-var2 collect-watch-data) 526 (setq data-tests-var2 1) 527 (should-have-watch-data '(data-tests-var2 1 set nil)) 528 (setq data-tests-var2-alias 2) 529 (should-have-watch-data '(data-tests-var2 2 set nil)) 530 ;; Watch var3-alias, then make it alias var3 (this removes the 531 ;; watcher flag). 532 (defvar data-tests-var3-alias 0) 533 (add-variable-watcher 'data-tests-var3-alias collect-watch-data) 534 (defvaralias 'data-tests-var3-alias 'data-tests-var3) 535 (should-have-watch-data '(data-tests-var3-alias 536 data-tests-var3 defvaralias nil)) 537 (setq data-tests-var3 1) 538 (setq data-tests-var3-alias 2) 539 (should (null watch-data))))) 540 541(ert-deftest data-tests-local-variable-watchers () 542 (with-no-warnings 543 (defvar-local data-tests-lvar 0)) 544 (let* ((buf1 (current-buffer)) 545 (buf2 nil) 546 (watch-data nil) 547 (collect-watch-data 548 (lambda (&rest args) (push args watch-data)))) 549 (cl-flet ((should-have-watch-data (data) 550 (should (equal (pop watch-data) data)) 551 (should (null watch-data)))) 552 (add-variable-watcher 'data-tests-lvar collect-watch-data) 553 (setq data-tests-lvar 1) 554 (should-have-watch-data `(data-tests-lvar 1 set ,buf1)) 555 (let ((data-tests-lvar 2)) 556 (should-have-watch-data `(data-tests-lvar 2 let ,buf1)) 557 (setq data-tests-lvar 3) 558 (should-have-watch-data `(data-tests-lvar 3 set ,buf1))) 559 (should-have-watch-data `(data-tests-lvar 1 unlet ,buf1)) 560 (setq-default data-tests-lvar 4) 561 (should-have-watch-data '(data-tests-lvar 4 set nil)) 562 (with-temp-buffer 563 (setq buf2 (current-buffer)) 564 (setq data-tests-lvar 1) 565 (should-have-watch-data `(data-tests-lvar 1 set ,buf2)) 566 (let ((data-tests-lvar 2)) 567 (should-have-watch-data `(data-tests-lvar 2 let ,buf2)) 568 (setq data-tests-lvar 3) 569 (should-have-watch-data `(data-tests-lvar 3 set ,buf2))) 570 (should-have-watch-data `(data-tests-lvar 1 unlet ,buf2)) 571 (kill-local-variable 'data-tests-lvar) 572 (should-have-watch-data `(data-tests-lvar nil makunbound ,buf2)) 573 (setq data-tests-lvar 3.5) 574 (should-have-watch-data `(data-tests-lvar 3.5 set ,buf2)) 575 (kill-all-local-variables) 576 (should-have-watch-data `(data-tests-lvar nil makunbound ,buf2))) 577 (setq-default data-tests-lvar 4) 578 (should-have-watch-data '(data-tests-lvar 4 set nil)) 579 (makunbound 'data-tests-lvar) 580 (should-have-watch-data '(data-tests-lvar nil makunbound nil)) 581 (setq data-tests-lvar 5) 582 (should-have-watch-data `(data-tests-lvar 5 set ,buf1)) 583 (remove-variable-watcher 'data-tests-lvar collect-watch-data) 584 (setq data-tests-lvar 6) 585 (should (null watch-data))))) 586 587(ert-deftest data-tests-kill-all-local-variables () ;bug#30846 588 (with-temp-buffer 589 (setq-local data-tests-foo1 1) 590 (setq-local data-tests-foo2 2) 591 (setq-local data-tests-foo3 3) 592 (let ((oldfoo2 nil)) 593 (add-variable-watcher 'data-tests-foo2 594 (lambda (&rest _) 595 (setq oldfoo2 (bound-and-true-p data-tests-foo2)))) 596 (kill-all-local-variables) 597 (should (equal oldfoo2 '2)) ;Watcher is run before changing the var. 598 (should (not (or (bound-and-true-p data-tests-foo1) 599 (bound-and-true-p data-tests-foo2) 600 (bound-and-true-p data-tests-foo3))))))) 601 602(ert-deftest data-tests-bignum () 603 (should (bignump (+ most-positive-fixnum 1))) 604 (let ((f0 (+ (float most-positive-fixnum) 1)) 605 (f-1 (- (float most-negative-fixnum) 1)) 606 (b0 (+ most-positive-fixnum 1)) 607 (b-1 (- most-negative-fixnum 1))) 608 (should (> b0 -1)) 609 (should (> b0 f-1)) 610 (should (> b0 b-1)) 611 (should (>= b0 -1)) 612 (should (>= b0 f-1)) 613 (should (>= b0 b-1)) 614 (should (>= b-1 b-1)) 615 616 (should (< -1 b0)) 617 (should (< f-1 b0)) 618 (should (< b-1 b0)) 619 (should (<= -1 b0)) 620 (should (<= f-1 b0)) 621 (should (<= b-1 b0)) 622 (should (<= b-1 b-1)) 623 624 (should (= (+ f0 b0) (+ b0 f0))) 625 (should (= (+ f0 b-1) (+ b-1 f0))) 626 (should (= (+ f-1 b0) (+ b0 f-1))) 627 (should (= (+ f-1 b-1) (+ b-1 f-1))) 628 629 (should (= (* f0 b0) (* b0 f0))) 630 (should (= (* f0 b-1) (* b-1 f0))) 631 (should (= (* f-1 b0) (* b0 f-1))) 632 (should (= (* f-1 b-1) (* b-1 f-1))) 633 634 (should (= b0 f0)) 635 (should (= b0 b0)) 636 637 (should (/= b0 f-1)) 638 (should (/= b0 b-1)) 639 640 (should (/= b0 0.0e+NaN)) 641 (should (/= b-1 0.0e+NaN)))) 642 643(ert-deftest data-tests-+ () 644 (should-not (fixnump (+ most-positive-fixnum most-positive-fixnum))) 645 (should (> (+ most-positive-fixnum most-positive-fixnum) most-positive-fixnum)) 646 (should (eq (- (+ most-positive-fixnum most-positive-fixnum) 647 (+ most-positive-fixnum most-positive-fixnum)) 648 0))) 649 650(ert-deftest data-tests-/ () 651 (let* ((x (* most-positive-fixnum 8)) 652 (y (* most-negative-fixnum 8)) 653 (z (- y))) 654 (should (= most-positive-fixnum (/ x 8))) 655 (should (= most-negative-fixnum (/ y 8))) 656 (should (= -1 (/ y z))) 657 (should (= -1 (/ z y))) 658 (should (= 0 (/ x (* 2 x)))) 659 (should (= 0 (/ y (* 2 y)))) 660 (should (= 0 (/ z (* 2 z)))))) 661 662(ert-deftest data-tests-number-predicates () 663 (should (fixnump 0)) 664 (should (fixnump most-negative-fixnum)) 665 (should (fixnump most-positive-fixnum)) 666 (should (integerp (+ most-positive-fixnum 1))) 667 (should (integer-or-marker-p (+ most-positive-fixnum 1))) 668 (should (numberp (+ most-positive-fixnum 1))) 669 (should (number-or-marker-p (+ most-positive-fixnum 1))) 670 (should (natnump (+ most-positive-fixnum 1))) 671 (should-not (fixnump (+ most-positive-fixnum 1))) 672 (should (bignump (+ most-positive-fixnum 1)))) 673 674(ert-deftest data-tests-number-to-string () 675 (let* ((s "99999999999999999999999999999") 676 (v (read s))) 677 (should (equal (number-to-string v) s)))) 678 679(ert-deftest data-tests-1+ () 680 (should (> (1+ most-positive-fixnum) most-positive-fixnum)) 681 (should (fixnump (1+ (1- most-negative-fixnum))))) 682 683(ert-deftest data-tests-1- () 684 (should (< (1- most-negative-fixnum) most-negative-fixnum)) 685 (should (fixnump (1- (1+ most-positive-fixnum))))) 686 687(ert-deftest data-tests-logand () 688 (should (= -1 (logand) (logand -1) (logand -1 -1))) 689 (let ((n (1+ most-positive-fixnum))) 690 (should (= (logand -1 n) n))) 691 (let ((n (* 2 most-negative-fixnum))) 692 (should (= (logand -1 n) n)))) 693 694(ert-deftest data-tests-logcount-2 () 695 (should (= (logcount (read "#xffffffffffffffffffffffffffffffff")) 128))) 696 697(ert-deftest data-tests-logior () 698 (should (= -1 (logior -1) (logior -1 -1))) 699 (should (= -1 (logior most-positive-fixnum most-negative-fixnum)))) 700 701(ert-deftest data-tests-logxor () 702 (should (= -1 (logxor -1) (logxor -1 -1 -1))) 703 (let ((n (1+ most-positive-fixnum))) 704 (should (= (logxor -1 n) (lognot n))))) 705 706(ert-deftest data-tests-minmax () 707 (let ((a (- most-negative-fixnum 1)) 708 (b (+ most-positive-fixnum 1)) 709 (c 0)) 710 (should (= (min a b c) a)) 711 (should (= (max a b c) b)))) 712 713(defun data-tests-check-sign (x y) 714 (should (eq (cl-signum x) (cl-signum y)))) 715 716(ert-deftest data-tests-%-mod () 717 (let* ((b1 (+ most-positive-fixnum 1)) 718 (nb1 (- b1)) 719 (b3 (+ most-positive-fixnum 3)) 720 (nb3 (- b3))) 721 (data-tests-check-sign (% 1 3) (% b1 b3)) 722 (data-tests-check-sign (mod 1 3) (mod b1 b3)) 723 (data-tests-check-sign (% 1 -3) (% b1 nb3)) 724 (data-tests-check-sign (mod 1 -3) (mod b1 nb3)) 725 (data-tests-check-sign (% -1 3) (% nb1 b3)) 726 (data-tests-check-sign (mod -1 3) (mod nb1 b3)) 727 (data-tests-check-sign (% -1 -3) (% nb1 nb3)) 728 (data-tests-check-sign (mod -1 -3) (mod nb1 nb3)))) 729 730(ert-deftest data-tests-mod-0 () 731 (dolist (num (list (1- most-negative-fixnum) -1 0 1 732 (1+ most-positive-fixnum))) 733 (should-error (mod num 0))) 734 (when (ignore-errors (/ 0.0 0)) 735 (should (equal (abs (mod 0.0 0)) (abs (- 0.0 (/ 0.0 0))))))) 736 737(ert-deftest data-tests-ash-lsh () 738 (should (= (ash most-negative-fixnum 1) 739 (* most-negative-fixnum 2))) 740 (should (= (ash 0 (* 2 most-positive-fixnum)) 0)) 741 (should (= (ash 1000 (* 2 most-negative-fixnum)) 0)) 742 (should (= (ash -1000 (* 2 most-negative-fixnum)) -1)) 743 (should (= (ash (* 2 most-negative-fixnum) (* 2 most-negative-fixnum)) -1)) 744 (should (= (lsh most-negative-fixnum 1) 745 (* most-negative-fixnum 2))) 746 (should (= (ash (* 2 most-negative-fixnum) -1) 747 most-negative-fixnum)) 748 (should (= (lsh most-positive-fixnum -1) (/ most-positive-fixnum 2))) 749 (should (= (lsh most-negative-fixnum -1) (lsh (- most-negative-fixnum) -1))) 750 (should (= (lsh -1 -1) most-positive-fixnum)) 751 (should-error (lsh (1- most-negative-fixnum) -1))) 752 753(ert-deftest data-tests-make-local-forwarded-var () ;bug#34318 754 ;; Boy, this bug is tricky to trigger. You need to: 755 ;; - call make-local-variable on a forwarded var (i.e. one that 756 ;; has a corresponding C var linked via DEFVAR_(LISP|INT|BOOL)) 757 ;; - cause the C code to modify this variable from the C side of the 758 ;; forwarding, but this needs to happen before the var is accessed 759 ;; from the Lisp side and before we switch to another buffer. 760 ;; The trigger in bug#34318 doesn't exist any more because the C code has 761 ;; changed. Instead I found the trigger below. 762 (with-temp-buffer 763 (setq last-coding-system-used 'bug34318) 764 (make-local-variable 'last-coding-system-used) 765 ;; This should set last-coding-system-used to `no-conversion'. 766 (decode-coding-string "hello" nil) 767 (should (equal (list last-coding-system-used 768 (default-value 'last-coding-system-used)) 769 '(no-conversion bug34318))))) 770 771;;; data-tests.el ends here 772