aboutsummaryrefslogtreecommitdiffstats
path: root/modules/mod-test
diff options
context:
space:
mode:
authorAurélien Aptel2015-11-16 01:00:25 +0100
committerTed Zlatanov2015-11-18 14:24:35 -0500
commit955e25dbcd0519d115f58b275923a71c04579e83 (patch)
treea75f83e478b62b1de689c1e8c91e4a6cb67f68f3 /modules/mod-test
parent218caccd968d16a1a8d1f336e72f211c3e169142 (diff)
downloademacs-955e25dbcd0519d115f58b275923a71c04579e83.tar.gz
emacs-955e25dbcd0519d115f58b275923a71c04579e83.zip
Add dynamic module test and helper script
Add 'modhelp.py' script (python2) to automate module testing and module generation. To build and test all modules in the modules/ dir $ ./modhelp.py test To generate a module from template code (good starting point) $ ./modhelp init mynewtestmodule See the script -h option for more documentation. * modules/modhelp.py: New module helper script. * modules/mod-test/Makefile: New file. Makefile for the test module. * modules/mod-test/mod-test.c: New file. Test module source file. * modules/mod-test/test.el: New file. ert test suite for the test module. * modules/.gitignore: New file. Local .gitignore file. Co-authored-by: Philipp Stephani <phst@google.com>
Diffstat (limited to 'modules/mod-test')
-rw-r--r--modules/mod-test/Makefile15
-rw-r--r--modules/mod-test/mod-test.c254
-rw-r--r--modules/mod-test/test.el91
3 files changed, 360 insertions, 0 deletions
diff --git a/modules/mod-test/Makefile b/modules/mod-test/Makefile
new file mode 100644
index 00000000000..18778f00599
--- /dev/null
+++ b/modules/mod-test/Makefile
@@ -0,0 +1,15 @@
1
2ROOT = ../..
3
4CC = gcc
5LD = gcc
6CFLAGS = -ggdb3 -Wall
7LDFLAGS =
8
9all: mod-test.so
10
11%.so: %.o
12 $(LD) -shared $(LDFLAGS) -o $@ $<
13
14%.o: %.c
15 $(CC) $(CFLAGS) -I$(ROOT)/src -fPIC -c $<
diff --git a/modules/mod-test/mod-test.c b/modules/mod-test/mod-test.c
new file mode 100644
index 00000000000..2de53152b1b
--- /dev/null
+++ b/modules/mod-test/mod-test.c
@@ -0,0 +1,254 @@
1#include <assert.h>
2#include <stdio.h>
3#include <emacs_module.h>
4
5int plugin_is_GPL_compatible;
6
7/*
8 * Always return symbol 't'
9 */
10static emacs_value Fmod_test_return_t (emacs_env *env, int nargs, emacs_value args[], void *data)
11{
12 return env->intern (env, "t");
13}
14
15
16/*
17 * Expose simple sum function
18 */
19static int64_t sum (int64_t a, int64_t b)
20{
21 return a + b;
22}
23
24static emacs_value Fmod_test_sum (emacs_env *env, int nargs, emacs_value args[], void* data)
25{
26 int64_t a = env->extract_integer (env, args[0]);
27 int64_t b = env->extract_integer (env, args[1]);
28
29 int64_t r = sum(a, b);
30
31 return env->make_integer (env, r);
32}
33
34
35/*
36 * Signal '(error 56)
37 */
38static emacs_value Fmod_test_signal (emacs_env *env, int nargs, emacs_value args[], void* data)
39{
40 assert (env->non_local_exit_check (env) == emacs_funcall_exit_return);
41 env->non_local_exit_signal (env, env->intern (env, "error"), env->make_integer (env, 56));
42 return NULL;
43}
44
45
46/*
47 * Throw '(tag 65)
48 */
49static emacs_value Fmod_test_throw (emacs_env *env, int nargs, emacs_value args[], void* data)
50{
51 assert (env->non_local_exit_check (env) == emacs_funcall_exit_return);
52 env->non_local_exit_throw (env, env->intern (env, "tag"), env->make_integer (env, 65));
53 return NULL;
54}
55
56
57/*
58 * Call argument function, catch all non-local exists and return
59 * either normal result or a list describing the non-local exit.
60 */
61static emacs_value Fmod_test_non_local_exit_funcall (emacs_env *env, int nargs, emacs_value args[], void* data)
62{
63 assert (nargs == 1);
64 const emacs_value result = env->funcall (env, args[0], 0, NULL);
65 emacs_value non_local_exit_symbol, non_local_exit_data;
66 enum emacs_funcall_exit code = env->non_local_exit_get (env, &non_local_exit_symbol, &non_local_exit_data);
67 switch (code)
68 {
69 case emacs_funcall_exit_return:
70 return result;
71 case emacs_funcall_exit_signal:
72 {
73 env->non_local_exit_clear (env);
74 const emacs_value Flist = env->intern (env, "list");
75 emacs_value list_args[] = {env->intern (env, "signal"), non_local_exit_symbol, non_local_exit_data};
76 return env->funcall (env, Flist, 3, list_args);
77 }
78 case emacs_funcall_exit_throw:
79 {
80 env->non_local_exit_clear (env);
81 const emacs_value Flist = env->intern (env, "list");
82 emacs_value list_args[] = {env->intern (env, "throw"), non_local_exit_symbol, non_local_exit_data};
83 return env->funcall (env, Flist, 3, list_args);
84 }
85 }
86 /* never reached */
87 return env->intern (env, "nil");;
88}
89
90
91/*
92 * Return a global referrence
93 */
94static emacs_value Fmod_test_globref_make (emacs_env *env, int nargs, emacs_value args[], void* data)
95{
96 /* make a big string and make it global */
97 size_t i;
98 char str[26*100];
99
100 for (i = 0; i < sizeof (str); i++)
101 {
102 str[i] = 'a' + (i % 26);
103 }
104
105 /* we don't need to null-terminate str */
106 emacs_value lisp_str = env->make_string (env, str, sizeof (str));
107 return env->make_global_ref (env, lisp_str);
108}
109
110
111/*
112 * Return a copy of the argument string where every 'a' is replaced with 'b'.
113 */
114static emacs_value Fmod_test_string_a_to_b (emacs_env *env, int nargs, emacs_value args[], void* data)
115{
116 emacs_value lisp_str = args[0];
117 size_t size = 0;
118 char * buf = NULL;
119 size_t i;
120
121 env->copy_string_contents (env, lisp_str, buf, &size);
122 buf = malloc (size);
123 env->copy_string_contents (env, lisp_str, buf, &size);
124
125 for (i = 0; i+1 < size; i++) {
126 if (buf[i] == 'a')
127 buf[i] = 'b';
128 }
129
130 return env->make_string (env, buf, size-1);
131}
132
133
134/*
135 * Embedded pointers in lisp objects.
136 */
137
138/* C struct (pointer to) that will be embedded */
139struct super_struct
140{
141 int amazing_int;
142 char large_unused_buffer[512];
143};
144
145/* Associated finalizer */
146static void finalizer (void *p)
147{
148 if (p)
149 free (p);
150}
151
152/*
153 * Return a new user-pointer to a super_struct, with amazing_int set
154 * to the passed parameter.
155 */
156static emacs_value Fmod_test_userptr_make (emacs_env *env, int nargs, emacs_value args[], void *data)
157{
158 struct super_struct *p = calloc (1, sizeof(*p));
159 p->amazing_int = env->extract_integer (env, args[0]);
160 return env->make_user_ptr (env, finalizer, p);
161}
162
163/*
164 * Return the amazing_int of a passed 'user-pointer to a super_struct'.
165 */
166static emacs_value Fmod_test_userptr_get (emacs_env *env, int nargs, emacs_value args[], void *data)
167{
168 struct super_struct *p = env->get_user_ptr (env, args[0]);
169 return env->make_integer (env, p->amazing_int);
170}
171
172
173/*
174 * Fill vector in args[0] with value in args[1]
175 */
176static emacs_value Fmod_test_vector_fill (emacs_env *env, int nargs, emacs_value args[], void *data)
177{
178 size_t i;
179 emacs_value vec = args[0];
180 emacs_value val = args[1];
181 const size_t size = env->vec_size (env, vec);
182 for (i = 0; i < size; i++)
183 env->vec_set (env, vec, i, val);
184 return env->intern (env, "t");
185}
186
187
188/*
189 * Return whether all elements of vector in args[0] are 'eq' to value in args[1]
190 */
191static emacs_value Fmod_test_vector_eq (emacs_env *env, int nargs, emacs_value args[], void *data)
192{
193 size_t i;
194 emacs_value vec = args[0];
195 emacs_value val = args[1];
196 const size_t size = env->vec_size (env, vec);
197 for (i = 0; i < size; i++)
198 if (!env->eq (env, env->vec_get (env, vec, i), val))
199 return env->intern (env, "nil");
200 return env->intern (env, "t");
201}
202
203
204/*
205 * Lisp utilities for easier readability (simple wrappers)
206 */
207
208/* Provide FEATURE to Emacs */
209static void provide (emacs_env *env, const char *feature)
210{
211 emacs_value Qfeat = env->intern (env, feature);
212 emacs_value Qprovide = env->intern (env, "provide");
213 emacs_value args[] = { Qfeat };
214
215 env->funcall (env, Qprovide, 1, args);
216}
217
218/* Binds NAME to FUN */
219static void bind_function (emacs_env *env, const char *name, emacs_value Sfun)
220{
221 emacs_value Qfset = env->intern (env, "fset");
222 emacs_value Qsym = env->intern (env, name);
223 emacs_value args[] = { Qsym, Sfun };
224
225 env->funcall (env, Qfset, 2, args);
226}
227
228/*
229 * Module init function.
230 */
231int emacs_module_init (struct emacs_runtime *ert)
232{
233 emacs_env *env = ert->get_environment (ert);
234
235#define DEFUN(lsym, csym, amin, amax, doc, data) \
236 bind_function (env, lsym, env->make_function (env, amin, amax, csym, doc, data))
237
238 DEFUN ("mod-test-return-t", Fmod_test_return_t, 1, 1, NULL, NULL);
239 DEFUN ("mod-test-sum", Fmod_test_sum, 2, 2, "Return A + B", NULL);
240 DEFUN ("mod-test-signal", Fmod_test_signal, 0, 0, NULL, NULL);
241 DEFUN ("mod-test-throw", Fmod_test_throw, 0, 0, NULL, NULL);
242 DEFUN ("mod-test-non-local-exit-funcall", Fmod_test_non_local_exit_funcall, 1, 1, NULL, NULL);
243 DEFUN ("mod-test-globref-make", Fmod_test_globref_make, 0, 0, NULL, NULL);
244 DEFUN ("mod-test-string-a-to-b", Fmod_test_string_a_to_b, 1, 1, NULL, NULL);
245 DEFUN ("mod-test-userptr-make", Fmod_test_userptr_make, 1, 1, NULL, NULL);
246 DEFUN ("mod-test-userptr-get", Fmod_test_userptr_get, 1, 1, NULL, NULL);
247 DEFUN ("mod-test-vector-fill", Fmod_test_vector_fill, 2, 2, NULL, NULL);
248 DEFUN ("mod-test-vector-eq", Fmod_test_vector_eq, 2, 2, NULL, NULL);
249
250#undef DEFUN
251
252 provide (env, "mod-test");
253 return 0;
254}
diff --git a/modules/mod-test/test.el b/modules/mod-test/test.el
new file mode 100644
index 00000000000..1242b6a2ec2
--- /dev/null
+++ b/modules/mod-test/test.el
@@ -0,0 +1,91 @@
1;;
2;; Dynamic modules tests
3;;
4
5(require 'ert)
6
7(add-to-list 'load-path (file-name-directory (or #$ (expand-file-name (buffer-file-name)))))
8(require 'mod-test)
9
10;;
11;; basic tests
12;;
13
14(ert-deftest mod-test-sum-test ()
15 (should (= (mod-test-sum 1 2) 3)))
16
17(ert-deftest mod-test-sum-docstring ()
18 (should (string= (documentation 'mod-test-sum) "Return A + B")))
19
20;;
21;; non-local exists (throw, signal)
22;;
23
24(ert-deftest mod-test-non-local-exit-signal-test ()
25 (should-error (mod-test-signal)))
26
27(ert-deftest mod-test-non-local-exit-throw-test ()
28 (should (equal
29 (catch 'tag
30 (mod-test-throw)
31 (ert-fail "expected throw"))
32 65)))
33
34(ert-deftest mod-test-non-local-exit-funcall-normal ()
35 (should (equal (mod-test-non-local-exit-funcall (lambda () 23))
36 23)))
37
38(ert-deftest mod-test-non-local-exit-funcall-signal ()
39 (should (equal (mod-test-non-local-exit-funcall (lambda () (signal 'error '(32))))
40 '(signal error (32)))))
41
42(ert-deftest mod-test-non-local-exit-funcall-throw ()
43 (should (equal (mod-test-non-local-exit-funcall (lambda () (throw 'tag 32)))
44 '(throw tag 32))))
45
46;;
47;; string
48;;
49
50(defun multiply-string (s n)
51 (let ((res ""))
52 (dotimes (i n res)
53 (setq res (concat res s)))))
54
55(ert-deftest mod-test-globref-make-test ()
56 (let ((mod-str (mod-test-globref-make))
57 (ref-str (multiply-string "abcdefghijklmnopqrstuvwxyz" 100)))
58 (garbage-collect) ;; XXX: not enough to really test but it's something..
59 (should (string= ref-str mod-str))))
60
61(ert-deftest mod-test-string-a-to-b-test ()
62 (should (string= (mod-test-string-a-to-b "aaa") "bbb")))
63
64;;
65;; user-pointer
66;;
67
68(ert-deftest mod-test-userptr-fun-test ()
69 (let* ((n 42)
70 (v (mod-test-userptr-make n))
71 (r (mod-test-userptr-get v)))
72
73 (should (eq (type-of v) 'user-ptr))
74 (should (integerp r))
75 (should (= r n))))
76
77;; TODO: try to test finalizer
78
79;;
80;; vectors
81;;
82
83(ert-deftest mod-test-vector-test ()
84 (dolist (s '(2 10 100 1000))
85 (dolist (e '(42 foo "foo"))
86 (let* ((v-ref (make-vector 2 e))
87 (eq-ref (eq (aref v-ref 0) (aref v-ref 1)))
88 (v-test (make-vector s nil)))
89
90 (should (eq (mod-test-vector-fill v-test e) t))
91 (should (eq (mod-test-vector-eq v-test e) eq-ref))))))