diff --git a/test/test_file_rw_bin.cc b/test/test_file_rw_bin.cc deleted file mode 100644 index 92f5db8..0000000 --- a/test/test_file_rw_bin.cc +++ /dev/null @@ -1,24 +0,0 @@ -// This file is under GNU General Public License 3.0 -// see LICENSE.txt -#include -#include - -#include -#include - -int main(int argc, char* argv[]) -{ - pEp::Adapter::pEpLog::set_enabled(true); - std::string filename = "test_rw.bin"; - - std::vector 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 v_in = pEp::Utils::file_read_bin(filename); - - assert(v_in == v_out); -} diff --git a/test/test_hexbin.cc b/test/test_hexbin.cc deleted file mode 100644 index 5c73458..0000000 --- a/test/test_hexbin.cc +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -#include -#include -#include - -using namespace pEp; - -int main() -{ - pEp::Adapter::pEpLog::set_enabled(true); - - { - // Valid hex string - std::string str_in{ "FFABCD00EF123200" }; - std::vector bin = Utils::hex2bin(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(str_in), "Uneven string should throw"); - } - - { - // Non-hex chars - std::string str_in{ "pEp!" }; - PITYASSERT_THROWS(Utils::hex2bin(str_in), "Invalid hex chars should throw"); - } - - pEpLog("All tests passsed"); -} \ No newline at end of file diff --git a/test/test_rand.cc b/test/test_std_utils.cc similarity index 52% rename from test/test_rand.cc rename to test/test_std_utils.cc index 122b7d5..d3ef3a7 100644 --- a/test/test_rand.cc +++ b/test/test_std_utils.cc @@ -1,8 +1,8 @@ #include #include -#include #include -#include +#include +#include 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 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 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 bin = Utils::hex2bin(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(str_in), "Uneven string should throw"); + } + + { + pEpLogH2("Test Non-hex chars throws"); + std::string str_in{ "pEp!" }; + PITYASSERT_THROWS(Utils::hex2bin(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"); +}