aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Ohler2011-03-03 02:01:51 -0700
committerChristian Ohler2011-03-03 02:01:51 -0700
commitde69c0a8d1ff21a0bd5663a555e47285aa1c70e1 (patch)
tree657d4d8862a494ac6a2b13174943dad442d0735f
parent7c0d14414fd20b67f52cec2df87ca0601acf2c90 (diff)
downloademacs-de69c0a8d1ff21a0bd5663a555e47285aa1c70e1.tar.gz
emacs-de69c0a8d1ff21a0bd5663a555e47285aa1c70e1.zip
Added fast path to ERT explanation of `equal'.
* emacs-lisp/ert.el (ert--explain-equal): New function. (ert--explain-equal-rec): Renamed from `ert--explain-not-equal'. All callers changed. (ert--explain-equal-including-properties): Renamed from `ert--explain-not-equal-including-properties'. All callers changed. * automated/ert-tests.el (ert-test-explain-not-equal-keymaps): New test.
-rw-r--r--lisp/ChangeLog9
-rw-r--r--lisp/emacs-lisp/ert.el42
-rw-r--r--test/ChangeLog5
-rw-r--r--test/automated/ert-tests.el33
4 files changed, 59 insertions, 30 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index b4b7525872b..9602bf20af6 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,5 +1,14 @@
12011-03-03 Christian Ohler <ohler@gnu.org> 12011-03-03 Christian Ohler <ohler@gnu.org>
2 2
3 * emacs-lisp/ert.el (ert--explain-equal): New function.
4 (ert--explain-equal-rec): Renamed from `ert--explain-not-equal'.
5 All callers changed.
6 (ert--explain-equal-including-properties): Renamed from
7 `ert--explain-not-equal-including-properties'. All callers
8 changed.
9
102011-03-03 Christian Ohler <ohler@gnu.org>
11
3 * emacs-lisp/ert.el (ert--stats-set-test-and-result) 12 * emacs-lisp/ert.el (ert--stats-set-test-and-result)
4 (ert-char-for-test-result, ert-string-for-test-result) 13 (ert-char-for-test-result, ert-string-for-test-result)
5 (ert-run-tests-batch, ert--print-test-for-ewoc): 14 (ert-run-tests-batch, ert--print-test-for-ewoc):
diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el
index 9767ae7549e..5bd8fd01b1e 100644
--- a/lisp/emacs-lisp/ert.el
+++ b/lisp/emacs-lisp/ert.el
@@ -219,7 +219,7 @@ Emacs bug 6581 at URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=6581'."
219 ;; This implementation is inefficient. Rather than making it 219 ;; This implementation is inefficient. Rather than making it
220 ;; efficient, let's hope bug 6581 gets fixed so that we can delete 220 ;; efficient, let's hope bug 6581 gets fixed so that we can delete
221 ;; it altogether. 221 ;; it altogether.
222 (not (ert--explain-not-equal-including-properties a b))) 222 (not (ert--explain-equal-including-properties a b)))
223 223
224 224
225;;; Defining and locating tests. 225;;; Defining and locating tests.
@@ -571,16 +571,15 @@ failed."
571 (when (and (not firstp) (eq fast slow)) (return nil)))) 571 (when (and (not firstp) (eq fast slow)) (return nil))))
572 572
573(defun ert--explain-format-atom (x) 573(defun ert--explain-format-atom (x)
574 "Format the atom X for `ert--explain-not-equal'." 574 "Format the atom X for `ert--explain-equal'."
575 (typecase x 575 (typecase x
576 (fixnum (list x (format "#x%x" x) (format "?%c" x))) 576 (fixnum (list x (format "#x%x" x) (format "?%c" x)))
577 (t x))) 577 (t x)))
578 578
579(defun ert--explain-not-equal (a b) 579(defun ert--explain-equal-rec (a b)
580 "Explainer function for `equal'. 580 "Returns a programmer-readable explanation of why A and B are not `equal'.
581 581
582Returns a programmer-readable explanation of why A and B are not 582Returns nil if they are."
583`equal', or nil if they are."
584 (if (not (equal (type-of a) (type-of b))) 583 (if (not (equal (type-of a) (type-of b)))
585 `(different-types ,a ,b) 584 `(different-types ,a ,b)
586 (etypecase a 585 (etypecase a
@@ -598,13 +597,13 @@ Returns a programmer-readable explanation of why A and B are not
598 (loop for i from 0 597 (loop for i from 0
599 for ai in a 598 for ai in a
600 for bi in b 599 for bi in b
601 for xi = (ert--explain-not-equal ai bi) 600 for xi = (ert--explain-equal-rec ai bi)
602 do (when xi (return `(list-elt ,i ,xi))) 601 do (when xi (return `(list-elt ,i ,xi)))
603 finally (assert (equal a b) t))) 602 finally (assert (equal a b) t)))
604 (let ((car-x (ert--explain-not-equal (car a) (car b)))) 603 (let ((car-x (ert--explain-equal-rec (car a) (car b))))
605 (if car-x 604 (if car-x
606 `(car ,car-x) 605 `(car ,car-x)
607 (let ((cdr-x (ert--explain-not-equal (cdr a) (cdr b)))) 606 (let ((cdr-x (ert--explain-equal-rec (cdr a) (cdr b))))
608 (if cdr-x 607 (if cdr-x
609 `(cdr ,cdr-x) 608 `(cdr ,cdr-x)
610 (assert (equal a b) t) 609 (assert (equal a b) t)
@@ -618,7 +617,7 @@ Returns a programmer-readable explanation of why A and B are not
618 (loop for i from 0 617 (loop for i from 0
619 for ai across a 618 for ai across a
620 for bi across b 619 for bi across b
621 for xi = (ert--explain-not-equal ai bi) 620 for xi = (ert--explain-equal-rec ai bi)
622 do (when xi (return `(array-elt ,i ,xi))) 621 do (when xi (return `(array-elt ,i ,xi)))
623 finally (assert (equal a b) t)))) 622 finally (assert (equal a b) t))))
624 (atom (if (not (equal a b)) 623 (atom (if (not (equal a b))
@@ -627,7 +626,15 @@ Returns a programmer-readable explanation of why A and B are not
627 `(different-atoms ,(ert--explain-format-atom a) 626 `(different-atoms ,(ert--explain-format-atom a)
628 ,(ert--explain-format-atom b))) 627 ,(ert--explain-format-atom b)))
629 nil))))) 628 nil)))))
630(put 'equal 'ert-explainer 'ert--explain-not-equal) 629
630(defun ert--explain-equal (a b)
631 "Explainer function for `equal'."
632 ;; Do a quick comparison in C to avoid running our expensive
633 ;; comparison when possible.
634 (if (equal a b)
635 nil
636 (ert--explain-equal-rec a b)))
637(put 'equal 'ert-explainer 'ert--explain-equal)
631 638
632(defun ert--significant-plist-keys (plist) 639(defun ert--significant-plist-keys (plist)
633 "Return the keys of PLIST that have non-null values, in order." 640 "Return the keys of PLIST that have non-null values, in order."
@@ -658,8 +665,8 @@ key/value pairs in each list does not matter."
658 (value-b (plist-get b key))) 665 (value-b (plist-get b key)))
659 (assert (not (equal value-a value-b)) t) 666 (assert (not (equal value-a value-b)) t)
660 `(different-properties-for-key 667 `(different-properties-for-key
661 ,key ,(ert--explain-not-equal-including-properties value-a 668 ,key ,(ert--explain-equal-including-properties value-a
662 value-b))))) 669 value-b)))))
663 (cond (keys-in-a-not-in-b 670 (cond (keys-in-a-not-in-b
664 (explain-with-key (first keys-in-a-not-in-b))) 671 (explain-with-key (first keys-in-a-not-in-b)))
665 (keys-in-b-not-in-a 672 (keys-in-b-not-in-a
@@ -681,13 +688,16 @@ If SUFFIXP is non-nil, returns a suffix of S, otherwise a prefix."
681 (t 688 (t
682 (substring s 0 len))))) 689 (substring s 0 len)))))
683 690
684(defun ert--explain-not-equal-including-properties (a b) 691;; TODO(ohler): Once bug 6581 is fixed, rename this to
692;; `ert--explain-equal-including-properties-rec' and add a fast-path
693;; wrapper like `ert--explain-equal'.
694(defun ert--explain-equal-including-properties (a b)
685 "Explainer function for `ert-equal-including-properties'. 695 "Explainer function for `ert-equal-including-properties'.
686 696
687Returns a programmer-readable explanation of why A and B are not 697Returns a programmer-readable explanation of why A and B are not
688`ert-equal-including-properties', or nil if they are." 698`ert-equal-including-properties', or nil if they are."
689 (if (not (equal a b)) 699 (if (not (equal a b))
690 (ert--explain-not-equal a b) 700 (ert--explain-equal a b)
691 (assert (stringp a) t) 701 (assert (stringp a) t)
692 (assert (stringp b) t) 702 (assert (stringp b) t)
693 (assert (eql (length a) (length b)) t) 703 (assert (eql (length a) (length b)) t)
@@ -713,7 +723,7 @@ Returns a programmer-readable explanation of why A and B are not
713 ))) 723 )))
714(put 'ert-equal-including-properties 724(put 'ert-equal-including-properties
715 'ert-explainer 725 'ert-explainer
716 'ert--explain-not-equal-including-properties) 726 'ert--explain-equal-including-properties)
717 727
718 728
719;;; Implementation of `ert-info'. 729;;; Implementation of `ert-info'.
diff --git a/test/ChangeLog b/test/ChangeLog
index dbfc6c6cefe..8b7feaddf62 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,8 @@
12011-03-03 Christian Ohler <ohler@gnu.org>
2
3 * automated/ert-tests.el (ert-test-explain-not-equal-keymaps):
4 New test.
5
12011-02-20 Ulf Jasper <ulf.jasper@web.de> 62011-02-20 Ulf Jasper <ulf.jasper@web.de>
2 7
3 * automated/icalendar-tests.el: Move from icalendar-testsuite.el; 8 * automated/icalendar-tests.el: Move from icalendar-testsuite.el;
diff --git a/test/automated/ert-tests.el b/test/automated/ert-tests.el
index b6d70dee7e2..cea994f64b8 100644
--- a/test/automated/ert-tests.el
+++ b/test/automated/ert-tests.el
@@ -796,27 +796,32 @@ This macro is used to test if macroexpansion in `should' works."
796 (should (equal (ert--string-first-line "foo\nbar") "foo")) 796 (should (equal (ert--string-first-line "foo\nbar") "foo"))
797 (should (equal (ert--string-first-line " foo\nbar\nbaz\n") " foo"))) 797 (should (equal (ert--string-first-line " foo\nbar\nbaz\n") " foo")))
798 798
799(ert-deftest ert-test-explain-not-equal () 799(ert-deftest ert-test-explain-equal ()
800 (should (equal (ert--explain-not-equal nil 'foo) 800 (should (equal (ert--explain-equal nil 'foo)
801 '(different-atoms nil foo))) 801 '(different-atoms nil foo)))
802 (should (equal (ert--explain-not-equal '(a a) '(a b)) 802 (should (equal (ert--explain-equal '(a a) '(a b))
803 '(list-elt 1 (different-atoms a b)))) 803 '(list-elt 1 (different-atoms a b))))
804 (should (equal (ert--explain-not-equal '(1 48) '(1 49)) 804 (should (equal (ert--explain-equal '(1 48) '(1 49))
805 '(list-elt 1 (different-atoms (48 "#x30" "?0") 805 '(list-elt 1 (different-atoms (48 "#x30" "?0")
806 (49 "#x31" "?1"))))) 806 (49 "#x31" "?1")))))
807 (should (equal (ert--explain-not-equal 'nil '(a)) 807 (should (equal (ert--explain-equal 'nil '(a))
808 '(different-types nil (a)))) 808 '(different-types nil (a))))
809 (should (equal (ert--explain-not-equal '(a b c) '(a b c d)) 809 (should (equal (ert--explain-equal '(a b c) '(a b c d))
810 '(proper-lists-of-different-length 3 4 (a b c) (a b c d) 810 '(proper-lists-of-different-length 3 4 (a b c) (a b c d)
811 first-mismatch-at 3))) 811 first-mismatch-at 3)))
812 (let ((sym (make-symbol "a"))) 812 (let ((sym (make-symbol "a")))
813 (should (equal (ert--explain-not-equal 'a sym) 813 (should (equal (ert--explain-equal 'a sym)
814 `(different-symbols-with-the-same-name a ,sym))))) 814 `(different-symbols-with-the-same-name a ,sym)))))
815 815
816(ert-deftest ert-test-explain-not-equal-improper-list () 816(ert-deftest ert-test-explain-equal-improper-list ()
817 (should (equal (ert--explain-not-equal '(a . b) '(a . c)) 817 (should (equal (ert--explain-equal '(a . b) '(a . c))
818 '(cdr (different-atoms b c))))) 818 '(cdr (different-atoms b c)))))
819 819
820(ert-deftest ert-test-explain-equal-keymaps ()
821 ;; This used to be very slow.
822 (should (equal (make-keymap) (make-keymap)))
823 (should (equal (make-sparse-keymap) (make-sparse-keymap))))
824
820(ert-deftest ert-test-significant-plist-keys () 825(ert-deftest ert-test-significant-plist-keys ()
821 (should (equal (ert--significant-plist-keys '()) '())) 826 (should (equal (ert--significant-plist-keys '()) '()))
822 (should (equal (ert--significant-plist-keys '(a b c d e f c g p q r nil s t)) 827 (should (equal (ert--significant-plist-keys '(a b c d e f c g p q r nil s t))
@@ -852,21 +857,21 @@ This macro is used to test if macroexpansion in `should' works."
852 (should (equal (ert--abbreviate-string "bar" 1 t) "r")) 857 (should (equal (ert--abbreviate-string "bar" 1 t) "r"))
853 (should (equal (ert--abbreviate-string "bar" 0 t) ""))) 858 (should (equal (ert--abbreviate-string "bar" 0 t) "")))
854 859
855(ert-deftest ert-test-explain-not-equal-string-properties () 860(ert-deftest ert-test-explain-equal-string-properties ()
856 (should 861 (should
857 (equal (ert--explain-not-equal-including-properties #("foo" 0 1 (a b)) 862 (equal (ert--explain-equal-including-properties #("foo" 0 1 (a b))
858 "foo") 863 "foo")
859 '(char 0 "f" 864 '(char 0 "f"
860 (different-properties-for-key a (different-atoms b nil)) 865 (different-properties-for-key a (different-atoms b nil))
861 context-before "" 866 context-before ""
862 context-after "oo"))) 867 context-after "oo")))
863 (should (equal (ert--explain-not-equal-including-properties 868 (should (equal (ert--explain-equal-including-properties
864 #("foo" 1 3 (a b)) 869 #("foo" 1 3 (a b))
865 #("goo" 0 1 (c d))) 870 #("goo" 0 1 (c d)))
866 '(array-elt 0 (different-atoms (?f "#x66" "?f") 871 '(array-elt 0 (different-atoms (?f "#x66" "?f")
867 (?g "#x67" "?g"))))) 872 (?g "#x67" "?g")))))
868 (should 873 (should
869 (equal (ert--explain-not-equal-including-properties 874 (equal (ert--explain-equal-including-properties
870 #("foo" 0 1 (a b c d) 1 3 (a b)) 875 #("foo" 0 1 (a b c d) 1 3 (a b))
871 #("foo" 0 1 (c d a b) 1 2 (a foo))) 876 #("foo" 0 1 (c d a b) 1 2 (a foo)))
872 '(char 1 "o" (different-properties-for-key a (different-atoms b foo)) 877 '(char 1 "o" (different-properties-for-key a (different-atoms b foo))