Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
- includes testing operations on G and H
  • Loading branch information
heat1q committed Nov 2, 2020
1 parent 3574604 commit 0bc44a1
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ set(BASE_SRC "src/core/gf2.cpp" "src/core/functions.cpp" "src/core/ldpc.cpp" "sr
add_executable(ldpc_sim "src/sim_cpu.cpp" ${BASE_SRC})
target_compile_definitions(ldpc_sim PRIVATE LOG_FRAME_TIME=1 ${SIM_FLAGS})

# add the executable
add_executable(ldpc_tests "tests/init.cpp" ${BASE_SRC})
target_compile_definitions(ldpc_tests PRIVATE ${SIM_FLAGS})

# shared library
add_library(ldpc SHARED "src/shared.cpp" ${BASE_SRC})
target_compile_definitions(ldpc PRIVATE LOG_FRAME_TIME=1 LIB_SHARED=1 ${SIM_FLAGS})

# specify the C++ standard
target_compile_features(ldpc_sim PRIVATE cxx_std_17)
target_compile_features(ldpc_tests PRIVATE cxx_std_17)
target_compile_features(ldpc PRIVATE cxx_std_17)
41 changes: 41 additions & 0 deletions tests/init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "../src/core/ldpc.h"
#include "../include/argparse/argparse.hpp"

#include "ldpctest.cpp"

int main(int argc, char *argv[])
{
argparse::ArgumentParser parser("ldpc_tests");
parser.add_argument("codefile").help("LDPC codefile containing all non-zero entries, compressed sparse row (CSR) format.");
parser.add_argument("-G").help("Generator matrix, compressed sparse row (CSR) format.").default_value(std::string(""));

std::string pcFile, genFile;
try
{
parser.parse_args(argc, argv);
pcFile = parser.get<std::string>("codefile");
genFile = parser.get<std::string>("-G");
}
catch (const std::runtime_error &e)
{
std::cout << e.what() << std::endl;
std::cout << parser;
exit(EXIT_FAILURE);
}

try
{
ldpc::ldpc_code code(pcFile, genFile);
ldpc_tests::gf2();
ldpc_tests::is_generator_matrix(code);
ldpc_tests::codeword(code);

std::cout << "All tests passed." << std::endl;
}
catch (const std::exception &e)
{
std::cout << "Assessment failed: " << e.what() << std::endl;
}

return 0;
}
72 changes: 72 additions & 0 deletions tests/ldpctest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "../src/core/ldpc.h"

namespace ldpc_tests
{
void gf2()
{
try
{
auto check = ldpc::bits_t(0);
if ((check * 1) != 0) throw 0;
if ((check + 1) == 0) throw 0;
if ((check + 1 + 1) != 0) throw 0;
if ((-check) == 0) throw 0;
}
catch (...)
{
throw std::runtime_error("failed: gf2 arithmetics");
}
std::cout << "passed: gf2 arithmetics" << std::endl;
}

void is_generator_matrix(const ldpc::ldpc_code &code)
{
for (ldpc::u64 i = 0; i < code.mc(); i++) // rows of H
{
for (ldpc::u64 j = 0; j < code.kc(); j++) // cols of G transpose = rows of G
{
auto &h = code.H().row_neighbor()[i];
auto &g = code.G().row_neighbor()[j];

auto sum = ldpc::bits_t(0);
for (auto hi : h)
{
for (auto gj : g)
{
sum += (hi.nodeIndex == gj.nodeIndex) *
code.G().nz_entry()[gj.edgeIndex].value *
code.H().nz_entry()[hi.edgeIndex].value;
}
}

if (sum != 0)
{
throw std::runtime_error("failed: is_generator_matrix");
}
}
}

std::cout << "passed: is_generator_matrix" << std::endl;
}

void codeword(const ldpc::ldpc_code &code)
{
ldpc::vec_bits_t u(code.kc(), 1);
for (auto &x : u)
{
x = rand() % 2;
}
auto cw = code.G().multiply_left(u);
auto synd = code.H().multiply_right(cw);

for (auto s : synd)
{
if (s != 0)
{
throw std::runtime_error("failed: encoding random information word");
}
}

std::cout << "passed: encoding random information word" << std::endl;
}
} // namespace ldpc_tests

0 comments on commit 0bc44a1

Please sign in to comment.