1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 // tbb::flow::tuple (implementation used in tbb::flow)
18 // if <tuple> is available on the compiler/platform, that version should be the
19 // one tested.
20 
21 #include "harness.h"
22 // this test should match that in graph.h, so we test whatever tuple is
23 // being used by the join_node.
24 #if __TBB_CPP11_TUPLE_PRESENT
25 #define __TESTING_STD_TUPLE__ 1
26 #include <tuple>
27 using namespace std;
28 #else
29 #define __TESTING_STD_TUPLE__ 0
30 #include "tbb/compat/tuple"
31 using namespace tbb::flow;
32 #endif /*!__TBB_CPP11_TUPLE_PRESENT*/
33 #include <string>
34 #include <iostream>
35 
36 class non_trivial {
37 public:
non_trivial()38     non_trivial() {}
~non_trivial()39     ~non_trivial() {}
non_trivial(const non_trivial & other)40     non_trivial(const non_trivial& other) : my_int(other.my_int), my_float(other.my_float) { }
get_int() const41     int get_int() const { return my_int; }
get_float() const42     float get_float() const { return my_float; }
set_int(int newval)43     void set_int(int newval) { my_int = newval; }
set_float(float newval)44     void set_float(float newval) { my_float = newval; }
45 private:
46     int my_int;
47     float my_float;
48 };
49 
50 template<typename T1, typename T2, typename T3, typename U1, typename U2, typename U3>
RunOneComparisonTest()51 void RunOneComparisonTest() {
52     typedef tuple<T1,T2,T3> t_tuple;
53     typedef tuple<U1,U2,U3> u_tuple;
54 
55     ASSERT(t_tuple((T1)1,(T2)1,(T3)1) == u_tuple((U1)1,(U2)1,(U3)1),NULL);
56     ASSERT(t_tuple((T1)1,(T2)0,(T3)1) <  u_tuple((U1)1,(U2)1,(U3)1),NULL);
57     ASSERT(t_tuple((T1)1,(T2)1,(T3)1) >  u_tuple((U1)1,(U2)1,(U3)0),NULL);
58     ASSERT(t_tuple((T1)1,(T2)0,(T3)1) != u_tuple((U1)1,(U2)1,(U3)1),NULL);
59     ASSERT(t_tuple((T1)1,(T2)0,(T3)1) <= u_tuple((U1)1,(U2)1,(U3)0),NULL);
60     ASSERT(t_tuple((T1)1,(T2)0,(T3)0) <= u_tuple((U1)1,(U2)0,(U3)0),NULL);
61     ASSERT(t_tuple((T1)1,(T2)1,(T3)1) >= u_tuple((U1)1,(U2)0,(U3)1),NULL);
62     ASSERT(t_tuple((T1)0,(T2)1,(T3)1) >= u_tuple((U1)0,(U2)1,(U3)1),NULL);
63 
64     ASSERT(!(t_tuple((T1)2,(T2)1,(T3)1) == u_tuple((U1)1,(U2)1,(U3)1)),NULL);
65     ASSERT(!(t_tuple((T1)1,(T2)2,(T3)1) == u_tuple((U1)1,(U2)1,(U3)1)),NULL);
66     ASSERT(!(t_tuple((T1)1,(T2)1,(T3)2) == u_tuple((U1)1,(U2)1,(U3)1)),NULL);
67 
68     ASSERT(!(t_tuple((T1)1,(T2)1,(T3)1) <  u_tuple((U1)1,(U2)1,(U3)1)),NULL);
69     ASSERT(!(t_tuple((T1)1,(T2)1,(T3)1) >  u_tuple((U1)1,(U2)1,(U3)1)),NULL);
70     ASSERT(!(t_tuple((T1)1,(T2)1,(T3)1) !=  u_tuple((U1)1,(U2)1,(U3)1)),NULL);
71 
72     ASSERT(t_tuple((T1)1,(T2)1,(T3)1) <= u_tuple((U1)1,(U2)1,(U3)1),NULL);
73     ASSERT(t_tuple((T1)1,(T2)1,(T3)1) >= u_tuple((U1)1,(U2)1,(U3)1),NULL);
74 
75 }
76 
77 #include "harness_defs.h"
78 
RunTests()79 void RunTests() {
80 
81 #if __TESTING_STD_TUPLE__
82     REMARK("Testing platform tuple\n");
83 #else
84     REMARK("Testing compat/tuple\n");
85 #endif
86     tuple<int> ituple1(3);
87     tuple<int> ituple2(5);
88     tuple<double> ftuple2(4.1);
89 
90     ASSERT(!(ituple1 == ituple2), NULL);
91     ASSERT(ituple1 != ituple2, NULL);
92     ASSERT(!(ituple1 > ituple2), NULL);
93     ASSERT(ituple1 < ituple2, NULL);
94     ASSERT(ituple1 <= ituple2, NULL);
95     ASSERT(!(ituple1 >= ituple2), NULL);
96     ASSERT(ituple1 < ftuple2, NULL);
97 
98     typedef tuple<int,double,float> tuple_type1;
99     typedef tuple<int,int,int> int_tuple_type;
100     typedef tuple<int,non_trivial,int> non_trivial_tuple_type;
101     typedef tuple<double,std::string,char> stringy_tuple_type;
102     const tuple_type1 tup1(42,3.14159,2.0f);
103     int_tuple_type int_tup(4, 5, 6);
104     non_trivial_tuple_type nti;
105     stringy_tuple_type stv;
106     get<1>(stv) = "hello";
107     get<2>(stv) = 'x';
108 
109     ASSERT(get<0>(stv) == 0.0, NULL);
110     ASSERT(get<1>(stv) == "hello", NULL);
111     ASSERT(get<2>(stv) == 'x', NULL);
112 
113     ASSERT(tuple_size<tuple_type1>::value == 3, NULL);
114     ASSERT(get<0>(tup1) == 42, NULL);
115     ASSERT(get<1>(tup1) == 3.14159, NULL);
116     ASSERT(get<2>(tup1) == 2.0, NULL);
117 
118     get<1>(nti).set_float(1.0);
119     get<1>(nti).set_int(32);
120     ASSERT(get<1>(nti).get_int() == 32, NULL);
121     ASSERT(get<1>(nti).get_float() == 1.0, NULL);
122 
123     // converting constructor
124     tuple<double,double,double> tup2(1,2.0,3.0f);
125     tuple<double,double,double> tup3(9,4.0,7.0f);
126     ASSERT(tup2 != tup3, NULL);
127 
128     ASSERT(tup2 < tup3, NULL);
129 
130     // assignment
131     tup2 = tup3;
132     ASSERT(tup2 == tup3, NULL);
133 
134     tup2 = int_tup;
135     ASSERT(get<0>(tup2) == 4, NULL);
136     ASSERT(get<1>(tup2) == 5, NULL);
137     ASSERT(get<2>(tup2) == 6, NULL);
138 
139     // increment component of tuple
140     get<0>(tup2) += 1;
141     ASSERT(get<0>(tup2) == 5, NULL);
142 
143     std::pair<int,int> two_pair( 4, 8);
144     tuple<int,int> two_pair_tuple;
145     two_pair_tuple = two_pair;
146     ASSERT(get<0>(two_pair_tuple) == 4, NULL);
147     ASSERT(get<1>(two_pair_tuple) == 8, NULL);
148 
149     //relational ops
150     ASSERT(int_tuple_type(1,1,0) == int_tuple_type(1,1,0),NULL);
151     ASSERT(int_tuple_type(1,0,1) <  int_tuple_type(1,1,1),NULL);
152     ASSERT(int_tuple_type(1,0,0) >  int_tuple_type(0,1,0),NULL);
153     ASSERT(int_tuple_type(0,0,0) != int_tuple_type(1,0,1),NULL);
154     ASSERT(int_tuple_type(0,1,0) <= int_tuple_type(0,1,1),NULL);
155     ASSERT(int_tuple_type(0,0,1) <= int_tuple_type(0,0,1),NULL);
156     ASSERT(int_tuple_type(1,1,1) >= int_tuple_type(1,0,0),NULL);
157     ASSERT(int_tuple_type(0,1,1) >= int_tuple_type(0,1,1),NULL);
158 
159 #if !__TBB_TUPLE_COMPARISON_COMPILATION_BROKEN
160     typedef tuple<int,float,double,char> mixed_tuple_left;
161     typedef tuple<float,int,char,double> mixed_tuple_right;
162 
163     ASSERT(mixed_tuple_left(1,1.f,1,char(1)) == mixed_tuple_right(1.f,1,char(1),1),NULL);
164     ASSERT(mixed_tuple_left(1,0.f,1,char(1)) <  mixed_tuple_right(1.f,1,char(1),1),NULL);
165     ASSERT(mixed_tuple_left(1,1.f,1,char(1)) >  mixed_tuple_right(1.f,1,char(0),1),NULL);
166     ASSERT(mixed_tuple_left(1,1.f,1,char(0)) != mixed_tuple_right(1.f,1,char(1),1),NULL);
167     ASSERT(mixed_tuple_left(1,0.f,1,char(1)) <= mixed_tuple_right(1.f,1,char(0),1),NULL);
168     ASSERT(mixed_tuple_left(1,0.f,0,char(1)) <= mixed_tuple_right(1.f,0,char(0),1),NULL);
169     ASSERT(mixed_tuple_left(1,1.f,1,char(0)) >= mixed_tuple_right(1.f,0,char(1),1),NULL);
170     ASSERT(mixed_tuple_left(0,1.f,1,char(0)) >= mixed_tuple_right(0.f,1,char(1),0),NULL);
171 
172     ASSERT(!(mixed_tuple_left(2,1.f,1,char(1)) == mixed_tuple_right(1.f,1,char(1),1)),NULL);
173     ASSERT(!(mixed_tuple_left(1,2.f,1,char(1)) == mixed_tuple_right(1.f,1,char(1),1)),NULL);
174     ASSERT(!(mixed_tuple_left(1,1.f,2,char(1)) == mixed_tuple_right(1.f,1,char(1),1)),NULL);
175     ASSERT(!(mixed_tuple_left(1,1.f,1,char(2)) == mixed_tuple_right(1.f,1,char(1),1)),NULL);
176 
177     ASSERT(!(mixed_tuple_left(1,1.f,1,char(1)) <  mixed_tuple_right(1.f,1,char(1),1)),NULL);
178     ASSERT(!(mixed_tuple_left(1,1.f,1,char(1)) >  mixed_tuple_right(1.f,1,char(1),1)),NULL);
179     ASSERT(!(mixed_tuple_left(1,1.f,1,char(1)) != mixed_tuple_right(1.f,1,char(1),1)),NULL);
180 
181     ASSERT(mixed_tuple_left(1,1.f,1,char(1)) <= mixed_tuple_right(1.f,1,char(1),1),NULL);
182     ASSERT(mixed_tuple_left(1,1.f,1,char(1)) >= mixed_tuple_right(1.f,1,char(1),1),NULL);
183 
184     RunOneComparisonTest<int,float,char,float,char,int>();
185     RunOneComparisonTest<double,float,char,float,double,int>();
186     RunOneComparisonTest<int,float,char,short,char,short>();
187     RunOneComparisonTest<double,float,short,float,char,int>();
188 #endif /* __TBB_TUPLE_COMPARISON_COMPILATION_BROKEN */
189 
190 
191     // the following should result in a syntax error
192     // typedef tuple<float,float> mixed_short_tuple;
193     // ASSERT(mixed_tuple_left(1,1.f,1,1) != mixed_short_tuple(1.f,1.f),NULL);
194 
195 }
196 
TestMain()197 int TestMain() {
198     RunTests();
199     return Harness::Done;
200 }
201