1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2013 - 2021, Göteborg Bit Factory.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // https://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #include <cmake.h>
28 #include <iostream>
29 #include <test.h>
30 #include <Variant.h>
31
32 #define EPSILON 0.0001
33
34 ////////////////////////////////////////////////////////////////////////////////
main(int,char **)35 int main (int, char**)
36 {
37 UnitTest t (54);
38
39 Variant v0 (true);
40 Variant v1 (42);
41 Variant v2 (3.14);
42 Variant v3 ("foo");
43 Variant v4 (1234567890, Variant::type_date);
44 Variant v5 (1200, Variant::type_duration);
45
46 // boolean * boolean -> ERROR
47 try {Variant v00 = v0 * v0; t.fail ("true * true --> error");}
48 catch (...) {t.pass ("true * true --> error");}
49
50 // boolean * integer -> integer
51 Variant v01 = v0 * v1;
52 t.is (v01.type (), Variant::type_integer, "true * 42 --> integer");
53 t.is (v01.get_integer (), 42, "true * 42 --> 42");
54
55 // boolean * real -> real
56 Variant v02 = v0 * v2;
57 t.is (v02.type (), Variant::type_real, "true * 3.14 --> real");
58 t.is (v02.get_real (), 3.14, EPSILON, "true * 3.14 --> 3.14");
59
60 // boolean * string -> string
61 Variant v03 = v0 * v3;
62 t.is (v03.type (), Variant::type_string, "true * foo --> real");
63 t.is (v03.get_string (), "foo", "true * foo --> foo");
64
65 // boolean * date -> ERROR
66 try {Variant v04 = v0 * v4; t.fail ("true * 1234567890 --> error");}
67 catch (...) {t.pass ("true * 1234567890 --> error");}
68
69 // boolean * duration -> duration
70 Variant v05 = v0 * v5;
71 t.is (v05.type (), Variant::type_duration, "true * 1200 --> duration");
72 t.is (v05.get_duration (), 1200, "true * 1200 --> 1200");
73
74 // integer * boolean -> integer
75 Variant v10 = v1 * v0;
76 t.is (v10.type (), Variant::type_integer, "42 * true --> integer");
77 t.is (v10.get_integer (), 42, "42 * true --> 42");
78
79 // integer * integer -> integer
80 Variant v11 = v1 * v1;
81 t.is (v11.type (), Variant::type_integer, "42 * 42 --> integer");
82 t.is (v11.get_integer (), 1764, "42 * 42 --> 1764");
83
84 // integer * real -> real
85 Variant v12 = v1 * v2;
86 t.is (v12.type (), Variant::type_real, "42 * 3.14 --> real");
87 t.is (v12.get_real (), 131.88, EPSILON, "42 * 3.14 --> 131.88");
88
89 // integer * string -> string
90 Variant v13 = v1 * v3;
91 t.is (v13.type (), Variant::type_string, "42 * foo --> string");
92 t.is (v13.get_string ().substr (0, 10), "foofoofoof",
93 "42 * foo --> foofoofoofoo...");
94 // integer * date -> error
95 try {Variant v14 = v1 * v4; t.fail ("42 * 1234567890 --> error");}
96 catch (...) {t.pass ("42 * 1234567890 --> error");}
97
98 // integer * duration -> duration
99 Variant v15 = v1 * v5;
100 t.is (v15.type (), Variant::type_duration, "42 * 1200 --> duration");
101 t.is (v15.get_duration (), 50400, "42 * 1200 --> 50400");
102
103 // real * boolean -> real
104 Variant v20 = v2 * v0;
105 t.is (v20.type (), Variant::type_real, "3.14 * true --> real");
106 t.is (v20.get_real (), 3.14, EPSILON, "3.14 * true --> 3.14");
107
108 // real * integer -> real
109 Variant v21 = v2 * v1;
110 t.is (v21.type (), Variant::type_real, "3.14 * 42 --> real");
111 t.is (v21.get_real (), 131.88, EPSILON, "3.14 * 42 --> 131.88");
112
113 // real * real -> real
114 Variant v22 = v2 * v2;
115 t.is (v22.type (), Variant::type_real, "3.14 * 3.14 --> real");
116 t.is (v22.get_real (), 9.8596, EPSILON, "3.14 * 3.14 --> 9.8596");
117
118 // real * string -> error
119 try {Variant v23 = v2 * v3; t.fail ("3.14 * foo --> error");}
120 catch (...) {t.pass ("3.14 * foo --> error");}
121
122 // real * date -> error
123 try {Variant v24 = v2 * v4; t.fail ("3.14 * 1234567890 --> error");}
124 catch (...) {t.pass ("3.14 * 1234567890 --> error");}
125
126 // real * duration -> duration
127 Variant v25 = v2 * v5;
128 t.is (v25.type (), Variant::type_duration, "3.14 * 1200 --> duration");
129 t.is (v25.get_duration (), 3768, "3.14 * 1200 --> 3768");
130
131 // string * boolean -> string
132 Variant v30 = v3 * v0;
133 t.is (v30.type (), Variant::type_string, "foo * true --> real");
134 t.is (v30.get_string (), "foo", "foo * true --> foo");
135
136 // string * integer -> string
137 Variant v31 = v3 * v1;
138 t.is (v31.type (), Variant::type_string, "foo * 42 --> string");
139 t.is (v31.get_string ().substr (0, 10), "foofoofoof",
140 "foo * 42 --> foofoofoof...");
141
142 // string * real -> string
143 try {Variant v32 = v3 * v2; t.fail ("foo * 3.14 --> error");}
144 catch (...) {t.pass ("foo * 3.14 --> error");}
145
146 // string * string -> string
147 try {Variant v33 = v3 * v3; t.fail ("foo * foo --> error");}
148 catch (...) {t.pass ("foo * foo --> error");}
149
150 // string * date -> string
151 try {Variant v34 = v3 * v4; t.fail ("foo * 1234567890 --> error");}
152 catch (...) {t.pass ("foo * 1234567890 --> error");}
153
154 // string * duration -> string
155 try {Variant v35 = v3 * v5; t.fail ("foo * 1200 --> error");}
156 catch (...) {t.pass ("foo * 1200 --> error");}
157
158 // date * boolean -> ERROR
159 try {Variant v40 = v4 * v0; t.fail ("1234567890 * true --> error");}
160 catch (...) {t.pass ("1234567890 * true --> error");}
161
162 // date * integer -> ERROR
163 try {Variant v41 = v4 * v1; t.fail ("1234567890 * 42 --> error");}
164 catch (...) {t.pass ("1234567890 * 42 --> error");}
165
166 // date * real -> ERROR
167 try {Variant v42 = v4 * v2; t.fail ("1234567890 * 3.14 --> error");}
168 catch (...) {t.pass ("1234567890 * 3.14 --> error");}
169
170 // date * string -> ERROR
171 try {Variant v43 = v4 * v3; t.fail ("1234567890 * foo --> error");}
172 catch (...) {t.pass ("1234567890 * foo --> error");}
173
174 // date * date -> ERROR
175 try {Variant v44 = v4 * v4; t.fail ("1234567890 * 1234567890 --> error");}
176 catch (...) {t.pass ("1234567890 * 1234567890 --> error");}
177
178 // date * duration -> ERROR
179 try {Variant v45 = v4 * v5; t.fail ("1234567890 * 1200 --> error");}
180 catch (...) {t.pass ("1234567890 * 1200 --> error");}
181
182 // duration * boolean -> duration
183 Variant v50 = v5 * v0;
184 t.is (v50.type (), Variant::type_duration, "1200 * true --> duration");
185 t.is (v50.get_duration (), 1200, "1200 * true --> 1200");
186
187 // duration * integer -> duration
188 Variant v51 = v5 * v1;
189 t.is (v51.type (), Variant::type_duration, "1200 * 42 --> duration");
190 t.is (v51.get_duration (), 50400, "1200 * 42 --> 50400");
191
192 // duration * real -> duration
193 Variant v52 = v5 * v2;
194 t.is (v52.type (), Variant::type_duration, "1200 * 3.14 --> duration");
195 t.is (v52.get_duration (), 3768, "1200 * 3.14 --> 3768");
196
197 // duration * string -> string
198 try {Variant v53 = v5 * v3; t.fail ("1200 * foo --> error");}
199 catch (...) {t.pass ("1200 * foo --> error");}
200
201 // duration * date -> date
202 try {Variant v54 = v5 * v4; t.fail ("1200 * 1234567890 --> error");}
203 catch (...) {t.pass ("1200 * 1234567890 --> error");}
204
205 // duration * duration -> duration
206 try {Variant v55 = v5 * v5; t.fail ("1200 * 1200 --> error");}
207 catch (...) {t.pass ("1200 * 1200 --> error");}
208
209 return 0;
210 }
211
212 ////////////////////////////////////////////////////////////////////////////////
213