Refactored tests

This commit is contained in:
Leonardo Vencovsky committed 2023-01-17 10:48:42 +01:00
1 parent 2c9759e5ec
commit bf02cede2b
11 files changed
+3942 -40

No files matched your search

+5 -38
View File
@@ -30,8 +30,6 @@
* [Available Collections](#available-collections)
* [Other Features](#other-features)
* [Design Decisions](#design-decisions)
* [What to use](#what-to-use)
* [Code Review](#code-review)
## Installation
@@ -158,13 +156,10 @@ Every macro is suffixed by `CMC` and each section is separated by an underscore
* __documentation__ - The markdowns used by [mdBook](https://github.com/rust-lang/mdBook) to generate the website
* __examples__ - Examples using the C Macro Collections Library
* __src__ - All headers part of the C Macro Collections Library
* __macro\_collections.h__ - Master header containing all collections and utilities
* __tests__ - Where all tests are hosted
## Available Collections
Check out the STATUS file at the root of the project for more information.
__The following table is an overview of all the currently available or upcoming data structures__:
| Collection <img width=250/> | Abstract Data Type <img width=250/> | Data Structure <img width=250/> | Details |
@@ -194,21 +189,21 @@ __The following table is an overview of all the currently available or upcoming
These are some features within the library that are implemented by all collections.
### Two-way iterators
## Two-way iterators
All collections come with a two-way iterator. You can go back and forwards in constant time and access elements in constant time.
### Custom Allocation
## Custom Allocation
All collections have a `cmc_alloc_node` which provides pointers to the four dynamic memory allocation functions in C: `malloc`, `calloc`, `realloc` and `free`. These pointers can be customized for each individual collection created or a default can be used, as specified in `cmc_alloc_node_default`.
### Callbacks
## Callbacks
Every function that operates on a collection can be separated in 5 different types. Create, Read, Update, Delete and (an extra one besides CRUD) Resize. You can define one callback function for each operation. Check out the documentation to see when each callback function is called.
### Functions Table
## Functions Table
Functions table is a `struct` of function pointers containing 'methods' for a custom data type. Some methods are optional and others are needed in order to a collection to operate. They are:
Functions table is a `struct` of function pointers containing 'methods' for a custom data type. Some methods are optional and others are needed in order for a collection to operate. They are:
#### CMP
@@ -293,31 +288,3 @@ You can use them as Stacks, Queues and Deques, but with modern memory hierarchy
### You can't structurally modify a collection when iterating over it
Modifying a collection will possibly invalidate all iterators currently initialized by it. Currently, the only collection that allows this is the LinkedList (using the node-based functions, not the iterator).
## What to use
(Outdated)
The following table shows how each collection is implemented and how well they do when using as common [abstract data types](https://en.wikipedia.org/wiki/Abstract_data_type).
* **Ideal** - The collection implements correctly the abstract data type;
* **Not Ideal** - The implementation is fulfilled but some functionalities are either not part of the ADT or not present;
* **Bad** - It can be done, but its a bad idea.
![DataStructuresDiagram](https://i.imgur.com/hFqBSlC.png)
![GoodColor](https://img.shields.io/badge/Ideal_Implementation-%236abf69.svg)
![AverageColor](https://img.shields.io/badge/Not_Ideal_Implementation-%2363a4ff.svg)
![BadColor](https://img.shields.io/badge/Bad_Implementation-%23ff6659.svg)
## Code Review
Check out some code reviews that covers some parts the project. Most of them were posted a long time ago and are probably outdated.
| About | Link |
| ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- |
| Unit Test *./utl/test.h* | [![Code Review](http://www.zomis.net/codereview/shield/?qid=222954)](http://codereview.stackexchange.com/q/222954/178948) |
| Interval Heap *./cmc/intervalheap.h* | [![Code Review](http://www.zomis.net/codereview/shield/?qid=223595)](http://codereview.stackexchange.com/q/223595/178948) |
| Hash Set *./cmc/hashset.h* | [![Code Review](http://www.zomis.net/codereview/shield/?qid=217333)](http://codereview.stackexchange.com/q/217333/178948) |
| Linked List *./cmc/linkedlist.h* | [![Code Review](http://www.zomis.net/codereview/shield/?qid=216737)](http://codereview.stackexchange.com/q/216737/178948) |
| Others | [![Code Review](http://www.zomis.net/codereview/shield/?qid=213553)](http://codereview.stackexchange.com/q/213553/178948) |
+2 -2
View File
@@ -27,7 +27,7 @@
*
* List bi-directional iterator.
*/
#ifdef CMC_EXT_DEQUE_ITER
#ifdef CMC_EXT_LIST_ITER
/* List Iterator */
struct CMC_DEF_ITER(SNAME)
@@ -45,4 +45,4 @@ struct CMC_DEF_ITER(SNAME)
bool end;
};
#endif /* CMC_EXT_DEQUE_ITER */
#endif /* CMC_EXT_LIST_ITER */
File renamed without changes.
File renamed without changes.
File renamed without changes.
+11
View File
@@ -0,0 +1,11 @@
CC=gcc
CFLAGS=-Wall -fprofile-arcs -ftest-coverage -g -O0
INCLUDE=-I ../src
main:
$(CC) ./main.c $(INCLUDE) $(CFLAGS) -o $@.exe
valgrind --leak-check=yes ./$@.exe
gcov -p main.gcda
clean:
rm ./*.gcov ./*.gcda ./*.gcno ./*.exe -f
+102
View File
@@ -0,0 +1,102 @@
#include <inttypes.h>
#include <stdio.h>
// #include "unt_bitset.h"
#include "unt_deque.h"
// #include "unt_hashbidimap.h"
// #include "unt_hashmap.h"
// #include "unt_hashmultimap.h"
// #include "unt_hashmultiset.h"
// #include "unt_hashset.h"
// #include "unt_heap.h"
// #include "unt_intervalheap.h"
// #include "unt_linkedlist.h"
#include "unt_list.h"
// #include "unt_queue.h"
// #include "unt_sortedlist.h"
#include "unt_stack.h"
// #include "unt_treemap.h"
// #include "unt_treeset.h"
// #include "unt_foreach.h"
#include "cmc/utl/assert.h"
#include "cmc/utl/timer.h"
#include "utl.h"
#define cmc_run(unit, unit_fails, test_fails) \
do \
{ \
uintmax_t f = unit(); \
if (f != 0) \
unit_fails += 1; \
test_fails += f; \
} while (0)
int main(void)
{
// clang-format off
printf(" +---------------------------------------------------------------+\n");
printf(" | C Macro Collections Library |\n");
printf(" | |\n");
printf(" | Test Suite |\n");
printf(" | |\n");
printf(" | https://github.com/LeoVen/C-Macro-Collections |\n");
printf(" | Leonardo Vencovsky (leonardo.vencovsky@gmail.com) |\n");
printf(" +---------------------------------------------------------------+\n\n");
// clang-format on
struct cmc_timer timer;
cmc_timer_start(timer);
uintmax_t tests = 0, units = 0;
// cmc_run(CMCBitSet, units, tests);
// cmc_run(CMCBitSetIter, units, tests);
cmc_run(CMCDeque, units, tests);
cmc_run(CMCDequeIter, units, tests);
// cmc_run(CMCHashBidiMap, units, tests);
// cmc_run(CMCHashBidiMapIter, units, tests);
// cmc_run(CMCHashMap, units, tests);
// cmc_run(CMCHashMapIter, units, tests);
// cmc_run(CMCHashMultiMap, units, tests);
// cmc_run(CMCHashMultiMapIter, units, tests);
// cmc_run(CMCHashMultiSet, units, tests);
// cmc_run(CMCHashMultiSetIter, units, tests);
// cmc_run(CMCHashSet, units, tests);
// cmc_run(CMCHashSetIter, units, tests);
// cmc_run(CMCHeap, units, tests);
// cmc_run(CMCHeapIter, units, tests);
// cmc_run(CMCIntervalHeap, units, tests);
// cmc_run(CMCIntervalHeapIter, units, tests);
// cmc_run(CMCLinkedList, units, tests);
// cmc_run(CMCLinkedListIter, units, tests);
cmc_run(CMCList, units, tests);
cmc_run(CMCListIter, units, tests);
// cmc_run(CMCQueue, units, tests);
// cmc_run(CMCQueueIter, units, tests);
// cmc_run(CMCSortedList, units, tests);
// cmc_run(CMCSortedListIter, units, tests);
cmc_run(CMCStack, units, tests);
cmc_run(CMCStackIter, units, tests);
// cmc_run(CMCTreeMap, units, tests);
// cmc_run(CMCTreeMapIter, units, tests);
// cmc_run(CMCTreeSet, units, tests);
// cmc_run(CMCTreeSetIter, units, tests);
// cmc_run(ForEach, units, tests);
cmc_timer_stop(timer);
printf(" +---------------------------------------------------------------+\n");
printf(" | SUMMARY |\n");
printf(" +---------------------------------------------------------------+\n");
printf(" | Total Units Failed : %25" PRIuMAX " units |\n", units);
printf(" | Total Tests Failed : %25" PRIuMAX " tests |\n", tests);
printf(" | Total Asserts Failed : %25" PRIuMAX " asserts |\n", cmc_assert_failed);
printf(" | Total Runtime : %25.0lf milliseconds |\n", timer.result);
printf(" +---------------------------------------------------------------+\n");
return tests;
}
+1725
View File
File diff suppressed because it is too large. Load diff
+1072
View File
File diff suppressed because it is too large. Load diff
+854
View File
@@ -0,0 +1,854 @@
#ifndef CMC_TESTS_UNT_STACK_H
#define CMC_TESTS_UNT_STACK_H
#include "utl.h"
#define V size_t
#define PFX s
#define SNAME stack
#define CMC_EXT_STACK_INIT
#define CMC_EXT_STACK_ITER
#include "cmc/stack.h"
struct stack_fval *s_fval = &(struct stack_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(CMCStack, true, {
CMC_CREATE_TEST(new, {
struct stack *s = s_new(1000000, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert_not_equals(ptr, NULL, s->buffer);
cmc_assert_equals(size_t, 1000000, s_capacity(s));
cmc_assert_equals(size_t, 0, s_count(s));
cmc_assert_not_equals(ptr, NULL, s->buffer);
s_free(s);
});
CMC_CREATE_TEST(new[capacity = 0], {
struct stack *s = s_new(0, s_fval);
cmc_assert_equals(ptr, NULL, s);
});
CMC_CREATE_TEST(new[capacity = UINT64_MAX], {
struct stack *s = s_new(UINT64_MAX, s_fval);
cmc_assert_equals(ptr, NULL, s);
});
CMC_CREATE_TEST(clear[count capacity], {
struct stack *s = s_new(100, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(buffer_growth[capacity = 1], {
struct stack *s = s_new(1, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(push[count], {
struct stack *s = s_new(100, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(push[capacity], {
struct stack *s = s_new(100, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(push[item preservation], {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 250; i++)
cmc_assert(s_push(s, i));
for (size_t i = 0; i < s_count(s); i++)
cmc_assert_equals(size_t, i, s->buffer[i]);
s_free(s);
});
CMC_CREATE_TEST(pop[count], {
struct stack *s = s_new(100, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(pop[capacity], {
struct stack *s = s_new(100, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(pop[item preservation], {
struct stack *s = s_new(100, s_fval);
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));
for (size_t i = 0; i < s_count(s); i++)
cmc_assert_equals(size_t, i, s->buffer[i]);
s_free(s);
});
CMC_CREATE_TEST(pop[count = 0], {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert_equals(bool, false, s_pop(s));
s_free(s);
});
CMC_CREATE_TEST(top, {
struct stack *s = s_new(100, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(top[count = 0], {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_push(s, 1));
cmc_assert(s_pop(s));
cmc_assert_equals(size_t, (size_t){ 0 }, s_top(s));
s_free(s);
});
CMC_CREATE_TEST(top[count = 1], {
struct stack *s = s_new(100, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(contains, {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i < 150; i++)
cmc_assert(s_push(s, i));
for (size_t i = 0; i < 150; i++)
cmc_assert(s_contains(s, i));
s_free(s);
});
CMC_CREATE_TEST(contains[count = 0], {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert_equals(bool, false, s_contains(s, 10));
s_free(s);
});
CMC_CREATE_TEST(empty, {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_empty(s));
s_free(s);
});
CMC_CREATE_TEST(empty[after_io], {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_push(s, 10));
cmc_assert(s_pop(s));
cmc_assert(s_empty(s));
s_free(s);
});
CMC_CREATE_TEST(full, {
struct stack *s = s_new(100, s_fval);
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));
s_free(s);
});
CMC_CREATE_TEST(full[capacity = 1], {
struct stack *s = s_new(1, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert(s_push(s, 10));
cmc_assert(s_full(s));
s_free(s);
});
CMC_CREATE_TEST(flags, {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
// clear
s->flag = CMC_FLAG_ERROR;
s_clear(s);
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
// customize
s->flag = CMC_FLAG_ERROR;
s_customize(s, NULL, NULL);
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
// push
s->flag = CMC_FLAG_ERROR;
cmc_assert(s_push(s, 1));
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
// pop
s->flag = CMC_FLAG_ERROR;
cmc_assert(s_pop(s));
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
cmc_assert(!s_pop(s));
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, s_flag(s));
// top
s->flag = CMC_FLAG_ERROR;
s_top(s);
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, s_flag(s));
cmc_assert(s_push(s, 1));
s_top(s);
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
// contains
s->flag = CMC_FLAG_ERROR;
cmc_assert(s_contains(s, 1));
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
// copy_of
s->flag = CMC_FLAG_ERROR;
struct stack *s2 = s_copy_of(s);
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s2));
// equals
s->flag = CMC_FLAG_ERROR;
s2->flag = CMC_FLAG_ERROR;
cmc_assert(s_equals(s, s2));
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s));
cmc_assert_equals(int32_t, CMC_FLAG_OK, s_flag(s2));
s_free(s);
s_free(s2);
});
CMC_CREATE_TEST(callbacks, {
struct stack *s = s_new_custom(100, s_fval, NULL, callbacks);
cmc_assert_not_equals(ptr, NULL, s);
total_create = 0;
total_read = 0;
total_update = 0;
total_delete = 0;
total_resize = 0;
cmc_assert(s_push(s, 10));
cmc_assert_equals(int32_t, 1, total_create);
cmc_assert(s_pop(s));
cmc_assert_equals(int32_t, 1, total_delete);
cmc_assert(s_push(s, 1));
cmc_assert_equals(int32_t, 2, total_create);
cmc_assert_equals(size_t, 1, s_top(s));
cmc_assert_equals(int32_t, 1, total_read);
cmc_assert(s_contains(s, 1));
cmc_assert_equals(int32_t, 2, total_read);
cmc_assert(s_resize(s, 1000));
cmc_assert_equals(int32_t, 1, total_resize);
cmc_assert(s_resize(s, 10));
cmc_assert_equals(int32_t, 2, total_resize);
cmc_assert_equals(int32_t, 2, total_create);
cmc_assert_equals(int32_t, 2, total_read);
cmc_assert_equals(int32_t, 0, total_update);
cmc_assert_equals(int32_t, 1, total_delete);
cmc_assert_equals(int32_t, 2, total_resize);
s_customize(s, NULL, NULL);
s_clear(s);
cmc_assert(s_push(s, 10));
cmc_assert(s_pop(s));
cmc_assert(s_push(s, 1));
cmc_assert_equals(size_t, 1, s_top(s));
cmc_assert(s_contains(s, 1));
cmc_assert(s_resize(s, 1000));
cmc_assert(s_resize(s, 10));
cmc_assert_equals(int32_t, 2, total_create);
cmc_assert_equals(int32_t, 2, total_read);
cmc_assert_equals(int32_t, 0, total_update);
cmc_assert_equals(int32_t, 1, total_delete);
cmc_assert_equals(int32_t, 2, total_resize);
cmc_assert_equals(ptr, NULL, s->callbacks);
s_free(s);
total_create = 0;
total_read = 0;
total_update = 0;
total_delete = 0;
total_resize = 0;
});
});
CMC_CREATE_UNIT(CMCStackIter, true, {
CMC_CREATE_TEST(PFX##_iter_start(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_start(s);
cmc_assert_equals(ptr, s, it.target);
cmc_assert_equals(size_t, 0, it.cursor);
cmc_assert_equals(bool, true, it.start);
cmc_assert_equals(bool, true, it.end);
cmc_assert(s_iter_at_start(&it));
cmc_assert(s_iter_at_end(&it));
cmc_assert(s_push(s, 1));
cmc_assert(s_push(s, 2));
it = s_iter_start(s);
cmc_assert_equals(size_t, s->count - 1, it.cursor);
cmc_assert_equals(bool, false, it.end);
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_end(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_end(s);
cmc_assert_equals(ptr, s, it.target);
cmc_assert_equals(size_t, 0, it.cursor);
cmc_assert_equals(bool, true, it.start);
cmc_assert_equals(bool, true, it.end);
cmc_assert(s_iter_at_start(&it));
cmc_assert(s_iter_at_end(&it));
cmc_assert(s_push(s, 1));
cmc_assert(s_push(s, 2));
cmc_assert(s_push(s, 3));
it = s_iter_end(s);
cmc_assert_equals(size_t, 0, it.cursor);
cmc_assert_equals(bool, false, it.start);
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_at_start(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_start(s);
// Empty checks
cmc_assert(s_iter_at_start(&it));
it = s_iter_end(s);
cmc_assert(s_iter_at_start(&it));
// Non-empty checks
cmc_assert(s_push(s, 1));
it = s_iter_end(s);
cmc_assert(!s_iter_at_start(&it));
it = s_iter_start(s);
cmc_assert(s_iter_at_start(&it));
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_at_end(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_start(s);
// Empty check
cmc_assert(s_iter_at_end(&it));
it = s_iter_end(s);
cmc_assert(s_iter_at_end(&it));
// Non-empty checks
cmc_assert(s_push(s, 1));
it = s_iter_end(s);
cmc_assert(s_iter_at_end(&it));
it = s_iter_start(s);
cmc_assert(!s_iter_at_end(&it));
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_to_start(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_start(s);
cmc_assert(!s_iter_to_start(&it));
for (size_t i = 1; i <= 100; i++)
s_push(s, i);
cmc_assert_equals(size_t, 100, s->count);
it = s_iter_end(s);
cmc_assert(!s_iter_at_start(&it));
cmc_assert(s_iter_at_end(&it));
cmc_assert_equals(size_t, 1, s_iter_value(&it));
cmc_assert(s_iter_to_start(&it));
cmc_assert(s_iter_at_start(&it));
cmc_assert(!s_iter_at_end(&it));
cmc_assert_equals(size_t, 100, s_iter_value(&it));
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_to_end(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_end(s);
cmc_assert(!s_iter_to_end(&it));
for (size_t i = 1; i <= 100; i++)
s_push(s, i);
it = s_iter_start(s);
cmc_assert(s_iter_at_start(&it));
cmc_assert(!s_iter_at_end(&it));
cmc_assert_equals(size_t, 100, s_iter_value(&it));
cmc_assert(s_iter_to_end(&it));
cmc_assert(!s_iter_at_start(&it));
cmc_assert(s_iter_at_end(&it));
cmc_assert_equals(size_t, 1, s_iter_value(&it));
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_next(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_start(s);
cmc_assert(!s_iter_next(&it));
for (size_t i = 1; i <= 1000; i++)
s_push(s, i);
size_t sum = 0;
for (it = s_iter_start(s); !s_iter_at_end(&it); s_iter_next(&it))
{
sum += s_iter_value(&it);
}
cmc_assert_equals(size_t, 500500, sum);
sum = 0;
s_iter_to_start(&it);
do
{
sum += s_iter_value(&it);
} while (s_iter_next(&it));
cmc_assert_equals(size_t, 500500, sum);
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_prev(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_end(s);
cmc_assert(!s_iter_prev(&it));
for (size_t i = 1; i <= 1000; i++)
s_push(s, i);
size_t sum = 0;
for (it = s_iter_end(s); !s_iter_at_start(&it); s_iter_prev(&it))
{
sum += s_iter_value(&it);
}
cmc_assert_equals(size_t, 500500, sum);
sum = 0;
s_iter_to_end(&it);
do
{
sum += s_iter_value(&it);
} while (s_iter_prev(&it));
cmc_assert_equals(size_t, 500500, sum);
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_advance(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_start(s);
cmc_assert(!s_iter_advance(&it, 1));
for (size_t i = 0; i <= 1000; i++)
s_push(s, i);
it = s_iter_start(s);
cmc_assert(!s_iter_advance(&it, 0));
cmc_assert(!s_iter_advance(&it, s->count));
size_t sum = 0;
for (it = s_iter_start(s);;)
{
sum += s_iter_value(&it);
if (!s_iter_advance(&it, 2))
break;
}
cmc_assert_equals(size_t, 250500, sum);
s_iter_to_start(&it);
cmc_assert(s_iter_advance(&it, s->count - 1));
// sum (1000, 990, 980, 970, ..., 910, 900)
sum = 0;
it = s_iter_start(s);
for (size_t val = s_iter_value(&it); val > 900; s_iter_advance(&it, 10))
{
val = s_iter_value(&it);
sum += val;
}
cmc_assert_equals(size_t, 10450, sum);
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_rewind(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_end(s);
cmc_assert(!s_iter_rewind(&it, 1));
for (size_t i = 0; i <= 1000; i++)
s_push(s, i);
it = s_iter_end(s);
cmc_assert(!s_iter_rewind(&it, 0));
cmc_assert(!s_iter_rewind(&it, s->count));
size_t sum = 0;
for (it = s_iter_end(s);;)
{
sum += s_iter_value(&it);
if (!s_iter_rewind(&it, 2))
break;
}
cmc_assert_equals(size_t, 250500, sum);
s_iter_to_end(&it);
cmc_assert(s_iter_rewind(&it, s->count - 1));
// sum (0, 10, 20, ..., 90, 100)
sum = 0;
it = s_iter_end(s);
for (size_t val = s_iter_value(&it); val < 100; s_iter_rewind(&it, 10))
{
val = s_iter_value(&it);
sum += val;
}
cmc_assert_equals(size_t, 550, sum);
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_go_to(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_end(s);
cmc_assert(!s_iter_go_to(&it, 0));
it = s_iter_start(s);
cmc_assert(!s_iter_go_to(&it, 0));
for (size_t i = 1001; i > 0; i--)
s_push(s, i - 1);
it = s_iter_start(s);
size_t sum = 0;
for (size_t i = 0; i < 1001; i++)
{
s_iter_go_to(&it, i);
sum += s_iter_value(&it);
}
cmc_assert_equals(size_t, 500500, sum);
sum = 0;
for (size_t i = 1001; i > 0; i--)
{
cmc_assert(s_iter_go_to(&it, i - 1));
sum += s_iter_value(&it);
}
cmc_assert_equals(size_t, 500500, sum);
sum = 0;
for (size_t i = 1000; i > 0; i -= 100)
{
cmc_assert(s_iter_go_to(&it, 1000 - i));
cmc_assert_equals(size_t, i, s_iter_index(&it));
sum += s_iter_value(&it);
}
cmc_assert_equals(size_t, 5500, sum);
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_value(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_end(s);
cmc_assert_equals(size_t, (size_t){ 0 }, s_iter_value(&it));
cmc_assert(s_push(s, 10));
it = s_iter_start(s);
cmc_assert_equals(size_t, 10, s_iter_value(&it));
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_rvalue(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
struct stack_iter it = s_iter_end(s);
cmc_assert_equals(ptr, NULL, s_iter_rvalue(&it));
cmc_assert(s_push(s, 10));
it = s_iter_start(s);
cmc_assert_not_equals(ptr, NULL, s_iter_rvalue(&it));
cmc_assert_equals(size_t, 10, *s_iter_rvalue(&it));
s_free(s);
});
CMC_CREATE_TEST(PFX##_iter_index(), {
struct stack *s = s_new(100, s_fval);
cmc_assert_not_equals(ptr, NULL, s);
for (size_t i = 0; i <= 1000; i++)
s_push(s, i);
struct stack_iter it = s_iter_start(s);
for (size_t i = 0; i < 1001; i++)
{
cmc_assert_equals(size_t, i, s_iter_index(&it));
s_iter_next(&it);
}
it = s_iter_end(s);
for (size_t i = 1001; i > 0; i--)
{
cmc_assert_equals(size_t, i - 1, s_iter_index(&it));
s_iter_prev(&it);
}
s_free(s);
});
});
#ifdef CMC_TEST_MAIN
int main(void)
{
int result = CMCStack() + CMCStackIter();
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | CMCStack Suit : %-48s |\n", result == 0 ? "PASSED" : "FAILED");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
}
#endif
#endif /* CMC_TESTS_UNT_STACK_H */
+171
View File
@@ -0,0 +1,171 @@
/* Utility file to be included in the unit tests */
#ifndef CMC_TESTS_UTL
#define CMC_TESTS_UTL
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include "cmc/cor/hashtable.h"
#include "cmc/utl/assert.h"
#include "cmc/utl/foreach.h"
#include "cmc/utl/futils.h"
#include "cmc/utl/test.h"
size_t numhash(size_t a)
{
return a;
}
size_t hashcapminus1(size_t a)
{
(void)a;
return cmc_hashtable_primes[0] - 1;
}
size_t hashcapminus4(size_t a)
{
(void)a;
return cmc_hashtable_primes[0] - 1;
}
size_t hash0(size_t a)
{
(void)a;
return 0;
}
// counter ftab functions
int32_t k_total_cmp = 0;
int32_t k_total_cpy = 0;
int32_t k_total_str = 0;
int32_t k_total_free = 0;
int32_t k_total_hash = 0;
int32_t k_total_pri = 0;
int32_t v_total_cmp = 0;
int32_t v_total_cpy = 0;
int32_t v_total_str = 0;
int32_t v_total_free = 0;
int32_t v_total_hash = 0;
int32_t v_total_pri = 0;
int k_c_cmp(size_t a, size_t b)
{
k_total_cmp++;
return (a > b) - (a < b);
}
size_t k_c_cpy(size_t a)
{
k_total_cpy++;
return a;
}
bool k_c_str(FILE *fptr, size_t a)
{
k_total_str++;
return fprintf(fptr, "%" PRIuMAX " ", a) > 0;
}
void k_c_free(size_t a)
{
(void)a;
k_total_free++;
}
size_t k_c_hash(size_t x)
{
k_total_hash++;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
return x;
}
int k_c_pri(size_t a, size_t b)
{
k_total_pri++;
return (a > b) - (a < b);
}
int v_c_cmp(size_t a, size_t b)
{
v_total_cmp++;
return (a > b) - (a < b);
}
size_t v_c_cpy(size_t a)
{
v_total_cpy++;
return a;
}
bool v_c_str(FILE *fptr, size_t a)
{
v_total_str++;
return fprintf(fptr, "%" PRIuMAX " ", a) > 0;
}
void v_c_free(size_t a)
{
(void)a;
v_total_free++;
}
size_t v_c_hash(size_t x)
{
v_total_hash++;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
return x;
}
int v_c_pri(size_t a, size_t b)
{
v_total_pri++;
return (a > b) - (a < b);
}
// callbacks
int total_create = 0;
int total_read = 0;
int total_update = 0;
int total_delete = 0;
int total_resize = 0;
void callback_create(void)
{
total_create++;
}
void callback_read(void)
{
total_read++;
}
void callback_update(void)
{
total_update++;
}
void callback_delete(void)
{
total_delete++;
}
void callback_resize(void)
{
total_resize++;
}
struct cmc_callbacks *callbacks = &(struct cmc_callbacks){ .create = callback_create,
.read = callback_read,
.update = callback_update,
.delete = callback_delete,
.resize = callback_resize };
#endif /* CMC_TESTS_UTL */