aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
Diffstat (limited to 'test/src')
-rw-r--r--test/src/emacs-module-tests.el148
1 files changed, 148 insertions, 0 deletions
diff --git a/test/src/emacs-module-tests.el b/test/src/emacs-module-tests.el
new file mode 100644
index 00000000000..93e85ae22db
--- /dev/null
+++ b/test/src/emacs-module-tests.el
@@ -0,0 +1,148 @@
1;;; Test GNU Emacs modules.
2
3;; Copyright 2015-2017 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 <http://www.gnu.org/licenses/>. */
19
20(require 'ert)
21
22(require 'mod-test
23 (expand-file-name "data/emacs-module/mod-test"
24 (getenv "EMACS_TEST_DIRECTORY")))
25
26;;
27;; Basic tests.
28;;
29
30(ert-deftest mod-test-sum-test ()
31 (should (= (mod-test-sum 1 2) 3))
32 (let ((descr (should-error (mod-test-sum 1 2 3))))
33 (should (eq (car descr) 'wrong-number-of-arguments))
34 (should (stringp (nth 1 descr)))
35 (should (eq 0
36 (string-match
37 (concat "#<module function "
38 "\\(at \\(0x\\)?[0-9a-fA-F]+\\( from .*\\)?"
39 "\\|Fmod_test_sum from .*\\)>")
40 (nth 1 descr))))
41 (should (= (nth 2 descr) 3)))
42 (should-error (mod-test-sum "1" 2) :type 'wrong-type-argument)
43 (should-error (mod-test-sum 1 "2") :type 'wrong-type-argument)
44 ;; The following tests are for 32-bit build --with-wide-int.
45 (should (= (mod-test-sum -1 most-positive-fixnum)
46 (1- most-positive-fixnum)))
47 (should (= (mod-test-sum 1 most-negative-fixnum)
48 (1+ most-negative-fixnum)))
49 (when (< #x1fffffff most-positive-fixnum)
50 (should (= (mod-test-sum 1 #x1fffffff)
51 (1+ #x1fffffff)))
52 (should (= (mod-test-sum -1 #x20000000)
53 #x1fffffff)))
54 (should-error (mod-test-sum 1 most-positive-fixnum)
55 :type 'overflow-error)
56 (should-error (mod-test-sum -1 most-negative-fixnum)
57 :type 'overflow-error))
58
59(ert-deftest mod-test-sum-docstring ()
60 (should (string= (documentation 'mod-test-sum) "Return A + B")))
61
62;;
63;; Non-local exists (throw, signal).
64;;
65
66(ert-deftest mod-test-non-local-exit-signal-test ()
67 (should-error (mod-test-signal))
68 (let (debugger-args backtrace)
69 (should-error
70 (let ((debugger (lambda (&rest args)
71 (setq debugger-args args
72 backtrace (with-output-to-string (backtrace)))
73 (cl-incf num-nonmacro-input-events)))
74 (debug-on-signal t))
75 (mod-test-signal)))
76 (should (equal debugger-args '(error (error . 56))))
77 (should (string-match-p
78 (rx bol " internal--module-call(" (+ nonl) ?\) ?\n
79 " apply(internal--module-call " (+ nonl) ?\) ?\n
80 " mod-test-signal()" eol)
81 backtrace))))
82
83(ert-deftest mod-test-non-local-exit-throw-test ()
84 (should (equal
85 (catch 'tag
86 (mod-test-throw)
87 (ert-fail "expected throw"))
88 65)))
89
90(ert-deftest mod-test-non-local-exit-funcall-normal ()
91 (should (equal (mod-test-non-local-exit-funcall (lambda () 23))
92 23)))
93
94(ert-deftest mod-test-non-local-exit-funcall-signal ()
95 (should (equal (mod-test-non-local-exit-funcall
96 (lambda () (signal 'error '(32))))
97 '(signal error (32)))))
98
99(ert-deftest mod-test-non-local-exit-funcall-throw ()
100 (should (equal (mod-test-non-local-exit-funcall (lambda () (throw 'tag 32)))
101 '(throw tag 32))))
102
103;;
104;; String tests.
105;;
106
107(defun multiply-string (s n)
108 (let ((res ""))
109 (dotimes (i n res)
110 (setq res (concat res s)))))
111
112(ert-deftest mod-test-globref-make-test ()
113 (let ((mod-str (mod-test-globref-make))
114 (ref-str (multiply-string "abcdefghijklmnopqrstuvwxyz" 100)))
115 (garbage-collect) ;; XXX: not enough to really test but it's something..
116 (should (string= ref-str mod-str))))
117
118(ert-deftest mod-test-string-a-to-b-test ()
119 (should (string= (mod-test-string-a-to-b "aaa") "bbb")))
120
121;;
122;; User-pointer tests.
123;;
124
125(ert-deftest mod-test-userptr-fun-test ()
126 (let* ((n 42)
127 (v (mod-test-userptr-make n))
128 (r (mod-test-userptr-get v)))
129
130 (should (eq (type-of v) 'user-ptr))
131 (should (integerp r))
132 (should (= r n))))
133
134;; TODO: try to test finalizer
135
136;;
137;; Vector tests.
138;;
139
140(ert-deftest mod-test-vector-test ()
141 (dolist (s '(2 10 100 1000))
142 (dolist (e '(42 foo "foo"))
143 (let* ((v-ref (make-vector 2 e))
144 (eq-ref (eq (aref v-ref 0) (aref v-ref 1)))
145 (v-test (make-vector s nil)))
146
147 (should (eq (mod-test-vector-fill v-test e) t))
148 (should (eq (mod-test-vector-eq v-test e) eq-ref))))))