ATF_TC_BODY(path_leaf_name, tc) { struct test { const char *in; const char *leaf; } tests[] = { { ".", "." }, { "foo", "foo" }, { "foo/bar", "bar" }, { "/foo", "foo" }, { "/foo/bar", "bar" }, { NULL, NULL }, }; struct test *t; for (t = &tests[0]; t->in != NULL; t++) { atf_fs_path_t p; atf_dynstr_t ln; printf("Input : %s\n", t->in); printf("Expected output: %s\n", t->leaf); RE(atf_fs_path_init_fmt(&p, "%s", t->in)); RE(atf_fs_path_leaf_name(&p, &ln)); printf("Output : %s\n", atf_dynstr_cstring(&ln)); ATF_REQUIRE(atf_equal_dynstr_cstring(&ln, t->leaf)); atf_dynstr_fini(&ln); atf_fs_path_fini(&p); printf("\n"); } }
static void check_line(int fd, const char *exp) { atf_dynstr_t line; atf_dynstr_init(&line); ATF_CHECK(!read_line(fd, &line)); ATF_CHECK_MSG(atf_equal_dynstr_cstring(&line, exp), "read: '%s', expected: '%s'", atf_dynstr_cstring(&line), exp); atf_dynstr_fini(&line); }
atf_error_t atf_fs_path_leaf_name(const atf_fs_path_t *p, atf_dynstr_t *ln) { size_t begpos = atf_dynstr_rfind_ch(&p->m_data, '/'); atf_error_t err; if (begpos == atf_dynstr_npos) begpos = 0; else begpos++; err = atf_dynstr_init_substr(ln, &p->m_data, begpos, atf_dynstr_npos); #if defined(HAVE_CONST_BASENAME) INV(atf_equal_dynstr_cstring(ln, basename(atf_dynstr_cstring(&p->m_data)))); #endif /* defined(HAVE_CONST_BASENAME) */ return err; }
atf_error_t atf_fs_path_branch_path(const atf_fs_path_t *p, atf_fs_path_t *bp) { const size_t endpos = atf_dynstr_rfind_ch(&p->m_data, '/'); atf_error_t err; if (endpos == atf_dynstr_npos) err = atf_fs_path_init_fmt(bp, "."); else if (endpos == 0) err = atf_fs_path_init_fmt(bp, "/"); else err = atf_dynstr_init_substr(&bp->m_data, &p->m_data, 0, endpos); #if defined(HAVE_CONST_DIRNAME) INV(atf_equal_dynstr_cstring(&bp->m_data, dirname(atf_dynstr_cstring(&p->m_data)))); #endif /* defined(HAVE_CONST_DIRNAME) */ return err; }
ATF_TC_BODY(equal_cstring, tc) { atf_dynstr_t str; RE(atf_dynstr_init(&str)); ATF_REQUIRE( atf_equal_dynstr_cstring(&str, "")); ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Test")); atf_dynstr_fini(&str); RE(atf_dynstr_init_fmt(&str, "Test")); ATF_REQUIRE( atf_equal_dynstr_cstring(&str, "Test")); ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "")); ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Tes")); ATF_REQUIRE(!atf_equal_dynstr_cstring(&str, "Test ")); atf_dynstr_fini(&str); }
bool atf_fs_path_is_root(const atf_fs_path_t *p) { return atf_equal_dynstr_cstring(&p->m_data, "/"); }
static atf_error_t handle_srcdir(struct params *p) { atf_error_t err; atf_dynstr_t leafname; atf_fs_path_t exe, srcdir; bool b; err = atf_fs_path_copy(&srcdir, &p->m_srcdir); if (atf_is_error(err)) goto out; if (!atf_fs_path_is_absolute(&srcdir)) { atf_fs_path_t srcdirabs; err = atf_fs_path_to_absolute(&srcdir, &srcdirabs); if (atf_is_error(err)) goto out_srcdir; atf_fs_path_fini(&srcdir); srcdir = srcdirabs; } err = atf_fs_path_leaf_name(&srcdir, &leafname); if (atf_is_error(err)) goto out_srcdir; else { const bool libs = atf_equal_dynstr_cstring(&leafname, ".libs"); atf_dynstr_fini(&leafname); if (libs) { err = srcdir_strip_libtool(&srcdir); if (atf_is_error(err)) goto out; } } err = atf_fs_path_copy(&exe, &srcdir); if (atf_is_error(err)) goto out_srcdir; err = atf_fs_path_append_fmt(&exe, "%s", progname); if (atf_is_error(err)) goto out_exe; err = atf_fs_exists(&exe, &b); if (!atf_is_error(err)) { if (b) { err = atf_map_insert(&p->m_config, "srcdir", strdup(atf_fs_path_cstring(&srcdir)), true); } else { err = user_error("Cannot find the test program in the source " "directory `%s'", atf_fs_path_cstring(&srcdir)); } } out_exe: atf_fs_path_fini(&exe); out_srcdir: atf_fs_path_fini(&srcdir); out: return err; }
ATF_TC_BODY(read_line, tc) { const char *l1 = "First line with % formatting % characters %"; const char *l2 = "Second line; much longer than the first one"; const char *l3 = "Last line, without terminator"; { FILE *f; f = fopen("test", "w"); ATF_REQUIRE(f != NULL); fclose(f); } { int fd; atf_dynstr_t dest; bool eof; fd = open("test", O_RDONLY); ATF_REQUIRE(fd != -1); RE(atf_dynstr_init(&dest)); eof = read_line(fd, &dest); ATF_REQUIRE(eof); atf_dynstr_fini(&dest); } { FILE *f; f = fopen("test", "w"); ATF_REQUIRE(f != NULL); fprintf(f, "%s\n", l1); fprintf(f, "%s\n", l2); fprintf(f, "%s", l3); fclose(f); } { int fd; atf_dynstr_t dest; bool eof; fd = open("test", O_RDONLY); ATF_REQUIRE(fd != -1); RE(atf_dynstr_init(&dest)); eof = read_line(fd, &dest); ATF_REQUIRE(!eof); printf("1st line: >%s<\n", atf_dynstr_cstring(&dest)); ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l1)); atf_dynstr_fini(&dest); RE(atf_dynstr_init(&dest)); eof = read_line(fd, &dest); ATF_REQUIRE(!eof); printf("2nd line: >%s<\n", atf_dynstr_cstring(&dest)); ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l2)); atf_dynstr_fini(&dest); RE(atf_dynstr_init(&dest)); eof = read_line(fd, &dest); ATF_REQUIRE(eof); printf("3rd line: >%s<\n", atf_dynstr_cstring(&dest)); ATF_REQUIRE(atf_equal_dynstr_cstring(&dest, l3)); atf_dynstr_fini(&dest); close(fd); } }