From 1450038bcdb2a07199f8d6fbec529297a751c22c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Sun, 8 Dec 2024 17:41:02 +0200 Subject: [PATCH] Use std::filesystem::path for test file paths --- test/bench-url.cpp | 8 ++++---- test/picojson_util.h | 3 ++- test/wpt-url.cpp | 21 +++++++++++---------- test/wpt-urlencoded-parser.cpp | 5 +++-- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/test/bench-url.cpp b/test/bench-url.cpp index c5df2d7..88f00d3 100644 --- a/test/bench-url.cpp +++ b/test/bench-url.cpp @@ -19,7 +19,7 @@ // ----------------------------------------------------------------------------- // Read samples from text file (URL in each line) and benchmark -int benchmark_txt(const char* file_name, uint64_t min_iters) { +int benchmark_txt(const std::filesystem::path& file_name, uint64_t min_iters) { std::vector url_strings; // Load URL samples @@ -60,7 +60,7 @@ int benchmark_txt(const char* file_name, uint64_t min_iters) { // ----------------------------------------------------------------------------- // Read samples from urltestdata.json and benchmark -int benchmark_wpt(const char* file_name, uint64_t min_iters) { +int benchmark_wpt(const std::filesystem::path& file_name, uint64_t min_iters) { std::vector> url_samples; // Load URL samples @@ -144,9 +144,9 @@ int main(int argc, const char* argv[]) : min_iters_def; if (file_name.extension() == ".json") { - return benchmark_wpt(file_name.string().c_str(), min_iters); + return benchmark_wpt(file_name, min_iters); } else if (file_name.extension() == ".txt") { - return benchmark_txt(file_name.string().c_str(), min_iters); + return benchmark_txt(file_name, min_iters); } std::cerr << "File containing URLs should have .json or .txt extension.\n"; diff --git a/test/picojson_util.h b/test/picojson_util.h index f4effd5..733c95e 100644 --- a/test/picojson_util.h +++ b/test/picojson_util.h @@ -6,6 +6,7 @@ // https://github.com/kazuho/picojson #include "picojson/picojson.h" +#include #include #include #include @@ -134,7 +135,7 @@ enum { // Parses a JSON file using the specified PicoJSON parsing context template -inline int load_file(Context& ctx, const char* file_name, const char* title = nullptr) { +inline int load_file(Context& ctx, const std::filesystem::path& file_name, const char* title = nullptr) { try { if (title) std::cout << title << ": " << file_name << '\n'; diff --git a/test/wpt-url.cpp b/test/wpt-url.cpp index e409b5b..51fdebe 100644 --- a/test/wpt-url.cpp +++ b/test/wpt-url.cpp @@ -13,6 +13,7 @@ #include "picojson_util.h" +#include #include #include #include @@ -22,11 +23,11 @@ // Test runner -int run_parser_tests(DataDrivenTest& ddt, const char* file_name); -int run_host_parser_tests(DataDrivenTest& ddt, const char* file_name); -int run_idna_v2_tests(DataDrivenTest& ddt, const char* file_name, const char* fixes_file_name); -int run_setter_tests(DataDrivenTest& ddt, const char* file_name); -int run_percent_encoding_tests(DataDrivenTest& ddt, const char* file_name); +int run_parser_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name); +int run_host_parser_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name); +int run_idna_v2_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name, const char* fixes_file_name); +int run_setter_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name); +int run_percent_encoding_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name); template int test_from_file(RunTests run_tests, Args&&... args); @@ -436,7 +437,7 @@ void test_percent_encoding(DataDrivenTest& ddt, const EncodingObj& obj) // Read samples from JSON files and run tests -int run_parser_tests(DataDrivenTest& ddt, const char* file_name) { +int run_parser_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name) { const auto test_item = [&](const picojson::value& item) { // analyze array item if (item.is()) { @@ -455,7 +456,7 @@ int run_parser_tests(DataDrivenTest& ddt, const char* file_name) { return json_util::load_file(context, file_name); } -int run_host_parser_tests(DataDrivenTest& ddt, const char* file_name) { +int run_host_parser_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name) { const auto test_item = [&](const picojson::value& item) { // analyze array item if (item.is()) { @@ -474,7 +475,7 @@ int run_host_parser_tests(DataDrivenTest& ddt, const char* file_name) { return json_util::load_file(context, file_name); } -int run_idna_v2_tests(DataDrivenTest& ddt, const char* file_name, const char* fixes_file_name) { +int run_idna_v2_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name, const char* fixes_file_name) { std::unordered_map fixes; if (fixes_file_name) { @@ -520,7 +521,7 @@ int run_idna_v2_tests(DataDrivenTest& ddt, const char* file_name, const char* fi } // parses setters_tests.json -int run_setter_tests(DataDrivenTest& ddt, const char* file_name) { +int run_setter_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name) { const auto test_item = [&](const std::string& setter_name, const picojson::value& item) { // analyze array item if (item.is()) { @@ -556,7 +557,7 @@ int run_setter_tests(DataDrivenTest& ddt, const char* file_name) { } // parses percent-encoding.json -int run_percent_encoding_tests(DataDrivenTest& ddt, const char* file_name) { +int run_percent_encoding_tests(DataDrivenTest& ddt, const std::filesystem::path& file_name) { const auto test_item = [&](const picojson::value& item) { // analyze array item if (item.is()) { diff --git a/test/wpt-urlencoded-parser.cpp b/test/wpt-urlencoded-parser.cpp index b99280e..60d6b33 100644 --- a/test/wpt-urlencoded-parser.cpp +++ b/test/wpt-urlencoded-parser.cpp @@ -8,6 +8,7 @@ // https://github.com/kazuho/picojson #include "picojson_util.h" +#include #include #include @@ -24,7 +25,7 @@ std::basic_ostream& operator<<( // https://github.com/web-platform-tests/wpt/blob/master/url/urlsearchparams-sort.any.js // -int test_from_file(const char* file_name, bool sort = false); +int test_from_file(const std::filesystem::path& file_name, bool sort = false); int main(int argc, char** argv) { @@ -108,7 +109,7 @@ void test_urlsearchparams_sort(DataDrivenTest& ddt, const TestObj& obj) { // Read data file and run tests from it -int test_from_file(const char* file_name, bool sort) +int test_from_file(const std::filesystem::path& file_name, bool sort) { DataDrivenTest ddt; ddt.config_show_passed(false);