-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_midpoint.cpp
103 lines (86 loc) · 2.48 KB
/
test_midpoint.cpp
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
/*
/home/ed/bin/bin/g++ -std=c++20 -g -Wall -Wextra -o test_midpoint test_midpoint.cpp
LD_LIBRARY_PATH=/home/ed/bin/lib64 ./test_midpoint
*/
#include <limits>
#include <type_traits>
#include "midpoint.h"
#include <iostream>
template<typename Int>
bool
test_twos_comp()
{
const std::make_unsigned_t<Int> a = 10, b = 5;
const auto mab = std::midpoint(a, b);
std::cout << "mab = " << int(mab) << '\n';
return true;
}
template<typename Real>
bool
test_huge()
{
const auto huge = std::numeric_limits<Real>::max();
const auto a = -huge;
const auto b = +huge;
const auto c = std::midpoint(a, b);
std::cout << "c = " << c << '\n';
std::cout << "c == 0: " << std::boolalpha << (c == Real{0}) << '\n';
return c == Real{0};
}
template<typename Tp>
bool
test_ptr()
{
constexpr int num = 21;
constexpr Tp arr[num]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
const auto a = arr;
const auto b = arr + num;
auto ab = std::midpoint(a, b);
std::cout << "a = " << a << "; b = " << b << "; ab = " << ab << '\n';
std::cout << "ab - a = " << ab - a << "; ab - b = " << ab - b << '\n';
std::cout << "ab = " << ab << "; *ab = " << *ab << '\n';
auto ba = std::midpoint(b-1, a-1); // rbegin, rend
std::cout << "ba - (b-1) = " << ba - (b-1) << "; ba - (a-1) = " << ba - (a-1) << '\n';
std::cout << "ba = " << ba << "; *ba = " << *ba << '\n';
return true;
}
bool
test_tiny()
{
auto d = std::numeric_limits<double>::max_digits10;
std::cout.precision(d);
const double K = 4.4501477170144023e-308;
const double X[]
{
K, std::midpoint(K, K)
};
std::cout << "X[0] = " << X[0] << '\n';
std::cout << "X[1] = " << X[1] << '\n';
return 0 == (X[1] - X[0]);
}
int
main()
{
bool ok = true;
const int a = 5, b = 10;
const auto mab = std::midpoint(a, b);
std::cout << "mab = " << mab << '\n';
std::cout << "\ntest_huge\n";
ok = ok && test_huge<float>();
ok = ok && test_huge<double>();
ok = ok && test_huge<long double>();
std::cout << "\ntest_twos_comp\n";
ok = ok && test_twos_comp<unsigned char>();
ok = ok && test_twos_comp<short>();
ok = ok && test_twos_comp<int>();
ok = ok && test_twos_comp<long>();
ok = ok && test_twos_comp<long long>();
std::cout << "\ntest_ptr\n";
ok = ok && test_ptr<int>();
ok = test_tiny();
std::cout << "\ntest 42\n";
auto m42 = std::midpoint(42, 42);
std::cout << "midpoint(42, 42) = " << m42 << '\n';
ok = ok && (m42 == 42);
return ok;
}