[CMake] Properly test if compiler supports compiler flags
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WTF / CheckedArithmeticOperations.cpp
1 /*
2  * Copyright (C) 2011, 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include <wtf/CheckedArithmetic.h>
28
29 namespace TestWebKitAPI {
30
31 class OverflowCrashLogger {
32 protected:
33     void overflowed()
34     {
35         m_overflowCount++;
36     }
37     
38     void clearOverflow()
39     {
40         m_overflowCount = 0;
41     }
42     
43     static void crash()
44     {
45         s_didCrash = true;
46     }
47     
48 public:
49     void reset()
50     {
51         m_overflowCount = 0;
52         s_didCrash = false;
53     }
54     
55     bool hasOverflowed() const { return m_overflowCount > 0; }
56     int overflowCount() const { return m_overflowCount; }
57
58     bool didCrash() const { return s_didCrash; }
59     
60 private:
61     int m_overflowCount { 0 };
62     static bool s_didCrash;
63 };
64
65 bool OverflowCrashLogger::s_didCrash = false;
66
67 template <typename type>
68 static void resetOverflow(Checked<type, OverflowCrashLogger>& value)
69 {
70     value.reset();
71     value = 100;
72     value *= std::numeric_limits<type>::max();
73 }
74
75 #define CheckedArithmeticTest(type, Coercer, MixedSignednessTester) \
76     TEST(WTF, Checked_##type) \
77     { \
78         typedef Coercer<type> CoercerType; \
79         typedef MixedSignednessTester<type, CoercerType> MixedSignednessTesterType; \
80         CheckedArithmeticTester<type, CoercerType, MixedSignednessTesterType>::run(); \
81     }
82     
83 #define coerceLiteral(x) Coercer::coerce(x)
84     
85 template <typename type, typename Coercer, typename MixedSignednessTester>
86 class CheckedArithmeticTester {
87 public:
88     static void run()
89     {
90         Checked<type, RecordOverflow> value;
91         EXPECT_EQ(coerceLiteral(0), value.unsafeGet());
92         EXPECT_EQ(std::numeric_limits<type>::max(), (value + std::numeric_limits<type>::max()).unsafeGet());
93         EXPECT_EQ(std::numeric_limits<type>::max(), (std::numeric_limits<type>::max() + value).unsafeGet());
94         EXPECT_EQ(std::numeric_limits<type>::min(), (value + std::numeric_limits<type>::min()).unsafeGet());
95         EXPECT_EQ(std::numeric_limits<type>::min(), (std::numeric_limits<type>::min() + value).unsafeGet());
96
97         EXPECT_EQ(coerceLiteral(0), (value * coerceLiteral(0)).unsafeGet());
98         EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) * value).unsafeGet());
99         EXPECT_EQ(coerceLiteral(0), (value * value).unsafeGet());
100         EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(0)).unsafeGet());
101         EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) - value).unsafeGet());
102         EXPECT_EQ(coerceLiteral(0), (value - value).unsafeGet());
103         EXPECT_EQ(coerceLiteral(0), (value++).unsafeGet());
104         EXPECT_EQ(coerceLiteral(1), (value--).unsafeGet());
105         EXPECT_EQ(coerceLiteral(1), (++value).unsafeGet());
106         EXPECT_EQ(coerceLiteral(0), (--value).unsafeGet());
107         EXPECT_EQ(coerceLiteral(10), (value += coerceLiteral(10)).unsafeGet());
108         EXPECT_EQ(coerceLiteral(10), value.unsafeGet());
109         EXPECT_EQ(coerceLiteral(100), (value *= coerceLiteral(10)).unsafeGet());
110         EXPECT_EQ(coerceLiteral(100), value.unsafeGet());
111         EXPECT_EQ(coerceLiteral(0), (value -= coerceLiteral(100)).unsafeGet());
112         EXPECT_EQ(coerceLiteral(0), value.unsafeGet());
113         value = 10;
114         EXPECT_EQ(coerceLiteral(10), value.unsafeGet());
115         EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(10)).unsafeGet());
116         EXPECT_EQ(coerceLiteral(10), value.unsafeGet());
117
118         value = std::numeric_limits<type>::min();
119         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - coerceLiteral(1))).hasOverflowed());
120         EXPECT_EQ(true, !((value--).hasOverflowed()));
121         EXPECT_EQ(true, value.hasOverflowed());
122         value = std::numeric_limits<type>::max();
123         EXPECT_EQ(true, !value.hasOverflowed());
124         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + coerceLiteral(1))).hasOverflowed());
125         EXPECT_EQ(true, !(value++).hasOverflowed());
126         EXPECT_EQ(true, value.hasOverflowed());
127         value = std::numeric_limits<type>::max();
128         EXPECT_EQ(true, (value += coerceLiteral(1)).hasOverflowed());
129         EXPECT_EQ(true, value.hasOverflowed());
130
131         value = 10;
132         type _value = 0;
133         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(0)).safeGet(_value));
134         _value = 0;
135         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(0) * value).safeGet(_value));
136         _value = 0;
137         EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
138         _value = 0;
139         EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value));
140         value = 0;
141         _value = 0;
142         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
143         _value = 0;
144         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value));
145         value = 1;
146         _value = 0;
147         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
148         _value = 0;
149         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value));
150         _value = 0;
151         value = 0;
152         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
153         _value = 0;
154         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)0).safeGet(_value));
155         _value = 0;
156         value = 1;
157         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
158         _value = 0;
159         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)1).safeGet(_value));
160         _value = 0;
161         value = 2;
162         EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
163         _value = 0;
164         EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)2).safeGet(_value));
165         value = 10;
166         EXPECT_EQ(true, (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).hasOverflowed());
167
168
169         Checked<type, OverflowCrashLogger> nvalue; // to hold a not overflowed value.
170         Checked<type, OverflowCrashLogger> ovalue; // to hold an overflowed value.
171
172 #if COMPILER(GCC)
173 #pragma GCC diagnostic push
174 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
175 #endif
176         bool unused;
177 #if COMPILER(GCC)
178 #pragma GCC diagnostic pop
179 #endif
180
181         _value = 75;
182         type _largeValue = 100;
183         type _smallValue = 50;
184
185         value = _smallValue;
186         nvalue = _value;
187         ovalue = _value;
188
189         // Make sure the OverflowCrashLogger is working as expected.
190         EXPECT_EQ(false, (ovalue.hasOverflowed()));
191         EXPECT_EQ(true, (resetOverflow(ovalue), ovalue.hasOverflowed()));
192         EXPECT_EQ(false, (resetOverflow(ovalue), ovalue.didCrash()));
193         EXPECT_EQ(true, (unused = (ovalue == ovalue), ovalue.didCrash()));
194         EXPECT_EQ(false, (resetOverflow(ovalue), ovalue.didCrash()));
195
196         EXPECT_EQ(false, nvalue.hasOverflowed());
197         EXPECT_EQ(false, nvalue.didCrash());
198
199         // Test operator== that should not overflow nor crash.
200         EXPECT_EQ(true, (nvalue == nvalue));
201         EXPECT_EQ(true, (nvalue == Checked<type, OverflowCrashLogger>(_value)));
202         EXPECT_EQ(false, (nvalue == value));
203         EXPECT_EQ(true, (nvalue == _value));
204         EXPECT_EQ(false, (nvalue == Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
205         EXPECT_EQ(false, (nvalue == std::numeric_limits<type>::max()));
206
207         EXPECT_EQ(false, nvalue.hasOverflowed());
208         EXPECT_EQ(false, nvalue.didCrash());
209
210         // Test operator!= that should not overflow nor crash.
211         EXPECT_EQ(false, (nvalue != nvalue));
212         EXPECT_EQ(false, (nvalue != Checked<type, OverflowCrashLogger>(_value)));
213         EXPECT_EQ(true, (nvalue != value));
214         EXPECT_EQ(false, (nvalue != _value));
215         EXPECT_EQ(true, (nvalue != Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
216         EXPECT_EQ(true, (nvalue != std::numeric_limits<type>::max()));
217
218         EXPECT_EQ(false, nvalue.hasOverflowed());
219         EXPECT_EQ(false, nvalue.didCrash());
220
221         // Test operator< that should not overflow nor crash.
222         EXPECT_EQ(false, (nvalue < nvalue));
223         EXPECT_EQ(false, (nvalue < value));
224         EXPECT_EQ(true, (nvalue < Checked<type, OverflowCrashLogger>(_largeValue)));
225         EXPECT_EQ(false, (nvalue < Checked<type, OverflowCrashLogger>(_value)));
226         EXPECT_EQ(false, (nvalue < Checked<type, OverflowCrashLogger>(_smallValue)));
227         EXPECT_EQ(true, (nvalue < _largeValue));
228         EXPECT_EQ(false, (nvalue < _value));
229         EXPECT_EQ(false, (nvalue < _smallValue));
230         EXPECT_EQ(true, (nvalue < Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
231         EXPECT_EQ(true, (nvalue < std::numeric_limits<type>::max()));
232
233         EXPECT_EQ(false, nvalue.hasOverflowed());
234         EXPECT_EQ(false, nvalue.didCrash());
235
236         // Test operator<= that should not overflow nor crash.
237         EXPECT_EQ(true, (nvalue <= nvalue));
238         EXPECT_EQ(false, (nvalue <= value));
239         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(_largeValue)));
240         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(_value)));
241         EXPECT_EQ(false, (nvalue <= Checked<type, OverflowCrashLogger>(_smallValue)));
242         EXPECT_EQ(true, (nvalue <= _largeValue));
243         EXPECT_EQ(true, (nvalue <= _value));
244         EXPECT_EQ(false, (nvalue <= _smallValue));
245         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
246         EXPECT_EQ(true, (nvalue <= std::numeric_limits<type>::max()));
247
248         EXPECT_EQ(false, nvalue.hasOverflowed());
249         EXPECT_EQ(false, nvalue.didCrash());
250
251         // Test operator> that should not overflow nor crash.
252         EXPECT_EQ(false, (nvalue > nvalue));
253         EXPECT_EQ(true, (nvalue > value));
254         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(_largeValue)));
255         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(_value)));
256         EXPECT_EQ(true, (nvalue > Checked<type, OverflowCrashLogger>(_smallValue)));
257         EXPECT_EQ(false, (nvalue > _largeValue));
258         EXPECT_EQ(false, (nvalue > _value));
259         EXPECT_EQ(true, (nvalue > _smallValue));
260         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
261         EXPECT_EQ(false, (nvalue > std::numeric_limits<type>::max()));
262
263         EXPECT_EQ(false, nvalue.hasOverflowed());
264         EXPECT_EQ(false, nvalue.didCrash());
265
266         // Test operator>= that should not overflow nor crash.
267         EXPECT_EQ(true, (nvalue >= nvalue));
268         EXPECT_EQ(true, (nvalue >= value));
269         EXPECT_EQ(false, (nvalue >= Checked<type, OverflowCrashLogger>(_largeValue)));
270         EXPECT_EQ(true, (nvalue >= Checked<type, OverflowCrashLogger>(_value)));
271         EXPECT_EQ(true, (nvalue >= Checked<type, OverflowCrashLogger>(_smallValue)));
272         EXPECT_EQ(false, (nvalue >= _largeValue));
273         EXPECT_EQ(true, (nvalue >= _value));
274         EXPECT_EQ(true, (nvalue >= _smallValue));
275         EXPECT_EQ(false, (nvalue >= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
276         EXPECT_EQ(false, (nvalue >= std::numeric_limits<type>::max()));
277
278         EXPECT_EQ(false, nvalue.hasOverflowed());
279         EXPECT_EQ(false, nvalue.didCrash());
280
281         // Test operator== with an overflowed value.
282         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == ovalue), ovalue.didCrash()));
283         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
284         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == value), ovalue.didCrash()));
285         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == _value), ovalue.didCrash()));
286         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == _value * std::numeric_limits<type>::max()), ovalue.didCrash()));
287         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
288         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == std::numeric_limits<type>::max()), ovalue.didCrash()));
289         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == nvalue), ovalue.didCrash()));
290         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue == ovalue), ovalue.didCrash()));
291
292         EXPECT_EQ(false, nvalue.hasOverflowed());
293
294         // Test operator!= with an overflowed value.
295         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != ovalue), ovalue.didCrash()));
296         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
297         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != value), ovalue.didCrash()));
298         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != _value), ovalue.didCrash()));
299         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != _value * std::numeric_limits<type>::max()), ovalue.didCrash()));
300         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
301         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != std::numeric_limits<type>::max()), ovalue.didCrash()));
302         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != nvalue), ovalue.didCrash()));
303         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue != ovalue), ovalue.didCrash()));
304
305         EXPECT_EQ(false, nvalue.hasOverflowed());
306
307         // Test operator< with an overflowed value.
308         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < ovalue), ovalue.didCrash()));
309         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < value), ovalue.didCrash()));
310         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
311         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
312         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
313         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _largeValue), ovalue.didCrash()));
314         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _value), ovalue.didCrash()));
315         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _smallValue), ovalue.didCrash()));
316         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
317         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < std::numeric_limits<type>::max()), ovalue.didCrash()));
318         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < nvalue), ovalue.didCrash()));
319         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue < ovalue), ovalue.didCrash()));
320
321         EXPECT_EQ(false, nvalue.hasOverflowed());
322
323         // Test operator<= with an overflowed value.
324         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= ovalue), ovalue.didCrash()));
325         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= value), ovalue.didCrash()));
326         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
327         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
328         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
329         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _largeValue), ovalue.didCrash()));
330         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _value), ovalue.didCrash()));
331         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _smallValue), ovalue.didCrash()));
332         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
333         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= std::numeric_limits<type>::max()), ovalue.didCrash()));
334         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= nvalue), ovalue.didCrash()));
335         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue <= ovalue), ovalue.didCrash()));
336
337         EXPECT_EQ(false, nvalue.hasOverflowed());
338
339         // Test operator> with an overflowed value.
340         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > ovalue), ovalue.didCrash()));
341         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > value), ovalue.didCrash()));
342         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
343         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
344         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
345         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _largeValue), ovalue.didCrash()));
346         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _value), ovalue.didCrash()));
347         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _smallValue), ovalue.didCrash()));
348         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
349         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > std::numeric_limits<type>::max()), ovalue.didCrash()));
350         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > nvalue), ovalue.didCrash()));
351         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue > ovalue), ovalue.didCrash()));
352
353         EXPECT_EQ(false, nvalue.hasOverflowed());
354
355         // Test operator>= with an overflowed value.
356         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= ovalue), ovalue.didCrash()));
357         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= value), ovalue.didCrash()));
358         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
359         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
360         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
361         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _largeValue), ovalue.didCrash()));
362         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _value), ovalue.didCrash()));
363         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _smallValue), ovalue.didCrash()));
364         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
365         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= std::numeric_limits<type>::max()), ovalue.didCrash()));
366         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= nvalue), ovalue.didCrash()));
367         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue >= ovalue), ovalue.didCrash()));
368
369         EXPECT_EQ(false, nvalue.hasOverflowed());
370
371         MixedSignednessTester::run();
372     }
373 };
374
375 template <typename type, typename Coercer>
376 class AllowMixedSignednessTest {
377 public:
378     static void run()
379     {
380         Checked<type, RecordOverflow> value;
381         value = 10;
382
383         EXPECT_EQ(coerceLiteral(0), (value + -10).unsafeGet());
384         EXPECT_EQ(0U, (value - 10U).unsafeGet());
385         EXPECT_EQ(coerceLiteral(0), (-10 + value).unsafeGet());
386         EXPECT_EQ(0U, (10U - value).unsafeGet());
387         value = std::numeric_limits<type>::min();
388         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - 1)).hasOverflowed());
389         EXPECT_EQ(true, !(value--).hasOverflowed());
390         EXPECT_EQ(true, value.hasOverflowed());
391         value = std::numeric_limits<type>::max();
392         EXPECT_EQ(true, !value.hasOverflowed());
393         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1)).hasOverflowed());
394         EXPECT_EQ(true, !(value++).hasOverflowed());
395         EXPECT_EQ(true, value.hasOverflowed());
396         value = std::numeric_limits<type>::max();
397         EXPECT_EQ(true, (value += 1).hasOverflowed());
398         EXPECT_EQ(true, value.hasOverflowed());
399         value = std::numeric_limits<type>::min();
400         EXPECT_EQ(true, (value - 1U).hasOverflowed());
401         EXPECT_EQ(true, !(value--).hasOverflowed());
402         EXPECT_EQ(true, value.hasOverflowed());
403         value = std::numeric_limits<type>::max();
404         EXPECT_EQ(true, !value.hasOverflowed());
405         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1U)).hasOverflowed());
406         EXPECT_EQ(true, !(value++).hasOverflowed());
407         EXPECT_EQ(true, value.hasOverflowed());
408         value = std::numeric_limits<type>::max();
409         EXPECT_EQ(true, (value += 1U).hasOverflowed());
410         EXPECT_EQ(true, value.hasOverflowed());
411     }
412 };
413
414 template <typename type, typename Coercer>
415 class IgnoreMixedSignednessTest {
416 public:
417     static void run() { }
418 };
419
420 template <typename type> class CoerceLiteralToUnsigned {
421 public:
422     static unsigned coerce(type x) { return static_cast<unsigned>(x); }
423 };
424     
425 template <typename type> class CoerceLiteralNop {
426 public:
427     static type coerce(type x) { return x; }
428 };
429
430 CheckedArithmeticTest(int8_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
431 CheckedArithmeticTest(int16_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
432 CheckedArithmeticTest(int32_t, CoerceLiteralNop, AllowMixedSignednessTest)
433 CheckedArithmeticTest(uint32_t, CoerceLiteralToUnsigned, AllowMixedSignednessTest)
434 CheckedArithmeticTest(int64_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
435 CheckedArithmeticTest(uint64_t, CoerceLiteralToUnsigned, IgnoreMixedSignednessTest)
436
437 TEST(CheckedArithmeticTest, IsInBounds)
438 {
439     // bigger precision, signed, signed
440     EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<int16_t>::max()));
441     EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<int16_t>::min()));
442
443     // bigger precision, unsigned, signed
444     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int32_t>::max()));
445     EXPECT_FALSE(WTF::isInBounds<uint32_t>(std::numeric_limits<int16_t>::min()));
446
447     EXPECT_FALSE(WTF::isInBounds<uint32_t>((int32_t)-1));
448     EXPECT_FALSE(WTF::isInBounds<uint16_t>((int32_t)-1));
449     EXPECT_FALSE(WTF::isInBounds<unsigned long>((int)-1));
450
451     EXPECT_TRUE(WTF::isInBounds<uint32_t>((int32_t)1));
452     EXPECT_TRUE(WTF::isInBounds<uint32_t>((int16_t)1));
453     EXPECT_TRUE(WTF::isInBounds<unsigned>((int)1));
454
455     EXPECT_TRUE(WTF::isInBounds<uint32_t>((int32_t)0));
456     EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)0));
457     EXPECT_TRUE(WTF::isInBounds<uint32_t>((int16_t)0));
458     EXPECT_TRUE(WTF::isInBounds<unsigned>((int)0));
459
460     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int32_t>::max()));
461     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int16_t>::max()));
462     EXPECT_TRUE(WTF::isInBounds<unsigned>(std::numeric_limits<int>::max()));
463
464     // bigger precision, signed, unsigned
465     EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<uint16_t>::max()));
466     EXPECT_FALSE(WTF::isInBounds<int32_t>(std::numeric_limits<uint32_t>::max()));
467     EXPECT_TRUE(WTF::isInBounds<int32_t>((uint32_t)0));
468
469     // bigger precision, unsigned, unsigned
470     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<uint16_t>::max()));
471     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<uint16_t>::min()));
472
473     // lower precision, signed signed
474     EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<int32_t>::max()));
475     EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<int32_t>::min()));
476     EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)-1));
477     EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)0));
478     EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)1));
479     // lower precision, unsigned, signed
480     EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<int32_t>::max()));
481     EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<int32_t>::min()));
482     EXPECT_FALSE(WTF::isInBounds<uint16_t>((int32_t)-1));
483     EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)0));
484     EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)1));
485     // lower precision, signed, unsigned
486     EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<uint32_t>::max()));
487     EXPECT_TRUE(WTF::isInBounds<int16_t>((uint32_t)0));
488     EXPECT_TRUE(WTF::isInBounds<int16_t>((uint32_t)1));
489     // lower precision, unsigned, unsigned
490     EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<uint32_t>::max()));
491     EXPECT_TRUE(WTF::isInBounds<uint16_t>((uint32_t)0));
492     EXPECT_TRUE(WTF::isInBounds<uint16_t>((uint32_t)1));
493 }
494
495 } // namespace TestWebKitAPI