blob: 7c45f135112de52ef50ea4c7b7fd3f2d9c3d0288 [file] [log] [blame]
ncihnegnc5458f22019-01-28 05:08:18 -08001#include <doctest/doctest.h>
onqtam119cfb62017-04-17 10:46:55 +03002
onqtamabf39d22017-10-28 21:30:45 +03003DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam119cfb62017-04-17 10:46:55 +03004#include <vector>
onqtamabf39d22017-10-28 21:30:45 +03005DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam119cfb62017-04-17 10:46:55 +03006
onqtamd320ac22017-04-17 11:44:32 +03007// =================================================================================================
8// NORMAL TEMPLATED TEST CASES
9// =================================================================================================
10
Stefane8ba7712022-04-28 02:47:24 +020011TYPE_TO_STRING_AS("SHORT!!!", short);
12
onqtambe460172018-05-30 18:15:58 +030013TEST_CASE_TEMPLATE("signed integers stuff", T, signed char, short, int) {
onqtamd320ac22017-04-17 11:44:32 +030014 T var = T();
15 --var;
16 CHECK(var == -1);
onqtam119cfb62017-04-17 10:46:55 +030017}
18
onqtamd320ac22017-04-17 11:44:32 +030019// teach the library how to stringify this type - otherwise <> will be used
20TYPE_TO_STRING(std::vector<int>);
onqtam119cfb62017-04-17 10:46:55 +030021
onqtambe460172018-05-30 18:15:58 +030022TEST_CASE_TEMPLATE("vector stuff", T, std::vector<int>) {
onqtamd320ac22017-04-17 11:44:32 +030023 T vec(10);
Viktor Kirilov09f55ca2018-03-05 19:34:25 +070024 CHECK(vec.size() == 20); // will fail
onqtam119cfb62017-04-17 10:46:55 +030025}
26
onqtamd320ac22017-04-17 11:44:32 +030027// =================================================================================================
28// NAMED TEMPLATED TEST CASES WITH DEFERRED INSTANTIATION
29// =================================================================================================
onqtam119cfb62017-04-17 10:46:55 +030030
onqtamd320ac22017-04-17 11:44:32 +030031TEST_CASE_TEMPLATE_DEFINE("default construction", T, test_id) {
32 T var = T();
33 CHECK(doctest::Approx(var) == T());
onqtam119cfb62017-04-17 10:46:55 +030034}
35
onqtam88911e32019-03-20 21:00:05 +020036TEST_CASE_TEMPLATE_INVOKE(test_id, signed char, short, int);
37TEST_CASE_TEMPLATE_INVOKE(test_id, double, double); // note that types won't be filtered for uniqueness
onqtam119cfb62017-04-17 10:46:55 +030038
onqtam88911e32019-03-20 21:00:05 +020039TEST_CASE_TEMPLATE_APPLY(test_id, std::tuple<unsigned char, char>);
zhihaoy73ff6122019-03-20 12:37:17 -050040
onqtamd320ac22017-04-17 11:44:32 +030041// =================================================================================================
42// MULTIPLE TYPES AS PARAMETERS
43// =================================================================================================
44
45template <typename first, typename second>
46struct TypePair
onqtam119cfb62017-04-17 10:46:55 +030047{
onqtamd320ac22017-04-17 11:44:32 +030048 typedef first A;
49 typedef second B;
onqtam119cfb62017-04-17 10:46:55 +030050};
51
Stefane8ba7712022-04-28 02:47:24 +020052TYPE_TO_STRING_AS("Custom name test", TypePair<int, char>);
53TYPE_TO_STRING_AS("Other custom name", TypePair<char, int>);
54TYPE_TO_STRING(TypePair<bool, int>);
55
onqtam4317c822018-05-30 19:18:55 +030056TEST_CASE_TEMPLATE("multiple types", T, TypePair<int, char>, TypePair<char, int>, TypePair<bool, int>) {
onqtamd320ac22017-04-17 11:44:32 +030057 typedef typename T::A T1;
58 typedef typename T::B T2;
onqtamf7153252017-04-19 21:41:31 +030059 T1 t1 = T1();
60 T2 t2 = T2();
onqtamd320ac22017-04-17 11:44:32 +030061 // use T1 and T2 types
onqtamc79b7542017-04-19 20:47:13 +030062 CHECK(t1 == T1());
63 CHECK(t2 != T2());
onqtam119cfb62017-04-17 10:46:55 +030064}
onqtam8c311762017-04-17 23:58:56 +030065
onqtam8c311762017-04-17 23:58:56 +030066// currently the string result will be "int_pair" instead of "TypePair<int, int>" because of the way the type stringification works
67typedef TypePair<int, int> int_pair;
68TYPE_TO_STRING(int_pair);
69
onqtambe460172018-05-30 18:15:58 +030070TEST_CASE_TEMPLATE("bad stringification of type pair", T, int_pair) {
onqtam8c311762017-04-17 23:58:56 +030071 typedef typename T::A T1;
72 typedef typename T::B T2;
onqtamf7153252017-04-19 21:41:31 +030073 T1 t1 = T1();
74 T2 t2 = T2();
onqtam8c311762017-04-17 23:58:56 +030075 // use T1 and T2 types
onqtamc79b7542017-04-19 20:47:13 +030076 CHECK(t1 == T1());
77 CHECK(t2 != T2());
onqtam8c311762017-04-17 23:58:56 +030078}