Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
Christopher Ferris | 7cfd24e | 2019-11-14 19:27:19 -0800 | [diff] [blame] | 19 | #include <regex> |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <tuple> |
| 22 | |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace gtest_extras { |
| 25 | |
| 26 | enum TestResult : uint8_t { |
| 27 | TEST_NONE = 0, |
| 28 | TEST_PASS, |
| 29 | TEST_XPASS, |
| 30 | TEST_FAIL, |
| 31 | TEST_XFAIL, |
| 32 | TEST_TIMEOUT, |
Christopher Ferris | 849d24e | 2019-03-09 16:47:04 -0800 | [diff] [blame] | 33 | TEST_SKIPPED, |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | class Test { |
| 37 | public: |
| 38 | Test(std::tuple<std::string, std::string>& test, size_t test_index, size_t run_index, int fd); |
Christopher Ferris | 4ba40d8 | 2020-05-11 13:31:11 -0700 | [diff] [blame] | 39 | ~Test() { CloseFd(); } |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 40 | |
Christopher Ferris | a382716 | 2019-06-12 19:01:56 -0700 | [diff] [blame] | 41 | void Print(); |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 42 | |
| 43 | void Stop(); |
| 44 | |
| 45 | bool Read(); |
| 46 | |
| 47 | void ReadUntilClosed(); |
| 48 | |
| 49 | void CloseFd(); |
| 50 | |
Christopher Ferris | 849d24e | 2019-03-09 16:47:04 -0800 | [diff] [blame] | 51 | void SetResultFromOutput(); |
| 52 | |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 53 | void AppendOutput(std::string& output) { output_ += output; } |
| 54 | void AppendOutput(const char* output) { output_ += output; } |
| 55 | |
| 56 | uint64_t RunTimeNs() const { return end_ns_ - start_ns_; } |
| 57 | uint64_t ElapsedNs(uint64_t cur_ns) const { return cur_ns - start_ns_; } |
| 58 | |
| 59 | bool ExpectFail() const { return test_name_.find("xfail") == 0; } |
| 60 | |
Christopher Ferris | c2f85d6 | 2019-03-10 13:40:40 -0700 | [diff] [blame] | 61 | const std::string& suite_name() const { return suite_name_; } |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 62 | const std::string& test_name() const { return test_name_; } |
| 63 | const std::string& name() const { return name_; } |
| 64 | |
| 65 | size_t test_index() const { return test_index_; } |
| 66 | size_t run_index() const { return run_index_; } |
| 67 | |
| 68 | int fd() const { return fd_; } |
| 69 | |
| 70 | uint64_t start_ns() const { return start_ns_; } |
| 71 | |
| 72 | uint64_t end_ns() const { return end_ns_; } |
| 73 | void set_end_ns(uint64_t end_ns) { end_ns_ = end_ns; } |
| 74 | |
| 75 | TestResult result() const { return result_; } |
| 76 | void set_result(TestResult result) { result_ = result; } |
| 77 | |
| 78 | void set_slow(bool slow) { slow_ = slow; } |
| 79 | bool slow() const { return slow_; } |
| 80 | |
| 81 | const std::string& output() const { return output_; } |
| 82 | |
| 83 | private: |
Christopher Ferris | c2f85d6 | 2019-03-10 13:40:40 -0700 | [diff] [blame] | 84 | std::string suite_name_; |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 85 | std::string test_name_; |
| 86 | std::string name_; |
| 87 | size_t test_index_; // Index into test list. |
| 88 | size_t run_index_; // Index into running list. |
Christopher Ferris | 4ba40d8 | 2020-05-11 13:31:11 -0700 | [diff] [blame] | 89 | int fd_ = -1; |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 90 | |
| 91 | uint64_t start_ns_; |
| 92 | uint64_t end_ns_ = 0; |
| 93 | bool slow_ = false; |
| 94 | |
| 95 | TestResult result_ = TEST_NONE; |
| 96 | std::string output_; |
Christopher Ferris | 7cfd24e | 2019-11-14 19:27:19 -0800 | [diff] [blame] | 97 | |
| 98 | static std::regex skipped_regex_; |
Christopher Ferris | 1a99356 | 2018-08-21 12:43:50 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace gtest_extras |
| 102 | } // namespace android |