1
0

Compare commits

..

No commits in common. "7568c2138bed14bea1641bd80d2adeb16611622e" and "7ad10ba73418d71b1b46868b026983b615fbd42f" have entirely different histories.

4 changed files with 183 additions and 376 deletions

View File

@ -1,7 +1,7 @@
/* /*
pl_memory.h pl_memory
* no dependencies * no dependencies
* simple memory allocators * simple
Do this: Do this:
#define PL_MEMORY_IMPLEMENTATION #define PL_MEMORY_IMPLEMENTATION
@ -19,9 +19,9 @@
* override assert by defining PL_ASSERT(x) * override assert by defining PL_ASSERT(x)
*/ */
// library version (format XYYZZ) // library version
#define PL_MEMORY_VERSION "1.0.0" #define PL_MEMORY_VERSION "0.6.0"
#define PL_MEMORY_VERSION_NUM 10000 #define PL_MEMORY_VERSION_NUM 00600
/* /*
Index of this file: Index of this file:
@ -59,10 +59,10 @@ Index of this file:
// [SECTION] forward declarations & basic types // [SECTION] forward declarations & basic types
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// basic types typedef struct _plTempAllocator plTempAllocator;
typedef struct _plTempAllocator plTempAllocator; typedef struct _plStackAllocator plStackAllocator;
typedef struct _plStackAllocator plStackAllocator; typedef struct _plPoolAllocator plPoolAllocator;
typedef struct _plPoolAllocator plPoolAllocator; typedef struct _plPoolAllocatorNode plPoolAllocatorNode;
typedef size_t plStackAllocatorMarker; typedef size_t plStackAllocatorMarker;
@ -105,24 +105,14 @@ void pl_stack_allocator_free_bottom_to_marker(plStackAllocator
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~pool allocator~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~pool allocator~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Notes void pl_pool_allocator_init (plPoolAllocator*, size_t szItemCount, size_t szItemSize, size_t szItemAlignment, size_t* pszBufferSize, void*);
// - setting pBuffer to NULL, will set pszBufferSize to required buffer size void* pl_pool_allocator_alloc(plPoolAllocator*);
// so you can allocate a properly sized buffer for the szItemCount (then call function again) void pl_pool_allocator_free (plPoolAllocator*, void* pItem);
// - to use a stack allocated buffer, first call the function with szItemCount = 0 & pszBufferSize
// set to size of the buffer; the function will return the number of items that can be support;
// call function again with this number
size_t pl_pool_allocator_init (plPoolAllocator*, size_t szItemCount, size_t szItemSize, size_t szItemAlignment, size_t* pszBufferSize, void* pBuffer);
void* pl_pool_allocator_alloc(plPoolAllocator*);
void pl_pool_allocator_free (plPoolAllocator*, void* pItem);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] structs // [SECTION] structs
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// the details of the following structures don't matter to you, but they must
// be visible so you can handle the memory allocations for them
typedef struct _plTempAllocator typedef struct _plTempAllocator
{ {
size_t szSize; size_t szSize;
@ -132,8 +122,6 @@ typedef struct _plTempAllocator
char** ppcMemoryBlocks; char** ppcMemoryBlocks;
size_t szMemoryBlockCount; size_t szMemoryBlockCount;
size_t szMemoryBlockCapacity; size_t szMemoryBlockCapacity;
size_t szCurrentBlockSizes;
size_t szNextBlockSizes;
} plTempAllocator; } plTempAllocator;
typedef struct _plStackAllocator typedef struct _plStackAllocator
@ -144,7 +132,6 @@ typedef struct _plStackAllocator
size_t szTopOffset; size_t szTopOffset;
} plStackAllocator; } plStackAllocator;
typedef struct _plPoolAllocatorNode plPoolAllocatorNode;
typedef struct _plPoolAllocatorNode typedef struct _plPoolAllocatorNode
{ {
plPoolAllocatorNode* ptNextNode; plPoolAllocatorNode* ptNextNode;
@ -172,6 +159,7 @@ Index of this file:
// [SECTION] defines // [SECTION] defines
// [SECTION] internal api // [SECTION] internal api
// [SECTION] public api implementation // [SECTION] public api implementation
// [SECTION] internal api implementation
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -190,23 +178,28 @@ Index of this file:
#ifndef PL_MEMORY_ALLOC #ifndef PL_MEMORY_ALLOC
#include <stdlib.h> #include <stdlib.h>
#define PL_MEMORY_ALLOC(x) malloc(x) #define PL_MEMORY_ALLOC(x) malloc(x)
#define PL_MEMORY_FREE(x) free(x) #define PL_MEMORY_FREE(x) free(x)
#endif #endif
#ifndef PL_ASSERT #ifndef PL_ASSERT
#include <assert.h> #include <assert.h>
#define PL_ASSERT(x) assert((x)) #define PL_ASSERT(x) assert((x))
#endif
#ifndef PL_MEMORY_TEMP_STACK_BLOCK_SIZE
#define PL_MEMORY_TEMP_BLOCK_SIZE 4194304
#endif #endif
#define PL__ALIGN_UP(num, align) (((num) + ((align)-1)) & ~((align)-1)) #define PL__ALIGN_UP(num, align) (((num) + ((align)-1)) & ~((align)-1))
#ifndef pl_vnsprintf #ifndef pl_vnsprintf
#include <stdio.h> #include <stdio.h>
#define pl_vnsprintf vsnprintf #define pl_vnsprintf vnsprintf
#endif #endif
#include <stdarg.h> // varargs #include <stdarg.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] internal api // [SECTION] internal api
@ -245,6 +238,7 @@ pl__align_forward_size(size_t szPtr, size_t szAlign)
return p; return p;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] public api implementation // [SECTION] public api implementation
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -254,9 +248,6 @@ pl_aligned_alloc(size_t szAlignment, size_t szSize)
{ {
void* pBuffer = NULL; void* pBuffer = NULL;
if(szAlignment == 0)
szAlignment = pl__get_next_power_of_2(szSize);
// ensure power of 2 // ensure power of 2
PL_ASSERT((szAlignment & (szAlignment -1)) == 0 && "alignment must be a power of 2"); PL_ASSERT((szAlignment & (szAlignment -1)) == 0 && "alignment must be a power of 2");
@ -303,8 +294,6 @@ pl_temp_allocator_alloc(plTempAllocator* ptAllocator, size_t szSize)
ptAllocator->szSize = PL_MEMORY_TEMP_STACK_SIZE; ptAllocator->szSize = PL_MEMORY_TEMP_STACK_SIZE;
ptAllocator->pcBuffer = ptAllocator->acStackBuffer; ptAllocator->pcBuffer = ptAllocator->acStackBuffer;
ptAllocator->szOffset = 0; ptAllocator->szOffset = 0;
ptAllocator->szCurrentBlockSizes = PL_MEMORY_TEMP_STACK_SIZE * 2;
ptAllocator->szNextBlockSizes = PL_MEMORY_TEMP_STACK_SIZE * 2;
memset(ptAllocator->acStackBuffer, 0, PL_MEMORY_TEMP_STACK_SIZE); memset(ptAllocator->acStackBuffer, 0, PL_MEMORY_TEMP_STACK_SIZE);
} }
@ -313,6 +302,7 @@ pl_temp_allocator_alloc(plTempAllocator* ptAllocator, size_t szSize)
// not enough room is available // not enough room is available
if(szSize > ptAllocator->szSize - ptAllocator->szOffset) if(szSize > ptAllocator->szSize - ptAllocator->szOffset)
{ {
PL_ASSERT(szSize < PL_MEMORY_TEMP_BLOCK_SIZE);
if(ptAllocator->szMemoryBlockCapacity == 0) // first overflow if(ptAllocator->szMemoryBlockCapacity == 0) // first overflow
{ {
// allocate block array // allocate block array
@ -320,64 +310,30 @@ pl_temp_allocator_alloc(plTempAllocator* ptAllocator, size_t szSize)
ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * ptAllocator->szMemoryBlockCapacity); ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * ptAllocator->szMemoryBlockCapacity);
memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * ptAllocator->szMemoryBlockCapacity)); memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * ptAllocator->szMemoryBlockCapacity));
size_t szNewBlockSize = ptAllocator->szCurrentBlockSizes;
if(szSize > szNewBlockSize)
{
ptAllocator->szNextBlockSizes = szSize;
szNewBlockSize = szSize;
}
// allocate first block // allocate first block
ptAllocator->ppcMemoryBlocks[0] = (char*)PL_MEMORY_ALLOC(szNewBlockSize); ptAllocator->ppcMemoryBlocks[0] = (char*)PL_MEMORY_ALLOC(PL_MEMORY_TEMP_BLOCK_SIZE);
ptAllocator->szSize = szNewBlockSize; ptAllocator->szSize = PL_MEMORY_TEMP_BLOCK_SIZE;
ptAllocator->szOffset = 0; ptAllocator->szOffset = 0;
ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[0]; ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[0];
} }
else if(ptAllocator->szMemoryBlockCount == ptAllocator->szMemoryBlockCapacity) // grow memory block storage else if(ptAllocator->szMemoryBlockCount == ptAllocator->szMemoryBlockCapacity) // grow memory block storage
{ {
size_t szNewBlockSize = ptAllocator->szCurrentBlockSizes;
if(szSize > szNewBlockSize)
{
ptAllocator->szNextBlockSizes = szSize;
szNewBlockSize = szSize;
}
char** ppcOldBlocks = ptAllocator->ppcMemoryBlocks; char** ppcOldBlocks = ptAllocator->ppcMemoryBlocks;
ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1)); ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1));
memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1))); memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1)));
memcpy(ptAllocator->ppcMemoryBlocks, ppcOldBlocks, sizeof(char*) * ptAllocator->szMemoryBlockCapacity); memcpy(ptAllocator->ppcMemoryBlocks, ppcOldBlocks, sizeof(char*) * ptAllocator->szMemoryBlockCapacity);
ptAllocator->szMemoryBlockCapacity++; ptAllocator->szMemoryBlockCapacity++;
ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount] = (char*)PL_MEMORY_ALLOC(szNewBlockSize); ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount] = (char*)PL_MEMORY_ALLOC(PL_MEMORY_TEMP_BLOCK_SIZE);
ptAllocator->szSize = szNewBlockSize; ptAllocator->szSize = PL_MEMORY_TEMP_BLOCK_SIZE;
ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount]; ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount];
ptAllocator->szOffset = 0; ptAllocator->szOffset = 0;
} }
else if(szSize <= ptAllocator->szCurrentBlockSizes) // block available & small enough else // block is available
{ {
ptAllocator->szSize = ptAllocator->szCurrentBlockSizes; ptAllocator->szSize = PL_MEMORY_TEMP_BLOCK_SIZE;
ptAllocator->szOffset = 0; ptAllocator->szOffset = 0;
ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount]; ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount];
} }
else // block available but too small
{
size_t szNewBlockSize = ptAllocator->szCurrentBlockSizes;
if(szSize > szNewBlockSize)
{
ptAllocator->szNextBlockSizes = szSize;
szNewBlockSize = szSize;
}
char** ppcOldBlocks = ptAllocator->ppcMemoryBlocks;
ptAllocator->ppcMemoryBlocks = (char**)PL_MEMORY_ALLOC(sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1));
memset(ptAllocator->ppcMemoryBlocks, 0, (sizeof(char*) * (ptAllocator->szMemoryBlockCapacity + 1)));
memcpy(ptAllocator->ppcMemoryBlocks, ppcOldBlocks, sizeof(char*) * ptAllocator->szMemoryBlockCapacity);
ptAllocator->szMemoryBlockCapacity++;
ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount] = (char*)PL_MEMORY_ALLOC(szNewBlockSize);
ptAllocator->szSize = szNewBlockSize;
ptAllocator->pcBuffer = ptAllocator->ppcMemoryBlocks[ptAllocator->szMemoryBlockCount];
ptAllocator->szOffset = 0;
}
ptAllocator->szMemoryBlockCount++; ptAllocator->szMemoryBlockCount++;
} }
@ -394,17 +350,6 @@ pl_temp_allocator_reset(plTempAllocator* ptAllocator)
ptAllocator->szOffset = 0; ptAllocator->szOffset = 0;
ptAllocator->szMemoryBlockCount = 0; ptAllocator->szMemoryBlockCount = 0;
ptAllocator->pcBuffer = ptAllocator->acStackBuffer; ptAllocator->pcBuffer = ptAllocator->acStackBuffer;
if(ptAllocator->szCurrentBlockSizes != ptAllocator->szNextBlockSizes)
{
for(size_t i = 0; i < ptAllocator->szMemoryBlockCapacity; i++)
{
PL_MEMORY_FREE(ptAllocator->ppcMemoryBlocks[i]);
ptAllocator->ppcMemoryBlocks[i] = (char*)PL_MEMORY_ALLOC(ptAllocator->szNextBlockSizes);
memset(ptAllocator->ppcMemoryBlocks[i], 0, ptAllocator->szNextBlockSizes);
}
ptAllocator->szCurrentBlockSizes = ptAllocator->szNextBlockSizes;
}
} }
void void
@ -473,8 +418,7 @@ pl_stack_allocator_alloc(plStackAllocator* ptAllocator, size_t szSize)
{ {
size_t szOffset = ptAllocator->szBottomOffset + szSize; size_t szOffset = ptAllocator->szBottomOffset + szSize;
if(szOffset >= ptAllocator->szTopOffset) PL_ASSERT(szOffset < ptAllocator->szTopOffset && "stack allocator full");
return NULL;
// update offset // update offset
void* pBuffer = ptAllocator->pucBuffer + ptAllocator->szBottomOffset; void* pBuffer = ptAllocator->pucBuffer + ptAllocator->szBottomOffset;
@ -493,8 +437,7 @@ pl_stack_allocator_aligned_alloc(plStackAllocator* ptAllocator, size_t szSize, s
uintptr_t pOffset = pl__align_forward_uintptr(pCurrentPointer, szAlignment); uintptr_t pOffset = pl__align_forward_uintptr(pCurrentPointer, szAlignment);
pOffset -= (uintptr_t)ptAllocator->pucBuffer; pOffset -= (uintptr_t)ptAllocator->pucBuffer;
if(pOffset + szSize > ptAllocator->szTopOffset) PL_ASSERT(pOffset + szSize <= ptAllocator->szTopOffset && "linear allocator full");
return NULL;
// check if allocator has enough space left // check if allocator has enough space left
if(pOffset + szSize <= ptAllocator->szSize) if(pOffset + szSize <= ptAllocator->szSize)
@ -524,8 +467,7 @@ pl_stack_allocator_aligned_alloc_top(plStackAllocator* ptAllocator, size_t szSiz
uintptr_t pOffset = pl__align_forward_uintptr(pCurrentPointer, szAlignment); uintptr_t pOffset = pl__align_forward_uintptr(pCurrentPointer, szAlignment);
pOffset -= (uintptr_t)ptAllocator->pucBuffer; pOffset -= (uintptr_t)ptAllocator->pucBuffer;
if(pOffset + szSize > ptAllocator->szTopOffset) PL_ASSERT(pOffset + szSize <= ptAllocator->szTopOffset && "linear allocator full");
return NULL;
// check if allocator has enough space left // check if allocator has enough space left
if(pOffset + szSize <= ptAllocator->szSize) if(pOffset + szSize <= ptAllocator->szSize)
@ -550,8 +492,7 @@ pl_stack_allocator_alloc_top(plStackAllocator* ptAllocator, size_t szSize)
{ {
size_t szOffset = ptAllocator->szTopOffset - szSize; size_t szOffset = ptAllocator->szTopOffset - szSize;
if(szOffset < ptAllocator->szBottomOffset || szOffset > ptAllocator->szTopOffset) PL_ASSERT(szOffset > ptAllocator->szBottomOffset && szOffset < ptAllocator->szTopOffset && "stack allocator full");
return NULL;
// update offset // update offset
void* pBuffer = ptAllocator->pucBuffer + szOffset; void* pBuffer = ptAllocator->pucBuffer + szOffset;
@ -621,36 +562,24 @@ pl_stack_allocator_reset(plStackAllocator* ptAllocator)
#endif #endif
} }
size_t void
pl_pool_allocator_init(plPoolAllocator* ptAllocator, size_t szItemCount, size_t szItemSize, size_t szItemAlignment, size_t* pszBufferSize, void* pBuffer) pl_pool_allocator_init(plPoolAllocator* ptAllocator, size_t szItemCount, size_t szItemSize, size_t szItemAlignment, size_t* pszBufferSize, void* pBuffer)
{ {
PL_ASSERT(ptAllocator); PL_ASSERT(ptAllocator);
PL_ASSERT(szItemCount > 0);
PL_ASSERT(szItemSize > 0); PL_ASSERT(szItemSize > 0);
PL_ASSERT(pszBufferSize); PL_ASSERT(pszBufferSize);
// gotta have room for node in unused blocks
if(szItemSize < sizeof(plPoolAllocatorNode))
{
szItemSize = sizeof(plPoolAllocatorNode);
}
// let us calculate alignment
if(szItemAlignment == 0) if(szItemAlignment == 0)
szItemAlignment = pl__get_next_power_of_2(szItemSize);
// let us calculate number of items
if(szItemCount == 0 && *pszBufferSize > 0)
{ {
size_t szAlignedItemSize = pl__align_forward_size(szItemSize, szItemAlignment); szItemAlignment = pl__get_next_power_of_2(szItemSize);
szItemCount = (*pszBufferSize - szItemAlignment) / (szAlignedItemSize);
return szItemCount;
} }
if(pBuffer == NULL) if(pBuffer == NULL)
{ {
size_t szAlignedItemSize = pl__align_forward_size(szItemSize, szItemAlignment); size_t szAlignedItemSize = pl__align_forward_size(szItemSize, szItemAlignment);
*pszBufferSize = szAlignedItemSize * szItemCount + szItemAlignment; *pszBufferSize = szAlignedItemSize * szItemCount + szItemAlignment;
return szItemCount; return;
} }
ptAllocator->szFreeItems = szItemCount; ptAllocator->szFreeItems = szItemCount;
@ -664,6 +593,7 @@ pl_pool_allocator_init(plPoolAllocator* ptAllocator, size_t szItemCount, size_t
uintptr_t pStart = pl__align_forward_uintptr(pInitialStart, (uintptr_t)szItemAlignment); uintptr_t pStart = pl__align_forward_uintptr(pInitialStart, (uintptr_t)szItemAlignment);
ptAllocator->szUsableSize -= (size_t)(pStart - pInitialStart); ptAllocator->szUsableSize -= (size_t)(pStart - pInitialStart);
PL_ASSERT(ptAllocator->szItemSize >= sizeof(plPoolAllocatorNode) && "pool allocator item size too small");
PL_ASSERT(ptAllocator->szUsableSize >= ptAllocator->szItemSize * szItemCount && "pool allocator buffer size too small"); PL_ASSERT(ptAllocator->szUsableSize >= ptAllocator->szItemSize * szItemCount && "pool allocator buffer size too small");
unsigned char* pUsableBuffer = (unsigned char*)pStart; unsigned char* pUsableBuffer = (unsigned char*)pStart;
@ -674,7 +604,6 @@ pl_pool_allocator_init(plPoolAllocator* ptAllocator, size_t szItemCount, size_t
pNode0->ptNextNode = pNode1; pNode0->ptNextNode = pNode1;
} }
ptAllocator->pFreeList = (plPoolAllocatorNode*)pUsableBuffer; ptAllocator->pFreeList = (plPoolAllocatorNode*)pUsableBuffer;
return szItemCount;
} }
void* void*
@ -699,4 +628,4 @@ pl_pool_allocator_free(plPoolAllocator* ptAllocator, void* pItem)
ptAllocator->pFreeList->ptNextNode = pOldFreeNode; ptAllocator->pFreeList->ptNextNode = pOldFreeNode;
} }
#endif // PL_MEMORY_IMPLEMENTATION #endif

View File

@ -1,16 +1,15 @@
/* /*
pl_stl.h pl_stl.h
* no dependencies
* simple asci & binary stl parser
*/ */
// library version (format XYYZZ) // library version
#define PL_STL_VERSION "1.0.0" #define PL_STL_VERSION "0.2.0"
#define PL_STL_VERSION_NUM 10000 #define PL_STL_VERSION_NUM 00200
/* /*
Index of this file: Index of this file:
// [SECTION] header mess // [SECTION] header mess
// [SECTION] includes
// [SECTION] forward declarations & basic types // [SECTION] forward declarations & basic types
// [SECTION] public api // [SECTION] public api
// [SECTION] structs // [SECTION] structs
@ -24,6 +23,12 @@ Index of this file:
#ifndef PL_STL_H #ifndef PL_STL_H
#define PL_STL_H #define PL_STL_H
//-----------------------------------------------------------------------------
// [SECTION] includes
//-----------------------------------------------------------------------------
#include <stdbool.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] forward declarations & basic types // [SECTION] forward declarations & basic types
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -45,7 +50,7 @@ typedef struct _plStlInfo
size_t szPositionStreamSize; size_t szPositionStreamSize;
size_t szNormalStreamSize; size_t szNormalStreamSize;
size_t szIndexBufferSize; size_t szIndexBufferSize;
int iPreloaded; bool bPreloaded;
} plStlInfo; } plStlInfo;
#endif // PL_STL_H #endif // PL_STL_H
@ -99,11 +104,11 @@ pl_load_stl(const char* pcData, size_t szDataSize, float* afPositionStream, floa
ptInfoOut = &_tInternalInfo; ptInfoOut = &_tInternalInfo;
bool bAsci = strncmp(pcData, "solid", 5) == 0; bool bAsci = strncmp(pcData, "solid", 5) == 0;
size_t szFacetCount = ptInfoOut->iPreloaded ? ptInfoOut->szIndexBufferSize / 3 : 0; size_t szFacetCount = ptInfoOut->bPreloaded ? ptInfoOut->szIndexBufferSize / 3 : 0;
size_t szCurrentCursor = 0; size_t szCurrentCursor = 0;
size_t szVertexCount = ptInfoOut->iPreloaded ? ptInfoOut->szIndexBufferSize : 0; size_t szVertexCount = ptInfoOut->bPreloaded ? ptInfoOut->szIndexBufferSize : 0;
if(!ptInfoOut->iPreloaded) if(!ptInfoOut->bPreloaded)
{ {
// find number of vertices & facets // find number of vertices & facets
@ -126,7 +131,7 @@ pl_load_stl(const char* pcData, size_t szDataSize, float* afPositionStream, floa
szVertexCount = szFacetCount * 3; szVertexCount = szFacetCount * 3;
} }
ptInfoOut->iPreloaded = 1; ptInfoOut->bPreloaded = true;
} }
ptInfoOut->szIndexBufferSize = szFacetCount * 3; ptInfoOut->szIndexBufferSize = szFacetCount * 3;

View File

@ -1,8 +1,5 @@
/* /*
pl_string.h pl_string
* no dependencies
* simple string ops
Do this: Do this:
#define PL_STRING_IMPLEMENTATION #define PL_STRING_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation. before you include this file in *one* C or C++ file to create the implementation.
@ -14,9 +11,9 @@
#include "pl_string.h" #include "pl_string.h"
*/ */
// library version (format XYYZZ) // library version
#define PL_STRING_VERSION "1.0.0" #define PL_STRING_VERSION "0.2.0"
#define PL_STRING_VERSION_NUM 10000 #define PL_STRING_VERSION_NUM 00200
/* /*
Index of this file: Index of this file:
@ -50,10 +47,10 @@ uint32_t pl_str_hash_data(const void* pData, size_t szDataSize, uint32_t uSee
uint32_t pl_str_hash (const char* pcData, size_t szDataSize, uint32_t uSeed); uint32_t pl_str_hash (const char* pcData, size_t szDataSize, uint32_t uSeed);
// file/path string ops // file/path string ops
const char* pl_str_get_file_extension(const char* pcFilePath, char* pcExtensionOut, size_t szOutSize); const char* pl_str_get_file_extension(const char* pcFilePath, char* pcExtensionOut);
const char* pl_str_get_file_name (const char* pcFilePath, char* pcFileOut, size_t szOutSize); const char* pl_str_get_file_name (const char* pcFilePath, char* pcFileOut);
bool pl_str_get_file_name_only(const char* pcFilePath, char* pcFileOut, size_t szOutSize); const char* pl_str_get_file_name_only(const char* pcFilePath, char* pcFileOut);
bool pl_str_get_directory (const char* pcFilePath, char* pcDirectoryOut, size_t szOutSize); void pl_str_get_directory (const char* pcFilePath, char* pcDirectoryOut);
// misc. opts // misc. opts
bool pl_str_concatenate (const char* pcStr0, const char* pcStr1, char* pcStringOut, size_t szDataSize); bool pl_str_concatenate (const char* pcStr0, const char* pcStr1, char* pcStringOut, size_t szDataSize);
@ -168,7 +165,7 @@ pl_str_hash(const char* pcData, size_t szDataSize, uint32_t uSeed)
} }
const char* const char*
pl_str_get_file_extension(const char* pcFilePath, char* pcExtensionOut, size_t szOutSize) pl_str_get_file_extension(const char* pcFilePath, char* pcExtensionOut)
{ {
const char* pcResult = NULL; const char* pcResult = NULL;
const size_t szLen = strlen(pcFilePath); const size_t szLen = strlen(pcFilePath);
@ -197,8 +194,7 @@ pl_str_get_file_extension(const char* pcFilePath, char* pcExtensionOut, size_t s
char c = pcFilePath[szLen - i - 1]; char c = pcFilePath[szLen - i - 1];
if(c == '.') if(c == '.')
{ {
if(pcExtensionOut) if(pcExtensionOut) strcpy(pcExtensionOut, &pcFilePath[szLen - i]);
strncpy(pcExtensionOut, &pcFilePath[szLen - i], szOutSize);
pcResult = &pcFilePath[szLen - i]; pcResult = &pcFilePath[szLen - i];
break; break;
} }
@ -206,15 +202,14 @@ pl_str_get_file_extension(const char* pcFilePath, char* pcExtensionOut, size_t s
} }
else else
{ {
if(pcExtensionOut) if(pcExtensionOut) memset(pcExtensionOut, 0, 1);
memset(pcExtensionOut, 0, szOutSize);
} }
return pcResult; return pcResult;
} }
const char* const char*
pl_str_get_file_name(const char* pcFilePath, char* pcFileOut, size_t szOutSize) pl_str_get_file_name(const char* pcFilePath, char* pcFileOut)
{ {
const char* pcResult = pcFilePath; const char* pcResult = pcFilePath;
const size_t szLen = strlen(pcFilePath); const size_t szLen = strlen(pcFilePath);
@ -238,8 +233,7 @@ pl_str_get_file_name(const char* pcFilePath, char* pcFileOut, size_t szOutSize)
if(uSlashCount == 0) if(uSlashCount == 0)
{ {
if(pcFileOut) if(pcFileOut) strcpy(pcFileOut, &pcFilePath[i + 1]);
strncpy(pcFileOut, &pcFilePath[i + 1], szOutSize);
pcResult = &pcFilePath[i + 1]; pcResult = &pcFilePath[i + 1];
break; break;
} }
@ -247,26 +241,16 @@ pl_str_get_file_name(const char* pcFilePath, char* pcFileOut, size_t szOutSize)
} }
else else
{ {
if(pcFileOut) if(pcFileOut) memcpy(pcFileOut, pcFilePath, szLen + 1);
{
size_t szCopySize = szLen + 1;
if(szCopySize > szOutSize)
szCopySize = szOutSize;
memcpy(pcFileOut, pcFilePath, szCopySize);
}
} }
return pcResult; return pcResult;
} }
bool const char*
pl_str_get_file_name_only(const char* pcFilePath, char* pcFileOut, size_t szOutSize) pl_str_get_file_name_only(const char* pcFilePath, char* pcFileOut)
{ {
PL_ASSERT(pcFileOut && "pl_str_get_file_name_only requires pcFileOut to be valid pointer"); const char* pcResult = pcFilePath;
if(pcFileOut == NULL)
return false;
const size_t szLen = strlen(pcFilePath); const size_t szLen = strlen(pcFilePath);
// check if string includes directory // check if string includes directory
@ -288,24 +272,19 @@ pl_str_get_file_name_only(const char* pcFilePath, char* pcFileOut, size_t szOutS
if(uSlashCount == 0) if(uSlashCount == 0)
{ {
if(pcFileOut) if(pcFileOut) strcpy(pcFileOut, &pcFilePath[i + 1]);
strncpy(pcFileOut, &pcFilePath[i + 1], szOutSize); pcResult = &pcFilePath[i + 1];
break; break;
} }
} }
} }
else else
{ {
if(pcFileOut) if(pcFileOut) memcpy(pcFileOut, pcFilePath, szLen + 1);
{
if(szLen + 1 > szOutSize)
return false;
memcpy(pcFileOut, pcFilePath, szLen + 1);
}
} }
if(szLen > szOutSize) const size_t szOutLen = strlen(pcFileOut);
return false;
bool bPeriodReached = false; bool bPeriodReached = false;
for(size_t i = 0; i < szLen; i++) for(size_t i = 0; i < szLen; i++)
{ {
@ -320,17 +299,15 @@ pl_str_get_file_name_only(const char* pcFilePath, char* pcFileOut, size_t szOutS
pcFileOut[i] = 0; pcFileOut[i] = 0;
} }
} }
return true;
return pcResult;
} }
bool void
pl_str_get_directory(const char* pcFilePath, char* pcDirectoryOut, size_t szOutSize) pl_str_get_directory(const char* pcFilePath, char* pcDirectoryOut)
{ {
size_t szLen = strlen(pcFilePath); size_t szLen = strlen(pcFilePath);
strncpy(pcDirectoryOut, pcFilePath, szOutSize); strcpy(pcDirectoryOut, pcFilePath);
if(szLen > szOutSize || szOutSize < 2)
return false;
while(szLen > 0) while(szLen > 0)
{ {
@ -346,7 +323,6 @@ pl_str_get_directory(const char* pcFilePath, char* pcDirectoryOut, size_t szOutS
pcDirectoryOut[0] = '.'; pcDirectoryOut[0] = '.';
pcDirectoryOut[1] = '/'; pcDirectoryOut[1] = '/';
} }
return true;
} }
bool bool
@ -357,6 +333,7 @@ pl_str_concatenate(const char* pcStr0, const char* pcStr1, char* pcStringOut, si
if(szLen0 + szLen1 > szDataSize) if(szLen0 + szLen1 > szDataSize)
{ {
PL_ASSERT(false && "buffer provided not big enough");
return false; return false;
} }

298
pl_test.h
View File

@ -1,5 +1,5 @@
/* /*
pl_test.h pl_test
Do this: Do this:
#define PL_TEST_IMPLEMENTATION #define PL_TEST_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation. before you include this file in *one* C or C++ file to create the implementation.
@ -9,22 +9,17 @@
#include ... #include ...
#define PL_TEST_IMPLEMENTATION #define PL_TEST_IMPLEMENTATION
#include "pl_test.h" #include "pl_test.h"
Notes:
* for console color output on windows, define "PL_TEST_WIN32_COLOR" before
including the implementation
*/ */
// library version (format XYYZZ) // library version
#define PL_TEST_VERSION "1.0.0" #define PL_TEST_VERSION "0.1.0"
#define PL_TEST_VERSION_NUM 10000 #define PL_TEST_VERSION_NUM 00100
/* /*
Index of this file: Index of this file:
// [SECTION] header mess // [SECTION] header mess
// [SECTION] includes // [SECTION] includes
// [SECTION] public api // [SECTION] public api
// [SECTION] structs
// [SECTION] private
// [SECTION] c file // [SECTION] c file
*/ */
@ -39,8 +34,8 @@ Index of this file:
// [SECTION] includes // [SECTION] includes
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <stdbool.h> // bool #include <stdbool.h>
#include <stdint.h> // uint32_t #include <stdint.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] forward declarations & basic types // [SECTION] forward declarations & basic types
@ -48,7 +43,6 @@ Index of this file:
// forward declarations // forward declarations
typedef struct _plTestContext plTestContext; typedef struct _plTestContext plTestContext;
typedef struct _plTestOptions plTestOptions;
typedef void (*PL_TEST_FUNCTION)(void*); typedef void (*PL_TEST_FUNCTION)(void*);
@ -58,29 +52,26 @@ typedef void (*PL_TEST_FUNCTION)(void*);
#define pl_test_register_test(TEST, DATA) pl__test_register_test((TEST), (DATA), #TEST) #define pl_test_register_test(TEST, DATA) pl__test_register_test((TEST), (DATA), #TEST)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] public api // [SECTION] public api
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
plTestContext* pl_create_test_context(plTestOptions); plTestContext* pl_create_test_context(void);
// tests // tests
void pl_test_run_suite(const char* pcSuiteName); void pl__test_register_test(PL_TEST_FUNCTION tTest, void* pData, const char* pcName);
bool pl_test_finish(void); bool pl_test_run(void);
// booleans // booleans
bool pl_test_expect_true (bool bValue, const char* pcMsg); bool pl_test_expect_true (bool bValue, const char* pcMsg);
bool pl_test_expect_false(bool bValue, const char* pcMsg); bool pl_test_expect_false(bool bValue, const char* pcMsg);
// integers // numbers
bool pl_test_expect_int_equal (int iValue0, int iValue1, const char* pcMsg); bool pl_test_expect_int_equal (int iValue0, int iValue1, const char* pcMsg);
bool pl_test_expect_int_not_equal (int iValue0, int iValue1, const char* pcMsg); bool pl_test_expect_int_not_equal (int iValue0, int iValue1, const char* pcMsg);
bool pl_test_expect_uint32_equal (uint32_t uValue0, uint32_t uValue1, const char* pcMsg); bool pl_test_expect_unsigned_equal (uint32_t uValue0, uint32_t uValue1, const char* pcMsg);
bool pl_test_expect_uint32_not_equal(uint32_t uValue0, uint32_t uValue1, const char* pcMsg); bool pl_test_expect_unsigned_not_equal (uint32_t uValue0, uint32_t uValue1, const char* pcMsg);
bool pl_test_expect_uint64_equal (uint64_t uValue0, uint64_t uValue1, const char* pcMsg);
bool pl_test_expect_uint64_not_equal(uint64_t uValue0, uint64_t uValue1, const char* pcMsg);
// floating point
bool pl_test_expect_float_near_equal (float fValue0, float fValue1, float fError, const char* pcMsg); bool pl_test_expect_float_near_equal (float fValue0, float fValue1, float fError, const char* pcMsg);
bool pl_test_expect_float_near_not_equal (float fValue0, float fValue1, float fError, const char* pcMsg); bool pl_test_expect_float_near_not_equal (float fValue0, float fValue1, float fError, const char* pcMsg);
bool pl_test_expect_double_near_equal (double dValue0, double dValue1, double dError, const char* pcMsg); bool pl_test_expect_double_near_equal (double dValue0, double dValue1, double dError, const char* pcMsg);
@ -90,23 +81,6 @@ bool pl_test_expect_double_near_not_equal(double dValue0, double dValue1, double
bool pl_test_expect_string_equal (const char* pcValue0, const char* pcValue1, const char* pcMsg); bool pl_test_expect_string_equal (const char* pcValue0, const char* pcValue1, const char* pcMsg);
bool pl_test_expect_string_not_equal(const char* pcValue0, const char* pcValue1, const char* pcMsg); bool pl_test_expect_string_not_equal(const char* pcValue0, const char* pcValue1, const char* pcMsg);
//-----------------------------------------------------------------------------
// [SECTION] structs
//-----------------------------------------------------------------------------
typedef struct _plTestOptions
{
bool bPrintSuiteResults;
bool bPrintAllPassedChecks;
bool bPrintColor;
} plTestOptions;
//-----------------------------------------------------------------------------
// [SECTION] private
//-----------------------------------------------------------------------------
void pl__test_register_test(PL_TEST_FUNCTION tTest, void* pData, const char* pcName);
#endif // PL_TEST_H #endif // PL_TEST_H
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -126,13 +100,6 @@ Index of this file:
#ifdef PL_TEST_IMPLEMENTATION #ifdef PL_TEST_IMPLEMENTATION
#if defined(PL_TEST_WIN32_COLOR) || defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static DWORD gtOriginalMode = 0;
static HANDLE gtStdOutHandle = 0;
#endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] includes // [SECTION] includes
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -163,15 +130,12 @@ typedef struct _plTest
typedef struct _plTestContext typedef struct _plTestContext
{ {
plTest* atTests; plTest* atTests;
plTest* ptCurrentTest; plTest* ptCurrentTest;
uint32_t uTestSize; uint32_t uTestSize;
uint32_t uTestCapacity; uint32_t uTestCapacity;
uint32_t uFailedTest; uint32_t uFailedTest;
plTestOptions tOptions; bool bPrintPasses;
uint32_t uTotalPassedTests;
uint32_t uTotalFailedTests;
} plTestContext; } plTestContext;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -185,26 +149,12 @@ plTestContext* gptTestContext = NULL;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
plTestContext* plTestContext*
pl_create_test_context(plTestOptions tOptions) pl_create_test_context(void)
{ {
#if defined(PL_TEST_WIN32_COLOR) || defined(_WIN32)
DWORD tCurrentMode = 0;
gtStdOutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if(gtStdOutHandle == INVALID_HANDLE_VALUE)
exit(GetLastError());
if(!GetConsoleMode(gtStdOutHandle, &tCurrentMode))
exit(GetLastError());
gtOriginalMode = tCurrentMode;
tCurrentMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; // enable ANSI escape codes
if(!SetConsoleMode(gtStdOutHandle, tCurrentMode))
exit(GetLastError());
#endif
gptTestContext = (plTestContext*)malloc(sizeof(plTestContext)); gptTestContext = (plTestContext*)malloc(sizeof(plTestContext));
memset(gptTestContext, 0, sizeof(plTestContext)); memset(gptTestContext, 0, sizeof(plTestContext));
gptTestContext->uTestCapacity = 64; gptTestContext->uTestCapacity = 64;
gptTestContext->tOptions = tOptions; gptTestContext->bPrintPasses = false;
gptTestContext->atTests = (plTest*)malloc(gptTestContext->uTestCapacity * sizeof(plTest)); gptTestContext->atTests = (plTest*)malloc(gptTestContext->uTestCapacity * sizeof(plTest));
memset(gptTestContext->atTests, 0, sizeof(gptTestContext->uTestCapacity * sizeof(plTest))); memset(gptTestContext->atTests, 0, sizeof(gptTestContext->uTestCapacity * sizeof(plTest)));
return gptTestContext; return gptTestContext;
@ -229,67 +179,30 @@ pl__test_register_test(PL_TEST_FUNCTION tTest, void* pData, const char* pcName)
gptTestContext->atTests[gptTestContext->uTestSize - 1].pData = pData; gptTestContext->atTests[gptTestContext->uTestSize - 1].pData = pData;
} }
void bool
pl_test_run_suite(const char* pcSuiteName) pl_test_run(void)
{ {
printf("\n------%s suite------\n\n", pcSuiteName);
for(uint32_t i = 0; i < gptTestContext->uTestSize; i++) for(uint32_t i = 0; i < gptTestContext->uTestSize; i++)
{ {
gptTestContext->ptCurrentTest = &gptTestContext->atTests[i]; gptTestContext->ptCurrentTest = &gptTestContext->atTests[i];
gptTestContext->ptCurrentTest->bFailureOccured = false; gptTestContext->ptCurrentTest->bFailureOccured = false;
printf("running -> \"%s\"\n", gptTestContext->ptCurrentTest->pcName); printf("-----------------------------------\n");
printf("\"%s\" running...\n\n", gptTestContext->ptCurrentTest->pcName);
gptTestContext->ptCurrentTest->tTest(gptTestContext->ptCurrentTest->pData); gptTestContext->ptCurrentTest->tTest(gptTestContext->ptCurrentTest->pData);
if(gptTestContext->ptCurrentTest->bFailureOccured) if(gptTestContext->ptCurrentTest->bFailureOccured)
{ {
pl__test_print_red("%s", NULL, " -> failed"); pl__test_print_red("\n\n\"%s\" failed", NULL, gptTestContext->ptCurrentTest->pcName);
gptTestContext->uFailedTest++; gptTestContext->uFailedTest++;
} }
else else
pl__test_print_green("%s", NULL, " passed"); printf("\n\n\"%s\" passed\n\n", gptTestContext->ptCurrentTest->pcName);
printf("-----------------------------------\n");
} }
if(gptTestContext->tOptions.bPrintSuiteResults) return gptTestContext->uFailedTest == 0;
{
printf("\nPassed: ");
pl__test_print_green("%u", NULL, gptTestContext->uTestSize - gptTestContext->uFailedTest);
printf("Failed: ");
pl__test_print_red("%u", NULL, gptTestContext->uFailedTest);
}
// printf("\n------End tests------\n\n");
// reset context
gptTestContext->uTotalPassedTests += gptTestContext->uTestSize - gptTestContext->uFailedTest;
gptTestContext->uTotalFailedTests += gptTestContext->uFailedTest;
gptTestContext->uTestSize = 0;
gptTestContext->uFailedTest = 0;
gptTestContext->ptCurrentTest = NULL;
memset(gptTestContext->atTests, 0, sizeof(plTest) * gptTestContext->uTestCapacity);
}
bool
pl_test_finish(void)
{
printf("\n------Results------\n");
printf("\nTests passed: ");
pl__test_print_green("%u", NULL, gptTestContext->uTotalPassedTests);
printf("Tests failed: ");
pl__test_print_red("%u", NULL, gptTestContext->uTotalFailedTests);
#if defined(PL_TEST_WIN32_COLOR) || defined(_WIN32)
if(!SetConsoleMode(gtStdOutHandle, gtOriginalMode))
exit(GetLastError());
#endif
return gptTestContext->uTotalFailedTests == 0;
} }
bool bool
@ -297,8 +210,7 @@ pl_test_expect_true(bool bValue, const char* pcMsg)
{ {
if(bValue) if(bValue)
{ {
if(gptTestContext->tOptions.bPrintAllPassedChecks) pl__test_print_green("Value: true | Expected Value: true", pcMsg);
pl__test_print_green("Value: true | Expected Value: true", pcMsg);
return true; return true;
} }
@ -317,67 +229,64 @@ pl_test_expect_false(bool bValue, const char* pcMsg)
return false; return false;
} }
if(gptTestContext->tOptions.bPrintAllPassedChecks) pl__test_print_green("Value: false | Expected Value: false", pcMsg);
pl__test_print_green("Value: false | Expected Value: false", pcMsg);
return true; return true;
} }
#define pl__test_expect_equal(value0, value1, pcMsg, format) \
if((value0) == (value1)) \
{ \
if(gptTestContext->tOptions.bPrintAllPassedChecks) \
pl__test_print_green(format " equals " format " | Equality Expected", (pcMsg), (value0), (value1)); \
return true; \
} \
pl__test_print_red(format " does not equal " format " | Equality Expected", (pcMsg), (value0), (value1)); \
gptTestContext->ptCurrentTest->bFailureOccured = true; \
return false;
#define pl__test_expect_not_equal(value0, value1, pcMsg, format) \
if((value0) == (value1)) \
{ \
pl__test_print_red(format " equals " format " | Equality Not Expected", (pcMsg), (value0), (value1)); \
gptTestContext->ptCurrentTest->bFailureOccured = true; \
return false; \
} \
if(gptTestContext->tOptions.bPrintAllPassedChecks) \
pl__test_print_green(format " does not equal " format " | Equality Expected", (pcMsg), (value0), (value1)); \
return true;
bool bool
pl_test_expect_int_equal(int iValue0, int iValue1, const char* pcMsg) pl_test_expect_int_equal(int iValue0, int iValue1, const char* pcMsg)
{ {
pl__test_expect_equal(iValue0, iValue1, pcMsg, "%i"); if(iValue0 == iValue1)
{
pl__test_print_green("%i equals %i | Equality Expected", pcMsg, iValue0, iValue1);
return true;
}
pl__test_print_red("%i does not equal %i | Equality Expected", pcMsg, iValue0, iValue1);
gptTestContext->ptCurrentTest->bFailureOccured = true;
return false;
} }
bool bool
pl_test_expect_int_not_equal(int iValue0, int iValue1, const char* pcMsg) pl_test_expect_int_not_equal(int iValue0, int iValue1, const char* pcMsg)
{ {
pl__test_expect_not_equal(iValue0, iValue1, pcMsg, "%i"); if(iValue0 == iValue1)
{
pl__test_print_red("%i equals %i | Equality Not Expected", pcMsg, iValue0, iValue1);
gptTestContext->ptCurrentTest->bFailureOccured = true;
return false;
}
pl__test_print_green("%i does not equal %i | Equality Not Expected", pcMsg, iValue0, iValue1);
return true;
} }
bool bool
pl_test_expect_uint64_equal(uint64_t uValue0, uint64_t uValue1, const char* pcMsg) pl_test_expect_unsigned_equal(uint32_t uValue0, uint32_t uValue1, const char* pcMsg)
{ {
pl__test_expect_equal(uValue0, uValue1, pcMsg, "%llu"); if(uValue0 == uValue1)
{
pl__test_print_green("%u equals %u | Equality Expected", pcMsg, uValue0, uValue1);
return true;
}
pl__test_print_red("%u does not equal %u | Equality Expected", pcMsg, uValue0, uValue1);
gptTestContext->ptCurrentTest->bFailureOccured = true;
return false;
} }
bool bool
pl_test_expect_uint64_not_equal(uint64_t uValue0, uint64_t uValue1, const char* pcMsg) pl_test_expect_unsigned_not_equal(uint32_t uValue0, uint32_t uValue1, const char* pcMsg)
{ {
pl__test_expect_not_equal(uValue0, uValue1, pcMsg, "%llu"); if(uValue0 == uValue1)
} {
pl__test_print_red("%u equals %u | Equality Not Expected", pcMsg, uValue0, uValue1);
gptTestContext->ptCurrentTest->bFailureOccured = true;
return false;
}
bool pl__test_print_green("%u does not equal %u | Equality Not Expected", pcMsg, uValue0, uValue1);
pl_test_expect_uint32_equal(uint32_t uValue0, uint32_t uValue1, const char* pcMsg) return true;
{
pl__test_expect_equal(uValue0, uValue1, pcMsg, "%u");
}
bool
pl_test_expect_uint32_not_equal(uint32_t uValue0, uint32_t uValue1, const char* pcMsg)
{
pl__test_expect_not_equal(uValue0, uValue1, pcMsg, "%u");
} }
bool bool
@ -397,8 +306,7 @@ pl_test_expect_double_near_equal(double dValue0, double dValue1, double dError,
{ {
if(dValue0 >= dValue1 - dError && dValue0 <= dValue1 + dError) if(dValue0 >= dValue1 - dError && dValue0 <= dValue1 + dError)
{ {
if(gptTestContext->tOptions.bPrintAllPassedChecks) pl__test_print_green("%0.6f equals %0.6f | Equality Expected within %0.6f", pcMsg, dValue0, dValue1, dError);
pl__test_print_green("%0.6f equals %0.6f | Equality Expected within %0.6f", pcMsg, dValue0, dValue1, dError);
return true; return true;
} }
@ -417,8 +325,7 @@ pl_test_expect_double_near_not_equal(double dValue0, double dValue1, double dErr
return false; return false;
} }
if(gptTestContext->tOptions.bPrintAllPassedChecks) pl__test_print_green("%0.6f does not equal %0.6f | Equality Not Expected within %0.6f", pcMsg, dValue0, dValue1, dError);
pl__test_print_green("%0.6f does not equal %0.6f | Equality Not Expected within %0.6f", pcMsg, dValue0, dValue1, dError);
return true; return true;
} }
@ -427,8 +334,7 @@ pl_test_expect_string_equal(const char* pcValue0, const char* pcValue1, const ch
{ {
if(strcmp(pcValue0, pcValue1) == 0) if(strcmp(pcValue0, pcValue1) == 0)
{ {
if(gptTestContext->tOptions.bPrintAllPassedChecks) pl__test_print_green("\"%s\" equals \"%s\" | Equality Expected", pcMsg, pcValue0, pcValue1);
pl__test_print_green("\"%s\" equals \"%s\" | Equality Expected", pcMsg, pcValue0, pcValue1);
return true; return true;
} }
@ -442,8 +348,7 @@ pl_test_expect_string_not_equal(const char* pcValue0, const char* pcValue1, cons
{ {
if(strcmp(pcValue0, pcValue1) == 0) if(strcmp(pcValue0, pcValue1) == 0)
{ {
if(gptTestContext->tOptions.bPrintAllPassedChecks) pl__test_print_green("\"%s\" equals \"%s\" | Equality Not Expected", pcMsg, pcValue0, pcValue1);
pl__test_print_green("\"%s\" equals \"%s\" | Equality Not Expected", pcMsg, pcValue0, pcValue1);
gptTestContext->ptCurrentTest->bFailureOccured = true; gptTestContext->ptCurrentTest->bFailureOccured = true;
return false; return false;
} }
@ -469,14 +374,11 @@ pl__test_print_va(const char* cPFormat, va_list args)
void static void static
pl__test_print_red(const char* cPFormat, const char* pcMsg, ...) pl__test_print_red(const char* cPFormat, const char* pcMsg, ...)
{ {
if(gptTestContext->tOptions.bPrintColor) #ifdef _WIN32
{ printf("");
#ifdef _WIN32 #else
printf(""); printf("\033[91m");
#else #endif
printf("\033[91m");
#endif
}
va_list argptr; va_list argptr;
va_start(argptr, pcMsg); va_start(argptr, pcMsg);
@ -488,27 +390,24 @@ pl__test_print_red(const char* cPFormat, const char* pcMsg, ...)
else else
printf("\n"); printf("\n");
if(gptTestContext->tOptions.bPrintColor) #ifdef _WIN32
{ printf("");
#ifdef _WIN32 #else
printf(""); printf("\033[0m");
#else #endif
printf("\033[0m");
#endif
}
} }
static void static void
pl__test_print_green(const char* cPFormat, const char* pcMsg, ...) pl__test_print_green(const char* cPFormat, const char* pcMsg, ...)
{ {
if(gptTestContext->tOptions.bPrintColor) if(!gptTestContext->bPrintPasses)
{ return;
#ifdef _WIN32
printf(""); #ifdef _WIN32
#else printf("");
printf("\033[92m"); #else
#endif printf("\033[92m");
} #endif
va_list argptr; va_list argptr;
va_start(argptr, pcMsg); va_start(argptr, pcMsg);
@ -520,14 +419,11 @@ pl__test_print_green(const char* cPFormat, const char* pcMsg, ...)
else else
printf("\n"); printf("\n");
if(gptTestContext->tOptions.bPrintColor) #ifdef _WIN32
{ printf("");
#ifdef _WIN32 #else
printf(""); printf("\033[0m");
#else #endif
printf("\033[0m");
#endif
}
} }
#endif // PL_TEST_IMPLEMENTATION #endif // PL_TEST_IMPLEMENTATION