Skip to content

Commit

Permalink
containers: move open hash table tests to actual test binary (Synthst…
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphire-arches committed Jun 1, 2024
1 parent 96a78f8 commit a59e087
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 82 deletions.
7 changes: 4 additions & 3 deletions src/deluge/memory/general_memory_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,18 @@ class GeneralMemoryAllocator {
void dealloc(void* address);
void* allocExternal(uint32_t requiredSize);
void deallocExternal(void* address);

/// Returns the new size actually achieved
uint32_t shortenRight(void* address, uint32_t newSize);
/// Returns the new size actually achieved
uint32_t shortenLeft(void* address, uint32_t amountToShorten, uint32_t numBytesToMoveRightIfSuccessful = 0);

void extend(void* address, uint32_t minAmountToExtend, uint32_t idealAmountToExtend,
uint32_t* getAmountExtendedLeft, uint32_t* getAmountExtendedRight, void* thingNotToStealFrom = NULL);
uint32_t extendRightAsMuchAsEasilyPossible(void* address);
void test();
uint32_t getAllocatedSize(void* address);
void checkStack(char const* caller);
void testShorten(int32_t i);
int32_t getRegion(void* address);
void testMemoryDeallocated(void* address);

void putStealableInQueue(Stealable* stealable, StealableQueue q);
void putStealableInAppropriateQueue(Stealable* stealable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "io/debug/log.h"
#include "memory/general_memory_allocator.h"
#include "util/functions.h"
#include <cinttypes>
#include <string.h>

#define SECONDARY_MEMORY_FUNCTION_NONE 0
Expand Down Expand Up @@ -378,81 +379,3 @@ void OpenAddressingHashTableWith8bitKey::setKeyAtAddress(uint32_t key, void* add
bool OpenAddressingHashTableWith8bitKey::doesKeyIndicateEmptyBucket(uint32_t key) {
return (key == (uint32_t)0xFF);
}

#define NUM_ELEMENTS_TO_ADD 64
void OpenAddressingHashTable::test() {
uint32_t elementsAdded[NUM_ELEMENTS_TO_ADD];

uint32_t count = 0;

while (true) {
count++;
if (!(count & ((1 << 13) - 1))) {
D_PRINTLN("still going");
}

int32_t numElementsAdded = 0;

// Add a bunch of elements
while (numElementsAdded < NUM_ELEMENTS_TO_ADD) {
do {
elementsAdded[numElementsAdded] = getNoise() & 0xFF;
} while (!elementsAdded[numElementsAdded]
|| (uint8_t)elementsAdded[numElementsAdded]
== 0xFF); // Don't allow 0 - we'll use that for special test. Or 0xFF, cos that means empty

bool result = insert(elementsAdded[numElementsAdded]);
numElementsAdded++;

if (!result) {
D_PRINTLN("couldn't add element");
while (1) {
;
}
}
}

if (numElements != NUM_ELEMENTS_TO_ADD) {
D_PRINTLN("wrong numElements");
while (1) {
;
}
}

// See if it'll let us remove an element that doesn't exist
bool result = remove(0);
if (result) {
D_PRINTLN("reported successful removal of nonexistent element");
while (1) {
;
}
}

for (int32_t i = 0; i < NUM_ELEMENTS_TO_ADD; i++) {
bool result = remove(elementsAdded[i]);
if (!result) {
D_PRINTLN("remove failed. i == %d numBuckets == %d numElements == %d key == %d", i, numBuckets,
numElements, elementsAdded[i]);
while (1) {
;
}
}
}

if (numElements != 0) {
D_PRINTLN("numElements didn't return to 0");
while (1) {
;
}
}

// See if it'll let us remove an element that doesn't exist
result = remove(0);
if (result) {
D_PRINTLN("reported successful removal of element when there are no elements at all");
while (1) {
;
}
}
}
}
17 changes: 16 additions & 1 deletion tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,24 @@ file(GLOB_RECURSE deluge_SOURCES
../../src/deluge/modulation/lfo.cpp
# For value scaling
../../src/deluge/gui/menu_item/value_scaling.cpp
# Containers
../../src/deluge/gui/l10n/*
../../src/deluge/util/cfunctions.c
../../src/deluge/util/container/*
../../src/deluge/util/d_string.cpp
../../src/deluge/util/functions.cpp
)

add_executable(UnitTests
RunAllTests.cpp

scheduler_tests.cpp
lfo_tests.cpp
scale_tests.cpp
value_scaling_tests.cpp
container/open_addressing_hash_table.cpp
)

add_executable(UnitTests RunAllTests.cpp scheduler_tests.cpp lfo_tests.cpp scale_tests.cpp value_scaling_tests.cpp)
add_test(NAME UnitTests
COMMAND UnitTests)
target_sources(UnitTests PRIVATE ${deluge_SOURCES})
Expand Down
53 changes: 53 additions & 0 deletions tests/unit/container/open_addressing_hash_table.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "CppUTest/TestHarness.h"

#include "util/container/hashtable/open_addressing_hash_table.h"

TEST_GROUP(OpenHashTableTest) {
};

constexpr const uint32_t NUM_ELEMENTS_TO_ADD = 64;

template <typename HashTable>
void runTest(HashTable& table) {
uint32_t elementsAdded[NUM_ELEMENTS_TO_ADD];
int32_t numElementsAdded = 0;

// Add a bunch of elements
while (numElementsAdded < NUM_ELEMENTS_TO_ADD) {
// Don't allow 0 - we'll use that for special test. Or 0xFF, cos that means empty
elementsAdded[numElementsAdded] = numElementsAdded + 1;

CHECK(table.insert(elementsAdded[numElementsAdded]));
numElementsAdded++;
}

CHECK_EQUAL(NUM_ELEMENTS_TO_ADD, table.numElements);

// See if it'll let us remove an element that doesn't exist
CHECK_FALSE(table.remove(0));

// Clean up elements
for (unsigned int& i : elementsAdded) {
CHECK(table.remove(i));
}

CHECK_EQUAL(0, table.numElements);

// See if it'll let us remove an element that doesn't exist
CHECK_FALSE(table.remove(0));
}

TEST(OpenHashTableTest, test8bit) {
OpenAddressingHashTableWith8bitKey table;
runTest(table);
}

TEST(OpenHashTableTest, test16bit) {
OpenAddressingHashTableWith16bitKey table;
runTest(table);
}

TEST(OpenHashTableTest, test32bit) {
OpenAddressingHashTableWith32bitKey table;
runTest(table);
}
3 changes: 3 additions & 0 deletions tests/unit/mocks/audio_engine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace AudioEngine {
bool bypassCulling{false};
}
122 changes: 122 additions & 0 deletions tests/unit/mocks/general_memory_allocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "memory/general_memory_allocator.h"
#include "CppUTest/TestHarness.h"
#include <cstdlib>

// Fake allocator object
GeneralMemoryAllocator allocator;

class MockMemoryAllocator {
public:
void* alloc(uint32_t requiredSize, bool mayUseOnChipRam, bool makeStealable, void* thingNotToStealFrom) {
return malloc(requiredSize);
}

void dealloc(void* address) { free(address); }

void* allocExternal(uint32_t requiredSize) { return malloc(requiredSize); }

void deallocExternal(void* address) { free(address); }

uint32_t shortenRight(void* address, uint32_t newSize) {
/* noop on mock allocator */
/// TODO: integrate some sort of sanity checking to make this actually check whether hte call is valid
return 0;
}

uint32_t shortenLeft(void* address, uint32_t amountToShorten, uint32_t numBytesToMoveRightIfSuccessful = 0) {
// TODO: implementing this requires actually tracking allocations so we know the size.
CHECK(false);
return -1;
}

void extend(void* address, uint32_t minAmountToExtend, uint32_t idealAmountToExtend,
uint32_t* getAmountExtendedLeft, uint32_t* getAmountExtendedRight, void* thingNotToStealFrom = NULL) {
// TODO: implementing this requires actually tracking allocations so we know the starting size.
CHECK(false);
}

uint32_t extendRightAsMuchAsEasilyPossible(void* address) {
// TODO: implementing this requires actually tracking allocations so we know the starting size.
CHECK(false);
return -1;
}

uint32_t getAllocatedSize(void* address) {
// TODO: implementing this requires actually tracking allocations so we know the starting size.
CHECK(false);
return -1;
}

void checkStack(char const* caller) { /* noop in tests */
}

int32_t getRegion(void* address) {
// TODO: we should expose an API to allow mocking this
return 0;
}
};

MockMemoryAllocator mockAllocator;

MemoryRegion::MemoryRegion() = default;
GeneralMemoryAllocator::GeneralMemoryAllocator()= default;

void* GeneralMemoryAllocator::alloc(uint32_t requiredSize, bool mayUseOnChipRam, bool makeStealable,
void* thingNotToStealFrom) {
return mockAllocator.alloc(requiredSize, mayUseOnChipRam, makeStealable, thingNotToStealFrom);
}

void GeneralMemoryAllocator::dealloc(void* address) {
return mockAllocator.dealloc(address);
}

void* GeneralMemoryAllocator::allocExternal(uint32_t requiredSize) {
return mockAllocator.allocExternal(requiredSize);
}

void GeneralMemoryAllocator::deallocExternal(void* address) {
return mockAllocator.deallocExternal(address);
}

uint32_t GeneralMemoryAllocator::shortenRight(void* address, uint32_t newSize) {
return mockAllocator.shortenRight(address, newSize);
}

uint32_t GeneralMemoryAllocator::shortenLeft(void* address, uint32_t amountToShorten,
uint32_t numBytesToMoveRightIfSuccessful) {
return mockAllocator.shortenLeft(address, amountToShorten, numBytesToMoveRightIfSuccessful);
}

void GeneralMemoryAllocator::extend(void* address, uint32_t minAmountToExtend, uint32_t idealAmountToExtend,
uint32_t* getAmountExtendedLeft, uint32_t* getAmountExtendedRight,
void* thingNotToStealFrom) {
return mockAllocator.extend(address, minAmountToExtend, idealAmountToExtend, getAmountExtendedLeft,
getAmountExtendedRight, thingNotToStealFrom);
}

uint32_t GeneralMemoryAllocator::extendRightAsMuchAsEasilyPossible(void* address) {
return mockAllocator.extendRightAsMuchAsEasilyPossible(address);
}

uint32_t GeneralMemoryAllocator::getAllocatedSize(void* address) {
return mockAllocator.getAllocatedSize(address);
}

void GeneralMemoryAllocator::checkStack(char const* caller) {
return mockAllocator.checkStack(caller);
}

int32_t GeneralMemoryAllocator::getRegion(void* address) {
return mockAllocator.getRegion(address);
}

extern "C" {

void* delugeAlloc(unsigned int requiredSize, bool mayUseOnChipRam) {
return GeneralMemoryAllocator::get().alloc(requiredSize, mayUseOnChipRam, false, nullptr);
}

void delugeDealloc(void* address) {
GeneralMemoryAllocator::get().dealloc(address);
}
}
15 changes: 15 additions & 0 deletions tests/unit/mocks/mock_defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cstdint>
int32_t __sdram_bss_end = 0;
int32_t program_stack_start = 0;
int32_t program_stack_end = 0;
int32_t __heap_start = 0;
uint32_t currentUIMode = 0;

#define NUM_ENCODERS 6
#define NUM_FUNCTION_ENCODERS 4
#define ENCODER_SCROLL_X 1
#define ENCODER_SCROLL_Y 0
#define ENCODER_TEMPO 3
#define ENCODER_SELECT 2
#define ENCODER_MOD_0 5
#define ENCODER_MOD_1 4
Loading

0 comments on commit a59e087

Please sign in to comment.