test-scm-c-read.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (C) 2008, 2014 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 License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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
  16. * 02110-1301 USA
  17. */
  18. /* Exercise `scm_c_read ()' and the port type API. Verify assumptions that
  19. can be made by port type implementations. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #undef NDEBUG
  24. #include <libguile.h>
  25. #include <assert.h>
  26. /* Size of our port's internal buffer. */
  27. #define PORT_BUFFER_SIZE 1024
  28. struct custom_port
  29. {
  30. size_t pos;
  31. size_t len;
  32. char *buf;
  33. };
  34. /* Return a new port of type PORT_TYPE. */
  35. static inline SCM
  36. make_port (scm_t_port_type *port_type)
  37. {
  38. struct custom_port *stream = scm_gc_typed_calloc (struct custom_port);
  39. stream->pos = 0;
  40. stream->len = PORT_BUFFER_SIZE;
  41. stream->buf = scm_gc_calloc (stream->len, "custom-port-buffer");
  42. return scm_c_make_port (port_type, SCM_RDNG, (scm_t_bits) stream);
  43. }
  44. static size_t
  45. custom_port_read (SCM port, SCM dst, size_t start, size_t count)
  46. {
  47. size_t to_copy = count;
  48. struct custom_port *stream = (void *) SCM_STREAM (port);
  49. if (stream->pos + to_copy > stream->len)
  50. to_copy = stream->len - stream->pos;
  51. memcpy (SCM_BYTEVECTOR_CONTENTS (dst) + start,
  52. stream->buf + stream->pos, to_copy);
  53. stream->pos += to_copy;
  54. return to_copy;
  55. }
  56. /* Return true (non-zero) if BUF contains only zeros. */
  57. static inline int
  58. zeroed_buffer_p (const char *buf, size_t len)
  59. {
  60. size_t i;
  61. for (i = 0; i < len; i++)
  62. if (buf[i] != 0)
  63. return 0;
  64. return 1;
  65. }
  66. /* Run the test. */
  67. static void *
  68. do_start (void *arg)
  69. {
  70. SCM port;
  71. scm_t_port_type *port_type;
  72. char buffer[PORT_BUFFER_SIZE + (PORT_BUFFER_SIZE / 2)];
  73. size_t read, last_read;
  74. port_type = scm_make_port_type ("custom-input-port", custom_port_read, NULL);
  75. port = make_port (port_type);
  76. read = 0;
  77. do
  78. {
  79. last_read = scm_c_read (port, &buffer[read], 123);
  80. assert (last_read <= 123);
  81. assert (zeroed_buffer_p (&buffer[read], last_read));
  82. read += last_read;
  83. }
  84. while (last_read > 0 && read < sizeof (buffer));
  85. /* We shouldn't be able to read more than what's in PORT's buffer. */
  86. assert (read == PORT_BUFFER_SIZE);
  87. return NULL;
  88. }
  89. int
  90. main (int argc, char *argv[])
  91. {
  92. scm_with_guile (do_start, NULL);
  93. return 0;
  94. }