Skip to content

Commit

Permalink
* NEW add unit test for new vector.
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghaEMQ committed Nov 8, 2023
1 parent ff4f77d commit 963c854
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct buf {
};

HashTable files;
nftp_vec *fcb_reg;
nftp_vec *fcb_reg = NULL;
HashTable senderfiles;

struct nctx {
Expand Down
134 changes: 78 additions & 56 deletions test/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,95 @@ int
test_vector()
{
nftp_log("test_vector");
nftp_vec *v1, *v2;
char *e0 = "e0", *e1, *e2 = "e2";
nftp_vec *v1;
char *e, *e0 = "e0", *e1 = "e1", *e2 = "e2";
int idx;
int cap = NFTP_SIZE / 4;

assert(0 == nftp_vec_alloc(&v1, NFTP_SIZE * 2));
assert(0 == nftp_vec_alloc(&v1, cap));
assert(NULL != v1);
assert(0 == nftp_vec_len(v1));
assert(NFTP_SIZE * 2 == nftp_vec_cap(v1));
assert(cap == nftp_vec_cap(v1));

assert(0 == nftp_vec_append(v1, (void *)e0));
assert(1 == nftp_vec_len(v1));
assert(0 == nftp_vec_get(v1, 0, (void **)&e1));
assert(e1 == e0);

assert(0 == nftp_vec_insert(v1, (void *)e2, 1));
assert(2 == nftp_vec_len(v1));
assert(0 == nftp_vec_delete(v1, (void **)&e1, 1));
assert(1 == nftp_vec_len(v1));
assert(e2 == e1);
assert(0 == nftp_vec_delete(v1, (void **)&e1, 0));
assert(0 == nftp_vec_len(v1));
assert(e0 == e1);

assert(0 == nftp_vec_push(v1, (void *)e0, NFTP_HEAD));
assert(0 == nftp_vec_get(v1, 0, (void **)&e));
assert(e == e0);

char *earr[3] = {e0, e1, e2};

// Push test
int k=0;
for (int i=0; i<cap - 1; ++i) {
k ++;
assert(0 == nftp_vec_push(v1, (void *) earr[k % 3], NFTP_TAIL));
assert(i+2 == nftp_vec_len(v1));
}

assert(NFTP_ERR_OVERFLOW == nftp_vec_push(v1, (void *) earr[0], NFTP_TAIL));
assert(NFTP_ERR_OVERFLOW == nftp_vec_push(v1, (void *) earr[0], NFTP_HEAD));

// Pop test
k=0;
for (int i=0; i<cap; ++i) {
assert(0 == nftp_vec_pop(v1, (void **)&e, NFTP_HEAD));
assert(earr[k ++ % 3] == e);
assert(cap-i-1 == nftp_vec_len(v1));
}

assert(NFTP_ERR_EMPTY == nftp_vec_pop(v1, (void **)&e, NFTP_HEAD));
assert(NFTP_ERR_EMPTY == nftp_vec_pop(v1, (void **)&e, NFTP_TAIL));

// Reverse push and pop
k=-1;
for (int i=0; i<cap; ++i) {
k ++;
assert(0 == nftp_vec_push(v1, (void *) earr[k % 3], NFTP_HEAD));
assert(i+1 == nftp_vec_len(v1));
}

assert(NFTP_ERR_OVERFLOW == nftp_vec_push(v1, (void *) earr[0], NFTP_TAIL));
assert(NFTP_ERR_OVERFLOW == nftp_vec_push(v1, (void *) earr[0], NFTP_HEAD));

k=0;
for (int i=0; i<cap; ++i) {
assert(0 == nftp_vec_pop(v1, (void **)&e, NFTP_TAIL));
assert(earr[k ++ % 3] == e);
assert(cap-i-1 == nftp_vec_len(v1));
}

assert(NFTP_ERR_EMPTY == nftp_vec_pop(v1, (void **)&e, NFTP_HEAD));
assert(NFTP_ERR_EMPTY == nftp_vec_pop(v1, (void **)&e, NFTP_TAIL));

// Insert and delete test
assert(0 == nftp_vec_append(v1, (void *)e2));
assert(1 == nftp_vec_len(v1));
assert(0 == nftp_vec_push(v1, (void *)e2, NFTP_HEAD));
assert(2 == nftp_vec_len(v1));
assert(0 == nftp_vec_pop(v1, (void **)&e1, NFTP_TAIL));
assert(e0 == e1);
assert(0 == nftp_vec_pop(v1, (void **)&e1, NFTP_TAIL));
assert(e2 == e1);
assert(0 == nftp_vec_len(v1));

assert(0 == nftp_vec_push(v1, (void *)e0, NFTP_TAIL));
assert(0 == nftp_vec_push(v1, (void *)e2, NFTP_TAIL));
assert(2 == nftp_vec_len(v1));

assert(0 == nftp_vec_getidx(v1, (void *)e2, &idx));
assert(1 == idx);

assert(0 == nftp_vec_pop(v1, (void **)&e1, NFTP_HEAD));
assert(e0 == e1);
assert(1 == nftp_vec_len(v1));

assert(0 == nftp_vec_alloc(&v2, 0));
assert(0 == nftp_vec_len(v2));
assert(0 == nftp_vec_append(v2, (void *)e2));
assert(0 == nftp_vec_append(v2, (void *)e2));
assert(0 == nftp_vec_append(v2, (void *)e2));
assert(3 == nftp_vec_len(v2));

assert(0 == nftp_vec_cat(v1, v2));
assert(4 == nftp_vec_len(v1));

assert(0 == nftp_vec_pop(v1, (void **)&e1, NFTP_HEAD));
assert(e2 == e1);
assert(0 == nftp_vec_pop(v1, (void **)&e1, NFTP_HEAD));
assert(e2 == e1);
assert(0 == nftp_vec_pop(v1, (void **)&e1, NFTP_HEAD));
assert(e2 == e1);
assert(0 == nftp_vec_pop(v1, (void **)&e1, NFTP_HEAD));
assert(e2 == e1);

assert(0 == nftp_vec_len(v1));
k=0;
for (int i=0; i<cap-1; ++i) {
k++;
assert(0 == nftp_vec_insert(v1, (void *)earr[k % 3], 1));
assert(i+2 == nftp_vec_len(v1));
}
assert(NFTP_ERR_OVERFLOW == nftp_vec_insert(v1, (void *)earr[0], 1));
// get and check
k=cap+1;
for (int i=0; i<cap; ++i) {
k--;
assert(0 == nftp_vec_get(v1, i, (void **)&e));
assert(e == earr[k % 3]);
}
// getidx and delete
k=cap+1;
for (int i=0; i<cap; ++i) {
k--;
assert(0 == nftp_vec_getidx(v1, earr[k % 3], &idx));
assert(idx == 0);
assert(0 == nftp_vec_delete(v1, (void **)&e, idx));
assert(e == earr[k % 3]);
}

assert(0 == nftp_vec_free(v1));
assert(0 == nftp_vec_free(v2));

return 0;
}
Expand Down

0 comments on commit 963c854

Please sign in to comment.