Initial commit

This commit is contained in:
2025-01-18 17:24:36 +03:00
commit 453843f51a
28 changed files with 7356 additions and 0 deletions

483
test/src/testalgo.c Normal file
View File

@@ -0,0 +1,483 @@
#include <bh/algo.h>
#include <bh/unit.h>
#include <string.h>
#include <stdlib.h>
static int int_equal(const void *lhs,
const void *rhs)
{
return *(const int*)lhs - *(const int*)rhs;
}
static int verify_partition(size_t index,
int pivot,
int *array,
size_t size)
{
size_t i;
for (i = 0; i < index; i++)
if (array[i] >= pivot)
return -1;
for (i = index; i < size; i++)
if (array[i] < pivot)
return -1;
return 0;
}
static int verify_heap(int *array,
size_t size)
{
size_t i, left, right;
for (i = 0; i < size; i++)
{
left = i * 2 + 1;
right = i * 2 + 2;
if (left < size && array[i] < array[left])
return -1;
if (right < size && array[i] < array[right])
return -1;
}
return 0;
}
static int reference_rand(void)
{
static uint32_t next = 2025;
next = next * 1103515245 + 12345;
return (next / 65536) % 32768;
}
static void reference_sort(int *array, size_t size)
{
size_t i, j;
int tmp;
/* Reference sort is... buble sort! */
for (i = 0; i < size; i++)
{
for (j = 1; j < size; j++)
{
if (array[j - 1] > array[j])
{
tmp = array[j - 1];
array[j - 1] = array[j];
array[j] = tmp;
}
}
}
}
static void fill_arrays(int *array,
int *reference,
size_t size,
int mask)
{
size_t i;
int negate;
/* Check if mask needs to be negated */
negate = mask < 0;
if (negate)
mask = -mask;
/* Fill the array */
for (i = 0; i < size; ++i)
{
array[i] = reference_rand();
if (mask > 1)
array[i] = array[i] % mask;
if (negate)
array[i] = -array[i];
if (reference)
reference[i] = array[i];
}
}
static int check_sort(void)
{
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int reference[317], data[317];
/* Test empty array and one element arrays */
data[0] = 1; data[1] = 2; data[2] = 3;
bh_sort(data, 0, sizeof(int), int_equal);
BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3);
bh_sort(data, 1, sizeof(int), int_equal);
BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3);
/* Test array against different sizes */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, 0);
reference_sort(reference, *size);
bh_sort(data, *size, sizeof(int), int_equal);
BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0);
}
/* Test against negative values */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, -1);
reference_sort(reference, *size);
bh_sort(data, *size, sizeof(int), int_equal);
BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0);
}
/* Test against duplicates */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, 4);
reference_sort(reference, *size);
bh_sort(data, *size, sizeof(int), int_equal);
BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0);
}
return 0;
}
static int check_partition(void)
{
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int reference[317], data[317], value, *pivot;
/* Test empty array and one element array */
data[0] = 1; data[1] = 2; data[2] = 3;
value = 0;
pivot = bh_partition(&value, data, 0, sizeof(int), int_equal);
BH_VERIFY(pivot != NULL);
BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3);
pivot = bh_partition(&value, data, 1, sizeof(int), int_equal);
BH_VERIFY(pivot == data);
BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3);
/* Test array against different sizes */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, 0);
value = 16384;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
/* Test against negative values */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, -1);
value = -16384;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
/* Test against duplicates */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, 4);
value = 2;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
/* Test array against small pivots */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, 0);
value = -100;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
/* Test array against large pivots */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, 0);
value = 65536;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
/* Test array against different sizes (pivot inside the array) */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, 0);
value = data[0];
pivot = bh_partition(data, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
/* Test against negative values (pivot inside the array) */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, -1);
value = data[0];
pivot = bh_partition(data, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
/* Test against duplicates (pivot inside the array) */
for (size = sizes; *size; ++size)
{
fill_arrays(data, reference, *size, 4);
value = data[0];
pivot = bh_partition(data, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
/* Same data test */
for (size = sizes; *size; ++size)
{
memset(data, 0, sizeof(int) * *size);
memset(reference, 0, sizeof(int) * *size);
value = data[*size - 1];
pivot = bh_partition(data + *size - 1, data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
}
return 0;
}
static int check_heap_make(void)
{
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int data[317];
/* Test empty array */
data[0] = 1; data[1] = 2; data[2] = 3;
bh_heap_make(data, 0, sizeof(int), int_equal);
BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3);
/* Test array against different sizes */
for (size = sizes; *size; ++size)
{
fill_arrays(data, NULL, *size, 0);
bh_heap_make(data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, *size) == 0);
}
/* Test array against negative values */
for (size = sizes; *size; ++size)
{
fill_arrays(data, NULL, *size, -1);
bh_heap_make(data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, *size) == 0);
}
/* Test against duplicates */
for (size = sizes; *size; ++size)
{
fill_arrays(data, NULL, *size, 4);
bh_heap_make(data, *size, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, *size) == 0);
}
return 0;
}
static int check_heap_insert(void)
{
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int data[317], reference[317];
/* Test array against different sizes */
for (size = sizes; *size; ++size)
{
size_t i;
fill_arrays(data, reference, *size, 0);
for (i = 0; i < *size; ++i)
{
int value;
value = data[i];
bh_heap_insert(&value, data, i, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, i + 1) == 0);
}
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size) == 0);
}
/* Test array against different sizes (inplace)*/
for (size = sizes; *size; ++size)
{
size_t i;
fill_arrays(data, reference, *size, 0);
for (i = 0; i < *size; ++i)
{
bh_heap_insert(NULL, data, i, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, i + 1) == 0);
}
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size) == 0);
}
return 0;
}
static int check_heap_remove(void)
{
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int data[317], reference[317];
/* Test array against different sizes */
for (size = sizes; *size; ++size)
{
size_t i;
fill_arrays(data, reference, *size, 0);
bh_heap_make(data, *size, sizeof(int), int_equal);
for (i = *size; i > 0; i--)
{
BH_VERIFY(verify_heap(data, i) == 0);
bh_heap_remove(data, i, sizeof(int), int_equal);
}
reference_sort(data, *size);
reference_sort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size) == 0);
}
return 0;
}
static int check_heap_replace(void)
{
int data[11] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0};
int reference[11] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0};
int value;
/* Prepare test arrays */
bh_heap_make(data, 10, sizeof(int), int_equal);
bh_heap_make(reference, 10, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, 10) == 0);
BH_VERIFY(verify_heap(reference, 10) == 0);
/* Verfify heap replace */
value = 20;
bh_heap_replace(&value, data, 10, sizeof(int), int_equal);
bh_heap_remove(reference, 10, sizeof(int), int_equal);
bh_heap_insert(&value, reference, 9, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, 10) == 0);
BH_VERIFY(verify_heap(reference, 10) == 0);
bh_sort(data, 10, sizeof(int), int_equal);
bh_sort(reference, 10, sizeof(int), int_equal);
BH_VERIFY(memcmp(data, reference, 10 * sizeof(int)) == 0);
/* Verify heap replace on single element array */
value = 400;
bh_heap_replace(&value, data, 1, sizeof(int), int_equal);
bh_heap_remove(reference, 1, sizeof(int), int_equal);
bh_heap_insert(&value, reference, 0, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, 1) == 0);
BH_VERIFY(verify_heap(reference, 1) == 0);
BH_VERIFY(memcmp(data, reference, 1 * sizeof(int)) == 0);
/* Prepare test arrays */
bh_sort(data, 10, sizeof(int), int_equal);
bh_sort(reference, 10, sizeof(int), int_equal);
bh_heap_make(data, 10, sizeof(int), int_equal);
bh_heap_make(reference, 10, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, 10) == 0);
BH_VERIFY(verify_heap(reference, 10) == 0);
data[10] = 1000;
value = 1000;
bh_heap_replace(NULL, data, 10, sizeof(int), int_equal);
bh_heap_remove(reference, 10, sizeof(int), int_equal);
bh_heap_insert(&value, reference, 9, sizeof(int), int_equal);
BH_VERIFY(verify_heap(data, 10) == 0);
BH_VERIFY(verify_heap(reference, 10) == 0);
bh_sort(data, 10, sizeof(int), int_equal);
bh_sort(reference, 10, sizeof(int), int_equal);
BH_VERIFY(memcmp(data, reference, 10 * sizeof(int)) == 0);
return 0;
}
int main(int argc, char **argv)
{
BH_UNUSED(argc);
BH_UNUSED(argv);
bh_unit_add("sort", check_sort);
bh_unit_add("partition", check_partition);
bh_unit_add("heap_make", check_heap_make);
bh_unit_add("heap_insert", check_heap_insert);
bh_unit_add("heap_remove", check_heap_remove);
bh_unit_add("heap_replace", check_heap_replace);
return bh_unit_run();
}

