Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gtest-unittest-api_test.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file gtest-unittest-api_test.cc
1 // Copyright 2009 Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: vladl@google.com (Vlad Losev)
30 //
31 // The Google C++ Testing Framework (Google Test)
32 //
33 // This file contains tests verifying correctness of data provided via
34 // UnitTest's public methods.
35 
36 #include "gtest/gtest.h"
37 
38 #include <string.h> // For strcmp.
39 #include <algorithm>
40 
42 
43 namespace testing {
44 namespace internal {
45 
46 template <typename T>
47 struct LessByName {
48  bool operator()(const T* a, const T* b) {
49  return strcmp(a->name(), b->name()) < 0;
50  }
51 };
52 
54  public:
55  // Returns the array of pointers to all test cases sorted by the test case
56  // name. The caller is responsible for deleting the array.
57  static TestCase const** GetSortedTestCases() {
58  UnitTest& unit_test = *UnitTest::GetInstance();
59  TestCase const** const test_cases =
60  new const TestCase*[unit_test.total_test_case_count()];
61 
62  for (int i = 0; i < unit_test.total_test_case_count(); ++i)
63  test_cases[i] = unit_test.GetTestCase(i);
64 
65  std::sort(test_cases,
66  test_cases + unit_test.total_test_case_count(),
68  return test_cases;
69  }
70 
71  // Returns the test case by its name. The caller doesn't own the returned
72  // pointer.
73  static const TestCase* FindTestCase(const char* name) {
74  UnitTest& unit_test = *UnitTest::GetInstance();
75  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
76  const TestCase* test_case = unit_test.GetTestCase(i);
77  if (0 == strcmp(test_case->name(), name))
78  return test_case;
79  }
80  return NULL;
81  }
82 
83  // Returns the array of pointers to all tests in a particular test case
84  // sorted by the test name. The caller is responsible for deleting the
85  // array.
86  static TestInfo const** GetSortedTests(const TestCase* test_case) {
87  TestInfo const** const tests =
88  new const TestInfo*[test_case->total_test_count()];
89 
90  for (int i = 0; i < test_case->total_test_count(); ++i)
91  tests[i] = test_case->GetTestInfo(i);
92 
93  std::sort(tests, tests + test_case->total_test_count(),
95  return tests;
96  }
97 };
98 
99 #if GTEST_HAS_TYPED_TEST
100 template <typename T> class TestCaseWithCommentTest : public Test {};
101 TYPED_TEST_CASE(TestCaseWithCommentTest, Types<int>);
102 TYPED_TEST(TestCaseWithCommentTest, Dummy) {}
103 
104 const int kTypedTestCases = 1;
105 const int kTypedTests = 1;
106 #else
107 const int kTypedTestCases = 0;
108 const int kTypedTests = 0;
109 #endif // GTEST_HAS_TYPED_TEST
110 
111 // We can only test the accessors that do not change value while tests run.
112 // Since tests can be run in any order, the values the accessors that track
113 // test execution (such as failed_test_count) can not be predicted.
114 TEST(ApiTest, UnitTestImmutableAccessorsWork) {
115  UnitTest* unit_test = UnitTest::GetInstance();
116 
119  EXPECT_EQ(2, unit_test->disabled_test_count());
120  EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
121  EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
122 
123  const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
124 
125  EXPECT_STREQ("ApiTest", test_cases[0]->name());
126  EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
127 #if GTEST_HAS_TYPED_TEST
128  EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
129 #endif // GTEST_HAS_TYPED_TEST
130 
131  delete[] test_cases;
132 
133  // The following lines initiate actions to verify certain methods in
134  // FinalSuccessChecker::TearDown.
135 
136  // Records a test property to verify TestResult::GetTestProperty().
137  RecordProperty("key", "value");
138 }
139 
140 AssertionResult IsNull(const char* str) {
141  if (str != NULL) {
142  return testing::AssertionFailure() << "argument is " << str;
143  }
144  return AssertionSuccess();
145 }
146 
147 TEST(ApiTest, TestCaseImmutableAccessorsWork) {
148  const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
149  ASSERT_TRUE(test_case != NULL);
150 
151  EXPECT_STREQ("ApiTest", test_case->name());
152  EXPECT_TRUE(IsNull(test_case->type_param()));
153  EXPECT_TRUE(test_case->should_run());
154  EXPECT_EQ(1, test_case->disabled_test_count());
155  EXPECT_EQ(3, test_case->test_to_run_count());
156  ASSERT_EQ(4, test_case->total_test_count());
157 
158  const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
159 
160  EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
161  EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
162  EXPECT_TRUE(IsNull(tests[0]->value_param()));
163  EXPECT_TRUE(IsNull(tests[0]->type_param()));
164  EXPECT_FALSE(tests[0]->should_run());
165 
166  EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
167  EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
168  EXPECT_TRUE(IsNull(tests[1]->value_param()));
169  EXPECT_TRUE(IsNull(tests[1]->type_param()));
170  EXPECT_TRUE(tests[1]->should_run());
171 
172  EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
173  EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
174  EXPECT_TRUE(IsNull(tests[2]->value_param()));
175  EXPECT_TRUE(IsNull(tests[2]->type_param()));
176  EXPECT_TRUE(tests[2]->should_run());
177 
178  EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
179  EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
180  EXPECT_TRUE(IsNull(tests[3]->value_param()));
181  EXPECT_TRUE(IsNull(tests[3]->type_param()));
182  EXPECT_TRUE(tests[3]->should_run());
183 
184  delete[] tests;
185  tests = NULL;
186 
187 #if GTEST_HAS_TYPED_TEST
188  test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
189  ASSERT_TRUE(test_case != NULL);
190 
191  EXPECT_STREQ("TestCaseWithCommentTest/0", test_case->name());
192  EXPECT_STREQ(GetTypeName<int>().c_str(), test_case->type_param());
193  EXPECT_TRUE(test_case->should_run());
194  EXPECT_EQ(0, test_case->disabled_test_count());
195  EXPECT_EQ(1, test_case->test_to_run_count());
196  ASSERT_EQ(1, test_case->total_test_count());
197 
198  tests = UnitTestHelper::GetSortedTests(test_case);
199 
200  EXPECT_STREQ("Dummy", tests[0]->name());
201  EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
202  EXPECT_TRUE(IsNull(tests[0]->value_param()));
203  EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
204  EXPECT_TRUE(tests[0]->should_run());
205 
206  delete[] tests;
207 #endif // GTEST_HAS_TYPED_TEST
208 }
209 
210 TEST(ApiTest, TestCaseDisabledAccessorsWork) {
211  const TestCase* test_case = UnitTestHelper::FindTestCase("DISABLED_Test");
212  ASSERT_TRUE(test_case != NULL);
213 
214  EXPECT_STREQ("DISABLED_Test", test_case->name());
215  EXPECT_TRUE(IsNull(test_case->type_param()));
216  EXPECT_FALSE(test_case->should_run());
217  EXPECT_EQ(1, test_case->disabled_test_count());
218  EXPECT_EQ(0, test_case->test_to_run_count());
219  ASSERT_EQ(1, test_case->total_test_count());
220 
221  const TestInfo* const test_info = test_case->GetTestInfo(0);
222  EXPECT_STREQ("Dummy2", test_info->name());
223  EXPECT_STREQ("DISABLED_Test", test_info->test_case_name());
224  EXPECT_TRUE(IsNull(test_info->value_param()));
225  EXPECT_TRUE(IsNull(test_info->type_param()));
226  EXPECT_FALSE(test_info->should_run());
227 }
228 
229 // These two tests are here to provide support for testing
230 // test_case_to_run_count, disabled_test_count, and test_to_run_count.
231 TEST(ApiTest, DISABLED_Dummy1) {}
232 TEST(DISABLED_Test, Dummy2) {}
233 
235  protected:
236  virtual void TearDown() {
237  UnitTest* unit_test = UnitTest::GetInstance();
238 
240  EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
241  EXPECT_EQ(0, unit_test->failed_test_case_count());
242  EXPECT_EQ(0, unit_test->failed_test_count());
243  EXPECT_TRUE(unit_test->Passed());
244  EXPECT_FALSE(unit_test->Failed());
246 
247  const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
248 
249  EXPECT_STREQ("ApiTest", test_cases[0]->name());
250  EXPECT_TRUE(IsNull(test_cases[0]->type_param()));
251  EXPECT_TRUE(test_cases[0]->should_run());
252  EXPECT_EQ(1, test_cases[0]->disabled_test_count());
253  ASSERT_EQ(4, test_cases[0]->total_test_count());
254  EXPECT_EQ(3, test_cases[0]->successful_test_count());
255  EXPECT_EQ(0, test_cases[0]->failed_test_count());
256  EXPECT_TRUE(test_cases[0]->Passed());
257  EXPECT_FALSE(test_cases[0]->Failed());
258 
259  EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
260  EXPECT_TRUE(IsNull(test_cases[1]->type_param()));
261  EXPECT_FALSE(test_cases[1]->should_run());
262  EXPECT_EQ(1, test_cases[1]->disabled_test_count());
263  ASSERT_EQ(1, test_cases[1]->total_test_count());
264  EXPECT_EQ(0, test_cases[1]->successful_test_count());
265  EXPECT_EQ(0, test_cases[1]->failed_test_count());
266 
267 #if GTEST_HAS_TYPED_TEST
268  EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
269  EXPECT_STREQ(GetTypeName<int>().c_str(), test_cases[2]->type_param());
270  EXPECT_TRUE(test_cases[2]->should_run());
271  EXPECT_EQ(0, test_cases[2]->disabled_test_count());
272  ASSERT_EQ(1, test_cases[2]->total_test_count());
273  EXPECT_EQ(1, test_cases[2]->successful_test_count());
274  EXPECT_EQ(0, test_cases[2]->failed_test_count());
275  EXPECT_TRUE(test_cases[2]->Passed());
276  EXPECT_FALSE(test_cases[2]->Failed());
277 #endif // GTEST_HAS_TYPED_TEST
278 
279  const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
280  const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
281  EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
282  EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
283  EXPECT_FALSE(tests[0]->should_run());
284 
285  EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
286  EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
287  EXPECT_TRUE(IsNull(tests[1]->value_param()));
288  EXPECT_TRUE(IsNull(tests[1]->type_param()));
289  EXPECT_TRUE(tests[1]->should_run());
290  EXPECT_TRUE(tests[1]->result()->Passed());
291  EXPECT_EQ(0, tests[1]->result()->test_property_count());
292 
293  EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
294  EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
295  EXPECT_TRUE(IsNull(tests[2]->value_param()));
296  EXPECT_TRUE(IsNull(tests[2]->type_param()));
297  EXPECT_TRUE(tests[2]->should_run());
298  EXPECT_TRUE(tests[2]->result()->Passed());
299  EXPECT_EQ(0, tests[2]->result()->test_property_count());
300 
301  EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
302  EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
303  EXPECT_TRUE(IsNull(tests[3]->value_param()));
304  EXPECT_TRUE(IsNull(tests[3]->type_param()));
305  EXPECT_TRUE(tests[3]->should_run());
306  EXPECT_TRUE(tests[3]->result()->Passed());
307  EXPECT_EQ(1, tests[3]->result()->test_property_count());
308  const TestProperty& property = tests[3]->result()->GetTestProperty(0);
309  EXPECT_STREQ("key", property.key());
310  EXPECT_STREQ("value", property.value());
311 
312  delete[] tests;
313 
314 #if GTEST_HAS_TYPED_TEST
315  test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
316  tests = UnitTestHelper::GetSortedTests(test_case);
317 
318  EXPECT_STREQ("Dummy", tests[0]->name());
319  EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
320  EXPECT_TRUE(IsNull(tests[0]->value_param()));
321  EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
322  EXPECT_TRUE(tests[0]->should_run());
323  EXPECT_TRUE(tests[0]->result()->Passed());
324  EXPECT_EQ(0, tests[0]->result()->test_property_count());
325 
326  delete[] tests;
327 #endif // GTEST_HAS_TYPED_TEST
328  delete[] test_cases;
329  }
330 };
331 
332 } // namespace internal
333 } // namespace testing
334 
335 int main(int argc, char **argv) {
336  InitGoogleTest(&argc, argv);
337 
339 
340  return RUN_ALL_TESTS();
341 }