1 /* test-foreign-object-c.c - exercise C foreign object interface */
3 /* Copyright (C) 2014 Free Software Foundation, Inc.
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.
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.
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
41 free (scm_foreign_object_ref (obj
, CSTR_SLOT_ADDR
));
45 make_cstr_from_static (SCM type
, const char *str
)
47 char *ours
= strdup (str
);
52 return scm_make_foreign_object_2 (type
, ours
, (void *) strlen (ours
));
56 cstr_equals_static_p (SCM cstr
, const char *str
)
61 addr
= scm_foreign_object_ref (cstr
, CSTR_SLOT_ADDR
);
62 len
= scm_foreign_object_unsigned_ref (cstr
, CSTR_SLOT_LEN
);
64 if (strlen (str
) != len
)
67 return strncmp (addr
, str
, len
) == 0;
71 test_scm_foreign_object (void)
73 SCM type_name
, slot_names
, type
, cstr
;
75 type_name
= scm_from_utf8_symbol ("<cstr>");
76 slot_names
= scm_list_2 (scm_from_utf8_symbol ("addr"),
77 scm_from_utf8_symbol ("len"));
78 type
= scm_make_foreign_object_type (type_name
, slot_names
, finalizer
);
80 cstr
= make_cstr_from_static (type
, "Hello, world!");
81 scm_assert_foreign_object_type (type
, cstr
);
83 if (!cstr_equals_static_p (cstr
, "Hello, world!"))
85 fprintf (stderr
, "fail: test-foreign-object 1\n");
91 for (i
= 0; i
< 5000; i
++)
92 cstr
= make_cstr_from_static (type
, "Hello, world!");
100 /* Allow time for the finalizer thread to run. */
101 scm_usleep (scm_from_uint (50 * 1000));
105 tests (void *data
, int argc
, char **argv
)
107 test_scm_foreign_object ();
111 main (int argc
, char *argv
[])
113 scm_boot_guile (argc
, argv
, tests
, NULL
);