The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

507 lines
22 KiB

2 months ago
  1. // Copyright 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Macros and functions for implementing parameterized tests
  31. // in Google C++ Testing and Mocking Framework (Google Test)
  32. //
  33. // GOOGLETEST_CM0001 DO NOT DELETE
  34. #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_
  35. #define GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_
  36. // Value-parameterized tests allow you to test your code with different
  37. // parameters without writing multiple copies of the same test.
  38. //
  39. // Here is how you use value-parameterized tests:
  40. #if 0
  41. // To write value-parameterized tests, first you should define a fixture
  42. // class. It is usually derived from testing::TestWithParam<T> (see below for
  43. // another inheritance scheme that's sometimes useful in more complicated
  44. // class hierarchies), where the type of your parameter values.
  45. // TestWithParam<T> is itself derived from testing::Test. T can be any
  46. // copyable type. If it's a raw pointer, you are responsible for managing the
  47. // lifespan of the pointed values.
  48. class FooTest : public ::testing::TestWithParam<const char*> {
  49. // You can implement all the usual class fixture members here.
  50. };
  51. // Then, use the TEST_P macro to define as many parameterized tests
  52. // for this fixture as you want. The _P suffix is for "parameterized"
  53. // or "pattern", whichever you prefer to think.
  54. TEST_P(FooTest, DoesBlah) {
  55. // Inside a test, access the test parameter with the GetParam() method
  56. // of the TestWithParam<T> class:
  57. EXPECT_TRUE(foo.Blah(GetParam()));
  58. ...
  59. }
  60. TEST_P(FooTest, HasBlahBlah) {
  61. ...
  62. }
  63. // Finally, you can use INSTANTIATE_TEST_SUITE_P to instantiate the test
  64. // case with any set of parameters you want. Google Test defines a number
  65. // of functions for generating test parameters. They return what we call
  66. // (surprise!) parameter generators. Here is a summary of them, which
  67. // are all in the testing namespace:
  68. //
  69. //
  70. // Range(begin, end [, step]) - Yields values {begin, begin+step,
  71. // begin+step+step, ...}. The values do not
  72. // include end. step defaults to 1.
  73. // Values(v1, v2, ..., vN) - Yields values {v1, v2, ..., vN}.
  74. // ValuesIn(container) - Yields values from a C-style array, an STL
  75. // ValuesIn(begin,end) container, or an iterator range [begin, end).
  76. // Bool() - Yields sequence {false, true}.
  77. // Combine(g1, g2, ..., gN) - Yields all combinations (the Cartesian product
  78. // for the math savvy) of the values generated
  79. // by the N generators.
  80. //
  81. // For more details, see comments at the definitions of these functions below
  82. // in this file.
  83. //
  84. // The following statement will instantiate tests from the FooTest test suite
  85. // each with parameter values "meeny", "miny", and "moe".
  86. INSTANTIATE_TEST_SUITE_P(InstantiationName,
  87. FooTest,
  88. Values("meeny", "miny", "moe"));
  89. // To distinguish different instances of the pattern, (yes, you
  90. // can instantiate it more than once) the first argument to the
  91. // INSTANTIATE_TEST_SUITE_P macro is a prefix that will be added to the
  92. // actual test suite name. Remember to pick unique prefixes for different
  93. // instantiations. The tests from the instantiation above will have
  94. // these names:
  95. //
  96. // * InstantiationName/FooTest.DoesBlah/0 for "meeny"
  97. // * InstantiationName/FooTest.DoesBlah/1 for "miny"
  98. // * InstantiationName/FooTest.DoesBlah/2 for "moe"
  99. // * InstantiationName/FooTest.HasBlahBlah/0 for "meeny"
  100. // * InstantiationName/FooTest.HasBlahBlah/1 for "miny"
  101. // * InstantiationName/FooTest.HasBlahBlah/2 for "moe"
  102. //
  103. // You can use these names in --gtest_filter.
  104. //
  105. // This statement will instantiate all tests from FooTest again, each
  106. // with parameter values "cat" and "dog":
  107. const char* pets[] = {"cat", "dog"};
  108. INSTANTIATE_TEST_SUITE_P(AnotherInstantiationName, FooTest, ValuesIn(pets));
  109. // The tests from the instantiation above will have these names:
  110. //
  111. // * AnotherInstantiationName/FooTest.DoesBlah/0 for "cat"
  112. // * AnotherInstantiationName/FooTest.DoesBlah/1 for "dog"
  113. // * AnotherInstantiationName/FooTest.HasBlahBlah/0 for "cat"
  114. // * AnotherInstantiationName/FooTest.HasBlahBlah/1 for "dog"
  115. //
  116. // Please note that INSTANTIATE_TEST_SUITE_P will instantiate all tests
  117. // in the given test suite, whether their definitions come before or
  118. // AFTER the INSTANTIATE_TEST_SUITE_P statement.
  119. //
  120. // Please also note that generator expressions (including parameters to the
  121. // generators) are evaluated in InitGoogleTest(), after main() has started.
  122. // This allows the user on one hand, to adjust generator parameters in order
  123. // to dynamically determine a set of tests to run and on the other hand,
  124. // give the user a chance to inspect the generated tests with Google Test
  125. // reflection API before RUN_ALL_TESTS() is executed.
  126. //
  127. // You can see samples/sample7_unittest.cc and samples/sample8_unittest.cc
  128. // for more examples.
  129. //
  130. // In the future, we plan to publish the API for defining new parameter
  131. // generators. But for now this interface remains part of the internal
  132. // implementation and is subject to change.
  133. //
  134. //
  135. // A parameterized test fixture must be derived from testing::Test and from
  136. // testing::WithParamInterface<T>, where T is the type of the parameter
  137. // values. Inheriting from TestWithParam<T> satisfies that requirement because
  138. // TestWithParam<T> inherits from both Test and WithParamInterface. In more
  139. // complicated hierarchies, however, it is occasionally useful to inherit
  140. // separately from Test and WithParamInterface. For example:
  141. class BaseTest : public ::testing::Test {
  142. // You can inherit all the usual members for a non-parameterized test
  143. // fixture here.
  144. };
  145. class DerivedTest : public BaseTest, public ::testing::WithParamInterface<int> {
  146. // The usual test fixture members go here too.
  147. };
  148. TEST_F(BaseTest, HasFoo) {
  149. // This is an ordinary non-parameterized test.
  150. }
  151. TEST_P(DerivedTest, DoesBlah) {
  152. // GetParam works just the same here as if you inherit from TestWithParam.
  153. EXPECT_TRUE(foo.Blah(GetParam()));
  154. }
  155. #endif // 0
  156. #include <iterator>
  157. #include <utility>
  158. #include "gtest/internal/gtest-internal.h"
  159. #include "gtest/internal/gtest-param-util.h"
  160. #include "gtest/internal/gtest-port.h"
  161. namespace testing {
  162. // Functions producing parameter generators.
  163. //
  164. // Google Test uses these generators to produce parameters for value-
  165. // parameterized tests. When a parameterized test suite is instantiated
  166. // with a particular generator, Google Test creates and runs tests
  167. // for each element in the sequence produced by the generator.
  168. //
  169. // In the following sample, tests from test suite FooTest are instantiated
  170. // each three times with parameter values 3, 5, and 8:
  171. //
  172. // class FooTest : public TestWithParam<int> { ... };
  173. //
  174. // TEST_P(FooTest, TestThis) {
  175. // }
  176. // TEST_P(FooTest, TestThat) {
  177. // }
  178. // INSTANTIATE_TEST_SUITE_P(TestSequence, FooTest, Values(3, 5, 8));
  179. //
  180. // Range() returns generators providing sequences of values in a range.
  181. //
  182. // Synopsis:
  183. // Range(start, end)
  184. // - returns a generator producing a sequence of values {start, start+1,
  185. // start+2, ..., }.
  186. // Range(start, end, step)
  187. // - returns a generator producing a sequence of values {start, start+step,
  188. // start+step+step, ..., }.
  189. // Notes:
  190. // * The generated sequences never include end. For example, Range(1, 5)
  191. // returns a generator producing a sequence {1, 2, 3, 4}. Range(1, 9, 2)
  192. // returns a generator producing {1, 3, 5, 7}.
  193. // * start and end must have the same type. That type may be any integral or
  194. // floating-point type or a user defined type satisfying these conditions:
  195. // * It must be assignable (have operator=() defined).
  196. // * It must have operator+() (operator+(int-compatible type) for
  197. // two-operand version).
  198. // * It must have operator<() defined.
  199. // Elements in the resulting sequences will also have that type.
  200. // * Condition start < end must be satisfied in order for resulting sequences
  201. // to contain any elements.
  202. //
  203. template <typename T, typename IncrementT>
  204. internal::ParamGenerator<T> Range(T start, T end, IncrementT step) {
  205. return internal::ParamGenerator<T>(
  206. new internal::RangeGenerator<T, IncrementT>(start, end, step));
  207. }
  208. template <typename T>
  209. internal::ParamGenerator<T> Range(T start, T end) {
  210. return Range(start, end, 1);
  211. }
  212. // ValuesIn() function allows generation of tests with parameters coming from
  213. // a container.
  214. //
  215. // Synopsis:
  216. // ValuesIn(const T (&array)[N])
  217. // - returns a generator producing sequences with elements from
  218. // a C-style array.
  219. // ValuesIn(const Container& container)
  220. // - returns a generator producing sequences with elements from
  221. // an STL-style container.
  222. // ValuesIn(Iterator begin, Iterator end)
  223. // - returns a generator producing sequences with elements from
  224. // a range [begin, end) defined by a pair of STL-style iterators. These
  225. // iterators can also be plain C pointers.
  226. //
  227. // Please note that ValuesIn copies the values from the containers
  228. // passed in and keeps them to generate tests in RUN_ALL_TESTS().
  229. //
  230. // Examples:
  231. //
  232. // This instantiates tests from test suite StringTest
  233. // each with C-string values of "foo", "bar", and "baz":
  234. //
  235. // const char* strings[] = {"foo", "bar", "baz"};
  236. // INSTANTIATE_TEST_SUITE_P(StringSequence, StringTest, ValuesIn(strings));
  237. //
  238. // This instantiates tests from test suite StlStringTest
  239. // each with STL strings with values "a" and "b":
  240. //
  241. // ::std::vector< ::std::string> GetParameterStrings() {
  242. // ::std::vector< ::std::string> v;
  243. // v.push_back("a");
  244. // v.push_back("b");
  245. // return v;
  246. // }
  247. //
  248. // INSTANTIATE_TEST_SUITE_P(CharSequence,
  249. // StlStringTest,
  250. // ValuesIn(GetParameterStrings()));
  251. //
  252. //
  253. // This will also instantiate tests from CharTest
  254. // each with parameter values 'a' and 'b':
  255. //
  256. // ::std::list<char> GetParameterChars() {
  257. // ::std::list<char> list;
  258. // list.push_back('a');
  259. // list.push_back('b');
  260. // return list;
  261. // }
  262. // ::std::list<char> l = GetParameterChars();
  263. // INSTANTIATE_TEST_SUITE_P(CharSequence2,
  264. // CharTest,
  265. // ValuesIn(l.begin(), l.end()));
  266. //
  267. template <typename ForwardIterator>
  268. internal::ParamGenerator<
  269. typename std::iterator_traits<ForwardIterator>::value_type>
  270. ValuesIn(ForwardIterator begin, ForwardIterator end) {
  271. typedef typename std::iterator_traits<ForwardIterator>::value_type ParamType;
  272. return internal::ParamGenerator<ParamType>(
  273. new internal::ValuesInIteratorRangeGenerator<ParamType>(begin, end));
  274. }
  275. template <typename T, size_t N>
  276. internal::ParamGenerator<T> ValuesIn(const T (&array)[N]) {
  277. return ValuesIn(array, array + N);
  278. }
  279. template <class Container>
  280. internal::ParamGenerator<typename Container::value_type> ValuesIn(
  281. const Container& container) {
  282. return ValuesIn(container.begin(), container.end());
  283. }
  284. // Values() allows generating tests from explicitly specified list of
  285. // parameters.
  286. //
  287. // Synopsis:
  288. // Values(T v1, T v2, ..., T vN)
  289. // - returns a generator producing sequences with elements v1, v2, ..., vN.
  290. //
  291. // For example, this instantiates tests from test suite BarTest each
  292. // with values "one", "two", and "three":
  293. //
  294. // INSTANTIATE_TEST_SUITE_P(NumSequence,
  295. // BarTest,
  296. // Values("one", "two", "three"));
  297. //
  298. // This instantiates tests from test suite BazTest each with values 1, 2, 3.5.
  299. // The exact type of values will depend on the type of parameter in BazTest.
  300. //
  301. // INSTANTIATE_TEST_SUITE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5));
  302. //
  303. //
  304. template <typename... T>
  305. internal::ValueArray<T...> Values(T... v) {
  306. return internal::ValueArray<T...>(std::move(v)...);
  307. }
  308. // Bool() allows generating tests with parameters in a set of (false, true).
  309. //
  310. // Synopsis:
  311. // Bool()
  312. // - returns a generator producing sequences with elements {false, true}.
  313. //
  314. // It is useful when testing code that depends on Boolean flags. Combinations
  315. // of multiple flags can be tested when several Bool()'s are combined using
  316. // Combine() function.
  317. //
  318. // In the following example all tests in the test suite FlagDependentTest
  319. // will be instantiated twice with parameters false and true.
  320. //
  321. // class FlagDependentTest : public testing::TestWithParam<bool> {
  322. // virtual void SetUp() {
  323. // external_flag = GetParam();
  324. // }
  325. // }
  326. // INSTANTIATE_TEST_SUITE_P(BoolSequence, FlagDependentTest, Bool());
  327. //
  328. inline internal::ParamGenerator<bool> Bool() {
  329. return Values(false, true);
  330. }
  331. // Combine() allows the user to combine two or more sequences to produce
  332. // values of a Cartesian product of those sequences' elements.
  333. //
  334. // Synopsis:
  335. // Combine(gen1, gen2, ..., genN)
  336. // - returns a generator producing sequences with elements coming from
  337. // the Cartesian product of elements from the sequences generated by
  338. // gen1, gen2, ..., genN. The sequence elements will have a type of
  339. // std::tuple<T1, T2, ..., TN> where T1, T2, ..., TN are the types
  340. // of elements from sequences produces by gen1, gen2, ..., genN.
  341. //
  342. // Example:
  343. //
  344. // This will instantiate tests in test suite AnimalTest each one with
  345. // the parameter values tuple("cat", BLACK), tuple("cat", WHITE),
  346. // tuple("dog", BLACK), and tuple("dog", WHITE):
  347. //
  348. // enum Color { BLACK, GRAY, WHITE };
  349. // class AnimalTest
  350. // : public testing::TestWithParam<std::tuple<const char*, Color> > {...};
  351. //
  352. // TEST_P(AnimalTest, AnimalLooksNice) {...}
  353. //
  354. // INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest,
  355. // Combine(Values("cat", "dog"),
  356. // Values(BLACK, WHITE)));
  357. //
  358. // This will instantiate tests in FlagDependentTest with all variations of two
  359. // Boolean flags:
  360. //
  361. // class FlagDependentTest
  362. // : public testing::TestWithParam<std::tuple<bool, bool> > {
  363. // virtual void SetUp() {
  364. // // Assigns external_flag_1 and external_flag_2 values from the tuple.
  365. // std::tie(external_flag_1, external_flag_2) = GetParam();
  366. // }
  367. // };
  368. //
  369. // TEST_P(FlagDependentTest, TestFeature1) {
  370. // // Test your code using external_flag_1 and external_flag_2 here.
  371. // }
  372. // INSTANTIATE_TEST_SUITE_P(TwoBoolSequence, FlagDependentTest,
  373. // Combine(Bool(), Bool()));
  374. //
  375. template <typename... Generator>
  376. internal::CartesianProductHolder<Generator...> Combine(const Generator&... g) {
  377. return internal::CartesianProductHolder<Generator...>(g...);
  378. }
  379. #define TEST_P(test_suite_name, test_name) \
  380. class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
  381. : public test_suite_name { \
  382. public: \
  383. GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {} \
  384. void TestBody() override; \
  385. \
  386. private: \
  387. static int AddToRegistry() { \
  388. ::testing::UnitTest::GetInstance() \
  389. ->parameterized_test_registry() \
  390. .GetTestSuitePatternHolder<test_suite_name>( \
  391. GTEST_STRINGIFY_(test_suite_name), \
  392. ::testing::internal::CodeLocation(__FILE__, __LINE__)) \
  393. ->AddTestPattern( \
  394. GTEST_STRINGIFY_(test_suite_name), GTEST_STRINGIFY_(test_name), \
  395. new ::testing::internal::TestMetaFactory<GTEST_TEST_CLASS_NAME_( \
  396. test_suite_name, test_name)>(), \
  397. ::testing::internal::CodeLocation(__FILE__, __LINE__)); \
  398. return 0; \
  399. } \
  400. static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \
  401. GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_suite_name, \
  402. test_name)); \
  403. }; \
  404. int GTEST_TEST_CLASS_NAME_(test_suite_name, \
  405. test_name)::gtest_registering_dummy_ = \
  406. GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::AddToRegistry(); \
  407. void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody()
  408. // The last argument to INSTANTIATE_TEST_SUITE_P allows the user to specify
  409. // generator and an optional function or functor that generates custom test name
  410. // suffixes based on the test parameters. Such a function or functor should
  411. // accept one argument of type testing::TestParamInfo<class ParamType>, and
  412. // return std::string.
  413. //
  414. // testing::PrintToStringParamName is a builtin test suffix generator that
  415. // returns the value of testing::PrintToString(GetParam()).
  416. //
  417. // Note: test names must be non-empty, unique, and may only contain ASCII
  418. // alphanumeric characters or underscore. Because PrintToString adds quotes
  419. // to std::string and C strings, it won't work for these types.
  420. #define GTEST_EXPAND_(arg) arg
  421. #define GTEST_GET_FIRST_(first, ...) first
  422. #define GTEST_GET_SECOND_(first, second, ...) second
  423. #define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \
  424. static ::testing::internal::ParamGenerator<test_suite_name::ParamType> \
  425. gtest_##prefix##test_suite_name##_EvalGenerator_() { \
  426. return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \
  427. } \
  428. static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_( \
  429. const ::testing::TestParamInfo<test_suite_name::ParamType>& info) { \
  430. if (::testing::internal::AlwaysFalse()) { \
  431. ::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_( \
  432. __VA_ARGS__, \
  433. ::testing::internal::DefaultParamName<test_suite_name::ParamType>, \
  434. DUMMY_PARAM_))); \
  435. auto t = std::make_tuple(__VA_ARGS__); \
  436. static_assert(std::tuple_size<decltype(t)>::value <= 2, \
  437. "Too Many Args!"); \
  438. } \
  439. return ((GTEST_EXPAND_(GTEST_GET_SECOND_( \
  440. __VA_ARGS__, \
  441. ::testing::internal::DefaultParamName<test_suite_name::ParamType>, \
  442. DUMMY_PARAM_))))(info); \
  443. } \
  444. static int gtest_##prefix##test_suite_name##_dummy_ \
  445. GTEST_ATTRIBUTE_UNUSED_ = \
  446. ::testing::UnitTest::GetInstance() \
  447. ->parameterized_test_registry() \
  448. .GetTestSuitePatternHolder<test_suite_name>( \
  449. GTEST_STRINGIFY_(test_suite_name), \
  450. ::testing::internal::CodeLocation(__FILE__, __LINE__)) \
  451. ->AddTestSuiteInstantiation( \
  452. GTEST_STRINGIFY_(prefix), \
  453. &gtest_##prefix##test_suite_name##_EvalGenerator_, \
  454. &gtest_##prefix##test_suite_name##_EvalGenerateName_, \
  455. __FILE__, __LINE__)
  456. // Allow Marking a Parameterized test class as not needing to be instantiated.
  457. #define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(T) \
  458. namespace gtest_do_not_use_outside_namespace_scope {} \
  459. static const ::testing::internal::MarkAsIgnored gtest_allow_ignore_##T( \
  460. GTEST_STRINGIFY_(T))
  461. // Legacy API is deprecated but still available
  462. #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  463. #define INSTANTIATE_TEST_CASE_P \
  464. static_assert(::testing::internal::InstantiateTestCase_P_IsDeprecated(), \
  465. ""); \
  466. INSTANTIATE_TEST_SUITE_P
  467. #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  468. } // namespace testing
  469. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_