Change code and naming style, fix several bugs, removed math types.

After a while I felt that putting underscores between words was not the
best solution, so I changed the underscores to capital letters.

Fixed consistency bug between POSIX/Win32 platform in BH_FileOpen.

Removed definitions for math types (vector, matrix, etc.) due to
potential aliasing issues.
This commit is contained in:
2025-01-30 13:53:26 +03:00
parent 8d73a9b473
commit c89cf8f316
22 changed files with 3400 additions and 4135 deletions

View File

@@ -12,7 +12,7 @@
* \param src Pointer to the element * \param src Pointer to the element
* \param size Element size in bytes * \param size Element size in bytes
*/ */
void bh_swap(void *dest, void BH_Swap(void *dest,
void *src, void *src,
size_t size); size_t size);
@@ -30,11 +30,11 @@ void bh_swap(void *dest,
* *
* \return Pointer to the first element of the second partition. * \return Pointer to the first element of the second partition.
*/ */
void *bh_partition(void *pivot, void *BH_Partition(void *pivot,
void *array, void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal); BH_EqualCallback equal);
/** /**
@@ -45,10 +45,10 @@ void *bh_partition(void *pivot,
* \param element Element size in bytes * \param element Element size in bytes
* \param equal Comparision function * \param equal Comparision function
*/ */
void bh_sort(void *array, void BH_Sort(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal); BH_EqualCallback equal);
/** /**
@@ -59,10 +59,10 @@ void bh_sort(void *array,
* \param element Element size in bytes * \param element Element size in bytes
* \param equal Comparision function * \param equal Comparision function
*/ */
void bh_heap_make(void *array, void BH_HeapMake(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal); BH_EqualCallback equal);
/** /**
@@ -73,10 +73,10 @@ void bh_heap_make(void *array,
* \param element Element size in bytes * \param element Element size in bytes
* \param equal Comparasion function * \param equal Comparasion function
*/ */
void bh_heap_remove(void *array, void BH_HeapRemove(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal); BH_EqualCallback equal);
/** /**
@@ -91,11 +91,11 @@ void bh_heap_remove(void *array,
* \param element Element size in bytes * \param element Element size in bytes
* \param equal Comparasion function * \param equal Comparasion function
*/ */
void bh_heap_insert(void *value, void BH_HeapInsert(void *value,
void *array, void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal); BH_EqualCallback equal);
/** /**
@@ -106,8 +106,8 @@ void bh_heap_insert(void *value,
* *
* This function is roughly equivalent to the following code: * This function is roughly equivalent to the following code:
* \code * \code
* bh_heap_remove(array, size, element, equal); * BH_HeapRemove(array, size, element, equal);
* bh_heap_insert(value, array, size - 1, element, equal); * BH_HeapInsert(value, array, size - 1, element, equal);
* \endcode * \endcode
* *
* \param value Pointer to the value * \param value Pointer to the value
@@ -116,11 +116,11 @@ void bh_heap_insert(void *value,
* \param element Element size in bytes * \param element Element size in bytes
* \param equal Comparasion function * \param equal Comparasion function
*/ */
void bh_heap_replace(void *value, void BH_HeapReplace(void *value,
void *array, void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal); BH_EqualCallback equal);
#endif /* BH_ALGO_H */ #endif /* BH_ALGO_H */

View File

@@ -17,8 +17,9 @@
#define BH_PTR2INT(x) ((intptr_t)(x)) #define BH_PTR2INT(x) ((intptr_t)(x))
#define BH_INT2PTR(x) ((void*)(x)) #define BH_INT2PTR(x) ((void*)(x))
typedef int (*bh_equal_cb_t)(const void *, const void *);
typedef size_t (*bh_hash_cb_t)(const void *); typedef int (*BH_EqualCallback)(const void *, const void *);
typedef size_t (*BH_HashCallback)(const void *);
#endif /* BH_COMMON_H */ #endif /* BH_COMMON_H */

View File

@@ -5,7 +5,7 @@
#include "common.h" #include "common.h"
typedef struct bh_hashmap_s bh_hashmap_t; typedef struct BH_Hashmap BH_Hashmap;
/** /**
@@ -17,8 +17,8 @@ typedef struct bh_hashmap_s bh_hashmap_t;
* \return On success, returns hashmap handle. * \return On success, returns hashmap handle.
* \return On failure, returns a null pointer. * \return On failure, returns a null pointer.
*/ */
bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal, BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal,
bh_hash_cb_t hash); BH_HashCallback hash);
/** /**
@@ -26,7 +26,7 @@ bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal,
* *
* \param hashmap Hashmap handle * \param hashmap Hashmap handle
*/ */
void bh_hashmap_free(bh_hashmap_t *hashmap); void BH_HashmapFree(BH_Hashmap *hashmap);
/** /**
@@ -34,7 +34,7 @@ void bh_hashmap_free(bh_hashmap_t *hashmap);
* *
* \param hashmap Hashmap handle * \param hashmap Hashmap handle
*/ */
void bh_hashmap_clear(bh_hashmap_t *hashmap); void BH_HashmapClear(BH_Hashmap *hashmap);
/** /**
@@ -52,7 +52,7 @@ void bh_hashmap_clear(bh_hashmap_t *hashmap);
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_hashmap_reserve(bh_hashmap_t *hashmap, int BH_HashmapReserve(BH_Hashmap *hashmap,
size_t size); size_t size);
@@ -69,7 +69,7 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_hashmap_insert(bh_hashmap_t *hashmap, int BH_HashmapInsert(BH_Hashmap *hashmap,
void *key, void *key,
void *value); void *value);
@@ -84,7 +84,7 @@ int bh_hashmap_insert(bh_hashmap_t *hashmap,
* \note If hashmap contains several elements with the same key, this function * \note If hashmap contains several elements with the same key, this function
* will remove only one key-value pair. * will remove only one key-value pair.
*/ */
void bh_hashmap_remove(bh_hashmap_t *hashmap, void BH_HashmapRemove(BH_Hashmap *hashmap,
void *key); void *key);
@@ -98,7 +98,7 @@ void bh_hashmap_remove(bh_hashmap_t *hashmap,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_hashmap_at(bh_hashmap_t *hashmap, int BH_HashmapAt(BH_Hashmap *hashmap,
void *key, void *key,
void **value); void **value);
@@ -111,7 +111,7 @@ int bh_hashmap_at(bh_hashmap_t *hashmap,
* \return If hashmap is empty, returns non-zero value * \return If hashmap is empty, returns non-zero value
* \return If hashmap is not empty, returns zero value * \return If hashmap is not empty, returns zero value
*/ */
int bh_hashmap_empty(bh_hashmap_t *hashmap); int BH_HashmapEmpty(BH_Hashmap *hashmap);
/** /**
@@ -121,7 +121,7 @@ int bh_hashmap_empty(bh_hashmap_t *hashmap);
* *
* \return Returns the size of the hashmap. * \return Returns the size of the hashmap.
*/ */
size_t bh_hashmap_size(bh_hashmap_t *hashmap); size_t BH_HashmapSize(BH_Hashmap *hashmap);
/** /**
@@ -131,7 +131,7 @@ size_t bh_hashmap_size(bh_hashmap_t *hashmap);
* *
* \return Returns the capacity of the hashmap. * \return Returns the capacity of the hashmap.
*/ */
size_t bh_hashmap_capacity(bh_hashmap_t *hashmap); size_t BH_HashmapCapacity(BH_Hashmap *hashmap);
/** /**
@@ -141,7 +141,7 @@ size_t bh_hashmap_capacity(bh_hashmap_t *hashmap);
* *
* \return Returns the load factor of the hashmap. * \return Returns the load factor of the hashmap.
*/ */
float bh_hashmap_factor(bh_hashmap_t *hashmap); float BH_HashmapFactor(BH_Hashmap *hashmap);
/** /**
@@ -152,7 +152,7 @@ float bh_hashmap_factor(bh_hashmap_t *hashmap);
* *
* \note New load factor will be applied on the next reserve/insert operation. * \note New load factor will be applied on the next reserve/insert operation.
*/ */
void bh_hashmap_set_factor(bh_hashmap_t *hashmap, void BH_HashmapSetFactor(BH_Hashmap *hashmap,
float factor); float factor);
@@ -168,7 +168,7 @@ void bh_hashmap_set_factor(bh_hashmap_t *hashmap,
* \note If hashmap contains several elements with the same key, this function * \note If hashmap contains several elements with the same key, this function
* will return iterator to one of them * will return iterator to one of them
*/ */
void *bh_hashmap_iter_at(bh_hashmap_t *hashmap, void *BH_HashmapIterAt(BH_Hashmap *hashmap,
void *key); void *key);
@@ -184,7 +184,7 @@ void *bh_hashmap_iter_at(bh_hashmap_t *hashmap,
* \return On success, returns new iterator value for the next element. * \return On success, returns new iterator value for the next element.
* \return On failure, returns NULL pointer. * \return On failure, returns NULL pointer.
*/ */
void *bh_hashmap_iter_next(bh_hashmap_t *hashmap, void *BH_HashmapIterNext(BH_Hashmap *hashmap,
void *iter); void *iter);
@@ -196,7 +196,7 @@ void *bh_hashmap_iter_next(bh_hashmap_t *hashmap,
* *
* \note Calling this function will invalidate iterators. * \note Calling this function will invalidate iterators.
*/ */
void bh_hashmap_iter_remove(bh_hashmap_t *hashmap, void BH_HashmapIterRemove(BH_Hashmap *hashmap,
void *iter); void *iter);
@@ -207,7 +207,7 @@ void bh_hashmap_iter_remove(bh_hashmap_t *hashmap,
* *
* \return Returns key. * \return Returns key.
*/ */
void *bh_hashmap_iter_key(void *iter); void *BH_HashmapIterKey(void *iter);
/** /**
@@ -217,7 +217,7 @@ void *bh_hashmap_iter_key(void *iter);
* *
* \return Returns value. * \return Returns value.
*/ */
void *bh_hashmap_iter_value(void *iter); void *BH_HashmapIterValue(void *iter);
#endif /* BH_HASHMAP_H */ #endif /* BH_HASHMAP_H */

View File

@@ -40,10 +40,11 @@
#define BH_IO_FLAG_EOF 0x0002 #define BH_IO_FLAG_EOF 0x0002
#define BH_IO_FLAG_OPEN 0x0004 #define BH_IO_FLAG_OPEN 0x0004
#define BH_FILE_CLASSNAME "bh_file" #define BH_FILE_CLASSNAME "BH_File"
typedef struct bh_io_s bh_io_t;
typedef int (*bh_io_func_t)(void *, int, void *, void *); typedef struct BH_IO BH_IO;
typedef int (*BH_IOCallback)(void *, int ,void *, void *);
/** /**
@@ -54,7 +55,7 @@ typedef int (*bh_io_func_t)(void *, int, void *, void *);
* \return On success, returns IO handle. * \return On success, returns IO handle.
* \return On failure, returns NULL pointer. * \return On failure, returns NULL pointer.
*/ */
bh_io_t *bh_file_new(const char *path); BH_IO *BH_FileNew(const char *path);
/** /**
@@ -65,7 +66,7 @@ bh_io_t *bh_file_new(const char *path);
* \return On success, returns IO handle. * \return On success, returns IO handle.
* \return On failure, returns NULL pointer. * \return On failure, returns NULL pointer.
*/ */
bh_io_t *bh_buffer_new(bh_io_t *io); BH_IO *BH_BufferNew(BH_IO *io);
/** /**
@@ -77,7 +78,7 @@ bh_io_t *bh_buffer_new(bh_io_t *io);
* \return On success, returns IO handle. * \return On success, returns IO handle.
* \return On failure, returns NULL pointer. * \return On failure, returns NULL pointer.
*/ */
bh_io_t *bh_io_new(bh_io_func_t func, BH_IO *BH_IONew(BH_IOCallback cb,
void *data); void *data);
@@ -86,7 +87,7 @@ bh_io_t *bh_io_new(bh_io_func_t func,
* *
* \param io IO handle * \param io IO handle
*/ */
void bh_io_free(bh_io_t *io); void BH_IOFree(BH_IO *io);
/** /**
@@ -97,7 +98,7 @@ void bh_io_free(bh_io_t *io);
* \return On success, returns pointer to constant string. * \return On success, returns pointer to constant string.
* \return On failure, returns NULL pointer * \return On failure, returns NULL pointer
*/ */
const char *bh_io_classname(bh_io_t *io); const char *BH_IOClassname(BH_IO* io);
/** /**
@@ -109,7 +110,7 @@ const char *bh_io_classname(bh_io_t *io);
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_open(bh_io_t *io, int BH_IOOpen(BH_IO *io,
int mode); int mode);
@@ -121,7 +122,7 @@ int bh_io_open(bh_io_t *io,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_close(bh_io_t *io); int BH_IOClose(BH_IO *io);
/** /**
@@ -135,7 +136,7 @@ int bh_io_close(bh_io_t *io);
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_read(bh_io_t *io, int BH_IORead(BH_IO *io,
char *buffer, char *buffer,
size_t size, size_t size,
size_t *actual); size_t *actual);
@@ -152,7 +153,7 @@ int bh_io_read(bh_io_t *io,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_write(bh_io_t *io, int BH_IOWrite(BH_IO *io,
const char *buffer, const char *buffer,
size_t size, size_t size,
size_t *actual); size_t *actual);
@@ -169,7 +170,7 @@ int bh_io_write(bh_io_t *io,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_peek(bh_io_t *io, int BH_IOPeek(BH_IO *io,
char *buffer, char *buffer,
size_t size, size_t size,
size_t *actual); size_t *actual);
@@ -184,7 +185,7 @@ int bh_io_peek(bh_io_t *io,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_tell(bh_io_t *io, int BH_IOTell(BH_IO *io,
int64_t *position); int64_t *position);
@@ -198,7 +199,7 @@ int bh_io_tell(bh_io_t *io,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_seek(bh_io_t *io, int BH_IOSeek(BH_IO *io,
int64_t position, int64_t position,
int direction); int direction);
@@ -211,7 +212,7 @@ int bh_io_seek(bh_io_t *io,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_flush(bh_io_t *io); int BH_IOFlush(BH_IO *io);
/** /**
@@ -223,7 +224,7 @@ int bh_io_flush(bh_io_t *io);
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_size(bh_io_t *io, int BH_IOSize(BH_IO *io,
int64_t *size); int64_t *size);
@@ -234,7 +235,7 @@ int bh_io_size(bh_io_t *io,
* *
* \return Flags of the IO * \return Flags of the IO
*/ */
int bh_io_flags(bh_io_t *io); int BH_IOFlags(BH_IO *io);
/** /**
@@ -245,7 +246,7 @@ int bh_io_flags(bh_io_t *io);
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_io_clear(bh_io_t *io); int BH_IOClear(BH_IO *io);
#endif /* BH_IO_H */ #endif /* BH_IO_H */

File diff suppressed because it is too large Load Diff

View File

@@ -5,7 +5,7 @@
#include "common.h" #include "common.h"
typedef struct bh_queue_s bh_queue_t; typedef struct BH_Queue BH_Queue;
/** /**
@@ -14,7 +14,7 @@ typedef struct bh_queue_s bh_queue_t;
* \return On success, returns the pointer to the new queue object. * \return On success, returns the pointer to the new queue object.
* \return On failure, returns a null pointer. * \return On failure, returns a null pointer.
*/ */
bh_queue_t *bh_queue_new(void); BH_Queue *BH_QueueNew(void);
/** /**
@@ -22,7 +22,7 @@ bh_queue_t *bh_queue_new(void);
* *
* \param queue Pointer to the queue object to be freed * \param queue Pointer to the queue object to be freed
*/ */
void bh_queue_free(bh_queue_t *queue); void BH_QueueFree(BH_Queue *queue);
/** /**
@@ -30,7 +30,7 @@ void bh_queue_free(bh_queue_t *queue);
* *
* \param queue Pointer to the queue object to be cleared * \param queue Pointer to the queue object to be cleared
*/ */
void bh_queue_clear(bh_queue_t *queue); void BH_QueueClear(BH_Queue *queue);
/** /**
@@ -47,7 +47,7 @@ void bh_queue_clear(bh_queue_t *queue);
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_queue_reserve(bh_queue_t *queue, int BH_QueueReserve(BH_Queue *queue,
size_t size); size_t size);
@@ -62,7 +62,7 @@ int bh_queue_reserve(bh_queue_t *queue,
* \return On success, returns zero value. * \return On success, returns zero value.
* \return On failure, returns error code. * \return On failure, returns error code.
*/ */
int bh_queue_insert(bh_queue_t *queue, int BH_QueueInsert(BH_Queue *queue,
void *value); void *value);
@@ -73,7 +73,7 @@ int bh_queue_insert(bh_queue_t *queue,
* *
* \note Calling this function will invalidate iterators. * \note Calling this function will invalidate iterators.
*/ */
void bh_queue_remove(bh_queue_t *queue); void BH_QueueRemove(BH_Queue *queue);
/** /**
@@ -84,7 +84,7 @@ void bh_queue_remove(bh_queue_t *queue);
* \return On success, returns front value from the queue. * \return On success, returns front value from the queue.
* \return On failure, returns null pointer. * \return On failure, returns null pointer.
*/ */
int bh_queue_front(bh_queue_t *queue, int BH_QueueFront(BH_Queue *queue,
void **value); void **value);
@@ -96,7 +96,7 @@ int bh_queue_front(bh_queue_t *queue,
* \return If queue is empty, returns non-zero value * \return If queue is empty, returns non-zero value
* \return If queue is not empty, returns zero value * \return If queue is not empty, returns zero value
*/ */
int bh_queue_empty(bh_queue_t *queue); int BH_QueueEmpty(BH_Queue *queue);
/** /**
@@ -106,7 +106,7 @@ int bh_queue_empty(bh_queue_t *queue);
* *
* \return Returns the size of the queue. * \return Returns the size of the queue.
*/ */
size_t bh_queue_size(bh_queue_t *queue); size_t BH_QueueSize(BH_Queue *queue);
/** /**
@@ -116,7 +116,7 @@ size_t bh_queue_size(bh_queue_t *queue);
* *
* \return Returns the capacity of the queue. * \return Returns the capacity of the queue.
*/ */
size_t bh_queue_capacity(bh_queue_t *queue); size_t BH_QueueCapacity(BH_Queue *queue);
/** /**
@@ -132,7 +132,7 @@ size_t bh_queue_capacity(bh_queue_t *queue);
* \return If the \a iter is the null pointer, returns iterator to the * \return If the \a iter is the null pointer, returns iterator to the
* first element of the queue. * first element of the queue.
*/ */
void *bh_queue_iter_next(bh_queue_t *queue, void *BH_QueueIterNext(BH_Queue *queue,
void *iter); void *iter);
@@ -143,7 +143,7 @@ void *bh_queue_iter_next(bh_queue_t *queue,
* *
* \return Returns value, pointed by iterator. * \return Returns value, pointed by iterator.
*/ */
void *bh_queue_iter_value(void *iter); void *BH_QueueIterValue(void *iter);
#endif /* BH_QUEUE_H */ #endif /* BH_QUEUE_H */

9
main.c
View File

@@ -2,9 +2,10 @@
int main() int main()
{ {
bh_io_t *io = bh_file_new("hello.txt"); BH_IO *io = BH_FileNew("hello.txt");
bh_io_open(io, BH_IO_WRITE); BH_IOOpen(io, BH_IO_WRITE);
bh_io_write(io, "Hello, world!", 13, NULL); BH_IOWrite(io, "Hello, world!", 13, NULL);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }

View File

@@ -2,7 +2,7 @@
#include <string.h> #include <string.h>
void bh_swap(void *dest, void BH_Swap(void *dest,
void *src, void *src,
size_t size) size_t size)
{ {
@@ -30,11 +30,11 @@ void bh_swap(void *dest,
} }
void *bh_partition(void *pivot, void *BH_Partition(void *pivot,
void *array, void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
char *start, *end, *i, *j; char *start, *end, *i, *j;
@@ -66,7 +66,7 @@ void *bh_partition(void *pivot,
pivot = i; pivot = i;
/* Swap elements and continue */ /* Swap elements and continue */
bh_swap(i, j, element); BH_Swap(i, j, element);
i += element; i += element;
j -= element; j -= element;
} }
@@ -77,10 +77,10 @@ void *bh_partition(void *pivot,
#if 0 #if 0
static void bh_sort_insert(void *array, static void BH_SortInsert(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
size_t i, j; size_t i, j;
@@ -95,7 +95,7 @@ static void bh_sort_insert(void *array,
rhs = (char *)array + (j - 1) * element; rhs = (char *)array + (j - 1) * element;
if (equal(lhs, rhs) < 0) if (equal(lhs, rhs) < 0)
bh_swap(lhs, rhs, element); BH_Swap(lhs, rhs, element);
else else
break; break;
} }
@@ -105,10 +105,10 @@ static void bh_sort_insert(void *array,
#endif #endif
static void bh_sort_shell(void *array, static void BH_SortShell(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
static const size_t gaps[10] = {1750, 701, 301, 132, 57, 23, 10, 4, 1, 0}; static const size_t gaps[10] = {1750, 701, 301, 132, 57, 23, 10, 4, 1, 0};
const size_t *gap; const size_t *gap;
@@ -127,7 +127,7 @@ static void bh_sort_shell(void *array,
rhs = (char *)array + (j - *gap) * element; rhs = (char *)array + (j - *gap) * element;
if (equal(lhs, rhs) < 0) if (equal(lhs, rhs) < 0)
bh_swap(lhs, rhs, element); BH_Swap(lhs, rhs, element);
else else
break; break;
} }
@@ -136,23 +136,23 @@ static void bh_sort_shell(void *array,
} }
static void bh_sort_heap(void *array, static void BH_SortHeap(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
size_t i; size_t i;
bh_heap_make(array, size, element, equal); BH_HeapMake(array, size, element, equal);
for (i = size; i > 0; i--) for (i = size; i > 0; i--)
bh_heap_remove(array, i, element, equal); BH_HeapRemove(array, i, element, equal);
} }
static void bh_sort_intro_r(void *array, static void BH_SortIntroR(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal, BH_EqualCallback equal,
size_t depth) size_t depth)
{ {
/* Introsort (with manual tail call optimization) */ /* Introsort (with manual tail call optimization) */
@@ -163,13 +163,13 @@ static void bh_sort_intro_r(void *array,
if (size < 16) if (size < 16)
{ {
/* There are less then 16 elements left - use Shell/Insert sort */ /* There are less then 16 elements left - use Shell/Insert sort */
bh_sort_shell(array, size, element, equal); BH_SortShell(array, size, element, equal);
return; return;
} }
else if (!depth) else if (!depth)
{ {
/* Max depth reached - use heap sort */ /* Max depth reached - use heap sort */
bh_sort_heap(array, size, element, equal); BH_SortHeap(array, size, element, equal);
return; return;
} }
@@ -199,10 +199,10 @@ static void bh_sort_intro_r(void *array,
} }
/* Partition the array */ /* Partition the array */
middle = bh_partition(pivot, array, size, element, equal); middle = BH_Partition(pivot, array, size, element, equal);
/* Recursive call into first half */ /* Recursive call into first half */
bh_sort_intro_r(array, (middle - start) / element, element, equal, depth - 1); BH_SortIntroR(array, (middle - start) / element, element, equal, depth - 1);
/* Setup array and size for the second half */ /* Setup array and size for the second half */
array = middle; array = middle;
@@ -212,10 +212,10 @@ static void bh_sort_intro_r(void *array,
} }
void bh_sort(void *array, void BH_Sort(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
size_t depth, depth_log; size_t depth, depth_log;
@@ -229,14 +229,14 @@ void bh_sort(void *array,
} }
/* Call main sorting function */ /* Call main sorting function */
bh_sort_intro_r(array, size, element, equal, depth); BH_SortIntroR(array, size, element, equal, depth);
} }
void bh_heap_make(void *array, void BH_HeapMake(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
char *start, *end; char *start, *end;
size_t i; size_t i;
@@ -268,7 +268,7 @@ void bh_heap_make(void *array,
if (equal(current, biggest) < 0) if (equal(current, biggest) < 0)
{ {
/* Swap content and recalculate children pointers */ /* Swap content and recalculate children pointers */
bh_swap(current, biggest, element); BH_Swap(current, biggest, element);
current = biggest; current = biggest;
left = start + (current - start) * 2 + element; left = start + (current - start) * 2 + element;
right = left + element; right = left + element;
@@ -280,10 +280,10 @@ void bh_heap_make(void *array,
} }
void bh_heap_remove(void *array, void BH_HeapRemove(void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
char *start, *end, *current, *left, *right; char *start, *end, *current, *left, *right;
@@ -298,7 +298,7 @@ void bh_heap_remove(void *array,
right = left + element; right = left + element;
/* Swap first and last element */ /* Swap first and last element */
bh_swap(current, end, element); BH_Swap(current, end, element);
/* Iterate until we reach the end */ /* Iterate until we reach the end */
while (left < end) while (left < end)
@@ -314,7 +314,7 @@ void bh_heap_remove(void *array,
if (equal(current, biggest) < 0) if (equal(current, biggest) < 0)
{ {
/* Swap content and recalculate children pointers */ /* Swap content and recalculate children pointers */
bh_swap(current, biggest, element); BH_Swap(current, biggest, element);
current = biggest; current = biggest;
left = start + (current - start) * 2 + element; left = start + (current - start) * 2 + element;
right = left + element; right = left + element;
@@ -325,11 +325,11 @@ void bh_heap_remove(void *array,
} }
void bh_heap_insert(void *value, void BH_HeapInsert(void *value,
void *array, void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
char *start, *end, *current; char *start, *end, *current;
@@ -353,7 +353,7 @@ void bh_heap_insert(void *value,
if (equal(parent, current) < 0) if (equal(parent, current) < 0)
{ {
/* Swap current and parent */ /* Swap current and parent */
bh_swap(parent, current, element); BH_Swap(parent, current, element);
current = parent; current = parent;
} }
else else
@@ -362,11 +362,11 @@ void bh_heap_insert(void *value,
} }
void bh_heap_replace(void *value, void BH_HeapReplace(void *value,
void *array, void *array,
size_t size, size_t size,
size_t element, size_t element,
bh_equal_cb_t equal) BH_EqualCallback equal)
{ {
char *start, *end, *current, *left, *right; char *start, *end, *current, *left, *right;
@@ -400,7 +400,7 @@ void bh_heap_replace(void *value,
if (equal(current, biggest) < 0) if (equal(current, biggest) < 0)
{ {
/* Swap content and recalculate children pointers */ /* Swap content and recalculate children pointers */
bh_swap(current, biggest, element); BH_Swap(current, biggest, element);
current = biggest; current = biggest;
left = start + (current - start) * 2 + element; left = start + (current - start) * 2 + element;
right = left + element; right = left + element;

View File

@@ -1,54 +1,69 @@
#include <bh/io.h> #include <bh/io.h>
typedef struct bh_file_s typedef struct BH_File
{ {
int implement; int implement;
int me; int me;
} bh_file_t; } BH_File;
static int file_info(bh_file_t *file,
static int BH_FileInfo(BH_File *file,
size_t *size, size_t *size,
const char **name); const char **name);
static int file_init(bh_file_t *file,
static int BH_FileInit(BH_File *file,
const char *path); const char *path);
static int file_destroy(bh_file_t *file);
static int file_open(bh_file_t *file, static int BH_FileDestroy(BH_File *file);
static int BH_FileOpen(BH_File *file,
int *mode); int *mode);
static int file_close(bh_file_t *file);
static int file_read(bh_file_t *file, static int BH_FileClose(BH_File *file);
static int BH_FileRead(BH_File *file,
char *data, char *data,
size_t *size); size_t *size);
static int file_write(bh_file_t *file,
static int BH_FileWrite(BH_File *file,
const char *data, const char *data,
size_t *size); size_t *size);
static int file_peek(bh_file_t* file,
static int BH_FilePeek(BH_File* file,
char *data, char *data,
size_t *size); size_t *size);
static int file_flush(bh_file_t *file);
static int file_seek(bh_file_t *file, static int BH_FileFlush(BH_File *file);
static int BH_FileSeek(BH_File *file,
int64_t *pos, int64_t *pos,
int *dir); int *dir);
static int file_tell(bh_file_t *file,
static int BH_FileTell(BH_File *file,
int64_t *pos); int64_t *pos);
static int file_size(bh_file_t *file,
static int BH_FileSize(BH_File *file,
int64_t *size); int64_t *size);
static int file_flags(bh_file_t *file);
static int file_clear(bh_file_t *file); static int BH_FileFlags(BH_File *file);
static int file_info(bh_file_t *file,
static int BH_FileClear(BH_File *file);
static int BH_FileInfo(BH_File *file,
size_t *size, size_t *size,
const char **name) const char **name)
{ {
@@ -62,109 +77,124 @@ static int file_info(bh_file_t *file,
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_init(bh_file_t *file,
static int BH_FileInit(BH_File *file,
const char *path) const char *path)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_destroy(bh_file_t *file)
static int BH_FileDestroy(BH_File *file)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_open(bh_file_t *file,
static int BH_FileOpen(BH_File *file,
int *mode) int *mode)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_close(bh_file_t *file)
static int BH_FileClose(BH_File *file)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_read(bh_file_t *file,
static int BH_FileRead(BH_File *file,
char *data, char *data,
size_t *size) size_t *size)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_write(bh_file_t *file,
static int BH_FileWrite(BH_File *file,
const char *data, const char *data,
size_t *size) size_t *size)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_peek(bh_file_t *file,
static int BH_FilePeek(BH_File *file,
char *data, char *data,
size_t *size) size_t *size)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_flush(bh_file_t *file)
static int BH_FileFlush(BH_File *file)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_seek(bh_file_t *file,
static int BH_FileSeek(BH_File *file,
int64_t *pos, int64_t *pos,
int *dir) int *dir)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_tell(bh_file_t *file,
static int BH_FileTell(BH_File *file,
int64_t *pos) int64_t *pos)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_size(bh_file_t *file,
static int BH_FileSize(BH_File *file,
int64_t *size) int64_t *size)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_flags(bh_file_t *file)
static int BH_FileFlags(BH_File *file)
{ {
return BH_IO_FLAG_ERROR; return BH_IO_FLAG_ERROR;
} }
static int file_clear(bh_file_t *file)
static int BH_FileClear(BH_File *file)
{ {
return BH_NOIMPL; return BH_NOIMPL;
} }
static int file_proc(bh_file_t *file,
static int BH_FileCallback(BH_File *file,
int type, int type,
void *arg1, void *arg1,
void *arg2) void *arg2)
{ {
switch (type) switch (type)
{ {
case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2); case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2);
case BH_IO_INIT_CB: return file_init(file, (const char *)arg1); case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1);
case BH_IO_DESTROY_CB: return file_destroy(file); case BH_IO_DESTROY_CB: return BH_FileDestroy(file);
case BH_IO_OPEN_CB: return file_open(file, (int *)arg1); case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1);
case BH_IO_CLOSE_CB: return file_close(file); case BH_IO_CLOSE_CB: return BH_FileClose(file);
case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2); case BH_IO_READ_CB: return BH_FileRead(file, (char *)arg1, (size_t *)arg2);
case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2); case BH_IO_WRITE_CB: return BH_FileWrite(file, (const char *)arg1, (size_t *)arg2);
case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2); case BH_IO_PEEK_CB: return BH_FilePeek(file, (char *)arg1, (size_t *)arg2);
case BH_IO_FLUSH_CB: return file_flush(file); case BH_IO_FLUSH_CB: return BH_FileFlush(file);
case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2); case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2);
case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1); case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1);
case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1); case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1);
case BH_IO_FLAGS_CB: return file_flags(file); case BH_IO_FLAGS_CB: return BH_FileFlags(file);
case BH_IO_CLEAR_CB: return file_clear(file); case BH_IO_CLEAR_CB: return BH_FileClear(file);
default: return BH_NOIMPL; default: return BH_NOIMPL;
} }
} }
bh_io_t *bh_file_new(const char *path)
BH_IO *BH_FileNew(const char *path)
{ {
return bh_io_new((bh_io_func_t)file_proc, (void *)path); return BH_IONew((BH_IOCallback)BH_FileCallback, (void *)path);
} }

View File

@@ -3,29 +3,29 @@
#include <string.h> #include <string.h>
typedef struct bh_hashmap_node_s typedef struct BH_HashmapNode
{ {
void *key; void *key;
void *value; void *value;
} bh_hashmap_node_t; } BH_HashmapNode;
struct bh_hashmap_s struct BH_Hashmap
{ {
bh_hashmap_node_t *data; BH_HashmapNode *data;
size_t *psls; size_t *psls;
size_t size; size_t size;
size_t capacity; size_t capacity;
size_t threshold; size_t threshold;
bh_equal_cb_t equal; BH_EqualCallback equal;
bh_hash_cb_t hash; BH_HashCallback hash;
float factor; float factor;
}; };
static void bh_hashmap_init(bh_hashmap_t *hashmap, static void BH_HashmapInit(BH_Hashmap *hashmap,
bh_equal_cb_t equal, BH_EqualCallback equal,
bh_hash_cb_t hash) BH_HashCallback hash)
{ {
memset(hashmap, 0, sizeof(*hashmap)); memset(hashmap, 0, sizeof(*hashmap));
hashmap->factor = 0.75f; hashmap->factor = 0.75f;
@@ -34,7 +34,7 @@ static void bh_hashmap_init(bh_hashmap_t *hashmap,
} }
static void bh_hashmap_destroy(bh_hashmap_t *hashmap) static void BH_HashmapDestroy(BH_Hashmap *hashmap)
{ {
if (hashmap->capacity) if (hashmap->capacity)
{ {
@@ -44,7 +44,7 @@ static void bh_hashmap_destroy(bh_hashmap_t *hashmap)
} }
static int calc_capacity(size_t size, static int BH_CalcCapacity(size_t size,
float factor, float factor,
size_t *capacity, size_t *capacity,
size_t *threshold) size_t *threshold)
@@ -71,54 +71,54 @@ static int calc_capacity(size_t size,
} }
/* Catch malloc overflow */ /* Catch malloc overflow */
if (*capacity >= ((size_t)-1) / sizeof(bh_hashmap_node_t)) if (*capacity >= ((size_t)-1) / sizeof(BH_HashmapNode))
return BH_OOM; return BH_OOM;
return BH_OK; return BH_OK;
} }
static void copy_hashmap(bh_hashmap_t *dest, static void BH_CopyHashmap(BH_Hashmap *dest,
bh_hashmap_t *src) BH_Hashmap *src)
{ {
void *iter; void *iter;
/* Iterate and insert data into hashmap */ /* Iterate and insert data into hashmap */
iter = bh_hashmap_iter_next(src, NULL); iter = BH_HashmapIterNext(src, NULL);
while (iter) while (iter)
{ {
void *key, *value; void *key, *value;
key = bh_hashmap_iter_key(iter); key = BH_HashmapIterKey(iter);
value = bh_hashmap_iter_value(iter); value = BH_HashmapIterValue(iter);
bh_hashmap_insert(dest, key, value); BH_HashmapInsert(dest, key, value);
iter = bh_hashmap_iter_next(src, iter); iter = BH_HashmapIterNext(src, iter);
} }
} }
bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal, BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal,
bh_hash_cb_t hash) BH_HashCallback hash)
{ {
bh_hashmap_t *result; BH_Hashmap *result;
result = malloc(sizeof(*result)); result = malloc(sizeof(*result));
if (result) if (result)
bh_hashmap_init(result, equal, hash); BH_HashmapInit(result, equal, hash);
return result; return result;
} }
void bh_hashmap_free(bh_hashmap_t *hashmap) void BH_HashmapFree(BH_Hashmap *hashmap)
{ {
bh_hashmap_destroy(hashmap); BH_HashmapDestroy(hashmap);
free(hashmap); free(hashmap);
} }
void bh_hashmap_clear(bh_hashmap_t *hashmap) void BH_HashmapClear(BH_Hashmap *hashmap)
{ {
if (hashmap->capacity) if (hashmap->capacity)
memset(hashmap->psls, 0, hashmap->capacity * sizeof(size_t)); memset(hashmap->psls, 0, hashmap->capacity * sizeof(size_t));
@@ -126,10 +126,10 @@ void bh_hashmap_clear(bh_hashmap_t *hashmap)
} }
int bh_hashmap_reserve(bh_hashmap_t *hashmap, int BH_HashmapReserve(BH_Hashmap *hashmap,
size_t size) size_t size)
{ {
bh_hashmap_t other; BH_Hashmap other;
size_t capacity, threshold; size_t capacity, threshold;
/* New capacity can't be smaller then current hashmap size */ /* New capacity can't be smaller then current hashmap size */
@@ -137,7 +137,7 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap,
size = hashmap->size; size = hashmap->size;
/* Calculate new capacity */ /* Calculate new capacity */
if (calc_capacity(size, hashmap->factor, &capacity, &threshold)) if (BH_CalcCapacity(size, hashmap->factor, &capacity, &threshold))
return BH_OOM; return BH_OOM;
/* Prevent same size reallocation */ /* Prevent same size reallocation */
@@ -145,7 +145,7 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap,
return BH_OK; return BH_OK;
/* Initialize new hashmap */ /* Initialize new hashmap */
bh_hashmap_init(&other, hashmap->equal, hashmap->hash); BH_HashmapInit(&other, hashmap->equal, hashmap->hash);
other.factor = hashmap->factor; other.factor = hashmap->factor;
if (capacity) if (capacity)
@@ -170,26 +170,26 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap,
memset(other.psls, 0, sizeof(size_t) * other.capacity); memset(other.psls, 0, sizeof(size_t) * other.capacity);
/* Copy data from old hashmap to the new hashmap */ /* Copy data from old hashmap to the new hashmap */
copy_hashmap(&other, hashmap); BH_CopyHashmap(&other, hashmap);
} }
/* Swap hashmaps */ /* Swap hashmaps */
bh_hashmap_destroy(hashmap); BH_HashmapDestroy(hashmap);
*hashmap = other; *hashmap = other;
return BH_OK; return BH_OK;
} }
int bh_hashmap_insert(bh_hashmap_t *hashmap, int BH_HashmapInsert(BH_Hashmap *hashmap,
void *key, void *key,
void *value) void *value)
{ {
size_t bucket, psl, tmp_psl; size_t bucket, psl, tmp_psl;
bh_hashmap_node_t item, tmp; BH_HashmapNode item, tmp;
/* Try to stay below hashmap threshold */ /* Try to stay below hashmap threshold */
if (hashmap->size + 1 > hashmap->threshold) if (hashmap->size + 1 > hashmap->threshold)
if (bh_hashmap_reserve(hashmap, hashmap->size + 1)) if (BH_HashmapReserve(hashmap, hashmap->size + 1))
if (hashmap->size >= hashmap->capacity) if (hashmap->size >= hashmap->capacity)
return BH_OOM; return BH_OOM;
@@ -228,59 +228,59 @@ int bh_hashmap_insert(bh_hashmap_t *hashmap,
} }
void bh_hashmap_remove(bh_hashmap_t *hashmap, void BH_HashmapRemove(BH_Hashmap *hashmap,
void *key) void *key)
{ {
void *iter; void *iter;
iter = bh_hashmap_iter_at(hashmap, key); iter = BH_HashmapIterAt(hashmap, key);
if (iter) if (iter)
bh_hashmap_iter_remove(hashmap, iter); BH_HashmapIterRemove(hashmap, iter);
} }
int bh_hashmap_at(bh_hashmap_t *hashmap, int BH_HashmapAt(BH_Hashmap *hashmap,
void *key, void *key,
void **value) void **value)
{ {
void *iter; void *iter;
iter = bh_hashmap_iter_at(hashmap, key); iter = BH_HashmapIterAt(hashmap, key);
if (!iter) if (!iter)
return BH_NOTFOUND; return BH_NOTFOUND;
if (value) if (value)
*value = bh_hashmap_iter_value(iter); *value = BH_HashmapIterValue(iter);
return BH_OK; return BH_OK;
} }
int bh_hashmap_empty(bh_hashmap_t *hashmap) int BH_HashmapEmpty(BH_Hashmap *hashmap)
{ {
return !hashmap->size; return !hashmap->size;
} }
size_t bh_hashmap_size(bh_hashmap_t *hashmap) size_t BH_HashmapSize(BH_Hashmap *hashmap)
{ {
return hashmap->size; return hashmap->size;
} }
size_t bh_hashmap_capacity(bh_hashmap_t *hashmap) size_t BH_HashmapCapacity(BH_Hashmap *hashmap)
{ {
return hashmap->capacity; return hashmap->capacity;
} }
float bh_hashmap_factor(bh_hashmap_t *hashmap) float BH_HashmapFactor(BH_Hashmap *hashmap)
{ {
return hashmap->factor; return hashmap->factor;
} }
void bh_hashmap_set_factor(bh_hashmap_t *hashmap, void BH_HashmapSetFactor(BH_Hashmap *hashmap,
float factor) float factor)
{ {
/* Limit the factor value to [0.15, 1.0] */ /* Limit the factor value to [0.15, 1.0] */
@@ -293,7 +293,7 @@ void bh_hashmap_set_factor(bh_hashmap_t *hashmap,
} }
void *bh_hashmap_iter_at(bh_hashmap_t *hashmap, void *BH_HashmapIterAt(BH_Hashmap *hashmap,
void *key) void *key)
{ {
size_t bucket, psl; size_t bucket, psl;
@@ -321,12 +321,12 @@ void *bh_hashmap_iter_at(bh_hashmap_t *hashmap,
} }
void *bh_hashmap_iter_next(bh_hashmap_t *hashmap, void *BH_HashmapIterNext(BH_Hashmap *hashmap,
void *iter) void *iter)
{ {
bh_hashmap_node_t *item; BH_HashmapNode *item;
item = (bh_hashmap_node_t *)iter; item = (BH_HashmapNode *)iter;
while (1) while (1)
{ {
/* Advance or set iterator to the first element */ /* Advance or set iterator to the first element */
@@ -346,7 +346,7 @@ void *bh_hashmap_iter_next(bh_hashmap_t *hashmap,
} }
void bh_hashmap_iter_remove(bh_hashmap_t *hashmap, void BH_HashmapIterRemove(BH_Hashmap *hashmap,
void *iter) void *iter)
{ {
size_t bucket, next_bucket; size_t bucket, next_bucket;
@@ -357,7 +357,7 @@ void bh_hashmap_iter_remove(bh_hashmap_t *hashmap,
/* Adjust hashmap size, calculate current and next bucket index */ /* Adjust hashmap size, calculate current and next bucket index */
hashmap->size--; hashmap->size--;
bucket = (bh_hashmap_node_t *)iter - hashmap->data; bucket = (BH_HashmapNode *)iter - hashmap->data;
next_bucket = (bucket + 1) & (hashmap->capacity - 1); next_bucket = (bucket + 1) & (hashmap->capacity - 1);
/* Shift all elements toward their preffered place */ /* Shift all elements toward their preffered place */
@@ -377,14 +377,14 @@ void bh_hashmap_iter_remove(bh_hashmap_t *hashmap,
} }
void *bh_hashmap_iter_key(void *iter) void *BH_HashmapIterKey(void *iter)
{ {
return ((bh_hashmap_node_t *)iter)->key; return ((BH_HashmapNode *)iter)->key;
} }
void *bh_hashmap_iter_value(void *iter) void *BH_HashmapIterValue(void *iter)
{ {
return ((bh_hashmap_node_t *)iter)->value; return ((BH_HashmapNode *)iter)->value;
} }

View File

@@ -4,21 +4,20 @@
#define BUFFER_SIZE (sizeof(char *)) #define BUFFER_SIZE (sizeof(char *))
struct BH_IO
struct bh_io_s
{ {
bh_io_func_t func; BH_IOCallback cb;
}; };
bh_io_t *bh_io_new(bh_io_func_t func, BH_IO *BH_IONew(BH_IOCallback cb,
void *data) void *data)
{ {
size_t requested; size_t requested;
bh_io_t *io; BH_IO *io;
/* Get information about IO device size */ /* Get information about IO device size */
if (func(NULL, BH_IO_INFO_CB, &requested, NULL)) if (cb(NULL, BH_IO_INFO_CB, &requested, NULL))
return NULL; return NULL;
/* Allocate space for the IO device */ /* Allocate space for the IO device */
@@ -27,8 +26,8 @@ bh_io_t *bh_io_new(bh_io_func_t func,
return NULL; return NULL;
/* Initialize IO device */ /* Initialize IO device */
io->func = func; io->cb = cb;
if (func(io + 1, BH_IO_INIT_CB, data, NULL)) if (cb(io + 1, BH_IO_INIT_CB, data, NULL))
{ {
free(io); free(io);
return NULL; return NULL;
@@ -38,28 +37,28 @@ bh_io_t *bh_io_new(bh_io_func_t func,
} }
void bh_io_free(bh_io_t *io) void BH_IOFree(BH_IO *io)
{ {
/* Prevent working with NULL io */ /* Prevent working with NULL io */
if (!io) if (!io)
return; return;
/* Call the IO device destruction handler */ /* Call the IO device destruction handler */
io->func(io + 1, BH_IO_DESTROY_CB, NULL, NULL); io->cb(io + 1, BH_IO_DESTROY_CB, NULL, NULL);
/* Deallocate object */ /* Deallocate object */
free(io); free(io);
} }
const char *bh_io_classname(bh_io_t *io) const char *BH_IOClassname(BH_IO *io)
{ {
const char *name; const char *name;
if (!io) if (!io)
goto error; goto error;
if (io->func(io + 1, BH_IO_INFO_CB, NULL, &name) != BH_OK) if (io->cb(io + 1, BH_IO_INFO_CB, NULL, &name) != BH_OK)
goto error; goto error;
return name; return name;
@@ -69,7 +68,7 @@ error:
} }
int bh_io_open(bh_io_t *io, int BH_IOOpen(BH_IO *io,
int mode) int mode)
{ {
/* Prevent working with NULL io */ /* Prevent working with NULL io */
@@ -77,22 +76,22 @@ int bh_io_open(bh_io_t *io,
return BH_ERROR; return BH_ERROR;
/* Call the IO device open handler with specified mode */ /* Call the IO device open handler with specified mode */
return io->func(io + 1, BH_IO_OPEN_CB, &mode, NULL); return io->cb(io + 1, BH_IO_OPEN_CB, &mode, NULL);
} }
int bh_io_close(bh_io_t *io) int BH_IOClose(BH_IO *io)
{ {
/* Prevent working with NULL io */ /* Prevent working with NULL io */
if (!io) if (!io)
return BH_ERROR; return BH_ERROR;
/* Call the IO device close handler */ /* Call the IO device close handler */
return io->func(io + 1, BH_IO_CLOSE_CB, NULL, NULL); return io->cb(io + 1, BH_IO_CLOSE_CB, NULL, NULL);
} }
int bh_io_read(bh_io_t *io, int BH_IORead(BH_IO *io,
char *buffer, char *buffer,
size_t size, size_t size,
size_t *actual) size_t *actual)
@@ -104,7 +103,7 @@ int bh_io_read(bh_io_t *io,
return BH_ERROR; return BH_ERROR;
/* Call the IO device read handler */ /* Call the IO device read handler */
code = io->func(io + 1, BH_IO_READ_CB, buffer, &size); code = io->cb(io + 1, BH_IO_READ_CB, buffer, &size);
/* If caller wants to know actual read size - report it back */ /* If caller wants to know actual read size - report it back */
if (actual) if (actual)
@@ -114,7 +113,7 @@ int bh_io_read(bh_io_t *io,
} }
int bh_io_write(bh_io_t *io, int BH_IOWrite(BH_IO *io,
const char *buffer, const char *buffer,
size_t size, size_t size,
size_t *actual) size_t *actual)
@@ -126,7 +125,7 @@ int bh_io_write(bh_io_t *io,
return BH_ERROR; return BH_ERROR;
/* Call the IO device write handler */ /* Call the IO device write handler */
code = io->func(io + 1, BH_IO_WRITE_CB, (void *)buffer, &size); code = io->cb(io + 1, BH_IO_WRITE_CB, (void *)buffer, &size);
/* If caller wants to know actual written size - report it back */ /* If caller wants to know actual written size - report it back */
if (actual) if (actual)
@@ -135,7 +134,7 @@ int bh_io_write(bh_io_t *io,
return code; return code;
} }
int bh_io_peek(bh_io_t *io, int BH_IOPeek(BH_IO *io,
char *buffer, char *buffer,
size_t size, size_t size,
size_t *actual) size_t *actual)
@@ -147,7 +146,7 @@ int bh_io_peek(bh_io_t *io,
return BH_ERROR; return BH_ERROR;
/* Call the IO device peek handler */ /* Call the IO device peek handler */
code = io->func(io + 1, BH_IO_PEEK_CB, (void *)buffer, &size); code = io->cb(io + 1, BH_IO_PEEK_CB, (void *)buffer, &size);
/* If caller wants to know actual written size - report it back */ /* If caller wants to know actual written size - report it back */
if (actual) if (actual)
@@ -157,7 +156,7 @@ int bh_io_peek(bh_io_t *io,
} }
int bh_io_tell(bh_io_t *io, int BH_IOTell(BH_IO *io,
int64_t *position) int64_t *position)
{ {
/* Prevent working with NULL io */ /* Prevent working with NULL io */
@@ -165,11 +164,11 @@ int bh_io_tell(bh_io_t *io,
return BH_ERROR; return BH_ERROR;
/* Call the IO device tell handler */ /* Call the IO device tell handler */
return io->func(io + 1, BH_IO_TELL_CB, position, NULL); return io->cb(io + 1, BH_IO_TELL_CB, position, NULL);
} }
int bh_io_seek(bh_io_t *io, int BH_IOSeek(BH_IO *io,
int64_t position, int64_t position,
int direction) int direction)
{ {
@@ -178,22 +177,22 @@ int bh_io_seek(bh_io_t *io,
return BH_ERROR; return BH_ERROR;
/* Call the IO device seek handler */ /* Call the IO device seek handler */
return io->func(io + 1, BH_IO_SEEK_CB, &position, &direction); return io->cb(io + 1, BH_IO_SEEK_CB, &position, &direction);
} }
int bh_io_flush(bh_io_t *io) int BH_IOFlush(BH_IO *io)
{ {
/* Prevent working with NULL io */ /* Prevent working with NULL io */
if (!io) if (!io)
return BH_ERROR; return BH_ERROR;
/* Call the IO device flush handler */ /* Call the IO device flush handler */
return io->func(io + 1, BH_IO_FLUSH_CB, NULL, NULL); return io->cb(io + 1, BH_IO_FLUSH_CB, NULL, NULL);
} }
int bh_io_size(bh_io_t *io, int BH_IOSize(BH_IO *io,
int64_t *size) int64_t *size)
{ {
/* Prevent working with NULL io */ /* Prevent working with NULL io */
@@ -201,27 +200,27 @@ int bh_io_size(bh_io_t *io,
return BH_ERROR; return BH_ERROR;
/* Call the IO device size handler */ /* Call the IO device size handler */
return io->func(io + 1, BH_IO_SIZE_CB, size, NULL); return io->cb(io + 1, BH_IO_SIZE_CB, size, NULL);
} }
int bh_io_flags(bh_io_t *io) int BH_IOFlags(BH_IO *io)
{ {
/* Prevent working with NULL io */ /* Prevent working with NULL io */
if (!io) if (!io)
return BH_IO_FLAG_ERROR; return BH_IO_FLAG_ERROR;
/* Call the IO device flags handler */ /* Call the IO device flags handler */
return io->func(io + 1, BH_IO_FLAGS_CB, NULL, NULL); return io->cb(io + 1, BH_IO_FLAGS_CB, NULL, NULL);
} }
int bh_io_clear(bh_io_t *io) int BH_IOClear(BH_IO *io)
{ {
/* Prevent working with NULL io */ /* Prevent working with NULL io */
if (!io) if (!io)
return BH_OK; return BH_OK;
/* Call the IO device clear error handler */ /* Call the IO device clear error handler */
return io->func(io + 1, BH_IO_CLEAR_CB, NULL, NULL); return io->cb(io + 1, BH_IO_CLEAR_CB, NULL, NULL);
} }

1976
src/math.c

File diff suppressed because it is too large Load Diff

View File

@@ -7,72 +7,72 @@
#include <unistd.h> #include <unistd.h>
typedef struct bh_file_s typedef struct BH_File
{ {
char *path; char *path;
int mode; int mode;
int flags; int flags;
int handle; int handle;
} bh_file_t; } BH_File;
static int file_info(bh_file_t *file, static int BH_FileInfo(BH_File *file,
size_t *size, size_t *size,
const char **ident); const char **name);
static int file_init(bh_file_t *file, static int BH_FileInit(BH_File *file,
const char *path); const char *path);
static int file_destroy(bh_file_t *file); static int BH_FileDestroy(BH_File *file);
static int file_open(bh_file_t *file, static int BH_FileOpen(BH_File *file,
int *mode); int *mode);
static int file_close(bh_file_t *file); static int BH_FileClose(BH_File *file);
static int file_read(bh_file_t *file, static int BH_FileRead(BH_File *file,
char *data, char *data,
size_t *size); size_t *size);
static int file_write(bh_file_t *file, static int BH_FileWrite(BH_File *file,
const char *data, const char *data,
size_t *size); size_t *size);
static int file_peek(bh_file_t *file, static int BH_FilePeek(BH_File* file,
char *data, char *data,
size_t *size); size_t *size);
static int file_flush(bh_file_t *file); static int BH_FileFlush(BH_File *file);
static int file_seek(bh_file_t *file, static int BH_FileSeek(BH_File *file,
int64_t *pos, int64_t *pos,
int *dir); int *dir);
static int file_tell(bh_file_t *file, static int BH_FileTell(BH_File *file,
int64_t *pos); int64_t *pos);
static int file_size(bh_file_t *file, static int BH_FileSize(BH_File *file,
int64_t *size); int64_t *size);
static int file_flags(bh_file_t *file); static int BH_FileFlags(BH_File *file);
static int file_clear(bh_file_t *file); static int BH_FileClear(BH_File *file);
static int file_info(bh_file_t *file, static int BH_FileInfo(BH_File *file,
size_t *size, size_t *size,
const char **name) const char **name)
{ {
@@ -86,7 +86,7 @@ static int file_info(bh_file_t *file,
} }
static int file_init(bh_file_t *file, static int BH_FileInit(BH_File *file,
const char *path) const char *path)
{ {
/* Check if path is valid */ /* Check if path is valid */
@@ -103,11 +103,11 @@ static int file_init(bh_file_t *file,
} }
static int file_destroy(bh_file_t *file) static int BH_FileDestroy(BH_File *file)
{ {
/* Close the file handle on destruction */ /* Close the file handle on destruction */
if (file->handle != -1) if (file->handle != -1)
file_close(file); BH_FileClose(file);
/* Free path string */ /* Free path string */
free(file->path); free(file->path);
@@ -116,7 +116,7 @@ static int file_destroy(bh_file_t *file)
} }
static int file_openflags(int mode) static int BH_FileOpenFlags(int mode)
{ {
int flags = 0; int flags = 0;
@@ -152,7 +152,7 @@ static int file_openflags(int mode)
} }
static int file_open(bh_file_t *file, static int BH_FileOpen(BH_File *file,
int *mode) int *mode)
{ {
static const mode_t open_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); static const mode_t open_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
@@ -163,7 +163,7 @@ static int file_open(bh_file_t *file,
return BH_ERROR; return BH_ERROR;
/* Determine file open flags */ /* Determine file open flags */
flags = file_openflags(*mode); flags = BH_FileOpenFlags(*mode);
if (flags == -1) if (flags == -1)
return BH_ERROR; return BH_ERROR;
@@ -176,7 +176,7 @@ static int file_open(bh_file_t *file,
} }
static int file_close(bh_file_t *file) static int BH_FileClose(BH_File *file)
{ {
/* If file is closed - report error */ /* If file is closed - report error */
if (file->handle == -1) if (file->handle == -1)
@@ -190,7 +190,7 @@ static int file_close(bh_file_t *file)
} }
static int file_read(bh_file_t *file, static int BH_FileRead(BH_File *file,
char *data, char *data,
size_t *size) size_t *size)
{ {
@@ -220,7 +220,7 @@ error:
} }
static int file_write(bh_file_t *file, static int BH_FileWrite(BH_File *file,
const char *data, const char *data,
size_t *size) size_t *size)
{ {
@@ -250,7 +250,7 @@ error:
} }
static int file_peek(bh_file_t *file, static int BH_FilePeek(BH_File *file,
char *data, char *data,
size_t *size) size_t *size)
{ {
@@ -262,13 +262,13 @@ static int file_peek(bh_file_t *file,
goto error; goto error;
/* Read data from the file */ /* Read data from the file */
if (file_read(file, data, size)) if (BH_FileRead(file, data, size))
goto error; goto error;
/* Backtrack by the read amount */ /* Backtrack by the read amount */
position = -((int64_t)*size); position = -((int64_t)*size);
direction = BH_IO_SEEK_CUR; direction = BH_IO_SEEK_CUR;
if (file_seek(file, &position, &direction)) if (BH_FileSeek(file, &position, &direction))
goto error; goto error;
return BH_OK; return BH_OK;
@@ -279,7 +279,7 @@ error:
} }
static int file_flush(bh_file_t *file) static int BH_FileFlush(BH_File *file)
{ {
/* Check if file is open */ /* Check if file is open */
if (file->handle == -1) if (file->handle == -1)
@@ -294,7 +294,7 @@ static int file_flush(bh_file_t *file)
} }
static int file_seek(bh_file_t *file, static int BH_FileSeek(BH_File *file,
int64_t *pos, int64_t *pos,
int *dir) int *dir)
{ {
@@ -314,7 +314,7 @@ error:
} }
static int file_tell(bh_file_t *file, static int BH_FileTell(BH_File *file,
int64_t *pos) int64_t *pos)
{ {
/* Check if file is open */ /* Check if file is open */
@@ -333,7 +333,7 @@ error:
} }
static int file_size(bh_file_t *file, static int BH_FileSize(BH_File *file,
int64_t *size) int64_t *size)
{ {
struct stat sb; struct stat sb;
@@ -355,7 +355,7 @@ error:
} }
static int file_flags(bh_file_t *file) static int BH_FileFlags(BH_File *file)
{ {
/* If file handle is valid - append IO_OPEN flag */ /* If file handle is valid - append IO_OPEN flag */
if (file->handle != -1) if (file->handle != -1)
@@ -365,7 +365,7 @@ static int file_flags(bh_file_t *file)
} }
static int file_clear(bh_file_t *file) static int BH_FileClear(BH_File *file)
{ {
/* Clear IO_ERROR flag */ /* Clear IO_ERROR flag */
file->flags &= ~BH_IO_FLAG_ERROR; file->flags &= ~BH_IO_FLAG_ERROR;
@@ -373,33 +373,33 @@ static int file_clear(bh_file_t *file)
} }
static int file_proc(bh_file_t *file, static int BH_FileCallback(BH_File *file,
int type, int type,
void *arg1, void *arg1,
void *arg2) void *arg2)
{ {
switch (type) switch (type)
{ {
case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2); case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2);
case BH_IO_INIT_CB: return file_init(file, (const char *)arg1); case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1);
case BH_IO_DESTROY_CB: return file_destroy(file); case BH_IO_DESTROY_CB: return BH_FileDestroy(file);
case BH_IO_OPEN_CB: return file_open(file, (int *)arg1); case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1);
case BH_IO_CLOSE_CB: return file_close(file); case BH_IO_CLOSE_CB: return BH_FileClose(file);
case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2); case BH_IO_READ_CB: return BH_FileRead(file, (char *)arg1, (size_t *)arg2);
case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2); case BH_IO_WRITE_CB: return BH_FileWrite(file, (const char *)arg1, (size_t *)arg2);
case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2); case BH_IO_PEEK_CB: return BH_FilePeek(file, (char *)arg1, (size_t *)arg2);
case BH_IO_FLUSH_CB: return file_flush(file); case BH_IO_FLUSH_CB: return BH_FileFlush(file);
case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2); case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2);
case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1); case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1);
case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1); case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1);
case BH_IO_FLAGS_CB: return file_flags(file); case BH_IO_FLAGS_CB: return BH_FileFlags(file);
case BH_IO_CLEAR_CB: return file_clear(file); case BH_IO_CLEAR_CB: return BH_FileClear(file);
default: return BH_NOIMPL; default: return BH_NOIMPL;
} }
} }
bh_io_t *bh_file_new(const char *path) BH_IO *BH_FileNew(const char *path)
{ {
return bh_io_new((bh_io_func_t)file_proc, (void *)path); return BH_IONew((BH_IOCallback)BH_FileCallback, (void *)path);
} }

View File

@@ -3,7 +3,7 @@
#include <string.h> #include <string.h>
struct bh_queue_s struct BH_Queue
{ {
void **data; void **data;
size_t size; size_t size;
@@ -13,54 +13,54 @@ struct bh_queue_s
}; };
static void bh_queue_init(bh_queue_t *queue) static void BH_QueueInit(BH_Queue *queue)
{ {
memset(queue, 0, sizeof(*queue)); memset(queue, 0, sizeof(*queue));
} }
static void bh_queue_destroy(bh_queue_t *queue) static void BH_QueueDestroy(BH_Queue *queue)
{ {
if (queue->capacity) if (queue->capacity)
free(queue->data); free(queue->data);
} }
static void queue_copy(bh_queue_t *dest, static void BH_QueueCopy(BH_Queue *dest,
bh_queue_t *src) BH_Queue *src)
{ {
void *iter; void *iter;
/* Iterate over old queue and insert data into new queue */ /* Iterate over old queue and insert data into new queue */
iter = bh_queue_iter_next(src, NULL); iter = BH_QueueIterNext(src, NULL);
while (iter) while (iter)
{ {
bh_queue_insert(dest, bh_queue_iter_value(iter)); BH_QueueInsert(dest, BH_QueueIterValue(iter));
iter = bh_queue_iter_next(src, iter); iter = BH_QueueIterNext(src, iter);
} }
} }
bh_queue_t *bh_queue_new(void) BH_Queue *BH_QueueNew(void)
{ {
bh_queue_t *result; BH_Queue *result;
result = malloc(sizeof(*result)); result = malloc(sizeof(*result));
if (result) if (result)
bh_queue_init(result); BH_QueueInit(result);
return result; return result;
} }
void bh_queue_free(bh_queue_t *queue) void BH_QueueFree(BH_Queue *queue)
{ {
bh_queue_destroy(queue); BH_QueueDestroy(queue);
free(queue); free(queue);
} }
void bh_queue_clear(bh_queue_t *queue) void BH_QueueClear(BH_Queue *queue)
{ {
queue->head = 0; queue->head = 0;
queue->tail = 0; queue->tail = 0;
@@ -68,10 +68,10 @@ void bh_queue_clear(bh_queue_t *queue)
} }
int bh_queue_reserve(bh_queue_t *queue, int BH_QueueReserve(BH_Queue *queue,
size_t size) size_t size)
{ {
bh_queue_t other; BH_Queue other;
/* New capacity should be great or equal to current size */ /* New capacity should be great or equal to current size */
if (size < queue->size) if (size < queue->size)
@@ -86,7 +86,7 @@ int bh_queue_reserve(bh_queue_t *queue,
return BH_OK; return BH_OK;
/* Prepare new empty queue */ /* Prepare new empty queue */
bh_queue_init(&other); BH_QueueInit(&other);
if (size) if (size)
{ {
/* Allocate new capacity for the queue */ /* Allocate new capacity for the queue */
@@ -96,7 +96,7 @@ int bh_queue_reserve(bh_queue_t *queue,
return BH_OOM; return BH_OOM;
/* Iterate over old queue and insert data into new queue */ /* Iterate over old queue and insert data into new queue */
queue_copy(&other, queue); BH_QueueCopy(&other, queue);
} }
/* If old queue had allocated data - free it */ /* If old queue had allocated data - free it */
@@ -109,7 +109,7 @@ int bh_queue_reserve(bh_queue_t *queue,
} }
int bh_queue_insert(bh_queue_t *queue, int BH_QueueInsert(BH_Queue *queue,
void *value) void *value)
{ {
/* Check if queue can contain new element */ /* Check if queue can contain new element */
@@ -119,7 +119,7 @@ int bh_queue_insert(bh_queue_t *queue,
/* Check for capacity overflow and reserve capacity */ /* Check for capacity overflow and reserve capacity */
capacity = (queue->capacity) ? (queue->capacity * 2) : (16); capacity = (queue->capacity) ? (queue->capacity * 2) : (16);
if (capacity < queue->capacity || bh_queue_reserve(queue, capacity)) if (capacity < queue->capacity || BH_QueueReserve(queue, capacity))
return BH_OOM; return BH_OOM;
} }
@@ -133,7 +133,7 @@ int bh_queue_insert(bh_queue_t *queue,
} }
void bh_queue_remove(bh_queue_t *queue) void BH_QueueRemove(BH_Queue *queue)
{ {
/* Do nothing if queue is empty */ /* Do nothing if queue is empty */
if (!queue->size) if (!queue->size)
@@ -146,7 +146,7 @@ void bh_queue_remove(bh_queue_t *queue)
} }
int bh_queue_front(bh_queue_t *queue, void **value) int BH_QueueFront(BH_Queue *queue, void **value)
{ {
/* Do nothing if queue is empty */ /* Do nothing if queue is empty */
if (!queue->size) if (!queue->size)
@@ -158,25 +158,25 @@ int bh_queue_front(bh_queue_t *queue, void **value)
} }
int bh_queue_empty(bh_queue_t *queue) int BH_QueueEmpty(BH_Queue *queue)
{ {
return !queue->size; return !queue->size;
} }
size_t bh_queue_size(bh_queue_t *queue) size_t BH_QueueSize(BH_Queue *queue)
{ {
return queue->size; return queue->size;
} }
size_t bh_queue_capacity(bh_queue_t *queue) size_t BH_QueueCapacity(BH_Queue *queue)
{ {
return queue->capacity; return queue->capacity;
} }
void *bh_queue_iter_next(bh_queue_t *queue, void *BH_QueueIterNext(BH_Queue *queue,
void *iter) void *iter)
{ {
void **element = (void **)iter; void **element = (void **)iter;
@@ -203,7 +203,7 @@ void *bh_queue_iter_next(bh_queue_t *queue,
} }
void *bh_queue_iter_value(void *iter) void *BH_QueueIterValue(void *iter)
{ {
return *(void **)iter; return *(void **)iter;
} }

View File

@@ -4,72 +4,72 @@
#include <windows.h> #include <windows.h>
typedef struct bh_file_s typedef struct BH_File
{ {
char *path; char *path;
int mode; int mode;
int flags; int flags;
HANDLE handle; HANDLE handle;
} bh_file_t; } BH_File;
static int file_info(bh_file_t *file, static int BH_FileInfo(BH_File *file,
size_t *size, size_t *size,
const char **name); const char **name);
static int file_init(bh_file_t *file, static int BH_FileInit(BH_File *file,
const char *path); const char *path);
static int file_destroy(bh_file_t *file); static int BH_FileDestroy(BH_File *file);
static int file_open(bh_file_t *file, static int BH_FileOpen(BH_File *file,
int *mode); int *mode);
static int file_close(bh_file_t *file); static int BH_FileClose(BH_File *file);
static int file_read(bh_file_t *file, static int BH_FileRead(BH_File *file,
char *data, char *data,
size_t *size); size_t *size);
static int file_write(bh_file_t *file, static int BH_FileWrite(BH_File *file,
const char *data, const char *data,
size_t *size); size_t *size);
static int file_peek(bh_file_t *file, static int BH_FilePeek(BH_File* file,
char *data, char *data,
size_t *size); size_t *size);
static int file_flush(bh_file_t *file); static int BH_FileFlush(BH_File *file);
static int file_seek(bh_file_t *file, static int BH_FileSeek(BH_File *file,
int64_t *pos, int64_t *pos,
int *dir); int *dir);
static int file_tell(bh_file_t *file, static int BH_FileTell(BH_File *file,
int64_t *pos); int64_t *pos);
static int file_size(bh_file_t *file, static int BH_FileSize(BH_File *file,
int64_t *size); int64_t *size);
static int file_flags(bh_file_t *file); static int BH_FileFlags(BH_File *file);
static int file_clear(bh_file_t *file); static int BH_FileClear(BH_File *file);
static int file_info(bh_file_t *file, static int BH_FileInfo(BH_File *file,
size_t *size, size_t *size,
const char **name) const char **name)
{ {
@@ -84,7 +84,7 @@ static int file_info(bh_file_t *file,
} }
static int file_init(bh_file_t *file, static int BH_FileInit(BH_File *file,
const char *path) const char *path)
{ {
/* Check if path is valid */ /* Check if path is valid */
@@ -101,11 +101,11 @@ static int file_init(bh_file_t *file,
} }
static int file_destroy(bh_file_t *file) static int BH_FileDestroy(BH_File *file)
{ {
/* Close the file handle on destruction */ /* Close the file handle on destruction */
if (file->handle != INVALID_HANDLE_VALUE) if (file->handle != INVALID_HANDLE_VALUE)
file_close(file); BH_FileClose(file);
/* Free path string */ /* Free path string */
free(file->path); free(file->path);
@@ -114,14 +114,14 @@ static int file_destroy(bh_file_t *file)
} }
static int file_open(bh_file_t *file, static int BH_FileOpen(BH_File *file,
int *mode) int *mode)
{ {
DWORD access = 0, how = 0; DWORD access = 0, how = 0;
/* Check if file is already openned */ /* Check if file is already openned */
if (file->handle != INVALID_HANDLE_VALUE) if (file->handle != INVALID_HANDLE_VALUE)
return BH_OK; return BH_ERROR;
/* Determine read/write access flags */ /* Determine read/write access flags */
if (*mode & BH_IO_READ) if (*mode & BH_IO_READ)
@@ -169,7 +169,7 @@ static int file_open(bh_file_t *file,
} }
static int file_close(bh_file_t *file) static int BH_FileClose(BH_File *file)
{ {
/* If file is opened - close it */ /* If file is opened - close it */
if (file->handle == INVALID_HANDLE_VALUE) if (file->handle == INVALID_HANDLE_VALUE)
@@ -183,7 +183,7 @@ static int file_close(bh_file_t *file)
} }
static int file_read(bh_file_t *file, static int BH_FileRead(BH_File *file,
char *data, char *data,
size_t *size) size_t *size)
{ {
@@ -212,7 +212,7 @@ error:
} }
static int file_write(bh_file_t *file, static int BH_FileWrite(BH_File *file,
const char *data, const char *data,
size_t *size) size_t *size)
{ {
@@ -251,7 +251,7 @@ error:
} }
static int file_peek(bh_file_t *file, static int BH_FilePeek(BH_File *file,
char *data, char *data,
size_t *size) size_t *size)
{ {
@@ -259,20 +259,20 @@ static int file_peek(bh_file_t *file,
int direction; int direction;
/* Read data from the file */ /* Read data from the file */
if (file_read(file, data, size)) if (BH_FileRead(file, data, size))
return BH_ERROR; return BH_ERROR;
/* Backtrack by the read amount */ /* Backtrack by the read amount */
position = -((int64_t)*size); position = -((int64_t)*size);
direction = BH_IO_SEEK_CUR; direction = BH_IO_SEEK_CUR;
if (file_seek(file, &position, &direction)) if (BH_FileSeek(file, &position, &direction))
return BH_ERROR; return BH_ERROR;
return BH_OK; return BH_OK;
} }
static int file_flush(bh_file_t *file) static int BH_FileFlush(BH_File *file)
{ {
/* Check if file is opened */ /* Check if file is opened */
if (file->handle == INVALID_HANDLE_VALUE) if (file->handle == INVALID_HANDLE_VALUE)
@@ -288,7 +288,7 @@ error:
} }
static int file_seek(bh_file_t *file, static int BH_FileSeek(BH_File *file,
int64_t *pos, int64_t *pos,
int *dir) int *dir)
{ {
@@ -311,7 +311,7 @@ error:
} }
static int file_tell(bh_file_t *file, static int BH_FileTell(BH_File *file,
int64_t *pos) int64_t *pos)
{ {
LARGE_INTEGER dummy, position; LARGE_INTEGER dummy, position;
@@ -334,7 +334,7 @@ error:
} }
static int file_size(bh_file_t *file, static int BH_FileSize(BH_File *file,
int64_t *size) int64_t *size)
{ {
LARGE_INTEGER dummy; LARGE_INTEGER dummy;
@@ -356,7 +356,7 @@ error:
} }
static int file_flags(bh_file_t *file) static int BH_FileFlags(BH_File *file)
{ {
/* If file handle is valid - append IO_OPEN flag */ /* If file handle is valid - append IO_OPEN flag */
if (file->handle != INVALID_HANDLE_VALUE) if (file->handle != INVALID_HANDLE_VALUE)
@@ -365,7 +365,7 @@ static int file_flags(bh_file_t *file)
} }
static int file_clear(bh_file_t *file) static int BH_FileClear(BH_File *file)
{ {
/* Clear IO_ERROR flag */ /* Clear IO_ERROR flag */
file->flags &= ~BH_IO_FLAG_ERROR; file->flags &= ~BH_IO_FLAG_ERROR;
@@ -373,33 +373,33 @@ static int file_clear(bh_file_t *file)
} }
static int file_proc(bh_file_t *file, static int BH_FileCallback(BH_File *file,
int type, int type,
void *arg1, void *arg1,
void *arg2) void *arg2)
{ {
switch (type) switch (type)
{ {
case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2); case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2);
case BH_IO_INIT_CB: return file_init(file, (const char *)arg1); case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1);
case BH_IO_DESTROY_CB: return file_destroy(file); case BH_IO_DESTROY_CB: return BH_FileDestroy(file);
case BH_IO_OPEN_CB: return file_open(file, (int *)arg1); case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1);
case BH_IO_CLOSE_CB: return file_close(file); case BH_IO_CLOSE_CB: return BH_FileClose(file);
case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2); case BH_IO_READ_CB: return BH_FileRead(file, (char *)arg1, (size_t *)arg2);
case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2); case BH_IO_WRITE_CB: return BH_FileWrite(file, (const char *)arg1, (size_t *)arg2);
case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2); case BH_IO_PEEK_CB: return BH_FilePeek(file, (char *)arg1, (size_t *)arg2);
case BH_IO_FLUSH_CB: return file_flush(file); case BH_IO_FLUSH_CB: return BH_FileFlush(file);
case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2); case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2);
case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1); case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1);
case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1); case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1);
case BH_IO_FLAGS_CB: return file_flags(file); case BH_IO_FLAGS_CB: return BH_FileFlags(file);
case BH_IO_CLEAR_CB: return file_clear(file); case BH_IO_CLEAR_CB: return BH_FileClear(file);
default: return BH_NOIMPL; default: return BH_NOIMPL;
} }
} }
bh_io_t *bh_file_new(const char *path) BH_IO *BH_FileNew(const char *path)
{ {
return bh_io_new((bh_io_func_t)file_proc, (void *)path); return BH_IONew((BH_IOCallback)BH_FileCallback, (void *)path);
} }

View File

@@ -4,14 +4,14 @@
#include <stdlib.h> #include <stdlib.h>
static int int_equal(const void *lhs, static int DBG_IntEqual(const void *lhs,
const void *rhs) const void *rhs)
{ {
return *(const int*)lhs - *(const int*)rhs; return *(const int*)lhs - *(const int*)rhs;
} }
static int verify_partition(size_t index, static int DBG_VerifyPartition(size_t index,
int pivot, int pivot,
int *array, int *array,
size_t size) size_t size)
@@ -30,7 +30,7 @@ static int verify_partition(size_t index,
} }
static int verify_heap(int *array, static int DBG_VerifyHeap(int *array,
size_t size) size_t size)
{ {
size_t i, left, right; size_t i, left, right;
@@ -50,7 +50,7 @@ static int verify_heap(int *array,
} }
static int reference_rand(void) static int DBG_ReferenceRand(void)
{ {
static uint32_t next = 2025; static uint32_t next = 2025;
next = next * 1103515245 + 12345; next = next * 1103515245 + 12345;
@@ -58,7 +58,8 @@ static int reference_rand(void)
} }
static void reference_sort(int *array, size_t size) static void DBG_ReferenceSort(int *array,
size_t size)
{ {
size_t i, j; size_t i, j;
int tmp; int tmp;
@@ -79,7 +80,7 @@ static void reference_sort(int *array, size_t size)
} }
static void fill_arrays(int *array, static void DBG_FillArrays(int *array,
int *reference, int *reference,
size_t size, size_t size,
int mask) int mask)
@@ -95,7 +96,7 @@ static void fill_arrays(int *array,
/* Fill the array */ /* Fill the array */
for (i = 0; i < size; ++i) for (i = 0; i < size; ++i)
{ {
array[i] = reference_rand(); array[i] = DBG_ReferenceRand();
if (mask > 1) if (mask > 1)
array[i] = array[i] % mask; array[i] = array[i] % mask;
@@ -109,19 +110,19 @@ static void fill_arrays(int *array,
} }
static int check_sort(void) static int CheckSort(void)
{ {
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size; size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int reference[317], data[317]; int reference[317], data[317];
/* Test empty array and one element arrays */ /* Test empty array and one element arrays */
data[0] = 1; data[1] = 2; data[2] = 3; data[0] = 1; data[1] = 2; data[2] = 3;
bh_sort(data, 0, sizeof(int), int_equal); BH_Sort(data, 0, sizeof(int), DBG_IntEqual);
BH_VERIFY(data[0] == 1); BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2); BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3); BH_VERIFY(data[2] == 3);
bh_sort(data, 1, sizeof(int), int_equal); BH_Sort(data, 1, sizeof(int), DBG_IntEqual);
BH_VERIFY(data[0] == 1); BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2); BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3); BH_VERIFY(data[2] == 3);
@@ -129,27 +130,27 @@ static int check_sort(void)
/* Test array against different sizes */ /* Test array against different sizes */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, 0); DBG_FillArrays(data, reference, *size, 0);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
bh_sort(data, *size, sizeof(int), int_equal); BH_Sort(data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0);
} }
/* Test against negative values */ /* Test against negative values */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, -1); DBG_FillArrays(data, reference, *size, -1);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
bh_sort(data, *size, sizeof(int), int_equal); BH_Sort(data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0);
} }
/* Test against duplicates */ /* Test against duplicates */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, 4); DBG_FillArrays(data, reference, *size, 4);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
bh_sort(data, *size, sizeof(int), int_equal); BH_Sort(data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0);
} }
@@ -157,7 +158,7 @@ static int check_sort(void)
} }
static int check_partition(void) static int CheckPartition(void)
{ {
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size; size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int reference[317], data[317], value, *pivot; int reference[317], data[317], value, *pivot;
@@ -165,13 +166,13 @@ static int check_partition(void)
/* Test empty array and one element array */ /* Test empty array and one element array */
data[0] = 1; data[1] = 2; data[2] = 3; data[0] = 1; data[1] = 2; data[2] = 3;
value = 0; value = 0;
pivot = bh_partition(&value, data, 0, sizeof(int), int_equal); pivot = BH_Partition(&value, data, 0, sizeof(int), DBG_IntEqual);
BH_VERIFY(pivot != NULL); BH_VERIFY(pivot != NULL);
BH_VERIFY(data[0] == 1); BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2); BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3); BH_VERIFY(data[2] == 3);
pivot = bh_partition(&value, data, 1, sizeof(int), int_equal); pivot = BH_Partition(&value, data, 1, sizeof(int), DBG_IntEqual);
BH_VERIFY(pivot == data); BH_VERIFY(pivot == data);
BH_VERIFY(data[0] == 1); BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2); BH_VERIFY(data[1] == 2);
@@ -180,104 +181,104 @@ static int check_partition(void)
/* Test array against different sizes */ /* Test array against different sizes */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, 0); DBG_FillArrays(data, reference, *size, 0);
value = 16384; value = 16384;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
/* Test against negative values */ /* Test against negative values */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, -1); DBG_FillArrays(data, reference, *size, -1);
value = -16384; value = -16384;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
/* Test against duplicates */ /* Test against duplicates */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, 4); DBG_FillArrays(data, reference, *size, 4);
value = 2; value = 2;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
/* Test array against small pivots */ /* Test array against small pivots */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, 0); DBG_FillArrays(data, reference, *size, 0);
value = -100; value = -100;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
/* Test array against large pivots */ /* Test array against large pivots */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, 0); DBG_FillArrays(data, reference, *size, 0);
value = 65536; value = 65536;
pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
/* Test array against different sizes (pivot inside the array) */ /* Test array against different sizes (pivot inside the array) */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, 0); DBG_FillArrays(data, reference, *size, 0);
value = data[0]; value = data[0];
pivot = bh_partition(data, data, *size, sizeof(int), int_equal); pivot = BH_Partition(data, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
/* Test against negative values (pivot inside the array) */ /* Test against negative values (pivot inside the array) */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, -1); DBG_FillArrays(data, reference, *size, -1);
value = data[0]; value = data[0];
pivot = bh_partition(data, data, *size, sizeof(int), int_equal); pivot = BH_Partition(data, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
/* Test against duplicates (pivot inside the array) */ /* Test against duplicates (pivot inside the array) */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, reference, *size, 4); DBG_FillArrays(data, reference, *size, 4);
value = data[0]; value = data[0];
pivot = bh_partition(data, data, *size, sizeof(int), int_equal); pivot = BH_Partition(data, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
@@ -287,11 +288,11 @@ static int check_partition(void)
memset(data, 0, sizeof(int) * *size); memset(data, 0, sizeof(int) * *size);
memset(reference, 0, sizeof(int) * *size); memset(reference, 0, sizeof(int) * *size);
value = data[*size - 1]; value = data[*size - 1];
pivot = bh_partition(data + *size - 1, data, *size, sizeof(int), int_equal); pivot = BH_Partition(data + *size - 1, data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0);
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0);
} }
@@ -299,14 +300,14 @@ static int check_partition(void)
} }
static int check_heap_make(void) static int CheckHeapMake(void)
{ {
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size; size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int data[317]; int data[317];
/* Test empty array */ /* Test empty array */
data[0] = 1; data[1] = 2; data[2] = 3; data[0] = 1; data[1] = 2; data[2] = 3;
bh_heap_make(data, 0, sizeof(int), int_equal); BH_HeapMake(data, 0, sizeof(int), DBG_IntEqual);
BH_VERIFY(data[0] == 1); BH_VERIFY(data[0] == 1);
BH_VERIFY(data[1] == 2); BH_VERIFY(data[1] == 2);
BH_VERIFY(data[2] == 3); BH_VERIFY(data[2] == 3);
@@ -314,32 +315,32 @@ static int check_heap_make(void)
/* Test array against different sizes */ /* Test array against different sizes */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, NULL, *size, 0); DBG_FillArrays(data, NULL, *size, 0);
bh_heap_make(data, *size, sizeof(int), int_equal); BH_HeapMake(data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, *size) == 0); BH_VERIFY(DBG_VerifyHeap(data, *size) == 0);
} }
/* Test array against negative values */ /* Test array against negative values */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, NULL, *size, -1); DBG_FillArrays(data, NULL, *size, -1);
bh_heap_make(data, *size, sizeof(int), int_equal); BH_HeapMake(data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, *size) == 0); BH_VERIFY(DBG_VerifyHeap(data, *size) == 0);
} }
/* Test against duplicates */ /* Test against duplicates */
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
fill_arrays(data, NULL, *size, 4); DBG_FillArrays(data, NULL, *size, 4);
bh_heap_make(data, *size, sizeof(int), int_equal); BH_HeapMake(data, *size, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, *size) == 0); BH_VERIFY(DBG_VerifyHeap(data, *size) == 0);
} }
return 0; return 0;
} }
static int check_heap_insert(void) static int CheckHeapInsert(void)
{ {
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size; size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int data[317], reference[317]; int data[317], reference[317];
@@ -348,18 +349,18 @@ static int check_heap_insert(void)
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
size_t i; size_t i;
fill_arrays(data, reference, *size, 0); DBG_FillArrays(data, reference, *size, 0);
for (i = 0; i < *size; ++i) for (i = 0; i < *size; ++i)
{ {
int value; int value;
value = data[i]; value = data[i];
bh_heap_insert(&value, data, i, sizeof(int), int_equal); BH_HeapInsert(&value, data, i, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, i + 1) == 0); BH_VERIFY(DBG_VerifyHeap(data, i + 1) == 0);
} }
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size) == 0); BH_VERIFY(memcmp(data, reference, *size) == 0);
} }
@@ -367,15 +368,15 @@ static int check_heap_insert(void)
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
size_t i; size_t i;
fill_arrays(data, reference, *size, 0); DBG_FillArrays(data, reference, *size, 0);
for (i = 0; i < *size; ++i) for (i = 0; i < *size; ++i)
{ {
bh_heap_insert(NULL, data, i, sizeof(int), int_equal); BH_HeapInsert(NULL, data, i, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, i + 1) == 0); BH_VERIFY(DBG_VerifyHeap(data, i + 1) == 0);
} }
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size) == 0); BH_VERIFY(memcmp(data, reference, *size) == 0);
} }
@@ -383,7 +384,7 @@ static int check_heap_insert(void)
} }
static int check_heap_remove(void) static int CheckHeapRemove(void)
{ {
size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size; size_t sizes[] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}, *size;
int data[317], reference[317]; int data[317], reference[317];
@@ -392,17 +393,17 @@ static int check_heap_remove(void)
for (size = sizes; *size; ++size) for (size = sizes; *size; ++size)
{ {
size_t i; size_t i;
fill_arrays(data, reference, *size, 0); DBG_FillArrays(data, reference, *size, 0);
bh_heap_make(data, *size, sizeof(int), int_equal); BH_HeapMake(data, *size, sizeof(int), DBG_IntEqual);
for (i = *size; i > 0; i--) for (i = *size; i > 0; i--)
{ {
BH_VERIFY(verify_heap(data, i) == 0); BH_VERIFY(DBG_VerifyHeap(data, i) == 0);
bh_heap_remove(data, i, sizeof(int), int_equal); BH_HeapRemove(data, i, sizeof(int), DBG_IntEqual);
} }
reference_sort(data, *size); DBG_ReferenceSort(data, *size);
reference_sort(reference, *size); DBG_ReferenceSort(reference, *size);
BH_VERIFY(memcmp(data, reference, *size) == 0); BH_VERIFY(memcmp(data, reference, *size) == 0);
} }
@@ -410,57 +411,57 @@ static int check_heap_remove(void)
} }
static int check_heap_replace(void) static int CheckHeapReplace(void)
{ {
int data[11] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}; 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 reference[11] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0};
int value; int value;
/* Prepare test arrays */ /* Prepare test arrays */
bh_heap_make(data, 10, sizeof(int), int_equal); BH_HeapMake(data, 10, sizeof(int), DBG_IntEqual);
bh_heap_make(reference, 10, sizeof(int), int_equal); BH_HeapMake(reference, 10, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, 10) == 0); BH_VERIFY(DBG_VerifyHeap(data, 10) == 0);
BH_VERIFY(verify_heap(reference, 10) == 0); BH_VERIFY(DBG_VerifyHeap(reference, 10) == 0);
/* Verfify heap replace */ /* Verfify heap replace */
value = 20; value = 20;
bh_heap_replace(&value, data, 10, sizeof(int), int_equal); BH_HeapReplace(&value, data, 10, sizeof(int), DBG_IntEqual);
bh_heap_remove(reference, 10, sizeof(int), int_equal); BH_HeapRemove(reference, 10, sizeof(int), DBG_IntEqual);
bh_heap_insert(&value, reference, 9, sizeof(int), int_equal); BH_HeapInsert(&value, reference, 9, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, 10) == 0); BH_VERIFY(DBG_VerifyHeap(data, 10) == 0);
BH_VERIFY(verify_heap(reference, 10) == 0); BH_VERIFY(DBG_VerifyHeap(reference, 10) == 0);
bh_sort(data, 10, sizeof(int), int_equal); BH_Sort(data, 10, sizeof(int), DBG_IntEqual);
bh_sort(reference, 10, sizeof(int), int_equal); BH_Sort(reference, 10, sizeof(int), DBG_IntEqual);
BH_VERIFY(memcmp(data, reference, 10 * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, 10 * sizeof(int)) == 0);
/* Verify heap replace on single element array */ /* Verify heap replace on single element array */
value = 400; value = 400;
bh_heap_replace(&value, data, 1, sizeof(int), int_equal); BH_HeapReplace(&value, data, 1, sizeof(int), DBG_IntEqual);
bh_heap_remove(reference, 1, sizeof(int), int_equal); BH_HeapRemove(reference, 1, sizeof(int), DBG_IntEqual);
bh_heap_insert(&value, reference, 0, sizeof(int), int_equal); BH_HeapInsert(&value, reference, 0, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, 1) == 0); BH_VERIFY(DBG_VerifyHeap(data, 1) == 0);
BH_VERIFY(verify_heap(reference, 1) == 0); BH_VERIFY(DBG_VerifyHeap(reference, 1) == 0);
BH_VERIFY(memcmp(data, reference, 1 * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, 1 * sizeof(int)) == 0);
/* Prepare test arrays */ /* Prepare test arrays */
bh_sort(data, 10, sizeof(int), int_equal); BH_Sort(data, 10, sizeof(int), DBG_IntEqual);
bh_sort(reference, 10, sizeof(int), int_equal); BH_Sort(reference, 10, sizeof(int), DBG_IntEqual);
bh_heap_make(data, 10, sizeof(int), int_equal); BH_HeapMake(data, 10, sizeof(int), DBG_IntEqual);
bh_heap_make(reference, 10, sizeof(int), int_equal); BH_HeapMake(reference, 10, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, 10) == 0); BH_VERIFY(DBG_VerifyHeap(data, 10) == 0);
BH_VERIFY(verify_heap(reference, 10) == 0); BH_VERIFY(DBG_VerifyHeap(reference, 10) == 0);
data[10] = 1000; data[10] = 1000;
value = 1000; value = 1000;
bh_heap_replace(NULL, data, 10, sizeof(int), int_equal); BH_HeapReplace(NULL, data, 10, sizeof(int), DBG_IntEqual);
bh_heap_remove(reference, 10, sizeof(int), int_equal); BH_HeapRemove(reference, 10, sizeof(int), DBG_IntEqual);
bh_heap_insert(&value, reference, 9, sizeof(int), int_equal); BH_HeapInsert(&value, reference, 9, sizeof(int), DBG_IntEqual);
BH_VERIFY(verify_heap(data, 10) == 0); BH_VERIFY(DBG_VerifyHeap(data, 10) == 0);
BH_VERIFY(verify_heap(reference, 10) == 0); BH_VERIFY(DBG_VerifyHeap(reference, 10) == 0);
bh_sort(data, 10, sizeof(int), int_equal); BH_Sort(data, 10, sizeof(int), DBG_IntEqual);
bh_sort(reference, 10, sizeof(int), int_equal); BH_Sort(reference, 10, sizeof(int), DBG_IntEqual);
BH_VERIFY(memcmp(data, reference, 10 * sizeof(int)) == 0); BH_VERIFY(memcmp(data, reference, 10 * sizeof(int)) == 0);
return 0; return 0;
@@ -472,12 +473,12 @@ int main(int argc, char **argv)
BH_UNUSED(argc); BH_UNUSED(argc);
BH_UNUSED(argv); BH_UNUSED(argv);
bh_unit_add("sort", check_sort); BH_UnitAdd("Sort", CheckSort);
bh_unit_add("partition", check_partition); BH_UnitAdd("Partition", CheckPartition);
bh_unit_add("heap_make", check_heap_make); BH_UnitAdd("HeapMake", CheckHeapMake);
bh_unit_add("heap_insert", check_heap_insert); BH_UnitAdd("HeapInsert", CheckHeapInsert);
bh_unit_add("heap_remove", check_heap_remove); BH_UnitAdd("HeapRemove", CheckHeapRemove);
bh_unit_add("heap_replace", check_heap_replace); BH_UnitAdd("HeapReplace", CheckHeapReplace);
return bh_unit_run(); return BH_UnitRun();
} }

