2207 lines
110 KiB
C++
2207 lines
110 KiB
C++
#define DN_UTEST_IMPLEMENTATION
|
|
#include "Standalone/dqn_utest.h"
|
|
|
|
#include <inttypes.h>
|
|
|
|
// NOTE: Taken from MSDN __cpuid example implementation
|
|
// https://learn.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex?view=msvc-170
|
|
struct DN_RefImplCPUReport {
|
|
unsigned int nIds_ = 0;
|
|
unsigned int nExIds_ = 0;
|
|
char vendor_[0x20] = {};
|
|
int vendorSize_ = 0;
|
|
char brand_[0x40] = {};
|
|
int brandSize_ = 0;
|
|
bool isIntel_ = false;
|
|
bool isAMD_ = false;
|
|
uint32_t f_1_ECX_ = 0;
|
|
uint32_t f_1_EDX_ = 0;
|
|
uint32_t f_7_EBX_ = 0;
|
|
uint32_t f_7_ECX_ = 0;
|
|
uint32_t f_81_ECX_ = 0;
|
|
uint32_t f_81_EDX_ = 0;
|
|
int data_[400][4] = {};
|
|
size_t dataSize_ = 0;
|
|
int extdata_[400][4] = {};
|
|
size_t extdataSize_ = 0;
|
|
|
|
bool SSE3(void) const { return f_1_ECX_ & (1 << 0); }
|
|
bool PCLMULQDQ(void) const { return f_1_ECX_ & (1 << 1); }
|
|
bool MONITOR(void) const { return f_1_ECX_ & (1 << 3); }
|
|
bool SSSE3(void) const { return f_1_ECX_ & (1 << 9); }
|
|
bool FMA(void) const { return f_1_ECX_ & (1 << 12); }
|
|
bool CMPXCHG16B(void) const { return f_1_ECX_ & (1 << 13); }
|
|
bool SSE41(void) const { return f_1_ECX_ & (1 << 19); }
|
|
bool SSE42(void) const { return f_1_ECX_ & (1 << 20); }
|
|
bool MOVBE(void) const { return f_1_ECX_ & (1 << 22); }
|
|
bool POPCNT(void) const { return f_1_ECX_ & (1 << 23); }
|
|
bool AES(void) const { return f_1_ECX_ & (1 << 25); }
|
|
bool XSAVE(void) const { return f_1_ECX_ & (1 << 26); }
|
|
bool OSXSAVE(void) const { return f_1_ECX_ & (1 << 27); }
|
|
bool AVX(void) const { return f_1_ECX_ & (1 << 28); }
|
|
bool F16C(void) const { return f_1_ECX_ & (1 << 29); }
|
|
bool RDRAND(void) const { return f_1_ECX_ & (1 << 30); }
|
|
|
|
bool MSR(void) const { return f_1_EDX_ & (1 << 5); }
|
|
bool CX8(void) const { return f_1_EDX_ & (1 << 8); }
|
|
bool SEP(void) const { return f_1_EDX_ & (1 << 11); }
|
|
bool CMOV(void) const { return f_1_EDX_ & (1 << 15); }
|
|
bool CLFSH(void) const { return f_1_EDX_ & (1 << 19); }
|
|
bool MMX(void) const { return f_1_EDX_ & (1 << 23); }
|
|
bool FXSR(void) const { return f_1_EDX_ & (1 << 24); }
|
|
bool SSE(void) const { return f_1_EDX_ & (1 << 25); }
|
|
bool SSE2(void) const { return f_1_EDX_ & (1 << 26); }
|
|
|
|
bool FSGSBASE(void) const { return f_7_EBX_ & (1 << 0); }
|
|
bool BMI1(void) const { return f_7_EBX_ & (1 << 3); }
|
|
bool HLE(void) const { return isIntel_ && f_7_EBX_ & (1 << 4); }
|
|
bool AVX2(void) const { return f_7_EBX_ & (1 << 5); }
|
|
bool BMI2(void) const { return f_7_EBX_ & (1 << 8); }
|
|
bool ERMS(void) const { return f_7_EBX_ & (1 << 9); }
|
|
bool INVPCID(void) const { return f_7_EBX_ & (1 << 10); }
|
|
bool RTM(void) const { return isIntel_ && f_7_EBX_ & (1 << 11); }
|
|
bool AVX512F(void) const { return f_7_EBX_ & (1 << 16); }
|
|
bool RDSEED(void) const { return f_7_EBX_ & (1 << 18); }
|
|
bool ADX(void) const { return f_7_EBX_ & (1 << 19); }
|
|
bool AVX512PF(void) const { return f_7_EBX_ & (1 << 26); }
|
|
bool AVX512ER(void) const { return f_7_EBX_ & (1 << 27); }
|
|
bool AVX512CD(void) const { return f_7_EBX_ & (1 << 28); }
|
|
bool SHA(void) const { return f_7_EBX_ & (1 << 29); }
|
|
|
|
bool PREFETCHWT1(void) const { return f_7_ECX_ & (1 << 0); }
|
|
|
|
bool LAHF(void) const { return f_81_ECX_ & (1 << 0); }
|
|
bool LZCNT(void) const { return isIntel_ && f_81_ECX_ & (1 << 5); }
|
|
bool ABM(void) const { return isAMD_ && f_81_ECX_ & (1 << 5); }
|
|
bool SSE4a(void) const { return isAMD_ && f_81_ECX_ & (1 << 6); }
|
|
bool XOP(void) const { return isAMD_ && f_81_ECX_ & (1 << 11); }
|
|
bool TBM(void) const { return isAMD_ && f_81_ECX_ & (1 << 21); }
|
|
|
|
bool SYSCALL(void) const { return isIntel_ && f_81_EDX_ & (1 << 11); }
|
|
bool MMXEXT(void) const { return isAMD_ && f_81_EDX_ & (1 << 22); }
|
|
bool RDTSCP(void) const { return f_81_EDX_ & (1 << 27); }
|
|
bool _3DNOWEXT(void) const { return isAMD_ && f_81_EDX_ & (1 << 30); }
|
|
bool _3DNOW(void) const { return isAMD_ && f_81_EDX_ & (1 << 31); }
|
|
};
|
|
|
|
DN_RefImplCPUReport DN_RefImplCPUReport_Init()
|
|
{
|
|
DN_RefImplCPUReport result = {};
|
|
|
|
// int cpuInfo[4] = {-1};
|
|
int cpui[4];
|
|
|
|
// Calling __cpuid with 0x0 as the function_id argument
|
|
// gets the number of the highest valid function ID.
|
|
__cpuid(cpui, 0);
|
|
result.nIds_ = cpui[0];
|
|
|
|
for (unsigned int i = 0; i <= result.nIds_; ++i) {
|
|
__cpuidex(cpui, i, 0);
|
|
memcpy(result.data_[result.dataSize_++], cpui, sizeof(cpui));
|
|
}
|
|
|
|
// Capture vendor string
|
|
*reinterpret_cast<int *>(result.vendor_) = result.data_[0][1];
|
|
*reinterpret_cast<int *>(result.vendor_ + 4) = result.data_[0][3];
|
|
*reinterpret_cast<int *>(result.vendor_ + 8) = result.data_[0][2];
|
|
result.vendorSize_ = (int)strlen(result.vendor_);
|
|
|
|
if (strcmp(result.vendor_, "GenuineIntel") == 0)
|
|
result.isIntel_ = true;
|
|
else if (strcmp(result.vendor_, "AuthenticAMD") == 0)
|
|
result.isAMD_ = true;
|
|
|
|
// load bitset with flags for function 0x00000001
|
|
if (result.nIds_ >= 1) {
|
|
result.f_1_ECX_ = result.data_[1][2];
|
|
result.f_1_EDX_ = result.data_[1][3];
|
|
}
|
|
|
|
// load bitset with flags for function 0x00000007
|
|
if (result.nIds_ >= 7) {
|
|
result.f_7_EBX_ = result.data_[7][1];
|
|
result.f_7_ECX_ = result.data_[7][2];
|
|
}
|
|
|
|
// Calling __cpuid with 0x80000000 as the function_id argument
|
|
// gets the number of the highest valid extended ID.
|
|
__cpuid(cpui, 0x80000000);
|
|
result.nExIds_ = cpui[0];
|
|
|
|
for (unsigned int i = 0x80000000; i <= result.nExIds_; ++i) {
|
|
__cpuidex(cpui, i, 0);
|
|
memcpy(result.extdata_[result.extdataSize_++], cpui, sizeof(cpui));
|
|
}
|
|
|
|
// load bitset with flags for function 0x80000001
|
|
if (result.nExIds_ >= 0x80000001) {
|
|
result.f_81_ECX_ = result.extdata_[1][2];
|
|
result.f_81_EDX_ = result.extdata_[1][3];
|
|
}
|
|
|
|
// Interpret CPU brand string if reported
|
|
if (result.nExIds_ >= 0x80000004) {
|
|
memcpy(result.brand_, result.extdata_[2], sizeof(cpui));
|
|
memcpy(result.brand_ + 16, result.extdata_[3], sizeof(cpui));
|
|
memcpy(result.brand_ + 32, result.extdata_[4], sizeof(cpui));
|
|
result.brandSize_ = (int)strlen(result.brand_);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#if 0
|
|
static void DN_RefImpl_CPUReportDump() // Print out supported instruction set features
|
|
{
|
|
auto support_message = [](std::string isa_feature, bool is_supported) {
|
|
printf("%s %s\n", isa_feature.c_str(), is_supported ? "supported" : "not supported");
|
|
};
|
|
|
|
printf("%s\n", DN_RefImplCPUReport::Vendor().c_str());
|
|
printf("%s\n", DN_RefImplCPUReport::Brand().c_str());
|
|
|
|
support_message("3DNOW", DN_RefImplCPUReport::_3DNOW());
|
|
support_message("3DNOWEXT", DN_RefImplCPUReport::_3DNOWEXT());
|
|
support_message("ABM", DN_RefImplCPUReport::ABM());
|
|
support_message("ADX", DN_RefImplCPUReport::ADX());
|
|
support_message("AES", DN_RefImplCPUReport::AES());
|
|
support_message("AVX", DN_RefImplCPUReport::AVX());
|
|
support_message("AVX2", DN_RefImplCPUReport::AVX2());
|
|
support_message("AVX512CD", DN_RefImplCPUReport::AVX512CD());
|
|
support_message("AVX512ER", DN_RefImplCPUReport::AVX512ER());
|
|
support_message("AVX512F", DN_RefImplCPUReport::AVX512F());
|
|
support_message("AVX512PF", DN_RefImplCPUReport::AVX512PF());
|
|
support_message("BMI1", DN_RefImplCPUReport::BMI1());
|
|
support_message("BMI2", DN_RefImplCPUReport::BMI2());
|
|
support_message("CLFSH", DN_RefImplCPUReport::CLFSH());
|
|
support_message("CMPXCHG16B", DN_RefImplCPUReport::CMPXCHG16B());
|
|
support_message("CX8", DN_RefImplCPUReport::CX8());
|
|
support_message("ERMS", DN_RefImplCPUReport::ERMS());
|
|
support_message("F16C", DN_RefImplCPUReport::F16C());
|
|
support_message("FMA", DN_RefImplCPUReport::FMA());
|
|
support_message("FSGSBASE", DN_RefImplCPUReport::FSGSBASE());
|
|
support_message("FXSR", DN_RefImplCPUReport::FXSR());
|
|
support_message("HLE", DN_RefImplCPUReport::HLE());
|
|
support_message("INVPCID", DN_RefImplCPUReport::INVPCID());
|
|
support_message("LAHF", DN_RefImplCPUReport::LAHF());
|
|
support_message("LZCNT", DN_RefImplCPUReport::LZCNT());
|
|
support_message("MMX", DN_RefImplCPUReport::MMX());
|
|
support_message("MMXEXT", DN_RefImplCPUReport::MMXEXT());
|
|
support_message("MONITOR", DN_RefImplCPUReport::MONITOR());
|
|
support_message("MOVBE", DN_RefImplCPUReport::MOVBE());
|
|
support_message("MSR", DN_RefImplCPUReport::MSR());
|
|
support_message("OSXSAVE", DN_RefImplCPUReport::OSXSAVE());
|
|
support_message("PCLMULQDQ", DN_RefImplCPUReport::PCLMULQDQ());
|
|
support_message("POPCNT", DN_RefImplCPUReport::POPCNT());
|
|
support_message("PREFETCHWT1", DN_RefImplCPUReport::PREFETCHWT1());
|
|
support_message("RDRAND", DN_RefImplCPUReport::RDRAND());
|
|
support_message("RDSEED", DN_RefImplCPUReport::RDSEED());
|
|
support_message("RDTSCP", DN_RefImplCPUReport::RDTSCP());
|
|
support_message("RTM", DN_RefImplCPUReport::RTM());
|
|
support_message("SEP", DN_RefImplCPUReport::SEP());
|
|
support_message("SHA", DN_RefImplCPUReport::SHA());
|
|
support_message("SSE", DN_RefImplCPUReport::SSE());
|
|
support_message("SSE2", DN_RefImplCPUReport::SSE2());
|
|
support_message("SSE3", DN_RefImplCPUReport::SSE3());
|
|
support_message("SSE4.1", DN_RefImplCPUReport::SSE41());
|
|
support_message("SSE4.2", DN_RefImplCPUReport::SSE42());
|
|
support_message("SSE4a", DN_RefImplCPUReport::SSE4a());
|
|
support_message("SSSE3", DN_RefImplCPUReport::SSSE3());
|
|
support_message("SYSCALL", DN_RefImplCPUReport::SYSCALL());
|
|
support_message("TBM", DN_RefImplCPUReport::TBM());
|
|
support_message("XOP", DN_RefImplCPUReport::XOP());
|
|
support_message("XSAVE", DN_RefImplCPUReport::XSAVE());
|
|
};
|
|
#endif
|
|
|
|
static DN_UTest DN_Test_Base()
|
|
{
|
|
DN_RefImplCPUReport ref_cpu_report = DN_RefImplCPUReport_Init();
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_Base") {
|
|
DN_UTEST_TEST("Query CPUID") {
|
|
DN_CPUReport cpu_report = DN_CPU_Report();
|
|
|
|
// NOTE: Sanity check our report against MSDN's example ////////////////////////////////////////
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_3DNow) == ref_cpu_report._3DNOW());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_3DNowExt) == ref_cpu_report._3DNOWEXT());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_ABM) == ref_cpu_report.ABM());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_AES) == ref_cpu_report.AES());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_AVX) == ref_cpu_report.AVX());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_AVX2) == ref_cpu_report.AVX2());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_AVX512CD) == ref_cpu_report.AVX512CD());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_AVX512ER) == ref_cpu_report.AVX512ER());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_AVX512F) == ref_cpu_report.AVX512F());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_AVX512PF) == ref_cpu_report.AVX512PF());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_CMPXCHG16B) == ref_cpu_report.CMPXCHG16B());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_F16C) == ref_cpu_report.F16C());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_FMA) == ref_cpu_report.FMA());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_MMX) == ref_cpu_report.MMX());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_MmxExt) == ref_cpu_report.MMXEXT());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_MONITOR) == ref_cpu_report.MONITOR());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_MOVBE) == ref_cpu_report.MOVBE());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_PCLMULQDQ) == ref_cpu_report.PCLMULQDQ());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_POPCNT) == ref_cpu_report.POPCNT());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_RDRAND) == ref_cpu_report.RDRAND());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_RDSEED) == ref_cpu_report.RDSEED());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_RDTSCP) == ref_cpu_report.RDTSCP());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SHA) == ref_cpu_report.SHA());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SSE) == ref_cpu_report.SSE());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SSE2) == ref_cpu_report.SSE2());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SSE3) == ref_cpu_report.SSE3());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SSE41) == ref_cpu_report.SSE41());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SSE42) == ref_cpu_report.SSE42());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SSE4A) == ref_cpu_report.SSE4a());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SSSE3) == ref_cpu_report.SSSE3());
|
|
|
|
// NOTE: Feature flags we haven't bothered detecting yet but are in MSDN's example /////////////
|
|
#if 0
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_ADX) == DN_RefImplCPUReport::ADX());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_BMI1) == DN_RefImplCPUReport::BMI1());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_BMI2) == DN_RefImplCPUReport::BMI2());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_CLFSH) == DN_RefImplCPUReport::CLFSH());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_CX8) == DN_RefImplCPUReport::CX8());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_ERMS) == DN_RefImplCPUReport::ERMS());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_FSGSBASE) == DN_RefImplCPUReport::FSGSBASE());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_FXSR) == DN_RefImplCPUReport::FXSR());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_HLE) == DN_RefImplCPUReport::HLE());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_INVPCID) == DN_RefImplCPUReport::INVPCID());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_LAHF) == DN_RefImplCPUReport::LAHF());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_LZCNT) == DN_RefImplCPUReport::LZCNT());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_MSR) == DN_RefImplCPUReport::MSR());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_OSXSAVE) == DN_RefImplCPUReport::OSXSAVE());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_PREFETCHWT1) == DN_RefImplCPUReport::PREFETCHWT1());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_RTM) == DN_RefImplCPUReport::RTM());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SEP) == DN_RefImplCPUReport::SEP());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_SYSCALL) == DN_RefImplCPUReport::SYSCALL());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_TBM) == DN_RefImplCPUReport::TBM());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_XOP) == DN_RefImplCPUReport::XOP());
|
|
DN_UTEST_ASSERT(&test, DN_CPU_HasFeature(&cpu_report, DN_CPUFeature_XSAVE) == DN_RefImplCPUReport::XSAVE());
|
|
#endif
|
|
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_Arena()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_Arena") {
|
|
DN_UTEST_TEST("Reused memory is zeroed out") {
|
|
uint8_t alignment = 1;
|
|
DN_USize alloc_size = DN_KILOBYTES(128);
|
|
DN_Arena arena = {};
|
|
DN_DEFER {
|
|
DN_Arena_Deinit(&arena);
|
|
};
|
|
|
|
// NOTE: Allocate 128 kilobytes, fill it with garbage, then reset the arena
|
|
uintptr_t first_ptr_address = 0;
|
|
{
|
|
DN_ArenaTempMem temp_mem = DN_Arena_TempMemBegin(&arena);
|
|
void *ptr = DN_Arena_Alloc(&arena, alloc_size, alignment, DN_ZeroMem_Yes);
|
|
first_ptr_address = DN_CAST(uintptr_t)ptr;
|
|
DN_MEMSET(ptr, 'z', alloc_size);
|
|
DN_Arena_TempMemEnd(temp_mem);
|
|
}
|
|
|
|
// NOTE: Reallocate 128 kilobytes
|
|
char *ptr = DN_CAST(char *)DN_Arena_Alloc(&arena, alloc_size, alignment, DN_ZeroMem_Yes);
|
|
|
|
// NOTE: Double check we got the same pointer
|
|
DN_UTEST_ASSERT(&test, first_ptr_address == DN_CAST(uintptr_t)ptr);
|
|
|
|
// NOTE: Check that the bytes are set to 0
|
|
for (DN_USize i = 0; i < alloc_size; i++)
|
|
DN_UTEST_ASSERT(&test, ptr[i] == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test arena grows naturally, 1mb + 4mb") {
|
|
// NOTE: Allocate 1mb, then 4mb, this should force the arena to grow
|
|
DN_Arena arena = DN_Arena_InitSize(DN_MEGABYTES(2), DN_MEGABYTES(2), DN_ArenaFlags_Nil);
|
|
DN_DEFER {
|
|
DN_Arena_Deinit(&arena);
|
|
};
|
|
|
|
char *ptr_1mb = DN_Arena_NewArray(&arena, char, DN_MEGABYTES(1), DN_ZeroMem_Yes);
|
|
char *ptr_4mb = DN_Arena_NewArray(&arena, char, DN_MEGABYTES(4), DN_ZeroMem_Yes);
|
|
DN_UTEST_ASSERT(&test, ptr_1mb);
|
|
DN_UTEST_ASSERT(&test, ptr_4mb);
|
|
|
|
DN_ArenaBlock const *block_4mb_begin = arena.curr;
|
|
char const *block_4mb_end = DN_CAST(char *)block_4mb_begin + block_4mb_begin->reserve;
|
|
|
|
DN_ArenaBlock const *block_1mb_begin = block_4mb_begin->prev;
|
|
DN_UTEST_ASSERTF(&test, block_1mb_begin, "New block should have been allocated");
|
|
char const *block_1mb_end = DN_CAST(char *)block_1mb_begin + block_1mb_begin->reserve;
|
|
|
|
DN_UTEST_ASSERTF(&test, block_1mb_begin != block_4mb_begin, "New block should have been allocated and linked");
|
|
DN_UTEST_ASSERTF(&test, ptr_1mb >= DN_CAST(char *)block_1mb_begin && ptr_1mb <= block_1mb_end, "Pointer was not allocated from correct memory block");
|
|
DN_UTEST_ASSERTF(&test, ptr_4mb >= DN_CAST(char *)block_4mb_begin && ptr_4mb <= block_4mb_end, "Pointer was not allocated from correct memory block");
|
|
}
|
|
|
|
DN_UTEST_TEST("Test arena grows naturally, 1mb, temp memory 4mb") {
|
|
DN_Arena arena = DN_Arena_InitSize(DN_MEGABYTES(2), DN_MEGABYTES(2), DN_ArenaFlags_Nil);
|
|
DN_DEFER {
|
|
DN_Arena_Deinit(&arena);
|
|
};
|
|
|
|
// NOTE: Allocate 1mb, then 4mb, this should force the arena to grow
|
|
char *ptr_1mb = DN_CAST(char *)DN_Arena_Alloc(&arena, DN_MEGABYTES(1), 1 /*align*/, DN_ZeroMem_Yes);
|
|
DN_UTEST_ASSERT(&test, ptr_1mb);
|
|
|
|
DN_ArenaTempMem temp_memory = DN_Arena_TempMemBegin(&arena);
|
|
{
|
|
char *ptr_4mb = DN_Arena_NewArray(&arena, char, DN_MEGABYTES(4), DN_ZeroMem_Yes);
|
|
DN_UTEST_ASSERT(&test, ptr_4mb);
|
|
|
|
DN_ArenaBlock const *block_4mb_begin = arena.curr;
|
|
char const *block_4mb_end = DN_CAST(char *) block_4mb_begin + block_4mb_begin->reserve;
|
|
|
|
DN_ArenaBlock const *block_1mb_begin = block_4mb_begin->prev;
|
|
char const *block_1mb_end = DN_CAST(char *) block_1mb_begin + block_1mb_begin->reserve;
|
|
|
|
DN_UTEST_ASSERTF(&test, block_1mb_begin != block_4mb_begin, "New block should have been allocated and linked");
|
|
DN_UTEST_ASSERTF(&test, ptr_1mb >= DN_CAST(char *)block_1mb_begin && ptr_1mb <= block_1mb_end, "Pointer was not allocated from correct memory block");
|
|
DN_UTEST_ASSERTF(&test, ptr_4mb >= DN_CAST(char *)block_4mb_begin && ptr_4mb <= block_4mb_end, "Pointer was not allocated from correct memory block");
|
|
}
|
|
DN_Arena_TempMemEnd(temp_memory);
|
|
DN_UTEST_ASSERT (&test, arena.curr->prev == nullptr);
|
|
DN_UTEST_ASSERTF(&test,
|
|
arena.curr->reserve >= DN_MEGABYTES(1),
|
|
"size=%" PRIu64 "MiB (%" PRIu64 "B), expect=%" PRIu64 "B",
|
|
(arena.curr->reserve / 1024 / 1024),
|
|
arena.curr->reserve,
|
|
DN_MEGABYTES(1));
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_Bin()
|
|
{
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_Bin") {
|
|
DN_UTEST_TEST("Convert 0x123") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("0x123"));
|
|
DN_UTEST_ASSERTF(&test, result == 0x123, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert 0xFFFF") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("0xFFFF"));
|
|
DN_UTEST_ASSERTF(&test, result == 0xFFFF, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert FFFF") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("FFFF"));
|
|
DN_UTEST_ASSERTF(&test, result == 0xFFFF, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert abCD") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("abCD"));
|
|
DN_UTEST_ASSERTF(&test, result == 0xabCD, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert 0xabCD") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("0xabCD"));
|
|
DN_UTEST_ASSERTF(&test, result == 0xabCD, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert 0x") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("0x"));
|
|
DN_UTEST_ASSERTF(&test, result == 0x0, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert 0X") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("0X"));
|
|
DN_UTEST_ASSERTF(&test, result == 0x0, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert 3") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("3"));
|
|
DN_UTEST_ASSERTF(&test, result == 3, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert f") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("f"));
|
|
DN_UTEST_ASSERTF(&test, result == 0xf, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert g") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("g"));
|
|
DN_UTEST_ASSERTF(&test, result == 0, "result: %" PRIu64, result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Convert -0x3") {
|
|
uint64_t result = DN_HexToU64(DN_STR8("-0x3"));
|
|
DN_UTEST_ASSERTF(&test, result == 0, "result: %" PRIu64, result);
|
|
}
|
|
|
|
uint32_t number = 0xd095f6;
|
|
DN_UTEST_TEST("Convert %x to string", number) {
|
|
DN_Str8 number_hex = DN_BytesToHex(tmem.arena, &number, sizeof(number));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(number_hex, DN_STR8("f695d000")), "number_hex=%.*s", DN_STR_FMT(number_hex));
|
|
}
|
|
|
|
number = 0xf6ed00;
|
|
DN_UTEST_TEST("Convert %x to string", number) {
|
|
DN_Str8 number_hex = DN_BytesToHex(tmem.arena, &number, sizeof(number));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(number_hex, DN_STR8("00edf600")), "number_hex=%.*s", DN_STR_FMT(number_hex));
|
|
}
|
|
|
|
DN_Str8 hex = DN_STR8("0xf6ed00");
|
|
DN_UTEST_TEST("Convert %.*s to bytes", DN_STR_FMT(hex)) {
|
|
DN_Str8 bytes = DN_HexToBytes(tmem.arena, hex);
|
|
DN_UTEST_ASSERTF(&test,
|
|
DN_Str8_Eq(bytes, DN_STR8("\xf6\xed\x00")),
|
|
"number_hex=%.*s",
|
|
DN_STR_FMT(DN_BytesToHex(tmem.arena, bytes.data, bytes.size)));
|
|
}
|
|
|
|
}
|
|
return test;
|
|
}
|
|
|
|
|
|
static DN_UTest DN_Test_BinarySearch()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_BinarySearch") {
|
|
DN_UTEST_TEST("Search array of 1 item") {
|
|
uint32_t array[] = {1};
|
|
DN_BinarySearchResult result = {};
|
|
|
|
// NOTE: Match =============================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
// NOTE: Lower bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
// NOTE: Upper bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
}
|
|
|
|
DN_UTEST_TEST("Search array of 2 items") {
|
|
uint32_t array[] = {1};
|
|
DN_BinarySearchResult result = {};
|
|
|
|
// NOTE: Match =============================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
// NOTE: Lower bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
// NOTE: Upper bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
}
|
|
|
|
DN_UTEST_TEST("Search array of 3 items") {
|
|
uint32_t array[] = {1, 2, 3};
|
|
DN_BinarySearchResult result = {};
|
|
|
|
// NOTE: Match =============================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 4U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 3);
|
|
|
|
// NOTE: Lower bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 4U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 3);
|
|
|
|
// NOTE: Upper bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 3);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 4U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 3);
|
|
}
|
|
|
|
DN_UTEST_TEST("Search array of 4 items") {
|
|
uint32_t array[] = {1, 2, 3, 4};
|
|
DN_BinarySearchResult result = {};
|
|
|
|
// NOTE: Match =============================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 4U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 3);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 5U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 4);
|
|
|
|
// NOTE: Lower bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 4U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 3);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 5U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 4);
|
|
|
|
// NOTE: Upper bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 1);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 3);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 4U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 4);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 5U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 4);
|
|
}
|
|
|
|
DN_UTEST_TEST("Search array with duplicate items") {
|
|
uint32_t array[] = {1, 1, 2, 2, 3};
|
|
DN_BinarySearchResult result = {};
|
|
|
|
// NOTE: Match =============================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 4);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 4U /*find*/, DN_BinarySearchType_Match);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 5);
|
|
|
|
// NOTE: Lower bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 4);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 4U /*find*/, DN_BinarySearchType_LowerBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 5);
|
|
|
|
// NOTE: Upper bound =======================================================================
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 0U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 1U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 2U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 4);
|
|
|
|
result = DN_BinarySearch<uint32_t>(array, DN_ARRAY_UCOUNT(array), 3U /*find*/, DN_BinarySearchType_UpperBound);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 5);
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_DSMap()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_DSMap") {
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
{
|
|
DN_Arena arena = {};
|
|
uint32_t const MAP_SIZE = 64;
|
|
DN_DSMap<uint64_t> map = DN_DSMap_Init<uint64_t>(&arena, MAP_SIZE, DN_DSMapFlags_Nil);
|
|
DN_DEFER { DN_DSMap_Deinit(&map, DN_ZeroMem_Yes); };
|
|
|
|
DN_UTEST_TEST("Find non-existent value") {
|
|
DN_DSMapResult<uint64_t> find = DN_DSMap_FindKeyStr8(&map, DN_STR8("Foo"));
|
|
DN_UTEST_ASSERT(&test, !find.found);
|
|
DN_UTEST_ASSERT(&test, map.size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.initial_size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.occupied == 1 /*Sentinel*/);
|
|
}
|
|
|
|
DN_DSMapKey key = DN_DSMap_KeyCStr8(&map, "Bar");
|
|
DN_UTEST_TEST("Insert value and lookup") {
|
|
uint64_t desired_value = 0xF00BAA;
|
|
uint64_t *slot_value = DN_DSMap_Set(&map, key, desired_value).value;
|
|
DN_UTEST_ASSERT(&test, slot_value);
|
|
DN_UTEST_ASSERT(&test, map.size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.initial_size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.occupied == 2);
|
|
|
|
uint64_t *value = DN_DSMap_Find(&map, key).value;
|
|
DN_UTEST_ASSERT(&test, value);
|
|
DN_UTEST_ASSERT(&test, *value == desired_value);
|
|
}
|
|
|
|
DN_UTEST_TEST("Remove key") {
|
|
DN_DSMap_Erase(&map, key);
|
|
DN_UTEST_ASSERT(&test, map.size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.initial_size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.occupied == 1 /*Sentinel*/);
|
|
}
|
|
}
|
|
|
|
enum DSMapTestType { DSMapTestType_Set, DSMapTestType_MakeSlot, DSMapTestType_Count };
|
|
for (int test_type = 0; test_type < DSMapTestType_Count; test_type++) {
|
|
DN_Str8 prefix = {};
|
|
switch (test_type) {
|
|
case DSMapTestType_Set: prefix = DN_STR8("Set"); break;
|
|
case DSMapTestType_MakeSlot: prefix = DN_STR8("Make slot"); break;
|
|
}
|
|
|
|
DN_ArenaTempMemScope temp_mem_scope = DN_ArenaTempMemScope(tmem.arena);
|
|
DN_Arena arena = {};
|
|
uint32_t const MAP_SIZE = 64;
|
|
DN_DSMap<uint64_t> map = DN_DSMap_Init<uint64_t>(&arena, MAP_SIZE, DN_DSMapFlags_Nil);
|
|
DN_DEFER { DN_DSMap_Deinit(&map, DN_ZeroMem_Yes); };
|
|
|
|
DN_UTEST_TEST("%.*s: Test growing", DN_STR_FMT(prefix)) {
|
|
uint64_t map_start_size = map.size;
|
|
uint64_t value = 0;
|
|
uint64_t grow_threshold = map_start_size * 3 / 4;
|
|
for (; map.occupied != grow_threshold; value++) {
|
|
DN_DSMapKey key = DN_DSMap_KeyU64(&map, value);
|
|
DN_UTEST_ASSERT(&test, !DN_DSMap_Find<uint64_t>(&map, key).found);
|
|
DN_DSMapResult<uint64_t> make_result = {};
|
|
if (test_type == DSMapTestType_Set) {
|
|
make_result = DN_DSMap_Set(&map, key, value);
|
|
} else {
|
|
make_result = DN_DSMap_Make(&map, key);
|
|
}
|
|
DN_UTEST_ASSERT(&test, !make_result.found);
|
|
DN_UTEST_ASSERT(&test, DN_DSMap_Find<uint64_t>(&map, key).value);
|
|
}
|
|
DN_UTEST_ASSERT(&test, map.initial_size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.size == map_start_size);
|
|
DN_UTEST_ASSERT(&test, map.occupied == 1 /*Sentinel*/ + value);
|
|
|
|
{ // NOTE: One more item should cause the table to grow by 2x
|
|
DN_DSMapKey key = DN_DSMap_KeyU64(&map, value);
|
|
DN_DSMapResult<uint64_t> make_result = {};
|
|
if (test_type == DSMapTestType_Set) {
|
|
make_result = DN_DSMap_Set(&map, key, value);
|
|
} else {
|
|
make_result = DN_DSMap_Make(&map, key);
|
|
}
|
|
|
|
value++;
|
|
DN_UTEST_ASSERT(&test, !make_result.found);
|
|
DN_UTEST_ASSERT(&test, map.size == map_start_size * 2);
|
|
DN_UTEST_ASSERT(&test, map.initial_size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.occupied == 1 /*Sentinel*/ + value);
|
|
}
|
|
}
|
|
|
|
DN_UTEST_TEST("%.*s: Check the sentinel is present", DN_STR_FMT(prefix)) {
|
|
DN_DSMapSlot<uint64_t> NIL_SLOT = {};
|
|
DN_DSMapSlot<uint64_t> sentinel = map.slots[DN_DS_MAP_SENTINEL_SLOT];
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(&sentinel, &NIL_SLOT, sizeof(NIL_SLOT)) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("%.*s: Recheck all the hash tables values after growing", DN_STR_FMT(prefix)) {
|
|
for (uint64_t index = 1 /*Sentinel*/; index < map.occupied; index++) {
|
|
DN_DSMapSlot<uint64_t> const *slot = map.slots + index;
|
|
|
|
// NOTE: Validate each slot value
|
|
uint64_t value_test = index - 1;
|
|
DN_DSMapKey key = DN_DSMap_KeyU64(&map, value_test);
|
|
DN_UTEST_ASSERT(&test, DN_DSMap_KeyEquals(slot->key, key));
|
|
if (test_type == DSMapTestType_Set) {
|
|
DN_UTEST_ASSERT(&test, slot->value == value_test);
|
|
} else {
|
|
DN_UTEST_ASSERT(&test, slot->value == 0); // NOTE: Make slot does not set the key so should be 0
|
|
}
|
|
DN_UTEST_ASSERT(&test, slot->key.hash == DN_DSMap_Hash(&map, slot->key));
|
|
|
|
// NOTE: Check the reverse lookup is correct
|
|
DN_DSMapResult<uint64_t> check = DN_DSMap_Find(&map, slot->key);
|
|
DN_UTEST_ASSERT(&test, slot->value == *check.value);
|
|
}
|
|
}
|
|
|
|
DN_UTEST_TEST("%.*s: Test shrinking", DN_STR_FMT(prefix)) {
|
|
uint64_t start_map_size = map.size;
|
|
uint64_t start_map_occupied = map.occupied;
|
|
uint64_t value = 0;
|
|
uint64_t shrink_threshold = map.size * 1 / 4;
|
|
for (; map.occupied != shrink_threshold; value++) {
|
|
DN_DSMapKey key = DN_DSMap_KeyU64(&map, value);
|
|
DN_UTEST_ASSERT(&test, DN_DSMap_Find<uint64_t>(&map, key).found);
|
|
DN_DSMap_Erase(&map, key);
|
|
DN_UTEST_ASSERT(&test, !DN_DSMap_Find<uint64_t>(&map, key).found);
|
|
}
|
|
DN_UTEST_ASSERT(&test, map.size == start_map_size);
|
|
DN_UTEST_ASSERT(&test, map.occupied == start_map_occupied - value);
|
|
|
|
{ // NOTE: One more item should cause the table to shrink by 2x
|
|
DN_DSMapKey key = DN_DSMap_KeyU64(&map, value);
|
|
DN_DSMap_Erase(&map, key);
|
|
value++;
|
|
|
|
DN_UTEST_ASSERT(&test, map.size == start_map_size / 2);
|
|
DN_UTEST_ASSERT(&test, map.occupied == start_map_occupied - value);
|
|
}
|
|
|
|
{ // NOTE: Check the sentinel is present
|
|
DN_DSMapSlot<uint64_t> NIL_SLOT = {};
|
|
DN_DSMapSlot<uint64_t> sentinel = map.slots[DN_DS_MAP_SENTINEL_SLOT];
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(&sentinel, &NIL_SLOT, sizeof(NIL_SLOT)) == 0);
|
|
}
|
|
|
|
// NOTE: Recheck all the hash table values after shrinking
|
|
for (uint64_t index = 1 /*Sentinel*/; index < map.occupied; index++) {
|
|
|
|
// NOTE: Generate the key
|
|
uint64_t value_test = value + (index - 1);
|
|
DN_DSMapKey key = DN_DSMap_KeyU64(&map, value_test);
|
|
|
|
// NOTE: Validate each slot value
|
|
DN_DSMapResult<uint64_t> find_result = DN_DSMap_Find(&map, key);
|
|
DN_UTEST_ASSERT(&test, find_result.value);
|
|
DN_UTEST_ASSERT(&test, find_result.slot->key == key);
|
|
if (test_type == DSMapTestType_Set) {
|
|
DN_UTEST_ASSERT(&test, *find_result.value == value_test);
|
|
} else {
|
|
DN_UTEST_ASSERT(&test, *find_result.value == 0); // NOTE: Make slot does not set the key so should be 0
|
|
}
|
|
DN_UTEST_ASSERT(&test, find_result.slot->key.hash == DN_DSMap_Hash(&map, find_result.slot->key));
|
|
|
|
// NOTE: Check the reverse lookup is correct
|
|
DN_DSMapResult<uint64_t> check = DN_DSMap_Find(&map, find_result.slot->key);
|
|
DN_UTEST_ASSERT(&test, *find_result.value == *check.value);
|
|
}
|
|
|
|
for (; map.occupied != 1; value++) { // NOTE: Remove all items from the table
|
|
DN_DSMapKey key = DN_DSMap_KeyU64(&map, value);
|
|
DN_UTEST_ASSERT(&test, DN_DSMap_Find<uint64_t>(&map, key).found);
|
|
DN_DSMap_Erase(&map, key);
|
|
DN_UTEST_ASSERT(&test, !DN_DSMap_Find<uint64_t>(&map, key).found);
|
|
}
|
|
DN_UTEST_ASSERT(&test, map.initial_size == MAP_SIZE);
|
|
DN_UTEST_ASSERT(&test, map.size == map.initial_size);
|
|
DN_UTEST_ASSERT(&test, map.occupied == 1 /*Sentinel*/);
|
|
}
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_FStr8()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_FStr8") {
|
|
DN_UTEST_TEST("Append too much fails") {
|
|
DN_FStr8<4> str = {};
|
|
DN_UTEST_ASSERT(&test, !DN_FStr8_Add(&str, DN_STR8("abcde")));
|
|
}
|
|
|
|
DN_UTEST_TEST("Append format string too much fails") {
|
|
DN_FStr8<4> str = {};
|
|
DN_UTEST_ASSERT(&test, !DN_FStr8_AddF(&str, "abcde"));
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_Fs()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_OS_[Path|File]") {
|
|
DN_UTEST_TEST("Make directory recursive \"abcd/efgh\"") {
|
|
DN_UTEST_ASSERTF(&test, DN_OS_MakeDir(DN_STR8("abcd/efgh")), "Failed to make directory");
|
|
DN_UTEST_ASSERTF(&test, DN_OS_DirExists(DN_STR8("abcd")), "Directory was not made");
|
|
DN_UTEST_ASSERTF(&test, DN_OS_DirExists(DN_STR8("abcd/efgh")), "Subdirectory was not made");
|
|
DN_UTEST_ASSERTF(&test, DN_OS_FileExists(DN_STR8("abcd")) == false, "This function should only return true for files");
|
|
DN_UTEST_ASSERTF(&test, DN_OS_FileExists(DN_STR8("abcd/efgh")) == false, "This function should only return true for files");
|
|
DN_UTEST_ASSERTF(&test, DN_OS_PathDelete(DN_STR8("abcd/efgh")), "Failed to delete directory");
|
|
DN_UTEST_ASSERTF(&test, DN_OS_PathDelete(DN_STR8("abcd")), "Failed to cleanup directory");
|
|
}
|
|
|
|
DN_UTEST_TEST("File write, read, copy, move and delete") {
|
|
// NOTE: Write step
|
|
DN_Str8 const SRC_FILE = DN_STR8("dqn_test_file");
|
|
DN_B32 write_result = DN_OS_WriteAll(SRC_FILE, DN_STR8("test"), nullptr);
|
|
DN_UTEST_ASSERT(&test, write_result);
|
|
DN_UTEST_ASSERT(&test, DN_OS_FileExists(SRC_FILE));
|
|
|
|
// NOTE: Read step
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
DN_Str8 read_file = DN_OS_ReadAll(tmem.arena, SRC_FILE, nullptr);
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_HasData(read_file), "Failed to load file");
|
|
DN_UTEST_ASSERTF(&test, read_file.size == 4, "File read wrong amount of bytes");
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(read_file, DN_STR8("test")), "read(%zu): %.*s", read_file.size, DN_STR_FMT(read_file));
|
|
|
|
// NOTE: Copy step
|
|
DN_Str8 const COPY_FILE = DN_STR8("dqn_test_file_copy");
|
|
DN_B32 copy_result = DN_OS_CopyFile(SRC_FILE, COPY_FILE, true /*overwrite*/, nullptr);
|
|
DN_UTEST_ASSERT(&test, copy_result);
|
|
DN_UTEST_ASSERT(&test, DN_OS_FileExists(COPY_FILE));
|
|
|
|
// NOTE: Move step
|
|
DN_Str8 const MOVE_FILE = DN_STR8("dqn_test_file_move");
|
|
DN_B32 move_result = DN_OS_MoveFile(COPY_FILE, MOVE_FILE, true /*overwrite*/, nullptr);
|
|
DN_UTEST_ASSERT(&test, move_result);
|
|
DN_UTEST_ASSERT(&test, DN_OS_FileExists(MOVE_FILE));
|
|
DN_UTEST_ASSERTF(&test, DN_OS_FileExists(COPY_FILE) == false, "Moving a file should remove the original");
|
|
|
|
// NOTE: Delete step
|
|
DN_B32 delete_src_file = DN_OS_PathDelete(SRC_FILE);
|
|
DN_B32 delete_moved_file = DN_OS_PathDelete(MOVE_FILE);
|
|
DN_UTEST_ASSERT(&test, delete_src_file);
|
|
DN_UTEST_ASSERT(&test, delete_moved_file);
|
|
|
|
// NOTE: Deleting non-existent file fails
|
|
DN_B32 delete_non_existent_src_file = DN_OS_PathDelete(SRC_FILE);
|
|
DN_B32 delete_non_existent_moved_file = DN_OS_PathDelete(MOVE_FILE);
|
|
DN_UTEST_ASSERT(&test, delete_non_existent_moved_file == false);
|
|
DN_UTEST_ASSERT(&test, delete_non_existent_src_file == false);
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_FixedArray()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_FArray") {
|
|
DN_UTEST_TEST("Initialise from raw array") {
|
|
int raw_array[] = {1, 2};
|
|
auto array = DN_FArray_Init<int, 4>(raw_array, DN_ARRAY_UCOUNT(raw_array));
|
|
DN_UTEST_ASSERT(&test, array.size == 2);
|
|
DN_UTEST_ASSERT(&test, array.data[0] == 1);
|
|
DN_UTEST_ASSERT(&test, array.data[1] == 2);
|
|
}
|
|
|
|
DN_UTEST_TEST("Erase stable 1 element from array") {
|
|
int raw_array[] = {1, 2, 3};
|
|
auto array = DN_FArray_Init<int, 4>(raw_array, DN_ARRAY_UCOUNT(raw_array));
|
|
DN_FArray_EraseRange(&array, 1 /*begin_index*/, 1 /*count*/, DN_ArrayErase_Stable);
|
|
DN_UTEST_ASSERT(&test, array.size == 2);
|
|
DN_UTEST_ASSERT(&test, array.data[0] == 1);
|
|
DN_UTEST_ASSERT(&test, array.data[1] == 3);
|
|
}
|
|
|
|
DN_UTEST_TEST("Erase unstable 1 element from array") {
|
|
int raw_array[] = {1, 2, 3};
|
|
auto array = DN_FArray_Init<int, 4>(raw_array, DN_ARRAY_UCOUNT(raw_array));
|
|
DN_FArray_EraseRange(&array, 0 /*begin_index*/, 1 /*count*/, DN_ArrayErase_Unstable);
|
|
DN_UTEST_ASSERT(&test, array.size == 2);
|
|
DN_UTEST_ASSERT(&test, array.data[0] == 3);
|
|
DN_UTEST_ASSERT(&test, array.data[1] == 2);
|
|
}
|
|
|
|
DN_UTEST_TEST("Add 1 element to array") {
|
|
int const ITEM = 2;
|
|
int raw_array[] = {1};
|
|
auto array = DN_FArray_Init<int, 4>(raw_array, DN_ARRAY_UCOUNT(raw_array));
|
|
DN_FArray_Add(&array, ITEM);
|
|
DN_UTEST_ASSERT(&test, array.size == 2);
|
|
DN_UTEST_ASSERT(&test, array.data[0] == 1);
|
|
DN_UTEST_ASSERT(&test, array.data[1] == ITEM);
|
|
}
|
|
|
|
DN_UTEST_TEST("Clear array") {
|
|
int raw_array[] = {1};
|
|
auto array = DN_FArray_Init<int, 4>(raw_array, DN_ARRAY_UCOUNT(raw_array));
|
|
DN_FArray_Clear(&array);
|
|
DN_UTEST_ASSERT(&test, array.size == 0);
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_Intrinsics()
|
|
{
|
|
DN_UTest test = {};
|
|
// TODO(dn): We don't have meaningful tests here, but since
|
|
// atomics/intrinsics are implemented using macros we ensure the macro was
|
|
// written properly with these tests.
|
|
|
|
DN_MSVC_WARNING_PUSH
|
|
|
|
// NOTE: MSVC SAL complains that we are using Interlocked functionality on
|
|
// variables it has detected as *not* being shared across threads. This is
|
|
// fine, we're just running some basic tests, so permit it.
|
|
//
|
|
// Warning 28112 is a knock-on effect of this that it doesn't like us
|
|
// reading the value of the variable that has been used in an Interlocked
|
|
// function locally.
|
|
DN_MSVC_WARNING_DISABLE(28113) // Accessing a local variable val via an Interlocked function.
|
|
DN_MSVC_WARNING_DISABLE(28112) // A variable (val) which is accessed via an Interlocked function must always be accessed via an Interlocked function. See line 759.
|
|
|
|
DN_UTEST_GROUP(test, "DN_Atomic") {
|
|
DN_UTEST_TEST("DN_Atomic_AddU32") {
|
|
uint32_t val = 0;
|
|
DN_Atomic_AddU32(&val, 1);
|
|
DN_UTEST_ASSERTF(&test, val == 1, "val: %u", val);
|
|
}
|
|
|
|
DN_UTEST_TEST("DN_Atomic_AddU64") {
|
|
uint64_t val = 0;
|
|
DN_Atomic_AddU64(&val, 1);
|
|
DN_UTEST_ASSERTF(&test, val == 1, "val: %" PRIu64, val);
|
|
}
|
|
|
|
DN_UTEST_TEST("DN_Atomic_SubU32") {
|
|
uint32_t val = 1;
|
|
DN_Atomic_SubU32(&val, 1);
|
|
DN_UTEST_ASSERTF(&test, val == 0, "val: %u", val);
|
|
}
|
|
|
|
DN_UTEST_TEST("DN_Atomic_SubU64") {
|
|
uint64_t val = 1;
|
|
DN_Atomic_SubU64(&val, 1);
|
|
DN_UTEST_ASSERTF(&test, val == 0, "val: %" PRIu64, val);
|
|
}
|
|
|
|
DN_UTEST_TEST("DN_Atomic_SetValue32") {
|
|
long a = 0;
|
|
long b = 111;
|
|
DN_Atomic_SetValue32(&a, b);
|
|
DN_UTEST_ASSERTF(&test, a == b, "a: %ld, b: %ld", a, b);
|
|
}
|
|
|
|
DN_UTEST_TEST("DN_Atomic_SetValue64") {
|
|
int64_t a = 0;
|
|
int64_t b = 111;
|
|
DN_Atomic_SetValue64(DN_CAST(uint64_t *)&a, b);
|
|
DN_UTEST_ASSERTF(&test, a == b, "a: %" PRId64 ", b: %" PRId64, a, b);
|
|
}
|
|
|
|
DN_UTest_Begin(&test, "DN_CPU_TSC");
|
|
DN_CPU_TSC();
|
|
DN_UTest_End(&test);
|
|
|
|
DN_UTest_Begin(&test, "DN_CompilerReadBarrierAndCPUReadFence");
|
|
DN_CompilerReadBarrierAndCPUReadFence;
|
|
DN_UTest_End(&test);
|
|
|
|
DN_UTest_Begin(&test, "DN_CompilerWriteBarrierAndCPUWriteFence");
|
|
DN_CompilerWriteBarrierAndCPUWriteFence;
|
|
DN_UTest_End(&test);
|
|
}
|
|
DN_MSVC_WARNING_POP
|
|
|
|
return test;
|
|
}
|
|
|
|
#if defined(DN_UNIT_TESTS_WITH_KECCAK)
|
|
DN_GCC_WARNING_PUSH
|
|
DN_GCC_WARNING_DISABLE(-Wunused-parameter)
|
|
DN_GCC_WARNING_DISABLE(-Wsign-compare)
|
|
|
|
DN_MSVC_WARNING_PUSH
|
|
DN_MSVC_WARNING_DISABLE(4244)
|
|
DN_MSVC_WARNING_DISABLE(4100)
|
|
DN_MSVC_WARNING_DISABLE(6385)
|
|
// NOTE: Keccak Reference Implementation ///////////////////////////////////////////////////////////
|
|
// A very compact Keccak implementation taken from the reference implementation
|
|
// repository
|
|
//
|
|
// https://github.com/XKCP/XKCP/blob/master/Standalone/CompactFIPS202/C/Keccak-more-compact.c
|
|
|
|
#define FOR(i,n) for(i=0; i<n; ++i)
|
|
void DN_RefImpl_Keccak_(int r, int c, const uint8_t *in, uint64_t inLen, uint8_t sfx, uint8_t *out, uint64_t outLen);
|
|
void DN_RefImpl_FIPS202_SHAKE128_(const uint8_t *in, uint64_t inLen, uint8_t *out, uint64_t outLen) { DN_RefImpl_Keccak_(1344, 256, in, inLen, 0x1F, out, outLen); }
|
|
void DN_RefImpl_FIPS202_SHAKE256_(const uint8_t *in, uint64_t inLen, uint8_t *out, uint64_t outLen) { DN_RefImpl_Keccak_(1088, 512, in, inLen, 0x1F, out, outLen); }
|
|
void DN_RefImpl_FIPS202_SHA3_224_(const uint8_t *in, uint64_t inLen, uint8_t *out) { DN_RefImpl_Keccak_(1152, 448, in, inLen, 0x06, out, 28); }
|
|
void DN_RefImpl_FIPS202_SHA3_256_(const uint8_t *in, uint64_t inLen, uint8_t *out) { DN_RefImpl_Keccak_(1088, 512, in, inLen, 0x06, out, 32); }
|
|
void DN_RefImpl_FIPS202_SHA3_384_(const uint8_t *in, uint64_t inLen, uint8_t *out) { DN_RefImpl_Keccak_(832, 768, in, inLen, 0x06, out, 48); }
|
|
void DN_RefImpl_FIPS202_SHA3_512_(const uint8_t *in, uint64_t inLen, uint8_t *out) { DN_RefImpl_Keccak_(576, 1024, in, inLen, 0x06, out, 64); }
|
|
|
|
int DN_RefImpl_LFSR86540_(uint8_t *R) { (*R)=((*R)<<1)^(((*R)&0x80)?0x71:0); return ((*R)&2)>>1; }
|
|
#define ROL(a,o) ((((uint64_t)a)<<o)^(((uint64_t)a)>>(64-o)))
|
|
|
|
static uint64_t DN_RefImpl_load64_ (const uint8_t *x) { int i; uint64_t u=0; FOR(i,8) { u<<=8; u|=x[7-i]; } return u; }
|
|
static void DN_RefImpl_store64_(uint8_t *x, uint64_t u) { int i; FOR(i,8) { x[i]=u; u>>=8; } }
|
|
static void DN_RefImpl_xor64_ (uint8_t *x, uint64_t u) { int i; FOR(i,8) { x[i]^=u; u>>=8; } }
|
|
|
|
#define rL(x,y) DN_RefImpl_load64_((uint8_t*)s+8*(x+5*y))
|
|
#define wL(x,y,l) DN_RefImpl_store64_((uint8_t*)s+8*(x+5*y),l)
|
|
#define XL(x,y,l) DN_RefImpl_xor64_((uint8_t*)s+8*(x+5*y),l)
|
|
|
|
void DN_RefImpl_Keccak_F1600(void *s)
|
|
{
|
|
int r,x,y,i,j,Y; uint8_t R=0x01; uint64_t C[5],D;
|
|
for(i=0; i<24; i++) {
|
|
/*??*/ FOR(x,5) C[x]=rL(x,0)^rL(x,1)^rL(x,2)^rL(x,3)^rL(x,4); FOR(x,5) { D=C[(x+4)%5]^ROL(C[(x+1)%5],1); FOR(y,5) XL(x,y,D); }
|
|
/*????*/ x=1; y=r=0; D=rL(x,y); FOR(j,24) { r+=j+1; Y=(2*x+3*y)%5; x=y; y=Y; C[0]=rL(x,y); wL(x,y,ROL(D,r%64)); D=C[0]; }
|
|
/*??*/ FOR(y,5) { FOR(x,5) C[x]=rL(x,y); FOR(x,5) wL(x,y,C[x]^((~C[(x+1)%5])&C[(x+2)%5])); }
|
|
/*??*/ FOR(j,7) if (DN_RefImpl_LFSR86540_(&R)) XL(0,0,(uint64_t)1<<((1<<j)-1));
|
|
}
|
|
}
|
|
|
|
void DN_RefImpl_Keccak_(int r, int c, const uint8_t *in, uint64_t inLen, uint8_t sfx, uint8_t *out, uint64_t outLen)
|
|
{
|
|
/*initialize*/ uint8_t s[200]; int R=r/8; int i,b=0; FOR(i,200) s[i]=0;
|
|
/*absorb*/ while(inLen>0) { b=(inLen<R)?inLen:R; FOR(i,b) s[i]^=in[i]; in+=b; inLen-=b; if (b==R) { DN_RefImpl_Keccak_F1600(s); b=0; } }
|
|
/*pad*/ s[b]^=sfx; if((sfx&0x80)&&(b==(R-1))) DN_RefImpl_Keccak_F1600(s); s[R-1]^=0x80; DN_RefImpl_Keccak_F1600(s);
|
|
/*squeeze*/ while(outLen>0) { b=(outLen<R)?outLen:R; FOR(i,b) out[i]=s[i]; out+=b; outLen-=b; if(outLen>0) DN_RefImpl_Keccak_F1600(s); }
|
|
}
|
|
|
|
#undef XL
|
|
#undef wL
|
|
#undef rL
|
|
#undef ROL
|
|
#undef FOR
|
|
DN_MSVC_WARNING_POP
|
|
DN_GCC_WARNING_POP
|
|
|
|
#define DN_KECCAK_IMPLEMENTATION
|
|
#include "Standalone/dqn_keccak.h"
|
|
|
|
#define DN_UTEST_HASH_X_MACRO \
|
|
DN_UTEST_HASH_X_ENTRY(SHA3_224, "SHA3-224") \
|
|
DN_UTEST_HASH_X_ENTRY(SHA3_256, "SHA3-256") \
|
|
DN_UTEST_HASH_X_ENTRY(SHA3_384, "SHA3-384") \
|
|
DN_UTEST_HASH_X_ENTRY(SHA3_512, "SHA3-512") \
|
|
DN_UTEST_HASH_X_ENTRY(Keccak_224, "Keccak-224") \
|
|
DN_UTEST_HASH_X_ENTRY(Keccak_256, "Keccak-256") \
|
|
DN_UTEST_HASH_X_ENTRY(Keccak_384, "Keccak-384") \
|
|
DN_UTEST_HASH_X_ENTRY(Keccak_512, "Keccak-512") \
|
|
DN_UTEST_HASH_X_ENTRY(Count, "Keccak-512")
|
|
|
|
enum DN_Tests__HashType
|
|
{
|
|
#define DN_UTEST_HASH_X_ENTRY(enum_val, string) Hash_##enum_val,
|
|
DN_UTEST_HASH_X_MACRO
|
|
#undef DN_UTEST_HASH_X_ENTRY
|
|
};
|
|
|
|
DN_Str8 const DN_UTEST_HASH_STRING_[] =
|
|
{
|
|
#define DN_UTEST_HASH_X_ENTRY(enum_val, string) DN_STR8(string),
|
|
DN_UTEST_HASH_X_MACRO
|
|
#undef DN_UTEST_HASH_X_ENTRY
|
|
};
|
|
|
|
void DN_Test_KeccakDispatch_(DN_UTest *test, int hash_type, DN_Str8 input)
|
|
{
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
DN_Str8 input_hex = DN_BytesToHex(tmem.arena, input.data, input.size);
|
|
|
|
switch(hash_type)
|
|
{
|
|
case Hash_SHA3_224:
|
|
{
|
|
DN_KeccakBytes28 hash = DN_SHA3_224StringToBytes28(input);
|
|
DN_KeccakBytes28 expect;
|
|
DN_RefImpl_FIPS202_SHA3_224_(DN_CAST(uint8_t *)input.data, input.size, (uint8_t *)expect.data);
|
|
DN_UTEST_ASSERTF(test,
|
|
DN_KeccakBytes28Equals(&hash, &expect),
|
|
"\ninput: %.*s"
|
|
"\nhash: %.*s"
|
|
"\nexpect: %.*s"
|
|
,
|
|
DN_STR_FMT(input_hex),
|
|
DN_KECCAK_STRING56_FMT(DN_KeccakBytes28ToHex(&hash).data),
|
|
DN_KECCAK_STRING56_FMT(DN_KeccakBytes28ToHex(&expect).data));
|
|
}
|
|
break;
|
|
|
|
case Hash_SHA3_256:
|
|
{
|
|
DN_KeccakBytes32 hash = DN_SHA3_256StringToBytes32(input);
|
|
DN_KeccakBytes32 expect;
|
|
DN_RefImpl_FIPS202_SHA3_256_(DN_CAST(uint8_t *)input.data, input.size, (uint8_t *)expect.data);
|
|
DN_UTEST_ASSERTF(test,
|
|
DN_KeccakBytes32Equals(&hash, &expect),
|
|
"\ninput: %.*s"
|
|
"\nhash: %.*s"
|
|
"\nexpect: %.*s"
|
|
,
|
|
DN_STR_FMT(input_hex),
|
|
DN_KECCAK_STRING64_FMT(DN_KeccakBytes32ToHex(&hash).data),
|
|
DN_KECCAK_STRING64_FMT(DN_KeccakBytes32ToHex(&expect).data));
|
|
}
|
|
break;
|
|
|
|
case Hash_SHA3_384:
|
|
{
|
|
DN_KeccakBytes48 hash = DN_SHA3_384StringToBytes48(input);
|
|
DN_KeccakBytes48 expect;
|
|
DN_RefImpl_FIPS202_SHA3_384_(DN_CAST(uint8_t *)input.data, input.size, (uint8_t *)expect.data);
|
|
DN_UTEST_ASSERTF(test,
|
|
DN_KeccakBytes48Equals(&hash, &expect),
|
|
"\ninput: %.*s"
|
|
"\nhash: %.*s"
|
|
"\nexpect: %.*s"
|
|
,
|
|
DN_STR_FMT(input_hex),
|
|
DN_KECCAK_STRING96_FMT(DN_KeccakBytes48ToHex(&hash).data),
|
|
DN_KECCAK_STRING96_FMT(DN_KeccakBytes48ToHex(&expect).data));
|
|
}
|
|
break;
|
|
|
|
case Hash_SHA3_512:
|
|
{
|
|
DN_KeccakBytes64 hash = DN_SHA3_512StringToBytes64(input);
|
|
DN_KeccakBytes64 expect;
|
|
DN_RefImpl_FIPS202_SHA3_512_(DN_CAST(uint8_t *)input.data, input.size, (uint8_t *)expect.data);
|
|
DN_UTEST_ASSERTF(test,
|
|
DN_KeccakBytes64Equals(&hash, &expect),
|
|
"\ninput: %.*s"
|
|
"\nhash: %.*s"
|
|
"\nexpect: %.*s"
|
|
,
|
|
DN_STR_FMT(input_hex),
|
|
DN_KECCAK_STRING128_FMT(DN_KeccakBytes64ToHex(&hash).data),
|
|
DN_KECCAK_STRING128_FMT(DN_KeccakBytes64ToHex(&expect).data));
|
|
}
|
|
break;
|
|
|
|
case Hash_Keccak_224:
|
|
{
|
|
DN_KeccakBytes28 hash = DN_Keccak224StringToBytes28(input);
|
|
DN_KeccakBytes28 expect;
|
|
DN_RefImpl_Keccak_(1152, 448, DN_CAST(uint8_t *)input.data, input.size, 0x01, (uint8_t *)expect.data, sizeof(expect));
|
|
DN_UTEST_ASSERTF(test,
|
|
DN_KeccakBytes28Equals(&hash, &expect),
|
|
"\ninput: %.*s"
|
|
"\nhash: %.*s"
|
|
"\nexpect: %.*s"
|
|
,
|
|
DN_STR_FMT(input_hex),
|
|
DN_KECCAK_STRING56_FMT(DN_KeccakBytes28ToHex(&hash).data),
|
|
DN_KECCAK_STRING56_FMT(DN_KeccakBytes28ToHex(&expect).data));
|
|
}
|
|
break;
|
|
|
|
case Hash_Keccak_256:
|
|
{
|
|
DN_KeccakBytes32 hash = DN_Keccak256StringToBytes32(input);
|
|
DN_KeccakBytes32 expect;
|
|
DN_RefImpl_Keccak_(1088, 512, DN_CAST(uint8_t *)input.data, input.size, 0x01, (uint8_t *)expect.data, sizeof(expect));
|
|
DN_UTEST_ASSERTF(test,
|
|
DN_KeccakBytes32Equals(&hash, &expect),
|
|
"\ninput: %.*s"
|
|
"\nhash: %.*s"
|
|
"\nexpect: %.*s"
|
|
,
|
|
DN_STR_FMT(input_hex),
|
|
DN_KECCAK_STRING64_FMT(DN_KeccakBytes32ToHex(&hash).data),
|
|
DN_KECCAK_STRING64_FMT(DN_KeccakBytes32ToHex(&expect).data));
|
|
}
|
|
break;
|
|
|
|
case Hash_Keccak_384:
|
|
{
|
|
DN_KeccakBytes48 hash = DN_Keccak384StringToBytes48(input);
|
|
DN_KeccakBytes48 expect;
|
|
DN_RefImpl_Keccak_(832, 768, DN_CAST(uint8_t *)input.data, input.size, 0x01, (uint8_t *)expect.data, sizeof(expect));
|
|
DN_UTEST_ASSERTF(test,
|
|
DN_KeccakBytes48Equals(&hash, &expect),
|
|
"\ninput: %.*s"
|
|
"\nhash: %.*s"
|
|
"\nexpect: %.*s"
|
|
,
|
|
DN_STR_FMT(input_hex),
|
|
DN_KECCAK_STRING96_FMT(DN_KeccakBytes48ToHex(&hash).data),
|
|
DN_KECCAK_STRING96_FMT(DN_KeccakBytes48ToHex(&expect).data));
|
|
}
|
|
break;
|
|
|
|
case Hash_Keccak_512:
|
|
{
|
|
DN_KeccakBytes64 hash = DN_Keccak512StringToBytes64(input);
|
|
DN_KeccakBytes64 expect;
|
|
DN_RefImpl_Keccak_(576, 1024, DN_CAST(uint8_t *)input.data, input.size, 0x01, (uint8_t *)expect.data, sizeof(expect));
|
|
DN_UTEST_ASSERTF(test,
|
|
DN_KeccakBytes64Equals(&hash, &expect),
|
|
"\ninput: %.*s"
|
|
"\nhash: %.*s"
|
|
"\nexpect: %.*s"
|
|
,
|
|
DN_STR_FMT(input_hex),
|
|
DN_KECCAK_STRING128_FMT(DN_KeccakBytes64ToHex(&hash).data),
|
|
DN_KECCAK_STRING128_FMT(DN_KeccakBytes64ToHex(&expect).data));
|
|
}
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
DN_UTest DN_Test_Keccak()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_Str8 const INPUTS[] = {
|
|
DN_STR8("abc"),
|
|
DN_STR8(""),
|
|
DN_STR8("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
|
|
DN_STR8("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmno"
|
|
"pqrstnopqrstu"),
|
|
};
|
|
|
|
DN_UTEST_GROUP(test, "DN_Keccak")
|
|
{
|
|
for (int hash_type = 0; hash_type < Hash_Count; hash_type++) {
|
|
DN_PCG32 rng = DN_PCG32_Init(0xd48e'be21'2af8'733d);
|
|
for (DN_Str8 input : INPUTS) {
|
|
DN_UTest_Begin(&test, "%.*s - Input: %.*s", DN_STR_FMT(DN_UTEST_HASH_STRING_[hash_type]), DN_CAST(int)DN_MIN(input.size, 54), input.data);
|
|
DN_Test_KeccakDispatch_(&test, hash_type, input);
|
|
DN_UTest_End(&test);
|
|
}
|
|
|
|
DN_UTest_Begin(&test, "%.*s - Deterministic random inputs", DN_STR_FMT(DN_UTEST_HASH_STRING_[hash_type]));
|
|
for (DN_USize index = 0; index < 128; index++) {
|
|
char src[4096] = {};
|
|
uint32_t src_size = DN_PCG32_Range(&rng, 0, sizeof(src));
|
|
|
|
for (DN_USize src_index = 0; src_index < src_size; src_index++)
|
|
src[src_index] = DN_CAST(char)DN_PCG32_Range(&rng, 0, 255);
|
|
|
|
DN_Str8 input = DN_Str8_Init(src, src_size);
|
|
DN_Test_KeccakDispatch_(&test, hash_type, input);
|
|
}
|
|
DN_UTest_End(&test);
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
#endif // defined(DN_UNIT_TESTS_WITH_KECCAK)
|
|
|
|
static DN_UTest DN_Test_M4()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_M4") {
|
|
DN_UTEST_TEST("Simple translate and scale matrix") {
|
|
DN_M4 translate = DN_M4_TranslateF(1, 2, 3);
|
|
DN_M4 scale = DN_M4_ScaleF(2, 2, 2);
|
|
DN_M4 result = DN_M4_Mul(translate, scale);
|
|
|
|
const DN_M4 EXPECT = {{
|
|
{2, 0, 0, 0},
|
|
{0, 2, 0, 0},
|
|
{0, 0, 2, 0},
|
|
{1, 2, 3, 1},
|
|
}};
|
|
|
|
DN_UTEST_ASSERTF(&test,
|
|
memcmp(result.columns, EXPECT.columns, sizeof(EXPECT)) == 0,
|
|
"\nresult =\n%s\nexpected =\n%s",
|
|
DN_M4_ColumnMajorString(result).data,
|
|
DN_M4_ColumnMajorString(EXPECT).data);
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_OS()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_OS") {
|
|
DN_UTEST_TEST("Generate secure RNG bytes with nullptr") {
|
|
DN_B32 result = DN_OS_SecureRNGBytes(nullptr, 1);
|
|
DN_UTEST_ASSERT(&test, result == false);
|
|
}
|
|
|
|
DN_UTEST_TEST("Generate secure RNG 32 bytes") {
|
|
char const ZERO[32] = {};
|
|
char buf[32] = {};
|
|
bool result = DN_OS_SecureRNGBytes(buf, DN_ARRAY_UCOUNT(buf));
|
|
DN_UTEST_ASSERT(&test, result);
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(buf, ZERO, DN_ARRAY_UCOUNT(buf)) != 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Generate secure RNG 0 bytes") {
|
|
char buf[32] = {};
|
|
buf[0] = 'Z';
|
|
DN_B32 result = DN_OS_SecureRNGBytes(buf, 0);
|
|
DN_UTEST_ASSERT(&test, result);
|
|
DN_UTEST_ASSERT(&test, buf[0] == 'Z');
|
|
}
|
|
|
|
DN_UTEST_TEST("Query executable directory") {
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
DN_Str8 result = DN_OS_EXEDir(tmem.arena);
|
|
DN_UTEST_ASSERT(&test, DN_Str8_HasData(result));
|
|
DN_UTEST_ASSERTF(&test, DN_OS_DirExists(result), "result(%zu): %.*s", result.size, DN_STR_FMT(result));
|
|
}
|
|
|
|
DN_UTEST_TEST("DN_OS_PerfCounterNow") {
|
|
uint64_t result = DN_OS_PerfCounterNow();
|
|
DN_UTEST_ASSERT(&test, result != 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Consecutive ticks are ordered") {
|
|
uint64_t a = DN_OS_PerfCounterNow();
|
|
uint64_t b = DN_OS_PerfCounterNow();
|
|
DN_UTEST_ASSERTF(&test, b >= a, "a: %" PRIu64 ", b: %" PRIu64, a, b);
|
|
}
|
|
|
|
DN_UTEST_TEST("Ticks to time are a correct order of magnitude") {
|
|
uint64_t a = DN_OS_PerfCounterNow();
|
|
uint64_t b = DN_OS_PerfCounterNow();
|
|
DN_F64 s = DN_OS_PerfCounterS(a, b);
|
|
DN_F64 ms = DN_OS_PerfCounterMs(a, b);
|
|
DN_F64 us = DN_OS_PerfCounterUs(a, b);
|
|
DN_F64 ns = DN_OS_PerfCounterNs(a, b);
|
|
DN_UTEST_ASSERTF(&test, s <= ms, "s: %f, ms: %f", s, ms);
|
|
DN_UTEST_ASSERTF(&test, ms <= us, "ms: %f, us: %f", ms, us);
|
|
DN_UTEST_ASSERTF(&test, us <= ns, "us: %f, ns: %f", us, ns);
|
|
}
|
|
}
|
|
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_Rect()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_Rect") {
|
|
DN_UTEST_TEST("No intersection") {
|
|
DN_Rect a = DN_Rect_Init2V2(DN_V2F32_Init1N(0), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect b = DN_Rect_Init2V2(DN_V2F32_Init2N(200, 0), DN_V2F32_Init2N(200, 200));
|
|
DN_Rect ab = DN_Rect_Intersection(a, b);
|
|
|
|
DN_V2F32 ab_max = ab.pos + ab.size;
|
|
DN_UTEST_ASSERTF(&test,
|
|
ab.pos.x == 0 && ab.pos.y == 0 && ab_max.x == 0 && ab_max.y == 0,
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
ab.pos.x,
|
|
ab.pos.y,
|
|
ab_max.x,
|
|
ab_max.y);
|
|
}
|
|
|
|
DN_UTEST_TEST("A's min intersects B") {
|
|
DN_Rect a = DN_Rect_Init2V2(DN_V2F32_Init2N(50, 50), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect b = DN_Rect_Init2V2(DN_V2F32_Init2N( 0, 0), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect ab = DN_Rect_Intersection(a, b);
|
|
|
|
DN_V2F32 ab_max = ab.pos + ab.size;
|
|
DN_UTEST_ASSERTF(&test,
|
|
ab.pos.x == 50 && ab.pos.y == 50 && ab_max.x == 100 && ab_max.y == 100,
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
ab.pos.x,
|
|
ab.pos.y,
|
|
ab_max.x,
|
|
ab_max.y);
|
|
}
|
|
|
|
DN_UTEST_TEST("B's min intersects A") {
|
|
DN_Rect a = DN_Rect_Init2V2(DN_V2F32_Init2N( 0, 0), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect b = DN_Rect_Init2V2(DN_V2F32_Init2N(50, 50), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect ab = DN_Rect_Intersection(a, b);
|
|
|
|
DN_V2F32 ab_max = ab.pos + ab.size;
|
|
DN_UTEST_ASSERTF(&test,
|
|
ab.pos.x == 50 && ab.pos.y == 50 && ab_max.x == 100 && ab_max.y == 100,
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
ab.pos.x,
|
|
ab.pos.y,
|
|
ab_max.x,
|
|
ab_max.y);
|
|
}
|
|
|
|
DN_UTEST_TEST("A's max intersects B") {
|
|
DN_Rect a = DN_Rect_Init2V2(DN_V2F32_Init2N(-50, -50), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect b = DN_Rect_Init2V2(DN_V2F32_Init2N( 0, 0), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect ab = DN_Rect_Intersection(a, b);
|
|
|
|
DN_V2F32 ab_max = ab.pos + ab.size;
|
|
DN_UTEST_ASSERTF(&test,
|
|
ab.pos.x == 0 && ab.pos.y == 0 && ab_max.x == 50 && ab_max.y == 50,
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
ab.pos.x,
|
|
ab.pos.y,
|
|
ab_max.x,
|
|
ab_max.y);
|
|
}
|
|
|
|
DN_UTEST_TEST("B's max intersects A") {
|
|
DN_Rect a = DN_Rect_Init2V2(DN_V2F32_Init2N( 0, 0), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect b = DN_Rect_Init2V2(DN_V2F32_Init2N(-50, -50), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect ab = DN_Rect_Intersection(a, b);
|
|
|
|
DN_V2F32 ab_max = ab.pos + ab.size;
|
|
DN_UTEST_ASSERTF(&test,
|
|
ab.pos.x == 0 && ab.pos.y == 0 && ab_max.x == 50 && ab_max.y == 50,
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
ab.pos.x,
|
|
ab.pos.y,
|
|
ab_max.x,
|
|
ab_max.y);
|
|
}
|
|
|
|
|
|
DN_UTEST_TEST("B contains A") {
|
|
DN_Rect a = DN_Rect_Init2V2(DN_V2F32_Init2N(25, 25), DN_V2F32_Init2N( 25, 25));
|
|
DN_Rect b = DN_Rect_Init2V2(DN_V2F32_Init2N( 0, 0), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect ab = DN_Rect_Intersection(a, b);
|
|
|
|
DN_V2F32 ab_max = ab.pos + ab.size;
|
|
DN_UTEST_ASSERTF(&test,
|
|
ab.pos.x == 25 && ab.pos.y == 25 && ab_max.x == 50 && ab_max.y == 50,
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
ab.pos.x,
|
|
ab.pos.y,
|
|
ab_max.x,
|
|
ab_max.y);
|
|
}
|
|
|
|
DN_UTEST_TEST("A contains B") {
|
|
DN_Rect a = DN_Rect_Init2V2(DN_V2F32_Init2N( 0, 0), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect b = DN_Rect_Init2V2(DN_V2F32_Init2N(25, 25), DN_V2F32_Init2N( 25, 25));
|
|
DN_Rect ab = DN_Rect_Intersection(a, b);
|
|
|
|
DN_V2F32 ab_max = ab.pos + ab.size;
|
|
DN_UTEST_ASSERTF(&test,
|
|
ab.pos.x == 25 && ab.pos.y == 25 && ab_max.x == 50 && ab_max.y == 50,
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
ab.pos.x,
|
|
ab.pos.y,
|
|
ab_max.x,
|
|
ab_max.y);
|
|
}
|
|
|
|
DN_UTEST_TEST("A equals B") {
|
|
DN_Rect a = DN_Rect_Init2V2(DN_V2F32_Init2N(0, 0), DN_V2F32_Init2N(100, 100));
|
|
DN_Rect b = a;
|
|
DN_Rect ab = DN_Rect_Intersection(a, b);
|
|
|
|
DN_V2F32 ab_max = ab.pos + ab.size;
|
|
DN_UTEST_ASSERTF(&test,
|
|
ab.pos.x == 0 && ab.pos.y == 0 && ab_max.x == 100 && ab_max.y == 100,
|
|
"ab = { min.x = %.2f, min.y = %.2f, max.x = %.2f. max.y = %.2f }",
|
|
ab.pos.x,
|
|
ab.pos.y,
|
|
ab_max.x,
|
|
ab_max.y);
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_Str8()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_Str8") {
|
|
DN_UTEST_TEST("Initialise with string literal w/ macro") {
|
|
DN_Str8 string = DN_STR8("AB");
|
|
DN_UTEST_ASSERTF(&test, string.size == 2, "size: %zu", string.size);
|
|
DN_UTEST_ASSERTF(&test, string.data[0] == 'A', "string[0]: %c", string.data[0]);
|
|
DN_UTEST_ASSERTF(&test, string.data[1] == 'B', "string[1]: %c", string.data[1]);
|
|
}
|
|
|
|
DN_UTEST_TEST("Initialise with format string") {
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
DN_Str8 string = DN_Str8_InitF(tmem.arena, "%s", "AB");
|
|
DN_UTEST_ASSERTF(&test, string.size == 2, "size: %zu", string.size);
|
|
DN_UTEST_ASSERTF(&test, string.data[0] == 'A', "string[0]: %c", string.data[0]);
|
|
DN_UTEST_ASSERTF(&test, string.data[1] == 'B', "string[1]: %c", string.data[1]);
|
|
DN_UTEST_ASSERTF(&test, string.data[2] == 0, "string[2]: %c", string.data[2]);
|
|
}
|
|
|
|
DN_UTEST_TEST("Copy string") {
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
DN_Str8 string = DN_STR8("AB");
|
|
DN_Str8 copy = DN_Str8_Copy(tmem.arena, string);
|
|
DN_UTEST_ASSERTF(&test, copy.size == 2, "size: %zu", copy.size);
|
|
DN_UTEST_ASSERTF(&test, copy.data[0] == 'A', "copy[0]: %c", copy.data[0]);
|
|
DN_UTEST_ASSERTF(&test, copy.data[1] == 'B', "copy[1]: %c", copy.data[1]);
|
|
DN_UTEST_ASSERTF(&test, copy.data[2] == 0, "copy[2]: %c", copy.data[2]);
|
|
}
|
|
|
|
DN_UTEST_TEST("Trim whitespace around string") {
|
|
DN_Str8 string = DN_Str8_TrimWhitespaceAround(DN_STR8(" AB "));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(string, DN_STR8("AB")), "[string=%.*s]", DN_STR_FMT(string));
|
|
}
|
|
|
|
DN_UTEST_TEST("Allocate string from arena") {
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
DN_Str8 string = DN_Str8_Alloc(tmem.arena, 2, DN_ZeroMem_No);
|
|
DN_UTEST_ASSERTF(&test, string.size == 2, "size: %zu", string.size);
|
|
}
|
|
|
|
// NOTE: DN_CStr8_Trim[Prefix/Suffix]
|
|
// ---------------------------------------------------------------------------------------------
|
|
DN_UTEST_TEST("Trim prefix with matching prefix") {
|
|
DN_Str8 input = DN_STR8("nft/abc");
|
|
DN_Str8 result = DN_Str8_TrimPrefix(input, DN_STR8("nft/"));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(result, DN_STR8("abc")), "%.*s", DN_STR_FMT(result));
|
|
}
|
|
|
|
DN_UTEST_TEST("Trim prefix with non matching prefix") {
|
|
DN_Str8 input = DN_STR8("nft/abc");
|
|
DN_Str8 result = DN_Str8_TrimPrefix(input, DN_STR8(" ft/"));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(result, input), "%.*s", DN_STR_FMT(result));
|
|
}
|
|
|
|
DN_UTEST_TEST("Trim suffix with matching suffix") {
|
|
DN_Str8 input = DN_STR8("nft/abc");
|
|
DN_Str8 result = DN_Str8_TrimSuffix(input, DN_STR8("abc"));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(result, DN_STR8("nft/")), "%.*s", DN_STR_FMT(result));
|
|
}
|
|
|
|
DN_UTEST_TEST("Trim suffix with non matching suffix") {
|
|
DN_Str8 input = DN_STR8("nft/abc");
|
|
DN_Str8 result = DN_Str8_TrimSuffix(input, DN_STR8("ab"));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(result, input), "%.*s", DN_STR_FMT(result));
|
|
}
|
|
|
|
// NOTE: DN_Str8_IsAllDigits //////////////////////////////////////////////////////////////
|
|
DN_UTEST_TEST("Is all digits fails on non-digit string") {
|
|
DN_B32 result = DN_Str8_IsAll(DN_STR8("@123string"), DN_Str8IsAll_Digits);
|
|
DN_UTEST_ASSERT(&test, result == false);
|
|
}
|
|
|
|
DN_UTEST_TEST("Is all digits fails on nullptr") {
|
|
DN_B32 result = DN_Str8_IsAll(DN_Str8_Init(nullptr, 0), DN_Str8IsAll_Digits);
|
|
DN_UTEST_ASSERT(&test, result == false);
|
|
}
|
|
|
|
DN_UTEST_TEST("Is all digits fails on nullptr w/ size") {
|
|
DN_B32 result = DN_Str8_IsAll(DN_Str8_Init(nullptr, 1), DN_Str8IsAll_Digits);
|
|
DN_UTEST_ASSERT(&test, result == false);
|
|
}
|
|
|
|
DN_UTEST_TEST("Is all digits fails on string w/ 0 size") {
|
|
char const buf[] = "@123string";
|
|
DN_B32 result = DN_Str8_IsAll(DN_Str8_Init(buf, 0), DN_Str8IsAll_Digits);
|
|
DN_UTEST_ASSERT(&test, !result);
|
|
}
|
|
|
|
DN_UTEST_TEST("Is all digits success") {
|
|
DN_B32 result = DN_Str8_IsAll(DN_STR8("23"), DN_Str8IsAll_Digits);
|
|
DN_UTEST_ASSERT(&test, DN_CAST(bool)result == true);
|
|
}
|
|
|
|
DN_UTEST_TEST("Is all digits fails on whitespace") {
|
|
DN_B32 result = DN_Str8_IsAll(DN_STR8("23 "), DN_Str8IsAll_Digits);
|
|
DN_UTEST_ASSERT(&test, DN_CAST(bool)result == false);
|
|
}
|
|
|
|
// NOTE: DN_Str8_BinarySplit
|
|
// ---------------------------------------------------------------------------------------------
|
|
{
|
|
{
|
|
char const *TEST_FMT = "Binary split \"%.*s\" with \"%.*s\"";
|
|
DN_Str8 delimiter = DN_STR8("/");
|
|
DN_Str8 input = DN_STR8("abcdef");
|
|
DN_UTEST_TEST(TEST_FMT, DN_STR_FMT(input), DN_STR_FMT(delimiter)) {
|
|
DN_Str8BinarySplitResult split = DN_Str8_BinarySplit(input, delimiter);
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(split.lhs, DN_STR8("abcdef")), "[lhs=%.*s]", DN_STR_FMT(split.lhs));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(split.rhs, DN_STR8("")), "[rhs=%.*s]", DN_STR_FMT(split.rhs));
|
|
}
|
|
|
|
input = DN_STR8("abc/def");
|
|
DN_UTEST_TEST(TEST_FMT, DN_STR_FMT(input), DN_STR_FMT(delimiter)) {
|
|
DN_Str8BinarySplitResult split = DN_Str8_BinarySplit(input, delimiter);
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(split.lhs, DN_STR8("abc")), "[lhs=%.*s]", DN_STR_FMT(split.lhs));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(split.rhs, DN_STR8("def")), "[rhs=%.*s]", DN_STR_FMT(split.rhs));
|
|
}
|
|
|
|
input = DN_STR8("/abcdef");
|
|
DN_UTEST_TEST(TEST_FMT, DN_STR_FMT(input), DN_STR_FMT(delimiter)) {
|
|
DN_Str8BinarySplitResult split = DN_Str8_BinarySplit(input, delimiter);
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(split.lhs, DN_STR8("")), "[lhs=%.*s]", DN_STR_FMT(split.lhs));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(split.rhs, DN_STR8("abcdef")), "[rhs=%.*s]", DN_STR_FMT(split.rhs));
|
|
}
|
|
}
|
|
|
|
{
|
|
DN_Str8 delimiter = DN_STR8("-=-");
|
|
DN_Str8 input = DN_STR8("123-=-456");
|
|
DN_UTEST_TEST("Binary split \"%.*s\" with \"%.*s\"", DN_STR_FMT(input), DN_STR_FMT(delimiter)) {
|
|
DN_Str8BinarySplitResult split = DN_Str8_BinarySplit(input, delimiter);
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(split.lhs, DN_STR8("123")), "[lhs=%.*s]", DN_STR_FMT(split.lhs));
|
|
DN_UTEST_ASSERTF(&test, DN_Str8_Eq(split.rhs, DN_STR8("456")), "[rhs=%.*s]", DN_STR_FMT(split.rhs));
|
|
}
|
|
}
|
|
}
|
|
|
|
// NOTE: DN_Str8_ToI64 ////////////////////////////////////////////////////////////////////
|
|
DN_UTEST_TEST("To I64: Convert null string") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_Str8_Init(nullptr, 5), 0);
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("To I64: Convert empty string") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_STR8(""), 0);
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("To I64: Convert \"1\"") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_STR8("1"), 0);
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == 1);
|
|
}
|
|
|
|
DN_UTEST_TEST("To I64: Convert \"-0\"") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_STR8("-0"), 0);
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("To I64: Convert \"-1\"") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_STR8("-1"), 0);
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == -1);
|
|
}
|
|
|
|
DN_UTEST_TEST("To I64: Convert \"1.2\"") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_STR8("1.2"), 0);
|
|
DN_UTEST_ASSERT(&test, !result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == 1);
|
|
}
|
|
|
|
DN_UTEST_TEST("To I64: Convert \"1,234\"") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_STR8("1,234"), ',');
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == 1234);
|
|
}
|
|
|
|
DN_UTEST_TEST("To I64: Convert \"1,2\"") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_STR8("1,2"), ',');
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == 12);
|
|
}
|
|
|
|
DN_UTEST_TEST("To I64: Convert \"12a3\"") {
|
|
DN_Str8ToI64Result result = DN_Str8_ToI64(DN_STR8("12a3"), 0);
|
|
DN_UTEST_ASSERT(&test, !result.success);
|
|
DN_UTEST_ASSERT(&test, result.value == 12);
|
|
}
|
|
|
|
// NOTE: DN_Str8_ToU64
|
|
// ---------------------------------------------------------------------------------------------
|
|
DN_UTEST_TEST("To U64: Convert nullptr") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_Str8_Init(nullptr, 5), 0);
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 0, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
DN_UTEST_TEST("To U64: Convert empty string") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_STR8(""), 0);
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 0, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
DN_UTEST_TEST("To U64: Convert \"1\"") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_STR8("1"), 0);
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 1, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
DN_UTEST_TEST("To U64: Convert \"-0\"") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_STR8("-0"), 0);
|
|
DN_UTEST_ASSERT(&test, !result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 0, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
DN_UTEST_TEST("To U64: Convert \"-1\"") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_STR8("-1"), 0);
|
|
DN_UTEST_ASSERT(&test, !result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 0, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
DN_UTEST_TEST("To U64: Convert \"1.2\"") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_STR8("1.2"), 0);
|
|
DN_UTEST_ASSERT(&test, !result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 1, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
DN_UTEST_TEST("To U64: Convert \"1,234\"") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_STR8("1,234"), ',');
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 1234, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
DN_UTEST_TEST("To U64: Convert \"1,2\"") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_STR8("1,2"), ',');
|
|
DN_UTEST_ASSERT(&test, result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 12, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
DN_UTEST_TEST("To U64: Convert \"12a3\"") {
|
|
DN_Str8ToU64Result result = DN_Str8_ToU64(DN_STR8("12a3"), 0);
|
|
DN_UTEST_ASSERT(&test, !result.success);
|
|
DN_UTEST_ASSERTF(&test, result.value == 12, "result: %" PRIu64, result.value);
|
|
}
|
|
|
|
// NOTE: DN_Str8_Find /////////////////////////////////////////////////////////////////////
|
|
DN_UTEST_TEST("Find: String (char) is not in buffer") {
|
|
DN_Str8 buf = DN_STR8("836a35becd4e74b66a0d6844d51f1a63018c7ebc44cf7e109e8e4bba57eefb55");
|
|
DN_Str8 find = DN_STR8("2");
|
|
DN_Str8FindResult result = DN_Str8_FindStr8(buf, find, DN_Str8EqCase_Sensitive);
|
|
DN_UTEST_ASSERT(&test, !result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 0);
|
|
DN_UTEST_ASSERT(&test, result.match.data == nullptr);
|
|
DN_UTEST_ASSERT(&test, result.match.size == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Find: String (char) is in buffer") {
|
|
DN_Str8 buf = DN_STR8("836a35becd4e74b66a0d6844d51f1a63018c7ebc44cf7e109e8e4bba57eefb55");
|
|
DN_Str8 find = DN_STR8("6");
|
|
DN_Str8FindResult result = DN_Str8_FindStr8(buf, find, DN_Str8EqCase_Sensitive);
|
|
DN_UTEST_ASSERT(&test, result.found);
|
|
DN_UTEST_ASSERT(&test, result.index == 2);
|
|
DN_UTEST_ASSERT(&test, result.match.data[0] == '6');
|
|
}
|
|
|
|
// NOTE: DN_Str8_FileNameFromPath /////////////////////////////////////////////////////////
|
|
DN_UTEST_TEST("File name from Windows path") {
|
|
DN_Str8 buf = DN_STR8("C:\\ABC\\test.exe");
|
|
DN_Str8 result = DN_Str8_FileNameFromPath(buf);
|
|
DN_UTEST_ASSERTF(&test, result == DN_STR8("test.exe"), "%.*s", DN_STR_FMT(result));
|
|
}
|
|
|
|
DN_UTEST_TEST("File name from Linux path") {
|
|
DN_Str8 buf = DN_STR8("/ABC/test.exe");
|
|
DN_Str8 result = DN_Str8_FileNameFromPath(buf);
|
|
DN_UTEST_ASSERTF(&test, result == DN_STR8("test.exe"), "%.*s", DN_STR_FMT(result));
|
|
}
|
|
|
|
// NOTE: DN_Str8_TrimPrefix
|
|
// =========================================================================================
|
|
DN_UTEST_TEST("Trim prefix") {
|
|
DN_Str8 prefix = DN_STR8("@123");
|
|
DN_Str8 buf = DN_STR8("@123string");
|
|
DN_Str8 result = DN_Str8_TrimPrefix(buf, prefix, DN_Str8EqCase_Sensitive);
|
|
DN_UTEST_ASSERT(&test, result == DN_STR8("string"));
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_TicketMutex()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_TicketMutex") {
|
|
DN_UTEST_TEST("Ticket mutex start and stop") {
|
|
// TODO: We don't have a meaningful test but since atomics are
|
|
// implemented with a macro this ensures that we test that they are
|
|
// written correctly.
|
|
DN_TicketMutex mutex = {};
|
|
DN_TicketMutex_Begin(&mutex);
|
|
DN_TicketMutex_End(&mutex);
|
|
DN_UTEST_ASSERT(&test, mutex.ticket == mutex.serving);
|
|
}
|
|
|
|
DN_UTEST_TEST("Ticket mutex start and stop w/ advanced API") {
|
|
DN_TicketMutex mutex = {};
|
|
unsigned int ticket_a = DN_TicketMutex_MakeTicket(&mutex);
|
|
unsigned int ticket_b = DN_TicketMutex_MakeTicket(&mutex);
|
|
DN_UTEST_ASSERT(&test, DN_CAST(bool)DN_TicketMutex_CanLock(&mutex, ticket_b) == false);
|
|
DN_UTEST_ASSERT(&test, DN_CAST(bool)DN_TicketMutex_CanLock(&mutex, ticket_a) == true);
|
|
|
|
DN_TicketMutex_BeginTicket(&mutex, ticket_a);
|
|
DN_TicketMutex_End(&mutex);
|
|
DN_TicketMutex_BeginTicket(&mutex, ticket_b);
|
|
DN_TicketMutex_End(&mutex);
|
|
|
|
DN_UTEST_ASSERT(&test, mutex.ticket == mutex.serving);
|
|
DN_UTEST_ASSERT(&test, mutex.ticket == ticket_b + 1);
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
static DN_UTest DN_Test_VArray()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "DN_VArray") {
|
|
{
|
|
DN_VArray<uint32_t> array = DN_VArray_InitByteSize<uint32_t>(DN_KILOBYTES(64));
|
|
DN_DEFER {
|
|
DN_VArray_Deinit(&array);
|
|
};
|
|
|
|
DN_UTEST_TEST("Test adding an array of items to the array") {
|
|
uint32_t array_literal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
|
DN_VArray_AddArray<uint32_t>(&array, array_literal, DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test stable erase, 1 item, the '2' value from the array") {
|
|
DN_VArray_EraseRange(&array, 2 /*begin_index*/, 1 /*count*/, DN_ArrayErase_Stable);
|
|
uint32_t array_literal[] = {0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test unstable erase, 1 item, the '1' value from the array") {
|
|
DN_VArray_EraseRange(&array, 1 /*begin_index*/, 1 /*count*/, DN_ArrayErase_Unstable);
|
|
uint32_t array_literal[] = {0, 15, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_ArrayErase erase_enums[] = {DN_ArrayErase_Stable, DN_ArrayErase_Unstable};
|
|
DN_UTEST_TEST("Test un/stable erase, OOB") {
|
|
for (DN_ArrayErase erase : erase_enums) {
|
|
uint32_t array_literal[] = {0, 15, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
|
|
DN_VArray_EraseRange(&array, DN_ARRAY_UCOUNT(array_literal) /*begin_index*/, DN_ARRAY_UCOUNT(array_literal) + 100 /*count*/, erase);
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
}
|
|
|
|
DN_UTEST_TEST("Test flipped begin/end index stable erase, 2 items, the '15, 3' value from the array") {
|
|
DN_VArray_EraseRange(&array, 2 /*begin_index*/, -2 /*count*/, DN_ArrayErase_Stable);
|
|
uint32_t array_literal[] = {0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test flipped begin/end index unstable erase, 2 items, the '4, 5' value from the array") {
|
|
DN_VArray_EraseRange(&array, 2 /*begin_index*/, -2 /*count*/, DN_ArrayErase_Unstable);
|
|
uint32_t array_literal[] = {0, 13, 14, 6, 7, 8, 9, 10, 11, 12};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test stable erase range, 2+1 (oob) item, the '13, 14, +1 OOB' value from the array") {
|
|
DN_VArray_EraseRange(&array, 8 /*begin_index*/, 3 /*count*/, DN_ArrayErase_Stable);
|
|
uint32_t array_literal[] = {0, 13, 14, 6, 7, 8, 9, 10};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test unstable erase range, 3+1 (oob) item, the '11, 12, +1 OOB' value from the array") {
|
|
DN_VArray_EraseRange(&array, 6 /*begin_index*/, 3 /*count*/, DN_ArrayErase_Unstable);
|
|
uint32_t array_literal[] = {0, 13, 14, 6, 7, 8};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test stable erase -overflow OOB, erasing the '0, 13' value from the array") {
|
|
DN_VArray_EraseRange(&array, 1 /*begin_index*/, -DN_ISIZE_MAX /*count*/, DN_ArrayErase_Stable);
|
|
uint32_t array_literal[] = {14, 6, 7, 8};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test unstable erase +overflow OOB, erasing the '7, 8' value from the array") {
|
|
DN_VArray_EraseRange(&array, 2 /*begin_index*/, DN_ISIZE_MAX /*count*/, DN_ArrayErase_Unstable);
|
|
uint32_t array_literal[] = {14, 6};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(array_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, array_literal, DN_ARRAY_UCOUNT(array_literal) * sizeof(array_literal[0])) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Test adding an array of items after erase") {
|
|
uint32_t array_literal[] = {0, 1, 2, 3};
|
|
DN_VArray_AddArray<uint32_t>(&array, array_literal, DN_ARRAY_UCOUNT(array_literal));
|
|
|
|
uint32_t expected_literal[] = {14, 6, 0, 1, 2, 3};
|
|
DN_UTEST_ASSERT(&test, array.size == DN_ARRAY_UCOUNT(expected_literal));
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(array.data, expected_literal, DN_ARRAY_UCOUNT(expected_literal) * sizeof(expected_literal[0])) == 0);
|
|
}
|
|
}
|
|
|
|
DN_UTEST_TEST("Array of unaligned objects are contiguously laid out in memory") {
|
|
// NOTE: Since we allocate from a virtual memory block, each time
|
|
// we request memory from the block we can demand some alignment
|
|
// on the returned pointer from the memory block. If there's
|
|
// additional alignment done in that function then we can no
|
|
// longer access the items in the array contiguously leading to
|
|
// confusing memory "corruption" errors.
|
|
//
|
|
// This test makes sure that the unaligned objects are allocated
|
|
// from the memory block (and hence the array) contiguously
|
|
// when the size of the object is not aligned with the required
|
|
// alignment of the object.
|
|
DN_MSVC_WARNING_PUSH
|
|
DN_MSVC_WARNING_DISABLE(4324) // warning C4324: 'TestVArray::UnalignedObject': structure was padded due to alignment specifier
|
|
struct alignas(8) UnalignedObject {
|
|
char data[511];
|
|
};
|
|
DN_MSVC_WARNING_POP
|
|
|
|
DN_VArray<UnalignedObject> array = DN_VArray_InitByteSize<UnalignedObject>(DN_KILOBYTES(64));
|
|
DN_DEFER {
|
|
DN_VArray_Deinit(&array);
|
|
};
|
|
|
|
// NOTE: Verify that the items returned from the data array are
|
|
// contiguous in memory.
|
|
UnalignedObject *make_item_a = DN_VArray_MakeArray(&array, 1, DN_ZeroMem_Yes);
|
|
UnalignedObject *make_item_b = DN_VArray_MakeArray(&array, 1, DN_ZeroMem_Yes);
|
|
DN_MEMSET(make_item_a->data, 'a', sizeof(make_item_a->data));
|
|
DN_MEMSET(make_item_b->data, 'b', sizeof(make_item_b->data));
|
|
DN_UTEST_ASSERT(&test, (uintptr_t)make_item_b == (uintptr_t)(make_item_a + 1));
|
|
|
|
// NOTE: Verify that accessing the items from the data array yield
|
|
// the same object.
|
|
DN_UTEST_ASSERT(&test, array.size == 2);
|
|
UnalignedObject *data_item_a = array.data + 0;
|
|
UnalignedObject *data_item_b = array.data + 1;
|
|
DN_UTEST_ASSERT(&test, (uintptr_t)data_item_b == (uintptr_t)(data_item_a + 1));
|
|
DN_UTEST_ASSERT(&test, (uintptr_t)data_item_b == (uintptr_t)(make_item_a + 1));
|
|
DN_UTEST_ASSERT(&test, (uintptr_t)data_item_b == (uintptr_t)make_item_b);
|
|
|
|
for (DN_USize i = 0; i < sizeof(data_item_a->data); i++) {
|
|
DN_UTEST_ASSERT(&test, data_item_a->data[i] == 'a');
|
|
}
|
|
|
|
for (DN_USize i = 0; i < sizeof(data_item_b->data); i++) {
|
|
DN_UTEST_ASSERT(&test, data_item_b->data[i] == 'b');
|
|
}
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
|
|
#if defined(DN_PLATFORM_WIN32)
|
|
static DN_UTest DN_Test_Win()
|
|
{
|
|
DN_UTest test = {};
|
|
DN_UTEST_GROUP(test, "OS Win32") {
|
|
DN_TLSTMem tmem = DN_TLS_TMem(nullptr);
|
|
DN_Str8 input8 = DN_STR8("String");
|
|
DN_Str16 input16 = DN_Str16{(wchar_t *)(L"String"), sizeof(L"String") / sizeof(L"String"[0]) - 1};
|
|
|
|
DN_UTEST_TEST("Str8 to Str16") {
|
|
DN_Str16 result = DN_Win_Str8ToStr16(tmem.arena, input8);
|
|
DN_UTEST_ASSERT(&test, result == input16);
|
|
}
|
|
|
|
DN_UTEST_TEST("Str16 to Str8") {
|
|
DN_Str8 result = DN_Win_Str16ToStr8(tmem.arena, input16);
|
|
DN_UTEST_ASSERT(&test, result == input8);
|
|
}
|
|
|
|
DN_UTEST_TEST("Str16 to Str8: Null terminates string") {
|
|
int size_required = DN_Win_Str16ToStr8Buffer(input16, nullptr, 0);
|
|
char *string = DN_Arena_NewArray(tmem.arena, char, size_required + 1, DN_ZeroMem_No);
|
|
|
|
// Fill the string with error sentinels
|
|
DN_MEMSET(string, 'Z', size_required + 1);
|
|
|
|
int size_returned = DN_Win_Str16ToStr8Buffer(input16, string, size_required + 1);
|
|
char const EXPECTED[] = {'S', 't', 'r', 'i', 'n', 'g', 0};
|
|
|
|
DN_UTEST_ASSERTF(&test, size_required == size_returned, "string_size: %d, result: %d", size_required, size_returned);
|
|
DN_UTEST_ASSERTF(&test, size_returned == DN_ARRAY_UCOUNT(EXPECTED) - 1, "string_size: %d, expected: %zu", size_returned, DN_ARRAY_UCOUNT(EXPECTED) - 1);
|
|
DN_UTEST_ASSERT(&test, DN_MEMCMP(EXPECTED, string, sizeof(EXPECTED)) == 0);
|
|
}
|
|
|
|
DN_UTEST_TEST("Str16 to Str8: Arena null terminates string") {
|
|
DN_Str8 string8 = DN_Win_Str16ToStr8(tmem.arena, input16);
|
|
int size_returned = DN_Win_Str16ToStr8Buffer(input16, nullptr, 0);
|
|
char const EXPECTED[] = {'S', 't', 'r', 'i', 'n', 'g', 0};
|
|
|
|
DN_UTEST_ASSERTF(&test, DN_CAST(int)string8.size == size_returned, "string_size: %d, result: %d", DN_CAST(int)string8.size, size_returned);
|
|
DN_UTEST_ASSERTF(&test, DN_CAST(int)string8.size == DN_ARRAY_UCOUNT(EXPECTED) - 1, "string_size: %d, expected: %zu", DN_CAST(int)string8.size, DN_ARRAY_UCOUNT(EXPECTED) - 1);
|
|
DN_UTEST_ASSERT (&test, DN_MEMCMP(EXPECTED, string8.data, sizeof(EXPECTED)) == 0);
|
|
}
|
|
}
|
|
return test;
|
|
}
|
|
#endif // DN_PLATFORM_WIN#@
|
|
|
|
void DN_Test_RunSuite()
|
|
{
|
|
DN_UTest tests[] =
|
|
{
|
|
DN_Test_Base(),
|
|
DN_Test_Arena(),
|
|
DN_Test_Bin(),
|
|
DN_Test_BinarySearch(),
|
|
DN_Test_DSMap(),
|
|
DN_Test_FStr8(),
|
|
DN_Test_Fs(),
|
|
DN_Test_FixedArray(),
|
|
DN_Test_Intrinsics(),
|
|
#if defined(DN_UNIT_TESTS_WITH_KECCAK)
|
|
DN_Test_Keccak(),
|
|
#endif
|
|
DN_Test_M4(),
|
|
DN_Test_OS(),
|
|
DN_Test_Rect(),
|
|
DN_Test_Str8(),
|
|
DN_Test_TicketMutex(),
|
|
DN_Test_VArray(),
|
|
#if defined(DN_PLATFORM_WIN32)
|
|
DN_Test_Win(),
|
|
#endif
|
|
};
|
|
|
|
int total_tests = 0;
|
|
int total_good_tests = 0;
|
|
for (const DN_UTest &test : tests) {
|
|
total_tests += test.num_tests_in_group;
|
|
total_good_tests += test.num_tests_ok_in_group;
|
|
}
|
|
|
|
fprintf(stdout, "Summary: %d/%d tests succeeded\n", total_good_tests, total_tests);
|
|
}
|
|
|