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); }
int main(int argc, char **argv) { ph_stream_t *stm; char namebuf[128]; int fd; char buf[BUFSIZ]; uint64_t amount; int len; ph_unused_parameter(argc); ph_unused_parameter(argv); ph_library_init(); plan_tests(18); strcpy(namebuf, "/tmp/phenomXXXXXX"); fd = ph_mkostemp(namebuf, 0); diag("opened %s -> %d", namebuf, fd); unlink(namebuf); stm = ph_stm_fd_open(fd, 0, PH_STM_BUFSIZE); ph_stm_write(stm, "lemon\n", 6, &amount); is(amount, 6); // Shouldn't see it yet is(0, pread(fd, buf, sizeof(buf), 0)); // Should see it now ph_stm_flush(stm); is(6, pread(fd, buf, sizeof(buf), 0)); ok(!memcmp("lemon\n", buf, 6), "right content"); ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked"); memset(buf, 0, sizeof(buf)); ok(ph_stm_read(stm, buf, 3, &amount), "read ok"); ok(amount == 3, "amount is %" PRIu64, amount); ok(!memcmp("lem", buf, 3), "got prefix"); ok(ph_stm_read(stm, buf, 3, &amount), "read ok"); ok(amount == 3, "amount is %" PRIu64, amount); ok(!memcmp("on\n", buf, 3), "got remainder"); ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked"); len = ph_stm_printf(stm, "testing %d %s!", 1, "two"); ok(14 == len, "printed len %d", len); ok(ph_stm_seek(stm, 0, SEEK_SET, NULL), "seeked"); memset(buf, 0, sizeof(buf)); ok(ph_stm_read(stm, buf, 14, &amount), "read ok"); ok(amount == 14, "len was %" PRIu64, amount); ok(!memcmp("testing 1 two!", buf, 14), "got formatted"); ok(ph_stm_close(stm), "closed"); return exit_status(); }