Fix unit tests, re-fix DqnArray::Insert

This commit is contained in:
Doyle T 2018-07-05 00:58:40 +10:00
parent a23e2687fe
commit c5d26f09c4
2 changed files with 19 additions and 18 deletions

View File

@ -1648,7 +1648,7 @@ void DqnFile_Test()
{ {
const char *const FILE_TO_OPEN = ".clang-format"; const char *const FILE_TO_OPEN = ".clang-format";
u32 expectedSize = 0; u32 expectedSize = 0;
#if defined(DQN_UNIX_IMPLEMENTATION) #if defined(DQN__IS_UNIX)
{ {
struct stat fileStat = {0}; struct stat fileStat = {0};
DQN_ASSERT(stat(FILE_TO_OPEN, &fileStat) == 0); DQN_ASSERT(stat(FILE_TO_OPEN, &fileStat) == 0);
@ -1665,7 +1665,7 @@ void DqnFile_Test()
DQN_ASSERT(size > 0); DQN_ASSERT(size > 0);
} }
#elif defined(DQN_WIN32_IMPLEMENTATION) #else
{ {
HANDLE handle = CreateFile(FILE_TO_OPEN, GENERIC_READ, 0, NULL, OPEN_EXISTING, HANDLE handle = CreateFile(FILE_TO_OPEN, GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL); FILE_ATTRIBUTE_NORMAL, NULL);

29
dqn.h
View File

@ -910,17 +910,17 @@ template<typename T> T *DqnArray<T>::Insert(isize index, T const *v, isize numIt
return nullptr; return nullptr;
} }
T *start = data + index; T *src = data + index;
T const *end = data + count; T *dest = src + numItems;
count = newCount; count = newCount;
if (start < end) if (src < dest)
memmove(start + numItems, start, numItems * sizeof(T)); memmove(dest, src, ((data + count) - src) * sizeof(T));
for (isize i = 0; i < numItems; i++) for (isize i = 0; i < numItems; i++)
start[i] = v[i]; src[i] = v[i];
return start; return src;
} }
template <typename T> void DqnArray<T>::EraseStable(isize index) template <typename T> void DqnArray<T>::EraseStable(isize index)
@ -1906,6 +1906,7 @@ DqnFixedString__VSprintf(DqnFixedString<MAX> *me, char const *fmt, va_list argLi
char *bufStart = me->str + bufOffset; char *bufStart = me->str + bufOffset;
i32 const remainingSpace = static_cast<i32>((me->str + MAX) - bufStart); i32 const remainingSpace = static_cast<i32>((me->str + MAX) - bufStart);
int result = Dqn_vsnprintf(bufStart, remainingSpace, fmt, argList); int result = Dqn_vsnprintf(bufStart, remainingSpace, fmt, argList);
me->len = result;
return result; return result;
} }
@ -2001,11 +2002,11 @@ struct DqnHashTable
template <typename T> template <typename T>
bool DqnHashTable<T>::Init(i64 const numTableEntries, DqnMemAPI *const api) bool DqnHashTable<T>::Init(i64 const numTableEntries, DqnMemAPI *const api)
{ {
isize arrayOfPtrsSize = (isize)(sizeof(*this->entries) * numTableEntries); usize arrayOfPtrsSize = sizeof(*this->entries) * numTableEntries;
auto *arrayOfPtrs = (u8 *)api->Alloc((isize)arrayOfPtrsSize); auto *arrayOfPtrs = (u8 *)api->Alloc((isize)arrayOfPtrsSize);
if (!arrayOfPtrs) return false; if (!arrayOfPtrs) return false;
isize usedEntriesSize = (isize)(sizeof(*this->usedEntries) * numTableEntries); usize usedEntriesSize = sizeof(*this->usedEntries) * numTableEntries;
auto *usedEntriesPtr = (u8 *)api->Alloc(usedEntriesSize); auto *usedEntriesPtr = (u8 *)api->Alloc(usedEntriesSize);
if (!usedEntriesPtr) if (!usedEntriesPtr)
{ {
@ -2297,7 +2298,7 @@ void DqnHashTable<T>::Free()
// Free usedEntries list // Free usedEntries list
{ {
isize sizeToFree = (isize)(sizeof(*this->usedEntries) * this->numEntries); usize sizeToFree = sizeof(*this->usedEntries) * this->numEntries;
this->memAPI->Free(this->usedEntries, sizeToFree); this->memAPI->Free(this->usedEntries, sizeToFree);
} }
@ -2316,7 +2317,7 @@ void DqnHashTable<T>::Free()
// Free the array of ptrs // Free the array of ptrs
{ {
isize sizeToFree = (isize)(ENTRY_SIZE * this->numEntries); usize sizeToFree = ENTRY_SIZE * this->numEntries;
this->memAPI->Free(this->entries, sizeToFree); this->memAPI->Free(this->entries, sizeToFree);
} }
@ -2364,10 +2365,10 @@ bool DqnHashTable<T>::ChangeNumEntries(i64 newNumEntries)
if (newNumEntries == this->numEntries) return true; if (newNumEntries == this->numEntries) return true;
Entry **newEntries = {}; Entry **newEntries = {};
isize newEntriesSize = (isize)(sizeof(*this->entries) * newNumEntries); usize newEntriesSize = sizeof(*this->entries) * newNumEntries;
i64 *newUsedEntries = {}; i64 *newUsedEntries = {};
isize newUsedEntriesSize = (isize)(sizeof(*this->usedEntries) * newNumEntries); usize newUsedEntriesSize = sizeof(*this->usedEntries) * newNumEntries;
i64 newUsedEntriesIndex = 0; i64 newUsedEntriesIndex = 0;
// NOTE: If you change allocation order, be sure to change the free order. // NOTE: If you change allocation order, be sure to change the free order.
@ -2421,13 +2422,13 @@ bool DqnHashTable<T>::ChangeNumEntries(i64 newNumEntries)
// Free the old entry list // Free the old entry list
{ {
usize freeSize = (isize)(sizeof(*this->entries) * this->numEntries); usize freeSize = sizeof(*this->entries) * this->numEntries;
this->memAPI->Free(this->entries, freeSize); this->memAPI->Free(this->entries, freeSize);
} }
// Free the old used entry list // Free the old used entry list
{ {
usize freeSize = (isize)(sizeof(*this->usedEntries) * this->numEntries); usize freeSize = sizeof(*this->usedEntries) * this->numEntries;
this->memAPI->Free(this->usedEntries, freeSize); this->memAPI->Free(this->usedEntries, freeSize);
} }