source: mainline/uspace/lib/cpp/src/__bits/test/tuple.cpp@ 7bbf91e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7bbf91e was 7bbf91e, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: changed internal to bits to avoid include space pollusion, fixed old std::hel:: bugs in files that weren't touched since

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <functional>
30#include <initializer_list>
31#include <__bits/test/tests.hpp>
32#include <string>
33#include <tuple>
34#include <type_traits>
35#include <utility>
36
37namespace std::test
38{
39 bool tuple_test::run(bool report)
40 {
41 report_ = report;
42 start();
43
44 test_constructors_and_assignment();
45 test_creation();
46 test_tie_and_structured_bindings();
47 test_tuple_ops();
48
49 return end();
50 }
51
52 const char* tuple_test::name()
53 {
54 return "tuple";
55 }
56
57 void tuple_test::test_constructors_and_assignment()
58 {
59 std::tuple<int, float> tpl1{1, .5f};
60 test_eq("value initialization pt1", std::get<0>(tpl1), 1);
61 test_eq("value initialization pt2", std::get<1>(tpl1), .5f);
62
63 auto p = std::make_pair(2, 1.f);
64 std::tuple<int, float> tpl2{p};
65 test_eq("pair initialization pt1", std::get<0>(tpl2), 2);
66 test_eq("pair initialization pt2", std::get<1>(tpl2), 1.f);
67
68 tpl1 = p;
69 test_eq("pair assignment pt1", std::get<0>(tpl1), 2);
70 test_eq("pair assignment pt2", std::get<1>(tpl1), 1.f);
71
72 auto tpl3 = std::make_tuple(std::string{"A"}, std::string{"B"});
73 auto tpl4 = std::make_tuple(std::string{"C"}, std::string{"D"});
74 tpl3 = std::move(tpl4);
75 test_eq("move assignment pt1", std::get<0>(tpl3), std::string{"C"});
76 test_eq("move assignment pt2", std::get<1>(tpl3), std::string{"D"});
77
78 auto tpl5 = std::make_tuple(1, .5f);
79 auto tpl6{std::move(tpl5)};
80 test_eq("move initialization pt1", std::get<0>(tpl6), 1);
81 test_eq("move initialization pt2", std::get<1>(tpl6), .5f);
82 }
83
84 void tuple_test::test_creation()
85 {
86 auto tpl1 = std::make_tuple(1, .5f, std::string{"test"}, true);
87 static_assert(std::is_same_v<std::tuple_element_t<0, decltype(tpl1)>, int>);
88 static_assert(std::is_same_v<std::tuple_element_t<1, decltype(tpl1)>, float>);
89 static_assert(std::is_same_v<std::tuple_element_t<2, decltype(tpl1)>, std::string>);
90 static_assert(std::is_same_v<std::tuple_element_t<3, decltype(tpl1)>, bool>);
91
92 test_eq("make_tuple pt1", std::get<0>(tpl1), 1);
93 test_eq("make_tuple pt2", std::get<1>(tpl1), .5f);
94 test_eq("make_tuple pt3", std::get<2>(tpl1), std::string{"test"});
95 test_eq("make_tuple pt4", std::get<3>(tpl1), true);
96
97 static_assert(std::tuple_size_v<decltype(tpl1)> == 4);
98
99 int i{};
100 float f{};
101 auto tpl2 = std::make_tuple(std::ref(i), std::cref(f));
102 static_assert(std::is_same_v<std::tuple_element_t<0, decltype(tpl2)>, int&>);
103 static_assert(std::is_same_v<std::tuple_element_t<1, decltype(tpl2)>, const float&>);
104
105 std::get<0>(tpl2) = 3;
106 test_eq("modify reference in tuple", i, 3);
107
108 auto tpl3 = std::forward_as_tuple(i, f);
109 static_assert(std::is_same_v<std::tuple_element_t<0, decltype(tpl3)>, int&>);
110 static_assert(std::is_same_v<std::tuple_element_t<1, decltype(tpl3)>, float&>);
111
112 std::get<1>(tpl3) = 1.5f;
113 test_eq("modify reference in forward_as_tuple", f, 1.5f);
114 }
115
116 void tuple_test::test_tie_and_structured_bindings()
117 {
118 int i1{};
119 float f1{};
120 auto tpl = std::make_tuple(1, .5f);
121 std::tie(i1, f1) = tpl;
122
123 test_eq("tie unpack pt1", i1, 1);
124 test_eq("tie unpack pt2", f1, .5f);
125
126 std::get<0>(tpl) = 2;
127 std::tie(i1, std::ignore) = tpl;
128
129 test_eq("tie unpack with ignore", i1, 2);
130
131 auto [i2, f2] = tpl;
132 test_eq("structured bindings pt1", i2, 2);
133 test_eq("structured bindings pt2", f2, .5f);
134 }
135
136 void tuple_test::test_tuple_ops()
137 {
138 auto tpl1 = std::make_tuple(1, .5f);
139 auto tpl2 = std::make_tuple(1, .5f);
140 auto tpl3 = std::make_tuple(1, 1.f);
141 auto tpl4 = std::make_tuple(2, .5f);
142 auto tpl5 = std::make_tuple(2, 1.f);
143
144 test_eq("tuple == pt1", (tpl1 == tpl2), true);
145 test_eq("tuple == pt2", (tpl1 == tpl3), false);
146 test_eq("tuple == pt3", (tpl1 == tpl4), false);
147 test_eq("tuple < pt1", (tpl1 < tpl2), false);
148 test_eq("tuple < pt2", (tpl1 < tpl3), true);
149 test_eq("tuple < pt3", (tpl1 < tpl4), true);
150 test_eq("tuple < pt4", (tpl1 < tpl5), true);
151
152 tpl1.swap(tpl5);
153 test_eq("tuple swap pt1", std::get<0>(tpl1), 2);
154 test_eq("tuple swap pt2", std::get<1>(tpl1), 1.f);
155 test_eq("tuple swap pt3", std::get<0>(tpl5), 1);
156 test_eq("tuple swap pt4", std::get<1>(tpl5), .5f);
157 }
158}
Note: See TracBrowser for help on using the repository browser.