Fixing and adding more tests

This commit is contained in:
Leonardo committed 2019-07-04 15:34:25 -03:00
1 parent 8c457eeb12
commit 97d50bc3d9
13 files changed
+2264 -19

No files matched your search

+2 -1
View File
@@ -64,8 +64,9 @@ CMC_CREATE_UNIT(deque_test, true, {
d_push_back(d, i);
cmc_assert_equals(size_t, 50, d_count(d));
cmc_assert_lesser_equals(size_t, d_capacity(d), d_count(d));
CMC_TEST_PASS_ELSE_FAIL(d_count(d) == 50);
CMC_TEST_PASS_ELSE_FAIL(d_count(d) == 50 && d_count(d) <= d_capacity(d));
d_free(d);
});
+73
View File
@@ -0,0 +1,73 @@
#include "utl/assert.h"
#include "utl/test.h"
#include "../src/queue.c"
CMC_CREATE_UNIT(queue_test, true, {
CMC_CREATE_TEST(new, {
queue *q = q_new(1000000);
cmc_assert_not_equals(ptr, NULL, q);
bool passed = q->capacity == 1000000 && q->count == 0 && q->buffer;
cmc_assert_equals(size_t, 1000000, q->capacity);
cmc_assert_equals(size_t, 0, q_count(q));
cmc_assert_not_equals(ptr, NULL, q->buffer);
CMC_TEST_PASS_ELSE_FAIL(passed);
q_free(q);
});
CMC_CREATE_TEST(new[edge_case:capacity = 0], {
queue *q = q_new(0);
cmc_assert_equals(ptr, NULL, q);
CMC_TEST_PASS_ELSE_FAIL(q == NULL);
});
CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], {
queue *q = q_new(UINT64_MAX);
cmc_assert_equals(ptr, NULL, q);
CMC_TEST_PASS_ELSE_FAIL(q == NULL);
});
CMC_CREATE_TEST(clear[count capacity], {
queue *q = q_new(100);
cmc_assert_not_equals(ptr, NULL, q);
for (size_t i = 0; i < 50; i++)
q_enqueue(q, i);
cmc_assert_equals(size_t, 50, q_count(q));
q_clear(q);
cmc_assert_equals(size_t, 0, q_count(q));
cmc_assert_equals(size_t, 100, q_capacity(q));
CMC_TEST_PASS_ELSE_FAIL(q_count(q) == 0 && q_capacity(q) == 100);
q_free(q);
});
CMC_CREATE_TEST(buffer_growth[edge_case:capacity = 1], {
queue *q = q_new(1);
cmc_assert_not_equals(ptr, NULL, q);
for (size_t i = 0; i < 50; i++)
q_enqueue(q, i);
cmc_assert_equals(size_t, 50, q_count(q));
CMC_TEST_PASS_ELSE_FAIL(q_count(q) == 50);
q_free(q);
});
});
+335
View File
@@ -0,0 +1,335 @@
#include "utl/assert.h"
#include "utl/test.h"
#include "../src/stack.c"
CMC_CREATE_UNIT(stack_test, true, {
CMC_CREATE_TEST(new, {
stack *s = s_new(1000000);
cmc_assert_not_equals(ptr, NULL, s);
bool passed = s->capacity == 1000000 && s->count == 0 && s->buffer;
cmc_assert_equals(size_t, 1000000, s->capacity);
cmc_assert_equals(size_t, 0, s_count(s));
cmc_assert_not_equals(ptr, NULL, s->buffer);
CMC_TEST_PASS_ELSE_FAIL(passed);
s_free(s);
});
CMC_CREATE_TEST(new[edge_case:capacity = 0], {
stack *s = s_new(0);
cmc_assert_equals(ptr, NULL, s);
CMC_TEST_PASS_ELSE_FAIL(s == NULL);
});
CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], {
stack *s = s_new(UINT64_MAX);
cmc_assert_equals(ptr, NULL, s);
CMC_TEST_PASS_ELSE_FAIL(s == NULL);
});
CMC_CREATE_TEST(clear[count capacity], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 50; i++)
cmc_assert(s_push(s, i));
cmc_assert_equals(size_t, 50, s_count(s));
s_clear(s);
cmc_assert_equals(size_t, 0, s_count(s));
cmc_assert_equals(size_t, 100, s_capacity(s));
CMC_TEST_PASS_ELSE_FAIL(s_count(s) == 0 && s_capacity(s) == 100);
s_free(s);
});
CMC_CREATE_TEST(buffer_growth[edge_case:capacity = 1], {
stack *s = s_new(1);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 50; i++)
cmc_assert(s_push(s, i));
cmc_assert_equals(size_t, 50, s_count(s));
cmc_assert_lesser_equals(size_t, s_capacity(s), s_count(s));
CMC_TEST_PASS_ELSE_FAIL(s_count(s) == 50 && s_count(s) <= s_capacity(s));
s_free(s);
});
CMC_CREATE_TEST(push[count], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
cmc_assert(s_push(s, i));
cmc_assert_equals(size_t, 250, s_count(s));
CMC_TEST_PASS_ELSE_FAIL(s_count(s) == 250);
s_free(s);
});
CMC_CREATE_TEST(push[capacity], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
cmc_assert(s_push(s, i));
cmc_assert_lesser_equals(size_t, s_capacity(s), s_count(s));
CMC_TEST_PASS_ELSE_FAIL(s_count(s) <= s_capacity(s));
s_free(s);
});
CMC_CREATE_TEST(push[item_preservation], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
cmc_assert(s_push(s, i));
bool passed = true;
for (size_t i = 0; i < s_count(s); i++)
{
passed = s->buffer[i] == i;
cmc_assert_equals(size_t, i, s->buffer[i]);
}
CMC_TEST_PASS_ELSE_FAIL(passed);
s_free(s);
});
CMC_CREATE_TEST(pop[count], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
cmc_assert(s_push(s, i));
cmc_assert(s_pop(s));
cmc_assert_equals(size_t, 249, s_count(s));
CMC_TEST_PASS_ELSE_FAIL(s_count(s) == 249);
s_free(s);
});
CMC_CREATE_TEST(pop[capacity], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
cmc_assert(s_push(s, i));
cmc_assert(s_pop(s));
cmc_assert_lesser_equals(size_t, s_capacity(s), s_count(s));
CMC_TEST_PASS_ELSE_FAIL(s_count(s) <= s_capacity(s));
s_free(s);
});
CMC_CREATE_TEST(pop[item_preservation], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
cmc_assert(s_push(s, i));
cmc_assert(s_pop(s));
cmc_assert(s_pop(s));
bool passed = true;
for (size_t i = 0; i < s_count(s); i++)
{
passed = s->buffer[i] == i;
cmc_assert_equals(size_t, i, s->buffer[i]);
}
CMC_TEST_PASS_ELSE_FAIL(passed);
s_free(s);
});
CMC_CREATE_TEST(push_if, {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
s_push_if(s, i, i % 2 == 0);
bool passed = true;
for (size_t i = 0; i < s_count(s); i++)
{
passed = passed && s->buffer[i] % 2 == 0;
cmc_assert(s->buffer[i] % 2 == 0);
}
CMC_TEST_PASS_ELSE_FAIL(passed);
s_free(s);
});
CMC_CREATE_TEST(pop_if, {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
{
s_push(s, i);
s_pop_if(s, s_top(s) % 2 == 0);
}
bool passed = true;
for (size_t i = 0; i < s_count(s); i++)
{
passed = passed && !(s->buffer[i] % 2 == 0);
cmc_assert(!(s->buffer[i] % 2 == 0));
}
CMC_TEST_PASS_ELSE_FAIL(passed);
s_free(s);
});
CMC_CREATE_TEST(top, {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
cmc_assert(s_push(s, i));
cmc_assert(s_pop(s));
cmc_assert(s_pop(s));
cmc_assert_equals(size_t, 247, s_top(s));
CMC_TEST_PASS_ELSE_FAIL(247 == s_top(s));
s_free(s);
});
CMC_CREATE_TEST(top[edge_case:count = 0], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_push(s, 1));
cmc_assert(s_pop(s));
cmc_assert_equals(size_t, s_impl_default_value(), s_top(s));
CMC_TEST_PASS_ELSE_FAIL(s_impl_default_value() == s_top(s));
s_free(s);
});
CMC_CREATE_TEST(top[edge_case:count = 1], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_push(s, 10));
cmc_assert_equals(size_t, 1, s_count(s));
cmc_assert_equals(size_t, 10, s_top(s));
CMC_TEST_PASS_ELSE_FAIL(10 == s_top(s));
s_free(s);
});
CMC_CREATE_TEST(empty, {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_empty(s));
CMC_TEST_PASS_ELSE_FAIL(s_empty(s));
s_free(s);
});
CMC_CREATE_TEST(empty[after_io], {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_push(s, 10));
cmc_assert(s_pop(s));
cmc_assert(s_empty(s));
CMC_TEST_PASS_ELSE_FAIL(s_empty(s));
s_free(s);
});
CMC_CREATE_TEST(full, {
stack *s = s_new(100);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < s_capacity(s); i++)
cmc_assert(s_push(s, i));
cmc_assert(s_full(s));
CMC_TEST_PASS_ELSE_FAIL(s_full(s));
s_free(s);
});
CMC_CREATE_TEST(full[edge_case:capacity = 1], {
stack *s = s_new(1);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_push(s, 10));
cmc_assert(s_full(s));
CMC_TEST_PASS_ELSE_FAIL(s_full(s));
s_free(s);
});
});
+38
View File
@@ -0,0 +1,38 @@
#include "utl/assert.h"
#include "utl/test.h"
#include "../src/treemap.c"
#include "utl.c"
CMC_CREATE_UNIT(treemap_test, true, {
CMC_CREATE_TEST(new, {
treemap *map = tm_new(cmp);
cmc_assert_not_equals(ptr, NULL, map);
CMC_TEST_PASS_ELSE_FAIL(map);
tm_free(map);
});
CMC_CREATE_TEST(clear[count], {
treemap *map = tm_new(cmp);
cmc_assert_not_equals(ptr, NULL, map);
for (size_t i = 0; i < 50; i++)
tm_insert(map, i, i);
cmc_assert_equals(size_t, 50, tm_count(map));
tm_clear(map);
cmc_assert_equals(size_t, 0, tm_count(map));
cmc_assert_equals(ptr, NULL, map->root);
CMC_TEST_PASS_ELSE_FAIL(tm_count(map) == 0);
tm_free(map);
});
});
+39
View File
@@ -0,0 +1,39 @@
#include "utl/assert.h"
#include "utl/test.h"
#include "../src/treeset.c"
#include "utl.c"
CMC_CREATE_UNIT(treeset_test, true, {
CMC_CREATE_TEST(new, {
treeset *set = ts_new(cmp);
cmc_assert_not_equals(ptr, NULL, set);
CMC_TEST_PASS_ELSE_FAIL(set);
ts_free(set);
});
CMC_CREATE_TEST(clear[count], {
treeset *set = ts_new(cmp);
cmc_assert_not_equals(ptr, NULL, set);
for (size_t i = 0; i < 50; i++)
ts_insert(set, i);
cmc_assert_equals(size_t, 50, ts_count(set));
ts_clear(set);
cmc_assert_equals(size_t, 0, ts_count(set));
cmc_assert_equals(ptr, NULL, set->root);
CMC_TEST_PASS_ELSE_FAIL(ts_count(set) == 0);
ts_free(set);
});
});