-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoly_test_1.cpp
More file actions
91 lines (82 loc) · 3.11 KB
/
poly_test_1.cpp
File metadata and controls
91 lines (82 loc) · 3.11 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
#include "poly.h"
#include <cstddef>
#include <array>
#include <type_traits>
namespace
{
template <typename T, std::size_t N>
constexpr bool operator==(const poly<T, N>& a, const poly<T, N>& b)
{
for (std::size_t i = 0; i < N; ++i)
if (!(a[i] == b[i]))
return false;
return true;
}
void test_static()
{
constexpr auto p = poly(2, 1);
static_assert(std::is_same_v<decltype(p), const poly<int, 2>>);
static_assert(p[0] == 2);
static_assert(p.at(1) == 3);
static_assert(p.at(0.5) == 2.5);
static_assert(p.at(p) == poly(4, 1));
static_assert(std::is_same_v<std::common_type_t<double, poly<int, 2>>, poly<double, 2>>);
static_assert(std::is_same_v<std::common_type_t<poly<int, 2>, poly<double, 2>>, poly<double, 2>>);
constexpr auto q = poly(poly(1.0, 2.0), 3.0, 4.0);
static_assert(std::is_same_v<decltype(q), const poly<poly<double, 2>, 3>>);
static_assert(q[0][0] == 1.0);
static_assert(q[0][1] == 2.0);
static_assert(q[1][0] == 3.0);
static_assert(q[1][1] == 0.0);
static_assert(q[2][0] == 4.0);
static_assert(q[2][1] == 0.0);
static_assert(q.at() == q);
static_assert(q.at(2.0, 3.0) == 29.0);
static_assert(q.at(2.0, 3.0, 4.0) == 29.0);
static_assert(q.at(2.0) == poly(23.0, 2.0));
static_assert(q.at(2.0).at(3.0) == q.at(2.0, 3.0));
static_assert(q.at(p) == poly(23.0, 21.0, 4.0, 0.0));
static_assert(q.at(-1, p) == poly(6.0, 2.0));
static_assert(q.at(std::array{2.0, 3.0}) == 29.0);
static_assert(std::is_same_v<decltype(p + q), poly<poly<double, 2>, 3>>);
static_assert(p + q == poly(poly(3.0, 2.0), 4.0, 4.0));
static_assert(-q == poly(poly(-1.0, -2.0), -3.0, -4.0));
static_assert(p - q == poly(poly(1.0, -2.0), -2.0, -4.0));
static_assert(q - p == poly(poly(-1.0, 2.0), 2.0, 4.0));
static_assert(std::is_same_v<decltype(p * q), poly<poly<double, 2>, 4>>);
static_assert(p * q == poly(poly(2.0, 4.0), poly(7.0, 2.0), 11.0, 4.0));
static_assert(cross(p, q) == poly(2.0 * q, q));
static_assert(cross(q, p) == poly(poly(p, 2.0 * p), const_poly(3.0 * p), const_poly(4.0 * p)));
constexpr auto pp(p);
static_assert(p == pp);
constexpr auto qq(q);
static_assert(q == qq);
}
template <typename T>
class weak_wrapper
{
private:
T a;
public:
weak_wrapper() {}
weak_wrapper(T a) : a(a) {}
weak_wrapper& operator-=(weak_wrapper b) {a -= b.a; return *this;}
weak_wrapper& operator+=(weak_wrapper b) {a += b.a; return *this;}
weak_wrapper& operator*=(weak_wrapper b) {a *= b.a; return *this;}
template <typename U>
friend weak_wrapper<U> operator*(weak_wrapper<U> a, weak_wrapper<U> b) {return a.a * b.a;}
};
void test_weak()
{
using t = weak_wrapper<int>;
poly(t(2)) * t(3);
poly(t(2)) + poly(t(3)); // Może się kompilować, ale nie musi.
// t(2) + t(3); // Skutkuje błędem podczas kompilowania.
// t(2) - t(3); // Skutkuje błędem podczas kompilowania.
}
} // anonimowa przestrzeń nazw
int main()
{
test_static();
test_weak();
}