View File

@@ -24,41 +24,41 @@ static void cleanup(void)
/** /**
* Check for invalid arguments. * Check for invalid arguments.
*/ */
static int check_null(void) static int checkNull(void)
{ {
bh_io_t *io; BH_IO *io;
/* Check against NULL pointers */ /* Check against NULL pointers */
BH_VERIFY(bh_file_new(NULL) == NULL); BH_VERIFY(BH_FileNew(NULL) == NULL);
BH_VERIFY(bh_io_classname(NULL) == NULL); BH_VERIFY(BH_IOClassname(NULL) == NULL);
BH_VERIFY(bh_io_open(NULL, 0) != BH_OK); BH_VERIFY(BH_IOOpen(NULL, 0) != BH_OK);
BH_VERIFY(bh_io_close(NULL) != BH_OK); BH_VERIFY(BH_IOClose(NULL) != BH_OK);
BH_VERIFY(bh_io_read(NULL, NULL, 0, NULL) != BH_OK); BH_VERIFY(BH_IORead(NULL, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_write(NULL, NULL, 0, NULL) != BH_OK); BH_VERIFY(BH_IOWrite(NULL, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_peek(NULL, NULL, 0, NULL) != BH_OK); BH_VERIFY(BH_IOPeek(NULL, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_tell(NULL, NULL) != BH_OK); BH_VERIFY(BH_IOTell(NULL, NULL) != BH_OK);
BH_VERIFY(bh_io_seek(NULL, 0, 0) != BH_OK); BH_VERIFY(BH_IOSeek(NULL, 0, 0) != BH_OK);
BH_VERIFY(bh_io_flush(NULL) != BH_OK); BH_VERIFY(BH_IOFlush(NULL) != BH_OK);
BH_VERIFY(bh_io_size(NULL, NULL) != BH_OK); BH_VERIFY(BH_IOSize(NULL, NULL) != BH_OK);
BH_VERIFY(bh_io_flags(NULL) == BH_IO_FLAG_ERROR); BH_VERIFY(BH_IOFlags(NULL) == BH_IO_FLAG_ERROR);
BH_VERIFY(bh_io_clear(NULL) == BH_OK); BH_VERIFY(BH_IOClear(NULL) == BH_OK);
bh_io_free(NULL); BH_IOFree(NULL);
/* Check against NULL pointers and valid IO object */ /* Check against NULL pointers and valid IO object */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, 0) != BH_OK); BH_VERIFY(BH_IOOpen(io, 0) != BH_OK);
BH_VERIFY(bh_io_close(io) != BH_OK); BH_VERIFY(BH_IOClose(io) != BH_OK);
BH_VERIFY(bh_io_read(io, NULL, 0, NULL) != BH_OK); BH_VERIFY(BH_IORead(io, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_write(io, NULL, 0, NULL) != BH_OK); BH_VERIFY(BH_IOWrite(io, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_peek(io, NULL, 0, NULL) != BH_OK); BH_VERIFY(BH_IOPeek(io, NULL, 0, NULL) != BH_OK);
BH_VERIFY(bh_io_tell(io, NULL) != BH_OK); BH_VERIFY(BH_IOTell(io, NULL) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, 0) != BH_OK); BH_VERIFY(BH_IOSeek(io, 0, 0) != BH_OK);
BH_VERIFY(bh_io_flush(io) != BH_OK); BH_VERIFY(BH_IOFlush(io) != BH_OK);
BH_VERIFY(bh_io_size(io, NULL) != BH_OK); BH_VERIFY(BH_IOSize(io, NULL) != BH_OK);
BH_VERIFY(bh_io_flags(io) == BH_IO_FLAG_ERROR); BH_VERIFY(BH_IOFlags(io) == BH_IO_FLAG_ERROR);
BH_VERIFY(bh_io_clear(io) == BH_OK); BH_VERIFY(BH_IOClear(io) == BH_OK);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -67,93 +67,93 @@ static int check_null(void)
/** /**
* Check for normal mode. * Check for normal mode.
*/ */
static int check_normal(void) static int checkNormal(void)
{ {
int64_t position; int64_t position;
char buffer[128]; char buffer[128];
size_t actual; size_t actual;
bh_io_t *io; BH_IO *io;
/* Check operations for write only access */ /* Check operations for write only access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_open(io, BH_IO_READ) != BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ) != BH_OK);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 10, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK); BH_VERIFY(BH_IORead(io, buffer, 1, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_END) == BH_OK);
BH_VERIFY(bh_io_tell(io, &position) == BH_OK); BH_VERIFY(BH_IOTell(io, &position) == BH_OK);
BH_VERIFY(position == 20); BH_VERIFY(position == 20);
bh_io_close(io); BH_IOClose(io);
/* Check operations for read only access */ /* Check operations for read only access */
BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 20); BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0); BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK); BH_VERIFY(BH_IOSeek(io, -5, BH_IO_SEEK_CUR) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "67890", 5) == 0); BH_VERIFY(memcmp(buffer, "67890", 5) == 0);
bh_io_free(io); BH_IOFree(io);
/* Check operations for read and write access */ /* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890abcde67890", 20, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890abcde67890", 20, &actual) == BH_OK);
BH_VERIFY(actual == 20); BH_VERIFY(actual == 20);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 35, &actual) == BH_OK);
BH_VERIFY(actual == 35); BH_VERIFY(actual == 35);
BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0); BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0);
bh_io_close(io); BH_IOClose(io);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -161,108 +161,108 @@ static int check_normal(void)
/** /**
* Check for truncate mode. * Check for truncate mode.
*/ */
static int check_truncate(void) static int checkTruncate(void)
{ {
int64_t position; int64_t position;
char buffer[128]; char buffer[128];
size_t actual; size_t actual;
bh_io_t *io; BH_IO *io;
/* Check operations for write only access */ /* Check operations for write only access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 10, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK); BH_VERIFY(BH_IORead(io, buffer, 1, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_END) == BH_OK);
BH_VERIFY(bh_io_tell(io, &position) == BH_OK); BH_VERIFY(BH_IOTell(io, &position) == BH_OK);
BH_VERIFY(position == 20); BH_VERIFY(position == 20);
bh_io_close(io); BH_IOClose(io);
/* Check operations for read only access without truncate */ /* Check operations for read only access without truncate */
BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 20); BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0); BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0);
BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK); BH_VERIFY(BH_IOSeek(io, -5, BH_IO_SEEK_CUR) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "67890", 5) == 0); BH_VERIFY(memcmp(buffer, "67890", 5) == 0);
bh_io_close(io); BH_IOClose(io);
/* Check operations for read only access */ /* Check operations for read only access */
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 0); BH_VERIFY(actual == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 0); BH_VERIFY(actual == 0);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 0); BH_VERIFY(actual == 0);
bh_io_free(io); BH_IOFree(io);
/* Check operations for read and write access */ /* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(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_IOOpen(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_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890abcde67890", 20, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890abcde67890", 20, &actual) == BH_OK);
BH_VERIFY(actual == 20); BH_VERIFY(actual == 20);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 35, &actual) == BH_OK);
BH_VERIFY(actual == 35); BH_VERIFY(actual == 35);
BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0); BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0);
bh_io_close(io); BH_IOClose(io);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
bh_io_close(io); BH_IOClose(io);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -270,100 +270,100 @@ static int check_truncate(void)
/** /**
* Check for exist mode. * Check for exist mode.
*/ */
static int check_exist(void) static int checkExist(void)
{ {
int64_t position; int64_t position;
char buffer[128]; char buffer[128];
size_t actual; size_t actual;
bh_io_t *io; BH_IO *io;
/* Check operations for write only access */ /* Check operations for write only access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_EXIST) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_EXIST) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 10, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK); BH_VERIFY(BH_IORead(io, buffer, 1, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_END) == BH_OK);
BH_VERIFY(bh_io_tell(io, &position) == BH_OK); BH_VERIFY(BH_IOTell(io, &position) == BH_OK);
BH_VERIFY(position == 20); BH_VERIFY(position == 20);
bh_io_close(io); BH_IOClose(io);
/* Check operations for read only access */ /* Check operations for read only access */
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_EXIST) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_EXIST) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, sizeof(buffer), &actual) == BH_OK);
BH_VERIFY(actual == 20); BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0); BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK); BH_VERIFY(BH_IOSeek(io, -5, BH_IO_SEEK_CUR) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "67890", 5) == 0); BH_VERIFY(memcmp(buffer, "67890", 5) == 0);
bh_io_free(io); BH_IOFree(io);
/* Check operations for read and write access */ /* Check operations for read and write access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(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_IOOpen(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_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890abcde67890", 20, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890abcde67890", 20, &actual) == BH_OK);
BH_VERIFY(actual == 20); BH_VERIFY(actual == 20);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 35, &actual) == BH_OK);
BH_VERIFY(actual == 35); BH_VERIFY(actual == 35);
BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0); BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0);
bh_io_close(io); BH_IOClose(io);
bh_io_free(io); BH_IOFree(io);
/* Check against non existing files */ /* Check against non existing files */
BH_VERIFY((io = bh_file_new(FILENAME2)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME2)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_EXIST) != BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_EXIST) != BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0); BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_OPEN) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_EXIST) != BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_EXIST) != BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0); BH_VERIFY((BH_IOFlags(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_IOOpen(io, BH_IO_WRITE | BH_IO_READ | BH_IO_EXIST) != BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0); BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_OPEN) == 0);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -371,83 +371,83 @@ static int check_exist(void)
/** /**
* Check in append mode. * Check in append mode.
*/ */
static int check_append(void) static int checkAppend(void)
{ {
int64_t position; int64_t position;
char buffer[128]; char buffer[128];
size_t actual; size_t actual;
bh_io_t *io; BH_IO *io;
/* Explicitly call cleanup */ /* Explicitly call cleanup */
cleanup(); cleanup();
/* Check operations for write only access */ /* Check operations for write only access */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_APPEND) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_APPEND) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 10, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK); BH_VERIFY(BH_IORead(io, buffer, 1, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_END) == BH_OK);
BH_VERIFY(bh_io_tell(io, &position) == BH_OK); BH_VERIFY(BH_IOTell(io, &position) == BH_OK);
BH_VERIFY(position == 25); BH_VERIFY(position == 25);
bh_io_close(io); BH_IOClose(io);
/* Check operations for read only access */ /* Check operations for read only access */
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_APPEND) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_APPEND) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) != BH_OK);
BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK); BH_VERIFY(BH_IOSeek(io, -5, BH_IO_SEEK_CUR) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0);
bh_io_close(io); BH_IOClose(io);
/* Check operations for read and write access */ /* 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_IOOpen(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_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK);
BH_VERIFY(actual == 5); BH_VERIFY(actual == 5);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_read(io, buffer, 40, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 40, &actual) == BH_OK);
BH_VERIFY(actual == 40); BH_VERIFY(actual == 40);
BH_VERIFY(memcmp(buffer, "12345678901234567890abcdeabcde1234567890", 40) == 0); BH_VERIFY(memcmp(buffer, "12345678901234567890abcdeabcde1234567890", 40) == 0);
bh_io_close(io); BH_IOClose(io);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
bh_io_close(io); BH_IOClose(io);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -455,37 +455,37 @@ static int check_append(void)
/** /**
* Check for create mode. * Check for create mode.
*/ */
static int check_create(void) static int checkCreate(void)
{ {
bh_io_t *io; BH_IO *io;
/* Check for already existing file */ /* Check for already existing file */
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_CREATE) != BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_CREATE) != BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0); BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_OPEN) == 0);
bh_io_free(io); BH_IOFree(io);
/* Check for new file with write access */ /* Check for new file with write access */
BH_VERIFY((io = bh_file_new(FILENAME2)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME2)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_CREATE) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_CREATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
bh_io_free(io); BH_IOFree(io);
/* Check for new file with read access */ /* Check for new file with read access */
BH_VERIFY((io = bh_file_new(FILENAME3)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME3)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_CREATE) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_CREATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
bh_io_free(io); BH_IOFree(io);
/* Check for new file with read/write access */ /* Check for new file with read/write access */
BH_VERIFY((io = bh_file_new(FILENAME4)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME4)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(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_IOOpen(io, BH_IO_WRITE | BH_IO_READ | BH_IO_CREATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -494,23 +494,23 @@ static int check_create(void)
/** /**
* Check for EOF flags. * Check for EOF flags.
*/ */
static int check_eof(void) static int checkEOF(void)
{ {
char buffer[128]; char buffer[128];
size_t actual; size_t actual;
bh_io_t *io; BH_IO *io;
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_read(io, buffer, 128, &actual) == BH_OK); BH_VERIFY(BH_IORead(io, buffer, 128, &actual) == BH_OK);
BH_VERIFY(actual == 0); BH_VERIFY(actual == 0);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_EOF); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_EOF);
bh_io_close(io); BH_IOClose(io);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -519,23 +519,23 @@ static int check_eof(void)
/** /**
* Check for error flags. * Check for error flags.
*/ */
static int check_error(void) static int checkError(void)
{ {
size_t actual; size_t actual;
bh_io_t *io; BH_IO *io;
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "12345", 5, &actual) != BH_OK); BH_VERIFY(BH_IOWrite(io, "12345", 5, &actual) != BH_OK);
BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_ERROR); BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_ERROR);
BH_VERIFY(bh_io_clear(io) == BH_OK); BH_VERIFY(BH_IOClear(io) == BH_OK);
BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_ERROR) == 0); BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_ERROR) == 0);
bh_io_close(io); BH_IOClose(io);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -544,42 +544,42 @@ static int check_error(void)
/** /**
* Check peek operation. * Check peek operation.
*/ */
static int check_peek(void) static int checkPeek(void)
{ {
int64_t previous, current; int64_t previous, current;
char buffer[128]; char buffer[128];
size_t actual; size_t actual;
bh_io_t *io; BH_IO *io;
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(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_IOOpen(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_IOFlags(io) & BH_IO_FLAG_OPEN);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) == BH_OK); BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10); BH_VERIFY(actual == 10);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_tell(io, &previous) == BH_OK); BH_VERIFY(BH_IOTell(io, &previous) == BH_OK);
BH_VERIFY(bh_io_peek(io, buffer, 128, &actual) == BH_OK); BH_VERIFY(BH_IOPeek(io, buffer, 128, &actual) == BH_OK);
BH_VERIFY(bh_io_tell(io, &current) == BH_OK); BH_VERIFY(BH_IOTell(io, &current) == BH_OK);
BH_VERIFY(actual == 20); BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "12345678901234567890", 20) == 0); BH_VERIFY(memcmp(buffer, "12345678901234567890", 20) == 0);
BH_VERIFY(previous == current); BH_VERIFY(previous == current);
BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK);
BH_VERIFY(bh_io_tell(io, &previous) == BH_OK); BH_VERIFY(BH_IOTell(io, &previous) == BH_OK);
BH_VERIFY(bh_io_peek(io, buffer, 128, &actual) == BH_OK); BH_VERIFY(BH_IOPeek(io, buffer, 128, &actual) == BH_OK);
BH_VERIFY(bh_io_tell(io, &current) == BH_OK); BH_VERIFY(BH_IOTell(io, &current) == BH_OK);
BH_VERIFY(actual == 20); BH_VERIFY(actual == 20);
BH_VERIFY(memcmp(buffer, "12345678901234567890", 20) == 0); BH_VERIFY(memcmp(buffer, "12345678901234567890", 20) == 0);
BH_VERIFY(previous == current); BH_VERIFY(previous == current);
bh_io_close(io); BH_IOClose(io);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -588,20 +588,20 @@ static int check_peek(void)
/** /**
* Check file size operation. * Check file size operation.
*/ */
static int check_size(void) static int checkSize(void)
{ {
bh_io_t *io; BH_IO *io;
int64_t size; int64_t size;
BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL);
BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0);
BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK); BH_VERIFY(BH_IOOpen(io, BH_IO_READ) == BH_OK);
BH_VERIFY(bh_io_size(io, &size) == BH_OK); BH_VERIFY(BH_IOSize(io, &size) == BH_OK);
BH_VERIFY(size == 20); BH_VERIFY(size == 20);
bh_io_close(io); BH_IOClose(io);
bh_io_free(io); BH_IOFree(io);
return 0; return 0;
} }
@@ -615,16 +615,16 @@ int main(int argc,
/* Cleanup */ /* Cleanup */
cleanup(); cleanup();
bh_unit_add("null", check_null); BH_UnitAdd("Null", checkNull);
bh_unit_add("normal", check_normal); BH_UnitAdd("Normal", checkNormal);
bh_unit_add("truncate", check_truncate); BH_UnitAdd("Truncate", checkTruncate);
bh_unit_add("exist", check_exist); BH_UnitAdd("Exist", checkExist);
bh_unit_add("append", check_append); BH_UnitAdd("Append", checkAppend);
bh_unit_add("create", check_create); BH_UnitAdd("Create", checkCreate);
bh_unit_add("eof", check_eof); BH_UnitAdd("EOF", checkEOF);
bh_unit_add("error", check_error); BH_UnitAdd("Error", checkError);
bh_unit_add("peek", check_peek); BH_UnitAdd("Peek", checkPeek);
bh_unit_add("size", check_size); BH_UnitAdd("Size", checkSize);
return bh_unit_run(); return BH_UnitRun();
} }

