Skip to content

Commit

Permalink
Convert custom tests to gtest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvsku committed Apr 19, 2024
1 parent 368ee93 commit 49260c1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 169 deletions.
2 changes: 2 additions & 0 deletions dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ IF(EVP_TEST AND PROJECT_IS_TOP_LEVEL)
)

FetchContent_MakeAvailable(googletest)

SET(GTEST_INCLUDES "${googletest_SOURCE_DIR}/googletest/include" PARENT_SCOPE)
ENDIF()
27 changes: 17 additions & 10 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
INCLUDE_DIRECTORIES("${EVP_ROOT}")
INCLUDE_DIRECTORIES("${EVP_HEADERS}")
INCLUDE_DIRECTORIES("${GTEST_INCLUDES}")

INCLUDE(GoogleTest)

ADD_COMPILE_DEFINITIONS("BASE_PATH=\"${EVP_ROOT}\"")
LINK_LIBRARIES(libevp)
LINK_LIBRARIES(GTest::gtest_main)

ADD_EXECUTABLE(test_v1_packing v1/test_packing.cpp)
ADD_EXECUTABLE(test_v1_packing
"v1/test_packing.cpp"
)

ADD_TEST(NAME v1_packing_single_file COMMAND test_v1_packing 0 ${CMAKE_SOURCE_DIR})
ADD_TEST(NAME v1_packing_folder COMMAND test_v1_packing 1 ${CMAKE_SOURCE_DIR})
gtest_discover_tests(test_v1_packing)

ADD_EXECUTABLE(test_v1_unpacking v1/test_unpacking.cpp)
ADD_EXECUTABLE(test_v1_unpacking
"v1/test_unpacking.cpp"
)

ADD_TEST(NAME v1_unpacking_single_file COMMAND test_v1_unpacking 0 ${CMAKE_SOURCE_DIR})
ADD_TEST(NAME v1_unpacking_folder COMMAND test_v1_unpacking 1 ${CMAKE_SOURCE_DIR})
ADD_TEST(NAME v1_get_file_from_evp COMMAND test_v1_unpacking 2 ${CMAKE_SOURCE_DIR})
ADD_TEST(NAME v1_get_file_from_evp_stream COMMAND test_v1_unpacking 3 ${CMAKE_SOURCE_DIR})
gtest_discover_tests(test_v1_unpacking)

ADD_EXECUTABLE(test_misc v1/test_misc.cpp)
ADD_EXECUTABLE(test_misc
"v1/test_misc.cpp"
)

ADD_TEST(NAME v1_get_evp_file_list COMMAND test_misc 0 ${CMAKE_SOURCE_DIR})
gtest_discover_tests(test_misc)
31 changes: 9 additions & 22 deletions test/v1/test_misc.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
#include "libevp.hpp"
#include "test/utilities_assert.hpp"
#include <libevp.hpp>
#include <gtest/gtest.h>

using namespace libevp;

