Commit | Line | Data |
---|---|---|
a7ee7f7c AW |
1 | /* test-foreign-object-c.c - exercise C foreign object interface */ |
2 | ||
3 | /* Copyright (C) 2014 Free Software Foundation, Inc. | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public License | |
7 | * as published by the Free Software Foundation; either version 3 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #ifdef HAVE_CONFIG_H | |
22 | # include <config.h> | |
23 | #endif | |
24 | ||
25 | #include <libguile.h> | |
26 | ||
27 | #include <stdlib.h> | |
28 | #include <stdio.h> | |
29 | #include <string.h> | |
30 | ||
31 | enum | |
32 | { | |
33 | CSTR_SLOT_ADDR, | |
34 | CSTR_SLOT_LEN, | |
35 | CSTR_SLOT_COUNT | |
36 | }; | |
37 | ||
38 | static void | |
39 | finalizer (SCM obj) | |
40 | { | |
41 | scm_t_bits addr = scm_foreign_object_ref (obj, CSTR_SLOT_ADDR); | |
42 | free ((void *) addr); | |
43 | } | |
44 | ||
45 | static SCM | |
46 | make_cstr_from_static (SCM type, const char *str) | |
47 | { | |
48 | char *ours = strdup (str); | |
49 | ||
50 | if (!ours) | |
51 | abort (); | |
52 | ||
53 | return scm_make_foreign_object_2 (type, (scm_t_bits) ours, strlen (ours)); | |
54 | } | |
55 | ||
56 | static int | |
57 | cstr_equals_static_p (SCM cstr, const char *str) | |
58 | { | |
59 | const char *addr; | |
60 | size_t len; | |
61 | ||
62 | addr = (const char *) scm_foreign_object_ref (cstr, CSTR_SLOT_ADDR); | |
63 | len = scm_foreign_object_ref (cstr, CSTR_SLOT_LEN); | |
64 | ||
65 | if (strlen (str) != len) | |
66 | return 0; | |
67 | ||
68 | return strncmp (addr, str, len) == 0; | |
69 | } | |
70 | ||
71 | static void | |
72 | test_scm_foreign_object (void) | |
73 | { | |
74 | SCM type_name, slot_names, type, cstr; | |
75 | ||
76 | type_name = scm_from_utf8_symbol ("<cstr>"); | |
77 | slot_names = scm_list_2 (scm_from_utf8_symbol ("addr"), | |
78 | scm_from_utf8_symbol ("len")); | |
79 | type = scm_make_foreign_object_type (type_name, slot_names, finalizer); | |
80 | ||
81 | cstr = make_cstr_from_static (type, "Hello, world!"); | |
82 | scm_assert_foreign_object_type (type, cstr); | |
83 | ||
84 | if (!cstr_equals_static_p (cstr, "Hello, world!")) | |
85 | { | |
86 | fprintf (stderr, "fail: test-foreign-object 1\n"); | |
87 | exit (EXIT_FAILURE); | |
88 | } | |
89 | ||
90 | { | |
91 | int i; | |
92 | for (i = 0; i < 5000; i++) | |
93 | cstr = make_cstr_from_static (type, "Hello, world!"); | |
94 | cstr = SCM_BOOL_F; | |
95 | } | |
96 | ||
97 | scm_gc (); | |
98 | scm_gc (); | |
99 | scm_gc (); | |
100 | ||
101 | /* Allow time for the finalizer thread to run. */ | |
102 | scm_usleep (scm_from_uint (50 * 1000)); | |
103 | } | |
104 | ||
105 | static void | |
106 | tests (void *data, int argc, char **argv) | |
107 | { | |
108 | test_scm_foreign_object (); | |
109 | } | |
110 | ||
111 | int | |
112 | main (int argc, char *argv[]) | |
113 | { | |
114 | scm_boot_guile (argc, argv, tests, NULL); | |
115 | return 0; | |
116 | } |