blob: dd54dc6fde6576e3e152456dd57214f058ea3a07 [file] [log] [blame]
Christopher Ferris1a993562018-08-21 12:43:50 -07001/*
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 Ferris7cfd24e2019-11-14 19:27:19 -080019#include <regex>
Christopher Ferris1a993562018-08-21 12:43:50 -070020#include <string>
21#include <tuple>
22
Christopher Ferris1a993562018-08-21 12:43:50 -070023namespace android {
24namespace gtest_extras {
25
26enum TestResult : uint8_t {
27 TEST_NONE = 0,
28 TEST_PASS,
29 TEST_XPASS,
30 TEST_FAIL,
31 TEST_XFAIL,
32 TEST_TIMEOUT,
Christopher Ferris849d24e2019-03-09 16:47:04 -080033 TEST_SKIPPED,
Christopher Ferris1a993562018-08-21 12:43:50 -070034};
35
36class Test {
37 public:
38 Test(std::tuple<std::string, std::string>& test, size_t test_index, size_t run_index, int fd);
Christopher Ferris4ba40d82020-05-11 13:31:11 -070039 ~Test() { CloseFd(); }
Christopher Ferris1a993562018-08-21 12:43:50 -070040
Christopher Ferrisa3827162019-06-12 19:01:56 -070041 void Print();
Christopher Ferris1a993562018-08-21 12:43:50 -070042
43 void Stop();
44
45 bool Read();
46
47 void ReadUntilClosed();
48
49 void CloseFd();
50
Christopher Ferris849d24e2019-03-09 16:47:04 -080051 void SetResultFromOutput();
52
Christopher Ferris1a993562018-08-21 12:43:50 -070053 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 Ferrisc2f85d62019-03-10 13:40:40 -070061 const std::string& suite_name() const { return suite_name_; }
Christopher Ferris1a993562018-08-21 12:43:50 -070062 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 Ferrisc2f85d62019-03-10 13:40:40 -070084 std::string suite_name_;
Christopher Ferris1a993562018-08-21 12:43:50 -070085 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 Ferris4ba40d82020-05-11 13:31:11 -070089 int fd_ = -1;
Christopher Ferris1a993562018-08-21 12:43:50 -070090
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 Ferris7cfd24e2019-11-14 19:27:19 -080097
98 static std::regex skipped_regex_;
Christopher Ferris1a993562018-08-21 12:43:50 -070099};
100
101} // namespace gtest_extras
102} // namespace android