test-foreign-object-c.c 2.6 KB

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