test-gh.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (C) 1999,2000,2001,2003, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. /* some bits originally by Jim Blandy <jimb@red-bean.com> */
  18. #ifndef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <libguile.h>
  22. #include <libguile/gh.h>
  23. #include <assert.h>
  24. #include <string.h>
  25. #if SCM_ENABLE_DEPRECATED
  26. static int
  27. string_equal (SCM str, char *lit)
  28. {
  29. int len = strlen (lit);
  30. int result;
  31. result = ((scm_i_string_length (str) == len)
  32. && (!memcmp (scm_i_string_chars (str), lit, len)));
  33. scm_remember_upto_here_1 (str);
  34. return result;
  35. }
  36. static void
  37. test_gh_set_substr ()
  38. {
  39. SCM string;
  40. string = gh_str02scm ("Free, darnit!");
  41. assert (gh_string_p (string));
  42. gh_set_substr ("dammit", string, 6, 6);
  43. assert (string_equal (string, "Free, dammit!"));
  44. /* Make sure that we can use the string itself as a source.
  45. I guess this behavior isn't really visible, since the GH API
  46. doesn't provide any direct access to the string contents. But I
  47. think it should, eventually. You can't write efficient string
  48. code if you have to copy the string just to look at it. */
  49. /* Copy a substring to an overlapping region to its right. */
  50. gh_set_substr (scm_i_string_chars (string), string, 4, 6);
  51. assert (string_equal (string, "FreeFree, it!"));
  52. string = gh_str02scm ("Free, darnit!");
  53. assert (gh_string_p (string));
  54. /* Copy a substring to an overlapping region to its left. */
  55. gh_set_substr (scm_i_string_chars (string) + 6, string, 2, 6);
  56. assert (string_equal (string, "Frdarnitrnit!"));
  57. }
  58. static void
  59. tests (void *data, int argc, char **argv)
  60. {
  61. test_gh_set_substr ();
  62. }
  63. int
  64. main (int argc, char *argv[])
  65. {
  66. scm_boot_guile (argc, argv, tests, NULL);
  67. return 0;
  68. }
  69. #else
  70. int
  71. main (int argc, char *argv[])
  72. {
  73. return 0;
  74. }
  75. #endif /* !SCM_ENABLE_DEPRECATED */