blob: 59a52e8264fe9d725aa4a2c4e784abe0a131a73d [file] [log] [blame]
onqtam119cfb62017-04-17 10:46:55 +03001#include "doctest.h"
2
onqtam119cfb62017-04-17 10:46:55 +03003#include <vector>
onqtam119cfb62017-04-17 10:46:55 +03004
onqtamd320ac22017-04-17 11:44:32 +03005// typedefs are required if variadic macro support is not available (otherwise the commas are a problem)
6typedef doctest::Types<char, short, int> int_types;
onqtam8c311762017-04-17 23:58:56 +03007typedef doctest::Types<float, double, float> float_types; // note that types won't be filtered for uniqueness
onqtamd320ac22017-04-17 11:44:32 +03008
9// =================================================================================================
10// NORMAL TEMPLATED TEST CASES
11// =================================================================================================
12
13TEST_CASE_TEMPLATE("signed integers stuff", T, int_types) {
14 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
onqtamd320ac22017-04-17 11:44:32 +030022TEST_CASE_TEMPLATE("vector stuff", T, doctest::Types<std::vector<int> >) {
23 T vec(10);
24 CHECK(vec.size() == 20); // fill 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
onqtamd320ac22017-04-17 11:44:32 +030036TEST_CASE_TEMPLATE_INSTANTIATE(test_id, int_types);
37TEST_CASE_TEMPLATE_INSTANTIATE(test_id, float_types);
onqtam119cfb62017-04-17 10:46:55 +030038
onqtamd320ac22017-04-17 11:44:32 +030039// =================================================================================================
40// MULTIPLE TYPES AS PARAMETERS
41// =================================================================================================
42
43template <typename first, typename second>
44struct TypePair
onqtam119cfb62017-04-17 10:46:55 +030045{
onqtamd320ac22017-04-17 11:44:32 +030046 typedef first A;
47 typedef second B;
onqtam119cfb62017-04-17 10:46:55 +030048};
49
onqtamd320ac22017-04-17 11:44:32 +030050typedef doctest::Types<
51 TypePair<int, char>,
52 TypePair<char, int>,
53 TypePair<bool, int>
54> pairs;
onqtam119cfb62017-04-17 10:46:55 +030055
onqtamd320ac22017-04-17 11:44:32 +030056TEST_CASE_TEMPLATE("multiple types", T, pairs) {
57 typedef typename T::A T1;
58 typedef typename T::B T2;
59 // use T1 and T2 types
60 CHECK(T1() == T1());
61 CHECK(T2() != T2());
onqtam119cfb62017-04-17 10:46:55 +030062}
onqtam8c311762017-04-17 23:58:56 +030063
64// if variadic macros are supported then "TypePair<int, int>" can be passed directly to the macro (otherwise the commas are a problem)
65// currently the string result will be "int_pair" instead of "TypePair<int, int>" because of the way the type stringification works
66typedef TypePair<int, int> int_pair;
67TYPE_TO_STRING(int_pair);
68
69TEST_CASE_TEMPLATE("bad stringification of type pair", T, doctest::Types<int_pair>) {
70 typedef typename T::A T1;
71 typedef typename T::B T2;
72 // use T1 and T2 types
73 CHECK(T1() == T1());
74 CHECK(T2() != T2());
75}