| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <__bits/test/tests.hpp>
|
|---|
| 8 | #include <functional>
|
|---|
| 9 | #include <initializer_list>
|
|---|
| 10 | #include <string>
|
|---|
| 11 | #include <tuple>
|
|---|
| 12 | #include <type_traits>
|
|---|
| 13 | #include <utility>
|
|---|
| 14 |
|
|---|
| 15 | namespace std::test
|
|---|
| 16 | {
|
|---|
| 17 | bool tuple_test::run(bool report)
|
|---|
| 18 | {
|
|---|
| 19 | report_ = report;
|
|---|
| 20 | start();
|
|---|
| 21 |
|
|---|
| 22 | test_constructors_and_assignment();
|
|---|
| 23 | test_creation();
|
|---|
| 24 | test_tie_and_structured_bindings();
|
|---|
| 25 | test_tuple_ops();
|
|---|
| 26 |
|
|---|
| 27 | return end();
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | const char* tuple_test::name()
|
|---|
| 31 | {
|
|---|
| 32 | return "tuple";
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | void tuple_test::test_constructors_and_assignment()
|
|---|
| 36 | {
|
|---|
| 37 | std::tuple<int, float> tpl1{1, .5f};
|
|---|
| 38 | test_eq("value initialization pt1", std::get<0>(tpl1), 1);
|
|---|
| 39 | test_eq("value initialization pt2", std::get<1>(tpl1), .5f);
|
|---|
| 40 |
|
|---|
| 41 | auto p = std::make_pair(2, 1.f);
|
|---|
| 42 | std::tuple<int, float> tpl2{p};
|
|---|
| 43 | test_eq("pair initialization pt1", std::get<0>(tpl2), 2);
|
|---|
| 44 | test_eq("pair initialization pt2", std::get<1>(tpl2), 1.f);
|
|---|
| 45 |
|
|---|
| 46 | tpl1 = p;
|
|---|
| 47 | test_eq("pair assignment pt1", std::get<0>(tpl1), 2);
|
|---|
| 48 | test_eq("pair assignment pt2", std::get<1>(tpl1), 1.f);
|
|---|
| 49 |
|
|---|
| 50 | auto tpl3 = std::make_tuple(std::string{"A"}, std::string{"B"});
|
|---|
| 51 | auto tpl4 = std::make_tuple(std::string{"C"}, std::string{"D"});
|
|---|
| 52 | tpl3 = std::move(tpl4);
|
|---|
| 53 | test_eq("move assignment pt1", std::get<0>(tpl3), std::string{"C"});
|
|---|
| 54 | test_eq("move assignment pt2", std::get<1>(tpl3), std::string{"D"});
|
|---|
| 55 |
|
|---|
| 56 | auto tpl5 = std::make_tuple(1, .5f);
|
|---|
| 57 | auto tpl6{std::move(tpl5)};
|
|---|
| 58 | test_eq("move initialization pt1", std::get<0>(tpl6), 1);
|
|---|
| 59 | test_eq("move initialization pt2", std::get<1>(tpl6), .5f);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void tuple_test::test_creation()
|
|---|
| 63 | {
|
|---|
| 64 | auto tpl1 = std::make_tuple(1, .5f, std::string{"test"}, true);
|
|---|
| 65 | static_assert(std::is_same_v<std::tuple_element_t<0, decltype(tpl1)>, int>);
|
|---|
| 66 | static_assert(std::is_same_v<std::tuple_element_t<1, decltype(tpl1)>, float>);
|
|---|
| 67 | static_assert(std::is_same_v<std::tuple_element_t<2, decltype(tpl1)>, std::string>);
|
|---|
| 68 | static_assert(std::is_same_v<std::tuple_element_t<3, decltype(tpl1)>, bool>);
|
|---|
| 69 |
|
|---|
| 70 | test_eq("make_tuple pt1", std::get<0>(tpl1), 1);
|
|---|
| 71 | test_eq("make_tuple pt2", std::get<1>(tpl1), .5f);
|
|---|
| 72 | test_eq("make_tuple pt3", std::get<2>(tpl1), std::string{"test"});
|
|---|
| 73 | test_eq("make_tuple pt4", std::get<3>(tpl1), true);
|
|---|
| 74 |
|
|---|
| 75 | static_assert(std::tuple_size_v<decltype(tpl1)> == 4);
|
|---|
| 76 |
|
|---|
| 77 | int i{};
|
|---|
| 78 | float f{};
|
|---|
| 79 | auto tpl2 = std::make_tuple(std::ref(i), std::cref(f));
|
|---|
| 80 | static_assert(std::is_same_v<std::tuple_element_t<0, decltype(tpl2)>, int&>);
|
|---|
| 81 | static_assert(std::is_same_v<std::tuple_element_t<1, decltype(tpl2)>, const float&>);
|
|---|
| 82 |
|
|---|
| 83 | std::get<0>(tpl2) = 3;
|
|---|
| 84 | test_eq("modify reference in tuple", i, 3);
|
|---|
| 85 |
|
|---|
| 86 | auto tpl3 = std::forward_as_tuple(i, f);
|
|---|
| 87 | static_assert(std::is_same_v<std::tuple_element_t<0, decltype(tpl3)>, int&>);
|
|---|
| 88 | static_assert(std::is_same_v<std::tuple_element_t<1, decltype(tpl3)>, float&>);
|
|---|
| 89 |
|
|---|
| 90 | std::get<1>(tpl3) = 1.5f;
|
|---|
| 91 | test_eq("modify reference in forward_as_tuple", f, 1.5f);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | void tuple_test::test_tie_and_structured_bindings()
|
|---|
| 95 | {
|
|---|
| 96 | int i1{};
|
|---|
| 97 | float f1{};
|
|---|
| 98 | auto tpl = std::make_tuple(1, .5f);
|
|---|
| 99 | std::tie(i1, f1) = tpl;
|
|---|
| 100 |
|
|---|
| 101 | test_eq("tie unpack pt1", i1, 1);
|
|---|
| 102 | test_eq("tie unpack pt2", f1, .5f);
|
|---|
| 103 |
|
|---|
| 104 | std::get<0>(tpl) = 2;
|
|---|
| 105 | /* std::tie(i1, std::ignore) = tpl; */
|
|---|
| 106 |
|
|---|
| 107 | /* test_eq("tie unpack with ignore", i1, 2); */
|
|---|
| 108 |
|
|---|
| 109 | auto [i2, f2] = tpl;
|
|---|
| 110 | test_eq("structured bindings pt1", i2, 2);
|
|---|
| 111 | test_eq("structured bindings pt2", f2, .5f);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | void tuple_test::test_tuple_ops()
|
|---|
| 115 | {
|
|---|
| 116 | auto tpl1 = std::make_tuple(1, .5f);
|
|---|
| 117 | auto tpl2 = std::make_tuple(1, .5f);
|
|---|
| 118 | auto tpl3 = std::make_tuple(1, 1.f);
|
|---|
| 119 | auto tpl4 = std::make_tuple(2, .5f);
|
|---|
| 120 | auto tpl5 = std::make_tuple(2, 1.f);
|
|---|
| 121 |
|
|---|
| 122 | test_eq("tuple == pt1", (tpl1 == tpl2), true);
|
|---|
| 123 | test_eq("tuple == pt2", (tpl1 == tpl3), false);
|
|---|
| 124 | test_eq("tuple == pt3", (tpl1 == tpl4), false);
|
|---|
| 125 | test_eq("tuple < pt1", (tpl1 < tpl2), false);
|
|---|
| 126 | test_eq("tuple < pt2", (tpl1 < tpl3), true);
|
|---|
| 127 | test_eq("tuple < pt3", (tpl1 < tpl4), true);
|
|---|
| 128 | test_eq("tuple < pt4", (tpl1 < tpl5), true);
|
|---|
| 129 |
|
|---|
| 130 | tpl1.swap(tpl5);
|
|---|
| 131 | test_eq("tuple swap pt1", std::get<0>(tpl1), 2);
|
|---|
| 132 | test_eq("tuple swap pt2", std::get<1>(tpl1), 1.f);
|
|---|
| 133 | test_eq("tuple swap pt3", std::get<0>(tpl5), 1);
|
|---|
| 134 | test_eq("tuple swap pt4", std::get<1>(tpl5), .5f);
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|