630
test/src/testfile.c Normal file
View File

@@ -0,0 +1,630 @@
#include <bh/unit.h>
#include <bh/io.h>
#include <string.h>
#define FILENAME1 "bhfile1.dat"
#define FILENAME2 "bhfile2.dat"
#define FILENAME3 "bhfile3.dat"
#define FILENAME4 "bhfile4.dat"
/**
* Cleanup any files, that could be left from previous test runs.
*/
static void cleanup(void)
{
remove(FILENAME1);
remove(FILENAME2);
remove(FILENAME3);
remove(FILENAME4);
}
/**
* Check for invalid arguments.
*/
static int check_null(void)
{
bh_io_t *io;
/* Check against NULL pointers */
BH_VERIFY(bh_file_new(NULL) == NULL);
BH_VERIFY(bh_io_classname(NULL) == NULL);
BH_VERIFY(bh_io_open(NULL, 0) != BH_OK);
BH_VERIFY(bh_io_close(NULL) != BH_OK);
BH_VERIFY(bh_io_read(NULL, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_write(NULL, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_peek(NULL, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_tell(NULL, NULL) != BH_OK);
BH_VERIFY(bh_io_seek(NULL, 0, 0) != BH_OK);
BH_VERIFY(bh_io_flush(NULL) != BH_OK);
BH_VERIFY(bh_io_size(NULL, NULL) != BH_OK);
BH_VERIFY(bh_io_flags(NULL) == BH_IO_FLAG_ERROR);
BH_VERIFY(bh_io_clear(NULL) == BH_OK);
bh_io_free(NULL);
/* Check against NULL pointers and valid IO object */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, 0) != BH_OK);
BH_VERIFY(bh_io_close(io) != BH_OK);
BH_VERIFY(bh_io_read(io, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_write(io, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_peek(io, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_tell(io, NULL) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, 0) != BH_OK);
BH_VERIFY(bh_io_flush(io) != BH_OK);
BH_VERIFY(bh_io_size(io, NULL) != BH_OK);
BH_VERIFY(bh_io_flags(io) == BH_IO_FLAG_ERROR);
BH_VERIFY(bh_io_clear(io) == BH_OK);
bh_io_free(io);
return 0;
}
/**
* Check for normal mode.
*/
static int check_normal(void)
{
int64_t position;
char buffer[128];
size_t actual;
bh_io_t *io;
/* Check operations for write only access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_open(io, BH_IO_READ) != BH_OK);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK);
BH_VERIFY(bh_io_tell(io, &position) == BH_OK);
BH_VERIFY(position == 20);
bh_io_close(io);
/* Check operations for read only access */
BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "67890", 5) == 0);
bh_io_close(io);
/* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890abcde67890", 20, &actual) == BH_OK);
BH_VERIFY(actual == 20);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK);
BH_VERIFY(actual == 35);
BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0);
bh_io_close(io);
bh_io_free(io);
return 0;
}
/**
* Check for truncate mode.
*/
static int check_truncate(void)
{
int64_t position;
char buffer[128];
size_t actual;
bh_io_t *io;
/* Check operations for write only access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK);
BH_VERIFY(bh_io_tell(io, &position) == BH_OK);
BH_VERIFY(position == 20);
bh_io_close(io);
/* Check operations for read only access without truncate */
BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0);
BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "67890", 5) == 0);
bh_io_close(io);
/* Check operations for read only access */
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 0);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 0);
bh_io_close(io);
/* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890abcde67890", 20, &actual) == BH_OK);
BH_VERIFY(actual == 20);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK);
BH_VERIFY(actual == 35);
BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0);
bh_io_close(io);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
bh_io_close(io);
bh_io_free(io);
return 0;
}
/**
* Check for exist mode.
*/
static int check_exist(void)
{
int64_t position;
char buffer[128];
size_t actual;
bh_io_t *io;
/* Check operations for write only access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_EXIST) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK);
BH_VERIFY(bh_io_tell(io, &position) == BH_OK);
BH_VERIFY(position == 20);
bh_io_close(io);
/* Check operations for read only access */
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_EXIST) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "67890", 5) == 0);
bh_io_close(io);
/* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_EXIST) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890abcde67890", 20, &actual) == BH_OK);
BH_VERIFY(actual == 20);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK);
BH_VERIFY(actual == 35);
BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0);
bh_io_close(io);
bh_io_free(io);
/* Check against non existing files */
BH_VERIFY((io = bh_file_new(FILENAME2)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_EXIST) != BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_EXIST) != BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_EXIST) != BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0);
bh_io_free(io);
return 0;
}
/**
* Check in append mode.
*/
static int check_append(void)
{
int64_t position;
char buffer[128];
size_t actual;
bh_io_t *io;
/* Explicitly call cleanup */
cleanup();
/* Check operations for write only access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_APPEND) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK);
BH_VERIFY(bh_io_tell(io, &position) == BH_OK);
BH_VERIFY(position == 25);
bh_io_close(io);
/* Check operations for read only access */
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_APPEND) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
bh_io_close(io);
/* Check operations for read and write access */
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_APPEND) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 40, &actual) == BH_OK);
BH_VERIFY(actual == 40);
BH_VERIFY(memcmp(buffer, "12345678901234567890abcdeabcde1234567890", 40) == 0);
bh_io_close(io);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
bh_io_close(io);
bh_io_free(io);
return 0;
}
/**
* Check for create mode.
*/
static int check_create(void)
{
bh_io_t *io;
/* Check for already existing file */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_CREATE) != BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0);
bh_io_free(io);
/* Check for new file with write access */
BH_VERIFY((io = bh_file_new(FILENAME2)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_CREATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
bh_io_free(io);
/* Check for new file with read access */
BH_VERIFY((io = bh_file_new(FILENAME3)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_CREATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
bh_io_free(io);
/* Check for new file with read/write access */
BH_VERIFY((io = bh_file_new(FILENAME4)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_CREATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
bh_io_free(io);
return 0;
}
/**
* Check for EOF flags.
*/
static int check_eof(void)
{
char buffer[128];
size_t actual;
bh_io_t *io;
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_read(io, buffer, 128, &actual) == BH_OK);
BH_VERIFY(actual == 0);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_EOF);
bh_io_close(io);
bh_io_free(io);
return 0;
}
/**
* Check for error flags.
*/
static int check_error(void)
{
size_t actual;
bh_io_t *io;
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "12345", 5, &actual) != BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_ERROR);
BH_VERIFY(bh_io_clear(io) == BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_ERROR) == 0);
bh_io_close(io);
bh_io_free(io);
return 0;
}
/**
* Check peek operation.
*/
static int check_peek(void)
{
int64_t previous, current;
char buffer[128];
size_t actual;
bh_io_t *io;
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_tell(io, &previous) == BH_OK);
BH_VERIFY(bh_io_peek(io, buffer, 128, &actual) == BH_OK);
BH_VERIFY(bh_io_tell(io, &current) == BH_OK);
BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "12345678901234567890", 20) == 0);
BH_VERIFY(previous == current);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_tell(io, &previous) == BH_OK);
BH_VERIFY(bh_io_peek(io, buffer, 128, &actual) == BH_OK);
BH_VERIFY(bh_io_tell(io, &current) == BH_OK);
BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "12345678901234567890", 20) == 0);
BH_VERIFY(previous == current);
bh_io_close(io);
bh_io_free(io);
return 0;
}
/**
* Check file size operation.
*/
static int check_size(void)
{
bh_io_t *io;
int64_t size;
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_size(io, &size) == BH_OK);
BH_VERIFY(size == 20);
bh_io_close(io);
bh_io_free(io);
return 0;
}
int main(int argc,
char **argv)
{
BH_UNUSED(argc);
BH_UNUSED(argv);
/* Cleanup */
cleanup();
bh_unit_add("null", check_null);
bh_unit_add("normal", check_normal);
bh_unit_add("truncate", check_truncate);
bh_unit_add("exist", check_exist);
bh_unit_add("append", check_append);
bh_unit_add("create", check_create);
bh_unit_add("eof", check_eof);
bh_unit_add("error", check_error);
bh_unit_add("peek", check_peek);
bh_unit_add("size", check_size);
return bh_unit_run();
}

