test-scm-c-read.c 3.3 KB

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