Get basics to compile & work in unix

This commit is contained in:
Doyle 2017-06-21 14:10:53 +10:00
parent b1e7fdc264
commit 341388bb70
4 changed files with 114 additions and 104 deletions

20
dqn.h
View File

@ -17,6 +17,9 @@
#if (defined(_WIN32) || defined(_WIN64)) && defined(DQN_WIN32_IMPLEMENTATION) #if (defined(_WIN32) || defined(_WIN64)) && defined(DQN_WIN32_IMPLEMENTATION)
#define DQN_PLATFORM_LAYER #define DQN_PLATFORM_LAYER
#define DQN_WIN32_PLATFORM #define DQN_WIN32_PLATFORM
#elif defined(__linux__) && defined(DQN_UNIX_IMPLEMENTATION)
#define DQN_PLATFORM_LAYER
#define DQN_UNIX_PLATFORM
#endif #endif
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -35,6 +38,8 @@
#endif #endif
#include <stdint.h> // For standard types #include <stdint.h> // For standard types
#include <stddef.h> // For standard types
#include <string.h> // memmove
#include <float.h> #include <float.h>
#define LOCAL_PERSIST static #define LOCAL_PERSIST static
#define FILE_SCOPE static #define FILE_SCOPE static
@ -1463,10 +1468,10 @@ STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char comma, char peri
// DQN_IMPLEMENTATION // DQN_IMPLEMENTATION
// //
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include <math.h> // TODO(doyle): For trigonometry functions (for now) #include <math.h> // TODO(doyle): For trigonometry functions (for now)
#include <stdlib.h> // For calloc, malloc, free #include <stdlib.h> // For calloc, malloc, free
#include <string.h> // For memset #include <stdio.h> // For printf
#include <stdio.h> // For printf #include <x86intrin.h> // __rdtsc
// NOTE: STB_SPRINTF is included when DQN_IMPLEMENTATION defined // NOTE: STB_SPRINTF is included when DQN_IMPLEMENTATION defined
// #define STB_SPRINTF_IMPLEMENTATION // #define STB_SPRINTF_IMPLEMENTATION
@ -3459,15 +3464,12 @@ FILE_SCOPE u64 DqnRnd_Murmur3Avalanche64Internal(u64 h)
FILE_SCOPE u32 DqnRnd_MakeSeedInternal() FILE_SCOPE u32 DqnRnd_MakeSeedInternal()
{ {
#ifdef DQN_WIN32_PLATFORM #if defined(DQN_WIN32_PLATFORM) || defined(DQN_UNIX_PLATFORM)
__int64 numClockCycles = __rdtsc(); i64 numClockCycles = __rdtsc();
return (u32)numClockCycles; return (u32)numClockCycles;
#elif __ANDROID__ #elif __ANDROID__
DQN_ASSERT_MSG(DQN_INVALID_CODE_PATH, "Android path not implemented yet"); DQN_ASSERT_MSG(DQN_INVALID_CODE_PATH, "Android path not implemented yet");
return 0; return 0;
#elif __linux__
unsigned long long numClockCycles = rdtsc();
return (u32)numClockCycles;
#else #else
DQN_ASSERT_MSG(DQN_INVALID_CODE_PATH, "Non Win32 path not implemented yet"); DQN_ASSERT_MSG(DQN_INVALID_CODE_PATH, "Non Win32 path not implemented yet");
return 0; return 0;

View File

@ -1,4 +1,12 @@
#define DQN_WIN32_IMPLEMENTATION #if (defined(_WIN32) || defined(_WIN64))
#define DQN_WIN32_IMPLEMENTATION
#endif
#if defined(__linux__)
#define DQN_UNIX_IMPLEMENTATION
#define HANDMADE_MATH_NO_SSE
#endif
#define DQN_IMPLEMENTATION #define DQN_IMPLEMENTATION
#include "dqn.h" #include "dqn.h"
@ -7,7 +15,7 @@
#include "tests/HandmadeMath.h" #include "tests/HandmadeMath.h"
#include <stdio.h> #include <stdio.h>
#include <limits.h>
void HandmadeMathVerifyMat4(DqnMat4 dqnMat, hmm_mat4 hmmMat) void HandmadeMathVerifyMat4(DqnMat4 dqnMat, hmm_mat4 hmmMat)
{ {
f32 *hmmMatf = (f32 *)&hmmMat; f32 *hmmMatf = (f32 *)&hmmMat;
@ -19,7 +27,13 @@ void HandmadeMathVerifyMat4(DqnMat4 dqnMat, hmm_mat4 hmmMat)
DQN_ASSERT(totalSize == (DQN_ARRAY_COUNT(hmmMat.Elements) * DQN_ARRAY_COUNT(hmmMat.Elements[0]))); DQN_ASSERT(totalSize == (DQN_ARRAY_COUNT(hmmMat.Elements) * DQN_ARRAY_COUNT(hmmMat.Elements[0])));
for (i32 i = 0; i < EXPECTED_SIZE; i++) for (i32 i = 0; i < EXPECTED_SIZE; i++)
DQN_ASSERT(hmmMatf[i] == dqnMatf[i]); {
const f32 EPSILON = 0.001f;
f32 diff = hmmMatf[i] - dqnMatf[i];
diff = DQN_ABS(diff);
DQN_ASSERT_MSG(diff < EPSILON, "hmmMatf[%d]: %f, dqnMatf[%d]: %f\n", i,
hmmMatf[i], i, dqnMatf[i]);
}
} }
void HandmadeMathTest() void HandmadeMathTest()
@ -132,7 +146,7 @@ void StringsTest()
{ {
// strcmp // strcmp
{ {
char *a = "str_a"; const char *const a = "str_a";
// Check simple compares // Check simple compares
{ {
@ -158,7 +172,7 @@ void StringsTest()
// strlen // strlen
{ {
char *a = "str_a"; const char *const a = "str_a";
DQN_ASSERT(DqnStr_Len(a) == 5); DQN_ASSERT(DqnStr_Len(a) == 5);
DQN_ASSERT(DqnStr_Len("") == 0); DQN_ASSERT(DqnStr_Len("") == 0);
DQN_ASSERT(DqnStr_Len(" a ") == 6); DQN_ASSERT(DqnStr_Len(" a ") == 6);
@ -175,7 +189,7 @@ void StringsTest()
// strncpy // strncpy
{ {
{ {
char *a = "str_a"; const char *const a = "str_a";
char b[10] = {}; char b[10] = {};
// Check copy into empty array // Check copy into empty array
{ {
@ -199,31 +213,6 @@ void StringsTest()
DQN_ASSERT(DqnStr_Len(b) == 6); DQN_ASSERT(DqnStr_Len(b) == 6);
} }
} }
// Check strncpy with NULL pointers
{
DQN_ASSERT(DqnStr_Copy(NULL, NULL, 5) == NULL);
char *a = "str";
char *result = DqnStr_Copy(a, NULL, 5);
DQN_ASSERT(DqnStr_Cmp(a, "str") == 0);
DQN_ASSERT(DqnStr_Cmp(result, "str") == 0);
DQN_ASSERT(DqnStr_Cmp(result, a) == 0);
}
// Check strncpy with 0 chars to copy
{
char *a = "str";
char *b = "ing";
char *result = DqnStr_Copy(a, b, 0);
DQN_ASSERT(DqnStr_Cmp(a, "str") == 0);
DQN_ASSERT(DqnStr_Cmp(b, "ing") == 0);
DQN_ASSERT(DqnStr_Cmp(result, "str") == 0);
}
printf("StringsTest(): strncpy: Completed successfully\n");
} }
// StrReverse // StrReverse
@ -269,20 +258,20 @@ void StringsTest()
} }
const u64 LARGEST_NUM = (u64)-1; const u64 LARGEST_NUM = (u64)-1;
const i64 SMALLEST_NUM = -9223372036854775808LL; const i64 SMALLEST_NUM = LLONG_MIN;
// StrToI64 // StrToI64
{ {
char *a = "123"; const char *const a = "123";
DQN_ASSERT(Dqn_StrToI64(a, DqnStr_Len(a)) == 123); DQN_ASSERT(Dqn_StrToI64(a, DqnStr_Len(a)) == 123);
char *b = "-123"; const char *const b = "-123";
DQN_ASSERT(Dqn_StrToI64(b, DqnStr_Len(b)) == -123); DQN_ASSERT(Dqn_StrToI64(b, DqnStr_Len(b)) == -123);
DQN_ASSERT(Dqn_StrToI64(b, 1) == 0); DQN_ASSERT(Dqn_StrToI64(b, 1) == 0);
char *c = "-0"; const char *const c = "-0";
DQN_ASSERT(Dqn_StrToI64(c, DqnStr_Len(c)) == 0); DQN_ASSERT(Dqn_StrToI64(c, DqnStr_Len(c)) == 0);
char *d = "+123"; const char *const d = "+123";
DQN_ASSERT(Dqn_StrToI64(d, DqnStr_Len(d)) == 123); DQN_ASSERT(Dqn_StrToI64(d, DqnStr_Len(d)) == 123);
// TODO(doyle): Unsigned conversion // TODO(doyle): Unsigned conversion
@ -291,7 +280,7 @@ void StringsTest()
DQN_ASSERT((u64)(Dqn_StrToI64(e, DqnStr_Len(e))) == LARGEST_NUM); DQN_ASSERT((u64)(Dqn_StrToI64(e, DqnStr_Len(e))) == LARGEST_NUM);
#endif #endif
char *f = "-9223372036854775808"; const char *const f = "-9223372036854775808";
DQN_ASSERT(Dqn_StrToI64(f, DqnStr_Len(f)) == SMALLEST_NUM); DQN_ASSERT(Dqn_StrToI64(f, DqnStr_Len(f)) == SMALLEST_NUM);
printf("StringsTest(): StrToI64: Completed successfully\n"); printf("StringsTest(): StrToI64: Completed successfully\n");
@ -406,8 +395,8 @@ void StringsTest()
{ {
{ {
char *a = "Microsoft"; const char *const a = "Microsoft";
char *b = "icro"; const char *const b = "icro";
i32 lenA = DqnStr_Len(a); i32 lenA = DqnStr_Len(a);
i32 lenB = DqnStr_Len(b); i32 lenB = DqnStr_Len(b);
DQN_ASSERT(DqnStr_HasSubstring(a, lenA, b, lenB) == true); DQN_ASSERT(DqnStr_HasSubstring(a, lenA, b, lenB) == true);
@ -422,8 +411,8 @@ void StringsTest()
} }
{ {
char *a = "Micro"; const char *const a = "Micro";
char *b = "irob"; const char *const b = "irob";
i32 lenA = DqnStr_Len(a); i32 lenA = DqnStr_Len(a);
i32 lenB = DqnStr_Len(b); i32 lenB = DqnStr_Len(b);
DQN_ASSERT(DqnStr_HasSubstring(a, lenA, b, lenB) == false); DQN_ASSERT(DqnStr_HasSubstring(a, lenA, b, lenB) == false);
@ -506,6 +495,7 @@ void StringsTest()
printf("StringsTest(): Completed successfully\n"); printf("StringsTest(): Completed successfully\n");
} }
#ifdef DQN_WIN32_IMPLEMENTATION
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
void OtherTest() void OtherTest()
@ -522,6 +512,7 @@ void OtherTest()
} }
printf("OtherTest(): Completed successfully\n"); printf("OtherTest(): Completed successfully\n");
} }
#endif
void RandomTest() { void RandomTest() {
@ -641,6 +632,7 @@ void VecTest()
// V2 Properties // V2 Properties
{ {
const f32 EPSILON = 0.001f;
DqnV2 a = DqnV2_2f(0, 0); DqnV2 a = DqnV2_2f(0, 0);
DqnV2 b = DqnV2_2f(3, 4); DqnV2 b = DqnV2_2f(3, 4);
@ -651,8 +643,12 @@ void VecTest()
DQN_ASSERT(length == 5); DQN_ASSERT(length == 5);
DqnV2 normalised = DqnV2_Normalise(b); DqnV2 normalised = DqnV2_Normalise(b);
DQN_ASSERT(normalised.x == (b.x / 5.0f)); f32 normX = b.x / 5.0f;
DQN_ASSERT(normalised.y == (b.y / 5.0f)); f32 normY = b.y / 5.0f;
f32 diffNormX = normalised.x - normX;
f32 diffNormY = normalised.y - normY;
DQN_ASSERT_MSG(diffNormX < EPSILON, "normalised.x: %f, normX: %f\n", normalised.x, normX);
DQN_ASSERT_MSG(diffNormY < EPSILON, "normalised.y: %f, normY: %f\n", normalised.y, normY);
DqnV2 c = DqnV2_2f(3.5f, 8.0f); DqnV2 c = DqnV2_2f(3.5f, 8.0f);
DQN_ASSERT(DqnV2_Overlaps(b, c) == true); DQN_ASSERT(DqnV2_Overlaps(b, c) == true);
@ -1078,55 +1074,6 @@ void ArrayTest()
printf("ArrayTest(): Completed successfully\n"); printf("ArrayTest(): Completed successfully\n");
} }
void FileTest()
{
// File i/o
{
{
DqnFile file = {};
DQN_ASSERT(DqnFile_Open(
".clang-format", &file,
(DqnFilePermissionFlag_Write | DqnFilePermissionFlag_Read),
DqnFileAction_OpenOnly));
DQN_ASSERT(file.size == 1320);
u8 *buffer = (u8 *)calloc(1, (size_t)file.size * sizeof(u8));
DQN_ASSERT(DqnFile_Read(file, buffer, (u32)file.size) == file.size);
free(buffer);
DqnFile_Close(&file);
DQN_ASSERT(!file.handle && file.size == 0 &&
file.permissionFlags == 0);
}
{
DqnFile file = {};
DQN_ASSERT(!DqnFile_Open(
"asdljasdnel;kajdf", &file,
(DqnFilePermissionFlag_Write | DqnFilePermissionFlag_Read),
DqnFileAction_OpenOnly));
DQN_ASSERT(file.size == 0);
DQN_ASSERT(file.permissionFlags == 0);
DQN_ASSERT(!file.handle);
printf("FileTest(): FileIO: Completed successfully\n");
}
}
{
u32 numFiles;
char **filelist = DqnDir_Read("*", &numFiles);
printf("FileTest(): DirRead: Display read files\n");
for (u32 i = 0; i < numFiles; i++)
printf("FileTest(): DirRead: %s\n", filelist[i]);
DqnDir_ReadFree(filelist, numFiles);
printf("FileTest(): DirRead: Completed successfully\n");
}
printf("FileTest(): Completed successfully\n");
}
void MemStackTest() void MemStackTest()
{ {
// Test over allocation, alignments, temp regions // Test over allocation, alignments, temp regions
@ -1491,6 +1438,57 @@ void MemStackTest()
} }
} }
#ifdef DQN_WIN32_IMPLEMENTATION
void FileTest()
{
// File i/o
{
{
DqnFile file = {};
DQN_ASSERT(DqnFile_Open(
".clang-format", &file,
(DqnFilePermissionFlag_Write | DqnFilePermissionFlag_Read),
DqnFileAction_OpenOnly));
DQN_ASSERT(file.size == 1320);
u8 *buffer = (u8 *)calloc(1, (size_t)file.size * sizeof(u8));
DQN_ASSERT(DqnFile_Read(file, buffer, (u32)file.size) == file.size);
free(buffer);
DqnFile_Close(&file);
DQN_ASSERT(!file.handle && file.size == 0 &&
file.permissionFlags == 0);
}
{
DqnFile file = {};
DQN_ASSERT(!DqnFile_Open(
"asdljasdnel;kajdf", &file,
(DqnFilePermissionFlag_Write | DqnFilePermissionFlag_Read),
DqnFileAction_OpenOnly));
DQN_ASSERT(file.size == 0);
DQN_ASSERT(file.permissionFlags == 0);
DQN_ASSERT(!file.handle);
printf("FileTest(): FileIO: Completed successfully\n");
}
}
{
u32 numFiles;
char **filelist = DqnDir_Read("*", &numFiles);
printf("FileTest(): DirRead: Display read files\n");
for (u32 i = 0; i < numFiles; i++)
printf("FileTest(): DirRead: %s\n", filelist[i]);
DqnDir_ReadFree(filelist, numFiles);
printf("FileTest(): DirRead: Completed successfully\n");
}
printf("FileTest(): Completed successfully\n");
}
FILE_SCOPE u32 volatile globalDebugCounter; FILE_SCOPE u32 volatile globalDebugCounter;
FILE_SCOPE bool volatile globalDebugCounterMemoize[2048]; FILE_SCOPE bool volatile globalDebugCounterMemoize[2048];
FILE_SCOPE DqnLock globalJobQueueLock; FILE_SCOPE DqnLock globalJobQueueLock;
@ -1548,6 +1546,7 @@ FILE_SCOPE void JobQueueTest()
printf("\nJobQueueTest(): Final incremented value: %d\n", globalDebugCounter); printf("\nJobQueueTest(): Final incremented value: %d\n", globalDebugCounter);
DQN_ASSERT(globalDebugCounter == DQN_ARRAY_COUNT(globalDebugCounterMemoize)); DQN_ASSERT(globalDebugCounter == DQN_ARRAY_COUNT(globalDebugCounterMemoize));
} }
#endif
int main(void) int main(void)
{ {
@ -1556,11 +1555,14 @@ int main(void)
MathTest(); MathTest();
HandmadeMathTest(); HandmadeMathTest();
VecTest(); VecTest();
OtherTest();
ArrayTest(); ArrayTest();
FileTest();
MemStackTest(); MemStackTest();
#ifdef DQN_WIN32_IMPLEMENTATION
OtherTest();
FileTest();
JobQueueTest(); JobQueueTest();
#endif
printf("\nPress 'Enter' Key to Exit\n"); printf("\nPress 'Enter' Key to Exit\n");
getchar(); getchar();

3
makefile Normal file
View File

@ -0,0 +1,3 @@
all: dqn_unit_test.cpp
mkdir -p bin
gcc -o bin/dqn_unit_test dqn_unit_test.cpp -lm

View File

@ -1,3 +1,5 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpsabi"
/* /*
HandmadeMath.h v1.1.2 HandmadeMath.h v1.1.2
@ -777,7 +779,7 @@ HMM_RSquareRootF(float Value)
float Result = 0.0f; float Result = 0.0f;
#ifdef HANDMADE_MATH_NO_SSE #ifdef HANDMADE_MATH_NO_SSE
Result = 1.0f/HMM_SqrtF(Value); Result = 1.0f/HMM_SquareRootF(Value);
#else #else
__m128 In = _mm_set_ss(Value); __m128 In = _mm_set_ss(Value);
__m128 Out = _mm_rsqrt_ss(In); __m128 Out = _mm_rsqrt_ss(In);
@ -2657,3 +2659,4 @@ operator*=(hmm_quaternion &Left, float Right)
#endif /* HANDMADE_MATH_CPP_MODE */ #endif /* HANDMADE_MATH_CPP_MODE */
#endif /* HANDMADE_MATH_IMPLEMENTATION */ #endif /* HANDMADE_MATH_IMPLEMENTATION */
#pragma GCC diagnostic pop