| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <charconv> |
| 11 | #include <cstddef> |
| 12 | #include <cstdlib> |
| 13 | #include <iostream> |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <optional> |
| 17 | #include <string> |
| 18 | #include <utility> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "test/common/assert.hpp" |
| 22 | #include "test/nextgen/common/expected.hpp" |
| 23 | #include "test/nextgen/common/test_config.hpp" |
| 24 | #include "test/nextgen/common/test_registry.hpp" |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | void remove_args(int& argc, char** argv, int idx, int count) { |
| 29 | KAI_ASSUME(idx + count <= argc); |
| 30 | |
| 31 | for (int j = idx + count; j < argc; ++j) { |
| 32 | argv[j - count] = argv[j]; |
| 33 | } |
| 34 | argc -= count; |
| 35 | } |
| 36 | |
| 37 | std::optional<size_t> value_to_num_shapes(const std::string& value) { |
| 38 | if (value == "small") { |
| 39 | return 10; |
| 40 | } |
| 41 | if (value == "large") { |
| 42 | return 100; |
| 43 | } |
| 44 | |
| 45 | size_t parsed{}; |
| 46 | const char* begin = value.data(); |
| 47 | const char* end = begin + value.size(); |
| 48 | |
| 49 | auto [ptr, ec] = std::from_chars(begin, end, parsed); |
| 50 | |
| 51 | if (ec != std::errc() || ptr != end || parsed == 0) { |
| 52 | return std::nullopt; |
| 53 | } |
| 54 | return parsed; |
| 55 | } |
| 56 | |
| 57 | enum class TestSizeParseErr { |
| 58 | Unset, |
| 59 | MissingValue, |
| 60 | InvalidValue, |
| 61 | }; |
| 62 | |
| 63 | Expected<size_t, TestSizeParseErr> parse_test_size(int& argc, char** argv) { |
| 64 | const std::string flag = "--test_size"; |
| 65 | std::string raw_value = "large"; |
| 66 | bool user_specified = false; |
| 67 | |
| 68 | // Scan argv for --test_size <value> or --test_size=<value>, |
| 69 | // extract the value if present, and remove the argument |
| 70 | for (int i = 1; i < argc; ++i) { |
| 71 | std::string arg = argv[i]; |
| 72 | |
| 73 | if (arg == flag) { |
| 74 | if (i + 1 >= argc) { |
| 75 | std::cerr << "Error: --test_size requires a value.\n"; |
| 76 | std::cerr << "Usage: --test_size <small|large|positive integer>\n"; |
| 77 | return TestSizeParseErr::MissingValue; |
| 78 | } |
| 79 | |
| 80 | user_specified = true; |
| 81 | raw_value = argv[i + 1]; |
| 82 | remove_args(argc, argv, i, 2); |
| 83 | --i; |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (arg.rfind(flag + "=", 0) == 0) { |
| 88 | user_specified = true; |
| 89 | raw_value = arg.substr(flag.size() + 1); |
| 90 | remove_args(argc, argv, i, 1); |
| 91 | --i; |
| 92 | continue; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (!user_specified) { |
| 97 | return TestSizeParseErr::Unset; |
| 98 | } |
| 99 | |
| 100 | auto parsed = value_to_num_shapes(raw_value); |
| 101 | if (!parsed) { |
| 102 | std::cerr << "Error: invalid --test_size value '" << raw_value << "'.\n"; |
| 103 | std::cerr << "Usage: --test_size <small|large|positive integer>\n"; |
| 104 | return TestSizeParseErr::InvalidValue; |
| 105 | } |
| 106 | |
| 107 | return *parsed; |
| 108 | } |
| 109 | |
| 110 | bool is_env_enabled(const char* name) { |
| 111 | const char* value = std::getenv(name); |
| 112 | if (value == nullptr || value[0] == '\0') { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | const std::string normalized_value = value; |
| 117 | return normalized_value != "0" && normalized_value != "false" && normalized_value != "FALSE" && |
| 118 | normalized_value != "off" && normalized_value != "OFF"; |
| 119 | } |
| 120 | |
| 121 | std::string get_skip_reason(const testing::TestResult& result) { |
| 122 | for (int i = 0; i < result.total_part_count(); ++i) { |
| 123 | const testing::TestPartResult& part = result.GetTestPartResult(i); |
| 124 | if (!part.skipped()) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | std::string reason = part.summary(); |
| 129 | if (reason.empty()) { |
| 130 | reason = part.message(); |
| 131 | } |
| 132 | if (!reason.empty()) { |
| 133 | return reason; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return "(no skip reason)"; |
| 138 | } |
| 139 | |
| 140 | void print_test_location(const testing::TestPartResult& result) { |
| 141 | if (result.file_name() == nullptr) { |
| 142 | std::cout << "unknown file"; |
| 143 | } else { |
| 144 | std::cout << result.file_name(); |
| 145 | } |
| 146 | |
| 147 | if (result.line_number() >= 0) { |
| 148 | std::cout << ":" << result.line_number(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | class QuietResultPrinter final : public ::testing::EmptyTestEventListener { |
| 153 | public: |
| 154 | void OnTestIterationStart(const testing::UnitTest& unit_test, int iteration) override { |
| 155 | skipped_by_reason_.clear(); |
| 156 | printed_failure_ = false; |
| 157 | |
| 158 | std::cout << "[==========] "; |
| 159 | if (GTEST_FLAG_GET(repeat) != 1) { |
| 160 | std::cout << "Iteration " << iteration + 1 << ": running "; |
| 161 | } else { |
| 162 | std::cout << "Running "; |
| 163 | } |
| 164 | std::cout << unit_test.test_to_run_count() << " tests from " << unit_test.test_suite_to_run_count() |
| 165 | << " test suites.\n"; |
| 166 | } |
| 167 | |
| 168 | void OnTestPartResult(const testing::TestPartResult& result) override { |
| 169 | if (!result.failed()) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | printed_failure_ = true; |
| 174 | std::cout << "\n[ FAILED ] "; |
| 175 | const testing::UnitTest& unit_test = *testing::UnitTest::GetInstance(); |
| 176 | if (const testing::TestInfo* test_info = unit_test.current_test_info()) { |
| 177 | std::cout << test_info->test_suite_name() << "." << test_info->name(); |
| 178 | } else if (const testing::TestSuite* test_suite = unit_test.current_test_suite()) { |
| 179 | std::cout << test_suite->name() << ": SetUpTestSuite or TearDownTestSuite"; |
| 180 | } else { |
| 181 | std::cout << "global test environment"; |
| 182 | } |
| 183 | std::cout << "\n"; |
| 184 | print_test_location(result); |
| 185 | std::cout << "\n" << result.message() << "\n"; |
| 186 | } |
| 187 | |
| 188 | void OnTestEnd(const testing::TestInfo& test_info) override { |
| 189 | const testing::TestResult& result = *test_info.result(); |
| 190 | if (result.Skipped()) { |
| 191 | ++skipped_by_reason_[get_skip_reason(result)]; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void OnTestIterationEnd(const testing::UnitTest& unit_test, int /*iteration*/) override { |
| 196 | std::cout << "[==========] " << unit_test.test_to_run_count() << " tests from " |
| 197 | << unit_test.test_suite_to_run_count() << " test suites ran. (" << unit_test.elapsed_time() |
| 198 | << " ms total)\n"; |
| 199 | std::cout << "[ PASSED ] " << unit_test.successful_test_count() << " tests.\n"; |
| 200 | |
| 201 | if (unit_test.skipped_test_count() > 0) { |
| 202 | std::cout << "[ SKIPPED ] " << unit_test.skipped_test_count() << " tests"; |
| 203 | if (!skipped_by_reason_.empty()) { |
| 204 | std::cout << " by reason"; |
| 205 | } |
| 206 | std::cout << ".\n"; |
| 207 | |
| 208 | std::vector<std::pair<std::string, size_t>> sorted_skips( |
| 209 | skipped_by_reason_.begin(), skipped_by_reason_.end()); |
| 210 | std::sort(sorted_skips.begin(), sorted_skips.end(), [](const auto& lhs, const auto& rhs) { |
| 211 | if (lhs.second != rhs.second) { |
| 212 | return lhs.second > rhs.second; |
| 213 | } |
| 214 | return lhs.first < rhs.first; |
| 215 | }); |
| 216 | |
| 217 | for (const auto& [reason, count] : sorted_skips) { |
| 218 | std::cout << " " << count << " - " << reason << "\n"; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (unit_test.Failed()) { |
| 223 | if (unit_test.failed_test_count() == 0) { |
| 224 | std::cout << "[ FAILED ] Test run failed"; |
| 225 | if (printed_failure_) { |
| 226 | std::cout << ", failure listed above"; |
| 227 | } |
| 228 | std::cout << ".\n"; |
| 229 | } else if (!printed_failure_) { |
| 230 | std::cout << "[ FAILED ] " << unit_test.failed_test_count() << " tests.\n"; |
| 231 | } else { |
| 232 | std::cout << "[ FAILED ] " << unit_test.failed_test_count() << " tests, listed above.\n"; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | std::cout << "Test seed = " << unit_test.random_seed() |
| 237 | << ", num_shapes=" << kai::test::TestConfig::Get().num_shapes() << "\n"; |
| 238 | } |
| 239 | |
| 240 | private: |
| 241 | std::map<std::string, size_t> skipped_by_reason_; |
| 242 | bool printed_failure_ = false; |
| 243 | }; |
| 244 | |
| 245 | class TestSummaryListener final : public ::testing::EmptyTestEventListener { |
| 246 | void OnTestIterationEnd(const testing::UnitTest& unit_test, int /*iteration*/) override { |
| 247 | std::cout << "Test seed = " << unit_test.random_seed() |
| 248 | << ", num_shapes=" << kai::test::TestConfig::Get().num_shapes() << "\n"; |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | } // namespace |
| 253 | |
| 254 | int main(int argc, char** argv) { |
| 255 | auto parsed = parse_test_size(argc, argv); |
| 256 | if (parsed.has_value()) { |
| 257 | kai::test::TestConfig::Get().set_num_shapes(parsed.value()); |
| 258 | } else if (parsed.error() != TestSizeParseErr::Unset) { |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | testing::InitGoogleTest(&argc, argv); |
| 263 | |
| 264 | const int seed = GTEST_FLAG_GET(random_seed); |
| 265 | if (seed == 0) { |
| 266 | // Set a fixed seed for reproducibility. |
| 267 | GTEST_FLAG_SET(random_seed, 42); |
| 268 | } |
| 269 | |
| 270 | auto& listeners = ::testing::UnitTest::GetInstance()->listeners(); |
| 271 | const bool quiet_output = is_env_enabled("KLEIDIAI_TEST_CI_QUIET") || is_env_enabled("KLEIDIAI_TEST_QUIET"); |
| 272 | if (quiet_output) { |
| 273 | if (auto* default_printer = listeners.default_result_printer()) { |
| 274 | delete listeners.Release(default_printer); |
| 275 | } |
| 276 | auto quiet_printer = std::make_unique<QuietResultPrinter>(); |
| 277 | listeners.Append(quiet_printer.release()); |
| 278 | } |
| 279 | |
| 280 | if (!quiet_output) { |
| 281 | auto summary_listener = std::make_unique<TestSummaryListener>(); |
| 282 | if (auto* default_printer = listeners.default_result_printer()) { |
| 283 | default_printer = listeners.Release(default_printer); |
| 284 | listeners.Append(summary_listener.release()); |
| 285 | listeners.Append(default_printer); |
| 286 | } else { |
| 287 | listeners.Append(summary_listener.release()); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | kai::test::TestRegistry::init(); |
| 292 | |
| 293 | return RUN_ALL_TESTS(); |
| 294 | } |