234
test/src/testhashmap.c Normal file
View File

@@ -0,0 +1,234 @@
#include <bh/hashmap.h>
#include <bh/unit.h>
static size_t direct_hash(const void *ptr)
{
return BH_PTR2INT(ptr);
}
static int direct_equal(const void *lhs, const void *rhs)
{
return BH_PTR2INT(lhs) - BH_PTR2INT(rhs);
}
static int new_free(void)
{
bh_hashmap_t *hashmap;
hashmap = bh_hashmap_new(direct_equal, direct_hash);
BH_VERIFY(hashmap != NULL);
BH_VERIFY(bh_hashmap_empty(hashmap) != 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 0);
BH_VERIFY(bh_hashmap_capacity(hashmap) == 0);
BH_VERIFY(bh_hashmap_factor(hashmap) >= 0.15f);
BH_VERIFY(bh_hashmap_factor(hashmap) <= 1.0f);
bh_hashmap_free(hashmap);
return 0;
}
static int grow_shrink(void)
{
bh_hashmap_t *hashmap;
void *iter;
hashmap = bh_hashmap_new(direct_equal, direct_hash);
BH_VERIFY(hashmap != NULL);
/* Allocate space for 1024 entries and insert 1 element */
BH_VERIFY(bh_hashmap_reserve(hashmap, 1024) == 0);
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(1337), BH_INT2PTR(80085)) == 0);
/* Check hashmap contents */
iter = bh_hashmap_iter_next(hashmap, NULL);
BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085);
BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL);
BH_VERIFY(bh_hashmap_empty(hashmap) == 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 1);
BH_VERIFY(bh_hashmap_capacity(hashmap) >= 1024);
/* Change factor and grow */
bh_hashmap_set_factor(hashmap, 0.35f);
/* Check hashmap contents */
iter = bh_hashmap_iter_next(hashmap, NULL);
BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085);
BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL);
BH_VERIFY(bh_hashmap_reserve(hashmap, 8192) == 0);
BH_VERIFY(bh_hashmap_empty(hashmap) == 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 1);
BH_VERIFY(bh_hashmap_capacity(hashmap) >= 8192);
BH_VERIFY(bh_hashmap_factor(hashmap) == 0.35f);
/* Shrink */
BH_VERIFY(bh_hashmap_reserve(hashmap, 0) == 0);
/* Check hashmap contents */
iter = bh_hashmap_iter_next(hashmap, NULL);
BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085);
BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL);
BH_VERIFY(bh_hashmap_empty(hashmap) == 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 1);
BH_VERIFY(bh_hashmap_capacity(hashmap) >= 1);
BH_VERIFY(bh_hashmap_capacity(hashmap) < 8192);
BH_VERIFY(bh_hashmap_factor(hashmap) == 0.35f);
/* Shrink to 0 (deallocate) */
bh_hashmap_clear(hashmap);
BH_VERIFY(bh_hashmap_empty(hashmap) != 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 0);
BH_VERIFY(bh_hashmap_capacity(hashmap) > 0);
BH_VERIFY(bh_hashmap_reserve(hashmap, 0) == 0);
BH_VERIFY(bh_hashmap_empty(hashmap) != 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 0);
BH_VERIFY(bh_hashmap_capacity(hashmap) == 0);
/* Check hashmap contents */
iter = bh_hashmap_iter_next(hashmap, NULL);
BH_VERIFY(iter == NULL);
bh_hashmap_free(hashmap);
return 0;
}
static int insert_remove(void)
{
bh_hashmap_t *hashmap;
size_t i, added, removed;
void *iter;
hashmap = bh_hashmap_new(direct_equal, direct_hash);
BH_VERIFY(hashmap != NULL);
bh_hashmap_set_factor(hashmap, 1.0f);
/* Insert elements into hashmap */
added = 0;
for (i = 1024; i > 0; i--)
{
added += (i - 1) / 4;
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR((i - 1) / 4), BH_INT2PTR(i)) == 0);
}
/* Remove elements */
iter = bh_hashmap_iter_next(hashmap, NULL);
removed = 0;
while (iter)
{
removed += BH_PTR2INT(bh_hashmap_iter_key(iter));
bh_hashmap_iter_remove(hashmap, iter);
iter = bh_hashmap_iter_next(hashmap, NULL);
}
/* Check inserted elements are equal to removed */
BH_VERIFY(added == removed);
bh_hashmap_free(hashmap);
return 0;
}
static int lookup(void)
{
bh_hashmap_t *hashmap;
size_t i;
hashmap = bh_hashmap_new(direct_equal, direct_hash);
BH_VERIFY(hashmap != NULL);
/* Insert elements into hashmap */
for (i = 0; i < 256; i++)
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(i * 4), BH_INT2PTR(i)) == 0);
/* Lookup inserted elements */
for (i = 0; i < 256; i++)
{
void *value;
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), NULL) == BH_OK);
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == i);
}
/* Lookup non-existing elements */
for (i = 256; i < 512; i++)
{
void *value;
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), NULL) != BH_OK);
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), &value) != BH_OK);
}
bh_hashmap_free(hashmap);
return 0;
}
static int clear(void)
{
bh_hashmap_t *hashmap;
size_t i;
hashmap = bh_hashmap_new(direct_equal, direct_hash);
BH_VERIFY(hashmap != NULL);
/* Insert elements into hashmap */
for (i = 0; i < 128; i++)
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(i), 0) == 0);
bh_hashmap_clear(hashmap);
/* Remove non-existing elements */
for (i = 0; i < 128; i++)
bh_hashmap_remove(hashmap, BH_INT2PTR(i));
bh_hashmap_free(hashmap);
return 0;
}
static int fields(void)
{
bh_hashmap_t *hashmap;
size_t i;
hashmap = bh_hashmap_new(direct_equal, direct_hash);
BH_VERIFY(hashmap != NULL);
BH_VERIFY(bh_hashmap_empty(hashmap) == 1);
/* Insert elements into hashmap */
for (i = 0; i < 14; i++)
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(i), NULL) == 0);
/* Check hashmap fields correspond to getter functions */
BH_VERIFY(bh_hashmap_size(hashmap) == 14);
BH_VERIFY(bh_hashmap_capacity(hashmap) >= 14);
BH_VERIFY(bh_hashmap_empty(hashmap) == 0);
bh_hashmap_free(hashmap);
return 0;
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
/* Add unit tests */
bh_unit_add("new_free", new_free);
bh_unit_add("grow_shrink", grow_shrink);
bh_unit_add("insert_remove", insert_remove);
bh_unit_add("lookup", lookup);
bh_unit_add("clear", clear);
bh_unit_add("fields", fields);
return bh_unit_run();
}

