コード例 #1
0
ファイル: string.c プロジェクト: DreamDuo/libphenom
static void string_stream_tests(void)
{
  ph_string_t *str;
  ph_stream_t *stm;
  char buf[5];
  uint64_t r;

  str = ph_string_make_empty(mt_misc, 16);
  stm = ph_stm_string_open(str);
  ok(stm, "made a stream");

  ph_stm_printf(stm, "hello world");
  ok(ph_string_equal_cstr(str, "hello world"), "see printf");

  ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "rewound");
  ok(ph_stm_read(stm, buf, sizeof(buf), &r), "read data");
  is(r, sizeof(buf));
  is(memcmp(buf, "hello", 5), 0);

  ph_stm_printf(stm, " kitty and append!");
  ok(ph_string_equal_cstr(str, "hello kitty and append!"), "see printf");

  ok(ph_stm_seek(stm, 6, SEEK_SET, NULL), "rewound");
  ok(ph_stm_read(stm, buf, sizeof(buf), &r), "read data");
  is(r, sizeof(buf));
  is(memcmp(buf, "kitty", 5), 0);

  ok(ph_stm_seek(stm, -5, SEEK_END, NULL), "rewound");
  ok(ph_stm_read(stm, buf, sizeof(buf), &r), "read data");
  is(r, sizeof(buf));
  is(memcmp(buf, "pend!", 5), 0);

  ph_stm_close(stm);
  ph_string_delref(str);
}
コード例 #2
0
ファイル: string.c プロジェクト: 01BTC10/libphenom
ph_string_t *ph_string_make_copy(ph_memtype_t mt,
    const char *buf, uint32_t len, uint32_t size)
{
  ph_string_t *str = ph_string_make_empty(mt, MAX(len, size));

  if (!str) {
    return NULL;
  }

  ph_string_append_buf(str, buf, len);
  return str;
}
コード例 #3
0
ファイル: string.c プロジェクト: 01BTC10/libphenom
ph_string_t *ph_string_make_printf(ph_memtype_t mt, uint32_t size,
    const char *fmt, ...)
{
  ph_string_t *str = ph_string_make_empty(mt, size);
  va_list ap;

  if (!str) {
    return NULL;
  }

  va_start(ap, fmt);
  ph_string_vprintf(str, fmt, ap);
  va_end(ap);

  return str;
}
コード例 #4
0
ファイル: string.c プロジェクト: DreamDuo/libphenom
int main(int argc, char **argv)
{
  ph_string_t *str, *str2;

  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(102);

  mt_misc = ph_memtype_register(&mt_def);

  stack_tests();

  // Tests reallocation
  str = ph_string_make_empty(mt_misc, 16);
  is(ph_string_append_cstr(str, "1234567890"), PH_OK);
  is(10, ph_string_len(str));
  is(ph_string_append_cstr(str, "1234567890"), PH_OK);
  is(20, ph_string_len(str));
  is(memcmp(str->buf, "12345678901234567890", 20), 0);
  ph_string_delref(str);

  // Tests reallocation and string formatting
  str = ph_string_make_empty(mt_misc, 4);
  is(ph_string_printf(str, "Hello %s", "world"), 11);
  is(ph_string_len(str), 11);

  str2 = ph_string_make_empty(mt_misc, 10);
  is(ph_string_printf(str2, "copy `Ps%p", (void*)str), 16);
  diag(":%.*s:", str2->len, str2->buf);
  is(ph_string_len(str2), 16);
  is(memcmp(str2->buf, "copy Hello world", 16), 0);
  ph_string_delref(str2);

  str2 = ph_string_make_empty(mt_misc, 10);
  is(ph_string_printf(str2, "copy `Ps%d%p", 5, (void*)str), 10);
  is(memcmp(str2->buf, "copy Hello", 10), 0);
  ok(!ph_string_equal(str, str2), "not same");
  ph_string_delref(str2);

  str2 = ph_string_make_empty(mt_misc, 10);
  ph_string_append_buf(str2, str->buf, str->len);
  ok(ph_string_equal(str, str2), "same");
  is(ph_string_compare(str, str2), 0);

  ph_string_delref(str);
  ph_string_delref(str2);

  str = ph_string_make_cstr(mt_misc, "abc");
  str2 = ph_string_make_cstr(mt_misc, "bbc");
  ok(ph_string_compare(str, str2) < 0, "abc < bbc");
  ok(ph_string_compare(str2, str) > 0, "abc < bbc");

  ph_string_delref(str2);
  str2 = ph_string_make_cstr(mt_misc, "abca");
  ok(ph_string_compare(str, str2) < 0, "abc < abca");
  ok(ph_string_compare(str2, str) > 0, "abc < abca");

  ph_string_delref(str2);
  str2 = ph_string_make_cstr(mt_misc, "ab");
  ok(ph_string_compare(str, str2) > 0, "abc > ab");
  ok(ph_string_compare(str2, str) < 0, "abc > ab");

  ph_string_delref(str2);
  ph_string_delref(str);

  str = ph_string_make_printf(mt_misc, 16, "Hello %d", 42);
  ok(ph_string_equal_cstr(str, "Hello 42"), "same");
  ph_string_delref(str);

  utf16_tests();

  string_stream_tests();

  return exit_status();
}