View File

@@ -2,161 +2,161 @@
#include <bh/unit.h> #include <bh/unit.h>
static size_t direct_hash(const void *ptr) static size_t DBG_PtrIntHash(const void *ptr)
{ {
return BH_PTR2INT(ptr); return BH_PTR2INT(ptr);
} }
static int direct_equal(const void *lhs, const void *rhs) static int DBG_PtrIntEqual(const void *lhs, const void *rhs)
{ {
return BH_PTR2INT(lhs) - BH_PTR2INT(rhs); return BH_PTR2INT(lhs) - BH_PTR2INT(rhs);
} }
static int new_free(void) static int NewFree(void)
{ {
bh_hashmap_t *hashmap; BH_Hashmap *hashmap;
hashmap = bh_hashmap_new(direct_equal, direct_hash); hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
BH_VERIFY(hashmap != NULL); BH_VERIFY(hashmap != NULL);
BH_VERIFY(bh_hashmap_empty(hashmap) != 0); BH_VERIFY(BH_HashmapEmpty(hashmap) != 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 0); BH_VERIFY(BH_HashmapSize(hashmap) == 0);
BH_VERIFY(bh_hashmap_capacity(hashmap) == 0); BH_VERIFY(BH_HashmapCapacity(hashmap) == 0);
BH_VERIFY(bh_hashmap_factor(hashmap) >= 0.15f); BH_VERIFY(BH_HashmapFactor(hashmap) >= 0.15f);
BH_VERIFY(bh_hashmap_factor(hashmap) <= 1.0f); BH_VERIFY(BH_HashmapFactor(hashmap) <= 1.0f);
bh_hashmap_free(hashmap); BH_HashmapFree(hashmap);
return 0; return 0;
} }
static int grow_shrink(void) static int GrowShrink(void)
{ {
bh_hashmap_t *hashmap; BH_Hashmap *hashmap;
void *iter; void *iter;
hashmap = bh_hashmap_new(direct_equal, direct_hash); hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
BH_VERIFY(hashmap != NULL); BH_VERIFY(hashmap != NULL);
/* Allocate space for 1024 entries and insert 1 element */ /* Allocate space for 1024 entries and insert 1 element */
BH_VERIFY(bh_hashmap_reserve(hashmap, 1024) == 0); BH_VERIFY(BH_HashmapReserve(hashmap, 1024) == 0);
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(1337), BH_INT2PTR(80085)) == 0); BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(1337), BH_INT2PTR(80085)) == 0);
/* Check hashmap contents */ /* Check hashmap contents */
iter = bh_hashmap_iter_next(hashmap, NULL); iter = BH_HashmapIterNext(hashmap, NULL);
BH_VERIFY(iter != NULL); BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337); BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085); BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085);
BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL); BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL);
BH_VERIFY(bh_hashmap_empty(hashmap) == 0); BH_VERIFY(BH_HashmapEmpty(hashmap) == 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 1); BH_VERIFY(BH_HashmapSize(hashmap) == 1);
BH_VERIFY(bh_hashmap_capacity(hashmap) >= 1024); BH_VERIFY(BH_HashmapCapacity(hashmap) >= 1024);
/* Change factor and grow */ /* Change factor and grow */
bh_hashmap_set_factor(hashmap, 0.35f); BH_HashmapSetFactor(hashmap, 0.35f);
/* Check hashmap contents */ /* Check hashmap contents */
iter = bh_hashmap_iter_next(hashmap, NULL); iter = BH_HashmapIterNext(hashmap, NULL);
BH_VERIFY(iter != NULL); BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337); BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085); BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085);
BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL); BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL);
BH_VERIFY(bh_hashmap_reserve(hashmap, 8192) == 0); BH_VERIFY(BH_HashmapReserve(hashmap, 8192) == 0);
BH_VERIFY(bh_hashmap_empty(hashmap) == 0); BH_VERIFY(BH_HashmapEmpty(hashmap) == 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 1); BH_VERIFY(BH_HashmapSize(hashmap) == 1);
BH_VERIFY(bh_hashmap_capacity(hashmap) >= 8192); BH_VERIFY(BH_HashmapCapacity(hashmap) >= 8192);
BH_VERIFY(bh_hashmap_factor(hashmap) == 0.35f); BH_VERIFY_DELTA(BH_HashmapFactor(hashmap), 0.35f, 0.001f);
/* Shrink */ /* Shrink */
BH_VERIFY(bh_hashmap_reserve(hashmap, 0) == 0); BH_VERIFY(BH_HashmapReserve(hashmap, 0) == 0);
/* Check hashmap contents */ /* Check hashmap contents */
iter = bh_hashmap_iter_next(hashmap, NULL); iter = BH_HashmapIterNext(hashmap, NULL);
BH_VERIFY(iter != NULL); BH_VERIFY(iter != NULL);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337); BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337);
BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085); BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085);
BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL); BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL);
BH_VERIFY(bh_hashmap_empty(hashmap) == 0); BH_VERIFY(BH_HashmapEmpty(hashmap) == 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 1); BH_VERIFY(BH_HashmapSize(hashmap) == 1);
BH_VERIFY(bh_hashmap_capacity(hashmap) >= 1); BH_VERIFY(BH_HashmapCapacity(hashmap) >= 1);
BH_VERIFY(bh_hashmap_capacity(hashmap) < 8192); BH_VERIFY(BH_HashmapCapacity(hashmap) < 8192);
BH_VERIFY(bh_hashmap_factor(hashmap) == 0.35f); BH_VERIFY_DELTA(BH_HashmapFactor(hashmap), 0.35f, 0.001f);
/* Shrink to 0 (deallocate) */ /* Shrink to 0 (deallocate) */
bh_hashmap_clear(hashmap); BH_HashmapClear(hashmap);
BH_VERIFY(bh_hashmap_empty(hashmap) != 0); BH_VERIFY(BH_HashmapEmpty(hashmap) != 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 0); BH_VERIFY(BH_HashmapSize(hashmap) == 0);
BH_VERIFY(bh_hashmap_capacity(hashmap) > 0); BH_VERIFY(BH_HashmapCapacity(hashmap) > 0);
BH_VERIFY(bh_hashmap_reserve(hashmap, 0) == 0); BH_VERIFY(BH_HashmapReserve(hashmap, 0) == 0);
BH_VERIFY(bh_hashmap_empty(hashmap) != 0); BH_VERIFY(BH_HashmapEmpty(hashmap) != 0);
BH_VERIFY(bh_hashmap_size(hashmap) == 0); BH_VERIFY(BH_HashmapSize(hashmap) == 0);
BH_VERIFY(bh_hashmap_capacity(hashmap) == 0); BH_VERIFY(BH_HashmapCapacity(hashmap) == 0);
/* Check hashmap contents */ /* Check hashmap contents */
iter = bh_hashmap_iter_next(hashmap, NULL); iter = BH_HashmapIterNext(hashmap, NULL);
BH_VERIFY(iter == NULL); BH_VERIFY(iter == NULL);
bh_hashmap_free(hashmap); BH_HashmapFree(hashmap);
return 0; return 0;
} }
static int insert_remove(void) static int InsertRemove(void)
{ {
bh_hashmap_t *hashmap; BH_Hashmap *hashmap;
size_t i, added, removed; size_t i, added, removed;
void *iter; void *iter;
hashmap = bh_hashmap_new(direct_equal, direct_hash); hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
BH_VERIFY(hashmap != NULL); BH_VERIFY(hashmap != NULL);
bh_hashmap_set_factor(hashmap, 1.0f); BH_HashmapSetFactor(hashmap, 1.0f);
/* Insert elements into hashmap */ /* Insert elements into hashmap */
added = 0; added = 0;
for (i = 1024; i > 0; i--) for (i = 1024; i > 0; i--)
{ {
added += (i - 1) / 4; added += (i - 1) / 4;
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR((i - 1) / 4), BH_INT2PTR(i)) == 0); BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR((i - 1) / 4), BH_INT2PTR(i)) == 0);
} }
/* Remove elements */ /* Remove elements */
iter = bh_hashmap_iter_next(hashmap, NULL); iter = BH_HashmapIterNext(hashmap, NULL);
removed = 0; removed = 0;
while (iter) while (iter)
{ {
removed += BH_PTR2INT(bh_hashmap_iter_key(iter)); removed += BH_PTR2INT(BH_HashmapIterKey(iter));
bh_hashmap_iter_remove(hashmap, iter); BH_HashmapIterRemove(hashmap, iter);
iter = bh_hashmap_iter_next(hashmap, NULL); iter = BH_HashmapIterNext(hashmap, NULL);
} }
/* Check inserted elements are equal to removed */ /* Check inserted elements are equal to removed */
BH_VERIFY(added == removed); BH_VERIFY(added == removed);
bh_hashmap_free(hashmap); BH_HashmapFree(hashmap);
return 0; return 0;
} }
static int lookup(void) static int Lookup(void)
{ {
bh_hashmap_t *hashmap; BH_Hashmap *hashmap;
size_t i; size_t i;
hashmap = bh_hashmap_new(direct_equal, direct_hash); hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
BH_VERIFY(hashmap != NULL); BH_VERIFY(hashmap != NULL);
/* Insert elements into hashmap */ /* Insert elements into hashmap */
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(i * 4), BH_INT2PTR(i)) == 0); BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(i * 4), BH_INT2PTR(i)) == 0);
/* Lookup inserted elements */ /* Lookup inserted elements */
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
{ {
void *value; void *value;
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), NULL) == BH_OK); BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), NULL) == BH_OK);
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), &value) == BH_OK); BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == (int)i); BH_VERIFY(BH_PTR2INT(value) == (int)i);
} }
@@ -165,55 +165,55 @@ static int lookup(void)
{ {
void *value; void *value;
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), NULL) != BH_OK); BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), NULL) != BH_OK);
BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), &value) != BH_OK); BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), &value) != BH_OK);
} }
bh_hashmap_free(hashmap); BH_HashmapFree(hashmap);
return 0; return 0;
} }
static int clear(void) static int Clear(void)
{ {
bh_hashmap_t *hashmap; BH_Hashmap *hashmap;
size_t i; size_t i;
hashmap = bh_hashmap_new(direct_equal, direct_hash); hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
BH_VERIFY(hashmap != NULL); BH_VERIFY(hashmap != NULL);
/* Insert elements into hashmap */ /* Insert elements into hashmap */
for (i = 0; i < 128; i++) for (i = 0; i < 128; i++)
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(i), 0) == 0); BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(i), 0) == 0);
bh_hashmap_clear(hashmap); BH_HashmapClear(hashmap);
/* Remove non-existing elements */ /* Remove non-existing elements */
for (i = 0; i < 128; i++) for (i = 0; i < 128; i++)
bh_hashmap_remove(hashmap, BH_INT2PTR(i)); BH_HashmapRemove(hashmap, BH_INT2PTR(i));
bh_hashmap_free(hashmap); BH_HashmapFree(hashmap);
return 0; return 0;
} }
static int fields(void) static int Fields(void)
{ {
bh_hashmap_t *hashmap; BH_Hashmap *hashmap;
size_t i; size_t i;
hashmap = bh_hashmap_new(direct_equal, direct_hash); hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash);
BH_VERIFY(hashmap != NULL); BH_VERIFY(hashmap != NULL);
BH_VERIFY(bh_hashmap_empty(hashmap) == 1); BH_VERIFY(BH_HashmapEmpty(hashmap) == 1);
/* Insert elements into hashmap */ /* Insert elements into hashmap */
for (i = 0; i < 14; i++) for (i = 0; i < 14; i++)
BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(i), NULL) == 0); BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(i), NULL) == 0);
/* Check hashmap fields correspond to getter functions */ /* Check hashmap fields correspond to getter functions */
BH_VERIFY(bh_hashmap_size(hashmap) == 14); BH_VERIFY(BH_HashmapSize(hashmap) == 14);
BH_VERIFY(bh_hashmap_capacity(hashmap) >= 14); BH_VERIFY(BH_HashmapCapacity(hashmap) >= 14);
BH_VERIFY(bh_hashmap_empty(hashmap) == 0); BH_VERIFY(BH_HashmapEmpty(hashmap) == 0);
bh_hashmap_free(hashmap); BH_HashmapFree(hashmap);
return 0; return 0;
} }
@@ -223,12 +223,12 @@ int main(int argc, char **argv)
(void)argv; (void)argv;
/* Add unit tests */ /* Add unit tests */
bh_unit_add("new_free", new_free); BH_UnitAdd("NewFree", NewFree);
bh_unit_add("grow_shrink", grow_shrink); BH_UnitAdd("GrowShrink", GrowShrink);
bh_unit_add("insert_remove", insert_remove); BH_UnitAdd("InsertRemove", InsertRemove);
bh_unit_add("lookup", lookup); BH_UnitAdd("Lookup", Lookup);
bh_unit_add("clear", clear); BH_UnitAdd("Clear", Clear);
bh_unit_add("fields", fields); BH_UnitAdd("Fields", Fields);
return bh_unit_run(); return BH_UnitRun();
} }