171
test/src/testqueue.c Normal file
View File

@@ -0,0 +1,171 @@
#include <bh/queue.h>
#include <bh/unit.h>
static int new_free(void)
{
bh_queue_t *queue;
queue = bh_queue_new();
BH_VERIFY(queue != NULL);
BH_VERIFY(bh_queue_size(queue) == 0);
BH_VERIFY(bh_queue_capacity(queue) == 0);
BH_VERIFY(bh_queue_empty(queue) != 0);
bh_queue_free(queue);
return 0;
}
static int grow_shrink(void)
{
bh_queue_t *queue;
void *value;
queue = bh_queue_new();
BH_VERIFY(queue != NULL);
/* Reserve 1024 elements and insert item into queue */
BH_VERIFY(bh_queue_reserve(queue, 1024) == BH_OK);
BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(1337)) == BH_OK);
BH_VERIFY(bh_queue_capacity(queue) >= 1024);
BH_VERIFY(bh_queue_size(queue) == 1);
BH_VERIFY(bh_queue_empty(queue) == 0);
/* Check queue content */
BH_VERIFY(bh_queue_front(queue, &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == 1337);
/* Grow queue */
BH_VERIFY(bh_queue_reserve(queue, 8192) == BH_OK);
BH_VERIFY(bh_queue_capacity(queue) >= 8192);
BH_VERIFY(bh_queue_size(queue) == 1);
BH_VERIFY(bh_queue_empty(queue) == 0);
/* Check queue content */
BH_VERIFY(bh_queue_front(queue, &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == 1337);
/* Shrink the queue */
BH_VERIFY(bh_queue_reserve(queue, 0) == BH_OK);
BH_VERIFY(bh_queue_capacity(queue) >= 1);
BH_VERIFY(bh_queue_capacity(queue) < 8192);
BH_VERIFY(bh_queue_size(queue) == 1);
BH_VERIFY(bh_queue_empty(queue) == 0);
/* Check queue content */
BH_VERIFY(bh_queue_front(queue, &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == 1337);
/* Shrink to 0 (deallocate) */
bh_queue_clear(queue);
BH_VERIFY(bh_queue_size(queue) == 0);
BH_VERIFY(bh_queue_empty(queue) != 0);
BH_VERIFY(bh_queue_capacity(queue) >= 1);
BH_VERIFY(bh_queue_reserve(queue, 0) == BH_OK);
BH_VERIFY(bh_queue_size(queue) == 0);
BH_VERIFY(bh_queue_empty(queue) != 0);
BH_VERIFY(bh_queue_capacity(queue) == 0);
bh_queue_free(queue);
return 0;
}
static int insert_remove(void)
{
bh_queue_t *queue;
size_t i, added, removed;
void *iter;
queue = bh_queue_new();
BH_VERIFY(queue != NULL);
added = 0;
for (i = 0; i < 256; i++)
{
added += i * 2;
BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(i * 2)) == BH_OK);
}
removed = 0;
iter = bh_queue_iter_next(queue, NULL);
while (iter)
{
void *value;
BH_VERIFY(bh_queue_front(queue, &value) == BH_OK);
removed += BH_PTR2INT(value);
bh_queue_remove(queue);
iter = bh_queue_iter_next(queue, NULL);
}
BH_VERIFY(added == removed);
BH_VERIFY(bh_queue_empty(queue) != 0);
BH_VERIFY(bh_queue_size(queue) == 0);
bh_queue_free(queue);
return 0;
}
static int rollover(void)
{
bh_queue_t *queue;
size_t i, j, capacity;
queue = bh_queue_new();
BH_VERIFY(queue != NULL);
BH_VERIFY(bh_queue_reserve(queue, 128) == 0);
capacity = bh_queue_capacity(queue);
for (i = 0; i < 128; i++)
{
for (j = 0; j < 3; j++)
bh_queue_remove(queue);
for (j = 0; j < 4 && bh_queue_size(queue) < 128; j++)
BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(i * 4 + j)) == 0);
}
BH_VERIFY(bh_queue_size(queue) == 128);
BH_VERIFY(bh_queue_capacity(queue) == capacity);
bh_queue_free(queue);
return 0;
}
static int fields(void)
{
bh_queue_t *queue;
queue = bh_queue_new();
BH_VERIFY(queue != NULL);
BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(1337)) == 0);
BH_VERIFY(bh_queue_size(queue) == 1);
BH_VERIFY(bh_queue_empty(queue) == 0);
BH_VERIFY(bh_queue_capacity(queue) >= 1);
bh_queue_free(queue);
return 0;
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
bh_unit_add("new_free", new_free);
bh_unit_add("grow_shrink", grow_shrink);
bh_unit_add("insert_remove", insert_remove);
bh_unit_add("rollover", rollover);
bh_unit_add("fields", fields);
return bh_unit_run();
}