From 29fc3ad5d2e1a91d71691e6399e432bbc2ffacca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Miku=C4=8Dionis?= Date: Thu, 11 Apr 2024 17:15:08 +0200 Subject: [PATCH] Added failing tests for redundant warnings "has no effect" on external function calls --- test/external_fn.cpp | 11 ++++++++++ test/models/external_fn.xml | 29 ++++++++++++++++++++---- test/test_parser.cpp | 44 +++++++++++++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/test/external_fn.cpp b/test/external_fn.cpp index cfc6de56..3bd82764 100644 --- a/test/external_fn.cpp +++ b/test/external_fn.cpp @@ -53,3 +53,14 @@ C_PUBLIC double calc_sum(int count, const double arr[]) return res; } C_PUBLIC unsigned int length(const char* str) { return std::strlen(str); } + +C_PUBLIC bool fast_double(double arr[], int count) +{ + for (auto i = 0; i < count; ++i) + arr[i] *= 2; + return count % 2 == 0; +} + +static int number = 42; +C_PUBLIC void set_number(int a) { number = a; } +C_PUBLIC int get_number() { return number; } diff --git a/test/models/external_fn.xml b/test/models/external_fn.xml index 1891dd30..0274dce9 100644 --- a/test/models/external_fn.xml +++ b/test/models/external_fn.xml @@ -1,19 +1,23 @@ - + import "libexternal_fn" { int square(int a); int multiply(int a, int b); double square_root(double c); double power(double c, int a); - double calc_sum(int n, const double d[3]); + double calc_sum(int n, const double& d[3]); int length(const string& s); + bool fast_double(double& d[3], int count); + void set_number(int a); + int get_number(); }; import "libbad" { int absent(int a); }; + const int a = 3, b = 7; const double c = 11; const double d[3] = { 3, 5, 7 }; @@ -25,12 +29,29 @@ const double c_r2 = square_root(square_root(c)); double c2 = power(c_r2, 4); double d_s = calc_sum(3, d); int l = length("123"); + +double dd[3] = d; + +void __ON_CONSTRUCT__() { + set_number(2); +} + +int number = get_number(); system P; diff --git a/test/test_parser.cpp b/test/test_parser.cpp index c11e47ab..f99f1c9d 100644 --- a/test/test_parser.cpp +++ b/test/test_parser.cpp @@ -42,13 +42,53 @@ TEST_CASE("Power expressions") CHECK(warnings.size() == 0); } +struct Contains +{ + std::string_view text; +}; + +bool operator==(std::string_view text, const Contains& sub) { return text.find(sub.text) != std::string_view::npos; } +bool operator!=(std::string_view text, const Contains& sub) { return !(text == sub); } +std::ostream& operator<<(std::ostream& os, const Contains& sub) { return os << sub.text; } + +enum class OS { Windows, macOS, Linux }; +#ifdef _WIN32 +constexpr auto target_os = OS::Windows; +#elif __linux__ +constexpr auto target_os = OS::Linux; +#elif __APPLE__ +constexpr auto target_os = OS::macOS; +#else +#error "Unsupported target operating system" +#endif + TEST_CASE("External functions") { auto doc = read_document("external_fn.xml"); REQUIRE(doc); auto& errs = doc->get_errors(); + REQUIRE(errs.size() == 3); + if constexpr (target_os == OS::Linux) { + CHECK(errs[0].msg == Contains{"libbad.so: cannot open shared object file: No such file or directory"}); + CHECK(errs[0].msg == Contains{"libbad.so: cannot open shared object file: No such file or directory"}); + CHECK(errs[2].msg == Contains{"undefined symbol: absent"}); + } else if constexpr (target_os == OS::Windows) { + CHECK(errs[0].msg == Contains{"Failed to open dynamic library libbad.dll: error 126: Module not found."}); + CHECK(errs[0].msg == Contains{"Failed to open dynamic library libbad.dll: error 126: Module not found."}); + CHECK(errs[2].msg == Contains{"Failed to find symbol: error 127: Procedure not found."}); + } else if constexpr (target_os == OS::macOS) { + CHECK(errs[0].msg == Contains{"libbad.dylib: cannot open shared object file: No such file or directory"}); + CHECK(errs[0].msg == Contains{"libbad.dylib: cannot open shared object file: No such file or directory"}); + CHECK(errs[2].msg == Contains{"undefined symbol: absent"}); + } else { + REQUIRE_MESSAGE(false, "OS is not supported"); + } auto& warns = doc->get_warnings(); - REQUIRE(errs.size() == 3); // "libbad" not found (x2), "absent" undefined. + CHECK(warns.size() == 0); + // TypeChecker is not run when errors are present, so we do it on our own: + auto checker = UTAP::TypeChecker{*doc}; + doc->accept(checker); + REQUIRE(errs.size() == 3); // no new errors CHECK(warns.size() == 0); } @@ -417,7 +457,7 @@ TEST_CASE("Array of structs") REQUIRE(doc); auto& errors = doc->get_errors(); CHECK_MESSAGE(errors.size() == 0, errors.at(0).msg); - CHECK(doc->get_warnings().size() == 0); + CHECK(doc->get_warnings().size() == 0); } TEST_CASE("Pre increment precedence bug")