File diff suppressed because it is too large Load Diff

View File

@@ -2,156 +2,156 @@
#include <bh/unit.h> #include <bh/unit.h>
static int new_free(void) static int NewFree(void)
{ {
bh_queue_t *queue; BH_Queue *queue;
queue = bh_queue_new(); queue = BH_QueueNew();
BH_VERIFY(queue != NULL); BH_VERIFY(queue != NULL);
BH_VERIFY(bh_queue_size(queue) == 0); BH_VERIFY(BH_QueueSize(queue) == 0);
BH_VERIFY(bh_queue_capacity(queue) == 0); BH_VERIFY(BH_QueueCapacity(queue) == 0);
BH_VERIFY(bh_queue_empty(queue) != 0); BH_VERIFY(BH_QueueEmpty(queue) != 0);
bh_queue_free(queue); BH_QueueFree(queue);
return 0; return 0;
} }
static int grow_shrink(void) static int GrowShrink(void)
{ {
bh_queue_t *queue; BH_Queue *queue;
void *value; void *value;
queue = bh_queue_new(); queue = BH_QueueNew();
BH_VERIFY(queue != NULL); BH_VERIFY(queue != NULL);
/* Reserve 1024 elements and insert item into queue */ /* Reserve 1024 elements and insert item into queue */
BH_VERIFY(bh_queue_reserve(queue, 1024) == BH_OK); BH_VERIFY(BH_QueueReserve(queue, 1024) == BH_OK);
BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(1337)) == BH_OK); BH_VERIFY(BH_QueueInsert(queue, BH_INT2PTR(1337)) == BH_OK);
BH_VERIFY(bh_queue_capacity(queue) >= 1024); BH_VERIFY(BH_QueueCapacity(queue) >= 1024);
BH_VERIFY(bh_queue_size(queue) == 1); BH_VERIFY(BH_QueueSize(queue) == 1);
BH_VERIFY(bh_queue_empty(queue) == 0); BH_VERIFY(BH_QueueEmpty(queue) == 0);
/* Check queue content */ /* Check queue content */
BH_VERIFY(bh_queue_front(queue, &value) == BH_OK); BH_VERIFY(BH_QueueFront(queue, &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == 1337); BH_VERIFY(BH_PTR2INT(value) == 1337);
/* Grow queue */ /* Grow queue */
BH_VERIFY(bh_queue_reserve(queue, 8192) == BH_OK); BH_VERIFY(BH_QueueReserve(queue, 8192) == BH_OK);
BH_VERIFY(bh_queue_capacity(queue) >= 8192); BH_VERIFY(BH_QueueCapacity(queue) >= 8192);
BH_VERIFY(bh_queue_size(queue) == 1); BH_VERIFY(BH_QueueSize(queue) == 1);
BH_VERIFY(bh_queue_empty(queue) == 0); BH_VERIFY(BH_QueueEmpty(queue) == 0);
/* Check queue content */ /* Check queue content */
BH_VERIFY(bh_queue_front(queue, &value) == BH_OK); BH_VERIFY(BH_QueueFront(queue, &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == 1337); BH_VERIFY(BH_PTR2INT(value) == 1337);
/* Shrink the queue */ /* Shrink the queue */
BH_VERIFY(bh_queue_reserve(queue, 0) == BH_OK); BH_VERIFY(BH_QueueReserve(queue, 0) == BH_OK);
BH_VERIFY(bh_queue_capacity(queue) >= 1); BH_VERIFY(BH_QueueCapacity(queue) >= 1);
BH_VERIFY(bh_queue_capacity(queue) < 8192); BH_VERIFY(BH_QueueCapacity(queue) < 8192);
BH_VERIFY(bh_queue_size(queue) == 1); BH_VERIFY(BH_QueueSize(queue) == 1);
BH_VERIFY(bh_queue_empty(queue) == 0); BH_VERIFY(BH_QueueEmpty(queue) == 0);
/* Check queue content */ /* Check queue content */
BH_VERIFY(bh_queue_front(queue, &value) == BH_OK); BH_VERIFY(BH_QueueFront(queue, &value) == BH_OK);
BH_VERIFY(BH_PTR2INT(value) == 1337); BH_VERIFY(BH_PTR2INT(value) == 1337);
/* Shrink to 0 (deallocate) */ /* Shrink to 0 (deallocate) */
bh_queue_clear(queue); BH_QueueClear(queue);
BH_VERIFY(bh_queue_size(queue) == 0); BH_VERIFY(BH_QueueSize(queue) == 0);
BH_VERIFY(bh_queue_empty(queue) != 0); BH_VERIFY(BH_QueueEmpty(queue) != 0);
BH_VERIFY(bh_queue_capacity(queue) >= 1); BH_VERIFY(BH_QueueCapacity(queue) >= 1);
BH_VERIFY(bh_queue_reserve(queue, 0) == BH_OK); BH_VERIFY(BH_QueueReserve(queue, 0) == BH_OK);
BH_VERIFY(bh_queue_size(queue) == 0); BH_VERIFY(BH_QueueSize(queue) == 0);
BH_VERIFY(bh_queue_empty(queue) != 0); BH_VERIFY(BH_QueueEmpty(queue) != 0);
BH_VERIFY(bh_queue_capacity(queue) == 0); BH_VERIFY(BH_QueueCapacity(queue) == 0);
bh_queue_free(queue); BH_QueueFree(queue);
return 0; return 0;
} }
static int insert_remove(void) static int InsertRemove(void)
{ {
bh_queue_t *queue; BH_Queue *queue;
size_t i, added, removed; size_t i, added, removed;
void *iter; void *iter;
queue = bh_queue_new(); queue = BH_QueueNew();
BH_VERIFY(queue != NULL); BH_VERIFY(queue != NULL);
added = 0; added = 0;
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
{ {
added += i * 2; added += i * 2;
BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(i * 2)) == BH_OK); BH_VERIFY(BH_QueueInsert(queue, BH_INT2PTR(i * 2)) == BH_OK);
} }
removed = 0; removed = 0;
iter = bh_queue_iter_next(queue, NULL); iter = BH_QueueIterNext(queue, NULL);
while (iter) while (iter)
{ {
void *value; void *value;
BH_VERIFY(bh_queue_front(queue, &value) == BH_OK); BH_VERIFY(BH_QueueFront(queue, &value) == BH_OK);
removed += BH_PTR2INT(value); removed += BH_PTR2INT(value);
bh_queue_remove(queue); BH_QueueRemove(queue);
iter = bh_queue_iter_next(queue, NULL); iter = BH_QueueIterNext(queue, NULL);
} }
BH_VERIFY(added == removed); BH_VERIFY(added == removed);
BH_VERIFY(bh_queue_empty(queue) != 0); BH_VERIFY(BH_QueueEmpty(queue) != 0);
BH_VERIFY(bh_queue_size(queue) == 0); BH_VERIFY(BH_QueueSize(queue) == 0);
bh_queue_free(queue); BH_QueueFree(queue);
return 0; return 0;
} }
static int rollover(void) static int Rollover(void)
{ {
bh_queue_t *queue; BH_Queue *queue;
size_t i, j, capacity; size_t i, j, capacity;
queue = bh_queue_new(); queue = BH_QueueNew();
BH_VERIFY(queue != NULL); BH_VERIFY(queue != NULL);
BH_VERIFY(bh_queue_reserve(queue, 128) == 0); BH_VERIFY(BH_QueueReserve(queue, 128) == 0);
capacity = bh_queue_capacity(queue); capacity = BH_QueueCapacity(queue);
for (i = 0; i < 128; i++) for (i = 0; i < 128; i++)
{ {
for (j = 0; j < 3; j++) for (j = 0; j < 3; j++)
bh_queue_remove(queue); BH_QueueRemove(queue);
for (j = 0; j < 4 && bh_queue_size(queue) < 128; j++) for (j = 0; j < 4 && BH_QueueSize(queue) < 128; j++)
BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(i * 4 + j)) == 0); BH_VERIFY(BH_QueueInsert(queue, BH_INT2PTR(i * 4 + j)) == 0);
} }
BH_VERIFY(bh_queue_size(queue) == 128); BH_VERIFY(BH_QueueSize(queue) == 128);
BH_VERIFY(bh_queue_capacity(queue) == capacity); BH_VERIFY(BH_QueueCapacity(queue) == capacity);
bh_queue_free(queue); BH_QueueFree(queue);
return 0; return 0;
} }
static int fields(void) static int Fields(void)
{ {
bh_queue_t *queue; BH_Queue *queue;
queue = bh_queue_new(); queue = BH_QueueNew();
BH_VERIFY(queue != NULL); BH_VERIFY(queue != NULL);
BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(1337)) == 0); BH_VERIFY(BH_QueueInsert(queue, BH_INT2PTR(1337)) == 0);
BH_VERIFY(bh_queue_size(queue) == 1); BH_VERIFY(BH_QueueSize(queue) == 1);
BH_VERIFY(bh_queue_empty(queue) == 0); BH_VERIFY(BH_QueueEmpty(queue) == 0);
BH_VERIFY(bh_queue_capacity(queue) >= 1); BH_VERIFY(BH_QueueCapacity(queue) >= 1);
bh_queue_free(queue); BH_QueueFree(queue);
return 0; return 0;
} }
@@ -161,11 +161,11 @@ int main(int argc, char **argv)
(void)argc; (void)argc;
(void)argv; (void)argv;
bh_unit_add("new_free", new_free); BH_UnitAdd("NewFree", NewFree);
bh_unit_add("grow_shrink", grow_shrink); BH_UnitAdd("GrowShrink", GrowShrink);
bh_unit_add("insert_remove", insert_remove); BH_UnitAdd("InsertRemove", InsertRemove);
bh_unit_add("rollover", rollover); BH_UnitAdd("Rollover", Rollover);
bh_unit_add("fields", fields); BH_UnitAdd("Fields", Fields);
return bh_unit_run(); return BH_UnitRun();
} }

