|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include <tvm/runtime/packed_func.h> |
| 22 | +#include <tvm/runtime/registry.h> |
| 23 | + |
| 24 | +#include <string> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include "../src/support/utils.h" |
| 28 | + |
| 29 | +namespace tvm { |
| 30 | +namespace runtime { |
| 31 | +namespace hexagon { |
| 32 | + |
| 33 | +class GtestPrinter : public testing::EmptyTestEventListener { |
| 34 | + void OnTestProgramStart(const testing::UnitTest& unit_test) override { |
| 35 | + gtest_out_ << "[==========] Running " << unit_test.test_to_run_count() << " test(s) from " |
| 36 | + << unit_test.test_suite_to_run_count() << " test suite(s).\n"; |
| 37 | + } |
| 38 | + |
| 39 | + void OnTestProgramEnd(const testing::UnitTest& unit_test) override { |
| 40 | + gtest_out_ << "[==========] " << unit_test.test_to_run_count() << " test(s) from " |
| 41 | + << unit_test.test_suite_to_run_count() << " test suite(s) ran. (" |
| 42 | + << unit_test.elapsed_time() << " ms total)\n"; |
| 43 | + gtest_out_ << "[ PASSED ] " << unit_test.successful_test_count() << " test(s)\n"; |
| 44 | + |
| 45 | + if (unit_test.failed_test_count()) { |
| 46 | + gtest_out_ << "[ FAILED ] " << unit_test.failed_test_count() << " test(s)\n"; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + void OnTestSuiteStart(const testing::TestSuite& test_suite) override { |
| 51 | + gtest_out_ << "[----------] " << test_suite.test_to_run_count() << " test(s) from " |
| 52 | + << test_suite.name() << "\n"; |
| 53 | + } |
| 54 | + |
| 55 | + void OnTestSuiteEnd(const testing::TestSuite& test_suite) override { |
| 56 | + gtest_out_ << "[----------] " << test_suite.test_to_run_count() << " test(s) from " |
| 57 | + << test_suite.name() << " (" << test_suite.elapsed_time() << " ms total)\n"; |
| 58 | + } |
| 59 | + |
| 60 | + void OnTestStart(const testing::TestInfo& test_info) override { |
| 61 | + gtest_out_ << "[ RUN ] " << test_info.test_suite_name() << "." << test_info.name() << "\n"; |
| 62 | + } |
| 63 | + |
| 64 | + void OnTestEnd(const testing::TestInfo& test_info) override { |
| 65 | + for (int i = 0; i < test_info.result()->total_part_count(); ++i) { |
| 66 | + gtest_out_ << test_info.result()->GetTestPartResult(i).message() << "\n"; |
| 67 | + } |
| 68 | + if (test_info.result()->Passed()) { |
| 69 | + gtest_out_ << "[ OK ]"; |
| 70 | + } else { |
| 71 | + gtest_out_ << "[ FAILED ]"; |
| 72 | + } |
| 73 | + gtest_out_ << " " << test_info.test_suite_name() << "." << test_info.name() << " (" |
| 74 | + << test_info.result()->elapsed_time() << " ms)\n"; |
| 75 | + } |
| 76 | + |
| 77 | + std::stringstream gtest_out_; |
| 78 | + |
| 79 | + public: |
| 80 | + std::string GetOutput() { return gtest_out_.str(); } |
| 81 | +}; |
| 82 | + |
| 83 | +TVM_REGISTER_GLOBAL("hexagon.run_unit_tests").set_body([](TVMArgs args, TVMRetValue* rv) { |
| 84 | + // gtest args are passed into this packed func as a singular string |
| 85 | + // split gtest args using <space> delimiter and build argument vector |
| 86 | + std::vector<std::string> parsed_args = tvm::support::Split(args[0], ' '); |
| 87 | + std::vector<char*> argv; |
| 88 | + |
| 89 | + // add executable name |
| 90 | + argv.push_back(const_cast<char*>("hexagon_run_unit_tests")); |
| 91 | + |
| 92 | + // add parsed arguments |
| 93 | + for (int i = 0; i < parsed_args.size(); ++i) { |
| 94 | + argv.push_back(const_cast<char*>(parsed_args[i].data())); |
| 95 | + } |
| 96 | + |
| 97 | + // end of parsed arguments |
| 98 | + argv.push_back(nullptr); |
| 99 | + |
| 100 | + // set argument count |
| 101 | + int argc = argv.size() - 1; |
| 102 | + |
| 103 | + // initialize gtest with arguments and run |
| 104 | + ::testing::InitGoogleTest(&argc, argv.data()); |
| 105 | + |
| 106 | + // add printer to capture gtest output in a string |
| 107 | + GtestPrinter* gprinter = new GtestPrinter(); |
| 108 | + testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners(); |
| 109 | + listeners.Append(gprinter); |
| 110 | + |
| 111 | + int gtest_error_code = RUN_ALL_TESTS(); |
| 112 | + std::string gtest_output = gprinter->GetOutput(); |
| 113 | + std::stringstream gtest_error_code_and_output; |
| 114 | + gtest_error_code_and_output << gtest_error_code << std::endl; |
| 115 | + gtest_error_code_and_output << gtest_output; |
| 116 | + *rv = gtest_error_code_and_output.str(); |
| 117 | + delete gprinter; |
| 118 | +}); |
| 119 | + |
| 120 | +} // namespace hexagon |
| 121 | +} // namespace runtime |
| 122 | +} // namespace tvm |
0 commit comments