int v1_get_evp_file_list(const std::string& base) {
std::string input = base + std::string("/test/v1/resources/valid_folders.evp");
TEST(misc, get_evp_file_list) {
std::string input = BASE_PATH + std::string("/test/v1/resources/valid_folders.evp");

auto files = evp::get_evp_file_list(input);

ASSERT(files.size() == 4);
ASSERT(files[0].generic_string() == "subfolder_1/text_1.txt");
ASSERT(files[1].generic_string() == "subfolder_1/text_2.txt");
ASSERT(files[2].generic_string() == "subfolder_2/text_3.txt");
ASSERT(files[3].generic_string() == "text_1.txt");

return 0;
EXPECT_TRUE(files.size() == 4);
EXPECT_TRUE(files[0].generic_string() == "subfolder_1/text_1.txt");
EXPECT_TRUE(files[1].generic_string() == "subfolder_1/text_2.txt");
EXPECT_TRUE(files[2].generic_string() == "subfolder_2/text_3.txt");
EXPECT_TRUE(files[3].generic_string() == "text_1.txt");
}

int main(int argc, char* argv[]) {
if (argc == 3) {
switch (std::stoi(argv[1])) {
case 0: return v1_get_evp_file_list(argv[2]);
default: break;
}
}

return 0;
}
72 changes: 20 additions & 52 deletions test/v1/test_packing.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "libevp.hpp"
#include "test/utilities_assert.hpp"
#include <libevp.hpp>
#include <gtest/gtest.h>

#include <fstream>
#include <iterator>
Expand All @@ -8,7 +8,7 @@

using namespace libevp;

bool compare_files(const std::string& p1, const std::string& p2) {
static bool compare_files(const std::string& p1, const std::string& p2) {
std::ifstream f1(p1, std::ifstream::binary | std::ifstream::ate);
std::ifstream f2(p2, std::ifstream::binary | std::ifstream::ate);

Expand All @@ -27,60 +27,28 @@ bool compare_files(const std::string& p1, const std::string& p2) {
std::istreambuf_iterator<char>(f2.rdbuf()));
}

int v1_packing_single_file(const std::string& base) {
std::string input = base + std::string("/test/v1/resources/files_to_pack/subfolder_2");
std::string output = base + std::string("/test/v1/resources/v1_packing_single_file.evp");
std::string valid = base + std::string("/test/v1/resources/valid_single_file.evp");
TEST(packing, v1_packing_single_file) {
std::string input = BASE_PATH + std::string("/test/v1/resources/files_to_pack/subfolder_2");
std::string output = BASE_PATH + std::string("/test/v1/resources/v1_packing_single_file.evp");
std::string valid = BASE_PATH + std::string("/test/v1/resources/valid_single_file.evp");

try {
auto r1 = evp::pack(input, output);
auto r1 = evp::pack(input, output);

if (!r1)
std::remove(output.c_str());
EXPECT_TRUE(r1.status == evp_result::e_status::ok);
EXPECT_TRUE(compare_files(output, valid));

ASSERT(r1);

auto r2 = compare_files(output, valid);
std::remove(output.c_str());

ASSERT(r2 == true);
}
catch (...) { return 1; }

return 0;
std::remove(output.c_str());
}

int v1_packing_folder(const std::string& base) {
std::string input = base + std::string("/test/v1/resources/files_to_pack");
std::string output = base + std::string("/test/v1/resources/v1_packing_folder.evp");
std::string valid = base + std::string("/test/v1/resources/valid_folders.evp");

try {
auto r1 = evp::pack(input, output);

if (!r1)
std::remove(output.c_str());
TEST(packing, v1_packing_folder) {
std::string input = BASE_PATH + std::string("/test/v1/resources/files_to_pack");
std::string output = BASE_PATH + std::string("/test/v1/resources/v1_packing_folder.evp");
std::string valid = BASE_PATH + std::string("/test/v1/resources/valid_folders.evp");

ASSERT(r1);
auto r1 = evp::pack(input, output);

auto r2 = compare_files(output, valid);
std::remove(output.c_str());

ASSERT(r2 == true);
}
catch (...) { return 1; }

return 0;
EXPECT_TRUE(r1.status == evp_result::e_status::ok);
EXPECT_TRUE(compare_files(output, valid));

std::remove(output.c_str());
}

int main(int argc, char* argv[]) {
if (argc == 3) {
switch (std::stoi(argv[1])) {
case 0: return v1_packing_single_file(argv[2]);
case 1: return v1_packing_folder(argv[2]);
default: break;
}
}

return 0;
}
122 changes: 37 additions & 85 deletions test/v1/test_unpacking.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "libevp.hpp"
#include "test/utilities_assert.hpp"
#include <libevp.hpp>
#include <gtest/gtest.h>

#include <fstream>
#include <iterator>
Expand All @@ -9,7 +9,7 @@

using namespace libevp;

bool compare_files(const std::string& p1, const std::string& p2) {
static bool compare_files(const std::string& p1, const std::string& p2) {
std::ifstream f1(p1, std::ifstream::binary | std::ifstream::ate);
std::ifstream f2(p2, std::ifstream::binary | std::ifstream::ate);

Expand All @@ -28,129 +28,81 @@ bool compare_files(const std::string& p1, const std::string& p2) {
std::istreambuf_iterator<char>(f2.rdbuf()));
}

bool compare_buffers(const std::vector<uint8_t>& b1, const std::vector<uint8_t>& b2) {
static bool compare_buffers(const std::vector<uint8_t>& b1, const std::vector<uint8_t>& b2) {
if (b1.size() != b2.size())
return false;

return std::equal(b1.begin(), b1.end(), b2.begin());
}

int v1_unpacking_single_file(const std::string& base) {
std::string input = base + std::string("/test/v1/resources/valid_single_file.evp");
std::string output = base + std::string("/test/v1/resources/unpack_here/");
std::string valid = base + std::string("/test/v1/resources/files_to_pack/subfolder_2/text_3.txt");
TEST(unpacking, v1_unpacking_single_file) {
std::string input = BASE_PATH + std::string("/test/v1/resources/valid_single_file.evp");
std::string output = BASE_PATH + std::string("/test/v1/resources/unpack_here/");
std::string valid = BASE_PATH + std::string("/test/v1/resources/files_to_pack/subfolder_2/text_3.txt");
std::string output_file = BASE_PATH + std::string("/test/v1/resources/unpack_here/text_3.txt");

std::filesystem::create_directories(output);

std::string output_file = base + std::string("/test/v1/resources/unpack_here/text_3.txt");
auto r1 = evp::unpack(input, output);

try {
auto r1 = evp::unpack(input, output);
EXPECT_TRUE(r1.status == evp_result::e_status::ok);
EXPECT_TRUE(compare_files(output_file, valid));

if (!r1)
std::filesystem::remove_all(output);

ASSERT(r1);

auto r2 = compare_files(output_file, valid);
std::filesystem::remove_all(output);

ASSERT(r2);
}
catch (...) { return 1; }

return 0;
std::filesystem::remove_all(output);
}

int v1_unpacking_folder(const std::string& base) {
std::string input = base + std::string("/test/v1/resources/valid_folders.evp");
std::string output = base + std::string("/test/v1/resources/unpack_here/");
std::string valid = base + std::string("/test/v1/resources/files_to_pack/");
TEST(unpacking, v1_unpacking_folder) {
std::string input = BASE_PATH + std::string("/test/v1/resources/valid_folders.evp");
std::string output = BASE_PATH + std::string("/test/v1/resources/unpack_here/");
std::string valid = BASE_PATH + std::string("/test/v1/resources/files_to_pack/");

std::filesystem::create_directories(output);

try {
auto r1 = evp::unpack(input, output);
auto r1 = evp::unpack(input, output);
EXPECT_TRUE(r1.status == evp_result::e_status::ok);

if (!r1)
std::filesystem::remove_all(output);
for (auto const& dir_entry : std::filesystem::recursive_directory_iterator(output)) {
if (std::filesystem::is_regular_file(dir_entry.path())) {
auto relative_path = std::regex_replace(dir_entry.path().generic_string(), std::regex(output), "");
std::string valid_file = valid + relative_path;

ASSERT(r1);

for (auto const& dir_entry : std::filesystem::recursive_directory_iterator(output)) {
if (std::filesystem::is_regular_file(dir_entry.path())) {
auto relative_path = std::regex_replace(dir_entry.path().generic_string(), std::regex(output), "");
std::string valid_file = valid + relative_path;

auto r2 = compare_files(dir_entry.path().generic_string(), valid_file);

if(!r2)
std::filesystem::remove_all(output);

ASSERT(r2);
}
EXPECT_TRUE(compare_files(dir_entry.path().generic_string(), valid_file));
}

std::filesystem::remove_all(output);
}
catch (...) { return 1; }

return 0;
std::filesystem::remove_all(output);
}

int v1_get_file_from_evp(const std::string& base) {
std::string input = base + std::string("/test/v1/resources/valid_folders.evp");
std::string valid = base + std::string("/test/v1/resources/files_to_pack/text_1.txt");
TEST(unpacking, v1_get_file_from_evp) {
std::string input = BASE_PATH + std::string("/test/v1/resources/valid_folders.evp");
std::string valid = BASE_PATH + std::string("/test/v1/resources/files_to_pack/text_1.txt");

std::vector<uint8_t> buffer;

auto r1 = evp::get_file_from_evp(input, "text_1.txt", buffer);
ASSERT(r1);
EXPECT_TRUE(r1.status == evp_result::e_status::ok);

std::ifstream stream(valid, std::ios::in | std::ios::binary);

ASSERT(stream.is_open());
ASSERT(!stream.bad());
EXPECT_TRUE(stream.is_open());

std::vector<uint8_t> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());

ASSERT(compare_buffers(buffer, contents));

return 0;
EXPECT_TRUE(compare_buffers(buffer, contents));
}

int v1_get_file_from_evp_stream(const std::string& base) {
std::string input = base + std::string("/test/v1/resources/valid_folders.evp");
std::string valid = base + std::string("/test/v1/resources/files_to_pack/text_1.txt");
TEST(unpacking, v1_get_file_from_evp_stream) {
std::string input = BASE_PATH + std::string("/test/v1/resources/valid_folders.evp");
std::string valid = BASE_PATH + std::string("/test/v1/resources/files_to_pack/text_1.txt");

std::stringstream ss;

auto r1 = evp::get_file_from_evp(input, "text_1.txt", ss);
ASSERT(r1);
EXPECT_TRUE(r1.status == evp_result::e_status::ok);

std::ifstream stream(valid, std::ios::in | std::ios::binary);

ASSERT(stream.is_open());
ASSERT(!stream.bad());
EXPECT_TRUE(stream.is_open());

std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(ss)), std::istreambuf_iterator<char>());
std::vector<uint8_t> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());

ASSERT(compare_buffers(buffer, contents));

return 0;
EXPECT_TRUE(compare_buffers(buffer, contents));
}

int main(int argc, char* argv[]) {
if (argc == 3) {
switch (std::stoi(argv[1])) {
case 0: return v1_unpacking_single_file(argv[2]);
case 1: return v1_unpacking_folder(argv[2]);
case 2: return v1_get_file_from_evp(argv[2]);
case 3: return v1_get_file_from_evp_stream(argv[2]);
default: break;
}
}

return 0;
}

0 comments on commit 49260c1

Please sign in to comment.