mirror of
https://github.com/bkthomps/Containers.git
synced 2025-11-16 12:34:47 +00:00
Add puzzle test case (#63)
This commit is contained in:
43
tst/deque.c
43
tst/deque.c
@@ -272,6 +272,47 @@ void test_single_full_block(void)
|
|||||||
deque_destroy(me);
|
deque_destroy(me);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct pair {
|
||||||
|
int cur_node;
|
||||||
|
int cur_cost;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int test_puzzle(int start_node, int dest_node)
|
||||||
|
{
|
||||||
|
deque q = deque_init(sizeof(struct pair));
|
||||||
|
struct pair cur;
|
||||||
|
cur.cur_node = start_node;
|
||||||
|
cur.cur_cost = 0;
|
||||||
|
assert(deque_is_empty(q));
|
||||||
|
deque_push_back(q, &cur);
|
||||||
|
assert(deque_size(q) == 1);
|
||||||
|
while (!deque_is_empty(q)) {
|
||||||
|
int node;
|
||||||
|
int cost;
|
||||||
|
deque_pop_front(&cur, q);
|
||||||
|
node = cur.cur_node;
|
||||||
|
cost = cur.cur_cost;
|
||||||
|
if (node > 2 * dest_node || node < 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (node == dest_node) {
|
||||||
|
deque_destroy(q);
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
cur.cur_cost = cost + 1;
|
||||||
|
cur.cur_node = node - 1;
|
||||||
|
deque_push_back(q, &cur);
|
||||||
|
assert(cur.cur_cost == cost + 1);
|
||||||
|
assert(cur.cur_node == node - 1);
|
||||||
|
cur.cur_node = 2 * node;
|
||||||
|
deque_push_back(q, &cur);
|
||||||
|
assert(cur.cur_cost == cost + 1);
|
||||||
|
assert(cur.cur_node == 2 * node);
|
||||||
|
}
|
||||||
|
deque_destroy(q);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
void test_deque(void)
|
void test_deque(void)
|
||||||
{
|
{
|
||||||
test_invalid_init();
|
test_invalid_init();
|
||||||
@@ -283,4 +324,6 @@ void test_deque(void)
|
|||||||
test_push_back_out_of_memory();
|
test_push_back_out_of_memory();
|
||||||
test_clear_out_of_memory();
|
test_clear_out_of_memory();
|
||||||
test_single_full_block();
|
test_single_full_block();
|
||||||
|
assert(test_puzzle(2, 5) == 4);
|
||||||
|
assert(test_puzzle(2, 10) == 5);
|
||||||
}
|
}
|
||||||
|
|||||||
44
tst/list.c
44
tst/list.c
@@ -281,6 +281,48 @@ void test_remove_all(void)
|
|||||||
assert(!list_destroy(me));
|
assert(!list_destroy(me));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct pair {
|
||||||
|
int cur_node;
|
||||||
|
int cur_cost;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int test_puzzle(int start_node, int dest_node)
|
||||||
|
{
|
||||||
|
list q = list_init(sizeof(struct pair));
|
||||||
|
struct pair cur;
|
||||||
|
cur.cur_node = start_node;
|
||||||
|
cur.cur_cost = 0;
|
||||||
|
assert(list_is_empty(q));
|
||||||
|
list_add_last(q, &cur);
|
||||||
|
assert(list_size(q) == 1);
|
||||||
|
while (!list_is_empty(q)) {
|
||||||
|
int node;
|
||||||
|
int cost;
|
||||||
|
list_get_first(&cur, q);
|
||||||
|
list_remove_first(q);
|
||||||
|
node = cur.cur_node;
|
||||||
|
cost = cur.cur_cost;
|
||||||
|
if (node > 2 * dest_node || node < 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (node == dest_node) {
|
||||||
|
list_destroy(q);
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
cur.cur_cost = cost + 1;
|
||||||
|
cur.cur_node = node - 1;
|
||||||
|
list_add_last(q, &cur);
|
||||||
|
assert(cur.cur_cost == cost + 1);
|
||||||
|
assert(cur.cur_node == node - 1);
|
||||||
|
cur.cur_node = 2 * node;
|
||||||
|
list_add_last(q, &cur);
|
||||||
|
assert(cur.cur_cost == cost + 1);
|
||||||
|
assert(cur.cur_node == 2 * node);
|
||||||
|
}
|
||||||
|
list_destroy(q);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
void test_list(void)
|
void test_list(void)
|
||||||
{
|
{
|
||||||
test_invalid_init();
|
test_invalid_init();
|
||||||
@@ -290,4 +332,6 @@ void test_list(void)
|
|||||||
test_add_at_out_of_memory();
|
test_add_at_out_of_memory();
|
||||||
test_add_last_out_of_memory();
|
test_add_last_out_of_memory();
|
||||||
test_remove_all();
|
test_remove_all();
|
||||||
|
assert(test_puzzle(2, 5) == 4);
|
||||||
|
assert(test_puzzle(2, 10) == 5);
|
||||||
}
|
}
|
||||||
|
|||||||
43
tst/queue.c
43
tst/queue.c
@@ -114,6 +114,47 @@ static void test_init_out_of_memory(void)
|
|||||||
assert(!queue_init(sizeof(int)));
|
assert(!queue_init(sizeof(int)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct pair {
|
||||||
|
int cur_node;
|
||||||
|
int cur_cost;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int test_puzzle(int start_node, int dest_node)
|
||||||
|
{
|
||||||
|
queue q = queue_init(sizeof(struct pair));
|
||||||
|
struct pair cur;
|
||||||
|
cur.cur_node = start_node;
|
||||||
|
cur.cur_cost = 0;
|
||||||
|
assert(queue_is_empty(q));
|
||||||
|
queue_push(q, &cur);
|
||||||
|
assert(queue_size(q) == 1);
|
||||||
|
while (!queue_is_empty(q)) {
|
||||||
|
int node;
|
||||||
|
int cost;
|
||||||
|
queue_pop(&cur, q);
|
||||||
|
node = cur.cur_node;
|
||||||
|
cost = cur.cur_cost;
|
||||||
|
if (node > 2 * dest_node || node < 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (node == dest_node) {
|
||||||
|
queue_destroy(q);
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
cur.cur_cost = cost + 1;
|
||||||
|
cur.cur_node = node - 1;
|
||||||
|
queue_push(q, &cur);
|
||||||
|
assert(cur.cur_cost == cost + 1);
|
||||||
|
assert(cur.cur_node == node - 1);
|
||||||
|
cur.cur_node = 2 * node;
|
||||||
|
queue_push(q, &cur);
|
||||||
|
assert(cur.cur_cost == cost + 1);
|
||||||
|
assert(cur.cur_node == 2 * node);
|
||||||
|
}
|
||||||
|
queue_destroy(q);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
void test_queue(void)
|
void test_queue(void)
|
||||||
{
|
{
|
||||||
test_invalid_init();
|
test_invalid_init();
|
||||||
@@ -121,4 +162,6 @@ void test_queue(void)
|
|||||||
test_large_alloc();
|
test_large_alloc();
|
||||||
test_automated_trim();
|
test_automated_trim();
|
||||||
test_init_out_of_memory();
|
test_init_out_of_memory();
|
||||||
|
assert(test_puzzle(2, 5) == 4);
|
||||||
|
assert(test_puzzle(2, 10) == 5);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user