Browse Source

Tests: collate tests of 'std_utils'

pull/1/head
heck 3 years ago
parent
commit
847d88f98d
  1. 24
      test/test_file_rw_bin.cc
  2. 39
      test/test_hexbin.cc
  3. 72
      test/test_std_utils.cc

24
test/test_file_rw_bin.cc

@ -1,24 +0,0 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include <iostream>
#include <cassert>
#include <pEp/pEpLog.hh>
#include <pEp/std_utils.hh>
int main(int argc, char* argv[])
{
pEp::Adapter::pEpLog::set_enabled(true);
std::string filename = "test_rw.bin";
std::vector<char> v_out{};
for (int i = 0; i < 1000; i++) {
v_out.push_back(pEp::Utils::random_char(0, 255));
}
pEp::Utils::file_write_bin(filename, v_out);
std::vector<char> v_in = pEp::Utils::file_read_bin(filename);
assert(v_in == v_out);
}

39
test/test_hexbin.cc

@ -1,39 +0,0 @@
#include <pEp/std_utils.hh>
#include <pEp/pEpLog.hh>
#include <iostream>
#include <cassert>
#include <pEp/pitytest11/PityTest.hh>
using namespace pEp;
int main()
{
pEp::Adapter::pEpLog::set_enabled(true);
{
// Valid hex string
std::string str_in{ "FFABCD00EF123200" };
std::vector<unsigned char> bin = Utils::hex2bin<unsigned char>(str_in);
PITYASSERT(str_in.length() == bin.size() * 2, "Size error");
std::string str_out = pEp::Utils::bin2hex(bin);
pEpLog("Hex_IN : '" + Utils::to_lower(str_in) + "'");
pEpLog("Hex_OUT : '" + Utils::to_lower(str_out) + "'");
PITYASSERT(Utils::to_lower(str_in) == Utils::to_lower(str_out), "roundtrip failed");
}
{
// Uneven string throws
std::string str_in{ "FFA" };
PITYASSERT_THROWS(Utils::hex2bin<char>(str_in), "Uneven string should throw");
}
{
// Non-hex chars
std::string str_in{ "pEp!" };
PITYASSERT_THROWS(Utils::hex2bin<char>(str_in), "Invalid hex chars should throw");
}
pEpLog("All tests passsed");
}

72
test/test_rand.cc → test/test_std_utils.cc

@ -1,8 +1,8 @@
#include <pEp/std_utils.hh>
#include <pEp/pEpLog.hh>
#include <pEp/pitytest11/PityTest.hh>
#include <iostream>
#include <chrono>
#include <cassert>
#include <pEp/pitytest11/PityTest.hh>
using namespace pEp;
@ -15,16 +15,15 @@ int test_random_string_fast()
std::string res = Utils::random_string_fast(0, 255, len);
PITYASSERT(
res.length() == len,
"random_string_fast: wrong length: " +
std::to_string(res.length()) + " is not " + std::to_string(len));
"random_string_fast: wrong length: " + std::to_string(res.length()) + " is not " +
std::to_string(len));
}
return 0;
}
int main()
int test_rand()
{
pEp::Adapter::pEpLog::set_enabled(true);
pEpLogH1("Test random functions");
test_random_string_fast();
pEpLog("Random functions benchmark");
@ -62,3 +61,62 @@ int main()
}
return 0;
}
int test_file_rw_bin()
{
pEpLogH1("Test file_read_bin() / file_write_bin()");
std::string filename = "test_rw.bin";
std::vector<char> v_out{};
for (int i = 0; i < 1000; i++) {
v_out.push_back(pEp::Utils::random_char(0, 255));
}
pEp::Utils::file_write_bin(filename, v_out);
std::vector<char> v_in = pEp::Utils::file_read_bin(filename);
PITYASSERT(v_in == v_out, "in is not equal out");
return 0;
}
int test_hex2bin() {
// Test hex2bin
pEpLogH1("Test hex2bin");
{
pEpLogH2("Test Valid hex string");
std::string str_in{ "FFABCD00EF123200" };
std::vector<unsigned char> bin = Utils::hex2bin<unsigned char>(str_in);
PITYASSERT(str_in.length() == bin.size() * 2, "Size error");
std::string str_out = pEp::Utils::bin2hex(bin);
pEpLog("Hex_IN : '" + Utils::to_lower(str_in) + "'");
pEpLog("Hex_OUT : '" + Utils::to_lower(str_out) + "'");
PITYASSERT(Utils::to_lower(str_in) == Utils::to_lower(str_out), "roundtrip failed");
}
{
pEpLogH2("Test Uneven string throws");
std::string str_in{ "FFA" };
PITYASSERT_THROWS(Utils::hex2bin<char>(str_in), "Uneven string should throw");
}
{
pEpLogH2("Test Non-hex chars throws");
std::string str_in{ "pEp!" };
PITYASSERT_THROWS(Utils::hex2bin<char>(str_in), "Invalid hex chars should throw");
}
return 0;
}
int main()
{
pEp::Adapter::pEpLog::set_enabled(true);
test_file_rw_bin();
test_hex2bin();
test_rand();
pEpLog("All tests passsed");
}
Loading…
Cancel
Save