Exemple #1
0
static ssize_t
port_write (void *cookie, const char *buf, size_t siz)
{
  SCM port = PTR2SCM (cookie);

#ifdef GUILE_CHARS_ARE_UCS4
  if (siz > SSIZE_MAX)
    {
      scm_c_write (port, buf, SSIZE_MAX);
      return SSIZE_MAX;
    }
  else
    {
      scm_c_write (port, buf, siz);
      return siz;
    }
#else
  {
    size_t i;
    SCM chr_as_int;
    SCM chr;

    for (i = 0; i < siz; i++)
      {
	chr_as_int = scm_from_uchar ((unsigned char) buf[i]);
	chr = scm_integer_to_char (chr_as_int);
	scm_write_char (chr, port);
      }
  }

  return siz;
#endif
}
static void
test_write_cchr(ScmObj port, int type)
{
  const char *expected = "?";
  scm_char_t c = { .ascii = '?' };

  SCM_REFSTK_INIT_REG(&port);

  TEST_ASSERT_EQUAL_INT(0, scm_write_cchr(c, SCM_ENC_SRC, port));

  scm_close_port(port);

  if (type == FILEPORT)
    chk_file_contents(expected);
  else if (type == STRINGPORT)
    chk_string_port_contents(port, expected);
}

static void
test_write_cchr__specify_closed_port__return_ERROR(ScmObj port, int type)
{
  scm_char_t c = { .ascii = '?' };

  SCM_REFSTK_INIT_REG(&port);

  scm_close_port(port);
  TEST_ASSERT_EQUAL_INT(-1, scm_write_cchr(c, SCM_ENC_SRC, port));
}

static void
test_write_char(ScmObj port, int type)
{
  const char *expected = "?";
  ScmObj c = SCM_OBJ_INIT;

  SCM_REFSTK_INIT_REG(&port,
                      &c);

  c = scm_make_char(&(scm_char_t){ .ascii = '?' }, SCM_ENC_SRC);
  TEST_ASSERT_EQUAL_INT(0, scm_write_char(c, port));

  scm_close_port(port);

  if (type == FILEPORT)
    chk_file_contents(expected);
  else if (type == STRINGPORT)
    chk_string_port_contents(port, expected);
}
    chk_file_contents(expected);
  else if (type == STRINGPORT)
    chk_string_port_contents(port, expected);
}

static void
test_write_char__specify_closed_port__return_ERROR(ScmObj port, int type)
{
  ScmObj c = SCM_OBJ_INIT;

  SCM_REFSTK_INIT_REG(&port,
                      &c);

  c = scm_make_char(&(scm_char_t){ .ascii = '?' }, SCM_ENC_SRC);
  scm_close_port(port);
  TEST_ASSERT_EQUAL_INT(-1, scm_write_char(c, port));
}

static void
test_write_cstr(ScmObj port, int type)
{
  const char *expected = "hello";

  SCM_REFSTK_INIT_REG(&port);

  TEST_ASSERT_EQUAL_INT(0, scm_write_cstr(expected, SCM_ENC_SRC, port));

  scm_close_port(port);

  if (type == FILEPORT)
    chk_file_contents(expected);