From 759ca4b896ce00bc3a76950f561a446eb04129ba Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Sat, 13 Jun 2020 13:28:42 +0200 Subject: [PATCH] added tests for Simplex and Hadamard encoders --- tests/hadamard_encoder_test.cc | 68 ++++++++++++++++++++++++++++++++++ tests/simplex_encoder_test.cc | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 tests/hadamard_encoder_test.cc create mode 100644 tests/simplex_encoder_test.cc diff --git a/tests/hadamard_encoder_test.cc b/tests/hadamard_encoder_test.cc new file mode 100644 index 0000000..d6e522c --- /dev/null +++ b/tests/hadamard_encoder_test.cc @@ -0,0 +1,68 @@ +/* +Test for the augmented Hadamard codes encoder + +Copyright 2020 Ahmet Inan +*/ + +#include +#include +#include "hadamard_encoder.hh" + +template +void hadamard_test() +{ + CODE::HadamardEncoder encode; + const int W = 1 << K; + const int N = 1 << (K - 1); + const int D = 1 << (K - 2); + int hist[N+1] = { 0 }; + for (int j = 0; j < W; ++j) { + int8_t cj[N]; + encode(cj, j); + for (int i = j + 1; i < W; ++i) { + int8_t ci[N]; + encode(ci, i); + int d = 0; + for (int n = 0; n < N; ++n) + d += ci[n] != cj[n]; + ++hist[d]; + } + } + std::cout << "Hadamard(" << N << ", " << K << ", " << D << ") hist:"; + for (int i = 0; i < N + 1; ++i) + if (hist[i]) + std::cout << " " << i << ":" << hist[i]; + std::cout << std::endl; + int d = 0; + while (!hist[d]) + ++d; + assert(d == D); +} + +int main() +{ + if (1) { + hadamard_test<2>(); + } + if (1) { + hadamard_test<3>(); + } + if (1) { + hadamard_test<4>(); + } + if (1) { + hadamard_test<5>(); + } + if (1) { + hadamard_test<6>(); + } + if (1) { + hadamard_test<7>(); + } + if (1) { + hadamard_test<8>(); + } + std::cerr << "Hadamard encoder test passed!" << std::endl; + return 0; +} + diff --git a/tests/simplex_encoder_test.cc b/tests/simplex_encoder_test.cc new file mode 100644 index 0000000..7ef1542 --- /dev/null +++ b/tests/simplex_encoder_test.cc @@ -0,0 +1,68 @@ +/* +Test for the Simplex codes encoder + +Copyright 2020 Ahmet Inan +*/ + +#include +#include +#include "simplex_encoder.hh" + +template +void simplex_test() +{ + CODE::SimplexEncoder encode; + const int W = 1 << K; + const int N = (1 << K) - 1; + const int D = 1 << (K - 1); + int hist[N+1] = { 0 }; + for (int j = 0; j < W; ++j) { + int8_t cj[N]; + encode(cj, j); + for (int i = j + 1; i < W; ++i) { + int8_t ci[N]; + encode(ci, i); + int d = 0; + for (int n = 0; n < N; ++n) + d += ci[n] != cj[n]; + ++hist[d]; + } + } + std::cout << "Simplex(" << N << ", " << K << ", " << D << ") hist:"; + for (int i = 0; i < N + 1; ++i) + if (hist[i]) + std::cout << " " << i << ":" << hist[i]; + std::cout << std::endl; + int d = 0; + while (!hist[d]) + ++d; + assert(d == D); +} + +int main() +{ + if (1) { + simplex_test<2>(); + } + if (1) { + simplex_test<3>(); + } + if (1) { + simplex_test<4>(); + } + if (1) { + simplex_test<5>(); + } + if (1) { + simplex_test<6>(); + } + if (1) { + simplex_test<7>(); + } + if (1) { + simplex_test<8>(); + } + std::cerr << "Simplex encoder test passed!" << std::endl; + return 0; +} +