Debian Codegraph @

src/tests/normal-tests/embedding.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;; Author: Juan Jose Garcia-Ripoll
5 ;;;; Created: Fri Apr 14 11:13:17 CEST 2006
6 ;;;; Contains: Embedding regression tests
7
8 (in-package :cl-test)
9
10 (suite 'emb)
11
12 ;;; Date: 21/06/2006 (goffioul)
13 ;;; Fixed: 23/06/2006 (juanjo)
14 ;;; Description:
15 ;;;
16 ;;; Multiple invocations of cl_shutdown() can hang ECL. Also,
17 ;;; cl_shutdown() is still invoked at exit (registered with
18 ;;; atexit()) even if cl_shutdown was previously invoked.
19 ;;;
20 ;;; Fixed: 03/2006 (juanjo)
21 ;;;
22 (test emb.0001.shutdown
23 (is-equal
24 (let* ((skeleton "
25 #include <ecl/ecl.h>
26 #include <stdlib.h>
27 int main (int argc, char **argv) {
28 cl_object x;
29 cl_boot(argc, argv);
30 si_safe_eval(3, x = c_string_to_object(~S), ECL_NIL, ECL_NIL);
31 cl_shutdown();
32 exit(0);
33 }")
34 (form '(push (lambda () (print :shutdown)) si::*exit-hooks*))
35 (c-code (format nil skeleton (format nil "~S" form)))
36 (data (test-C-program c-code :capture-output t)))
37 data)
38 '(:shutdown)))
39
40 ;;; Date: 2016-05-25 (Vadim Penzin)
41 ;;; Date: 2016-05-27 (Vadim Penzin)
42 ;;; Description:
43 ;;;
44 ;;; ECL_HANDLER_CASE C macro misses condition handlers because the
45 ;;; macro looks up handler tags in env->values[1] instead of
46 ;;; env->values[0] and copies the condition object from
47 ;;; env->values[0] instead of env->values[1].
48 ;;;
49 ;;; Case study: http://penzin.net/ecl-handler-case.html
50 ;;; Bug: https://gitlab.com/embeddable-common-lisp/ecl/issues/248
51 ;;; Notes:
52 ;;;
53 ;;; ECL_RESTART_CASE is very similar, but testing would require
54 ;;; user interaction (ie picking the restart), hence we only test
55 ;;; the ECL_HANDLER_CASE.
56 ;;;
57 (test emb.0002.handlers
58 (is-true
59 (let* ((c-code "
60 #include <stdio.h>
61 #include <ecl/ecl.h>
62
63 int
64 main ( const int argc, const char * const argv [] )
65 {
66 cl_boot ( argc, (char **) argv );
67 int result = 1;
68
69 cl_env_ptr const environment = ecl_process_env ();
70 const cl_object conditions =
71 ecl_list1 ( ecl_make_symbol ( \"DIVISION-BY-ZERO\", \"CL\" ) );
72
73 ECL_HANDLER_CASE_BEGIN ( environment, conditions ) {
74 ecl_divide ( ecl_make_fixnum ( 1 ), ecl_make_fixnum ( 0 ) );
75 } ECL_HANDLER_CASE ( 1, condition ) {
76 result = 0;
77 } ECL_HANDLER_CASE_END;
78
79 return result;
80 }
81 "))
82 (test-C-program c-code))))
83
84 ;;; Date: 2020-01-11 (Marius Gerbershagen)
85 ;;; Description:
86 ;;;
87 ;;; Verify that the ECL_WITH_LISP_FPE macro works correctly
88 ;;;
89 (test emb.0003.with-lisp-fpe
90 (is-true
91 (let ((c-code "
92 #include <math.h>
93 #include <ecl/ecl.h>
94
95 int main(int argc, char **argv) {
96 double a, b;
97 int ret = 1;
98 cl_env_ptr env;
99 cl_object conditions;
100
101 ECL_WITH_LISP_FPE_BEGIN {
102 cl_boot(argc, argv);
103 } ECL_WITH_LISP_FPE_END;
104
105 env = ecl_process_env();
106 conditions = ecl_list1(ecl_make_symbol(\"ARITHMETIC-ERROR\", \"CL\"));
107 ECL_HANDLER_CASE_BEGIN(env, conditions) {
108 a = 1.0 / (double) (argc - 1); /* equivalent of 1.0/0.0 but without
109 compilers complaining about it */
110 } ECL_HANDLER_CASE(1, condition) {
111 ret = 2;
112 goto out;
113 } ECL_HANDLER_CASE_END;
114
115 ECL_WITH_LISP_FPE_BEGIN {
116 ECL_HANDLER_CASE_BEGIN(env, conditions) {
117 b = ecl_to_double(ecl_make_double_float(1.0));
118 } ECL_HANDLER_CASE(1, condition) {
119 ret = 3; /* Exception bits being set before ECL_WITH_LISP_FPE_BEGIN
120 * shouldn't lead to a floating point exception being
121 * signaled when creating a double float ...
122 */
123 goto out;
124 } ECL_HANDLER_CASE_END;
125 } ECL_WITH_LISP_FPE_END;
126
127 ECL_WITH_LISP_FPE_BEGIN {
128 ECL_HANDLER_CASE_BEGIN(env, conditions) {
129 b = ecl_to_double(cl_N(2, ecl_make_double_float(1.0), ecl_make_double_float((double) (argc - 1))));
130 } ECL_HANDLER_CASE(1, condition) {
131 b = 0.0; /* ... but dividing by a zero float should definitely do so
132 */
133 } ECL_HANDLER_CASE_END;
134 } ECL_WITH_LISP_FPE_END;
135
136 if (isinf(a) &&
137 #ifdef ECL_AVOID_FPE_H
138 isinf(b)
139 #else
140 b == 0.0
141 #endif
142 ) {
143 ret = 0;
144 }
145
146 out:
147 cl_shutdown();
148 return ret;
149 }
150 "))
151 (test-C-program c-code))))
152
153
154 ;;; Date: 2020-02-29 (Marius Gerbershagen)
155 ;;; Description:
156 ;;;
157 ;;; Verify that ecl_import_current_thread works correctly
158 ;;;
159 #+threads
160 (test emb.0004.import-current-thread
161 (is-true
162 (let* ((c-code "
163 #define NUM_THREADS 50
164 #define BUFSIZE 1024
165
166 #include <stdio.h>
167 #include <stdlib.h>
168 #include <ecl/ecl.h>
169 /* Force use of ordinary thread creation functions instead of gc defines like
170 * GC_CreateThread, GC_pthread_create. */
171 #undef CreateThread
172 #undef pthread_create
173 #undef pthread_join
174 #ifdef _WIN32
175 # include <windows.h>
176 #else
177 # include <unistd.h>
178 # include <pthread.h>
179 #endif
180
181 #ifdef ECL_WINDOWS_THREADS
182 static DWORD WINAPI
183 #else
184 static void *
185 #endif
186 thread_entry_point(void *data)
187 {
188 cl_object form = (cl_object)data;
189
190 if (!ecl_import_current_thread(ECL_NIL, ECL_NIL)) {
191 exit(3);
192 }
193
194 cl_eval(form);
195
196 ecl_release_current_thread();
197 #ifdef ECL_WINDOWS_THREADS
198 return 1;
199 #else
200 return NULL;
201 #endif
202 }
203
204
205 int main(int narg, char **argv)
206 {
207 #ifdef ECL_WINDOWS_THREADS
208 HANDLE child_thread[NUM_THREADS];
209 DWORD thread_id[NUM_THREADS];
210 #else
211 pthread_t child_thread[NUM_THREADS];
212 #endif
213 int i, code;
214 char buffer[BUFSIZE];
215 volatile cl_object forms[NUM_THREADS];
216
217 cl_boot(narg, argv);
218 snprintf(buffer, BUFSIZE, \"(defparameter *test-results* (make-array %d :initial-element nil))\", NUM_THREADS);
219 cl_eval(c_string_to_object(buffer));
220
221 for (i = 0; i < NUM_THREADS; i++) {
222 snprintf(buffer, BUFSIZE, \"(setf (elt *test-results* %d) %d)\", i, i);
223 forms[i] = c_string_to_object(buffer);
224 #ifdef ECL_WINDOWS_THREADS
225 child_thread[i] = (HANDLE)CreateThread(NULL, 0, thread_entry_point,
226 (void*)forms[i], 0, &thread_id[i]);
227 if (child_thread[i] == NULL) {
228 exit(1);
229 }
230 #else
231 code = pthread_create(&child_thread[i], NULL, thread_entry_point,
232 (void*)forms[i]);
233 if (code) {
234 exit(1);
235 }
236 #endif
237 }
238
239 for (i = 0; i < NUM_THREADS; i++) {
240 #ifdef ECL_WINDOWS_THREADS
241 WaitForSingleObject(child_thread[i], INFINITE);
242 CloseHandle(child_thread[i]);
243 #else
244 pthread_join(child_thread[i], NULL);
245 #endif
246 }
247
248 for (i = 0; i < NUM_THREADS; i++) {
249 snprintf(buffer, BUFSIZE, \"(elt *test-results* %d)\", i);
250 cl_object result = cl_eval(c_string_to_object(buffer));
251 if (!ecl_eql(result, ecl_make_fixnum(i))) {
252 exit(2);
253 }
254 }
255
256 cl_shutdown();
257 return 0;
258 }"))
259 (test-C-program c-code))))
260
261 ;;; Date: 2021-08-13 (Marius Gerbershagen)
262 ;;; Description:
263 ;;;
264 ;;; Verify that ecl_decode_from_cstring, ecl_encode_to_cstring and
265 ;;; wide string equivalents work correctly
266 ;;;
267 #+unicode
268 (test emb.0005.decode/encode-cstrings
269 (is-true
270 (let* ((c-code "
271 #include <ecl/ecl.h>
272
273 int main(int argc, char** argv) {
274 cl_boot(argc, argv);
275
276 cl_object utf_8 = ecl_make_keyword(\"UTF-8\");
277
278 unsigned char invalid[3] = {0xff, 0xfe, 0};
279 if (ecl_decode_from_cstring((char*)invalid, -1, utf_8) != NULL) {
280 return -1;
281 }
282
283 unsigned char x[9] = {0xf0, 0x9f, 0x91, 0x89, 0xf0, 0x9f, 0x91, 0x88, 0};
284 cl_object s = cl_make_string(1, ecl_make_fixnum(2));
285 ecl_char_set(s, 0, 0x1f449);
286 ecl_char_set(s, 1, 0x1f448);
287
288 if (!ecl_equal(s, ecl_decode_from_cstring((char*)x, -1, utf_8))
289 || !ecl_equal(s, ecl_decode_from_cstring((char*)x, 8, utf_8))) {
290 return -2;
291 }
292
293 unsigned char y[9];
294 if (ecl_encode_to_cstring((char*)y, 9, s, utf_8) != 9) {
295 return -3;
296 }
297 for (int i = 0; i < 9; i++) {
298 if (x[i] != y[i]) {
299 return -4;
300 }
301 }
302
303 if (ecl_encode_to_cstring((char*)y, 1, s, utf_8) != 9) {
304 return -5;
305 }
306
307 if (ecl_encode_to_cstring((char*)y, 9, s, ecl_make_keyword(\"US-ASCII\")) != -1) {
308 return -6;
309 }
310
311 #ifdef HAVE_WCHAR_H
312 if (sizeof(wchar_t) == 2) {
313 wchar_t u[5] = {0xd83d, 0xdc49, 0xd83d, 0xdc48, 0};
314 if (!ecl_equal(s, ecl_decode_from_unicode_wstring(u, -1))
315 || !ecl_equal(s, ecl_decode_from_unicode_wstring(u, 4))) {
316 return -7;
317 }
318
319 wchar_t v[5];
320 if (ecl_encode_to_unicode_wstring(v, 5, s) != 5) {
321 return -8;
322 }
323 for (int i = 0; i < 5; i++) {
324 if (u[i] != v[i]) {
325 return -9;
326 }
327 }
328
329 if (ecl_encode_to_unicode_wstring(v, 1, s) != 5) {
330 return -10;
331 }
332 } else if (sizeof(wchar_t) == 4) {
333 wchar_t u[3] = {0x1f449, 0x1f448, 0};
334 if (!ecl_equal(s, ecl_decode_from_unicode_wstring(u, -1))
335 || !ecl_equal(s, ecl_decode_from_unicode_wstring(u, 2))) {
336 return -7;
337 }
338
339 wchar_t v[3];
340 if (ecl_encode_to_unicode_wstring(v, 3, s) != 3) {
341 return -8;
342 }
343 for (int i = 0; i < 3; i++) {
344 if (u[i] != v[i]) {
345 return -9;
346 }
347 }
348
349 if (ecl_encode_to_unicode_wstring(v, 1, s) != 3) {
350 return -10;
351 }
352 }
353 #endif
354
355 cl_shutdown();
356 return 0;
357 }
358 "))
359 (test-C-program c-code))))