test-scm-c-read.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /* Return a new port of type PORT_TYPE. */
  29. static inline SCM
  30. make_port (scm_t_bits port_type)
  31. {
  32. SCM port;
  33. char *c_buffer;
  34. scm_t_port *c_port;
  35. c_buffer = scm_gc_calloc (PORT_BUFFER_SIZE, "custom-port-buffer");
  36. port = scm_new_port_table_entry (port_type);
  37. /* Associate C_BUFFER with PORT, for test purposes. */
  38. SCM_SETSTREAM (port, (scm_t_bits) c_buffer);
  39. /* Use C_BUFFER as PORT's internal buffer. */
  40. c_port = SCM_PTAB_ENTRY (port);
  41. c_port->read_pos = c_port->read_buf = (unsigned char *) c_buffer;
  42. c_port->read_end = (unsigned char *) c_buffer + PORT_BUFFER_SIZE;
  43. c_port->read_buf_size = PORT_BUFFER_SIZE;
  44. /* Mark PORT as open and readable. */
  45. SCM_SET_CELL_TYPE (port, port_type | SCM_OPN | SCM_RDNG);
  46. return port;
  47. }
  48. /* Read one byte from PORT. */
  49. static int
  50. fill_input (SCM port)
  51. {
  52. int result;
  53. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  54. /* Make sure that C_PORT's internal buffer wasn't changed behind our back.
  55. See http://lists.gnu.org/archive/html/guile-devel/2008-11/msg00042.html
  56. for an example where this assumption matters. */
  57. assert (c_port->read_buf == (unsigned char *) SCM_STREAM (port));
  58. assert (c_port->read_buf_size == PORT_BUFFER_SIZE);
  59. if (c_port->read_pos >= c_port->read_end)
  60. result = EOF;
  61. else
  62. result = (int) *c_port->read_pos++;
  63. return result;
  64. }
  65. /* Return true (non-zero) if BUF contains only zeros. */
  66. static inline int
  67. zeroed_buffer_p (const char *buf, size_t len)
  68. {
  69. size_t i;
  70. for (i = 0; i < len; i++)
  71. if (buf[i] != 0)
  72. return 0;
  73. return 1;
  74. }
  75. /* Run the test. */
  76. static void *
  77. do_start (void *arg)
  78. {
  79. SCM port;
  80. scm_t_bits port_type;
  81. char buffer[PORT_BUFFER_SIZE + (PORT_BUFFER_SIZE / 2)];
  82. size_t read, last_read;
  83. port_type = scm_make_port_type ("custom-input-port", fill_input, NULL);
  84. port = make_port (port_type);
  85. read = 0;
  86. do
  87. {
  88. last_read = scm_c_read (port, &buffer[read], 123);
  89. assert (last_read <= 123);
  90. assert (zeroed_buffer_p (&buffer[read], last_read));
  91. read += last_read;
  92. }
  93. while (last_read > 0 && read < sizeof (buffer));
  94. /* We shouldn't be able to read more than what's in PORT's buffer. */
  95. assert (read == PORT_BUFFER_SIZE);
  96. return NULL;
  97. }
  98. int
  99. main (int argc, char *argv[])
  100. {
  101. scm_with_guile (do_start, NULL);
  102. return 0;
  103. }