-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
160 lines (135 loc) · 5.09 KB
/
tests.cpp
File metadata and controls
160 lines (135 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <vector>
#include <list>
#include <forward_list>
#include <deque>
#include <array>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <span>
#include <valarray>
#include <tuple>
#include <string>
#include <cstdint>
#include "pretty_view\pretty_view.hpp"
using namespace rmsn;
template<typename T>
void test(const char *const name, const T& value) {
std::cout << std::string(10, '-') << "\n";
std::cout << "TEST " << name << "\n";
std::cout << "1st way:\n" << pretty_view<decltype(value)>{value} << "\n";
std::cout << "2st way:\n" << value << "\n";
std::cout << std::string(10, '-') << "\n";
}
namespace {
enum class gender {
MALE,
FEMALE
};
std::string to_string(const gender& gender) {
switch (gender) {
case gender::MALE:
return "male";
case gender::FEMALE:
return "female";
default:
return "";
}
}
class student {
std::uint8_t age_;
std::string name_;
gender gender_;
std::vector<std::uint16_t> marks_;
public:
template<typename String, typename Vector>
requires requires (const std::remove_reference_t<String>& string) { std::string{string}; } && std::is_same_v<std::remove_reference_t<Vector>, std::vector<std::uint16_t>>
explicit student(const std::uint8_t age, String&& name, const gender gender, Vector&& marks) noexcept : age_{age},
name_{std::forward<String>(name)},
gender_{gender},
marks_{std::forward<Vector>(marks)} {}
friend std::ostream& operator<<(std::ostream& os, const student& student) {
os << "student[\n"
<< "\tage=" << +student.age_ << ",\n"
<< "\tname=" << student.name_ << ",\n"
<< "\tgender=" << to_string(student.gender_) << ",\n"
<< "\tmarks=" << student.marks_ << "\n"
<< "]";
return os;
}
};
};
int main() {
const int arr[] = {1, 2, 3, 4, 5};
test("int arr[]", arr);
test("vector<int>", std::vector<int>{1, 2, 3});
test("list<int>", std::list<int>{1, 2, 3});
test("forward_list<int>", std::forward_list<int>{1, 2, 3});
test("deque<int>", std::deque<int>{4, 5, 6});
test("array<int,3>", std::array<int, 3>{7, 8, 9});
test("valarray<int>", std::valarray<int>{10, 11, 12});
test("set<int>", std::set<int>{1, 3, 2});
test("unordered_set<int>", std::unordered_set<int>{4, 5, 6});
test("map<int,string>", std::map<int, std::string>{{1, "hello"}, {2, "world"}});
test("unordered_map<string,int>", std::unordered_map<std::string, int>{{"x", 1}, {"y", 2}});
test("vector<string>", std::vector<std::string>{"hello", "world"});
test("span<char>", std::span<const char>("abc", 3));
test("tuple<int,double,const char*>", std::make_tuple(1, 2.5, "hi"));
test("pair<int,string>", std::pair<int, std::string>{10, "xx"});
const auto nested_tup = std::tuple<int, std::tuple<int, int>, std::pair<int, int>>{
10,
std::make_tuple(20, 30),
std::pair<int, int>{40, 50}
};
test("nested_tuple", nested_tup);
const std::vector<std::tuple<int, double, std::string>> vt = {
{1, 2.5, "a"},
{3, 4.5, "b"}
};
test("vector of tuples", vt);
const std::vector<std::vector<std::tuple<int, int>>> crazy = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
test("vector<vector<tuple>>", crazy);
const auto mega = std::make_tuple(
std::vector<int>{1, 2, 3},
std::set<std::string>{"x", "y"},
std::tuple<std::vector<int>, std::vector<std::vector<int>>>{
{5, 6},
{{7, 8}, {9, 10}}
}
);
test("super nested structure", mega);
const std::map<int, std::tuple<int, std::vector<int>, std::string>> mega_map{
{1, {10, {1, 2, 3}, "aaa"}},
{2, {20, {4, 5}, "bbb"}}
};
test("map<int, tuple<...>>", mega_map);
const std::vector<std::tuple<
int,
std::vector<std::string>,
std::tuple<std::vector<int>, std::string>
>> insane = {
{1, {"x", "y"}, {{1, 2, 3}, "lol"}}
};
test("insane nested tuple", insane);
test("empty vector<int>", std::vector<int>{});
test("empty list<string>", std::list<std::string>{});
test("empty set<int>", std::set<int>{});
test("empty tuple", std::tuple<>{});
test("vector<tuple{}>", std::vector<std::tuple<>>{std::tuple<>{}, std::tuple<>{}});
const int raw[4] = {1, 2, 3, 4};
const std::span s{raw};
test("std::span", s);
std::vector<student> students;
students.reserve(4);
students.emplace_back(22, "Roman", gender::MALE, std::vector<std::uint16_t>{4, 5, 3, 4});
students.emplace_back(22, "Vadim", gender::MALE, std::vector<std::uint16_t>{3, 5, 4});
students.emplace_back(21, "Anna", gender::FEMALE, std::vector<std::uint16_t>{3, 4, 3, 2});
students.emplace_back(18, "Vanrye", gender::MALE, std::vector<std::uint16_t>{5, 5, 5, 5, 5});
test("vector<custom_type>", students);
return 0;
}