test: misc fix and refactor

This commit is contained in:
CismonX 2025-06-16 18:32:36 +08:00
parent ba9a23c295
commit 99ac2b79f8
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
4 changed files with 27 additions and 29 deletions

View file

@ -42,10 +42,11 @@
struct check_item { struct check_item {
int id; int id;
unsigned flags; unsigned flags;
int ref;
}; };
// Forward declaration start // Forward declaration start
static int dent_check (int, struct check_item *, int const *, int, int); static int dent_check (int, struct check_item *, int, int);
static int dent_delete (int, struct check_item *); static int dent_delete (int, struct check_item *);
static int dent_new (int, struct check_item *); static int dent_new (int, struct check_item *);
static int dent_permute (int, struct check_item *, struct check_item *); static int dent_permute (int, struct check_item *, struct check_item *);
@ -55,8 +56,7 @@ static int do_check_fs_dents (int, int);
static int static int
dent_check ( dent_check (
int dirfd, int dirfd,
struct check_item *item, struct check_item *items,
int const *map,
int n, int n,
int ignore_dirty int ignore_dirty
) { ) {
@ -81,7 +81,7 @@ dent_check (
if (id < 0 || id >= n) { if (id < 0 || id >= n) {
return -1; return -1;
} }
struct check_item *found = item + map[id]; struct check_item *found = items + items[id].ref;
if (found->flags & (ITEM_DELETED | ITEM_MARKED)) { if (found->flags & (ITEM_DELETED | ITEM_MARKED)) {
return -1; return -1;
} }
@ -95,14 +95,14 @@ dent_check (
found->flags |= ITEM_MARKED; found->flags |= ITEM_MARKED;
} }
for (last_found = item + n; item < last_found; ++item) { for (last_found = items + n; items < last_found; ++items) {
if (ignore_dirty && item->flags & ITEM_DIRTY) { if (ignore_dirty && items->flags & ITEM_DIRTY) {
continue; continue;
} }
if (!(item->flags & (ITEM_DELETED | ITEM_MARKED))) { if (!(items->flags & (ITEM_DELETED | ITEM_MARKED))) {
return -1; return -1;
} }
item->flags &= ~ITEM_MARKED; items->flags &= ~ITEM_MARKED;
} }
return 0; return 0;
} }
@ -176,10 +176,10 @@ dent_permute (
} }
struct check_item item_tmp = *item1; struct check_item item_tmp = *item1;
*item1 = *item2; item1->id = item2->id;
*item2 = item_tmp; item1->flags = item2->flags | ITEM_DIRTY;
item1->flags |= ITEM_DIRTY; item2->id = item_tmp.id;
item2->flags |= ITEM_DIRTY; item2->flags = item_tmp.flags | ITEM_DIRTY;
return 0; return 0;
} }
@ -192,15 +192,14 @@ do_check_fs_dents (
#define ASSERT_NE(val, expr) ASSERT_EXPR_INT(expr, r_, (val) != r_, goto end;) #define ASSERT_NE(val, expr) ASSERT_EXPR_INT(expr, r_, (val) != r_, goto end;)
struct check_item *items = calloc(n, sizeof(struct check_item)); struct check_item *items = calloc(n, sizeof(struct check_item));
int *map = malloc(sizeof(int) * n); if (items == NULL) {
if (items == NULL || map == NULL) {
return -1; return -1;
} }
int status = -1; int status = -1;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
struct check_item *item = items + i; struct check_item *item = items + i;
map[i] = item->id = i; item->ref = item->id = i;
ASSERT_EQ(0, dent_new(dirfd, item)); ASSERT_EQ(0, dent_new(dirfd, item));
} }
@ -219,20 +218,19 @@ do_check_fs_dents (
continue; continue;
} }
ASSERT_EQ(0, dent_permute(dirfd, i1, i2)); ASSERT_EQ(0, dent_permute(dirfd, i1, i2));
map[i1->id] = i1 - items; items[i1->id].ref = i1 - items;
map[i2->id] = i2 - items; items[i2->id].ref = i2 - items;
#endif #endif
} }
} }
ASSERT_EQ(0, dent_check(dirfd, items, map, n, 1)); ASSERT_EQ(0, dent_check(dirfd, items, n, 1));
ASSERT_EQ(0, lseek(dirfd, 0, SEEK_SET)); ASSERT_EQ(0, lseek(dirfd, 0, SEEK_SET));
ASSERT_EQ(0, dent_check(dirfd, items, map, n, 0)); ASSERT_EQ(0, dent_check(dirfd, items, n, 0));
status = 0; status = 0;
end: end:
free(items); free(items);
free(map);
return status; return status;
} }

View file

@ -122,7 +122,7 @@ do_check_fs_regrw (
#ifndef O_DIRECT #ifndef O_DIRECT
# define O_DIRECT 0 # define O_DIRECT 0
#endif #endif
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC | O_DIRECT); int fd = open(path, O_RDWR | O_CREAT | O_TRUNC | O_DIRECT, 0600);
ASSERT_NE(-1, fd); ASSERT_NE(-1, fd);
struct stat stat_buf; struct stat stat_buf;

View file

@ -68,11 +68,12 @@ do_check_fs_times (
#define ASSERT_NE(val, expr) ASSERT_EXPR_INT(expr, r_, (val) != r_, goto end;) #define ASSERT_NE(val, expr) ASSERT_EXPR_INT(expr, r_, (val) != r_, goto end;)
int status = -1; int status = -1;
int fd = -1;
struct timespec now; struct timespec now;
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now)); ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now));
int fd = openat(dirfd, FILE1_NAME, O_WRONLY | O_CREAT | O_EXCL); fd = openat(dirfd, FILE1_NAME, O_WRONLY | O_CREAT | O_EXCL, 0600);
ASSERT_NE(-1, fd); ASSERT_NE(-1, fd);
struct stat stat_buf; struct stat stat_buf;

View file

@ -58,18 +58,17 @@ do_check_sandbox (
# error "not implemented" # error "not implemented"
#endif #endif
#define ASSERT_BAD_SYS(expr, cleanup_action) \ #define ASSERT_BAD_SYS(expr, cleanup_action) \
ASSERT_EXPR_INT(expr, r_, (err_ = errno, r_ < 0), { \ ASSERT_EXPR_INT(expr, r_, r_ < 0, { \
cleanup_action \ cleanup_action \
goto end; \ goto end; \
}); \ }); \
ASSERT_EXPR_INT(err_, r_, r_ == ERR1 || r_ == ERR2, goto end;) ASSERT_EXPR_INT(errno, r_, r_ == ERR1 || r_ == ERR2, goto end;)
#define ASSERT_BAD_FD(expr) ASSERT_BAD_SYS(expr, close(r_);) #define ASSERT_BAD_FD(expr) ASSERT_BAD_SYS(expr, close(r_);)
#define ASSERT_EQ(val, expr) ASSERT_EXPR_INT(expr, r_, (val) == r_, goto end;) #define ASSERT_EQ(val, expr) ASSERT_EXPR_INT(expr, r_, (val) == r_, goto end;)
#define ASSERT_NE(val, expr) ASSERT_EXPR_INT(expr, r_, (val) != r_, goto end;) #define ASSERT_NE(val, expr) ASSERT_EXPR_INT(expr, r_, (val) != r_, goto end;)
int err_;
int status = -1; int status = -1;
int fd = socket(AF_INET, SOCK_STREAM, 0); int fd = socket(AF_INET, SOCK_STREAM, 0);