Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Testing #25

Merged
merged 14 commits into from
Nov 30, 2023
Merged
1 change: 1 addition & 0 deletions src/common/include/common/types.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdint> // uint64_t
#pragma once

////////////////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 7 additions & 2 deletions src/sequential/include/sequential/sequential.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <utility>
#include <vector>

#include "common/logger.hpp"

Check failure on line 10 in src/sequential/include/sequential/sequential.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/sequential/include/sequential/sequential.hpp:10:10 [clang-diagnostic-error]

'common/logger.hpp' file not found
#include "common/status.hpp"
#include "common/types.hpp"

Expand Down Expand Up @@ -102,10 +102,15 @@
ErrorType
remove(KeyType key);

std::vector<ValueType>
getElements() const;



private:
std::vector<SequentialBucket> buckets_ = {SequentialBucket()};
std::vector<SequentialBucket> buckets_{1<<20};
size_t length_ = 0;
size_t capacity_ = 1;
size_t capacity_ = 1<<20;

ErrorType
resize(size_t new_size);
Expand Down
2 changes: 2 additions & 0 deletions src/sequential/sequential.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <algorithm> // std::swap

#include "sequential/sequential.hpp"

Check failure on line 3 in src/sequential/sequential.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/src/sequential/sequential.cpp:3:10 [clang-diagnostic-error]

'sequential/sequential.hpp' file not found


////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -207,6 +207,7 @@
ErrorType e = this->resize(2 * this->capacity_);
assert(e == ErrorType::ok && "error in resize");
}

ErrorType e = insert_without_resize(this->buckets_, key, value, hashcode);
assert(e == ErrorType::ok && "error in insert_without_resize");
++this->length_;
Expand Down Expand Up @@ -298,3 +299,4 @@
return ErrorType::ok;
}


1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(performance_test)
add_subdirectory(trace_test)
add_subdirectory(unit_test)
48 changes: 48 additions & 0 deletions test/unit_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# NOTE: We include header files to make them visible to IDEs.
add_executable(unit_test
main.cpp
../../src/sequential/sequential.cpp
../../src/sequential/sequential.hpp
../../src/traces/traces.cpp
)

target_compile_options(unit_test
PRIVATE
${MM_REQUIRED_WARN_FLAGS}
${MM_EXTRA_WARN_FLAGS}
)

# CMake flags for Release builds are suboptimal.
# See: https://gitlab.kitware.com/cmake/cmake/-/issues/20812.
# See: https://stackoverflow.com/questions/28178978/how-to-generate-pdb-files-for-release-build-with-cmake-flags.
# TODO(glin): Can this be refactored into a function?
if(MSVC)
target_compile_options(unit_test
PRIVATE
$<$<CONFIG:Release>:/Zc:inline>
$<$<CONFIG:Release>:/Zi>
$<$<CONFIG:Release>:/Gy>
)
target_link_options(unit_test
PRIVATE
$<$<CONFIG:Release>:/DEBUG>
$<$<CONFIG:Release>:/INCREMENTAL:NO>
$<$<CONFIG:Release>:/OPT:REF>
$<$<CONFIG:Release>:/OPT:ICF>
)
elseif((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang"))
target_compile_options(unit_test
PRIVATE
$<$<CONFIG:Release>:-g>
)
target_link_options(unit_test
PRIVATE
$<$<CONFIG:Release>:-g>
)
if(WIN32)
target_compile_options(unit_test
PRIVATE
$<$<CONFIG:Release>:-gcodeview>
)
endif()
endif()
135 changes: 135 additions & 0 deletions test/unit_test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "../../src/traces/traces.hpp"

Check failure on line 1 in test/unit_test/main.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/test/unit_test/main.cpp:1:10 [clang-diagnostic-error]

'../../src/traces/traces.hpp' file not found
#include <vector>
#include <unordered_map>
#include <iostream>
#include "../../src/sequential/sequential.hpp"
#include "../../src/parallel/parallel.hpp"
#include <unordered_set>

#include "../../src/common/types.hpp"


std::unordered_map<KeyType, ValueType> map;
griffinandrew marked this conversation as resolved.
Show resolved Hide resolved
SequentialRobinHoodHashTable sequential_hash_table;
ParallelRobinHoodHashTable parallel_hash_table;


//run trace on unordered map for baseline comparison
void
test_traces_on_unordered_map(const std::vector<Trace>& traces)
{
for (const auto& trace : traces) {
switch (trace.op) {
case TraceOperator::insert:
map[trace.key] = trace.value;
break;
case TraceOperator::search:
map.find(trace.key);
break;
case TraceOperator::remove:
map.erase(trace.key);
break;
}
}
}

//run trace on sequential hash table for comparison
void
test_traces_on_sequential(const std::vector<Trace>& traces)
{
for (const auto& trace : traces) {
switch (trace.op) {
case TraceOperator::insert:
sequential_hash_table.insert((trace.key), (trace.value));
break;
case TraceOperator::search:
sequential_hash_table.search(trace.key);
break;
case TraceOperator::remove:
sequential_hash_table.remove(trace.key);
break;
}
}
}

//run trace on parallel hash table for comparison
void
test_traces_on_parallel(const std::vector<Trace>& traces)
{
for (const auto& trace : traces) {
switch (trace.op) {
case TraceOperator::insert:
parallel_hash_table.insert((trace.key), (trace.value));
break;
case TraceOperator::search:
parallel_hash_table.find(trace.key);
break;
case TraceOperator::remove:
parallel_hash_table.remove(trace.key);
break;
}
}
}


int
main(int argc, char** argv)
{

//generate random traces
std::vector<Trace> traces = generate_random_traces(100, 1000);

//run traces on all three hash tables
test_traces_on_unordered_map(traces);
test_traces_on_sequential(traces);
test_traces_on_parallel(traces);

bool equal = true;

//check if all values in unordered map are in sequential hash table
for (auto& element: map)
{
auto actual = sequential_hash_table.search(element.first);

if(actual.value() != element.second)
{
std::cout << "**************************" << std::endl;
std::cout << "********FAILURE**********" << std::endl;
std::cout << "********SEQUENTIAL*******" << std::endl;
std::cout << "Search failed for key " << element.first << std::endl;
std::cout << "**************************" << std::endl;
bool equal = false;
}

}

//check if all values in unordered map are in parallel hash table
for (auto& element: map)
{
auto actual = parallel_hash_table.search(element.first);

if(actual.value() != element.second)
{
std::cout << "**************************" << std::endl;
std::cout << "********FAILURE**********" << std::endl;
std::cout << "********PARALLEL*******" << std::endl;
std::cout << "Search failed for key " << element.first << std::endl;
std::cout << "**************************" << std::endl;
bool equal = false;
}

}

//if all values in unordered map are in both hash tables, then they are equal
if(equal)
{
std::cout << "**************************" << std::endl;
std::cout << "*********SUCCESS**********" << std::endl;
std::cout << "****Hash tables equal*****" << std::endl;
std::cout << "**************************" << std::endl;
}

return 0;
}