Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ set(CURL_LIBRARY "C:/vcpkg/installed/x64-windows/lib/libcurl.a")
set(CMAKE_BUILD_TYPE Debug)
add_executable(main main.cpp)

add_executable(unit_tests tests/test_main.cpp)

target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)

target_link_libraries(main PRIVATE CURL::libcurl)
target_link_libraries(main PRIVATE CURL::libcurl)

target_link_libraries(unit_tests PRIVATE nlohmann_json::nlohmann_json)
28 changes: 23 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#ifndef UNIT_TESTING
#include <curl/curl.h>
#endif
#include <string.h>

#include <unordered_set>
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
#include <filesystem>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
Expand All @@ -29,8 +33,9 @@ std::string FormatHTMLToString(const std::string &response);
TestCaseResponse GetTestCases(const std::string &content);

std::pair<std::string, std::string> GetParamName(const std::string &param);
void CreateJSON(json *response, const TestCaseResponse &testCases);
void CreateJSON(json *response, const TestCaseResponse &testCases, const std::string &outputDir = "../../../Questions");

#ifndef UNIT_TESTING
int main()
{
std::string questionName = "";
Expand Down Expand Up @@ -104,6 +109,7 @@ int main()
curl_easy_cleanup(curl);
return 0;
}
#endif

// returns number of bytes in the chunk
// data is set to a ptr that points to block of data recieved in this chunk
Expand Down Expand Up @@ -369,23 +375,35 @@ TestCaseResponse GetTestCases(const std::string &content)
return tests;
}

void CreateJSON(json *response, const TestCaseResponse &tests)
void CreateJSON(json *response, const TestCaseResponse &tests, const std::string &outputDir)
{
namespace fs = std::filesystem;
// filter out invalid characters from title
std::string title = (*response)["title"];
const std::string invalid_chars = "\\/:*?\"<>|";
for (char c : invalid_chars)
{
std::replace(title.begin(), title.end(), c, '_');
}
std::string jsonName = "../../../Questions/" + title + ".txt";
fs::path basePath(outputDir);
std::error_code ec;
if (!fs::exists(basePath))
{
fs::create_directories(basePath, ec);
if (ec)
{
std::cerr << "Error creating output directory: " << ec.message() << std::endl;
return;
}
}
fs::path outputPath = basePath / (title + ".txt");

std::ofstream outputJSON;
outputJSON.open(jsonName);
outputJSON.open(outputPath);
// should have to create the file so always should open
if (!outputJSON.is_open())
{
std::cerr << "Error creating output file for JSON response" << std::endl;
std::cerr << "Error creating output file for JSON response: " << outputPath << std::endl;
return;
}

Expand Down
82 changes: 82 additions & 0 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#define UNIT_TESTING

#include <cassert>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>

#include "../main.cpp"

void TestFormatHTMLToString()
{
std::string html = "<div>Value:&nbsp;&lt;tag&gt;&#39;s</div>\n\nNext\t\tLine";
std::string expected = "Value:<tag>\nNextLine";
auto formatted = FormatHTMLToString(html);
assert(formatted == expected);
}

void TestGetParamName()
{
auto param = GetParamName("nums = [1, 2, 3]");
assert(param.first == "nums");
assert(param.second == "[1,2,3]");
}

void TestGetTestCases()
{
std::string content =
"Example 1:\n"
"Input: nums = [1, 2, 3], k = 3\n"
"Output: 2\n"
"Example 2:\n"
"Input: nums = [1, 2], k = 1\n"
"Output: 1\n";

TestCaseResponse tests = GetTestCases(content);
assert(tests.testCases.size() == 2);
assert(tests.testCases[0] == "2");
assert(tests.testCases[1] == "1");
assert(tests.testCaseParams.size() == 4);
assert(tests.testCaseParams[0] == std::make_pair(std::string("nums"), std::string("[1,2,3]")));
assert(tests.testCaseParams[1] == std::make_pair(std::string("k"), std::string("3")));
}

void TestCreateJSON()
{
json response = {{"title", "Sample/Title"}, {"content", "desc"}, {"difficulty", "Easy"}};
TestCaseResponse tests;
tests.testCases = {"42"};
tests.testCaseParams = {{"nums", "[1,2,3]"}, {"k", "3"}};

std::filesystem::path tempRoot = std::filesystem::temp_directory_path() / "leetcode_tests";
std::filesystem::remove_all(tempRoot);

std::filesystem::path outputDir = tempRoot / "Questions";
CreateJSON(&response, tests, outputDir.string());

std::filesystem::path outputFile = outputDir / "Sample_Title.txt";
assert(std::filesystem::exists(outputFile));

std::ifstream input(outputFile);
std::stringstream buffer;
buffer << input.rdbuf();
std::string content = buffer.str();
assert(content.find("\"expectedResult\": \"42\"") != std::string::npos);
assert(content.find("\"nums\": \"[1,2,3]\"") != std::string::npos);
assert(content.find("\"k\": \"3\"") != std::string::npos);

std::filesystem::remove_all(tempRoot);
}

int main()
{
TestFormatHTMLToString();
TestGetParamName();
TestGetTestCases();
TestCreateJSON();

std::cout << "All tests passed!" << std::endl;
return 0;
}