View File

@@ -4,7 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
typedef int (*bh_unit_cb_t)(void); typedef int (*BH_UnitCallback)(void);
#define BH_VERIFY(e) \ #define BH_VERIFY(e) \
do { \ do { \
@@ -34,7 +34,23 @@ typedef int (*bh_unit_cb_t)(void);
} while(0) } while(0)
void bh_unit_add(const char *name, bh_unit_cb_t func);
int bh_unit_run(void); /**
* Adds unit test \a cb with name \a name for the testing.
*
* \param name Unit test name
* \param cb Unit test function
*/
void BH_UnitAdd(const char *name,
BH_UnitCallback cb);
/**
* Runs unit tests.
*
* \return On success, returns zero.
* \return On failure, returns error code.
*/
int BH_UnitRun(void);
#endif /* BH_UNIT_H */ #endif /* BH_UNIT_H */

View File

@@ -1,27 +1,31 @@
#include <bh/unit.h> #include <bh/unit.h>
#include <stdlib.h> #include <stdlib.h>
typedef struct bh_unit_s typedef struct BH_Unit
{ {
struct bh_unit_s *next; struct BH_Unit *next;
const char *name; const char *name;
bh_unit_cb_t func; BH_UnitCallback cb;
} bh_unit_t; } BH_Unit;
static bh_unit_t *root = NULL;
void bh_unit_add(const char *name, bh_unit_cb_t func) static BH_Unit *root = NULL;
void BH_UnitAdd(const char *name, BH_UnitCallback cb)
{ {
bh_unit_t *unit, *current; BH_Unit *unit, *current;
/* Allocate and fill new unit test entry */
unit = malloc(sizeof(*unit)); unit = malloc(sizeof(*unit));
if (!unit) if (!unit)
return; return;
unit->name = name;
unit->func = func;
unit->next = NULL; unit->next = NULL;
unit->name = name;
unit->cb = cb;
/* Append unit test entry */
current = root; current = root;
while (current && current->next) while (current && current->next)
current = current->next; current = current->next;
@@ -32,16 +36,17 @@ void bh_unit_add(const char *name, bh_unit_cb_t func)
root = unit; root = unit;
} }
int bh_unit_run(void)
int BH_UnitRun(void)
{ {
bh_unit_t *current; BH_Unit *current;
printf("Running tests...\n"); printf("Running tests...\n");
current = root; current = root;
while (current) while (current)
{ {
printf("%s\n", current->name); printf("%s\n", current->name);
if (current->func()) if (current->cb())
{ {
printf("\tFAIL\n"); printf("\tFAIL\n");
return -1; return -1;