From c89cf8f3165fa8c60b2d945716d071f390add973 Mon Sep 17 00:00:00 2001 From: Mikhail Romanko Date: Thu, 30 Jan 2025 13:53:26 +0300 Subject: 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. --- include/bh/algo.h | 50 +- include/bh/common.h | 5 +- include/bh/hashmap.h | 58 +- include/bh/io.h | 69 +- include/bh/math.h | 2089 ++++++++++++++++++------------------------------ include/bh/queue.h | 34 +- main.c | 9 +- src/algo.c | 110 +-- src/dummy/file.c | 186 +++-- src/hashmap.c | 138 ++-- src/io.c | 97 ++- src/math.c | 1924 ++++++++++++++++++++------------------------ src/posix/file.c | 166 ++-- src/queue.c | 54 +- src/win32/file.c | 164 ++-- test/src/testalgo.c | 283 +++---- test/src/testfile.c | 572 ++++++------- test/src/testhashmap.c | 192 ++--- test/src/testmath.c | 1290 ++++++++++++++---------------- test/src/testqueue.c | 144 ++-- unit/include/bh/unit.h | 22 +- unit/src/unit.c | 29 +- 22 files changed, 3475 insertions(+), 4210 deletions(-) diff --git a/include/bh/algo.h b/include/bh/algo.h index f549f1a..5d9bb36 100755 --- a/include/bh/algo.h +++ b/include/bh/algo.h @@ -12,7 +12,7 @@ * \param src Pointer to the element * \param size Element size in bytes */ -void bh_swap(void *dest, +void BH_Swap(void *dest, void *src, size_t size); @@ -30,11 +30,11 @@ void bh_swap(void *dest, * * \return Pointer to the first element of the second partition. */ -void *bh_partition(void *pivot, +void *BH_Partition(void *pivot, void *array, size_t size, 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 equal Comparision function */ -void bh_sort(void *array, +void BH_Sort(void *array, size_t size, 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 equal Comparision function */ -void bh_heap_make(void *array, - size_t size, - size_t element, - bh_equal_cb_t equal); +void BH_HeapMake(void *array, + size_t size, + size_t element, + BH_EqualCallback equal); /** @@ -73,10 +73,10 @@ void bh_heap_make(void *array, * \param element Element size in bytes * \param equal Comparasion function */ -void bh_heap_remove(void *array, - size_t size, - size_t element, - bh_equal_cb_t equal); +void BH_HeapRemove(void *array, + size_t size, + size_t element, + BH_EqualCallback equal); /** @@ -91,11 +91,11 @@ void bh_heap_remove(void *array, * \param element Element size in bytes * \param equal Comparasion function */ -void bh_heap_insert(void *value, - void *array, - size_t size, - size_t element, - bh_equal_cb_t equal); +void BH_HeapInsert(void *value, + void *array, + size_t size, + size_t element, + BH_EqualCallback equal); /** @@ -106,8 +106,8 @@ void bh_heap_insert(void *value, * * This function is roughly equivalent to the following code: * \code - * bh_heap_remove(array, size, element, equal); - * bh_heap_insert(value, array, size - 1, element, equal); + * BH_HeapRemove(array, size, element, equal); + * BH_HeapInsert(value, array, size - 1, element, equal); * \endcode * * \param value Pointer to the value @@ -116,11 +116,11 @@ void bh_heap_insert(void *value, * \param element Element size in bytes * \param equal Comparasion function */ -void bh_heap_replace(void *value, - void *array, - size_t size, - size_t element, - bh_equal_cb_t equal); +void BH_HeapReplace(void *value, + void *array, + size_t size, + size_t element, + BH_EqualCallback equal); #endif /* BH_ALGO_H */ diff --git a/include/bh/common.h b/include/bh/common.h index 6f4ac88..d4aadbe 100644 --- a/include/bh/common.h +++ b/include/bh/common.h @@ -17,8 +17,9 @@ #define BH_PTR2INT(x) ((intptr_t)(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 */ diff --git a/include/bh/hashmap.h b/include/bh/hashmap.h index da89609..d3710e4 100755 --- a/include/bh/hashmap.h +++ b/include/bh/hashmap.h @@ -5,7 +5,7 @@ #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 failure, returns a null pointer. */ -bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal, - bh_hash_cb_t hash); +BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal, + BH_HashCallback hash); /** @@ -26,7 +26,7 @@ bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal, * * \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 */ -void bh_hashmap_clear(bh_hashmap_t *hashmap); +void BH_HashmapClear(BH_Hashmap *hashmap); /** @@ -52,8 +52,8 @@ void bh_hashmap_clear(bh_hashmap_t *hashmap); * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_hashmap_reserve(bh_hashmap_t *hashmap, - size_t size); +int BH_HashmapReserve(BH_Hashmap *hashmap, + size_t size); /** @@ -69,9 +69,9 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_hashmap_insert(bh_hashmap_t *hashmap, - void *key, - void *value); +int BH_HashmapInsert(BH_Hashmap *hashmap, + void *key, + void *value); /** @@ -84,8 +84,8 @@ int bh_hashmap_insert(bh_hashmap_t *hashmap, * \note If hashmap contains several elements with the same key, this function * will remove only one key-value pair. */ -void bh_hashmap_remove(bh_hashmap_t *hashmap, - void *key); +void BH_HashmapRemove(BH_Hashmap *hashmap, + void *key); /** @@ -98,9 +98,9 @@ void bh_hashmap_remove(bh_hashmap_t *hashmap, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_hashmap_at(bh_hashmap_t *hashmap, - void *key, - void **value); +int BH_HashmapAt(BH_Hashmap *hashmap, + void *key, + 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 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. */ -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. */ -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. */ -float bh_hashmap_factor(bh_hashmap_t *hashmap); +float BH_HashmapFactor(BH_Hashmap *hashmap); /** @@ -152,8 +152,8 @@ float bh_hashmap_factor(bh_hashmap_t *hashmap); * * \note New load factor will be applied on the next reserve/insert operation. */ -void bh_hashmap_set_factor(bh_hashmap_t *hashmap, - float factor); +void BH_HashmapSetFactor(BH_Hashmap *hashmap, + float factor); /** @@ -168,8 +168,8 @@ void bh_hashmap_set_factor(bh_hashmap_t *hashmap, * \note If hashmap contains several elements with the same key, this function * will return iterator to one of them */ -void *bh_hashmap_iter_at(bh_hashmap_t *hashmap, - void *key); +void *BH_HashmapIterAt(BH_Hashmap *hashmap, + void *key); /** @@ -184,8 +184,8 @@ void *bh_hashmap_iter_at(bh_hashmap_t *hashmap, * \return On success, returns new iterator value for the next element. * \return On failure, returns NULL pointer. */ -void *bh_hashmap_iter_next(bh_hashmap_t *hashmap, - void *iter); +void *BH_HashmapIterNext(BH_Hashmap *hashmap, + void *iter); /** @@ -196,8 +196,8 @@ void *bh_hashmap_iter_next(bh_hashmap_t *hashmap, * * \note Calling this function will invalidate iterators. */ -void bh_hashmap_iter_remove(bh_hashmap_t *hashmap, - void *iter); +void BH_HashmapIterRemove(BH_Hashmap *hashmap, + void *iter); /** @@ -207,7 +207,7 @@ void bh_hashmap_iter_remove(bh_hashmap_t *hashmap, * * \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. */ -void *bh_hashmap_iter_value(void *iter); +void *BH_HashmapIterValue(void *iter); #endif /* BH_HASHMAP_H */ diff --git a/include/bh/io.h b/include/bh/io.h index 1d964bf..ee2da95 100644 --- a/include/bh/io.h +++ b/include/bh/io.h @@ -40,10 +40,11 @@ #define BH_IO_FLAG_EOF 0x0002 #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 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 failure, returns NULL pointer. */ -bh_io_t *bh_buffer_new(bh_io_t *io); +BH_IO *BH_BufferNew(BH_IO *io); /** @@ -77,8 +78,8 @@ bh_io_t *bh_buffer_new(bh_io_t *io); * \return On success, returns IO handle. * \return On failure, returns NULL pointer. */ -bh_io_t *bh_io_new(bh_io_func_t func, - void *data); +BH_IO *BH_IONew(BH_IOCallback cb, + void *data); /** @@ -86,7 +87,7 @@ bh_io_t *bh_io_new(bh_io_func_t func, * * \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 failure, returns NULL pointer */ -const char *bh_io_classname(bh_io_t *io); +const char *BH_IOClassname(BH_IO* io); /** @@ -109,8 +110,8 @@ const char *bh_io_classname(bh_io_t *io); * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_open(bh_io_t *io, - int mode); +int BH_IOOpen(BH_IO *io, + int mode); /** @@ -121,7 +122,7 @@ int bh_io_open(bh_io_t *io, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_close(bh_io_t *io); +int BH_IOClose(BH_IO *io); /** @@ -135,10 +136,10 @@ int bh_io_close(bh_io_t *io); * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_read(bh_io_t *io, - char *buffer, - size_t size, - size_t *actual); +int BH_IORead(BH_IO *io, + char *buffer, + size_t size, + size_t *actual); /** @@ -152,10 +153,10 @@ int bh_io_read(bh_io_t *io, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_write(bh_io_t *io, - const char *buffer, - size_t size, - size_t *actual); +int BH_IOWrite(BH_IO *io, + const char *buffer, + size_t size, + size_t *actual); /** @@ -169,10 +170,10 @@ int bh_io_write(bh_io_t *io, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_peek(bh_io_t *io, - char *buffer, - size_t size, - size_t *actual); +int BH_IOPeek(BH_IO *io, + char *buffer, + size_t size, + size_t *actual); /** @@ -184,8 +185,8 @@ int bh_io_peek(bh_io_t *io, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_tell(bh_io_t *io, - int64_t *position); +int BH_IOTell(BH_IO *io, + int64_t *position); /** @@ -198,9 +199,9 @@ int bh_io_tell(bh_io_t *io, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_seek(bh_io_t *io, - int64_t position, - int direction); +int BH_IOSeek(BH_IO *io, + int64_t position, + int direction); /** @@ -211,7 +212,7 @@ int bh_io_seek(bh_io_t *io, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_flush(bh_io_t *io); +int BH_IOFlush(BH_IO *io); /** @@ -223,8 +224,8 @@ int bh_io_flush(bh_io_t *io); * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_io_size(bh_io_t *io, - int64_t *size); +int BH_IOSize(BH_IO *io, + int64_t *size); /** @@ -234,7 +235,7 @@ int bh_io_size(bh_io_t *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 failure, returns error code. */ -int bh_io_clear(bh_io_t *io); +int BH_IOClear(BH_IO *io); #endif /* BH_IO_H */ diff --git a/include/bh/math.h b/include/bh/math.h index 25aef35..5471a4a 100644 --- a/include/bh/math.h +++ b/include/bh/math.h @@ -6,1888 +6,1395 @@ /** - * 2D floating point vector. - */ -typedef struct bh_point2f_s -{ - /** X component */ - float x; - - /** Y Component */ - float y; -} bh_point2f_t; - - -/** - * 3D floating point vector. - */ -typedef struct bh_point3f_s -{ - /** X component */ - float x; - - /** Y component */ - float y; - - /** Z component */ - float z; -} bh_point3f_t; - - -/** - * 4D floating point vector. - */ -typedef struct bh_point4f_s -{ - /** X component */ - float x; - - /** Y component */ - float y; - - /** Z component */ - float z; - - /** W component */ - float w; -} bh_point4f_t; - - -/** - * 2D integer vector. - */ -typedef struct bh_point2i_s -{ - /** X component */ - int x; - - /** Y component */ - int y; -} bh_point2i_t; - - -/** - * 3D integer vector. - */ -typedef struct bh_point3i_s -{ - /** X component */ - int x; - - /** Y component */ - int y; - - /** Z component */ - int z; -} bh_point3i_t; - - -/** - * 4D integer vector. - */ -typedef struct bh_point4i_s -{ - /** X component */ - int x; - - /** Y component */ - int y; - - /** Z component */ - int z; - - /** W component */ - int w; -} bh_point4i_t; - - -/** - * 3x3 floating point matrix. - * - * Each vector represent one column of the matrix. - */ -typedef struct bh_matrix3f_s -{ - /** First column */ - bh_point3f_t x; - - /** Second column */ - bh_point3f_t y; - - /** Third colum */ - bh_point3f_t z; -} bh_matrix3f_t; - - -/** - * 4x4 floating point matrix. - * - * Each vector represent one column of the matrix. - */ -typedef struct bh_matrix4f_s -{ - /** First column */ - bh_point4f_t x; - - /** Second column */ - bh_point4f_t y; - - /** Third column */ - bh_point4f_t z; - - /** Forth column */ - bh_point4f_t w; -} bh_matrix4f_t; - - -/** - * 2D floating point line. - */ -typedef struct bh_line2f_s -{ - /** Normal vector */ - bh_point2f_t normal; - - /** Distance from the origin */ - float distance; -} bh_line2f_t; - - -/** - * 3D floating point line. - */ -typedef struct bh_line3f_s -{ - /** Origin */ - bh_point3f_t origin; - - /** Normal vector */ - bh_point3f_t normal; -} bh_line3f_t; - - -/** - * 3D floating point plane. - */ -typedef struct bh_plane3f_s -{ - /** Normal vector */ - bh_point3f_t normal; - - /** Distance from the origin */ - float distance; -} bh_plane3f_t; - - -/** - * 2D floating point ray. - */ -typedef struct bh_ray2f_s -{ - /** Origin */ - bh_point2f_t origin; - - /** Normal or direction */ - bh_point2f_t normal; -} bh_ray2f_t; - - -/** - * 3D floating point ray. - */ -typedef bh_line3f_t bh_ray3f_t; - -/** - * 2D floating point AABB - */ -typedef struct bh_aabb2f_s -{ - /** Origin */ - bh_point2f_t origin; - - /** Size */ - bh_point2f_t size; -} bh_aabb2f_t; - - -/** - * 3D floating point AABB - */ -typedef struct bh_aabb3f_s -{ - /** Origin */ - bh_point3f_t origin; - - /** Size */ - bh_point3f_t size; -} bh_aabb3f_t; - - -/** - * 2D integer AABB - */ -typedef struct bh_aabb2i_s -{ - /** Origin */ - bh_point2i_t origin; - - /** Size */ - bh_point2i_t size; -} bh_aabb2i_t; - - -/** - * 3D integer AABB - */ -typedef struct bh_aabb3i_s -{ - /** Origin */ - bh_point3i_t origin; - - /** Size */ - bh_point3i_t size; -} bh_aabb3i_t; - - -/** - * Floating point quaternion - */ -typedef bh_point4f_t bh_quat_t; - - -/** - * Adds components of two vectors. - * - * result = a + b - * - * \param a Value - * \param b Value - * \param result Result - */ -void bh_point4f_add(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); - - -/** - * Subtracts components of two vectors. - * - * result = a - b - * - * \param a Value - * \param b Value - * \param result Result - */ -void bh_point4f_sub(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); - - -/** - * Multiplies components of two vectors. - * - * result = a * b - * - * \param a Value - * \param b Value - * \param result Result - */ -void bh_point4f_mul(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); - - -/** - * Scales the vector. - * - * result = a * b - * - * \param a Value - * \param b Factor - * \param result Result - */ -void bh_point4f_scale(const bh_point4f_t *a, - float b, - bh_point4f_t *result); - - -/** - * Multiplies and adds three vectors. - * - * result = a * b + c - * - * \param a Value - * \param b Value - * \param c Value - * \param result Result - */ -void bh_point4f_madd(const bh_point4f_t *a, - const bh_point4f_t *b, - const bh_point4f_t *c, - bh_point4f_t *result); - - -/** - * Negates the vector. - * - * result = -in + * Adds \a a and \a b floating point vectors and stores result into \a out. * - * \param in Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -void bh_point4f_negate(const bh_point4f_t *in, - bh_point4f_t *result); +void BH_Vec4fAdd(const float *a, + const float *b, + float *out); /** - * Calculates the dot product of two vectors. + * Subtracts \a a and \a b floating point vectors and stores result into \a out. * - * \param a Value - * \param b Value - * - * \return Returns the dot product. + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -float bh_point4f_dot(const bh_point4f_t *a, - const bh_point4f_t *b); +void BH_Vec4fSub(const float *a, + const float *b, + float *out); /** - * Calculates the dot product of two vectors with w component. - * - * \param a Value - * \param b Value + * Multiplies \a a and \a b floating point vectors and stores result into + * \a out. * - * \return Returns the dot product. + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -float bh_point4f_dot3(const bh_point4f_t *a, - const bh_point4f_t *b); +void BH_Vec4fMul(const float *a, + const float *b, + float *out); /** - * Calculates the cross product of two vectors without w component. + * Scales \a a vector by the value \a b and stores result into \a out. * - * result = a x b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B value + * \param out Output vector */ -void bh_point4f_cross(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void BH_Vec4fScale(const float *a, + float b, + float *out); /** - * Calculates the length of the vector. - * - * \param in Value + * Multiples \a a and \a b vectors, adds to \a c and stores result into \a out. * - * \return Returns the length. + * \param a A 4D vector + * \param b B 4D vector + * \param c C 4D vector + * \param out Output 4D vector */ -float bh_point4f_length(const bh_point4f_t *in); +void BH_Vec4fMulAdd(const float *a, + const float *b, + const float *c, + float *out); /** - * Normilizes the vector. + * Negates \a in vector and stores result into \a out. * - * result = in / |in| - * - * \param in Value - * \param result Result + * \param in Input 4D vector + * \param out Output 4D vector */ -void bh_point4f_normal(const bh_point4f_t *in, - bh_point4f_t *result); +void BH_Vec4fNegate(const float *in, + float *out); /** - * Calculates vector, containing minimum components of two vectors. - * - * result = min(a, b) + * Computes dot product of \a a and \a b vectors and returns the result. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector */ -void bh_point4f_min(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +float BH_Vec4fDot(const float *a, + const float *b); /** - * Calculates vector, containing maximum components of two vectors. + * Computes length of the \a in vector and returns the result. * - * result = max(a, b) - * - * \param a Value - * \param b Value - * \param result Result + * \param in Input 4D vector */ -void bh_point4f_max(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +float BH_Vec4fLength(const float *in); /** - * Calculates linear interpolation between two vectors. - * - * result = a + (b - a) * t + * Computes normal vector from the \a in and stores result into \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param in Input 4D vector + * \param out Output 4D vector */ -void bh_point4f_lerp(const bh_point4f_t *a, - const bh_point4f_t *b, - float t, - bh_point4f_t *result); +void BH_Vec4fNormal(const float *in, + float *out); /** - * Calculates spherical linear interpolation between two vectors. + * Computes minimum vector from the \a a and \a b vectors and stores result + * into \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -void bh_point4f_slerp(const bh_point4f_t *a, - const bh_point4f_t *b, - float t, - bh_point4f_t *result); +void BH_Vec4fMin(const float *a, + const float *b, + float *out); /** - * Adds components of two vectors. + * Computes maximum vector from the \a a and \a b vectors and stores result + * into \a out. * - * result = a + b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -void bh_point3f_add(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void BH_Vec4fMax(const float *a, + const float *b, + float *out); /** - * Subtracts components of two vectors. - * - * result = a - b + * Interpolates between \a a and \a b vector by \a t amount and stores result + * into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param t Amount + * \param out Output 4D vector */ -void bh_point3f_sub(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void BH_Vec4fLerp(const float *a, + const float *b, + float t, + float *out); /** - * Multiplies components of two vectors. + * Adds \a a and \a b floating point vectors and stores result into \a out. * - * result = a * b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3f_mul(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void BH_Vec3fAdd(const float *a, + const float *b, + float *out); /** - * Scales the vector. - * - * result = a * b + * Subtracts \a a and \a b floating point vectors and stores result into \a out. * - * \param a Value - * \param b Factor - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3f_scale(const bh_point3f_t *a, - float b, - bh_point3f_t *result); +void BH_Vec3fSub(const float *a, + const float *b, + float *out); /** - * Multiplies and adds three vectors. + * Multiplies \a a and \a b floating point vectors and stores result into + * \a out. * - * result = a * b + c - * - * \param a Value - * \param b Value - * \param c Value - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3f_madd(const bh_point3f_t *a, - const bh_point3f_t *b, - const bh_point3f_t *c, - bh_point3f_t *result); +void BH_Vec3fMul(const float *a, + const float *b, + float *out); /** - * Negates the vector. - * - * result = -in + * Scales \a a vector by the value \a b and stores result into \a out. * - * \param in Value - * \param result Result + * \param a A 3D vector + * \param b B value + * \param out Output 3D vector */ -void bh_point3f_negate(const bh_point3f_t *in, - bh_point3f_t *result); +void BH_Vec3fScale(const float *a, + float b, + float *out); /** - * Calculates the dot product of two vectors. + * Multiples \a a and \a b vectors, adds to \a c and stores result into \a out. * - * \param a Value - * \param b Value - * - * \return Returns the dot product. + * \param a A 3D vector + * \param b B 3D vector + * \param c C 3D vector + * \param out Output 3D vector */ -float bh_point3f_dot(const bh_point3f_t *a, - const bh_point3f_t *b); +void BH_Vec3fMulAdd(const float *a, + const float *b, + const float *c, + float *out); /** - * Calculates the cross product of two vectors. + * Negates \a in vector and stores result into \a out. * - * result = a x b + * \param in Input 3D vector + * \param out Output 3D vector * - * \param a Value - * \param b Value - * \param result Result */ -void bh_point3f_cross(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void BH_Vec3fNegate(const float *in, + float *out); /** - * Calculates the length of the vector. - * - * \param in Value + * Computes dot product of \a a and \a b vectors and returns the result. * - * \return Returns the length. + * \param a A 3D vector + * \param b B 3D vector */ -float bh_point3f_length(const bh_point3f_t *in); +float BH_Vec3fDot(const float *a, + const float *b); /** - * Normilizes the vector. + * Computes cross product of \a a and \a b vectors and stores + * result into \a out. * - * result = in / |in| - * - * \param in Value - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3f_normal(const bh_point3f_t *in, - bh_point3f_t *result); +void BH_Vec3fCross(const float *a, + const float *b, + float *out); /** - * Calculates vector, containing minimum components of two vectors. - * - * result = min(a, b) + * Computes length of the \a in vector and returns the result. * - * \param a Value - * \param b Value - * \param result Result + * \param in Input 3D vector */ -void bh_point3f_min(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +float BH_Vec3fLength(const float *in); /** - * Calculates vector, containing maximum components of two vectors. + * Computes normal vector from the \a in and stores result into \a out. * - * result = max(a, b) - * - * \param a Value - * \param b Value - * \param result Result + * \param in Input 3D vector + * \param out Output 3D vector */ -void bh_point3f_max(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void BH_Vec3fNormal(const float *in, + float *out); /** - * Calculates linear interpolation between two vectors. - * - * result = a + (b - a) * t + * Computes minimum vector from the \a a and \a b vectors and stores result into + * \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3f_lerp(const bh_point3f_t *a, - const bh_point3f_t *b, - float t, - bh_point3f_t *result); +void BH_Vec3fMin(const float *a, + const float *b, + float *out); /** - * Calculates spherical linear interpolation between two vectors. + * Computes maximum vector from the \a a and \a b vectors and stores result into + * \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3f_slerp(const bh_point3f_t *a, - const bh_point3f_t *b, - float t, - bh_point3f_t *result); +void BH_Vec3fMax(const float *a, + const float *b, + float *out); /** - * Adds components of two vectors. + * Interpolates between \a a and \a b vector by \a t amount and stores result + * into \a out. * - * result = a + b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param t Amount + * \param out Output 3D vector */ -void bh_point2f_add(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void BH_Vec3fLerp(const float *a, + const float *b, + float t, + float *out); /** - * Subtracts components of two vectors. - * - * result = a - b + * Adds \a a and \a b floating point vectors and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point2f_sub(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void BH_Vec2fAdd(const float *a, + const float *b, + float *out); /** - * Multiplies components of two vectors. + * Subtracts \a a and \a b floating point vectors and stores result into \a out. * - * result = a * b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point2f_mul(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void BH_Vec2fSub(const float *a, + const float *b, + float *out); /** - * Scales the vector. - * - * result = a * b + * Multiplies \a a and \a b floating point vectors and stores result into + * \a out. * - * \param a Value - * \param b Factor - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point2f_scale(const bh_point2f_t *a, - float b, - bh_point2f_t *result); +void BH_Vec2fMul(const float *a, + const float *b, + float *out); /** - * Multiplies and adds three vectors. + * Scales \a a vector by the value \a b and stores result into \a out. * - * result = a * b + c - * - * \param a Value - * \param b Value - * \param c Value - * \param result Result + * \param a A 2D vector + * \param b B value + * \param out Output 2D vector */ -void bh_point2f_madd(const bh_point2f_t *a, - const bh_point2f_t *b, - const bh_point2f_t *c, - bh_point2f_t *result); +void BH_Vec2fScale(const float *a, + float b, + float *out); /** - * Negates the vector. - * - * result = -in + * Multiples \a a and \a b vectors, adds to \a c and stores result into \a out. * - * \param in Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param c C 2D vector + * \param out Output 2D vector */ -void bh_point2f_negate(const bh_point2f_t *in, - bh_point2f_t *result); +void BH_Vec2fMulAdd(const float *a, + const float *b, + const float *c, + float *out); /** - * Calculates the dot product of two vectors. + * Negates \a in vector and stores result into \a out. * - * \param a Value - * \param b Value + * \param in Input 2D vector + * \param out Output 2D vector * - * \return Returns the dot product. */ -float bh_point2f_dot(const bh_point2f_t *a, - const bh_point2f_t *b); +void BH_Vec2fNegate(const float *in, + float *out); /** - * Calculates the cross product of two vectors and returns its z component. - * - * result = a x b - * - * \param a Value - * \param b Value + * Computes dot product of \a a and \a b vectors and returns the result. * - * \return Returns z component of the result vector. + * \param a A 2D vector + * \param b B 2D vector */ -float bh_point2f_cross(const bh_point2f_t *a, - const bh_point2f_t *b); +float BH_Vec2fDot(const float *a, + const float *b); /** - * Calculates the length of the vector. + * Computes cross product of \a a and \a b vectors and returns the result. * - * \param in Value - * - * \return Returns the length. + * \param a A 2D vector + * \param b B 2D vector */ -float bh_point2f_length(const bh_point2f_t *in); +float BH_Vec2fCross(const float *a, + const float *b); /** - * Normilizes the vector. - * - * result = in / |in| + * Computes length of the \a in vector and returns the result. * - * \param in Value - * \param result Result + * \param in Input 2D vector */ -void bh_point2f_normal(const bh_point2f_t *in, - bh_point2f_t *result); +float BH_Vec2fLength(const float *in); /** - * Calculates vector, containing minimum components of two vectors. + * Computes normal vector from the \a in and stores result into + * \a out. * - * result = min(a, b) - * - * \param a Value - * \param b Value - * \param result Result + * \param in Input 2D vector + * \param out Output 2D vector */ -void bh_point2f_min(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void BH_Vec2fNormal(const float *in, + float *out); /** - * Calculates vector, containing maximum components of two vectors. - * - * result = max(a, b) + * Computes minimum vector from the \a a and \a b vectors and stores result into + * \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point2f_max(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void BH_Vec2fMin(const float *a, + const float *b, + float *out); /** - * Calculates linear interpolation between two vectors. + * Computes maximum vector from the \a a and \a b vectors and stores result into + * \a out. * - * result = a + (b - a) * t - * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point2f_lerp(const bh_point2f_t *a, - const bh_point2f_t *b, - float t, - bh_point2f_t *result); +void BH_Vec2fMax(const float *a, + const float *b, + float *out); /** - * Calculates spherical linear interpolation between two vectors. + * Interpolates between \a a and \a b vector by \a t amount and stores result + * into \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param t Amount + * \param out Output 2D vector */ -void bh_point2f_slerp(const bh_point2f_t *a, - const bh_point2f_t *b, - float t, - bh_point2f_t *result); +void BH_Vec2fLerp(const float *a, + const float *b, + float t, + float *out); /** - * Adds components of two vectors. - * - * result = a + b + * Adds \a a and \a b integer vectors and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -void bh_point4i_add(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void BH_Vec4iAdd(const int *a, + const int *b, + int *out); /** - * Subtracts components of two vectors. + * Subtracts \a a and \a b integer vectors and stores result into \a out. * - * result = a - b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -void bh_point4i_sub(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void BH_Vec4iSub(const int *a, + const int *b, + int *out); /** - * Multiplies components of two vectors. - * - * result = a * b + * Multiplies \a a and \a b integers vectors and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param out Output vector */ -void bh_point4i_mul(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void BH_Vec4iMul(const int *a, + const int *b, + int *out); /** - * Scales the vector. + * Scales \a a vector by the value \a b and stores result into \a out. * - * result = a * b - * - * \param a Value - * \param b Factor - * \param result Result + * \param a A 4D vector + * \param b B value + * \param out Output 4D vector */ -void bh_point4i_scale(const bh_point4i_t *a, - int b, - bh_point4i_t *result); +void BH_Vec4iScale(const int *a, + int b, + int *out); /** - * Multiplies and adds three vectors. - * - * result = a * b + c + * Multiples \a a and \a b vectors, adds to \a c and stores result into \a out. * - * \param a Value - * \param b Value - * \param c Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param c C 4D vector + * \param out Output 4D vector */ -void bh_point4i_madd(const bh_point4i_t *a, - const bh_point4i_t *b, - const bh_point4i_t *c, - bh_point4i_t *result); +void BH_Vec4iMulAdd(const int *a, + const int *b, + const int *c, + int *out); /** - * Negates the vector. + * Negates \a in vector and stores result into \a out. * - * result = -in - * - * \param in Value - * \param result Result + * \param in Input 4D vector + * \param out Output 4D vector */ -void bh_point4i_negate(const bh_point4i_t *in, - bh_point4i_t *result); +void BH_Vec4iNegate(const int *in, + int *out); /** - * Calculates vector, containing minimum components of two vectors. - * - * result = min(a, b) + * Computes minimum vector from the \a a and \a b vectors and stores result into + * \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -void bh_point4i_min(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void BH_Vec4iMin(const int *a, + const int *b, + int *out); /** - * Calculates vector, containing maximum components of two vectors. + * Computes maximum vector from the \a a and \a b vectors and stores result into + * \a out. * - * result = max(a, b) - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4D vector + * \param b B 4D vector + * \param out Output 4D vector */ -void bh_point4i_max(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void BH_Vec4iMax(const int *a, + const int *b, + int *out); /** - * Calculates linear interpolation between two vectors. - * - * result = a + (b - a) * t + * Adds \a a and \a b integer vectors and stores result into \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point4i_lerp(const bh_point4i_t *a, - const bh_point4i_t *b, - float t, - bh_point4i_t *result); +void BH_Vec3iAdd(const int *a, + const int *b, + int *out); /** - * Adds components of two vectors. + * Subtracts \a a and \a b integer vectors and stores result into \a out. * - * result = a + b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3i_add(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void BH_Vec3iSub(const int *a, + const int *b, + int *out); /** - * Subtracts components of two vectors. - * - * result = a - b + * Multiplies \a a and \a b integers vectors and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3i_sub(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void BH_Vec3iMul(const int *a, + const int *b, + int *out); /** - * Multiplies components of two vectors. + * Scales \a a vector by the value \a b and stores result into \a out. * - * result = a * b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3D vector + * \param b B value + * \param out Output 3D vector */ -void bh_point3i_mul(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void BH_Vec3iScale(const int *a, + int b, + int *out); /** - * Scales the vector. - * - * result = a * b + * Multiples \a a and \a b vectors, adds to \a c and stores result into \a out. * - * \param a Value - * \param b Factor - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param c C 3D vector + * \param out Output 3D vector */ -void bh_point3i_scale(const bh_point3i_t *a, - int b, - bh_point3i_t *result); +void BH_Vec3iMulAdd(const int *a, + const int *b, + const int *c, + int *out); /** - * Multiplies and adds three vectors. + * Negates \a in vector and stores result into \a out. * - * result = a * b + c + * \param in Input 3D vector + * \param out Output 3D vector * - * \param a Value - * \param b Value - * \param c Value - * \param result Result */ -void bh_point3i_madd(const bh_point3i_t *a, - const bh_point3i_t *b, - const bh_point3i_t *c, - bh_point3i_t *result); +void BH_Vec3iNegate(const int *in, + int *out); /** - * Negates the vector. + * Computes minimum vector from the \a a and \a b vectors and stores result into + * \a out. * - * result = -in - * - * \param in Value - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3i_negate(const bh_point3i_t *in, - bh_point3i_t *result); +void BH_Vec3iMin(const int *a, + const int *b, + int *out); /** - * Calculates vector, containing minimum components of two vectors. - * - * result = min(a, b) + * Computes maximum vector from the \a a and \a b vectors and stores result into + * \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3D vector + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_point3i_min(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void BH_Vec3iMax(const int *a, + const int *b, + int *out); /** - * Calculates vector, containing maximum components of two vectors. + * Adds \a a and \a b integer vectors and stores result into \a out. * - * result = max(a, b) - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point3i_max(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void BH_Vec2iAdd(const int *a, + const int *b, + int *out); /** - * Calculates linear interpolation between two vectors. - * - * result = a + (b - a) * t + * Subtracts \a a and \a b integer vectors and stores result into \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point3i_lerp(const bh_point3i_t *a, - const bh_point3i_t *b, - float t, - bh_point3i_t *result); +void BH_Vec2iSub(const int *a, + const int *b, + int *out); /** - * Adds components of two vectors. + * Multiplies \a a and \a b integers vectors and stores result into \a out. * - * result = a + b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point2i_add(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +void BH_Vec2iMul(const int *a, + const int *b, + int *out); /** - * Subtracts components of two vectors. - * - * result = a - b + * Scales \a a vector by the value \a b and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 2D vector + * \param b B 2D value + * \param out Output 2D vector */ -void bh_point2i_sub(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +void BH_Vec2iScale(const int *a, + int b, + int *out); /** - * Multiplies components of two vectors. + * Multiples \a a and \a b vectors, adds to \a c and stores result into \a out. * - * result = a * b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param c C 2D vector + * \param out Output 2D vector */ -void bh_point2i_mul(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +void BH_Vec2iMulAdd(const int *a, + const int *b, + const int *c, + int *out); /** - * Scales the vector. + * Negates \a in vector and stores result into \a out. * - * result = a * b + * \param in Input 2D vector + * \param out Output 2D vector * - * \param a Value - * \param b Factor - * \param result Result */ -void bh_point2i_scale(const bh_point2i_t *a, - int b, - bh_point2i_t *result); +void BH_Vec2iNegate(const int *in, + int *out); /** - * Multiplies and adds three vectors. - * - * result = a * b + c + * Computes minimum vector from the \a a and \a b vectors and stores result into + * \a out. * - * \param a Value - * \param b Value - * \param c Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point2i_madd(const bh_point2i_t *a, - const bh_point2i_t *b, - const bh_point2i_t *c, - bh_point2i_t *result); +void BH_Vec2iMin(const int *a, + const int *b, + int *out); /** - * Negates the vector. + * Computes maximum vector from the \a a and \a b vectors and stores result into + * \a out. * - * result = -in - * - * \param in Value - * \param result Result + * \param a A 2D vector + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_point2i_negate(const bh_point2i_t *in, - bh_point2i_t *result); +void BH_Vec2iMax(const int *a, + const int *b, + int *out); /** - * Calculates vector, containing minimum components of two vectors. - * - * result = min(a, b) + * Adds \a a and \a b floating point quaternions and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A quaternion + * \param b B quaternion + * \param out Output quaternion */ -void bh_point2i_min(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +#define BH_Quat4fAdd(a, b, out) \ + BH_Vec4fAdd(a, b, out) /** - * Calculates vector, containing maximum components of two vectors. + * Subtracts \a a and \a b floating point quaternions and stores result into + * \a out. * - * result = max(a, b) - * - * \param a Value - * \param b Value - * \param result Result + * \param a A quaternion + * \param b B quaternion + * \param out Output quaternion */ -void bh_point2i_max(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +#define BH_Quat4fSub(a, b, out) \ + BH_Vec4fSub(a, b, out) /** - * Calculates linear interpolation between two vectors. - * - * result = a + (b - a) * t + * Scales \a a quaternion by the value \a b and stores result into \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A quaternion + * \param b B value + * \param out Output quaternion */ -void bh_point2i_lerp(const bh_point2i_t *a, - const bh_point2i_t *b, - float t, - bh_point2i_t *result); +#define BH_Quat4fScale(a, b, out) \ + BH_Vec4fScale(a, b, out) /** - * Adds components of two quaternions. - * - * result = a + b + * Negates \a in quaternion and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param in Input quaternion + * \param out Output quaternion */ -#define bh_quat_add bh_point4f_add +#define BH_Quat4fNegate(in, out) \ + BH_Vec4fNegate(in, out) /** - * Subtracts components of two quaternions. + * Computes dot product of \a a and \a b quaternions and returns the result. * - * result = a - b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A quaternion + * \param b B quaternion */ -#define bh_quat_sub bh_point4f_sub +#define BH_Quat4fDot(a, b) \ + BH_Vec4fDot(a, b) /** - * Scales the quaternion. - * - * result = a * b + * Computes length of the \a in quaternion and returns the result. * - * \param a Value - * \param b Factor - * \param result Result + * \param in Input quaternion */ -#define bh_quat_scale bh_point4f_scale +#define BH_Quat4fLength(in) \ + BH_Vec4fLength(in) /** - * Negates the quaternion. + * Computes normal quaternion from the \a in and stores result into \a out. * - * result = -in - * - * \param in Value - * \param result Result + * \param in Input quaternion + * \param out Output quaternion */ -#define bh_quat_negate bh_point4f_negate +#define BH_Quat4fNormal(in, out) \ + BH_Vec4fNormal(in, out) /** - * Calculates the dot product of two quaternions. - * - * \param a Value - * \param b Value + * Interpolates between \a a and \a b quaternion by \a t amount and stores + * result into \a out. * - * \return Returns the dot product. + * \param a A quaternion + * \param b B quaternion + * \param t Amount + * \param out Output quaternion */ -#define bh_quat_dot bh_point4f_dot +#define BH_Quat4fLerp(a, b, t, out) \ + BH_Vec4fLerp(a, b, t, out) /** - * Calculates the length of the quaternion. + * Stores identity quaternion into \a out. * - * \param in Value - * - * \return Returns the length. + * \param out Output quaternion. */ -#define bh_quat_length bh_point4f_length +void BH_Quat4fIdentity(float *out); /** - * Normilizes the quaternion. - * - * result = in / |in| + * Conjugates the \a in quaternion and stores result into \a out. * - * \param in Value - * \param result Result + * \param in Input quaternion + * \param out Output quaternion */ -#define bh_quat_normal bh_point4f_normal +void BH_Quat4fConjugate(const float *in, + float *out); /** - * Calculates linear interpolation between two quaternions. + * Computes the inverse of the \a in quaternion and stores result into \a out. * - * result = a + (b - a) * t - * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param in Input quaternion + * \param out Output quaternion */ -#define bh_quat_lerp bh_point4f_lerp +void BH_Quat4fInverse(const float *in, + float *out); /** - * Calculates spherical linear interpolation between two quaternions. + * Multiplies the \a a and \a b quaternions and stores result into \a out. * - * \param a Value - * \param b Value - * \param t Factor - * \param result Result + * \param a A quaternion + * \param b B quaternion + * \param out Output quaternion */ -#define bh_quat_slerp bh_point4f_slerp +void BH_Quat4fMul(const float *a, + const float *b, + float *out); /** - * Sets quaternions to identity. + * Spherically interpolates between \a a and \a b quaternions by \a t amount and + * stores result into \a out. * - * \param result Result + * \param a A quaternion + * \param b B quaternion + * \param t Amount + * \param out Output quaternion */ -void bh_quat_identity(bh_quat_t *result); +void BH_Quat4fSlerp(const float *a, + const float *b, + float t, + float *out); /** - * Conjugates the quaternion. - * - * result = (-in.x, -in.y, -in.z, in.w) + * Computes the quaternion that represents \a roll, \a pitch, \a yaw (euler + * angles) and stores result into \a out. * - * \param in Value - * \param result Result - */ -void bh_quat_conjugate(const bh_quat_t *in, - bh_quat_t *result); - - -/** - * Calculates the inverse of the quaternion. + * Order of the rotation is ZYX (yaw, pitch, roll) * - * \param in Value - * \param result Result - */ -void bh_quat_inverse(const bh_quat_t *in, - bh_quat_t *result); - - -/** - * Multiplies two quaternions. - * - * result = a * b - * - * \param a Value - * \param b Value - * \param result Result - */ -void bh_quat_mul(const bh_quat_t *a, - const bh_quat_t *b, - bh_quat_t *result); - - -/** - * Sets quaternion from euler angles (roll, pitch, yaw). - * - * Order of the rotation is ZYX (yaw, pitch, roll). - * - * \param roll Roll - * \param pitch Pitch - * \param yaw Yaw - * \param result Result + * \param roll Roll + * \param pitch Pitch + * \param yaw Yaw + * \param out Output quaternion */ -void bh_quat_set_euler(float roll, - float pitch, - float yaw, - bh_quat_t *result); +void BH_Quat4fFromEuler(float roll, + float pitch, + float yaw, + float *out); /** - * Sets quaternion from axis of rotation and angle. + * Computes quaternion that represents rotation by angle \a angle around + * axis \a axis and stores result into \a out. * - * \param axis Axis - * \param angle Angle - * \param result Result + * \param axis Axis 3D vector + * \param angle Angle + * \param out Output quaternion */ -void bh_quat_set_rotation(const bh_point3f_t *axis, - float angle, - bh_quat_t *result); +void BH_Quat4fFromAxis(const float *axis, + float angle, + float *out); /** - * Calculates euler angles (roll, pitch, yaw) from quaternion. + * Computes euler angles from quaternion \a in and stores result into \a roll, + * \a pitch, \a yaw. * - * Order of the rotation is ZYX (yaw, pitch, roll). + * Order of the rotation is ZYX (yaw, pitch, roll) * - * \param in Value - * \param roll Roll - * \param pitch Pitch - * \param yaw Yaw + * \param in Input quaternion + * \param roll Output roll + * \param pitch Output pitch + * \param yaw Output yaw */ -void bh_quat_euler(const bh_quat_t *in, - float *roll, - float *pitch, - float *yaw); +void BH_Quat4fToEuler(const float *in, + float *roll, + float *pitch, + float *yaw); /** - * Calculates axis of rotation and angle from quaternion. + * Computes rotation around axis from quaternion \a in and stores result into + * \a axis and \a angle. * - * \param in Value - * \param axis Axis - * \param angle Angle + * \param in Input quaternion + * \param axis Output axis 3D vector + * \param angle Output angle */ -void bh_quat_rotation(const bh_quat_t *in, - bh_point3f_t *axis, - float *angle); +void BH_Quat4fToAxis(const float *in, + float *axis, + float *angle); /** - * Calculates rotation matrix from quaternion. + * Computes 4x4 rotation matrix from quaternion \a in and stores result into + * \a out. * - * \param in Value - * \param result Result + * \param in Input quaternion + * \param out Output 4x4 matrix */ -void bh_quat_matrix(const bh_quat_t *in, - bh_matrix4f_t *result); +void BH_Quat4fToMat4f(const float *in, + float *out); /** - * Sets matrix to identity. + * Stores identity matrix into \a out. * - * \param result Result + * \param out Output 4x4 matrix. */ -void bh_matrix4f_identity(bh_matrix4f_t *result); +void BH_Mat4fIdentity(float *out); /** - * Adds two matricies. - * - * result = a + b + * Adds \a a and \a b floating point matricies and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4x4 matrix + * \param b B 4x4 matrix + * \param out Output 4x4 matrix */ -void bh_matrix4f_add(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result); +void BH_Mat4fAdd(const float *a, + const float *b, + float *out); /** - * Subtracts two matricies. + * Subtracts \a a and \a b floating point matricies and stores result into + * \a out. * - * result = a - b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4x4 matrix + * \param b B 4x4 matrix + * \param out Output 4x4 matrix */ -void bh_matrix4f_sub(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result); +void BH_Mat4fSub(const float *a, + const float *b, + float *out); /** - * Multiplies two matricies. - * - * result = a * b + * Multiplies \a a and \a b floating point matricies and stores result into + * \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 4x4 matrix + * \param b B 4x4 matrix + * \param out Output 4x4 matrix */ -void bh_matrix4f_mul(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result); +void BH_Mat4fMul(const float *a, + const float *b, + float *out); /** - * Scales the matrix. + * Scales \a a matrix by the value \a b and stores result into \a out. * - * result = a * b - * - * \param a Value - * \param b Factor - * \param result Result + * \param a A 4x4 matrix + * \param b B value + * \param out Output 4x4 matrix */ -void bh_matrix4f_scale(const bh_matrix4f_t *a, - float b, - bh_matrix4f_t *result); +void BH_Mat4fScale(const float *a, + float b, + float *out); /** - * Transposes the matrix. - * - * result = a^T + * Transposes matrix \a in and stores result into \a out. * - * \param in Value - * \param result Result + * \param in Input 4x4 matrix + * \param out Output 4x4 matrix */ -void bh_matrix4f_transpose(const bh_matrix4f_t *in, - bh_matrix4f_t *result); +void BH_Mat4fTranspose(const float *in, + float *out); /** - * Calculates the trace of the matrix. + * Computes \a in matrix trace and returns the result. * - * \param in Value - * - * \return Returns trace value. + * \param in Input 4x4 matrix */ -float bh_matrix4f_trace(const bh_matrix4f_t *in); +float BH_Mat4fTrace(const float *in); /** - * Calculates the determinant of the matrix. - * - * \param in Value + * Computes \a in matrix determinant and returns the result. * - * \return Returns determinant value. + * \param in Input 4x4 matrix */ -float bh_matrix4f_determinant(const bh_matrix4f_t *in); +float BH_Mat4fDet(const float *in); /** - * Calculates the inverse of the matrix. - * - * If matrix has no inverse - identity matrix will be returned. - * - * result = in^-1 + * Computes inverse of \a in matrix and stores result into \a out. * - * \param in Value - * \param result Result + * \param in Input 4x4 matrix + * \param out OUtput 4x4 matrix * - * \return On success, returns inverse of the matrix. - * \return On failure, returns identity matrix. + * \return On success, returns zero. + * \return On failure, returns error code. */ -int bh_matrix4f_inverse(const bh_matrix4f_t *in, - bh_matrix4f_t *result); +int BH_Mat4fInverse(const float *in, + float *out); /** - * Calculates scaling matrix. + * Computes scaling matrix from values \a x, \a y, \a z and stores result into + * \a out. * - * \param x X scale - * \param y Y scale - * \param z Z scale - * \param result Result + * \param x X scale + * \param y Y scale + * \param z Z scale + * \param out Output 4x4 matrix */ -void bh_matrix4f_scaling(float x, - float y, - float z, - bh_matrix4f_t *result); +void BH_Mat4fFromScale(float x, + float y, + float z, + float *out); /** - * Calculates translation matrix. + * Computes translation matrix from values \a x, \a y, \a z and stores result + * into \a out. * - * \param x X scale - * \param y Y scale - * \param z Z scale - * \param result Result + * \param x X translation + * \param y Y translation + * \param z Z translation + * \param out Output 4x4 matrix */ -void bh_matrix4f_translation(float x, +void BH_Mat4fFromTranslation(float x, float y, float z, - bh_matrix4f_t *result); + float *out); /** - * Calculates x-rotation matrix (or rotation around x axis). + * Computes rotation matrix around x axis with angle \a angle and stores + * result \a out. * - * \param angle Angle - * \param result Result + * \param angle Angle + * \param out Output 4x4 matrix */ -void bh_matrix4f_rotation_x(float angle, - bh_matrix4f_t *result); +void BH_Mat4fFromRotationX(float angle, + float *out); /** - * Calculates y-rotation matrix (or rotation around y axis). + * Computes rotation matrix around y axis with angle \a angle and stores + * result \a out. * - * \param angle Angle - * \param result Result + * \param angle Angle + * \param out Output 4x4 matrix */ -void bh_matrix4f_rotation_y(float angle, - bh_matrix4f_t *result); +void BH_Mat4fFromRotationY(float angle, + float *out); /** - * Calculates z-rotation matrix (or rotation around z axis). + * Computes rotation matrix around z axis with angle \a angle and stores + * result \a out. * - * \param angle Angle - * \param result Result + * \param angle Angle + * \param out Output 4x4 matrix */ -void bh_matrix4f_rotation_z(float angle, - bh_matrix4f_t *result); +void BH_Mat4fFromRotationZ(float angle, + float *out); /** - * Calculates rotation matrix around axis. + * Computes rotation matrix around axis \a axis with angle \a angle and stores + * result \a out. * - * \param axis Axis - * \param angle Angle - * \param result Result + * \param axis Axis 3D vector + * \param angle Angle + * \param out Output 4x4 matrix */ -void bh_matrix4f_rotation(const bh_point3f_t *axis, - float angle, - bh_matrix4f_t *result); +void BH_Mat4fFromAxis(const float *axis, + float angle, + float *out); /** - * Calculates rotation matrix from euler angles (roll, pitch, yaw). + * Computes the rotation matrix that represents \a roll, \a pitch, \a yaw (euler + * angles) and stores result into \a out. * - * Order of the rotation is ZYX (yaw, pitch, roll). + * Order of the rotation is ZYX (yaw, pitch, roll) * - * \param roll Roll - * \param pitch Pitch - * \param yaw Yaw - * \param result Result + * \param roll Roll + * \param pitch Pitch + * \param yaw Yaw + * \param out Output 4x4 matrix */ -void bh_matrix4f_rotation_euler(float roll, - float pitch, - float yaw, - bh_matrix4f_t *result); +void BH_Mat4fFromEuler(float roll, + float pitch, + float yaw, + float *out); /** - * Calculates rotation matrix from quaternion. + * Computes 4x4 rotation matrix from quaternion \a in and stores result into + * \a out. * - * \param rotation Quaternion - * \param result Result + * \param in Input quaternion + * \param out Output 4x4 matrix */ -void bh_matrix4f_rotation_quat(bh_quat_t *rotation, - bh_matrix4f_t *result); +void BH_Mat4fFromQuat4f(const float *in, + float *out); /** - * Calculates orthographic projection matrix. + * Computes orthographic projection matrix and stores result into \a out. * - * \param x_min Min x - * \param x_max Max x - * \param y_min Min y - * \param y_max Max y - * \param z_min Min z - * \param z_max Max z - * \param result Result + * \param x_min Min x value + * \param x_max Max x value + * \param y_min Min y value + * \param y_max Max y value + * \param z_min Min z value + * \param z_max Max z value + * \param out Output 4x4 matrix */ -void bh_matrix4f_ortho(float x_min, - float x_max, - float y_min, - float y_max, - float z_min, - float z_max, - bh_matrix4f_t *result); +void BH_Mat4fFromOrtho(float xMin, + float xMax, + float yMin, + float yMax, + float zMin, + float zMax, + float *out); /** - * Calculates perspective projection matrix. + * Computes perspective projection matrix and stores result into \a out. * * \param fov Field of view * \param aspect Aspect ratio - * \param z_min Min z - * \param z_max Max z - * \param result Result + * \param z_min Min z value + * \param z_max Max z value + * \param out Output 4x4 matrix */ -void bh_matrix4f_perspective(float fov, - float aspect, - float z_min, - float z_max, - bh_matrix4f_t *result); +void BH_Mat4fFromFrustum(float fov, + float aspect, + float zMin, + float zMax, + float *out); /** - * Calculates camera view matrix. + * Computes camera view matrix and stores result into \a out. * - * \param camera Position - * \param at Target - * \param up Up - * \param result Result + * \param pos Position vector + * \param at Target vector + * \param up Up vector + * \param out Output 4x4 matrix */ -void bh_matrix4f_lookat(const bh_point3f_t *camera, - const bh_point3f_t *at, - const bh_point3f_t *up, - bh_matrix4f_t *result); +void BH_Mat4fFromLookAt(const float *pos, + const float *at, + const float *up, + float *out); /** - * Applies matrix to vector. + * Multiplies matrix \a a by vector \a b and stores result into \a out. * - * \param a Matrix - * \param b Vector - * \param result Result + * \param a A 4x4 matrix + * \param b B 4D vector + * \param out Output 4D vector */ -void bh_matrix4f_transform_point3f(const bh_matrix4f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void BH_Mat4fApplyVec4f(const float *a, + const float *b, + float *out); /** - * Applies matrix to vector. + * Multiplies matrix \a a by vector \a b and stores result into \a out. * - * \param a Matrix - * \param b Vector - * \param result Result + * \param a A 4x4 matrix + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_matrix4f_transform_point4f(const bh_matrix4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void BH_Mat4fApplyVec4f(const float *a, + const float *b, + float *out); /** - * Sets matrix to identity. + * Stores identity matrix into \a out. * - * \param result Result + * \param out Output 3x3 matrix. */ -void bh_matrix3f_identity(bh_matrix3f_t *result); +void BH_Mat3fIdentity(float *out); /** - * Adds two matricies. - * - * result = a + b + * Adds \a a and \a b floating point matricies and stores result into \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3x3 matrix + * \param b B 3x3 matrix + * \param out Output 3x3 matrix */ -void bh_matrix3f_add(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result); +void BH_Mat3fAdd(const float *a, + const float *b, + float *out); /** - * Subtracts two matricies. + * Subtracts \a a and \a b floating point matricies and stores result into + * \a out. * - * result = a - b - * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3x3 matrix + * \param b B 3x3 matrix + * \param out Output 3x3 matrix */ -void bh_matrix3f_sub(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result); +void BH_Mat3fSub(const float *a, + const float *b, + float *out); /** - * Multiplies two matricies. - * - * result = a * b + * Multiplies \a a and \a b floating point matricies and stores result into + * \a out. * - * \param a Value - * \param b Value - * \param result Result + * \param a A 3x3 matrix + * \param b B 3x3 matrix + * \param out Output 3x3 matrix */ -void bh_matrix3f_mul(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result); +void BH_Mat3fMul(const float *a, + const float *b, + float *out); /** - * Scales the matrix. + * Scales \a a matrix by the value \a b and stores result into \a out. * - * result = a * b - * - * \param a Value - * \param b Factor - * \param result Result + * \param a A 3x3 matrix + * \param b B value + * \param out Output 3x3 matrix */ -void bh_matrix3f_scale(const bh_matrix3f_t *a, - float b, - bh_matrix3f_t *result); +void BH_Mat3fScale(const float *a, + float b, + float *out); /** - * Transposes the matrix. - * - * result = a^T + * Transposes matrix \a in and stores result into \a out. * - * \param in Value - * \param result Result + * \param in Input 3x3 matrix + * \param out Output 3x3 matrix */ -void bh_matrix3f_transpose(const bh_matrix3f_t *in, - bh_matrix3f_t *result); +void BH_Mat3fTranspose(const float *in, + float *out); /** - * Calculates the trace of the matrix. + * Computes \a in matrix trace and returns the result. * - * \param in Value - * - * \return Returns trace value. + * \param in Input 3x3 matrix */ -float bh_matrix3f_trace(const bh_matrix3f_t *in); +float BH_Mat3fTrace(const float *in); /** - * Calculates the determinant of the matrix. - * - * \param in Value + * Computes \a in matrix determinant and returns the result. * - * \return Returns determinant value. + * \param in Input 3x3 matrix */ -float bh_matrix3f_determinant(const bh_matrix3f_t *in); +float BH_Mat3fDet(const float *in); /** - * Calculates the inverse of the matrix. + * Computes inverse of \a in matrix and stores result into \a out. * - * If matrix has no inverse - identity matrix will be returned. + * \param in Input 3x3 matrix + * \param out OUtput 3x3 matrix * - * result = in^-1 - * - * \param in Value - * \param result Result - * - * \return On success, returns inverse of the matrix. - * \return On failure, returns identity matrix. + * \return On success, returns zero. + * \return On failure, returns error code. */ -int bh_matrix3f_inverse(const bh_matrix3f_t *in, - bh_matrix3f_t *result); +int BH_Mat3fInverse(const float *in, + float *out); /** - * Calculates scaling matrix. + * Computes scaling matrix from values \a x, \a y and stores result into + * \a out. * - * \param x X scale - * \param y Y scale - * \param z Z scale - * \param result Result + * \param x X scale + * \param y Y scale + * \param out Output 3x3 matrix */ -void bh_matrix3f_scaling(float x, - float y, - bh_matrix3f_t *result); +void BH_Mat3fFromScale(float x, + float y, + float *out); /** - * Calculates translation matrix. + * Computes translation matrix from values \a x, \a y and stores result + * into \a out. * - * \param x X scale - * \param y Y scale - * \param z Z scale - * \param result Result + * \param x X translation + * \param y Y translation + * \param out Output 3x3 matrix */ -void bh_matrix3f_translation(float x, +void BH_Mat3fFromTranslation(float x, float y, - bh_matrix3f_t *result); + float *out); /** - * Calculates rotation matrix. + * Computes rotation matrix around with angle \a angle and stores result \a out. * - * \param angle Angle - * \param result Result + * \param angle Angle + * \param out Output 3x3 matrix */ -void bh_matrix3f_rotation(float angle, - bh_matrix3f_t *result); +void BH_Mat3fFromRotation(float angle, + float *out); /** - * Applies matrix to vector. + * Multiplies matrix \a a by vector \a b and stores result into \a out. * - * \param a Matrix - * \param b Vector - * \param result Result + * \param a A 3x3 matrix + * \param b B 3D vector + * \param out Output 3D vector */ -void bh_matrix3f_transform_point2f(const bh_matrix3f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void BH_Mat3fApplyVec3f(float *a, + float *b, + float *out); /** - * Applies matrix to vector. + * Multiplies matrix \a a by vector \a b and stores result into \a out. * - * \param a Matrix - * \param b Vector - * \param result Result + * \param a A 3x3 matrix + * \param b B 2D vector + * \param out Output 2D vector */ -void bh_matrix3f_transform_point3f(const bh_matrix3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void BH_Mat3fApplyVec2f(float *a, + float *b, + float *out); #endif /* BH_MATH_H */ diff --git a/include/bh/queue.h b/include/bh/queue.h index 7905c77..f7b8b12 100755 --- a/include/bh/queue.h +++ b/include/bh/queue.h @@ -5,7 +5,7 @@ #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 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 */ -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 */ -void bh_queue_clear(bh_queue_t *queue); +void BH_QueueClear(BH_Queue *queue); /** @@ -47,8 +47,8 @@ void bh_queue_clear(bh_queue_t *queue); * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_queue_reserve(bh_queue_t *queue, - size_t size); +int BH_QueueReserve(BH_Queue *queue, + size_t size); /** @@ -62,8 +62,8 @@ int bh_queue_reserve(bh_queue_t *queue, * \return On success, returns zero value. * \return On failure, returns error code. */ -int bh_queue_insert(bh_queue_t *queue, - void *value); +int BH_QueueInsert(BH_Queue *queue, + void *value); /** @@ -73,7 +73,7 @@ int bh_queue_insert(bh_queue_t *queue, * * \note Calling this function will invalidate iterators. */ -void bh_queue_remove(bh_queue_t *queue); +void BH_QueueRemove(BH_Queue *queue); /** @@ -84,8 +84,8 @@ void bh_queue_remove(bh_queue_t *queue); * \return On success, returns front value from the queue. * \return On failure, returns null pointer. */ -int bh_queue_front(bh_queue_t *queue, - void **value); +int BH_QueueFront(BH_Queue *queue, + 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 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. */ -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. */ -size_t bh_queue_capacity(bh_queue_t *queue); +size_t BH_QueueCapacity(BH_Queue *queue); /** @@ -132,8 +132,8 @@ size_t bh_queue_capacity(bh_queue_t *queue); * \return If the \a iter is the null pointer, returns iterator to the * first element of the queue. */ -void *bh_queue_iter_next(bh_queue_t *queue, - void *iter); +void *BH_QueueIterNext(BH_Queue *queue, + void *iter); /** @@ -143,7 +143,7 @@ void *bh_queue_iter_next(bh_queue_t *queue, * * \return Returns value, pointed by iterator. */ -void *bh_queue_iter_value(void *iter); +void *BH_QueueIterValue(void *iter); #endif /* BH_QUEUE_H */ diff --git a/main.c b/main.c index 1f85fab..70d6f86 100644 --- a/main.c +++ b/main.c @@ -2,9 +2,10 @@ int main() { - bh_io_t *io = bh_file_new("hello.txt"); - bh_io_open(io, BH_IO_WRITE); - bh_io_write(io, "Hello, world!", 13, NULL); - bh_io_free(io); + BH_IO *io = BH_FileNew("hello.txt"); + BH_IOOpen(io, BH_IO_WRITE); + BH_IOWrite(io, "Hello, world!", 13, NULL); + BH_IOFree(io); + return 0; } \ No newline at end of file diff --git a/src/algo.c b/src/algo.c index 591a1d5..8a42c95 100755 --- a/src/algo.c +++ b/src/algo.c @@ -2,7 +2,7 @@ #include -void bh_swap(void *dest, +void BH_Swap(void *dest, void *src, size_t size) { @@ -30,11 +30,11 @@ void bh_swap(void *dest, } -void *bh_partition(void *pivot, +void *BH_Partition(void *pivot, void *array, size_t size, size_t element, - bh_equal_cb_t equal) + BH_EqualCallback equal) { char *start, *end, *i, *j; @@ -66,7 +66,7 @@ void *bh_partition(void *pivot, pivot = i; /* Swap elements and continue */ - bh_swap(i, j, element); + BH_Swap(i, j, element); i += element; j -= element; } @@ -77,10 +77,10 @@ void *bh_partition(void *pivot, #if 0 -static void bh_sort_insert(void *array, - size_t size, - size_t element, - bh_equal_cb_t equal) +static void BH_SortInsert(void *array, + size_t size, + size_t element, + BH_EqualCallback equal) { size_t i, j; @@ -95,7 +95,7 @@ static void bh_sort_insert(void *array, rhs = (char *)array + (j - 1) * element; if (equal(lhs, rhs) < 0) - bh_swap(lhs, rhs, element); + BH_Swap(lhs, rhs, element); else break; } @@ -105,10 +105,10 @@ static void bh_sort_insert(void *array, #endif -static void bh_sort_shell(void *array, - size_t size, - size_t element, - bh_equal_cb_t equal) +static void BH_SortShell(void *array, + size_t size, + size_t element, + BH_EqualCallback equal) { static const size_t gaps[10] = {1750, 701, 301, 132, 57, 23, 10, 4, 1, 0}; const size_t *gap; @@ -127,7 +127,7 @@ static void bh_sort_shell(void *array, rhs = (char *)array + (j - *gap) * element; if (equal(lhs, rhs) < 0) - bh_swap(lhs, rhs, element); + BH_Swap(lhs, rhs, element); else break; } @@ -136,24 +136,24 @@ static void bh_sort_shell(void *array, } -static void bh_sort_heap(void *array, - size_t size, - size_t element, - bh_equal_cb_t equal) +static void BH_SortHeap(void *array, + size_t size, + size_t element, + BH_EqualCallback equal) { size_t i; - bh_heap_make(array, size, element, equal); + BH_HeapMake(array, size, element, equal); 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, - size_t size, - size_t element, - bh_equal_cb_t equal, - size_t depth) +static void BH_SortIntroR(void *array, + size_t size, + size_t element, + BH_EqualCallback equal, + size_t depth) { /* Introsort (with manual tail call optimization) */ while (1) @@ -163,13 +163,13 @@ static void bh_sort_intro_r(void *array, if (size < 16) { /* 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; } else if (!depth) { /* Max depth reached - use heap sort */ - bh_sort_heap(array, size, element, equal); + BH_SortHeap(array, size, element, equal); return; } @@ -199,10 +199,10 @@ static void bh_sort_intro_r(void *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 */ - 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 */ 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 element, - bh_equal_cb_t equal) + BH_EqualCallback equal) { size_t depth, depth_log; @@ -229,14 +229,14 @@ void bh_sort(void *array, } /* 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, - size_t size, - size_t element, - bh_equal_cb_t equal) +void BH_HeapMake(void *array, + size_t size, + size_t element, + BH_EqualCallback equal) { char *start, *end; size_t i; @@ -268,7 +268,7 @@ void bh_heap_make(void *array, if (equal(current, biggest) < 0) { /* Swap content and recalculate children pointers */ - bh_swap(current, biggest, element); + BH_Swap(current, biggest, element); current = biggest; left = start + (current - start) * 2 + element; right = left + element; @@ -280,10 +280,10 @@ void bh_heap_make(void *array, } -void bh_heap_remove(void *array, - size_t size, - size_t element, - bh_equal_cb_t equal) +void BH_HeapRemove(void *array, + size_t size, + size_t element, + BH_EqualCallback equal) { char *start, *end, *current, *left, *right; @@ -298,7 +298,7 @@ void bh_heap_remove(void *array, right = left + element; /* Swap first and last element */ - bh_swap(current, end, element); + BH_Swap(current, end, element); /* Iterate until we reach the end */ while (left < end) @@ -314,7 +314,7 @@ void bh_heap_remove(void *array, if (equal(current, biggest) < 0) { /* Swap content and recalculate children pointers */ - bh_swap(current, biggest, element); + BH_Swap(current, biggest, element); current = biggest; left = start + (current - start) * 2 + element; right = left + element; @@ -325,11 +325,11 @@ void bh_heap_remove(void *array, } -void bh_heap_insert(void *value, - void *array, - size_t size, - size_t element, - bh_equal_cb_t equal) +void BH_HeapInsert(void *value, + void *array, + size_t size, + size_t element, + BH_EqualCallback equal) { char *start, *end, *current; @@ -353,7 +353,7 @@ void bh_heap_insert(void *value, if (equal(parent, current) < 0) { /* Swap current and parent */ - bh_swap(parent, current, element); + BH_Swap(parent, current, element); current = parent; } else @@ -362,11 +362,11 @@ void bh_heap_insert(void *value, } -void bh_heap_replace(void *value, - void *array, - size_t size, - size_t element, - bh_equal_cb_t equal) +void BH_HeapReplace(void *value, + void *array, + size_t size, + size_t element, + BH_EqualCallback equal) { char *start, *end, *current, *left, *right; @@ -400,7 +400,7 @@ void bh_heap_replace(void *value, if (equal(current, biggest) < 0) { /* Swap content and recalculate children pointers */ - bh_swap(current, biggest, element); + BH_Swap(current, biggest, element); current = biggest; left = start + (current - start) * 2 + element; right = left + element; diff --git a/src/dummy/file.c b/src/dummy/file.c index 7b35553..eab4c72 100644 --- a/src/dummy/file.c +++ b/src/dummy/file.c @@ -1,56 +1,71 @@ #include -typedef struct bh_file_s +typedef struct BH_File { int implement; int me; -} bh_file_t; +} BH_File; -static int file_info(bh_file_t *file, - size_t *size, - const char **name); -static int file_init(bh_file_t *file, - const char *path); +static int BH_FileInfo(BH_File *file, + size_t *size, + const char **name); -static int file_destroy(bh_file_t *file); -static int file_open(bh_file_t *file, - int *mode); +static int BH_FileInit(BH_File *file, + const char *path); -static int file_close(bh_file_t *file); -static int file_read(bh_file_t *file, - char *data, - size_t *size); +static int BH_FileDestroy(BH_File *file); -static int file_write(bh_file_t *file, - const char *data, - size_t *size); -static int file_peek(bh_file_t* file, - char *data, - size_t *size); +static int BH_FileOpen(BH_File *file, + int *mode); -static int file_flush(bh_file_t *file); -static int file_seek(bh_file_t *file, - int64_t *pos, - int *dir); +static int BH_FileClose(BH_File *file); -static int file_tell(bh_file_t *file, - int64_t *pos); -static int file_size(bh_file_t *file, - int64_t *size); +static int BH_FileRead(BH_File *file, + char *data, + size_t *size); -static int file_flags(bh_file_t *file); -static int file_clear(bh_file_t *file); +static int BH_FileWrite(BH_File *file, + const char *data, + size_t *size); -static int file_info(bh_file_t *file, - size_t *size, - const char **name) + +static int BH_FilePeek(BH_File* file, + char *data, + size_t *size); + + +static int BH_FileFlush(BH_File *file); + + +static int BH_FileSeek(BH_File *file, + int64_t *pos, + int *dir); + + +static int BH_FileTell(BH_File *file, + int64_t *pos); + + +static int BH_FileSize(BH_File *file, + int64_t *size); + + +static int BH_FileFlags(BH_File *file); + + +static int BH_FileClear(BH_File *file); + + +static int BH_FileInfo(BH_File *file, + size_t *size, + const char **name) { static const char classname[] = BH_FILE_CLASSNAME; @@ -62,109 +77,124 @@ static int file_info(bh_file_t *file, return BH_NOIMPL; } -static int file_init(bh_file_t *file, - const char *path) + +static int BH_FileInit(BH_File *file, + const char *path) { return BH_NOIMPL; } -static int file_destroy(bh_file_t *file) + +static int BH_FileDestroy(BH_File *file) { return BH_NOIMPL; } -static int file_open(bh_file_t *file, - int *mode) + +static int BH_FileOpen(BH_File *file, + int *mode) { return BH_NOIMPL; } -static int file_close(bh_file_t *file) + +static int BH_FileClose(BH_File *file) { return BH_NOIMPL; } -static int file_read(bh_file_t *file, - char *data, - size_t *size) + +static int BH_FileRead(BH_File *file, + char *data, + size_t *size) { return BH_NOIMPL; } -static int file_write(bh_file_t *file, - const char *data, - size_t *size) + +static int BH_FileWrite(BH_File *file, + const char *data, + size_t *size) { return BH_NOIMPL; } -static int file_peek(bh_file_t *file, - char *data, - size_t *size) + +static int BH_FilePeek(BH_File *file, + char *data, + size_t *size) { return BH_NOIMPL; } -static int file_flush(bh_file_t *file) + +static int BH_FileFlush(BH_File *file) { return BH_NOIMPL; } -static int file_seek(bh_file_t *file, - int64_t *pos, - int *dir) + +static int BH_FileSeek(BH_File *file, + int64_t *pos, + int *dir) { return BH_NOIMPL; } -static int file_tell(bh_file_t *file, - int64_t *pos) + +static int BH_FileTell(BH_File *file, + int64_t *pos) { return BH_NOIMPL; } -static int file_size(bh_file_t *file, - int64_t *size) + +static int BH_FileSize(BH_File *file, + int64_t *size) { return BH_NOIMPL; } -static int file_flags(bh_file_t *file) + +static int BH_FileFlags(BH_File *file) { return BH_IO_FLAG_ERROR; } -static int file_clear(bh_file_t *file) + +static int BH_FileClear(BH_File *file) { return BH_NOIMPL; } -static int file_proc(bh_file_t *file, - int type, - void *arg1, - void *arg2) + +static int BH_FileCallback(BH_File *file, + int type, + void *arg1, + void *arg2) { switch (type) { - case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2); - case BH_IO_INIT_CB: return file_init(file, (const char *)arg1); - case BH_IO_DESTROY_CB: return file_destroy(file); - case BH_IO_OPEN_CB: return file_open(file, (int *)arg1); - case BH_IO_CLOSE_CB: return file_close(file); - case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2); - case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2); - case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2); - case BH_IO_FLUSH_CB: return file_flush(file); - case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2); - case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1); - case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1); - case BH_IO_FLAGS_CB: return file_flags(file); - case BH_IO_CLEAR_CB: return file_clear(file); + case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2); + case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1); + case BH_IO_DESTROY_CB: return BH_FileDestroy(file); + case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1); + case BH_IO_CLOSE_CB: return BH_FileClose(file); + case BH_IO_READ_CB: return BH_FileRead(file, (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 BH_FilePeek(file, (char *)arg1, (size_t *)arg2); + case BH_IO_FLUSH_CB: return BH_FileFlush(file); + case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2); + case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1); + case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1); + case BH_IO_FLAGS_CB: return BH_FileFlags(file); + case BH_IO_CLEAR_CB: return BH_FileClear(file); 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); } diff --git a/src/hashmap.c b/src/hashmap.c index 7bb3199..989f101 100755 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -3,29 +3,29 @@ #include -typedef struct bh_hashmap_node_s +typedef struct BH_HashmapNode { void *key; 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 size; size_t capacity; size_t threshold; - bh_equal_cb_t equal; - bh_hash_cb_t hash; + BH_EqualCallback equal; + BH_HashCallback hash; float factor; }; -static void bh_hashmap_init(bh_hashmap_t *hashmap, - bh_equal_cb_t equal, - bh_hash_cb_t hash) +static void BH_HashmapInit(BH_Hashmap *hashmap, + BH_EqualCallback equal, + BH_HashCallback hash) { memset(hashmap, 0, sizeof(*hashmap)); 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) { @@ -44,10 +44,10 @@ static void bh_hashmap_destroy(bh_hashmap_t *hashmap) } -static int calc_capacity(size_t size, - float factor, - size_t *capacity, - size_t *threshold) +static int BH_CalcCapacity(size_t size, + float factor, + size_t *capacity, + size_t *threshold) { /* Check if we need any capacity at all */ if (!size) @@ -71,54 +71,54 @@ static int calc_capacity(size_t size, } /* 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_OK; } -static void copy_hashmap(bh_hashmap_t *dest, - bh_hashmap_t *src) +static void BH_CopyHashmap(BH_Hashmap *dest, + BH_Hashmap *src) { void *iter; /* Iterate and insert data into hashmap */ - iter = bh_hashmap_iter_next(src, NULL); + iter = BH_HashmapIterNext(src, NULL); while (iter) { void *key, *value; - key = bh_hashmap_iter_key(iter); - value = bh_hashmap_iter_value(iter); - bh_hashmap_insert(dest, key, value); + key = BH_HashmapIterKey(iter); + value = BH_HashmapIterValue(iter); + 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_hash_cb_t hash) +BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal, + BH_HashCallback hash) { - bh_hashmap_t *result; + BH_Hashmap *result; result = malloc(sizeof(*result)); if (result) - bh_hashmap_init(result, equal, hash); + BH_HashmapInit(result, equal, hash); 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); } -void bh_hashmap_clear(bh_hashmap_t *hashmap) +void BH_HashmapClear(BH_Hashmap *hashmap) { if (hashmap->capacity) 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, - size_t size) +int BH_HashmapReserve(BH_Hashmap *hashmap, + size_t size) { - bh_hashmap_t other; + BH_Hashmap other; size_t capacity, threshold; /* 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; /* Calculate new capacity */ - if (calc_capacity(size, hashmap->factor, &capacity, &threshold)) + if (BH_CalcCapacity(size, hashmap->factor, &capacity, &threshold)) return BH_OOM; /* Prevent same size reallocation */ @@ -145,7 +145,7 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap, return BH_OK; /* Initialize new hashmap */ - bh_hashmap_init(&other, hashmap->equal, hashmap->hash); + BH_HashmapInit(&other, hashmap->equal, hashmap->hash); other.factor = hashmap->factor; if (capacity) @@ -170,26 +170,26 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap, memset(other.psls, 0, sizeof(size_t) * other.capacity); /* Copy data from old hashmap to the new hashmap */ - copy_hashmap(&other, hashmap); + BH_CopyHashmap(&other, hashmap); } /* Swap hashmaps */ - bh_hashmap_destroy(hashmap); + BH_HashmapDestroy(hashmap); *hashmap = other; return BH_OK; } -int bh_hashmap_insert(bh_hashmap_t *hashmap, - void *key, - void *value) +int BH_HashmapInsert(BH_Hashmap *hashmap, + void *key, + void *value) { size_t bucket, psl, tmp_psl; - bh_hashmap_node_t item, tmp; + BH_HashmapNode item, tmp; /* Try to stay below 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) return BH_OOM; @@ -228,60 +228,60 @@ int bh_hashmap_insert(bh_hashmap_t *hashmap, } -void bh_hashmap_remove(bh_hashmap_t *hashmap, - void *key) +void BH_HashmapRemove(BH_Hashmap *hashmap, + void *key) { void *iter; - iter = bh_hashmap_iter_at(hashmap, key); + iter = BH_HashmapIterAt(hashmap, key); if (iter) - bh_hashmap_iter_remove(hashmap, iter); + BH_HashmapIterRemove(hashmap, iter); } -int bh_hashmap_at(bh_hashmap_t *hashmap, - void *key, - void **value) +int BH_HashmapAt(BH_Hashmap *hashmap, + void *key, + void **value) { void *iter; - iter = bh_hashmap_iter_at(hashmap, key); + iter = BH_HashmapIterAt(hashmap, key); if (!iter) return BH_NOTFOUND; if (value) - *value = bh_hashmap_iter_value(iter); + *value = BH_HashmapIterValue(iter); return BH_OK; } -int bh_hashmap_empty(bh_hashmap_t *hashmap) +int BH_HashmapEmpty(BH_Hashmap *hashmap) { return !hashmap->size; } -size_t bh_hashmap_size(bh_hashmap_t *hashmap) +size_t BH_HashmapSize(BH_Hashmap *hashmap) { return hashmap->size; } -size_t bh_hashmap_capacity(bh_hashmap_t *hashmap) +size_t BH_HashmapCapacity(BH_Hashmap *hashmap) { return hashmap->capacity; } -float bh_hashmap_factor(bh_hashmap_t *hashmap) +float BH_HashmapFactor(BH_Hashmap *hashmap) { return hashmap->factor; } -void bh_hashmap_set_factor(bh_hashmap_t *hashmap, - float factor) +void BH_HashmapSetFactor(BH_Hashmap *hashmap, + float factor) { /* Limit the factor value to [0.15, 1.0] */ factor = (factor > 1.0f) ? (1.0f) : (factor); @@ -293,8 +293,8 @@ void bh_hashmap_set_factor(bh_hashmap_t *hashmap, } -void *bh_hashmap_iter_at(bh_hashmap_t *hashmap, - void *key) +void *BH_HashmapIterAt(BH_Hashmap *hashmap, + void *key) { 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 *iter) +void *BH_HashmapIterNext(BH_Hashmap *hashmap, + void *iter) { - bh_hashmap_node_t *item; + BH_HashmapNode *item; - item = (bh_hashmap_node_t *)iter; + item = (BH_HashmapNode *)iter; while (1) { /* Advance or set iterator to the first element */ @@ -346,8 +346,8 @@ void *bh_hashmap_iter_next(bh_hashmap_t *hashmap, } -void bh_hashmap_iter_remove(bh_hashmap_t *hashmap, - void *iter) +void BH_HashmapIterRemove(BH_Hashmap *hashmap, + void *iter) { 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 */ hashmap->size--; - bucket = (bh_hashmap_node_t *)iter - hashmap->data; + bucket = (BH_HashmapNode *)iter - hashmap->data; next_bucket = (bucket + 1) & (hashmap->capacity - 1); /* 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; } diff --git a/src/io.c b/src/io.c index 7fc04cf..8322707 100644 --- a/src/io.c +++ b/src/io.c @@ -4,21 +4,20 @@ #define BUFFER_SIZE (sizeof(char *)) - -struct bh_io_s +struct BH_IO { - bh_io_func_t func; + BH_IOCallback cb; }; -bh_io_t *bh_io_new(bh_io_func_t func, - void *data) +BH_IO *BH_IONew(BH_IOCallback cb, + void *data) { size_t requested; - bh_io_t *io; + BH_IO *io; /* 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; /* Allocate space for the IO device */ @@ -27,8 +26,8 @@ bh_io_t *bh_io_new(bh_io_func_t func, return NULL; /* Initialize IO device */ - io->func = func; - if (func(io + 1, BH_IO_INIT_CB, data, NULL)) + io->cb = cb; + if (cb(io + 1, BH_IO_INIT_CB, data, NULL)) { free(io); 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 */ if (!io) return; /* 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 */ free(io); } -const char *bh_io_classname(bh_io_t *io) +const char *BH_IOClassname(BH_IO *io) { const char *name; if (!io) 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; return name; @@ -69,33 +68,33 @@ error: } -int bh_io_open(bh_io_t *io, - int mode) +int BH_IOOpen(BH_IO *io, + int mode) { /* Prevent working with NULL io */ if (!io) return BH_ERROR; /* 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 */ if (!io) return BH_ERROR; /* 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, - char *buffer, - size_t size, - size_t *actual) +int BH_IORead(BH_IO *io, + char *buffer, + size_t size, + size_t *actual) { int code; @@ -104,7 +103,7 @@ int bh_io_read(bh_io_t *io, return BH_ERROR; /* 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 (actual) @@ -114,10 +113,10 @@ int bh_io_read(bh_io_t *io, } -int bh_io_write(bh_io_t *io, - const char *buffer, - size_t size, - size_t *actual) +int BH_IOWrite(BH_IO *io, + const char *buffer, + size_t size, + size_t *actual) { int code; @@ -126,7 +125,7 @@ int bh_io_write(bh_io_t *io, return BH_ERROR; /* 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 (actual) @@ -135,10 +134,10 @@ int bh_io_write(bh_io_t *io, return code; } -int bh_io_peek(bh_io_t *io, - char *buffer, - size_t size, - size_t *actual) +int BH_IOPeek(BH_IO *io, + char *buffer, + size_t size, + size_t *actual) { int code; @@ -147,7 +146,7 @@ int bh_io_peek(bh_io_t *io, return BH_ERROR; /* 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 (actual) @@ -157,71 +156,71 @@ int bh_io_peek(bh_io_t *io, } -int bh_io_tell(bh_io_t *io, - int64_t *position) +int BH_IOTell(BH_IO *io, + int64_t *position) { /* Prevent working with NULL io */ if (!io) return BH_ERROR; /* 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, - int64_t position, - int direction) +int BH_IOSeek(BH_IO *io, + int64_t position, + int direction) { /* Prevent working with NULL io */ if (!io) return BH_ERROR; /* 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 */ if (!io) return BH_ERROR; /* 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, - int64_t *size) +int BH_IOSize(BH_IO *io, + int64_t *size) { /* Prevent working with NULL io */ if (!io) return BH_ERROR; /* 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 */ if (!io) return BH_IO_FLAG_ERROR; /* 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 */ if (!io) return BH_OK; /* 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); } diff --git a/src/math.c b/src/math.c index 2e1d0cd..bdb90be 100644 --- a/src/math.c +++ b/src/math.c @@ -1,858 +1,728 @@ #include #include +#include -void bh_point4f_add(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void BH_Vec4fAdd(const float *a, + const float *b, + float *out) { - result->x = a->x + b->x; - result->y = a->y + b->y; - result->z = a->z + b->z; - result->w = a->w + b->w; + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + out[3] = a[3] + b[3]; } -void bh_point4f_sub(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void BH_Vec4fSub(const float *a, + const float *b, + float *out) { - result->x = a->x - b->x; - result->y = a->y - b->y; - result->z = a->z - b->z; - result->w = a->w - b->w; + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + out[3] = a[3] - b[3]; } -void bh_point4f_mul(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void BH_Vec4fMul(const float *a, + const float *b, + float *out) { - result->x = a->x * b->x; - result->y = a->y * b->y; - result->z = a->z * b->z; - result->w = a->w * b->w; + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + out[2] = a[2] * b[2]; + out[3] = a[3] * b[3]; } -void bh_point4f_scale(const bh_point4f_t *a, - float b, - bh_point4f_t *result) +void BH_Vec4fScale(const float *a, + const float b, + float *out) { - result->x = a->x * b; - result->y = a->y * b; - result->z = a->z * b; - result->w = a->w * b; + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; } -void bh_point4f_madd(const bh_point4f_t *a, - const bh_point4f_t *b, - const bh_point4f_t *c, - bh_point4f_t *result) +void BH_Vec4fMulAdd(const float *a, + const float *b, + const float *c, + float *out) { - result->x = a->x * b->x + c->x; - result->y = a->y * b->y + c->y; - result->z = a->z * b->z + c->z; - result->w = a->w * b->w + c->w; + out[0] = a[0] * b[0] + c[0]; + out[1] = a[1] * b[1] + c[1]; + out[2] = a[2] * b[2] + c[2]; + out[3] = a[3] * b[3] + c[3]; } -void bh_point4f_negate(const bh_point4f_t *in, - bh_point4f_t *result) +void BH_Vec4fNegate(const float *in, + float *out) { - result->x = -in->x; - result->y = -in->y; - result->z = -in->z; - result->w = -in->w; + out[0] = -in[0]; + out[1] = -in[1]; + out[2] = -in[2]; + out[3] = -in[3]; } -float bh_point4f_dot(const bh_point4f_t *a, - const bh_point4f_t *b) +float BH_Vec4fDot(const float *a, + const float *b) { - return a->x * b->x + a->y * b->y + a->z * b->z + a->w * b->w; + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; } -float bh_point4f_dot3(const bh_point4f_t *a, - const bh_point4f_t *b) +float BH_Vec4fLength(const float *in) { - return a->x * b->x + a->y * b->y + a->z * b->z; + return sqrt(BH_Vec4fDot(in, in)); } -void bh_point4f_cross(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void BH_Vec4fNormal(const float *in, + float *out) { - bh_point4f_t tmp; - - tmp.x = a->y * b->z - a->z * b->y; - tmp.y = a->z * b->x - a->x * b->z; - tmp.z = a->x * b->y - a->y * b->x; - tmp.w = 0.0f; - - *result = tmp; -} - - -float bh_point4f_length(const bh_point4f_t *in) -{ - return sqrtf(bh_point4f_dot(in, in)); -} - - -void bh_point4f_normal(const bh_point4f_t *in, - bh_point4f_t *result) -{ - float length; - - length = 1.0f / bh_point4f_length(in); - bh_point4f_scale(in, length, result); -} - - -void bh_point4f_min(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) -{ - if (a->x < b->x) result->x = a->x; else result->x = b->x; - if (a->y < b->y) result->y = a->y; else result->y = b->y; - if (a->z < b->z) result->z = a->z; else result->z = b->z; - if (a->w < b->w) result->w = a->w; else result->w = b->w; + BH_Vec4fScale(in, 1.0f / BH_Vec4fLength(in), out); } -void bh_point4f_max(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void BH_Vec4fMin(const float *a, + const float *b, + float *out) { - if (a->x > b->x) result->x = a->x; else result->x = b->x; - if (a->y > b->y) result->y = a->y; else result->y = b->y; - if (a->z > b->z) result->z = a->z; else result->z = b->z; - if (a->w > b->w) result->w = a->w; else result->w = b->w; + if (a[0] < b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] < b[1]) out[1] = a[1]; else out[1] = b[1]; + if (a[2] < b[2]) out[2] = a[2]; else out[2] = b[2]; + if (a[3] < b[3]) out[3] = a[3]; else out[3] = b[3]; } -void bh_point4f_lerp(const bh_point4f_t *a, - const bh_point4f_t *b, - float t, - bh_point4f_t *result) +void BH_Vec4fMax(const float *a, + const float *b, + float *out) { - bh_point4f_t tmp; - - bh_point4f_sub(b, a, &tmp); - bh_point4f_scale(&tmp, t, &tmp); - bh_point4f_add(a, &tmp, result); + if (a[0] > b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] > b[1]) out[1] = a[1]; else out[1] = b[1]; + if (a[2] > b[2]) out[2] = a[2]; else out[2] = b[2]; + if (a[3] > b[3]) out[3] = a[3]; else out[3] = b[3]; } -void bh_point4f_slerp(const bh_point4f_t *a, - const bh_point4f_t *b, - float t, - bh_point4f_t *result) +void BH_Vec4fLerp(const float *a, + const float *b, + float t, + float *out) { - float angle, denom; - bh_point4f_t from, to; + float tmp[4]; - angle = acosf(bh_point4f_dot(a, b)); - - /* Special case - reducing to linear interpolation */ - if (angle == 0.0f) - { - bh_point4f_lerp(a, b, t, result); - } - else - { - denom = 1.0f / sinf(angle); - bh_point4f_scale(a, sinf((1 - t) * angle) * denom, &from); - bh_point4f_scale(b, sinf(t * angle) * denom, &to); - bh_point4f_add(&from, &to, result); - } + BH_Vec4fSub(b, a, tmp); + BH_Vec4fScale(tmp, t, tmp); + BH_Vec4fAdd(a, tmp, out); } -void bh_point3f_add(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void BH_Vec3fAdd(const float *a, + const float *b, + float *out) { - result->x = a->x + b->x; - result->y = a->y + b->y; - result->z = a->z + b->z; + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; } -void bh_point3f_sub(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void BH_Vec3fSub(const float *a, + const float *b, + float *out) { - result->x = a->x - b->x; - result->y = a->y - b->y; - result->z = a->z - b->z; + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; } -void bh_point3f_mul(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void BH_Vec3fMul(const float *a, + const float *b, + float *out) { - result->x = a->x * b->x; - result->y = a->y * b->y; - result->z = a->z * b->z; + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + out[2] = a[2] * b[2]; } -void bh_point3f_scale(const bh_point3f_t *a, - float b, - bh_point3f_t *result) +void BH_Vec3fScale(const float *a, + const float b, + float *out) { - result->x = a->x * b; - result->y = a->y * b; - result->z = a->z * b; + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; } -void bh_point3f_madd(const bh_point3f_t *a, - const bh_point3f_t *b, - const bh_point3f_t *c, - bh_point3f_t *result) +void BH_Vec3fMulAdd(const float *a, + const float *b, + const float *c, + float *out) { - result->x = a->x * b->x + c->x; - result->y = a->y * b->y + c->y; - result->z = a->z * b->z + c->z; + out[0] = a[0] * b[0] + c[0]; + out[1] = a[1] * b[1] + c[1]; + out[2] = a[2] * b[2] + c[2]; } -void bh_point3f_negate(const bh_point3f_t *in, - bh_point3f_t *result) +void BH_Vec3fNegate(const float *in, + float *out) { - result->x = -in->x; - result->y = -in->y; - result->z = -in->z; + out[0] = -in[0]; + out[1] = -in[1]; + out[2] = -in[2]; } -float bh_point3f_dot(const bh_point3f_t *a, - const bh_point3f_t *b) +float BH_Vec3fDot(const float *a, + const float *b) { - return a->x * b->x + a->y * b->y + a->z * b->z; + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } -void bh_point3f_cross(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void BH_Vec3fCross(const float *a, + const float *b, + float *out) { - bh_point3f_t tmp; - - tmp.x = a->y * b->z - a->z * b->y; - tmp.y = a->z * b->x - a->x * b->z; - tmp.z = a->x * b->y - a->y * b->x; + float tmp[3]; - *result = tmp; + tmp[0] = a[1] * b[2] - a[2] * b[1]; + tmp[1] = a[2] * b[0] - a[0] * b[2]; + tmp[2] = a[0] * b[1] - a[1] * b[0]; + memcpy(out, tmp, sizeof(tmp)); } -float bh_point3f_length(const bh_point3f_t *in) +float BH_Vec3fLength(const float *in) { - return sqrtf(bh_point3f_dot(in, in)); + return sqrt(BH_Vec3fDot(in, in)); } -void bh_point3f_normal(const bh_point3f_t *in, - bh_point3f_t *result) +void BH_Vec3fNormal(const float *in, + float *out) { - float length; - - length = 1.0f / bh_point3f_length(in); - bh_point3f_scale(in, length, result); + BH_Vec3fScale(in, 1.0f / BH_Vec3fLength(in), out); } -void bh_point3f_min(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void BH_Vec3fMin(const float *a, + const float *b, + float *out) { - if (a->x < b->x) result->x = a->x; else result->x = b->x; - if (a->y < b->y) result->y = a->y; else result->y = b->y; - if (a->z < b->z) result->z = a->z; else result->z = b->z; + if (a[0] < b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] < b[1]) out[1] = a[1]; else out[1] = b[1]; + if (a[2] < b[2]) out[2] = a[2]; else out[2] = b[2]; } -void bh_point3f_max(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void BH_Vec3fMax(const float *a, + const float *b, + float *out) { - if (a->x > b->x) result->x = a->x; else result->x = b->x; - if (a->y > b->y) result->y = a->y; else result->y = b->y; - if (a->z > b->z) result->z = a->z; else result->z = b->z; + if (a[0] > b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] > b[1]) out[1] = a[1]; else out[1] = b[1]; + if (a[2] > b[2]) out[2] = a[2]; else out[2] = b[2]; } -void bh_point3f_lerp(const bh_point3f_t *a, - const bh_point3f_t *b, - float t, - bh_point3f_t *result) +void BH_Vec3fLerp(const float *a, + const float *b, + float t, + float *out) { - bh_point3f_t tmp; + float tmp[3]; - bh_point3f_sub(b, a, &tmp); - bh_point3f_scale(&tmp, t, &tmp); - bh_point3f_add(a, &tmp, result); + BH_Vec3fSub(b, a, tmp); + BH_Vec3fScale(tmp, t, tmp); + BH_Vec3fAdd(a, tmp, out); } -void bh_point3f_slerp(const bh_point3f_t *a, - const bh_point3f_t *b, - float t, - bh_point3f_t *result) +void BH_Vec2fAdd(const float *a, + const float *b, + float *out) { - float angle, denom; - bh_point3f_t from, to; - - angle = acosf(bh_point3f_dot(a, b)); - - /* Special case - reducing to linear interpolation */ - if (angle == 0.0f) - { - bh_point3f_lerp(a, b, t, result); - } - else - { - denom = 1.0f / sinf(angle); - bh_point3f_scale(a, sinf((1 - t) * angle) * denom, &from); - bh_point3f_scale(b, sinf(t * angle) * denom, &to); - bh_point3f_add(&from, &to, result); - } + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; } -void bh_point2f_add(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void BH_Vec2fSub(const float *a, + const float *b, + float *out) { - result->x = a->x + b->x; - result->y = a->y + b->y; + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; } -void bh_point2f_sub(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void BH_Vec2fMul(const float *a, + const float *b, + float *out) { - result->x = a->x - b->x; - result->y = a->y - b->y; + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; } -void bh_point2f_mul(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void BH_Vec2fScale(const float *a, + const float b, + float *out) { - result->x = a->x * b->x; - result->y = a->y * b->y; + out[0] = a[0] * b; + out[1] = a[1] * b; } -void bh_point2f_scale(const bh_point2f_t *a, - float b, - bh_point2f_t *result) +void BH_Vec2fMulAdd(const float *a, + const float *b, + const float *c, + float *out) { - result->x = a->x * b; - result->y = a->y * b; + out[0] = a[0] * b[0] + c[0]; + out[1] = a[1] * b[1] + c[1]; } -void bh_point2f_madd(const bh_point2f_t *a, - const bh_point2f_t *b, - const bh_point2f_t *c, - bh_point2f_t *result) +void BH_Vec2fNegate(const float *in, + float *out) { - result->x = a->x * b->x + c->x; - result->y = a->y * b->y + c->y; + out[0] = -in[0]; + out[1] = -in[1]; } -void bh_point2f_negate(const bh_point2f_t *in, - bh_point2f_t *result) +float BH_Vec2fDot(const float *a, + const float *b) { - result->x = -in->x; - result->y = -in->y; + return a[0] * b[0] + a[1] * b[1]; } -float bh_point2f_dot(const bh_point2f_t *a, - const bh_point2f_t *b) +float BH_Vec2fCross(const float *a, + const float *b) { - return a->x * b->x + a->y * b->y; + return a[0] * b[1] - a[1] * b[0]; } -float bh_point2f_cross(const bh_point2f_t *a, - const bh_point2f_t *b) +float BH_Vec2fLength(const float *in) { - return a->x * b->y - a->y * b->x; + return sqrt(BH_Vec2fDot(in, in)); } -float bh_point2f_length(const bh_point2f_t *in) +void BH_Vec2fNormal(const float *in, + float *out) { - return sqrtf(bh_point2f_dot(in, in)); + BH_Vec2fScale(in, 1.0f / BH_Vec2fLength(in), out); } -void bh_point2f_normal(const bh_point2f_t *in, - bh_point2f_t *result) +void BH_Vec2fMin(const float *a, + const float *b, + float *out) { - float length; - - length = 1.0f / bh_point2f_length(in); - bh_point2f_scale(in, length, result); + if (a[0] < b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] < b[1]) out[1] = a[1]; else out[1] = b[1]; } -void bh_point2f_min(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void BH_Vec2fMax(const float *a, + const float *b, + float *out) { - if (a->x < b->x) result->x = a->x; else result->x = b->x; - if (a->y < b->y) result->y = a->y; else result->y = b->y; + if (a[0] > b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] > b[1]) out[1] = a[1]; else out[1] = b[1]; } -void bh_point2f_max(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void BH_Vec2fLerp(const float *a, + const float *b, + float t, + float *out) { - if (a->x > b->x) result->x = a->x; else result->x = b->x; - if (a->y > b->y) result->y = a->y; else result->y = b->y; -} - - -void bh_point2f_lerp(const bh_point2f_t *a, - const bh_point2f_t *b, - float t, - bh_point2f_t *result) -{ - bh_point2f_t tmp; - - bh_point2f_sub(b, a, &tmp); - bh_point2f_scale(&tmp, t, &tmp); - bh_point2f_add(a, &tmp, result); -} - - -void bh_point2f_slerp(const bh_point2f_t *a, - const bh_point2f_t *b, - float t, - bh_point2f_t *result) -{ - float angle, denom; - bh_point2f_t from, to; - - angle = acosf(bh_point2f_dot(a, b)); - - /* Special case - reducing to linear interpolation */ - if (angle == 0.0f) - { - bh_point2f_lerp(a, b, t, result); - } - else { - denom = 1.0f / sinf(angle); - bh_point2f_scale(a, sinf((1 - t) * angle) * denom, &from); - bh_point2f_scale(b, sinf(t * angle) * denom, &to); - bh_point2f_add(&from, &to, result); - } -} + float tmp[2]; - -void bh_point4i_add(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) -{ - result->x = a->x + b->x; - result->y = a->y + b->y; - result->z = a->z + b->z; - result->w = a->w + b->w; + BH_Vec2fSub(b, a, tmp); + BH_Vec2fScale(tmp, t, tmp); + BH_Vec2fAdd(a, tmp, out); } -void bh_point4i_sub(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void BH_Vec4iAdd(const int *a, + const int *b, + int *out) { - result->x = a->x - b->x; - result->y = a->y - b->y; - result->z = a->z - b->z; - result->w = a->w - b->w; + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; + out[3] = a[3] + b[3]; } -void bh_point4i_mul(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void BH_Vec4iSub(const int *a, + const int *b, + int *out) { - result->x = a->x * b->x; - result->y = a->y * b->y; - result->z = a->z * b->z; - result->w = a->w * b->w; + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; + out[3] = a[3] - b[3]; } -void bh_point4i_scale(const bh_point4i_t *a, - int b, - bh_point4i_t *result) +void BH_Vec4iMul(const int *a, + const int *b, + int *out) { - result->x = a->x * b; - result->y = a->y * b; - result->z = a->z * b; - result->w = a->w * b; + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + out[2] = a[2] * b[2]; + out[3] = a[3] * b[3]; } -void bh_point4i_madd(const bh_point4i_t *a, - const bh_point4i_t *b, - const bh_point4i_t *c, - bh_point4i_t *result) +void BH_Vec4iScale(const int *a, + int b, + int *out) { - result->x = a->x * b->x + c->x; - result->y = a->y * b->y + c->y; - result->z = a->z * b->z + c->z; - result->w = a->w * b->w + c->w; + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; + out[3] = a[3] * b; } -void bh_point4i_negate(const bh_point4i_t *in, - bh_point4i_t *result) +void BH_Vec4iMulAdd(const int *a, + const int *b, + const int *c, + int *out) { - result->x = -in->x; - result->y = -in->y; - result->z = -in->z; - result->w = -in->w; + out[0] = a[0] * b[0] + c[0]; + out[1] = a[1] * b[1] + c[1]; + out[2] = a[2] * b[2] + c[2]; + out[3] = a[3] * b[3] + c[3]; } -void bh_point4i_min(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void BH_Vec4iNegate(const int *in, + int *out) { - if (a->x < b->x) result->x = a->x; else result->x = b->x; - if (a->y < b->y) result->y = a->y; else result->y = b->y; - if (a->z < b->z) result->z = a->z; else result->z = b->z; - if (a->w < b->w) result->w = a->w; else result->w = b->w; + out[0] = -in[0]; + out[1] = -in[1]; + out[2] = -in[2]; + out[3] = -in[3]; } -void bh_point4i_max(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void BH_Vec4iMin(const int *a, + const int *b, + int *out) { - if (a->x > b->x) result->x = a->x; else result->x = b->x; - if (a->y > b->y) result->y = a->y; else result->y = b->y; - if (a->z > b->z) result->z = a->z; else result->z = b->z; - if (a->w > b->w) result->w = a->w; else result->w = b->w; + if (a[0] < b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] < b[1]) out[1] = a[1]; else out[1] = b[1]; + if (a[2] < b[2]) out[2] = a[2]; else out[2] = b[2]; + if (a[3] < b[3]) out[3] = a[3]; else out[3] = b[3]; } -void bh_point4i_lerp(const bh_point4i_t *a, - const bh_point4i_t *b, - float t, - bh_point4i_t *result) +void BH_Vec4iMax(const int *a, + const int *b, + int *out) { - bh_point4i_t tmp; - - tmp.x = (b->x - a->x) * t; - tmp.y = (b->y - a->y) * t; - tmp.z = (b->z - a->z) * t; - tmp.w = (b->w - a->w) * t; - - bh_point4i_add(a, &tmp, result); + if (a[0] > b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] > b[1]) out[1] = a[1]; else out[1] = b[1]; + if (a[2] > b[2]) out[2] = a[2]; else out[2] = b[2]; + if (a[3] > b[3]) out[3] = a[3]; else out[3] = b[3]; } -void bh_point3i_add(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void BH_Vec3iAdd(const int *a, + const int *b, + int *out) { - result->x = a->x + b->x; - result->y = a->y + b->y; - result->z = a->z + b->z; + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; + out[2] = a[2] + b[2]; } -void bh_point3i_sub(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void BH_Vec3iSub(const int *a, + const int *b, + int *out) { - result->x = a->x - b->x; - result->y = a->y - b->y; - result->z = a->z - b->z; + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; + out[2] = a[2] - b[2]; } -void bh_point3i_mul(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void BH_Vec3iMul(const int *a, + const int *b, + int *out) { - result->x = a->x * b->x; - result->y = a->y * b->y; - result->z = a->z * b->z; + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; + out[2] = a[2] * b[2]; } -void bh_point3i_scale(const bh_point3i_t *a, - int b, - bh_point3i_t *result) +void BH_Vec3iScale(const int *a, + int b, + int *out) { - result->x = a->x * b; - result->y = a->y * b; - result->z = a->z * b; + out[0] = a[0] * b; + out[1] = a[1] * b; + out[2] = a[2] * b; } -void bh_point3i_madd(const bh_point3i_t *a, - const bh_point3i_t *b, - const bh_point3i_t *c, - bh_point3i_t *result) +void BH_Vec3iMulAdd(const int *a, + const int *b, + const int *c, + int *out) { - result->x = a->x * b->x + c->x; - result->y = a->y * b->y + c->y; - result->z = a->z * b->z + c->z; + out[0] = a[0] * b[0] + c[0]; + out[1] = a[1] * b[1] + c[1]; + out[2] = a[2] * b[2] + c[2]; } -void bh_point3i_negate(const bh_point3i_t *in, - bh_point3i_t *result) +void BH_Vec3iNegate(const int *in, + int *out) { - result->x = -in->x; - result->y = -in->y; - result->z = -in->z; + out[0] = -in[0]; + out[1] = -in[1]; + out[2] = -in[2]; } -void bh_point3i_min(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void BH_Vec3iMin(const int *a, + const int *b, + int *out) { - if (a->x < b->x) result->x = a->x; else result->x = b->x; - if (a->y < b->y) result->y = a->y; else result->y = b->y; - if (a->z < b->z) result->z = a->z; else result->z = b->z; + if (a[0] < b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] < b[1]) out[1] = a[1]; else out[1] = b[1]; + if (a[2] < b[2]) out[2] = a[2]; else out[2] = b[2]; } -void bh_point3i_max(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void BH_Vec3iMax(const int *a, + const int *b, + int *out) { - if (a->x > b->x) result->x = a->x; else result->x = b->x; - if (a->y > b->y) result->y = a->y; else result->y = b->y; - if (a->z > b->z) result->z = a->z; else result->z = b->z; + if (a[0] > b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] > b[1]) out[1] = a[1]; else out[1] = b[1]; + if (a[2] > b[2]) out[2] = a[2]; else out[2] = b[2]; } -void bh_point3i_lerp(const bh_point3i_t *a, - const bh_point3i_t *b, - float t, - bh_point3i_t *result) +void BH_Vec2iAdd(const int *a, + const int *b, + int *out) { - bh_point3i_t tmp; - - tmp.x = (b->x - a->x) * t; - tmp.y = (b->y - a->y) * t; - tmp.z = (b->z - a->z) * t; - - bh_point3i_add(a, &tmp, result); + out[0] = a[0] + b[0]; + out[1] = a[1] + b[1]; } -void bh_point2i_add(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void BH_Vec2iSub(const int *a, + const int *b, + int *out) { - result->x = a->x + b->x; - result->y = a->y + b->y; + out[0] = a[0] - b[0]; + out[1] = a[1] - b[1]; } -void bh_point2i_sub(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void BH_Vec2iMul(const int *a, + const int *b, + int *out) { - result->x = a->x - b->x; - result->y = a->y - b->y; + out[0] = a[0] * b[0]; + out[1] = a[1] * b[1]; } -void bh_point2i_mul(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void BH_Vec2iScale(const int *a, + int b, + int *out) { - result->x = a->x * b->x; - result->y = a->y * b->y; + out[0] = a[0] * b; + out[1] = a[1] * b; } -void bh_point2i_scale(const bh_point2i_t *a, - int b, - bh_point2i_t *result) +void BH_Vec2iMulAdd(const int *a, + const int *b, + const int *c, + int *out) { - result->x = a->x * b; - result->y = a->y * b; + out[0] = a[0] * b[0] + c[0]; + out[1] = a[1] * b[1] + c[1]; } -void bh_point2i_madd(const bh_point2i_t *a, - const bh_point2i_t *b, - const bh_point2i_t *c, - bh_point2i_t *result) +void BH_Vec2iNegate(const int *in, + int *out) { - result->x = a->x * b->x + c->x; - result->y = a->y * b->y + c->y; + out[0] = -in[0]; + out[1] = -in[1]; } -void bh_point2i_negate(const bh_point2i_t *in, - bh_point2i_t *result) +void BH_Vec2iMin(const int *a, + const int *b, + int *out) { - result->x = -in->x; - result->y = -in->y; + if (a[0] < b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] < b[1]) out[1] = a[1]; else out[1] = b[1]; } -void bh_point2i_min(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void BH_Vec2iMax(const int *a, + const int *b, + int *out) { - if (a->x < b->x) result->x = a->x; else result->x = b->x; - if (a->y < b->y) result->y = a->y; else result->y = b->y; + if (a[0] > b[0]) out[0] = a[0]; else out[0] = b[0]; + if (a[1] > b[1]) out[1] = a[1]; else out[1] = b[1]; } -void bh_point2i_max(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void BH_Quat4fIdentity(float *out) { - if (a->x > b->x) result->x = a->x; else result->x = b->x; - if (a->y > b->y) result->y = a->y; else result->y = b->y; + static const float ident[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + memcpy(out, ident, sizeof(ident)); } -void bh_point2i_lerp(const bh_point2i_t *a, - const bh_point2i_t *b, - float t, - bh_point2i_t *result) +void BH_Quat4fConjugate(const float *in, + float *out) { - bh_point2i_t tmp; - - tmp.x = (b->x - a->x) * t; - tmp.y = (b->y - a->y) * t; - - bh_point2i_add(a, &tmp, result); + out[0] = -in[0]; + out[1] = -in[1]; + out[2] = -in[2]; + out[3] = in[3]; } -void bh_quat_identity(bh_quat_t *result) +void BH_Quat4fInverse(const float *in, + float *out) { - static const bh_quat_t ident = {0.0f, 0.0f, 0.0f, 1.0f}; - - *result = ident; -} - + float dot; -void bh_quat_conjugate(const bh_quat_t *in, - bh_quat_t *result) -{ - result->x = -in->x; - result->y = -in->y; - result->z = -in->z; - result->w = in->w; + dot = BH_Vec4fDot(in, in); + BH_Quat4fConjugate(in, out); + BH_Quat4fScale(out, 1.0f / dot, out); } -void bh_quat_inverse(const bh_quat_t *in, - bh_quat_t *result) +void BH_Quat4fMul(const float *a, + const float *b, + float *out) { - float length; + float tmp1[4], tmp2[4], tmp3[4]; + float w; - length = bh_quat_dot(in, in); - bh_quat_conjugate(in, result); - bh_quat_scale(result, 1.0f / length, result); + w = a[3] * b[3] - BH_Vec3fDot(a, b); + BH_Vec4fScale(a, b[3], tmp1); + BH_Vec4fScale(b, a[3], tmp2); + BH_Vec3fCross(a, b, tmp3); + BH_Vec4fAdd(tmp1, tmp2, out); + BH_Vec4fAdd(tmp3, out, out); + out[3] = w; } -void bh_quat_mul(const bh_quat_t *a, - const bh_quat_t *b, - bh_quat_t *result) +void BH_Quat4fSlerp(const float *a, + const float *b, + float t, + float *out) { - bh_quat_t tmp1, tmp2, tmp3; - float w; + float angle, denom; + float from[4], to[4]; - w = a->w * b->w - bh_point4f_dot3(a, b); + angle = acos(BH_Vec4fDot(a, b)); + if (angle == 0.0f) + { + BH_Vec4fLerp(a, b, t, out); + return; + } - bh_point4f_scale(a, b->w, &tmp1); - bh_point4f_scale(b, a->w, &tmp2); - bh_point4f_cross(a, b, &tmp3); - bh_point4f_add(&tmp1, &tmp2, result); - bh_point4f_add(&tmp3, result, result); - result->w = w; + denom = 1.0f / sin(angle); + BH_Vec4fScale(a, sin((1 - t) * angle) * denom, from); + BH_Vec4fScale(b, sin(t * angle) * denom, to); + BH_Vec4fAdd(from, to, out); } -void bh_quat_set_euler(float roll, - float pitch, - float yaw, - bh_quat_t *result) +void BH_Quat4fFromEuler(float roll, + float pitch, + float yaw, + float *out) { float cr, cp, cy, sr, sp, sy; - cr = cosf(roll / 2.0f); - cp = cosf(pitch / 2.0f); - cy = cosf(yaw / 2.0f); - sr = sinf(roll / 2.0f); - sp = sinf(pitch / 2.0f); - sy = sinf(yaw / 2.0f); + cr = cos(roll / 2.0f); + cp = cos(pitch / 2.0f); + cy = cos(yaw / 2.0f); + sr = sin(roll / 2.0f); + sp = sin(pitch / 2.0f); + sy = sin(yaw / 2.0f); - result->x = sr * cp * cy - cr * sp * sy; - result->y = cr * sp * cy + sr * cp * sy; - result->z = cr * cp * sy - sr * sp * cy; - result->w = cr * cp * cy + sr * sp * sy; + out[0] = sr * cp * cy - cr * sp * sy; + out[1] = cr * sp * cy + sr * cp * sy; + out[2] = cr * cp * sy - sr * sp * cy; + out[3] = cr * cp * cy + sr * sp * sy; } -void bh_quat_set_rotation(const bh_point3f_t *axis, - float angle, - bh_quat_t *result) +void BH_Quat4fFromAxis(const float *axis, + float angle, + float *out) { float c, s; - c = cosf(angle / 2.0f); - s = sinf(angle / 2.0f); + c = cos(angle / 2.0f); + s = sin(angle / 2.0f); - result->x = axis->x * s; - result->y = axis->y * s; - result->z = axis->z * s; - result->w = c; + out[0] = axis[0] * s; + out[1] = axis[1] * s; + out[2] = axis[2] * s; + out[3] = c; } -void bh_quat_euler(const bh_quat_t *in, - float *roll, - float *pitch, - float *yaw) +void BH_Quat4fToEuler(const float *in, + float *roll, + float *pitch, + float *yaw) { float ww, xw, yw, zw, xx, xy, xz, yy, yz, zz, angle; - xx = in->x * in->x; - xy = in->x * in->y; - xz = in->x * in->z; - xw = in->x * in->w; - yy = in->y * in->y; - yz = in->y * in->z; - yw = in->y * in->w; - zz = in->z * in->z; - zw = in->z * in->w; - ww = in->w * in->w; + xx = in[0] * in[0]; + xy = in[0] * in[1]; + xz = in[0] * in[2]; + xw = in[0] * in[3]; + yy = in[1] * in[1]; + yz = in[1] * in[2]; + yw = in[1] * in[3]; + zz = in[2] * in[2]; + zw = in[2] * in[3]; + ww = in[3] * in[3]; angle = 2.0f * (yw - xz); if (angle > 1.0f) @@ -860,403 +730,348 @@ void bh_quat_euler(const bh_quat_t *in, if (angle < -1.0f) angle = -1.0f; - *pitch = asinf(angle); + *pitch = asin(angle); if (*pitch == (M_PI / 2.0f)) { *roll = 0.0f; - *yaw = -2.0f * atan2f(in->x, in->w); + *yaw = -2.0f * atan2(in[0], in[3]); } else if (*pitch == (M_PI / -2.0f)) { *roll = 0.0f; - *yaw = 2.0f * atan2f(in->x, in->w); + *yaw = 2.0f * atan2(in[0], in[3]); } else { - *roll = atan2f(2.0f * (xw + yz), ww - xx - yy + zz); - *yaw = atan2f(2.0f * (zw + xy), ww + xx - yy - zz); + *roll = atan2(2.0f * (xw + yz), ww - xx - yy + zz); + *yaw = atan2(2.0f * (zw + xy), ww + xx - yy - zz); } } -void bh_quat_rotation(const bh_quat_t *in, - bh_point3f_t *axis, - float *angle) +void BH_Quat4fToAxis(const float *in, + float *axis, + float *angle) { - float tmp; - - *angle = 2.0f * acosf(in->w); - tmp = sqrtf(1.0f - in->w * in->w); + *angle = 2.0f * acos(in[3]); if (*angle == 0.0f) { - axis->x = 1.0f; - axis->y = 0.0f; - axis->z = 0.0f; + axis[0] = 1.0f; + axis[1] = 0.0f; + axis[2] = 0.0f; } else { - axis->x = in->x / tmp; - axis->y = in->y / tmp; - axis->z = in->z / tmp; + float tmp; + + tmp = sqrt(1.0f - in[3] * in[3]); + axis[0] = in[0] / tmp; + axis[1] = in[1] / tmp; + axis[2] = in[2] / tmp; } } -void bh_quat_matrix(const bh_quat_t *in, - bh_matrix4f_t *result) +void BH_Quat4fToMat4f(const float *in, + float *out) { float xx, xy, xz, xw, yy, yz, yw, zz, zw; - xx = in->x * in->x; - xy = in->x * in->y; - xz = in->x * in->z; - xw = in->x * in->w; - yy = in->y * in->y; - yz = in->y * in->z; - yw = in->y * in->w; - zz = in->z * in->z; - zw = in->z * in->w; - - bh_matrix4f_identity(result); - result->x.x = 1.0f - 2.0f * (yy + zz); - result->x.y = 2.0f * (xy + zw); - result->x.z = 2.0f * (xz - yw); - result->y.x = 2.0f * (xy - zw); - result->y.y = 1.0f - 2.0f * (xx + zz); - result->y.z = 2.0f * (yz + xw); - result->z.x = 2.0f * (xz + yw); - result->z.y = 2.0f * (yz - xw); - result->z.z = 1.0f - 2.0f * (xx + yy); -} - - -void bh_matrix4f_identity(bh_matrix4f_t *result) -{ - static const bh_matrix4f_t ident = { - {1.0f, 0.0f, 0.0f, 0.0f}, - {0.0f, 1.0f, 0.0f, 0.0f}, - {0.0f, 0.0f, 1.0f, 0.0f}, - {0.0f, 0.0f, 0.0f, 1.0f} + xx = in[0] * in[0]; + xy = in[0] * in[1]; + xz = in[0] * in[2]; + xw = in[0] * in[3]; + yy = in[1] * in[1]; + yz = in[1] * in[2]; + yw = in[1] * in[3]; + zz = in[2] * in[2]; + zw = in[2] * in[3]; + + BH_Mat4fIdentity(out); + out[0] = 1.0f - 2.0f * (yy + zz); + out[1] = 2.0f * (xy + zw); + out[2] = 2.0f * (xz - yw); + out[4] = 2.0f * (xy - zw); + out[5] = 1.0f - 2.0f * (xx + zz); + out[6] = 2.0f * (yz + xw); + out[8] = 2.0f * (xz + yw); + out[9] = 2.0f * (yz - xw); + out[10] = 1.0f - 2.0f * (xx + yy); +} + + +void BH_Mat4fIdentity(float *out) +{ + const float ident[16] = + { + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f }; - *result = ident; + memcpy(out, ident, sizeof(ident)); } -void bh_matrix4f_add(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result) +void BH_Mat4fAdd(const float *a, + const float *b, + float *out) { - bh_point4f_add(&a->x, &b->x, &result->x); - bh_point4f_add(&a->y, &b->y, &result->y); - bh_point4f_add(&a->z, &b->z, &result->z); - bh_point4f_add(&a->w, &b->w, &result->w); + BH_Vec4fAdd(&a[0], &b[0], &out[0]); + BH_Vec4fAdd(&a[4], &b[4], &out[4]); + BH_Vec4fAdd(&a[8], &b[8], &out[8]); + BH_Vec4fAdd(&a[12], &b[12], &out[12]); } -void bh_matrix4f_sub(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result) +void BH_Mat4fSub(const float *a, + const float *b, + float *out) { - bh_point4f_sub(&a->x, &b->x, &result->x); - bh_point4f_sub(&a->y, &b->y, &result->y); - bh_point4f_sub(&a->z, &b->z, &result->z); - bh_point4f_sub(&a->w, &b->w, &result->w); + BH_Vec4fSub(&a[0], &b[0], &out[0]); + BH_Vec4fSub(&a[4], &b[4], &out[4]); + BH_Vec4fSub(&a[8], &b[8], &out[8]); + BH_Vec4fSub(&a[12], &b[12], &out[12]); } -void bh_matrix4f_mul(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result) +void BH_Mat4fMul(const float *a, + const float *b, + float *out) { - bh_matrix4f_t tmp; - bh_point4f_t row; + float tmp[16], row[4]; - row.x = row.y = row.z = row.w = b->x.x; bh_point4f_mul(&a->x, &row, &tmp.x); - row.x = row.y = row.z = row.w = b->x.y; bh_point4f_madd(&a->y, &row, &tmp.x, &tmp.x); - row.x = row.y = row.z = row.w = b->x.z; bh_point4f_madd(&a->z, &row, &tmp.x, &tmp.x); - row.x = row.y = row.z = row.w = b->x.w; bh_point4f_madd(&a->w, &row, &tmp.x, &tmp.x); + row[0] = row[1] = row[2] = row[3] = b[0]; BH_Vec4fMul(&a[0], row, &tmp[0]); + row[0] = row[1] = row[2] = row[3] = b[1]; BH_Vec4fMulAdd(&a[4], row, &tmp[0], &tmp[0]); + row[0] = row[1] = row[2] = row[3] = b[2]; BH_Vec4fMulAdd(&a[8], row, &tmp[0], &tmp[0]); + row[0] = row[1] = row[2] = row[3] = b[3]; BH_Vec4fMulAdd(&a[12], row, &tmp[0], &tmp[0]); - row.x = row.y = row.z = row.w = b->y.x; bh_point4f_mul(&a->x, &row, &tmp.y); - row.x = row.y = row.z = row.w = b->y.y; bh_point4f_madd(&a->y, &row, &tmp.y, &tmp.y); - row.x = row.y = row.z = row.w = b->y.z; bh_point4f_madd(&a->z, &row, &tmp.y, &tmp.y); - row.x = row.y = row.z = row.w = b->y.w; bh_point4f_madd(&a->w, &row, &tmp.y, &tmp.y); + row[0] = row[1] = row[2] = row[3] = b[4]; BH_Vec4fMul(&a[0], row, &tmp[4]); + row[0] = row[1] = row[2] = row[3] = b[5]; BH_Vec4fMulAdd(&a[4], row, &tmp[4], &tmp[4]); + row[0] = row[1] = row[2] = row[3] = b[6]; BH_Vec4fMulAdd(&a[8], row, &tmp[4], &tmp[4]); + row[0] = row[1] = row[2] = row[3] = b[7]; BH_Vec4fMulAdd(&a[12], row, &tmp[4], &tmp[4]); - row.x = row.y = row.z = row.w = b->z.x; bh_point4f_mul(&a->x, &row, &tmp.z); - row.x = row.y = row.z = row.w = b->z.y; bh_point4f_madd(&a->y, &row, &tmp.z, &tmp.z); - row.x = row.y = row.z = row.w = b->z.z; bh_point4f_madd(&a->z, &row, &tmp.z, &tmp.z); - row.x = row.y = row.z = row.w = b->z.w; bh_point4f_madd(&a->w, &row, &tmp.z, &tmp.z); + row[0] = row[1] = row[2] = row[3] = b[8]; BH_Vec4fMul(&a[0], row, &tmp[8]); + row[0] = row[1] = row[2] = row[3] = b[9]; BH_Vec4fMulAdd(&a[4], row, &tmp[8], &tmp[8]); + row[0] = row[1] = row[2] = row[3] = b[10]; BH_Vec4fMulAdd(&a[8], row, &tmp[8], &tmp[8]); + row[0] = row[1] = row[2] = row[3] = b[11]; BH_Vec4fMulAdd(&a[12], row, &tmp[8], &tmp[8]); - row.x = row.y = row.z = row.w = b->w.x; bh_point4f_mul(&a->x, &row, &tmp.w); - row.x = row.y = row.z = row.w = b->w.y; bh_point4f_madd(&a->y, &row, &tmp.w, &tmp.w); - row.x = row.y = row.z = row.w = b->w.z; bh_point4f_madd(&a->z, &row, &tmp.w, &tmp.w); - row.x = row.y = row.z = row.w = b->w.w; bh_point4f_madd(&a->w, &row, &tmp.w, &tmp.w); + row[0] = row[1] = row[2] = row[3] = b[12]; BH_Vec4fMul(&a[0], row, &tmp[12]); + row[0] = row[1] = row[2] = row[3] = b[13]; BH_Vec4fMulAdd(&a[4], row, &tmp[12], &tmp[12]); + row[0] = row[1] = row[2] = row[3] = b[14]; BH_Vec4fMulAdd(&a[8], row, &tmp[12], &tmp[12]); + row[0] = row[1] = row[2] = row[3] = b[15]; BH_Vec4fMulAdd(&a[12], row, &tmp[12], &tmp[12]); - *result = tmp; + memcpy(out, tmp, sizeof(tmp)); } -void bh_matrix4f_scale(const bh_matrix4f_t *a, - float b, - bh_matrix4f_t *result) +void BH_Mat4fScale(const float *a, + float b, + float *out) { - bh_point4f_scale(&a->x, b, &result->x); - bh_point4f_scale(&a->y, b, &result->y); - bh_point4f_scale(&a->z, b, &result->z); - bh_point4f_scale(&a->w, b, &result->w); + BH_Vec4fScale(&a[0], b, &out[0]); + BH_Vec4fScale(&a[4], b, &out[4]); + BH_Vec4fScale(&a[8], b, &out[8]); + BH_Vec4fScale(&a[12], b, &out[12]); } -void bh_matrix4f_transpose(const bh_matrix4f_t *in, - bh_matrix4f_t *result) +void BH_Mat4fTranspose(const float *in, + float *out) { - bh_matrix4f_t tmp; - - tmp.x.x = in->x.x; - tmp.x.y = in->y.x; - tmp.x.z = in->z.x; - tmp.x.w = in->w.x; - - tmp.y.x = in->x.y; - tmp.y.y = in->y.y; - tmp.y.z = in->z.y; - tmp.y.w = in->w.y; + float tmp[16]; - tmp.z.x = in->x.z; - tmp.z.y = in->y.z; - tmp.z.z = in->z.z; - tmp.z.w = in->w.z; + tmp[0] = in[0]; tmp[4] = in[1]; tmp[8] = in[2]; tmp[12] = in[3]; + tmp[1] = in[4]; tmp[5] = in[5]; tmp[9] = in[6]; tmp[13] = in[7]; + tmp[2] = in[8]; tmp[6] = in[9]; tmp[10] = in[10]; tmp[14] = in[11]; + tmp[3] = in[12]; tmp[7] = in[13]; tmp[11] = in[14]; tmp[15] = in[15]; - tmp.w.x = in->x.w; - tmp.w.y = in->y.w; - tmp.w.z = in->z.w; - tmp.w.w = in->w.w; - - *result = tmp; + memcpy(out, tmp, sizeof(tmp)); } -float bh_matrix4f_trace(const bh_matrix4f_t *in) +float BH_Mat4fTrace(const float *in) { - return in->x.x + in->y.y + in->z.z + in->w.w; + return in[0] + in[5] + in[10] + in[15]; } -float bh_matrix4f_determinant(const bh_matrix4f_t *in) +float BH_Mat4fDet(const float *in) { float a, b, c, d, e, f, result; - a = in->x.z * in->y.w - in->x.w * in->y.z; - b = in->x.z * in->z.w - in->x.w * in->z.z; - c = in->x.z * in->w.w - in->x.w * in->w.z; - d = in->y.z * in->z.w - in->y.w * in->z.z; - e = in->y.z * in->w.w - in->y.w * in->w.z; - f = in->z.z * in->w.w - in->z.w * in->w.z; + a = in[2] * in[7] - in[3] * in[6]; + b = in[2] * in[11] - in[3] * in[10]; + c = in[2] * in[15] - in[3] * in[14]; + d = in[6] * in[11] - in[7] * in[10]; + e = in[6] * in[15] - in[7] * in[14]; + f = in[10] * in[15] - in[11] * in[14]; result = 0.0f; - result += in->x.x * (in->y.y * f - in->z.y * e + in->w.y * d); - result -= in->y.x * (in->x.y * f - in->z.y * c + in->w.y * b); - result += in->z.x * (in->x.y * e - in->y.y * c + in->w.y * a); - result -= in->w.x * (in->x.y * d - in->y.y * b + in->z.y * a); + result += in[0] * (in[5] * f - in[9] * e + in[13] * d); + result -= in[4] * (in[1] * f - in[9] * c + in[13] * b); + result += in[8] * (in[1] * e - in[5] * c + in[13] * a); + result -= in[12] * (in[1] * d - in[5] * b + in[9] * a); return result; } -int bh_matrix4f_inverse(const bh_matrix4f_t *in, - bh_matrix4f_t *result) +int BH_Mat4fInverse(const float *in, + float *out) { float a, b, c, d, e, f, det; - bh_matrix4f_t tmp; + float tmp[16]; - a = in->x.z * in->y.w - in->x.w * in->y.z; - b = in->x.z * in->z.w - in->x.w * in->z.z; - c = in->x.z * in->w.w - in->x.w * in->w.z; - d = in->y.z * in->z.w - in->y.w * in->z.z; - e = in->y.z * in->w.w - in->y.w * in->w.z; - f = in->z.z * in->w.w - in->z.w * in->w.z; + a = in[2] * in[7] - in[3] * in[6]; + b = in[2] * in[11] - in[3] * in[10]; + c = in[2] * in[15] - in[3] * in[14]; + d = in[6] * in[11] - in[7] * in[10]; + e = in[6] * in[15] - in[7] * in[14]; + f = in[10] * in[15] - in[11] * in[14]; - tmp.x.x = (in->y.y * f - in->z.y * e + in->w.y * d); - tmp.x.y = -(in->x.y * f - in->z.y * c + in->w.y * b); - tmp.x.z = (in->x.y * e - in->y.y * c + in->w.y * a); - tmp.x.w = -(in->x.y * d - in->y.y * b + in->z.y * a); + tmp[0] = (in[5] * f - in[9] * e + in[13] * d); + tmp[1] = -(in[1] * f - in[9] * c + in[13] * b); + tmp[2] = (in[1] * e - in[5] * c + in[13] * a); + tmp[3] = -(in[1] * d - in[5] * b + in[9] * a); det = 0.0f; - det += in->x.x * tmp.x.x; - det += in->y.x * tmp.x.y; - det += in->z.x * tmp.x.z; - det += in->w.x * tmp.x.w; + det += in[0] * tmp[0]; + det += in[4] * tmp[1]; + det += in[8] * tmp[2]; + det += in[12] * tmp[3]; if (det == 0.0f) return BH_ERROR; - tmp.y.x = -(in->y.x * f - in->z.x * e + in->w.x * d); - tmp.y.y = (in->x.x * f - in->z.x * c + in->w.x * b); - tmp.y.z = -(in->x.x * e - in->y.x * c + in->w.x * a); - tmp.y.w = (in->x.x * d - in->y.x * b + in->z.x * a); - - a = in->x.y * in->y.w - in->x.w * in->y.y; - b = in->x.y * in->z.w - in->x.w * in->z.y; - c = in->x.y * in->w.w - in->x.w * in->w.y; - d = in->y.y * in->z.w - in->y.w * in->z.y; - e = in->y.y * in->w.w - in->y.w * in->w.y; - f = in->z.y * in->w.w - in->z.w * in->w.y; - - tmp.z.x = (in->y.x * f - in->z.x * e + in->w.x * d); - tmp.z.y = -(in->x.x * f - in->z.x * c + in->w.x * b); - tmp.z.z = (in->x.x * e - in->y.x * c + in->w.x * a); - tmp.z.w = -(in->x.x * d - in->y.x * b + in->z.x * a); - - a = in->x.y * in->y.z - in->x.z * in->y.y; - b = in->x.y * in->z.z - in->x.z * in->z.y; - c = in->x.y * in->w.z - in->x.z * in->w.y; - d = in->y.y * in->z.z - in->y.z * in->z.y; - e = in->y.y * in->w.z - in->y.z * in->w.y; - f = in->z.y * in->w.z - in->z.z * in->w.y; - - tmp.w.x = -(in->y.x * f - in->z.x * e + in->w.x * d); - tmp.w.y = (in->x.x * f - in->z.x * c + in->w.x * b); - tmp.w.z = -(in->x.x * e - in->y.x * c + in->w.x * a); - tmp.w.w = (in->x.x * d - in->y.x * b + in->z.x * a); - - bh_matrix4f_scale(&tmp, 1.0f / det, result); + tmp[4] = -(in[4] * f - in[8] * e + in[12] * d); + tmp[5] = (in[0] * f - in[8] * c + in[12] * b); + tmp[6] = -(in[0] * e - in[4] * c + in[12] * a); + tmp[7] = (in[0] * d - in[4] * b + in[8] * a); + + a = in[1] * in[7] - in[3] * in[5]; + b = in[1] * in[11] - in[3] * in[9]; + c = in[1] * in[15] - in[3] * in[13]; + d = in[5] * in[11] - in[7] * in[9]; + e = in[5] * in[15] - in[7] * in[13]; + f = in[9] * in[15] - in[11] * in[13]; + + tmp[8] = (in[4] * f - in[8] * e + in[12] * d); + tmp[9] = -(in[0] * f - in[8] * c + in[12] * b); + tmp[10] = (in[0] * e - in[4] * c + in[12] * a); + tmp[11] = -(in[0] * d - in[4] * b + in[8] * a); + + a = in[1] * in[6] - in[2] * in[5]; + b = in[1] * in[10] - in[2] * in[9]; + c = in[1] * in[14] - in[2] * in[13]; + d = in[5] * in[10] - in[6] * in[9]; + e = in[5] * in[14] - in[6] * in[13]; + f = in[9] * in[14] - in[10] * in[13]; + + tmp[12] = -(in[4] * f - in[8] * e + in[12] * d); + tmp[13] = (in[0] * f - in[8] * c + in[12] * b); + tmp[14] = -(in[0] * e - in[4] * c + in[12] * a); + tmp[15] = (in[0] * d - in[4] * b + in[8] * a); + + BH_Mat4fScale(tmp, 1.0f / det, out); return BH_OK; } -void bh_matrix4f_scaling(float x, - float y, - float z, - bh_matrix4f_t *result) +void BH_Mat4fFromScale(float x, + float y, + float z, + float *out) { - bh_matrix4f_identity(result); - result->x.x = x; - result->y.y = y; - result->z.z = z; + BH_Mat4fIdentity(out); + out[0] = x; + out[5] = y; + out[10] = z; } -void bh_matrix4f_translation(float x, +void BH_Mat4fFromTranslation(float x, float y, float z, - bh_matrix4f_t *result) + float *out) { - bh_matrix4f_identity(result); - result->w.x = x; - result->w.y = y; - result->w.z = z; + BH_Mat4fIdentity(out); + out[12] = x; + out[13] = y; + out[14] = z; } -void bh_matrix4f_rotation_x(float angle, - bh_matrix4f_t *result) +void BH_Mat4fFromRotationX(float angle, + float *out) { float c, s; - c = cosf(angle); - s = sinf(angle); + c = cos(angle); + s = sin(angle); - bh_matrix4f_identity(result); - result->y.y = c; - result->z.z = c; - result->z.y = -s; - result->y.z = s; + BH_Mat4fIdentity(out); + out[5] = c; + out[6] = s; + out[9] = -s; + out[10] = c; } -void bh_matrix4f_rotation_y(float angle, - bh_matrix4f_t *result) +void BH_Mat4fFromRotationY(float angle, + float *out) { float c, s; - c = cosf(angle); - s = sinf(angle); + c = cos(angle); + s = sin(angle); - bh_matrix4f_identity(result); - result->x.x = c; - result->z.z = c; - result->z.x = s; - result->x.z = -s; + BH_Mat4fIdentity(out); + out[0] = c; + out[2] = -s; + out[8] = s; + out[10] = c; } -void bh_matrix4f_rotation_z(float angle, - bh_matrix4f_t *result) +void BH_Mat4fFromRotationZ(float angle, + float *out) { float c, s; - c = cosf(angle); - s = sinf(angle); + c = cos(angle); + s = sin(angle); - bh_matrix4f_identity(result); - result->x.x = c; - result->y.y = c; - result->y.x = -s; - result->x.y = s; + BH_Mat4fIdentity(out); + out[0] = c; + out[1] = s; + out[4] = -s; + out[5] = c; } -void bh_matrix4f_rotation(const bh_point3f_t *axis, - float angle, - bh_matrix4f_t *result) +void BH_Mat4fFromAxis(const float *axis, + float angle, + float *out) { float x, y, z, length; float c, s, moc, xx, xy, xz, yy, yz, zz; - length = bh_point3f_length(axis); + length = BH_Vec3fLength(axis); + BH_Mat4fIdentity(out); if (length == 0.0f) - { - bh_matrix4f_identity(result); return; - } - x = axis->x / length; - y = axis->y / length; - z = axis->z / length; + x = axis[0] / length; + y = axis[1] / length; + z = axis[2] / length; - /* Handle simple axis aligned rotations */ - if (x == 0.0f) - { - if (y == 0.0f) - { - if (z != 0.0f) - { - if (z < 0.0f) - bh_matrix4f_rotation_z(-angle, result); - else - bh_matrix4f_rotation_z(angle, result); - - return; - } - } - else if (z == 0.0f) - { - if (y < 0.0f) - bh_matrix4f_rotation_y(-angle, result); - else - bh_matrix4f_rotation_y(angle, result); - - return; - } - } - else if (y == 0.0f && z == 0.0f) - { - if (x < 0.0f) - bh_matrix4f_rotation_x(-angle, result); - else - bh_matrix4f_rotation_x(angle, result); - - return; - } - - /* Rotate around arbitrary axis */ - bh_matrix4f_identity(result); - - c = cosf(angle); - s = sinf(angle); + c = cos(angle); + s = sin(angle); moc = 1.0f - c; xx = x * x; @@ -1266,382 +1081,369 @@ void bh_matrix4f_rotation(const bh_point3f_t *axis, yz = y * z; zz = z * z; - result->x.x = c + xx * moc; - result->y.x = xy * moc - z * s; - result->z.x = xz * moc + y * s; + out[0] = c + xx * moc; + out[1] = xy * moc + z * s; + out[2] = xz * moc - y * s; - result->x.y = xy * moc + z * s; - result->y.y = c + yy * moc; - result->z.y = yz * moc - x * s; + out[4] = xy * moc - z * s; + out[5] = c + yy * moc; + out[6] = yz * moc + x * s; - result->x.z = xz * moc - y * s; - result->y.z = yz * moc + x * s; - result->z.z = c + zz * moc; + out[8] = xz * moc + y * s; + out[9] = yz * moc - x * s; + out[10] = c + zz * moc; } -void bh_matrix4f_rotation_euler(float roll, - float pitch, - float yaw, - bh_matrix4f_t *result) +void BH_Mat4fFromEuler(float roll, + float pitch, + float yaw, + float *out) { float rs, rc, ys, yc, ps, pc; - rs = sinf(roll); - rc = cosf(roll); - ps = sinf(pitch); - pc = cosf(pitch); - ys = sinf(yaw); - yc = cosf(yaw); + rs = sin(roll); + rc = cos(roll); + ps = sin(pitch); + pc = cos(pitch); + ys = sin(yaw); + yc = cos(yaw); - bh_matrix4f_identity(result); - result->x.x = pc * yc; - result->x.y = pc * ys; - result->x.z = -ps; - result->y.x = ps * rs * yc - rc * ys; - result->y.y = ps * rs * ys + rc * yc; - result->y.z = pc * rs; - result->z.x = rs * ys + ps * rc * yc; - result->z.y = ps * rc * ys - rs * yc; - result->z.z = pc * rc; + BH_Mat4fIdentity(out); + out[0] = pc * yc; + out[1] = pc * ys; + out[2] = -ps; + out[4] = ps * rs * yc - rc * ys; + out[5] = ps * rs * ys + rc * yc; + out[6] = pc * rs; + out[8] = rs * ys + ps * rc * yc; + out[9] = ps * rc * ys - rs * yc; + out[10] = pc * rc; } -void bh_matrix4f_rotation_quat(bh_quat_t *rotation, - bh_matrix4f_t *result) +void BH_Mat4fFromQuat4f(const float *in, + float *out) { - bh_quat_matrix(rotation, result); + BH_Quat4fToMat4f(in, out); } -void bh_matrix4f_ortho(float x_min, - float x_max, - float y_min, - float y_max, - float z_min, - float z_max, - bh_matrix4f_t *result) +void BH_Mat4fFromOrtho(float xMin, + float xMax, + float yMin, + float yMax, + float zMin, + float zMax, + float *out) { float dx, dy, dz; - dx = x_max - x_min; - dy = y_max - y_min; - dz = z_max - z_min; + dx = xMax - xMin; + dy = yMax - yMin; + dz = zMax - zMin; - bh_matrix4f_identity(result); + BH_Mat4fIdentity(out); - result->x.x = 2.0f / dx; - result->y.y = 2.0f / dy; - result->z.z = -2.0f / dz; - result->w.x = -(x_max + x_min) / dx; - result->w.y = -(y_max + y_min) / dy; - result->w.z = -(z_max + z_min) / dz; + out[0] = 2.0f / dx; + out[5] = 2.0f / dy; + out[10] = -2.0f / dz; + out[12] = -(xMax + xMin) / dx; + out[13] = -(yMax + yMin) / dy; + out[14] = -(zMax + zMin) / dz; } -void bh_matrix4f_perspective(float fov, - float aspect, - float z_min, - float z_max, - bh_matrix4f_t *result) +void BH_Mat4fFromFrustum(float fov, + float aspect, + float zMin, + float zMax, + float *out) { float t, dz; - dz = z_max - z_min; + dz = zMax - zMin; t = tanf(fov / 2.0f); - bh_matrix4f_identity(result); + BH_Mat4fIdentity(out); - result->x.x = 1.0f / (aspect * t); - result->y.y = 1.0f / t; - result->z.z = -(z_max + z_min) / dz; - result->z.w = -1.0f; - result->w.z = -(2.0f * z_max * z_min) / dz; - result->w.w = 0.0f; + out[0] = 1.0f / (aspect * t); + out[5] = 1.0f / t; + out[10] = -(zMax + zMin) / dz; + out[11] = -1.0f; + out[14] = -(2.0f * zMax * zMin) / dz; + out[15] = 0.0f; } -void bh_matrix4f_lookat(const bh_point3f_t *camera, - const bh_point3f_t *at, - const bh_point3f_t *up, - bh_matrix4f_t *result) +void BH_Mat4fFromLookAt(const float *pos, + const float *at, + const float *up, + float *out) { - bh_point3f_t cdir, cright, cup; + float cdir[3], cright[3], cup[3]; - bh_point3f_sub(camera, at, &cdir); bh_point3f_normal(&cdir, &cdir); - bh_point3f_cross(up, &cdir, &cright); bh_point3f_normal(&cright, &cright); - bh_point3f_cross(&cdir, &cright, &cup); + BH_Vec3fSub(pos, at, cdir); + BH_Vec3fNormal(cdir, cdir); + BH_Vec3fCross(up, cdir, cright); + BH_Vec3fNormal(cright, cright); + BH_Vec3fCross(cdir, cright, cup); - result->x.x = cright.x; - result->x.y = cup.x; - result->x.z = cdir.x; - result->x.w = 0.0f; + out[0] = cright[0]; + out[1] = cup[0]; + out[2] = cdir[0]; + out[3] = 0.0f; - result->y.x = cright.y; - result->y.y = cup.y; - result->y.z = cdir.y; - result->y.w = 0.0f; + out[4] = cright[1]; + out[5] = cup[1]; + out[6] = cdir[1]; + out[7] = 0.0f; - result->z.x = cright.z; - result->z.y = cup.z; - result->z.z = cdir.z; - result->z.w = 0.0f; + out[8] = cright[2]; + out[9] = cup[2]; + out[10] = cdir[2]; + out[11] = 0.0f; - result->w.x = -bh_point3f_dot(&cright, camera); - result->w.y = -bh_point3f_dot(&cup, camera); - result->w.z = -bh_point3f_dot(&cdir, camera); - result->w.w = 1.0f; + out[12] = -BH_Vec3fDot(cright, pos); + out[13] = -BH_Vec3fDot(cup, pos); + out[14] = -BH_Vec3fDot(cdir, pos); + out[15] = 1.0f; } -void bh_matrix4f_transform_point3f(const bh_matrix4f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void BH_Mat4fApplyVec4f(const float *a, + const float *b, + float *out) { - bh_point4f_t tmp, row; + float tmp[4], row[4]; - row.x = row.y = row.z = row.w = b->x; bh_point4f_mul(&a->x, &row, &tmp); - row.x = row.y = row.z = row.w = b->y; bh_point4f_madd(&a->y, &row, &tmp, &tmp); - row.x = row.y = row.z = row.w = b->z; bh_point4f_madd(&a->z, &row, &tmp, &tmp); - row.x = row.y = row.z = row.w = 1.0f; bh_point4f_madd(&a->w, &row, &tmp, &tmp); + row[0] = row[1] = row[2] = row[3] = b[0]; BH_Vec4fMul(&a[0], row, tmp); + row[0] = row[1] = row[2] = row[3] = b[1]; BH_Vec4fMulAdd(&a[4], row, tmp, tmp); + row[0] = row[1] = row[2] = row[3] = b[2]; BH_Vec4fMulAdd(&a[8], row, tmp, tmp); + row[0] = row[1] = row[2] = row[3] = b[3]; BH_Vec4fMulAdd(&a[12], row, tmp, tmp); - result->x = tmp.x; - result->y = tmp.y; - result->z = tmp.z; + memcpy(out, tmp, sizeof(tmp)); } -void bh_matrix4f_transform_point4f(const bh_matrix4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void BH_Mat4fApplyVec3f(const float *a, + const float *b, + float *out) { - bh_point4f_t tmp, row; + float tmp[4], row[4]; - row.x = row.y = row.z = row.w = b->x; bh_point4f_mul(&a->x, &row, &tmp); - row.x = row.y = row.z = row.w = b->y; bh_point4f_madd(&a->y, &row, &tmp, &tmp); - row.x = row.y = row.z = row.w = b->z; bh_point4f_madd(&a->z, &row, &tmp, &tmp); - row.x = row.y = row.z = row.w = b->w; bh_point4f_madd(&a->w, &row, &tmp, &tmp); + row[0] = row[1] = row[2] = row[3] = b[0]; BH_Vec4fMul(&a[0], row, tmp); + row[0] = row[1] = row[2] = row[3] = b[1]; BH_Vec4fMulAdd(&a[4], row, tmp, tmp); + row[0] = row[1] = row[2] = row[3] = b[2]; BH_Vec4fMulAdd(&a[8], row, tmp, tmp); + row[0] = row[1] = row[2] = row[3] = 1.0f; BH_Vec4fMulAdd(&a[12], row, tmp, tmp); - *result = tmp; + memcpy(out, tmp, sizeof(float) * 3); } -void bh_matrix3f_identity(bh_matrix3f_t *result) +void BH_Mat3fIdentity(float *out) { - static const bh_matrix3f_t ident = { - {1.0f, 0.0f, 0.0f}, - {0.0f, 1.0f, 0.0f}, - {0.0f, 0.0f, 1.0f} + static const float ident[9] = + { + 1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f }; - *result = ident; + memcpy(out, ident, sizeof(ident)); } -void bh_matrix3f_add(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result) +void BH_Mat3fAdd(const float *a, + const float *b, + float *out) { - bh_point3f_add(&a->x, &b->x, &result->x); - bh_point3f_add(&a->y, &b->y, &result->y); - bh_point3f_add(&a->z, &b->z, &result->z); + BH_Vec3fAdd(&a[0], &b[0], &out[0]); + BH_Vec3fAdd(&a[3], &b[3], &out[3]); + BH_Vec3fAdd(&a[6], &b[6], &out[6]); } -void bh_matrix3f_sub(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result) +void BH_Mat3fSub(const float *a, + const float *b, + float *out) { - bh_point3f_sub(&a->x, &b->x, &result->x); - bh_point3f_sub(&a->y, &b->y, &result->y); - bh_point3f_sub(&a->z, &b->z, &result->z); + BH_Vec3fSub(&a[0], &b[0], &out[0]); + BH_Vec3fSub(&a[3], &b[3], &out[3]); + BH_Vec3fSub(&a[6], &b[6], &out[6]); } -void bh_matrix3f_mul(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result) +void BH_Mat3fMul(const float *a, + const float *b, + float *out) { - bh_matrix3f_t tmp; - bh_point3f_t row; + float tmp[9], row[3]; - row.x = row.y = row.z = b->x.x; bh_point3f_mul(&a->x, &row, &tmp.x); - row.x = row.y = row.z = b->x.y; bh_point3f_madd(&a->y, &row, &tmp.x, &tmp.x); - row.x = row.y = row.z = b->x.z; bh_point3f_madd(&a->z, &row, &tmp.x, &tmp.x); + row[0] = row[1] = row[2] = b[0]; BH_Vec3fMul(&a[0], row, &tmp[0]); + row[0] = row[1] = row[2] = b[1]; BH_Vec3fMulAdd(&a[3], row, &tmp[0], &tmp[0]); + row[0] = row[1] = row[2] = b[2]; BH_Vec3fMulAdd(&a[6], row, &tmp[0], &tmp[0]); - row.x = row.y = row.z = b->y.x; bh_point3f_mul(&a->x, &row, &tmp.y); - row.x = row.y = row.z = b->y.y; bh_point3f_madd(&a->y, &row, &tmp.y, &tmp.y); - row.x = row.y = row.z = b->y.z; bh_point3f_madd(&a->z, &row, &tmp.y, &tmp.y); + row[0] = row[1] = row[2] = b[3]; BH_Vec3fMul(&a[0], row, &tmp[3]); + row[0] = row[1] = row[2] = b[4]; BH_Vec3fMulAdd(&a[3], row, &tmp[3], &tmp[3]); + row[0] = row[1] = row[2] = b[5]; BH_Vec3fMulAdd(&a[6], row, &tmp[3], &tmp[3]); - row.x = row.y = row.z = b->z.x; bh_point3f_mul(&a->x, &row, &tmp.z); - row.x = row.y = row.z = b->z.y; bh_point3f_madd(&a->y, &row, &tmp.z, &tmp.z); - row.x = row.y = row.z = b->z.z; bh_point3f_madd(&a->z, &row, &tmp.z, &tmp.z); + row[0] = row[1] = row[2] = b[6]; BH_Vec3fMul(&a[0], row, &tmp[6]); + row[0] = row[1] = row[2] = b[7]; BH_Vec3fMulAdd(&a[3], row, &tmp[6], &tmp[6]); + row[0] = row[1] = row[2] = b[8]; BH_Vec3fMulAdd(&a[6], row, &tmp[6], &tmp[6]); - *result = tmp; + memcpy(out, tmp, sizeof(tmp)); } -void bh_matrix3f_scale(const bh_matrix3f_t *a, - float b, - bh_matrix3f_t *result) +void BH_Mat3fScale(const float *a, + float b, + float *out) { - bh_point3f_scale(&a->x, b, &result->x); - bh_point3f_scale(&a->y, b, &result->y); - bh_point3f_scale(&a->z, b, &result->z); + BH_Vec3fScale(&a[0], b, &out[0]); + BH_Vec3fScale(&a[3], b, &out[3]); + BH_Vec3fScale(&a[6], b, &out[6]); } -void bh_matrix3f_transpose(const bh_matrix3f_t *in, - bh_matrix3f_t *result) +void BH_Mat3fTranspose(const float *in, + float *out) { - bh_matrix3f_t tmp; - - tmp.x.x = in->x.x; - tmp.x.y = in->y.x; - tmp.x.z = in->z.x; + float tmp[9]; - tmp.y.x = in->x.y; - tmp.y.y = in->y.y; - tmp.y.z = in->z.y; + tmp[0] = in[0]; tmp[3] = in[1]; tmp[6] = in[2]; + tmp[1] = in[3]; tmp[4] = in[4]; tmp[7] = in[5]; + tmp[2] = in[6]; tmp[5] = in[7]; tmp[8] = in[8]; - tmp.z.x = in->x.z; - tmp.z.y = in->y.z; - tmp.z.z = in->z.z; - - *result = tmp; + memcpy(out, tmp, sizeof(tmp)); } -float bh_matrix3f_trace(const bh_matrix3f_t *in) +float BH_Mat3fTrace(const float *in) { - return in->x.x + in->y.y + in->z.z; + return in[0] + in[4] + in[8]; } -float bh_matrix3f_determinant(const bh_matrix3f_t *in) +float BH_Mat3fDet(const float *in) { float a, b, c, result; - a = in->y.y * in->z.z - in->z.y * in->y.z; - b = in->x.y * in->z.z - in->z.y * in->x.z; - c = in->x.y * in->y.z - in->y.y * in->x.z; + a = in[4] * in[8] - in[7] * in[5]; + b = in[1] * in[8] - in[7] * in[2]; + c = in[1] * in[5] - in[4] * in[2]; result = 0.0f; - result += in->x.x * a; - result -= in->y.x * b; - result += in->z.x * c; + result += in[0] * a; + result -= in[3] * b; + result += in[6] * c; return result; } -int bh_matrix3f_inverse(const bh_matrix3f_t *in, - bh_matrix3f_t *result) +int BH_Mat3fInverse(const float *in, + float *out) { float a, b, c, det; - bh_matrix3f_t tmp; + float tmp[16]; - a = in->y.y * in->z.z - in->z.y * in->y.z; - b = in->x.y * in->z.z - in->z.y * in->x.z; - c = in->x.y * in->y.z - in->y.y * in->x.z; + a = in[4] * in[8] - in[7] * in[5]; + b = in[1] * in[8] - in[7] * in[2]; + c = in[1] * in[5] - in[4] * in[2]; - tmp.x.x = a; - tmp.x.y = -b; - tmp.x.z = c; + tmp[0] = a; + tmp[1] = -b; + tmp[2] = c; det = 0.0f; - det += in->x.x * tmp.x.x; - det += in->y.x * tmp.x.y; - det += in->z.x * tmp.x.z; + det += in[0] * tmp[0]; + det += in[3] * tmp[1]; + det += in[6] * tmp[2]; if (det == 0.0f) return BH_ERROR; - a = in->y.x * in->z.z - in->z.x * in->y.z; - b = in->x.x * in->z.z - in->z.x * in->x.z; - c = in->x.x * in->y.z - in->y.x * in->x.z; + a = in[3] * in[8] - in[6] * in[5]; + b = in[0] * in[8] - in[6] * in[2]; + c = in[0] * in[5] - in[3] * in[2]; - tmp.y.x = -a; - tmp.y.y = b; - tmp.y.z = -c; + tmp[3] = -a; + tmp[4] = b; + tmp[5] = -c; - a = in->y.x * in->z.y - in->z.x * in->y.y; - b = in->x.x * in->z.y - in->z.x * in->x.y; - c = in->x.x * in->y.y - in->y.x * in->x.y; + a = in[3] * in[7] - in[6] * in[4]; + b = in[0] * in[7] - in[6] * in[1]; + c = in[0] * in[4] - in[3] * in[1]; - tmp.z.x = a; - tmp.z.y = -b; - tmp.z.z = c; + tmp[6] = a; + tmp[7] = -b; + tmp[8] = c; - bh_matrix3f_scale(&tmp, 1.0f / det, result); + BH_Mat3fScale(tmp, 1.0f / det, out); return BH_OK; } -void bh_matrix3f_scaling(float x, - float y, - bh_matrix3f_t *result) +void BH_Mat3fFromScale(float x, + float y, + float *out) { - bh_matrix3f_identity(result); - - result->x.x = x; - result->y.y = y; + BH_Mat3fIdentity(out); + out[0] = x; + out[4] = y; } -void bh_matrix3f_translation(float x, +void BH_Mat3fFromTranslation(float x, float y, - bh_matrix3f_t *result) + float *out) { - bh_matrix3f_identity(result); - - result->z.x = x; - result->z.y = y; + BH_Mat3fIdentity(out); + out[6] = x; + out[7] = y; } -void bh_matrix3f_rotation(float angle, - bh_matrix3f_t *result) +void BH_Mat3fFromRotation(float angle, + float *out) { float c, s; - c = cosf(angle); - s = sinf(angle); + c = cos(angle); + s = sin(angle); - bh_matrix3f_identity(result); - result->x.x = c; - result->y.y = c; - result->y.x = -s; - result->x.y = s; + BH_Mat3fIdentity(out); + out[0] = c; + out[1] = s; + out[3] = -s; + out[4] = c; } -void bh_matrix3f_transform_point2f(const bh_matrix3f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void BH_Mat3fApplyVec3f(float *a, + float *b, + float *out) { - bh_point3f_t tmp, row; + float tmp[3], row[3]; - row.x = row.y = row.z = b->x; bh_point3f_mul(&a->x, &row, &tmp); - row.x = row.y = row.z = b->y; bh_point3f_madd(&a->y, &row, &tmp, &tmp); - row.x = row.y = row.z = 1.0f; bh_point3f_madd(&a->z, &row, &tmp, &tmp); + row[0] = row[1] = row[2] = b[0]; BH_Vec3fMul(&a[0], row, tmp); + row[0] = row[1] = row[2] = b[1]; BH_Vec3fMulAdd(&a[3], row, tmp, tmp); + row[0] = row[1] = row[2] = b[2]; BH_Vec3fMulAdd(&a[6], row, tmp, tmp); - result->x = tmp.x; - result->y = tmp.y; + memcpy(out, tmp, sizeof(tmp)); } -void bh_matrix3f_transform_point3f(const bh_matrix3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void BH_Mat3fApplyVec2f(float *a, + float *b, + float *out) { - bh_point3f_t tmp, row; + float tmp[3], row[3]; - row.x = row.y = row.z = b->x; bh_point3f_mul(&a->x, &row, &tmp); - row.x = row.y = row.z = b->y; bh_point3f_madd(&a->y, &row, &tmp, &tmp); - row.x = row.y = row.z = b->z; bh_point3f_madd(&a->z, &row, &tmp, &tmp); + row[0] = row[1] = row[2] = b[0]; BH_Vec3fMul(&a[0], row, tmp); + row[0] = row[1] = row[2] = b[1]; BH_Vec3fMulAdd(&a[3], row, tmp, tmp); + row[0] = row[1] = row[2] = 1.0f; BH_Vec3fMulAdd(&a[6], row, tmp, tmp); - result->x = tmp.x; - result->y = tmp.y; - result->z = tmp.z; + memcpy(out, tmp, sizeof(float) * 2); } diff --git a/src/posix/file.c b/src/posix/file.c index 314235c..87a5a2b 100644 --- a/src/posix/file.c +++ b/src/posix/file.c @@ -7,74 +7,74 @@ #include -typedef struct bh_file_s +typedef struct BH_File { char *path; int mode; int flags; int handle; -} bh_file_t; +} BH_File; -static int file_info(bh_file_t *file, - size_t *size, - const char **ident); +static int BH_FileInfo(BH_File *file, + size_t *size, + const char **name); -static int file_init(bh_file_t *file, - const char *path); +static int BH_FileInit(BH_File *file, + 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, - int *mode); +static int BH_FileOpen(BH_File *file, + 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, - char *data, - size_t *size); +static int BH_FileRead(BH_File *file, + char *data, + size_t *size); -static int file_write(bh_file_t *file, - const char *data, - size_t *size); +static int BH_FileWrite(BH_File *file, + const char *data, + size_t *size); -static int file_peek(bh_file_t *file, - char *data, - size_t *size); +static int BH_FilePeek(BH_File* file, + char *data, + 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, - int64_t *pos, - int *dir); +static int BH_FileSeek(BH_File *file, + int64_t *pos, + int *dir); -static int file_tell(bh_file_t *file, - int64_t *pos); +static int BH_FileTell(BH_File *file, + int64_t *pos); -static int file_size(bh_file_t *file, - int64_t *size); +static int BH_FileSize(BH_File *file, + 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, - size_t *size, - const char **name) +static int BH_FileInfo(BH_File *file, + size_t *size, + const char **name) { static const char classname[] = BH_FILE_CLASSNAME; @@ -86,8 +86,8 @@ static int file_info(bh_file_t *file, } -static int file_init(bh_file_t *file, - const char *path) +static int BH_FileInit(BH_File *file, + const char *path) { /* Check if path is valid */ if (!path) @@ -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 */ if (file->handle != -1) - file_close(file); + BH_FileClose(file); /* Free path string */ 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; @@ -152,8 +152,8 @@ static int file_openflags(int mode) } -static int file_open(bh_file_t *file, - int *mode) +static int BH_FileOpen(BH_File *file, + int *mode) { static const mode_t open_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); int flags; @@ -163,7 +163,7 @@ static int file_open(bh_file_t *file, return BH_ERROR; /* Determine file open flags */ - flags = file_openflags(*mode); + flags = BH_FileOpenFlags(*mode); if (flags == -1) 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->handle == -1) @@ -190,9 +190,9 @@ static int file_close(bh_file_t *file) } -static int file_read(bh_file_t *file, - char *data, - size_t *size) +static int BH_FileRead(BH_File *file, + char *data, + size_t *size) { ssize_t readed; @@ -220,9 +220,9 @@ error: } -static int file_write(bh_file_t *file, - const char *data, - size_t *size) +static int BH_FileWrite(BH_File *file, + const char *data, + size_t *size) { ssize_t written; @@ -250,9 +250,9 @@ error: } -static int file_peek(bh_file_t *file, - char *data, - size_t *size) +static int BH_FilePeek(BH_File *file, + char *data, + size_t *size) { int64_t position; int direction; @@ -262,13 +262,13 @@ static int file_peek(bh_file_t *file, goto error; /* Read data from the file */ - if (file_read(file, data, size)) + if (BH_FileRead(file, data, size)) goto error; /* Backtrack by the read amount */ position = -((int64_t)*size); direction = BH_IO_SEEK_CUR; - if (file_seek(file, &position, &direction)) + if (BH_FileSeek(file, &position, &direction)) goto error; 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 */ if (file->handle == -1) @@ -294,9 +294,9 @@ static int file_flush(bh_file_t *file) } -static int file_seek(bh_file_t *file, - int64_t *pos, - int *dir) +static int BH_FileSeek(BH_File *file, + int64_t *pos, + int *dir) { /* Check if file is open */ if (file->handle == -1) @@ -314,8 +314,8 @@ error: } -static int file_tell(bh_file_t *file, - int64_t *pos) +static int BH_FileTell(BH_File *file, + int64_t *pos) { /* Check if file is open */ if (file->handle == -1) @@ -333,8 +333,8 @@ error: } -static int file_size(bh_file_t *file, - int64_t *size) +static int BH_FileSize(BH_File *file, + int64_t *size) { 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 != -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 */ 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, - int type, - void *arg1, - void *arg2) +static int BH_FileCallback(BH_File *file, + int type, + void *arg1, + void *arg2) { switch (type) { - case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2); - case BH_IO_INIT_CB: return file_init(file, (const char *)arg1); - case BH_IO_DESTROY_CB: return file_destroy(file); - case BH_IO_OPEN_CB: return file_open(file, (int *)arg1); - case BH_IO_CLOSE_CB: return file_close(file); - case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2); - case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2); - case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2); - case BH_IO_FLUSH_CB: return file_flush(file); - case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2); - case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1); - case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1); - case BH_IO_FLAGS_CB: return file_flags(file); - case BH_IO_CLEAR_CB: return file_clear(file); + case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2); + case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1); + case BH_IO_DESTROY_CB: return BH_FileDestroy(file); + case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1); + case BH_IO_CLOSE_CB: return BH_FileClose(file); + case BH_IO_READ_CB: return BH_FileRead(file, (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 BH_FilePeek(file, (char *)arg1, (size_t *)arg2); + case BH_IO_FLUSH_CB: return BH_FileFlush(file); + case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2); + case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1); + case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1); + case BH_IO_FLAGS_CB: return BH_FileFlags(file); + case BH_IO_CLEAR_CB: return BH_FileClear(file); 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); } diff --git a/src/queue.c b/src/queue.c index 3ce8a57..656d27d 100755 --- a/src/queue.c +++ b/src/queue.c @@ -3,7 +3,7 @@ #include -struct bh_queue_s +struct BH_Queue { void **data; 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)); } -static void bh_queue_destroy(bh_queue_t *queue) +static void BH_QueueDestroy(BH_Queue *queue) { if (queue->capacity) free(queue->data); } -static void queue_copy(bh_queue_t *dest, - bh_queue_t *src) +static void BH_QueueCopy(BH_Queue *dest, + BH_Queue *src) { void *iter; /* Iterate over old queue and insert data into new queue */ - iter = bh_queue_iter_next(src, NULL); + iter = BH_QueueIterNext(src, NULL); while (iter) { - bh_queue_insert(dest, bh_queue_iter_value(iter)); - iter = bh_queue_iter_next(src, iter); + BH_QueueInsert(dest, BH_QueueIterValue(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)); if (result) - bh_queue_init(result); + BH_QueueInit(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); } -void bh_queue_clear(bh_queue_t *queue) +void BH_QueueClear(BH_Queue *queue) { queue->head = 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) { - bh_queue_t other; + BH_Queue other; /* New capacity should be great or equal to current size */ if (size < queue->size) @@ -86,7 +86,7 @@ int bh_queue_reserve(bh_queue_t *queue, return BH_OK; /* Prepare new empty queue */ - bh_queue_init(&other); + BH_QueueInit(&other); if (size) { /* Allocate new capacity for the queue */ @@ -96,7 +96,7 @@ int bh_queue_reserve(bh_queue_t *queue, return BH_OOM; /* 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 */ @@ -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) { /* 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 */ 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; } @@ -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 */ 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 */ 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; } -size_t bh_queue_size(bh_queue_t *queue) +size_t BH_QueueSize(BH_Queue *queue) { return queue->size; } -size_t bh_queue_capacity(bh_queue_t *queue) +size_t BH_QueueCapacity(BH_Queue *queue) { return queue->capacity; } -void *bh_queue_iter_next(bh_queue_t *queue, +void *BH_QueueIterNext(BH_Queue *queue, 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; } diff --git a/src/win32/file.c b/src/win32/file.c index 8effc0d..bed16ab 100644 --- a/src/win32/file.c +++ b/src/win32/file.c @@ -4,74 +4,74 @@ #include -typedef struct bh_file_s +typedef struct BH_File { char *path; int mode; int flags; HANDLE handle; -} bh_file_t; +} BH_File; -static int file_info(bh_file_t *file, - size_t *size, - const char **name); +static int BH_FileInfo(BH_File *file, + size_t *size, + const char **name); -static int file_init(bh_file_t *file, - const char *path); +static int BH_FileInit(BH_File *file, + 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, - int *mode); +static int BH_FileOpen(BH_File *file, + 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, - char *data, - size_t *size); +static int BH_FileRead(BH_File *file, + char *data, + size_t *size); -static int file_write(bh_file_t *file, - const char *data, - size_t *size); +static int BH_FileWrite(BH_File *file, + const char *data, + size_t *size); -static int file_peek(bh_file_t *file, - char *data, - size_t *size); +static int BH_FilePeek(BH_File* file, + char *data, + 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, - int64_t *pos, - int *dir); +static int BH_FileSeek(BH_File *file, + int64_t *pos, + int *dir); -static int file_tell(bh_file_t *file, - int64_t *pos); +static int BH_FileTell(BH_File *file, + int64_t *pos); -static int file_size(bh_file_t *file, - int64_t *size); +static int BH_FileSize(BH_File *file, + 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, - size_t *size, - const char **name) +static int BH_FileInfo(BH_File *file, + size_t *size, + const char **name) { static const char classname[] = BH_FILE_CLASSNAME; @@ -84,8 +84,8 @@ static int file_info(bh_file_t *file, } -static int file_init(bh_file_t *file, - const char *path) +static int BH_FileInit(BH_File *file, + const char *path) { /* Check if path is valid */ if (!path) @@ -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 */ if (file->handle != INVALID_HANDLE_VALUE) - file_close(file); + BH_FileClose(file); /* Free path string */ free(file->path); @@ -114,14 +114,14 @@ static int file_destroy(bh_file_t *file) } -static int file_open(bh_file_t *file, - int *mode) +static int BH_FileOpen(BH_File *file, + int *mode) { DWORD access = 0, how = 0; /* Check if file is already openned */ if (file->handle != INVALID_HANDLE_VALUE) - return BH_OK; + return BH_ERROR; /* Determine read/write access flags */ 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->handle == INVALID_HANDLE_VALUE) @@ -183,9 +183,9 @@ static int file_close(bh_file_t *file) } -static int file_read(bh_file_t *file, - char *data, - size_t *size) +static int BH_FileRead(BH_File *file, + char *data, + size_t *size) { DWORD readed; @@ -212,9 +212,9 @@ error: } -static int file_write(bh_file_t *file, - const char *data, - size_t *size) +static int BH_FileWrite(BH_File *file, + const char *data, + size_t *size) { DWORD written; @@ -251,28 +251,28 @@ error: } -static int file_peek(bh_file_t *file, - char *data, - size_t *size) +static int BH_FilePeek(BH_File *file, + char *data, + size_t *size) { int64_t position; int direction; /* Read data from the file */ - if (file_read(file, data, size)) + if (BH_FileRead(file, data, size)) return BH_ERROR; /* Backtrack by the read amount */ position = -((int64_t)*size); direction = BH_IO_SEEK_CUR; - if (file_seek(file, &position, &direction)) + if (BH_FileSeek(file, &position, &direction)) return BH_ERROR; return BH_OK; } -static int file_flush(bh_file_t *file) +static int BH_FileFlush(BH_File *file) { /* Check if file is opened */ if (file->handle == INVALID_HANDLE_VALUE) @@ -288,9 +288,9 @@ error: } -static int file_seek(bh_file_t *file, - int64_t *pos, - int *dir) +static int BH_FileSeek(BH_File *file, + int64_t *pos, + int *dir) { LARGE_INTEGER position; @@ -311,8 +311,8 @@ error: } -static int file_tell(bh_file_t *file, - int64_t *pos) +static int BH_FileTell(BH_File *file, + int64_t *pos) { LARGE_INTEGER dummy, position; @@ -334,8 +334,8 @@ error: } -static int file_size(bh_file_t *file, - int64_t *size) +static int BH_FileSize(BH_File *file, + int64_t *size) { 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 != 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 */ 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, - int type, - void *arg1, - void *arg2) +static int BH_FileCallback(BH_File *file, + int type, + void *arg1, + void *arg2) { switch (type) { - case BH_IO_INFO_CB: return file_info(file, (size_t *)arg1, (const char **)arg2); - case BH_IO_INIT_CB: return file_init(file, (const char *)arg1); - case BH_IO_DESTROY_CB: return file_destroy(file); - case BH_IO_OPEN_CB: return file_open(file, (int *)arg1); - case BH_IO_CLOSE_CB: return file_close(file); - case BH_IO_READ_CB: return file_read(file, (char *)arg1, (size_t *)arg2); - case BH_IO_WRITE_CB: return file_write(file, (const char *)arg1, (size_t *)arg2); - case BH_IO_PEEK_CB: return file_peek(file, (char *)arg1, (size_t *)arg2); - case BH_IO_FLUSH_CB: return file_flush(file); - case BH_IO_SEEK_CB: return file_seek(file, (int64_t *)arg1, (int *)arg2); - case BH_IO_TELL_CB: return file_tell(file, (int64_t *)arg1); - case BH_IO_SIZE_CB: return file_size(file, (int64_t *)arg1); - case BH_IO_FLAGS_CB: return file_flags(file); - case BH_IO_CLEAR_CB: return file_clear(file); + case BH_IO_INFO_CB: return BH_FileInfo(file, (size_t *)arg1, (const char **)arg2); + case BH_IO_INIT_CB: return BH_FileInit(file, (const char *)arg1); + case BH_IO_DESTROY_CB: return BH_FileDestroy(file); + case BH_IO_OPEN_CB: return BH_FileOpen(file, (int *)arg1); + case BH_IO_CLOSE_CB: return BH_FileClose(file); + case BH_IO_READ_CB: return BH_FileRead(file, (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 BH_FilePeek(file, (char *)arg1, (size_t *)arg2); + case BH_IO_FLUSH_CB: return BH_FileFlush(file); + case BH_IO_SEEK_CB: return BH_FileSeek(file, (int64_t *)arg1, (int *)arg2); + case BH_IO_TELL_CB: return BH_FileTell(file, (int64_t *)arg1); + case BH_IO_SIZE_CB: return BH_FileSize(file, (int64_t *)arg1); + case BH_IO_FLAGS_CB: return BH_FileFlags(file); + case BH_IO_CLEAR_CB: return BH_FileClear(file); 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); } diff --git a/test/src/testalgo.c b/test/src/testalgo.c index 1eb8460..63c1879 100644 --- a/test/src/testalgo.c +++ b/test/src/testalgo.c @@ -4,17 +4,17 @@ #include -static int int_equal(const void *lhs, - const void *rhs) +static int DBG_IntEqual(const void *lhs, + const void *rhs) { return *(const int*)lhs - *(const int*)rhs; } -static int verify_partition(size_t index, - int pivot, - int *array, - size_t size) +static int DBG_VerifyPartition(size_t index, + int pivot, + int *array, + size_t size) { size_t i; @@ -30,8 +30,8 @@ static int verify_partition(size_t index, } -static int verify_heap(int *array, - size_t size) +static int DBG_VerifyHeap(int *array, + size_t size) { 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; 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; int tmp; @@ -79,10 +80,10 @@ static void reference_sort(int *array, size_t size) } -static void fill_arrays(int *array, - int *reference, - size_t size, - int mask) +static void DBG_FillArrays(int *array, + int *reference, + size_t size, + int mask) { size_t i; int negate; @@ -95,7 +96,7 @@ static void fill_arrays(int *array, /* Fill the array */ for (i = 0; i < size; ++i) { - array[i] = reference_rand(); + array[i] = DBG_ReferenceRand(); if (mask > 1) 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; int reference[317], data[317]; /* Test empty array and one element arrays */ data[0] = 1; data[1] = 2; data[2] = 3; - bh_sort(data, 0, sizeof(int), int_equal); + BH_Sort(data, 0, sizeof(int), DBG_IntEqual); BH_VERIFY(data[0] == 1); BH_VERIFY(data[1] == 2); 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[1] == 2); BH_VERIFY(data[2] == 3); @@ -129,27 +130,27 @@ static int check_sort(void) /* Test array against different sizes */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, 0); - reference_sort(reference, *size); - bh_sort(data, *size, sizeof(int), int_equal); + DBG_FillArrays(data, reference, *size, 0); + DBG_ReferenceSort(reference, *size); + BH_Sort(data, *size, sizeof(int), DBG_IntEqual); BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0); } /* Test against negative values */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, -1); - reference_sort(reference, *size); - bh_sort(data, *size, sizeof(int), int_equal); + DBG_FillArrays(data, reference, *size, -1); + DBG_ReferenceSort(reference, *size); + BH_Sort(data, *size, sizeof(int), DBG_IntEqual); BH_VERIFY(memcmp(reference, data, *size * sizeof(int)) == 0); } /* Test against duplicates */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, 4); - reference_sort(reference, *size); - bh_sort(data, *size, sizeof(int), int_equal); + DBG_FillArrays(data, reference, *size, 4); + DBG_ReferenceSort(reference, *size); + BH_Sort(data, *size, sizeof(int), DBG_IntEqual); 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; int reference[317], data[317], value, *pivot; @@ -165,13 +166,13 @@ static int check_partition(void) /* Test empty array and one element array */ data[0] = 1; data[1] = 2; data[2] = 3; value = 0; - pivot = bh_partition(&value, data, 0, sizeof(int), int_equal); + pivot = BH_Partition(&value, data, 0, sizeof(int), DBG_IntEqual); BH_VERIFY(pivot != NULL); BH_VERIFY(data[0] == 1); BH_VERIFY(data[1] == 2); BH_VERIFY(data[2] == 3); - pivot = bh_partition(&value, data, 1, sizeof(int), int_equal); + pivot = BH_Partition(&value, data, 1, sizeof(int), DBG_IntEqual); BH_VERIFY(pivot == data); BH_VERIFY(data[0] == 1); BH_VERIFY(data[1] == 2); @@ -180,104 +181,104 @@ static int check_partition(void) /* Test array against different sizes */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, 0); + DBG_FillArrays(data, reference, *size, 0); value = 16384; - pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); } /* Test against negative values */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, -1); + DBG_FillArrays(data, reference, *size, -1); value = -16384; - pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); } /* Test against duplicates */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, 4); + DBG_FillArrays(data, reference, *size, 4); value = 2; - pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); } /* Test array against small pivots */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, 0); + DBG_FillArrays(data, reference, *size, 0); value = -100; - pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); } /* Test array against large pivots */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, 0); + DBG_FillArrays(data, reference, *size, 0); value = 65536; - pivot = bh_partition(&value, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(&value, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); } /* Test array against different sizes (pivot inside the array) */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, 0); + DBG_FillArrays(data, reference, *size, 0); value = data[0]; - pivot = bh_partition(data, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(data, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); } /* Test against negative values (pivot inside the array) */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, -1); + DBG_FillArrays(data, reference, *size, -1); value = data[0]; - pivot = bh_partition(data, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(data, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); BH_VERIFY(memcmp(data, reference, *size * sizeof(int)) == 0); } /* Test against duplicates (pivot inside the array) */ for (size = sizes; *size; ++size) { - fill_arrays(data, reference, *size, 4); + DBG_FillArrays(data, reference, *size, 4); value = data[0]; - pivot = bh_partition(data, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(data, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); 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(reference, 0, sizeof(int) * *size); value = data[*size - 1]; - pivot = bh_partition(data + *size - 1, data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_partition(pivot - data, value, data, *size) == 0); + pivot = BH_Partition(data + *size - 1, data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyPartition(pivot - data, value, data, *size) == 0); - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); 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; int data[317]; /* Test empty array */ data[0] = 1; data[1] = 2; data[2] = 3; - bh_heap_make(data, 0, sizeof(int), int_equal); + BH_HeapMake(data, 0, sizeof(int), DBG_IntEqual); BH_VERIFY(data[0] == 1); BH_VERIFY(data[1] == 2); BH_VERIFY(data[2] == 3); @@ -314,32 +315,32 @@ static int check_heap_make(void) /* Test array against different sizes */ for (size = sizes; *size; ++size) { - fill_arrays(data, NULL, *size, 0); - bh_heap_make(data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_heap(data, *size) == 0); + DBG_FillArrays(data, NULL, *size, 0); + BH_HeapMake(data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyHeap(data, *size) == 0); } /* Test array against negative values */ for (size = sizes; *size; ++size) { - fill_arrays(data, NULL, *size, -1); - bh_heap_make(data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_heap(data, *size) == 0); + DBG_FillArrays(data, NULL, *size, -1); + BH_HeapMake(data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyHeap(data, *size) == 0); } /* Test against duplicates */ for (size = sizes; *size; ++size) { - fill_arrays(data, NULL, *size, 4); - bh_heap_make(data, *size, sizeof(int), int_equal); - BH_VERIFY(verify_heap(data, *size) == 0); + DBG_FillArrays(data, NULL, *size, 4); + BH_HeapMake(data, *size, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyHeap(data, *size) == 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; int data[317], reference[317]; @@ -348,18 +349,18 @@ static int check_heap_insert(void) for (size = sizes; *size; ++size) { size_t i; - fill_arrays(data, reference, *size, 0); + DBG_FillArrays(data, reference, *size, 0); for (i = 0; i < *size; ++i) { int value; value = data[i]; - bh_heap_insert(&value, data, i, sizeof(int), int_equal); - BH_VERIFY(verify_heap(data, i + 1) == 0); + BH_HeapInsert(&value, data, i, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyHeap(data, i + 1) == 0); } - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); BH_VERIFY(memcmp(data, reference, *size) == 0); } @@ -367,15 +368,15 @@ static int check_heap_insert(void) for (size = sizes; *size; ++size) { size_t i; - fill_arrays(data, reference, *size, 0); + DBG_FillArrays(data, reference, *size, 0); for (i = 0; i < *size; ++i) { - bh_heap_insert(NULL, data, i, sizeof(int), int_equal); - BH_VERIFY(verify_heap(data, i + 1) == 0); + BH_HeapInsert(NULL, data, i, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyHeap(data, i + 1) == 0); } - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); 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; int data[317], reference[317]; @@ -392,17 +393,17 @@ static int check_heap_remove(void) for (size = sizes; *size; ++size) { size_t i; - fill_arrays(data, reference, *size, 0); - bh_heap_make(data, *size, sizeof(int), int_equal); + DBG_FillArrays(data, reference, *size, 0); + BH_HeapMake(data, *size, sizeof(int), DBG_IntEqual); for (i = *size; i > 0; i--) { - BH_VERIFY(verify_heap(data, i) == 0); - bh_heap_remove(data, i, sizeof(int), int_equal); + BH_VERIFY(DBG_VerifyHeap(data, i) == 0); + BH_HeapRemove(data, i, sizeof(int), DBG_IntEqual); } - reference_sort(data, *size); - reference_sort(reference, *size); + DBG_ReferenceSort(data, *size); + DBG_ReferenceSort(reference, *size); 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 reference[11] = {1, 2, 3, 5, 11, 23, 41, 79, 163, 317, 0}; int value; /* Prepare test arrays */ - bh_heap_make(data, 10, sizeof(int), int_equal); - bh_heap_make(reference, 10, sizeof(int), int_equal); - BH_VERIFY(verify_heap(data, 10) == 0); - BH_VERIFY(verify_heap(reference, 10) == 0); + BH_HeapMake(data, 10, sizeof(int), DBG_IntEqual); + BH_HeapMake(reference, 10, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyHeap(data, 10) == 0); + BH_VERIFY(DBG_VerifyHeap(reference, 10) == 0); /* Verfify heap replace */ value = 20; - bh_heap_replace(&value, data, 10, sizeof(int), int_equal); - bh_heap_remove(reference, 10, sizeof(int), int_equal); - bh_heap_insert(&value, reference, 9, sizeof(int), int_equal); - - BH_VERIFY(verify_heap(data, 10) == 0); - BH_VERIFY(verify_heap(reference, 10) == 0); - bh_sort(data, 10, sizeof(int), int_equal); - bh_sort(reference, 10, sizeof(int), int_equal); + BH_HeapReplace(&value, data, 10, sizeof(int), DBG_IntEqual); + BH_HeapRemove(reference, 10, sizeof(int), DBG_IntEqual); + BH_HeapInsert(&value, reference, 9, sizeof(int), DBG_IntEqual); + + BH_VERIFY(DBG_VerifyHeap(data, 10) == 0); + BH_VERIFY(DBG_VerifyHeap(reference, 10) == 0); + BH_Sort(data, 10, sizeof(int), DBG_IntEqual); + BH_Sort(reference, 10, sizeof(int), DBG_IntEqual); BH_VERIFY(memcmp(data, reference, 10 * sizeof(int)) == 0); /* Verify heap replace on single element array */ value = 400; - bh_heap_replace(&value, data, 1, sizeof(int), int_equal); - bh_heap_remove(reference, 1, sizeof(int), int_equal); - bh_heap_insert(&value, reference, 0, sizeof(int), int_equal); + BH_HeapReplace(&value, data, 1, sizeof(int), DBG_IntEqual); + BH_HeapRemove(reference, 1, sizeof(int), DBG_IntEqual); + BH_HeapInsert(&value, reference, 0, sizeof(int), DBG_IntEqual); - BH_VERIFY(verify_heap(data, 1) == 0); - BH_VERIFY(verify_heap(reference, 1) == 0); + BH_VERIFY(DBG_VerifyHeap(data, 1) == 0); + BH_VERIFY(DBG_VerifyHeap(reference, 1) == 0); BH_VERIFY(memcmp(data, reference, 1 * sizeof(int)) == 0); /* Prepare test arrays */ - bh_sort(data, 10, sizeof(int), int_equal); - bh_sort(reference, 10, sizeof(int), int_equal); - bh_heap_make(data, 10, sizeof(int), int_equal); - bh_heap_make(reference, 10, sizeof(int), int_equal); - BH_VERIFY(verify_heap(data, 10) == 0); - BH_VERIFY(verify_heap(reference, 10) == 0); + BH_Sort(data, 10, sizeof(int), DBG_IntEqual); + BH_Sort(reference, 10, sizeof(int), DBG_IntEqual); + BH_HeapMake(data, 10, sizeof(int), DBG_IntEqual); + BH_HeapMake(reference, 10, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyHeap(data, 10) == 0); + BH_VERIFY(DBG_VerifyHeap(reference, 10) == 0); data[10] = 1000; value = 1000; - bh_heap_replace(NULL, data, 10, sizeof(int), int_equal); - bh_heap_remove(reference, 10, sizeof(int), int_equal); - bh_heap_insert(&value, reference, 9, sizeof(int), int_equal); - BH_VERIFY(verify_heap(data, 10) == 0); - BH_VERIFY(verify_heap(reference, 10) == 0); - bh_sort(data, 10, sizeof(int), int_equal); - bh_sort(reference, 10, sizeof(int), int_equal); + BH_HeapReplace(NULL, data, 10, sizeof(int), DBG_IntEqual); + BH_HeapRemove(reference, 10, sizeof(int), DBG_IntEqual); + BH_HeapInsert(&value, reference, 9, sizeof(int), DBG_IntEqual); + BH_VERIFY(DBG_VerifyHeap(data, 10) == 0); + BH_VERIFY(DBG_VerifyHeap(reference, 10) == 0); + BH_Sort(data, 10, sizeof(int), DBG_IntEqual); + BH_Sort(reference, 10, sizeof(int), DBG_IntEqual); BH_VERIFY(memcmp(data, reference, 10 * sizeof(int)) == 0); return 0; @@ -472,12 +473,12 @@ int main(int argc, char **argv) BH_UNUSED(argc); BH_UNUSED(argv); - bh_unit_add("sort", check_sort); - bh_unit_add("partition", check_partition); - bh_unit_add("heap_make", check_heap_make); - bh_unit_add("heap_insert", check_heap_insert); - bh_unit_add("heap_remove", check_heap_remove); - bh_unit_add("heap_replace", check_heap_replace); + BH_UnitAdd("Sort", CheckSort); + BH_UnitAdd("Partition", CheckPartition); + BH_UnitAdd("HeapMake", CheckHeapMake); + BH_UnitAdd("HeapInsert", CheckHeapInsert); + BH_UnitAdd("HeapRemove", CheckHeapRemove); + BH_UnitAdd("HeapReplace", CheckHeapReplace); - return bh_unit_run(); + return BH_UnitRun(); } diff --git a/test/src/testfile.c b/test/src/testfile.c index 3cf0581..194b52a 100644 --- a/test/src/testfile.c +++ b/test/src/testfile.c @@ -24,41 +24,41 @@ static void cleanup(void) /** * Check for invalid arguments. */ -static int check_null(void) +static int checkNull(void) { - bh_io_t *io; + BH_IO *io; /* Check against NULL pointers */ - BH_VERIFY(bh_file_new(NULL) == NULL); - BH_VERIFY(bh_io_classname(NULL) == NULL); - BH_VERIFY(bh_io_open(NULL, 0) != BH_OK); - BH_VERIFY(bh_io_close(NULL) != BH_OK); - BH_VERIFY(bh_io_read(NULL, NULL, 0, NULL) != BH_OK); - BH_VERIFY(bh_io_write(NULL, NULL, 0, NULL) != BH_OK); - BH_VERIFY(bh_io_peek(NULL, NULL, 0, NULL) != BH_OK); - BH_VERIFY(bh_io_tell(NULL, NULL) != BH_OK); - BH_VERIFY(bh_io_seek(NULL, 0, 0) != BH_OK); - BH_VERIFY(bh_io_flush(NULL) != BH_OK); - BH_VERIFY(bh_io_size(NULL, NULL) != BH_OK); - BH_VERIFY(bh_io_flags(NULL) == BH_IO_FLAG_ERROR); - BH_VERIFY(bh_io_clear(NULL) == BH_OK); - bh_io_free(NULL); + BH_VERIFY(BH_FileNew(NULL) == NULL); + BH_VERIFY(BH_IOClassname(NULL) == NULL); + BH_VERIFY(BH_IOOpen(NULL, 0) != BH_OK); + BH_VERIFY(BH_IOClose(NULL) != BH_OK); + BH_VERIFY(BH_IORead(NULL, NULL, 0, NULL) != BH_OK); + BH_VERIFY(BH_IOWrite(NULL, NULL, 0, NULL) != BH_OK); + BH_VERIFY(BH_IOPeek(NULL, NULL, 0, NULL) != BH_OK); + BH_VERIFY(BH_IOTell(NULL, NULL) != BH_OK); + BH_VERIFY(BH_IOSeek(NULL, 0, 0) != BH_OK); + BH_VERIFY(BH_IOFlush(NULL) != BH_OK); + BH_VERIFY(BH_IOSize(NULL, NULL) != BH_OK); + BH_VERIFY(BH_IOFlags(NULL) == BH_IO_FLAG_ERROR); + BH_VERIFY(BH_IOClear(NULL) == BH_OK); + BH_IOFree(NULL); /* Check against NULL pointers and valid IO object */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, 0) != BH_OK); - BH_VERIFY(bh_io_close(io) != BH_OK); - BH_VERIFY(bh_io_read(io, NULL, 0, NULL) != BH_OK); - BH_VERIFY(bh_io_write(io, NULL, 0, NULL) != BH_OK); - BH_VERIFY(bh_io_peek(io, NULL, 0, NULL) != BH_OK); - BH_VERIFY(bh_io_tell(io, NULL) != BH_OK); - BH_VERIFY(bh_io_seek(io, 0, 0) != BH_OK); - BH_VERIFY(bh_io_flush(io) != BH_OK); - BH_VERIFY(bh_io_size(io, NULL) != BH_OK); - BH_VERIFY(bh_io_flags(io) == BH_IO_FLAG_ERROR); - BH_VERIFY(bh_io_clear(io) == BH_OK); - bh_io_free(io); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, 0) != BH_OK); + BH_VERIFY(BH_IOClose(io) != BH_OK); + BH_VERIFY(BH_IORead(io, NULL, 0, NULL) != BH_OK); + BH_VERIFY(BH_IOWrite(io, NULL, 0, NULL) != BH_OK); + BH_VERIFY(BH_IOPeek(io, NULL, 0, NULL) != BH_OK); + BH_VERIFY(BH_IOTell(io, NULL) != BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, 0) != BH_OK); + BH_VERIFY(BH_IOFlush(io) != BH_OK); + BH_VERIFY(BH_IOSize(io, NULL) != BH_OK); + BH_VERIFY(BH_IOFlags(io) == BH_IO_FLAG_ERROR); + BH_VERIFY(BH_IOClear(io) == BH_OK); + BH_IOFree(io); return 0; } @@ -67,93 +67,93 @@ static int check_null(void) /** * Check for normal mode. */ -static int check_normal(void) +static int checkNormal(void) { int64_t position; char buffer[128]; size_t actual; - bh_io_t *io; + BH_IO *io; /* Check operations for write only access */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE) == BH_OK); + 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(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(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 10, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(actual == 5); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK); - BH_VERIFY(bh_io_tell(io, &position) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 1, &actual) != BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_END) == BH_OK); + BH_VERIFY(BH_IOTell(io, &position) == BH_OK); BH_VERIFY(position == 20); - bh_io_close(io); + BH_IOClose(io); /* Check operations for read only access */ - BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(actual == 10); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, sizeof(buffer), &actual) == BH_OK); BH_VERIFY(actual == 20); BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0); - BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK); - BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) != BH_OK); + BH_VERIFY(BH_IOSeek(io, -5, BH_IO_SEEK_CUR) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "67890", 5) == 0); - bh_io_free(io); + BH_IOFree(io); /* Check operations for read and write access */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_READ) == BH_OK); + 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(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(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(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(actual == 10); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 35, &actual) == BH_OK); BH_VERIFY(actual == 35); BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0); - bh_io_close(io); + BH_IOClose(io); - bh_io_free(io); + BH_IOFree(io); return 0; } @@ -161,108 +161,108 @@ static int check_normal(void) /** * Check for truncate mode. */ -static int check_truncate(void) +static int checkTruncate(void) { int64_t position; char buffer[128]; size_t actual; - bh_io_t *io; + BH_IO *io; /* Check operations for write only access */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK); + 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(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(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 10, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(actual == 5); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK); - BH_VERIFY(bh_io_tell(io, &position) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 1, &actual) != BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_END) == BH_OK); + BH_VERIFY(BH_IOTell(io, &position) == BH_OK); BH_VERIFY(position == 20); - bh_io_close(io); + BH_IOClose(io); /* Check operations for read only access without truncate */ - BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(actual == 10); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, sizeof(buffer), &actual) == BH_OK); BH_VERIFY(actual == 20); BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0); - BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, -5, BH_IO_SEEK_CUR) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "67890", 5) == 0); - bh_io_close(io); + BH_IOClose(io); /* Check operations for read only access */ - BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); 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(bh_io_write(io, "1234567890", 10, &actual) != BH_OK); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK); + BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) != BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, sizeof(buffer), &actual) == BH_OK); BH_VERIFY(actual == 0); - bh_io_free(io); + BH_IOFree(io); /* Check operations for read and write access */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); + 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(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(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(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(actual == 10); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 35, &actual) == BH_OK); BH_VERIFY(actual == 35); 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_io_flags(io) & BH_IO_FLAG_OPEN); - bh_io_close(io); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_IOClose(io); - bh_io_free(io); + BH_IOFree(io); return 0; } @@ -270,100 +270,100 @@ static int check_truncate(void) /** * Check for exist mode. */ -static int check_exist(void) +static int checkExist(void) { int64_t position; char buffer[128]; size_t actual; - bh_io_t *io; + BH_IO *io; /* Check operations for write only access */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_EXIST) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_EXIST) == BH_OK); + 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(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(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 10, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(actual == 5); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK); - BH_VERIFY(bh_io_tell(io, &position) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 1, &actual) != BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_END) == BH_OK); + BH_VERIFY(BH_IOTell(io, &position) == BH_OK); BH_VERIFY(position == 20); - bh_io_close(io); + BH_IOClose(io); /* Check operations for read only access */ - BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_EXIST) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_EXIST) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(actual == 10); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, sizeof(buffer), &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, sizeof(buffer), &actual) == BH_OK); BH_VERIFY(actual == 20); BH_VERIFY(memcmp(buffer, "1234567890abcde67890", 20) == 0); - BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK); - BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) != BH_OK); + BH_VERIFY(BH_IOSeek(io, -5, BH_IO_SEEK_CUR) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "67890", 5) == 0); - bh_io_free(io); + BH_IOFree(io); /* Check operations for read and write access */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_EXIST) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_READ | BH_IO_EXIST) == BH_OK); + 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(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(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(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(actual == 10); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 35, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 35, &actual) == BH_OK); BH_VERIFY(actual == 35); BH_VERIFY(memcmp(buffer, "abcde12345678901234567890abcde67890", 35) == 0); - bh_io_close(io); - bh_io_free(io); + BH_IOClose(io); + BH_IOFree(io); /* Check against non existing files */ - BH_VERIFY((io = bh_file_new(FILENAME2)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_EXIST) != BH_OK); - BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_EXIST) != BH_OK); - BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_EXIST) != BH_OK); - BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0); - bh_io_free(io); + BH_VERIFY((io = BH_FileNew(FILENAME2)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_EXIST) != BH_OK); + BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_OPEN) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_EXIST) != BH_OK); + BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_OPEN) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_READ | BH_IO_EXIST) != BH_OK); + BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_OPEN) == 0); + BH_IOFree(io); return 0; } @@ -371,83 +371,83 @@ static int check_exist(void) /** * Check in append mode. */ -static int check_append(void) +static int checkAppend(void) { int64_t position; char buffer[128]; size_t actual; - bh_io_t *io; + BH_IO *io; /* Explicitly call cleanup */ cleanup(); /* Check operations for write only access */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_APPEND) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_APPEND) == BH_OK); + 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(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(bh_io_seek(io, 10, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_write(io, "abcde", 5, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 10, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IOWrite(io, "abcde", 5, &actual) == BH_OK); BH_VERIFY(actual == 5); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 1, &actual) != BH_OK); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_END) == BH_OK); - BH_VERIFY(bh_io_tell(io, &position) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 1, &actual) != BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_END) == BH_OK); + BH_VERIFY(BH_IOTell(io, &position) == BH_OK); BH_VERIFY(position == 25); - bh_io_close(io); + BH_IOClose(io); /* Check operations for read only access */ - BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_APPEND) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_APPEND) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(actual == 10); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); - BH_VERIFY(bh_io_read(io, buffer, 10, &actual) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 10, &actual) == BH_OK); BH_VERIFY(actual == 10); BH_VERIFY(memcmp(buffer, "1234567890", 10) == 0); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); - BH_VERIFY(bh_io_write(io, "1234567890", 10, &actual) != BH_OK); - BH_VERIFY(bh_io_seek(io, -5, BH_IO_SEEK_CUR) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 5, &actual) == BH_OK); + BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) != BH_OK); + BH_VERIFY(BH_IOSeek(io, -5, BH_IO_SEEK_CUR) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 5, &actual) == BH_OK); BH_VERIFY(actual == 5); BH_VERIFY(memcmp(buffer, "abcde", 5) == 0); - bh_io_close(io); + BH_IOClose(io); /* Check operations for read and write access */ - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_APPEND) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_READ | BH_IO_APPEND) == BH_OK); + 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(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(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_read(io, buffer, 40, &actual) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IORead(io, buffer, 40, &actual) == BH_OK); BH_VERIFY(actual == 40); 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_io_flags(io) & BH_IO_FLAG_OPEN); - bh_io_close(io); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_TRUNCATE) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_IOClose(io); - bh_io_free(io); + BH_IOFree(io); return 0; } @@ -455,37 +455,37 @@ static int check_append(void) /** * 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 */ - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_CREATE) != BH_OK); - BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_OPEN) == 0); - bh_io_free(io); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_CREATE) != BH_OK); + BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_OPEN) == 0); + BH_IOFree(io); /* Check for new file with write access */ - BH_VERIFY((io = bh_file_new(FILENAME2)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_CREATE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); - bh_io_free(io); + BH_VERIFY((io = BH_FileNew(FILENAME2)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_CREATE) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_IOFree(io); /* Check for new file with read access */ - BH_VERIFY((io = bh_file_new(FILENAME3)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_CREATE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); - bh_io_free(io); + BH_VERIFY((io = BH_FileNew(FILENAME3)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_CREATE) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_IOFree(io); /* Check for new file with read/write access */ - BH_VERIFY((io = bh_file_new(FILENAME4)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_CREATE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); - bh_io_free(io); + BH_VERIFY((io = BH_FileNew(FILENAME4)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_READ | BH_IO_CREATE) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); + BH_IOFree(io); return 0; } @@ -494,23 +494,23 @@ static int check_create(void) /** * Check for EOF flags. */ -static int check_eof(void) +static int checkEOF(void) { char buffer[128]; size_t actual; - bh_io_t *io; + BH_IO *io; - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); + 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(bh_io_flags(io) & BH_IO_FLAG_EOF); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_EOF); - bh_io_close(io); - bh_io_free(io); + BH_IOClose(io); + BH_IOFree(io); return 0; } @@ -519,23 +519,23 @@ static int check_eof(void) /** * Check for error flags. */ -static int check_error(void) +static int checkError(void) { size_t actual; - bh_io_t *io; + BH_IO *io; - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_READ) == BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_OPEN); - BH_VERIFY(bh_io_write(io, "12345", 5, &actual) != BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_ERROR); - BH_VERIFY(bh_io_clear(io) == BH_OK); - BH_VERIFY((bh_io_flags(io) & BH_IO_FLAG_ERROR) == 0); + BH_VERIFY(BH_IOWrite(io, "12345", 5, &actual) != BH_OK); + BH_VERIFY(BH_IOFlags(io) & BH_IO_FLAG_ERROR); + BH_VERIFY(BH_IOClear(io) == BH_OK); + BH_VERIFY((BH_IOFlags(io) & BH_IO_FLAG_ERROR) == 0); - bh_io_close(io); - bh_io_free(io); + BH_IOClose(io); + BH_IOFree(io); return 0; } @@ -544,42 +544,42 @@ static int check_error(void) /** * Check peek operation. */ -static int check_peek(void) +static int checkPeek(void) { int64_t previous, current; char buffer[128]; size_t actual; - bh_io_t *io; + BH_IO *io; - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_WRITE | BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); - BH_VERIFY(bh_io_flags(io) & BH_IO_FLAG_OPEN); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + BH_VERIFY(BH_IOOpen(io, BH_IO_WRITE | BH_IO_READ | BH_IO_TRUNCATE) == BH_OK); + 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(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(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_tell(io, &previous) == BH_OK); - BH_VERIFY(bh_io_peek(io, buffer, 128, &actual) == BH_OK); - BH_VERIFY(bh_io_tell(io, ¤t) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IOTell(io, &previous) == BH_OK); + BH_VERIFY(BH_IOPeek(io, buffer, 128, &actual) == BH_OK); + BH_VERIFY(BH_IOTell(io, ¤t) == BH_OK); BH_VERIFY(actual == 20); BH_VERIFY(memcmp(buffer, "12345678901234567890", 20) == 0); BH_VERIFY(previous == current); - BH_VERIFY(bh_io_seek(io, 0, BH_IO_SEEK_SET) == BH_OK); - BH_VERIFY(bh_io_tell(io, &previous) == BH_OK); - BH_VERIFY(bh_io_peek(io, buffer, 128, &actual) == BH_OK); - BH_VERIFY(bh_io_tell(io, ¤t) == BH_OK); + BH_VERIFY(BH_IOSeek(io, 0, BH_IO_SEEK_SET) == BH_OK); + BH_VERIFY(BH_IOTell(io, &previous) == BH_OK); + BH_VERIFY(BH_IOPeek(io, buffer, 128, &actual) == BH_OK); + BH_VERIFY(BH_IOTell(io, ¤t) == BH_OK); BH_VERIFY(actual == 20); BH_VERIFY(memcmp(buffer, "12345678901234567890", 20) == 0); BH_VERIFY(previous == current); - bh_io_close(io); - bh_io_free(io); + BH_IOClose(io); + BH_IOFree(io); return 0; } @@ -588,20 +588,20 @@ static int check_peek(void) /** * Check file size operation. */ -static int check_size(void) +static int checkSize(void) { - bh_io_t *io; + BH_IO *io; int64_t size; - BH_VERIFY((io = bh_file_new(FILENAME1)) != NULL); - BH_VERIFY(strcmp(bh_io_classname(io), BH_FILE_CLASSNAME) == 0); - BH_VERIFY(bh_io_open(io, BH_IO_READ) == BH_OK); + BH_VERIFY((io = BH_FileNew(FILENAME1)) != NULL); + BH_VERIFY(strcmp(BH_IOClassname(io), BH_FILE_CLASSNAME) == 0); + 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_io_close(io); - bh_io_free(io); + BH_IOClose(io); + BH_IOFree(io); return 0; } @@ -615,16 +615,16 @@ int main(int argc, /* Cleanup */ cleanup(); - bh_unit_add("null", check_null); - bh_unit_add("normal", check_normal); - bh_unit_add("truncate", check_truncate); - bh_unit_add("exist", check_exist); - bh_unit_add("append", check_append); - bh_unit_add("create", check_create); - bh_unit_add("eof", check_eof); - bh_unit_add("error", check_error); - bh_unit_add("peek", check_peek); - bh_unit_add("size", check_size); - - return bh_unit_run(); + BH_UnitAdd("Null", checkNull); + BH_UnitAdd("Normal", checkNormal); + BH_UnitAdd("Truncate", checkTruncate); + BH_UnitAdd("Exist", checkExist); + BH_UnitAdd("Append", checkAppend); + BH_UnitAdd("Create", checkCreate); + BH_UnitAdd("EOF", checkEOF); + BH_UnitAdd("Error", checkError); + BH_UnitAdd("Peek", checkPeek); + BH_UnitAdd("Size", checkSize); + + return BH_UnitRun(); } diff --git a/test/src/testhashmap.c b/test/src/testhashmap.c index b166e5a..88b7396 100644 --- a/test/src/testhashmap.c +++ b/test/src/testhashmap.c @@ -2,161 +2,161 @@ #include -static size_t direct_hash(const void *ptr) +static size_t DBG_PtrIntHash(const void *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); } -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(bh_hashmap_empty(hashmap) != 0); - BH_VERIFY(bh_hashmap_size(hashmap) == 0); - BH_VERIFY(bh_hashmap_capacity(hashmap) == 0); - BH_VERIFY(bh_hashmap_factor(hashmap) >= 0.15f); - BH_VERIFY(bh_hashmap_factor(hashmap) <= 1.0f); + BH_VERIFY(BH_HashmapEmpty(hashmap) != 0); + BH_VERIFY(BH_HashmapSize(hashmap) == 0); + BH_VERIFY(BH_HashmapCapacity(hashmap) == 0); + BH_VERIFY(BH_HashmapFactor(hashmap) >= 0.15f); + BH_VERIFY(BH_HashmapFactor(hashmap) <= 1.0f); - bh_hashmap_free(hashmap); + BH_HashmapFree(hashmap); return 0; } -static int grow_shrink(void) +static int GrowShrink(void) { - bh_hashmap_t *hashmap; + BH_Hashmap *hashmap; void *iter; - hashmap = bh_hashmap_new(direct_equal, direct_hash); + hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash); BH_VERIFY(hashmap != NULL); /* Allocate space for 1024 entries and insert 1 element */ - BH_VERIFY(bh_hashmap_reserve(hashmap, 1024) == 0); - BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(1337), BH_INT2PTR(80085)) == 0); + BH_VERIFY(BH_HashmapReserve(hashmap, 1024) == 0); + BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(1337), BH_INT2PTR(80085)) == 0); /* Check hashmap contents */ - iter = bh_hashmap_iter_next(hashmap, NULL); + iter = BH_HashmapIterNext(hashmap, NULL); BH_VERIFY(iter != NULL); - BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337); - BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085); - BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL); - BH_VERIFY(bh_hashmap_empty(hashmap) == 0); - BH_VERIFY(bh_hashmap_size(hashmap) == 1); - BH_VERIFY(bh_hashmap_capacity(hashmap) >= 1024); + BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337); + BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085); + BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL); + BH_VERIFY(BH_HashmapEmpty(hashmap) == 0); + BH_VERIFY(BH_HashmapSize(hashmap) == 1); + BH_VERIFY(BH_HashmapCapacity(hashmap) >= 1024); /* Change factor and grow */ - bh_hashmap_set_factor(hashmap, 0.35f); + BH_HashmapSetFactor(hashmap, 0.35f); /* Check hashmap contents */ - iter = bh_hashmap_iter_next(hashmap, NULL); + iter = BH_HashmapIterNext(hashmap, NULL); BH_VERIFY(iter != NULL); - BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337); - BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085); - BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL); - BH_VERIFY(bh_hashmap_reserve(hashmap, 8192) == 0); - BH_VERIFY(bh_hashmap_empty(hashmap) == 0); - BH_VERIFY(bh_hashmap_size(hashmap) == 1); - BH_VERIFY(bh_hashmap_capacity(hashmap) >= 8192); - BH_VERIFY(bh_hashmap_factor(hashmap) == 0.35f); + BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337); + BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085); + BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL); + BH_VERIFY(BH_HashmapReserve(hashmap, 8192) == 0); + BH_VERIFY(BH_HashmapEmpty(hashmap) == 0); + BH_VERIFY(BH_HashmapSize(hashmap) == 1); + BH_VERIFY(BH_HashmapCapacity(hashmap) >= 8192); + BH_VERIFY_DELTA(BH_HashmapFactor(hashmap), 0.35f, 0.001f); /* Shrink */ - BH_VERIFY(bh_hashmap_reserve(hashmap, 0) == 0); + BH_VERIFY(BH_HashmapReserve(hashmap, 0) == 0); /* Check hashmap contents */ - iter = bh_hashmap_iter_next(hashmap, NULL); + iter = BH_HashmapIterNext(hashmap, NULL); BH_VERIFY(iter != NULL); - BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_key(iter)) == 1337); - BH_VERIFY(BH_PTR2INT(bh_hashmap_iter_value(iter)) == 80085); - BH_VERIFY(bh_hashmap_iter_next(hashmap, iter) == NULL); - BH_VERIFY(bh_hashmap_empty(hashmap) == 0); - BH_VERIFY(bh_hashmap_size(hashmap) == 1); - BH_VERIFY(bh_hashmap_capacity(hashmap) >= 1); - BH_VERIFY(bh_hashmap_capacity(hashmap) < 8192); - BH_VERIFY(bh_hashmap_factor(hashmap) == 0.35f); + BH_VERIFY(BH_PTR2INT(BH_HashmapIterKey(iter)) == 1337); + BH_VERIFY(BH_PTR2INT(BH_HashmapIterValue(iter)) == 80085); + BH_VERIFY(BH_HashmapIterNext(hashmap, iter) == NULL); + BH_VERIFY(BH_HashmapEmpty(hashmap) == 0); + BH_VERIFY(BH_HashmapSize(hashmap) == 1); + BH_VERIFY(BH_HashmapCapacity(hashmap) >= 1); + BH_VERIFY(BH_HashmapCapacity(hashmap) < 8192); + BH_VERIFY_DELTA(BH_HashmapFactor(hashmap), 0.35f, 0.001f); /* Shrink to 0 (deallocate) */ - bh_hashmap_clear(hashmap); - BH_VERIFY(bh_hashmap_empty(hashmap) != 0); - BH_VERIFY(bh_hashmap_size(hashmap) == 0); - BH_VERIFY(bh_hashmap_capacity(hashmap) > 0); + BH_HashmapClear(hashmap); + BH_VERIFY(BH_HashmapEmpty(hashmap) != 0); + BH_VERIFY(BH_HashmapSize(hashmap) == 0); + BH_VERIFY(BH_HashmapCapacity(hashmap) > 0); - BH_VERIFY(bh_hashmap_reserve(hashmap, 0) == 0); - BH_VERIFY(bh_hashmap_empty(hashmap) != 0); - BH_VERIFY(bh_hashmap_size(hashmap) == 0); - BH_VERIFY(bh_hashmap_capacity(hashmap) == 0); + BH_VERIFY(BH_HashmapReserve(hashmap, 0) == 0); + BH_VERIFY(BH_HashmapEmpty(hashmap) != 0); + BH_VERIFY(BH_HashmapSize(hashmap) == 0); + BH_VERIFY(BH_HashmapCapacity(hashmap) == 0); /* Check hashmap contents */ - iter = bh_hashmap_iter_next(hashmap, NULL); + iter = BH_HashmapIterNext(hashmap, NULL); BH_VERIFY(iter == NULL); - bh_hashmap_free(hashmap); + BH_HashmapFree(hashmap); return 0; } -static int insert_remove(void) +static int InsertRemove(void) { - bh_hashmap_t *hashmap; + BH_Hashmap *hashmap; size_t i, added, removed; void *iter; - hashmap = bh_hashmap_new(direct_equal, direct_hash); + hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash); BH_VERIFY(hashmap != NULL); - bh_hashmap_set_factor(hashmap, 1.0f); + BH_HashmapSetFactor(hashmap, 1.0f); /* Insert elements into hashmap */ added = 0; for (i = 1024; i > 0; i--) { added += (i - 1) / 4; - BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR((i - 1) / 4), BH_INT2PTR(i)) == 0); + BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR((i - 1) / 4), BH_INT2PTR(i)) == 0); } /* Remove elements */ - iter = bh_hashmap_iter_next(hashmap, NULL); + iter = BH_HashmapIterNext(hashmap, NULL); removed = 0; while (iter) { - removed += BH_PTR2INT(bh_hashmap_iter_key(iter)); - bh_hashmap_iter_remove(hashmap, iter); + removed += BH_PTR2INT(BH_HashmapIterKey(iter)); + BH_HashmapIterRemove(hashmap, iter); - iter = bh_hashmap_iter_next(hashmap, NULL); + iter = BH_HashmapIterNext(hashmap, NULL); } /* Check inserted elements are equal to removed */ BH_VERIFY(added == removed); - bh_hashmap_free(hashmap); + BH_HashmapFree(hashmap); return 0; } -static int lookup(void) +static int Lookup(void) { - bh_hashmap_t *hashmap; + BH_Hashmap *hashmap; size_t i; - hashmap = bh_hashmap_new(direct_equal, direct_hash); + hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash); BH_VERIFY(hashmap != NULL); /* Insert elements into hashmap */ for (i = 0; i < 256; i++) - BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(i * 4), BH_INT2PTR(i)) == 0); + BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(i * 4), BH_INT2PTR(i)) == 0); /* Lookup inserted elements */ for (i = 0; i < 256; i++) { void *value; - BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), NULL) == BH_OK); - BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), &value) == BH_OK); + BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), NULL) == BH_OK); + BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), &value) == BH_OK); BH_VERIFY(BH_PTR2INT(value) == (int)i); } @@ -165,55 +165,55 @@ static int lookup(void) { void *value; - BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), NULL) != BH_OK); - BH_VERIFY(bh_hashmap_at(hashmap, BH_INT2PTR(i * 4), &value) != BH_OK); + BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), NULL) != BH_OK); + BH_VERIFY(BH_HashmapAt(hashmap, BH_INT2PTR(i * 4), &value) != BH_OK); } - bh_hashmap_free(hashmap); + BH_HashmapFree(hashmap); return 0; } -static int clear(void) +static int Clear(void) { - bh_hashmap_t *hashmap; + BH_Hashmap *hashmap; size_t i; - hashmap = bh_hashmap_new(direct_equal, direct_hash); + hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash); BH_VERIFY(hashmap != NULL); /* Insert elements into hashmap */ for (i = 0; i < 128; i++) - BH_VERIFY(bh_hashmap_insert(hashmap, BH_INT2PTR(i), 0) == 0); + BH_VERIFY(BH_HashmapInsert(hashmap, BH_INT2PTR(i), 0) == 0); - bh_hashmap_clear(hashmap); + BH_HashmapClear(hashmap); /* Remove non-existing elements */ 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; } -static int fields(void) +static int Fields(void) { - bh_hashmap_t *hashmap; + BH_Hashmap *hashmap; size_t i; - hashmap = bh_hashmap_new(direct_equal, direct_hash); + hashmap = BH_HashmapNew(DBG_PtrIntEqual, DBG_PtrIntHash); BH_VERIFY(hashmap != NULL); - BH_VERIFY(bh_hashmap_empty(hashmap) == 1); + BH_VERIFY(BH_HashmapEmpty(hashmap) == 1); /* Insert elements into hashmap */ 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 */ - BH_VERIFY(bh_hashmap_size(hashmap) == 14); - BH_VERIFY(bh_hashmap_capacity(hashmap) >= 14); - BH_VERIFY(bh_hashmap_empty(hashmap) == 0); + BH_VERIFY(BH_HashmapSize(hashmap) == 14); + BH_VERIFY(BH_HashmapCapacity(hashmap) >= 14); + BH_VERIFY(BH_HashmapEmpty(hashmap) == 0); - bh_hashmap_free(hashmap); + BH_HashmapFree(hashmap); return 0; } @@ -223,12 +223,12 @@ int main(int argc, char **argv) (void)argv; /* Add unit tests */ - bh_unit_add("new_free", new_free); - bh_unit_add("grow_shrink", grow_shrink); - bh_unit_add("insert_remove", insert_remove); - bh_unit_add("lookup", lookup); - bh_unit_add("clear", clear); - bh_unit_add("fields", fields); - - return bh_unit_run(); + BH_UnitAdd("NewFree", NewFree); + BH_UnitAdd("GrowShrink", GrowShrink); + BH_UnitAdd("InsertRemove", InsertRemove); + BH_UnitAdd("Lookup", Lookup); + BH_UnitAdd("Clear", Clear); + BH_UnitAdd("Fields", Fields); + + return BH_UnitRun(); } diff --git a/test/src/testmath.c b/test/src/testmath.c index c927861..e41f726 100644 --- a/test/src/testmath.c +++ b/test/src/testmath.c @@ -5,802 +5,704 @@ #define ACCEPTABLE_DELTA 0.0001f -static int check_point4f(void) +static int checkVec4f(void) { - bh_point4f_t a, b, c, r; + float a[4], b[4], c[4], r[4]; float value; - a.x = 1.0f; a.y = 2.0f; a.z = 3.0f; a.w = 4.0f; - b.x = 5.0f; b.y = 6.0f; b.z = 7.0f; b.w = 8.0f; - c.x = 1.5f; c.y = 2.5f; c.z = 3.5f; c.w = 4.5f; - - bh_point4f_add(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 8.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 10.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 12.0000f, ACCEPTABLE_DELTA); - - bh_point4f_sub(&a, &b, &r); - BH_VERIFY_DELTA(r.x, -4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, -4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, -4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, -4.0000f, ACCEPTABLE_DELTA); - - bh_point4f_mul(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 12.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 21.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 32.0000f, ACCEPTABLE_DELTA); - - bh_point4f_scale(&a, 10.0f, &r); - BH_VERIFY_DELTA(r.x, 10.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 20.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 30.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 40.0000f, ACCEPTABLE_DELTA); - - bh_point4f_madd(&a, &b, &c, &r); - BH_VERIFY_DELTA(r.x, 6.5000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 14.5000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 24.5000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 36.5000f, ACCEPTABLE_DELTA); - - bh_point4f_negate(&a, &r); - BH_VERIFY_DELTA(r.x, -1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, -2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, -3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, -4.0000f, ACCEPTABLE_DELTA); - - value = bh_point4f_dot(&a, &b); + a[0] = 1.0f; a[1] = 2.0f; a[2] = 3.0f; a[3] = 4.0f; + b[0] = 5.0f; b[1] = 6.0f; b[2] = 7.0f; b[3] = 8.0f; + c[0] = 1.5f; c[1] = 2.5f; c[2] = 3.5f; c[3] = 4.5f; + + BH_Vec4fAdd(a, b, r); + BH_VERIFY_DELTA(r[0], 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 8.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 12.0000f, ACCEPTABLE_DELTA); + + BH_Vec4fSub(a, b, r); + BH_VERIFY_DELTA(r[0], -4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], -4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], -4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], -4.0000f, ACCEPTABLE_DELTA); + + BH_Vec4fMul(a, b, r); + BH_VERIFY_DELTA(r[0], 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 12.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 21.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 32.0000f, ACCEPTABLE_DELTA); + + BH_Vec4fScale(a, 10.0f, r); + BH_VERIFY_DELTA(r[0], 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 20.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 30.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 40.0000f, ACCEPTABLE_DELTA); + + BH_Vec4fMulAdd(a, b, c, r); + BH_VERIFY_DELTA(r[0], 6.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 14.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 24.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 36.5000f, ACCEPTABLE_DELTA); + + BH_Vec4fNegate(a, r); + BH_VERIFY_DELTA(r[0], -1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], -2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], -3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], -4.0000f, ACCEPTABLE_DELTA); + + value = BH_Vec4fDot(a, b); BH_VERIFY_DELTA(value, 70.0000f, ACCEPTABLE_DELTA); - value = bh_point4f_dot3(&a, &b); - BH_VERIFY_DELTA(value, 38.0000f, ACCEPTABLE_DELTA); - - bh_point4f_cross(&a, &b, &r); - BH_VERIFY_DELTA(r.x, -4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 8.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, -4.0000f, ACCEPTABLE_DELTA); - - value = bh_point4f_length(&a); + value = BH_Vec4fLength(a); BH_VERIFY_DELTA(value, sqrt(30.0f), ACCEPTABLE_DELTA); - bh_point4f_normal(&a, &r); - BH_VERIFY_DELTA(r.x, 1.0f / sqrt(30.0f), ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f / sqrt(30.0f), ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 3.0f / sqrt(30.0f), ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 4.0f / sqrt(30.0f), ACCEPTABLE_DELTA); - - bh_point4f_min(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 4.0000f, ACCEPTABLE_DELTA); - - bh_point4f_max(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 7.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 8.0000f, ACCEPTABLE_DELTA); - - bh_point4f_lerp(&a, &b, 0.0f, &r); - BH_VERIFY_DELTA(r.x, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 4.0000f, ACCEPTABLE_DELTA); - - bh_point4f_lerp(&a, &b, 0.5f, &r); - BH_VERIFY_DELTA(r.x, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 6.0000f, ACCEPTABLE_DELTA); - - bh_point4f_lerp(&a, &b, 1.0f, &r); - BH_VERIFY_DELTA(r.x, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 7.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 8.0000f, ACCEPTABLE_DELTA); - - bh_point4f_slerp(&a, &b, 0.0f, &r); - BH_VERIFY_DELTA(r.x, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 4.0000f, ACCEPTABLE_DELTA); - - bh_point4f_slerp(&a, &b, 0.5f, &r); - BH_VERIFY_DELTA(r.x, 0.50350f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 0.67135f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 0.83918f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 1.00702f, ACCEPTABLE_DELTA); - - bh_point4f_slerp(&a, &b, 1.0f, &r); - BH_VERIFY_DELTA(r.x, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 7.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w, 8.0000f, ACCEPTABLE_DELTA); + BH_Vec4fNormal(a, r); + BH_VERIFY_DELTA(r[0], 1.0f / sqrt(30.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0f / sqrt(30.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 3.0f / sqrt(30.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 4.0f / sqrt(30.0f), ACCEPTABLE_DELTA); + + BH_Vec4fMin(a, b, r); + BH_VERIFY_DELTA(r[0], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 4.0000f, ACCEPTABLE_DELTA); + + BH_Vec4fMax(a, b, r); + BH_VERIFY_DELTA(r[0], 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 8.0000f, ACCEPTABLE_DELTA); + + BH_Vec4fLerp(a, b, 0.0f, r); + BH_VERIFY_DELTA(r[0], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 4.0000f, ACCEPTABLE_DELTA); + + BH_Vec4fLerp(a, b, 0.5f, r); + BH_VERIFY_DELTA(r[0], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 6.0000f, ACCEPTABLE_DELTA); + + BH_Vec4fLerp(a, b, 1.0f, r); + BH_VERIFY_DELTA(r[0], 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 8.0000f, ACCEPTABLE_DELTA); return 0; } -static int check_point3f(void) +static int checkVec3f(void) { - bh_point3f_t a, b, c, r; + float a[3], b[3], c[3], r[3]; float value; - a.x = 1.0f; a.y = 2.0f; a.z = 3.0f; - b.x = 5.0f; b.y = 6.0f; b.z = 7.0f; - c.x = 1.5f; c.y = 2.5f; c.z = 3.5f; - - bh_point3f_add(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 6.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 8.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 10.0f, ACCEPTABLE_DELTA); - - bh_point3f_sub(&a, &b, &r); - BH_VERIFY_DELTA(r.x, -4.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, -4.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, -4.0f, ACCEPTABLE_DELTA); - - bh_point3f_mul(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 12.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 21.0f, ACCEPTABLE_DELTA); - - bh_point3f_scale(&a, 10.0f, &r); - BH_VERIFY_DELTA(r.x, 10.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 20.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 30.0f, ACCEPTABLE_DELTA); - - bh_point3f_madd(&a, &b, &c, &r); - BH_VERIFY_DELTA(r.x, 6.5f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 14.5f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 24.5f, ACCEPTABLE_DELTA); - - bh_point3f_negate(&a, &r); - BH_VERIFY_DELTA(r.x, -1.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, -2.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, -3.0f, ACCEPTABLE_DELTA); - - value = bh_point3f_dot(&a, &b); + a[0] = 1.0f; a[1] = 2.0f; a[2] = 3.0f; + b[0] = 5.0f; b[1] = 6.0f; b[2] = 7.0f; + c[0] = 1.5f; c[1] = 2.5f; c[2] = 3.5f; + + BH_Vec3fAdd(a, b, r); + BH_VERIFY_DELTA(r[0], 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 8.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 10.0f, ACCEPTABLE_DELTA); + + BH_Vec3fSub(a, b, r); + BH_VERIFY_DELTA(r[0], -4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], -4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], -4.0f, ACCEPTABLE_DELTA); + + BH_Vec3fMul(a, b, r); + BH_VERIFY_DELTA(r[0], 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 12.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 21.0f, ACCEPTABLE_DELTA); + + BH_Vec3fScale(a, 10.0f, r); + BH_VERIFY_DELTA(r[0], 10.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 20.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 30.0f, ACCEPTABLE_DELTA); + + BH_Vec3fMulAdd(a, b, c, r); + BH_VERIFY_DELTA(r[0], 6.5f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 14.5f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 24.5f, ACCEPTABLE_DELTA); + + BH_Vec3fNegate(a, r); + BH_VERIFY_DELTA(r[0], -1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], -2.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], -3.0f, ACCEPTABLE_DELTA); + + value = BH_Vec3fDot(a, b); BH_VERIFY_DELTA(value, 38.0f, ACCEPTABLE_DELTA); - bh_point3f_cross(&a, &b, &r); - BH_VERIFY_DELTA(r.x, -4.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 8.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, -4.0f, ACCEPTABLE_DELTA); + BH_Vec3fCross(a, b, r); + BH_VERIFY_DELTA(r[0], -4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 8.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], -4.0f, ACCEPTABLE_DELTA); - value = bh_point3f_length(&a); + value = BH_Vec3fLength(a); BH_VERIFY_DELTA(value, sqrt(14.0f), ACCEPTABLE_DELTA); - bh_point3f_normal(&a, &r); - BH_VERIFY_DELTA(r.x, 1.0f / sqrt(14.0f), ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f / sqrt(14.0f), ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 3.0f / sqrt(14.0f), ACCEPTABLE_DELTA); - - bh_point3f_min(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 3.0f, ACCEPTABLE_DELTA); - - bh_point3f_max(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 7.0f, ACCEPTABLE_DELTA); - - bh_point3f_lerp(&a, &b, 0.0f, &r); - BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 3.0f, ACCEPTABLE_DELTA); - - bh_point3f_lerp(&a, &b, 0.5f, &r); - BH_VERIFY_DELTA(r.x, 3.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 4.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 5.0f, ACCEPTABLE_DELTA); - - bh_point3f_lerp(&a, &b, 1.0f, &r); - BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 7.0f, ACCEPTABLE_DELTA); - - bh_point3f_slerp(&a, &b, 0.0f, &r); - BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 3.0f, ACCEPTABLE_DELTA); - - bh_point3f_slerp(&a, &b, 0.5f, &r); - BH_VERIFY_DELTA(r.x, 0.67937f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 0.90582f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 1.13228f, ACCEPTABLE_DELTA); - - bh_point3f_slerp(&a, &b, 1.0f, &r); - BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z, 7.0f, ACCEPTABLE_DELTA); + BH_Vec3fNormal(a, r); + BH_VERIFY_DELTA(r[0], 1.0f / sqrt(14.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0f / sqrt(14.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 3.0f / sqrt(14.0f), ACCEPTABLE_DELTA); + + BH_Vec3fMin(a, b, r); + BH_VERIFY_DELTA(r[0], 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 3.0f, ACCEPTABLE_DELTA); + + BH_Vec3fMax(a, b, r); + BH_VERIFY_DELTA(r[0], 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 7.0f, ACCEPTABLE_DELTA); + + BH_Vec3fLerp(a, b, 0.0f, r); + BH_VERIFY_DELTA(r[0], 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 3.0f, ACCEPTABLE_DELTA); + + BH_Vec3fLerp(a, b, 0.5f, r); + BH_VERIFY_DELTA(r[0], 3.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 5.0f, ACCEPTABLE_DELTA); + + BH_Vec3fLerp(a, b, 1.0f, r); + BH_VERIFY_DELTA(r[0], 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 7.0f, ACCEPTABLE_DELTA); return 0; } -static int check_point2f(void) +static int checkVec2f(void) { - bh_point2f_t a, b, c, r; + float a[2], b[2], c[2], r[2]; float value; - a.x = 1.0f; a.y = 2.0f; - b.x = 5.0f; b.y = 6.0f; - c.x = 1.5f; c.y = 2.5f; + a[0] = 1.0f; a[1] = 2.0f; + b[0] = 5.0f; b[1] = 6.0f; + c[0] = 1.5f; c[1] = 2.5f; - bh_point2f_add(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 6.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 8.0f, ACCEPTABLE_DELTA); + BH_Vec2fAdd(a, b, r); + BH_VERIFY_DELTA(r[0], 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 8.0f, ACCEPTABLE_DELTA); - bh_point2f_sub(&a, &b, &r); - BH_VERIFY_DELTA(r.x, -4.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, -4.0f, ACCEPTABLE_DELTA); + BH_Vec2fSub(a, b, r); + BH_VERIFY_DELTA(r[0], -4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], -4.0f, ACCEPTABLE_DELTA); - bh_point2f_mul(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 12.0f, ACCEPTABLE_DELTA); + BH_Vec2fMul(a, b, r); + BH_VERIFY_DELTA(r[0], 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 12.0f, ACCEPTABLE_DELTA); - bh_point2f_scale(&a, 10.0f, &r); - BH_VERIFY_DELTA(r.x, 10.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 20.0f, ACCEPTABLE_DELTA); + BH_Vec2fScale(a, 10.0f, r); + BH_VERIFY_DELTA(r[0], 10.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 20.0f, ACCEPTABLE_DELTA); - bh_point2f_madd(&a, &b, &c, &r); - BH_VERIFY_DELTA(r.x, 6.5f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 14.5f, ACCEPTABLE_DELTA); + BH_Vec2fMulAdd(a, b, c, r); + BH_VERIFY_DELTA(r[0], 6.5f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 14.5f, ACCEPTABLE_DELTA); - bh_point2f_negate(&a, &r); - BH_VERIFY_DELTA(r.x, -1.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, -2.0f, ACCEPTABLE_DELTA); + BH_Vec2fNegate(a, r); + BH_VERIFY_DELTA(r[0], -1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], -2.0f, ACCEPTABLE_DELTA); - value = bh_point2f_dot(&a, &b); + value = BH_Vec2fDot(a, b); BH_VERIFY_DELTA(value, 17.0f, ACCEPTABLE_DELTA); - value = bh_point2f_cross(&a, &b); + value = BH_Vec2fCross(a, b); BH_VERIFY_DELTA(value, -4.0f, ACCEPTABLE_DELTA); - value = bh_point2f_length(&a); + value = BH_Vec2fLength(a); BH_VERIFY_DELTA(value, sqrt(5.0f), ACCEPTABLE_DELTA); - bh_point2f_normal(&a, &r); - BH_VERIFY_DELTA(r.x, 1.0f / sqrt(5.0f), ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f / sqrt(5.0f), ACCEPTABLE_DELTA); + BH_Vec2fNormal(a, r); + BH_VERIFY_DELTA(r[0], 1.0f / sqrt(5.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0f / sqrt(5.0f), ACCEPTABLE_DELTA); - bh_point2f_min(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); + BH_Vec2fMin(a, b, r); + BH_VERIFY_DELTA(r[0], 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0f, ACCEPTABLE_DELTA); - bh_point2f_max(&a, &b, &r); - BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); + BH_Vec2fMax(a, b, r); + BH_VERIFY_DELTA(r[0], 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0f, ACCEPTABLE_DELTA); - bh_point2f_lerp(&a, &b, 0.0f, &r); - BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); + BH_Vec2fLerp(a, b, 0.0f, r); + BH_VERIFY_DELTA(r[0], 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 2.0f, ACCEPTABLE_DELTA); - bh_point2f_lerp(&a, &b, 0.5f, &r); - BH_VERIFY_DELTA(r.x, 3.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 4.0f, ACCEPTABLE_DELTA); + BH_Vec2fLerp(a, b, 0.5f, r); + BH_VERIFY_DELTA(r[0], 3.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 4.0f, ACCEPTABLE_DELTA); - bh_point2f_lerp(&a, &b, 1.0f, &r); - BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); - - bh_point2f_slerp(&a, &b, 0.0f, &r); - BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); - - bh_point2f_slerp(&a, &b, 0.5f, &r); - BH_VERIFY_DELTA(r.x, 1.00000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 1.33333f, ACCEPTABLE_DELTA); - - bh_point2f_slerp(&a, &b, 1.0f, &r); - BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); + BH_Vec2fLerp(a, b, 1.0f, r); + BH_VERIFY_DELTA(r[0], 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0f, ACCEPTABLE_DELTA); return 0; } -static int check_point4i(void) +static int checkVec4i(void) { - bh_point4i_t a, b, c, r; - - a.x = 1; a.y = 2; a.z = 3; a.w = 4; - b.x = 5; b.y = 6; b.z = 7; b.w = 8; - c.x = 4; c.y = 3; c.z = 2; c.w = 1; - - bh_point4i_add(&a, &b, &r); - BH_VERIFY(r.x == 6); - BH_VERIFY(r.y == 8); - BH_VERIFY(r.z == 10); - BH_VERIFY(r.w == 12); - - bh_point4i_sub(&a, &b, &r); - BH_VERIFY(r.x == -4); - BH_VERIFY(r.y == -4); - BH_VERIFY(r.z == -4); - BH_VERIFY(r.w == -4); - - bh_point4i_mul(&a, &b, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 12); - BH_VERIFY(r.z == 21); - BH_VERIFY(r.w == 32); - - bh_point4i_scale(&a, 10, &r); - BH_VERIFY(r.x == 10); - BH_VERIFY(r.y == 20); - BH_VERIFY(r.z == 30); - BH_VERIFY(r.w == 40); - - bh_point4i_madd(&a, &b, &c, &r); - BH_VERIFY(r.x == 9); - BH_VERIFY(r.y == 15); - BH_VERIFY(r.z == 23); - BH_VERIFY(r.w == 33); - - bh_point4i_negate(&a, &r); - BH_VERIFY(r.x == -1); - BH_VERIFY(r.y == -2); - BH_VERIFY(r.z == -3); - BH_VERIFY(r.w == -4); - - bh_point4i_min(&a, &b, &r); - BH_VERIFY(r.x == 1); - BH_VERIFY(r.y == 2); - BH_VERIFY(r.z == 3); - BH_VERIFY(r.w == 4); - - bh_point4i_max(&a, &b, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 6); - BH_VERIFY(r.z == 7); - BH_VERIFY(r.w == 8); - - bh_point4i_lerp(&a, &b, 0.0f, &r); - BH_VERIFY(r.x == 1); - BH_VERIFY(r.y == 2); - BH_VERIFY(r.z == 3); - BH_VERIFY(r.w == 4); - - bh_point4i_lerp(&a, &b, 0.5f, &r); - BH_VERIFY(r.x == 3); - BH_VERIFY(r.y == 4); - BH_VERIFY(r.z == 5); - BH_VERIFY(r.w == 6); - - bh_point4i_lerp(&a, &b, 1.0f, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 6); - BH_VERIFY(r.z == 7); - BH_VERIFY(r.w == 8); + int a[4], b[4], c[4], r[4]; + + a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; + b[0] = 5; b[1] = 6; b[2] = 7; b[3] = 8; + c[0] = 4; c[1] = 3; c[2] = 2; c[3] = 1; + + BH_Vec4iAdd(a, b, r); + BH_VERIFY(r[0] == 6); + BH_VERIFY(r[1] == 8); + BH_VERIFY(r[2] == 10); + BH_VERIFY(r[3] == 12); + + BH_Vec4iSub(a, b, r); + BH_VERIFY(r[0] == -4); + BH_VERIFY(r[1] == -4); + BH_VERIFY(r[2] == -4); + BH_VERIFY(r[3] == -4); + + BH_Vec4iMul(a, b, r); + BH_VERIFY(r[0] == 5); + BH_VERIFY(r[1] == 12); + BH_VERIFY(r[2] == 21); + BH_VERIFY(r[3] == 32); + + BH_Vec4iScale(a, 10, r); + BH_VERIFY(r[0] == 10); + BH_VERIFY(r[1] == 20); + BH_VERIFY(r[2] == 30); + BH_VERIFY(r[3] == 40); + + BH_Vec4iMulAdd(a, b, c, r); + BH_VERIFY(r[0] == 9); + BH_VERIFY(r[1] == 15); + BH_VERIFY(r[2] == 23); + BH_VERIFY(r[3] == 33); + + BH_Vec4iNegate(a, r); + BH_VERIFY(r[0] == -1); + BH_VERIFY(r[1] == -2); + BH_VERIFY(r[2] == -3); + BH_VERIFY(r[3] == -4); + + BH_Vec4iMin(a, b, r); + BH_VERIFY(r[0] == 1); + BH_VERIFY(r[1] == 2); + BH_VERIFY(r[2] == 3); + BH_VERIFY(r[3] == 4); + + BH_Vec4iMax(a, b, r); + BH_VERIFY(r[0] == 5); + BH_VERIFY(r[1] == 6); + BH_VERIFY(r[2] == 7); + BH_VERIFY(r[3] == 8); return 0; } -static int check_point3i(void) +static int checkVec3i(void) { - bh_point3i_t a, b, c, r; - - a.x = 1; a.y = 2; a.z = 3; - b.x = 5; b.y = 6; b.z = 7; - c.x = 4; c.y = 3; c.z = 2; - - bh_point3i_add(&a, &b, &r); - BH_VERIFY(r.x == 6); - BH_VERIFY(r.y == 8); - BH_VERIFY(r.z == 10); - - bh_point3i_sub(&a, &b, &r); - BH_VERIFY(r.x == -4); - BH_VERIFY(r.y == -4); - BH_VERIFY(r.z == -4); - - bh_point3i_mul(&a, &b, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 12); - BH_VERIFY(r.z == 21); - - bh_point3i_scale(&a, 10, &r); - BH_VERIFY(r.x == 10); - BH_VERIFY(r.y == 20); - BH_VERIFY(r.z == 30); - - bh_point3i_madd(&a, &b, &c, &r); - BH_VERIFY(r.x == 9); - BH_VERIFY(r.y == 15); - BH_VERIFY(r.z == 23); - - bh_point3i_negate(&a, &r); - BH_VERIFY(r.x == -1); - BH_VERIFY(r.y == -2); - BH_VERIFY(r.z == -3); - - bh_point3i_min(&a, &b, &r); - BH_VERIFY(r.x == 1); - BH_VERIFY(r.y == 2); - BH_VERIFY(r.z == 3); - - bh_point3i_max(&a, &b, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 6); - BH_VERIFY(r.z == 7); - - bh_point3i_lerp(&a, &b, 0.0f, &r); - BH_VERIFY(r.x == 1); - BH_VERIFY(r.y == 2); - BH_VERIFY(r.z == 3); - - bh_point3i_lerp(&a, &b, 0.5f, &r); - BH_VERIFY(r.x == 3); - BH_VERIFY(r.y == 4); - BH_VERIFY(r.z == 5); - - bh_point3i_lerp(&a, &b, 1.0f, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 6); - BH_VERIFY(r.z == 7); + int a[3], b[3], c[3], r[3]; + + a[0] = 1; a[1] = 2; a[2] = 3; + b[0] = 5; b[1] = 6; b[2] = 7; + c[0] = 4; c[1] = 3; c[2] = 2; + + BH_Vec3iAdd(a, b, r); + BH_VERIFY(r[0] == 6); + BH_VERIFY(r[1] == 8); + BH_VERIFY(r[2] == 10); + + BH_Vec3iSub(a, b, r); + BH_VERIFY(r[0] == -4); + BH_VERIFY(r[1] == -4); + BH_VERIFY(r[2] == -4); + + BH_Vec3iMul(a, b, r); + BH_VERIFY(r[0] == 5); + BH_VERIFY(r[1] == 12); + BH_VERIFY(r[2] == 21); + + BH_Vec3iScale(a, 10, r); + BH_VERIFY(r[0] == 10); + BH_VERIFY(r[1] == 20); + BH_VERIFY(r[2] == 30); + + BH_Vec3iMulAdd(a, b, c, r); + BH_VERIFY(r[0] == 9); + BH_VERIFY(r[1] == 15); + BH_VERIFY(r[2] == 23); + + BH_Vec3iNegate(a, r); + BH_VERIFY(r[0] == -1); + BH_VERIFY(r[1] == -2); + BH_VERIFY(r[2] == -3); + + BH_Vec3iMin(a, b, r); + BH_VERIFY(r[0] == 1); + BH_VERIFY(r[1] == 2); + BH_VERIFY(r[2] == 3); + + BH_Vec3iMax(a, b, r); + BH_VERIFY(r[0] == 5); + BH_VERIFY(r[1] == 6); + BH_VERIFY(r[2] == 7); return 0; } -static int check_point2i(void) +static int checkVec2i(void) { - bh_point2i_t a, b, c, r; - - a.x = 1; a.y = 2; - b.x = 5; b.y = 6; - c.x = 4; c.y = 3; - - bh_point2i_add(&a, &b, &r); - BH_VERIFY(r.x == 6); - BH_VERIFY(r.y == 8); - - bh_point2i_sub(&a, &b, &r); - BH_VERIFY(r.x == -4); - BH_VERIFY(r.y == -4); + int a[2], b[2], c[2], r[2]; - bh_point2i_mul(&a, &b, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 12); + a[0] = 1; a[1] = 2; + b[0] = 5; b[1] = 6; + c[0] = 4; c[1] = 3; - bh_point2i_scale(&a, 10, &r); - BH_VERIFY(r.x == 10); - BH_VERIFY(r.y == 20); + BH_Vec2iAdd(a, b, r); + BH_VERIFY(r[0] == 6); + BH_VERIFY(r[1] == 8); - bh_point2i_madd(&a, &b, &c, &r); - BH_VERIFY(r.x == 9); - BH_VERIFY(r.y == 15); + BH_Vec2iSub(a, b, r); + BH_VERIFY(r[0] == -4); + BH_VERIFY(r[1] == -4); - bh_point2i_negate(&a, &r); - BH_VERIFY(r.x == -1); - BH_VERIFY(r.y == -2); + BH_Vec2iMul(a, b, r); + BH_VERIFY(r[0] == 5); + BH_VERIFY(r[1] == 12); - bh_point2i_min(&a, &b, &r); - BH_VERIFY(r.x == 1); - BH_VERIFY(r.y == 2); + BH_Vec2iScale(a, 10, r); + BH_VERIFY(r[0] == 10); + BH_VERIFY(r[1] == 20); - bh_point2i_max(&a, &b, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 6); + BH_Vec2iMulAdd(a, b, c, r); + BH_VERIFY(r[0] == 9); + BH_VERIFY(r[1] == 15); - bh_point2i_lerp(&a, &b, 0.0f, &r); - BH_VERIFY(r.x == 1); - BH_VERIFY(r.y == 2); + BH_Vec2iNegate(a, r); + BH_VERIFY(r[0] == -1); + BH_VERIFY(r[1] == -2); - bh_point2i_lerp(&a, &b, 0.5f, &r); - BH_VERIFY(r.x == 3); - BH_VERIFY(r.y == 4); + BH_Vec2iMin(a, b, r); + BH_VERIFY(r[0] == 1); + BH_VERIFY(r[1] == 2); - bh_point2i_lerp(&a, &b, 1.0f, &r); - BH_VERIFY(r.x == 5); - BH_VERIFY(r.y == 6); + BH_Vec2iMax(a, b, r); + BH_VERIFY(r[0] == 5); + BH_VERIFY(r[1] == 6); return 0; } -static int check_quat(void) +static int checkQuat(void) { return 0; } -static int check_matrix4f(void) +static int checkMat4f(void) { - bh_matrix4f_t a, b, r; + float a[16], b[16], r[16]; float value; - a.x.x = 5.0f; a.y.x = 1.0f; a.z.x = 2.0f; a.w.x = 7.0f; - a.x.y = 3.0f; a.y.y = 0.0f; a.z.y = 0.0f; a.w.y = 2.0f; - a.x.z = 1.0f; a.y.z = 3.0f; a.z.z = 4.0f; a.w.z = 5.0f; - a.x.w = 2.0f; a.y.w = 0.0f; a.z.w = 0.0f; a.w.w = 3.0f; - - b.x.x = 5.0f; b.y.x = 1.0f; b.z.x = 2.0f; b.w.x = 7.0f; - b.x.y = 3.0f; b.y.y = 1.0f; b.z.y = 0.0f; b.w.y = 2.0f; - b.x.z = 4.0f; b.y.z = 2.0f; b.z.z = 4.0f; b.w.z = 5.0f; - b.x.w = 6.0f; b.y.w = 2.0f; b.z.w = 0.0f; b.w.w = 4.0f; - - bh_matrix4f_identity(&r); - BH_VERIFY(r.x.x == 1.0f); - BH_VERIFY(r.x.y == 0.0f); - BH_VERIFY(r.x.z == 0.0f); - BH_VERIFY(r.x.w == 0.0f); - - BH_VERIFY(r.y.x == 0.0f); - BH_VERIFY(r.y.y == 1.0f); - BH_VERIFY(r.y.z == 0.0f); - BH_VERIFY(r.y.w == 0.0f); - - BH_VERIFY(r.z.x == 0.0f); - BH_VERIFY(r.z.y == 0.0f); - BH_VERIFY(r.z.z == 1.0f); - BH_VERIFY(r.z.w == 0.0f); - - BH_VERIFY(r.w.x == 0.0f); - BH_VERIFY(r.w.y == 0.0f); - BH_VERIFY(r.w.z == 0.0f); - BH_VERIFY(r.w.w == 1.0f); - - bh_matrix4f_add(&a, &b, &r); - BH_VERIFY_DELTA(r.x.x, 10.0f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.w, 8.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.w, 2.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 8.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.w, 0.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.w.x, 14.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.y, 4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.z, 10.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.w, 7.0000f, ACCEPTABLE_DELTA); - - bh_matrix4f_sub(&a, &b, &r); - BH_VERIFY_DELTA(r.x.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, -3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.w, -4.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, -1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.w, -2.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.w, 0.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.w.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.z, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.w, -1.0000f, ACCEPTABLE_DELTA); - - bh_matrix4f_mul(&a, &b, &r); - BH_VERIFY_DELTA(r.x.x, 78.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 27.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 60.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.w, 28.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 24.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 7.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 22.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.w, 8.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 18.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 18.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.w, 4.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.w.x, 75.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.y, 29.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.z, 53.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.w, 26.0000f, ACCEPTABLE_DELTA); - - bh_matrix4f_scale(&a, 10, &r); - BH_VERIFY_DELTA(r.x.x, 50.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 30.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 10.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.w, 20.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 10.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 30.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.w, 0.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 20.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 40.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.w, 0.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.w.x, 70.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.y, 20.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.z, 50.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.w, 30.0000f, ACCEPTABLE_DELTA); - - bh_matrix4f_transpose(&a, &r); - BH_VERIFY_DELTA(r.x.x, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.w, 7.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.w, 2.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.w, 5.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.w.x, 2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.z, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.w, 3.0000f, ACCEPTABLE_DELTA); - - value = bh_matrix4f_trace(&a); + a[0] = 5.0f; a[4] = 1.0f; a[8] = 2.0f; a[12] = 7.0f; + a[1] = 3.0f; a[5] = 0.0f; a[9] = 0.0f; a[13] = 2.0f; + a[2] = 1.0f; a[6] = 3.0f; a[10] = 4.0f; a[14] = 5.0f; + a[3] = 2.0f; a[7] = 0.0f; a[11] = 0.0f; a[15] = 3.0f; + + b[0] = 5.0f; b[4] = 1.0f; b[8] = 2.0f; b[12] = 7.0f; + b[1] = 3.0f; b[5] = 1.0f; b[9] = 0.0f; b[13] = 2.0f; + b[2] = 4.0f; b[6] = 2.0f; b[10] = 4.0f; b[14] = 5.0f; + b[3] = 6.0f; b[7] = 2.0f; b[11] = 0.0f; b[15] = 4.0f; + + BH_Mat4fIdentity(r); + BH_VERIFY(r[0] == 1.0f); + BH_VERIFY(r[1] == 0.0f); + BH_VERIFY(r[2] == 0.0f); + BH_VERIFY(r[3] == 0.0f); + + BH_VERIFY(r[4] == 0.0f); + BH_VERIFY(r[5] == 1.0f); + BH_VERIFY(r[6] == 0.0f); + BH_VERIFY(r[7] == 0.0f); + + BH_VERIFY(r[8] == 0.0f); + BH_VERIFY(r[9] == 0.0f); + BH_VERIFY(r[10] == 1.0f); + BH_VERIFY(r[11] == 0.0f); + + BH_VERIFY(r[12] == 0.0f); + BH_VERIFY(r[13] == 0.0f); + BH_VERIFY(r[14] == 0.0f); + BH_VERIFY(r[15] == 1.0f); + + BH_Mat4fAdd(a, b, r); + BH_VERIFY_DELTA(r[0], 10.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 8.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[4], 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[6], 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 2.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[8], 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[9], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[10], 8.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[11], 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[12], 14.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[13], 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[14], 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[15], 7.0000f, ACCEPTABLE_DELTA); + + BH_Mat4fSub(a, b, r); + BH_VERIFY_DELTA(r[0], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], -3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], -4.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[4], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], -1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[6], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], -2.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[8], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[9], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[10], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[11], 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[12], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[13], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[14], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[15], -1.0000f, ACCEPTABLE_DELTA); + + BH_Mat4fMul(a, b, r); + BH_VERIFY_DELTA(r[0], 78.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 27.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 60.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 28.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[4], 24.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[6], 22.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 8.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[8], 18.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[9], 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[10], 18.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[11], 4.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[12], 75.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[13], 29.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[14], 53.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[15], 26.0000f, ACCEPTABLE_DELTA); + + BH_Mat4fScale(a, 10, r); + BH_VERIFY_DELTA(r[0], 50.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 30.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 20.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[4], 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[6], 30.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[8], 20.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[9], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[10], 40.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[11], 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[12], 70.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[13], 20.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[14], 50.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[15], 30.0000f, ACCEPTABLE_DELTA); + + BH_Mat4fTranspose(a, r); + BH_VERIFY_DELTA(r[0], 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 7.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[4], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[6], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 2.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[8], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[9], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[10], 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[11], 5.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[12], 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[13], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[14], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[15], 3.0000f, ACCEPTABLE_DELTA); + + value = BH_Mat4fTrace(a); BH_VERIFY_DELTA(value, 12.0000f, ACCEPTABLE_DELTA); - value = bh_matrix4f_determinant(&a); + value = BH_Mat4fDet(a); BH_VERIFY_DELTA(value, 10.0000f, ACCEPTABLE_DELTA); - value = bh_matrix4f_determinant(&b); + value = BH_Mat4fDet(b); BH_VERIFY_DELTA(value, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY(bh_matrix4f_inverse(&a, &r) == BH_OK); - BH_VERIFY_DELTA(r.x.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, -2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 1.5000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.w, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY(BH_Mat4fInverse(a, r) == BH_OK); + BH_VERIFY_DELTA(r[0], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], -2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 1.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.x, 0.6000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 1.8000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, -1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.w, -0.4000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[4], 0.6000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 1.8000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[6], -1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], -0.4000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, -0.5000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.w, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[8], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[9], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[10], -0.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[11], 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.x, -0.4000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.y, 1.8000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.z, -2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.w.w, 0.6000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[12], -0.4000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[13], 1.8000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[14], -2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[15], 0.6000f, ACCEPTABLE_DELTA); - BH_VERIFY(bh_matrix4f_inverse(&b, &r) != BH_OK); + BH_VERIFY(BH_Mat4fInverse(b, r) != BH_OK); return 0; } -static int check_matrix3f(void) +static int checkMat3f(void) { - bh_matrix3f_t a, b, r; + float a[9], b[9], r[9]; float value; - a.x.x = 5.0f; a.y.x = 1.0f; a.z.x = 2.0f; - a.x.y = 3.0f; a.y.y = 0.0f; a.z.y = 0.0f; - a.x.z = 1.0f; a.y.z = 3.0f; a.z.z = 4.0f; - - b.x.x = 2.0f; b.y.x = 1.0f; b.z.x = 2.0f; - b.x.y = 3.0f; b.y.y = 1.0f; b.z.y = 0.0f; - b.x.z = 4.0f; b.y.z = 2.0f; b.z.z = 4.0f; - - bh_matrix3f_identity(&r); - BH_VERIFY(r.x.x == 1.0f); - BH_VERIFY(r.x.y == 0.0f); - BH_VERIFY(r.x.z == 0.0f); - - BH_VERIFY(r.y.x == 0.0f); - BH_VERIFY(r.y.y == 1.0f); - BH_VERIFY(r.y.z == 0.0f); - - BH_VERIFY(r.z.x == 0.0f); - BH_VERIFY(r.z.y == 0.0f); - BH_VERIFY(r.z.z == 1.0f); - - bh_matrix3f_add(&a, &b, &r); - BH_VERIFY_DELTA(r.x.x, 7.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 5.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 5.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 4.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 8.0000f, ACCEPTABLE_DELTA); - - bh_matrix3f_sub(&a, &b, &r); - BH_VERIFY_DELTA(r.x.x, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, -3.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, -1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 1.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 0.0000f, ACCEPTABLE_DELTA); - - bh_matrix3f_mul(&a, &b, &r); - BH_VERIFY_DELTA(r.x.x, 21.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 27.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 10.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 12.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 18.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 6.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 18.0000f, ACCEPTABLE_DELTA); - - bh_matrix3f_scale(&a, 10, &r); - BH_VERIFY_DELTA(r.x.x, 50.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 30.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 10.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 10.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 30.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 20.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 40.0000f, ACCEPTABLE_DELTA); - - bh_matrix3f_transpose(&a, &r); - BH_VERIFY_DELTA(r.x.x, 5.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 2.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.y.x, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, 0.0000f, ACCEPTABLE_DELTA); - - BH_VERIFY_DELTA(r.z.x, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, 4.0000f, ACCEPTABLE_DELTA); - - value = bh_matrix3f_trace(&a); + a[0] = 5.0f; a[3] = 1.0f; a[6] = 2.0f; + a[1] = 3.0f; a[4] = 0.0f; a[7] = 0.0f; + a[2] = 1.0f; a[5] = 3.0f; a[8] = 4.0f; + + b[0] = 2.0f; b[3] = 1.0f; b[6] = 2.0f; + b[1] = 3.0f; b[4] = 1.0f; b[7] = 0.0f; + b[2] = 4.0f; b[5] = 2.0f; b[8] = 4.0f; + + BH_Mat3fIdentity(r); + BH_VERIFY(r[0] == 1.0f); + BH_VERIFY(r[1] == 0.0f); + BH_VERIFY(r[2] == 0.0f); + + BH_VERIFY(r[3] == 0.0f); + BH_VERIFY(r[4] == 1.0f); + BH_VERIFY(r[5] == 0.0f); + + BH_VERIFY(r[6] == 0.0f); + BH_VERIFY(r[7] == 0.0f); + BH_VERIFY(r[8] == 1.0f); + + BH_Mat3fAdd(a, b, r); + BH_VERIFY_DELTA(r[0], 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 5.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[3], 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[4], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 5.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[6], 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[8], 8.0000f, ACCEPTABLE_DELTA); + + BH_Mat3fSub(a, b, r); + BH_VERIFY_DELTA(r[0], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], -3.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[3], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[4], -1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 1.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[6], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[8], 0.0000f, ACCEPTABLE_DELTA); + + BH_Mat3fMul(a, b, r); + BH_VERIFY_DELTA(r[0], 21.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 27.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[3], 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[4], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 12.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[6], 18.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[8], 18.0000f, ACCEPTABLE_DELTA); + + BH_Mat3fScale(a, 10, r); + BH_VERIFY_DELTA(r[0], 50.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 30.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 10.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[3], 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[4], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 30.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[6], 20.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[8], 40.0000f, ACCEPTABLE_DELTA); + + BH_Mat3fTranspose(a, r); + BH_VERIFY_DELTA(r[0], 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 2.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[3], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[4], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r[6], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[8], 4.0000f, ACCEPTABLE_DELTA); + + value = BH_Mat3fTrace(a); BH_VERIFY_DELTA(value, 9.0000f, ACCEPTABLE_DELTA); - value = bh_matrix3f_determinant(&a); + value = BH_Mat3fDet(a); BH_VERIFY_DELTA(value, 6.0000f, ACCEPTABLE_DELTA); - value = bh_matrix3f_determinant(&b); + value = BH_Mat3fDet(b); BH_VERIFY_DELTA(value, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY(bh_matrix3f_inverse(&a, &r) == BH_OK); - BH_VERIFY_DELTA(r.x.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.y, -2.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.x.z, 1.5000f, ACCEPTABLE_DELTA); + BH_VERIFY(BH_Mat3fInverse(a, r) == BH_OK); + BH_VERIFY_DELTA(r[0], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[1], -2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[2], 1.5000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.x, 0.3333f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.y, 3.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.y.z, -2.3333f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[3], 0.3333f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[4], 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[5], -2.3333f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.x, 0.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.y, 1.0000f, ACCEPTABLE_DELTA); - BH_VERIFY_DELTA(r.z.z, -0.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[6], 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[7], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r[8], -0.5000f, ACCEPTABLE_DELTA); - BH_VERIFY(bh_matrix3f_inverse(&b, &r) != BH_OK); + BH_VERIFY(BH_Mat3fInverse(b, r) != BH_OK); return 0; } @@ -811,15 +713,15 @@ int main(int argc, char **argv) (void)argc; (void)argv; - bh_unit_add("point4f", check_point4f); - bh_unit_add("point3f", check_point3f); - bh_unit_add("point2f", check_point2f); - bh_unit_add("point4i", check_point4i); - bh_unit_add("point3i", check_point3i); - bh_unit_add("point2i", check_point2i); - bh_unit_add("quat", check_quat); - bh_unit_add("matrix4f", check_matrix4f); - bh_unit_add("matrix3f", check_matrix3f); - - return bh_unit_run(); + BH_UnitAdd("Vec4f", checkVec4f); + BH_UnitAdd("Vec3f", checkVec3f); + BH_UnitAdd("Vec2f", checkVec2f); + BH_UnitAdd("Vec4i", checkVec4i); + BH_UnitAdd("Vec3i", checkVec3i); + BH_UnitAdd("Vec2i", checkVec2i); + BH_UnitAdd("Quat", checkQuat); + BH_UnitAdd("Mat4f", checkMat4f); + BH_UnitAdd("Mat3f", checkMat3f); + + return BH_UnitRun(); } diff --git a/test/src/testqueue.c b/test/src/testqueue.c index 930774a..edeb75c 100644 --- a/test/src/testqueue.c +++ b/test/src/testqueue.c @@ -2,156 +2,156 @@ #include -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(bh_queue_size(queue) == 0); - BH_VERIFY(bh_queue_capacity(queue) == 0); - BH_VERIFY(bh_queue_empty(queue) != 0); + BH_VERIFY(BH_QueueSize(queue) == 0); + BH_VERIFY(BH_QueueCapacity(queue) == 0); + BH_VERIFY(BH_QueueEmpty(queue) != 0); - bh_queue_free(queue); + BH_QueueFree(queue); return 0; } -static int grow_shrink(void) +static int GrowShrink(void) { - bh_queue_t *queue; + BH_Queue *queue; void *value; - queue = bh_queue_new(); + queue = BH_QueueNew(); BH_VERIFY(queue != NULL); /* Reserve 1024 elements and insert item into queue */ - BH_VERIFY(bh_queue_reserve(queue, 1024) == BH_OK); - BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(1337)) == BH_OK); - BH_VERIFY(bh_queue_capacity(queue) >= 1024); - BH_VERIFY(bh_queue_size(queue) == 1); - BH_VERIFY(bh_queue_empty(queue) == 0); + BH_VERIFY(BH_QueueReserve(queue, 1024) == BH_OK); + BH_VERIFY(BH_QueueInsert(queue, BH_INT2PTR(1337)) == BH_OK); + BH_VERIFY(BH_QueueCapacity(queue) >= 1024); + BH_VERIFY(BH_QueueSize(queue) == 1); + BH_VERIFY(BH_QueueEmpty(queue) == 0); /* 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); /* Grow queue */ - BH_VERIFY(bh_queue_reserve(queue, 8192) == BH_OK); - BH_VERIFY(bh_queue_capacity(queue) >= 8192); - BH_VERIFY(bh_queue_size(queue) == 1); - BH_VERIFY(bh_queue_empty(queue) == 0); + BH_VERIFY(BH_QueueReserve(queue, 8192) == BH_OK); + BH_VERIFY(BH_QueueCapacity(queue) >= 8192); + BH_VERIFY(BH_QueueSize(queue) == 1); + BH_VERIFY(BH_QueueEmpty(queue) == 0); /* 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); /* Shrink the queue */ - BH_VERIFY(bh_queue_reserve(queue, 0) == BH_OK); - BH_VERIFY(bh_queue_capacity(queue) >= 1); - BH_VERIFY(bh_queue_capacity(queue) < 8192); - BH_VERIFY(bh_queue_size(queue) == 1); - BH_VERIFY(bh_queue_empty(queue) == 0); + BH_VERIFY(BH_QueueReserve(queue, 0) == BH_OK); + BH_VERIFY(BH_QueueCapacity(queue) >= 1); + BH_VERIFY(BH_QueueCapacity(queue) < 8192); + BH_VERIFY(BH_QueueSize(queue) == 1); + BH_VERIFY(BH_QueueEmpty(queue) == 0); /* 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); /* Shrink to 0 (deallocate) */ - bh_queue_clear(queue); - BH_VERIFY(bh_queue_size(queue) == 0); - BH_VERIFY(bh_queue_empty(queue) != 0); - BH_VERIFY(bh_queue_capacity(queue) >= 1); + BH_QueueClear(queue); + BH_VERIFY(BH_QueueSize(queue) == 0); + BH_VERIFY(BH_QueueEmpty(queue) != 0); + BH_VERIFY(BH_QueueCapacity(queue) >= 1); - BH_VERIFY(bh_queue_reserve(queue, 0) == BH_OK); - BH_VERIFY(bh_queue_size(queue) == 0); - BH_VERIFY(bh_queue_empty(queue) != 0); - BH_VERIFY(bh_queue_capacity(queue) == 0); + BH_VERIFY(BH_QueueReserve(queue, 0) == BH_OK); + BH_VERIFY(BH_QueueSize(queue) == 0); + BH_VERIFY(BH_QueueEmpty(queue) != 0); + BH_VERIFY(BH_QueueCapacity(queue) == 0); - bh_queue_free(queue); + BH_QueueFree(queue); return 0; } -static int insert_remove(void) +static int InsertRemove(void) { - bh_queue_t *queue; + BH_Queue *queue; size_t i, added, removed; void *iter; - queue = bh_queue_new(); + queue = BH_QueueNew(); BH_VERIFY(queue != NULL); added = 0; for (i = 0; i < 256; i++) { added += i * 2; - BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(i * 2)) == BH_OK); + BH_VERIFY(BH_QueueInsert(queue, BH_INT2PTR(i * 2)) == BH_OK); } removed = 0; - iter = bh_queue_iter_next(queue, NULL); + iter = BH_QueueIterNext(queue, NULL); while (iter) { void *value; - BH_VERIFY(bh_queue_front(queue, &value) == BH_OK); + BH_VERIFY(BH_QueueFront(queue, &value) == BH_OK); removed += BH_PTR2INT(value); - bh_queue_remove(queue); - iter = bh_queue_iter_next(queue, NULL); + BH_QueueRemove(queue); + iter = BH_QueueIterNext(queue, NULL); } BH_VERIFY(added == removed); - BH_VERIFY(bh_queue_empty(queue) != 0); - BH_VERIFY(bh_queue_size(queue) == 0); + BH_VERIFY(BH_QueueEmpty(queue) != 0); + BH_VERIFY(BH_QueueSize(queue) == 0); - bh_queue_free(queue); + BH_QueueFree(queue); return 0; } -static int rollover(void) +static int Rollover(void) { - bh_queue_t *queue; + BH_Queue *queue; size_t i, j, capacity; - queue = bh_queue_new(); + queue = BH_QueueNew(); BH_VERIFY(queue != NULL); - BH_VERIFY(bh_queue_reserve(queue, 128) == 0); - capacity = bh_queue_capacity(queue); + BH_VERIFY(BH_QueueReserve(queue, 128) == 0); + capacity = BH_QueueCapacity(queue); for (i = 0; i < 128; i++) { for (j = 0; j < 3; j++) - bh_queue_remove(queue); + BH_QueueRemove(queue); - for (j = 0; j < 4 && bh_queue_size(queue) < 128; j++) - BH_VERIFY(bh_queue_insert(queue, BH_INT2PTR(i * 4 + j)) == 0); + for (j = 0; j < 4 && BH_QueueSize(queue) < 128; j++) + BH_VERIFY(BH_QueueInsert(queue, BH_INT2PTR(i * 4 + j)) == 0); } - BH_VERIFY(bh_queue_size(queue) == 128); - BH_VERIFY(bh_queue_capacity(queue) == capacity); + BH_VERIFY(BH_QueueSize(queue) == 128); + BH_VERIFY(BH_QueueCapacity(queue) == capacity); - bh_queue_free(queue); + BH_QueueFree(queue); 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(bh_queue_insert(queue, BH_INT2PTR(1337)) == 0); - BH_VERIFY(bh_queue_size(queue) == 1); - BH_VERIFY(bh_queue_empty(queue) == 0); - BH_VERIFY(bh_queue_capacity(queue) >= 1); + BH_VERIFY(BH_QueueInsert(queue, BH_INT2PTR(1337)) == 0); + BH_VERIFY(BH_QueueSize(queue) == 1); + BH_VERIFY(BH_QueueEmpty(queue) == 0); + BH_VERIFY(BH_QueueCapacity(queue) >= 1); - bh_queue_free(queue); + BH_QueueFree(queue); return 0; } @@ -161,11 +161,11 @@ int main(int argc, char **argv) (void)argc; (void)argv; - bh_unit_add("new_free", new_free); - bh_unit_add("grow_shrink", grow_shrink); - bh_unit_add("insert_remove", insert_remove); - bh_unit_add("rollover", rollover); - bh_unit_add("fields", fields); + BH_UnitAdd("NewFree", NewFree); + BH_UnitAdd("GrowShrink", GrowShrink); + BH_UnitAdd("InsertRemove", InsertRemove); + BH_UnitAdd("Rollover", Rollover); + BH_UnitAdd("Fields", Fields); - return bh_unit_run(); + return BH_UnitRun(); } diff --git a/unit/include/bh/unit.h b/unit/include/bh/unit.h index 6667ca4..3b3b2b6 100755 --- a/unit/include/bh/unit.h +++ b/unit/include/bh/unit.h @@ -4,7 +4,7 @@ #include #include -typedef int (*bh_unit_cb_t)(void); +typedef int (*BH_UnitCallback)(void); #define BH_VERIFY(e) \ do { \ @@ -34,7 +34,23 @@ typedef int (*bh_unit_cb_t)(void); } 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 */ diff --git a/unit/src/unit.c b/unit/src/unit.c index a5f143c..6063a4f 100755 --- a/unit/src/unit.c +++ b/unit/src/unit.c @@ -1,27 +1,31 @@ #include #include -typedef struct bh_unit_s +typedef struct BH_Unit { - struct bh_unit_s *next; + struct BH_Unit *next; const char *name; - bh_unit_cb_t func; -} bh_unit_t; + BH_UnitCallback cb; +} 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)); if (!unit) return; - unit->name = name; - unit->func = func; unit->next = NULL; + unit->name = name; + unit->cb = cb; + /* Append unit test entry */ current = root; while (current && current->next) current = current->next; @@ -32,16 +36,17 @@ void bh_unit_add(const char *name, bh_unit_cb_t func) root = unit; } -int bh_unit_run(void) + +int BH_UnitRun(void) { - bh_unit_t *current; + BH_Unit *current; printf("Running tests...\n"); current = root; while (current) { printf("%s\n", current->name); - if (current->func()) + if (current->cb()) { printf("\tFAIL\n"); return -1; -- cgit v1.2.3