CheckedArithmetic's operator bool() and operator==() is broken.
[WebKit.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 #include <wtf/DataLog.h> // mlam
29
30 namespace TestWebKitAPI {
31
32 class OverflowCrashLogger {
33 protected:
34     void overflowed()
35     {
36         m_overflowCount++;
37     }
38     
39     void clearOverflow()
40     {
41         m_overflowCount = 0;
42     }
43     
44     static void crash()
45     {
46         s_didCrash = true;
47     }
48     
49 public:
50     void reset()
51     {
52         m_overflowCount = 0;
53         s_didCrash = false;
54     }
55     
56     bool hasOverflowed() const { return m_overflowCount > 0; }
57     int overflowCount() const { return m_overflowCount; }
58
59     bool didCrash() const { return s_didCrash; }
60     
61 private:
62     int m_overflowCount { 0 };
63     static bool s_didCrash;
64 };
65
66 bool OverflowCrashLogger::s_didCrash = false;
67
68 template <typename type>
69 static void resetOverflow(Checked<type, OverflowCrashLogger>& value)
70 {
71     value.reset();
72     value = 100;
73     value *= std::numeric_limits<type>::max();
74 }
75
76 #define CheckedArithmeticTest(type, Coercer, MixedSignednessTester) \
77     TEST(WTF, Checked_##type) \
78     { \
79         typedef Coercer<type> CoercerType; \
80         typedef MixedSignednessTester<type, CoercerType> MixedSignednessTesterType; \
81         CheckedArithmeticTester<type, CoercerType, MixedSignednessTesterType>::run(); \
82     }
83     
84 #define coerceLiteral(x) Coercer::coerce(x)
85     
86 template <typename type, typename Coercer, typename MixedSignednessTester>
87 class CheckedArithmeticTester {
88 public:
89     static void run()
90     {
91         Checked<type, RecordOverflow> value;
92         EXPECT_EQ(coerceLiteral(0), value.unsafeGet());
93         EXPECT_EQ(std::numeric_limits<type>::max(), (value + std::numeric_limits<type>::max()).unsafeGet());
94         EXPECT_EQ(std::numeric_limits<type>::max(), (std::numeric_limits<type>::max() + value).unsafeGet());
95         EXPECT_EQ(std::numeric_limits<type>::min(), (value + std::numeric_limits<type>::min()).unsafeGet());
96         EXPECT_EQ(std::numeric_limits<type>::min(), (std::numeric_limits<type>::min() + value).unsafeGet());
97
98         EXPECT_EQ(coerceLiteral(0), (value * coerceLiteral(0)).unsafeGet());
99         EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) * value).unsafeGet());
100         EXPECT_EQ(coerceLiteral(0), (value * value).unsafeGet());
101         EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(0)).unsafeGet());
102         EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) - value).unsafeGet());
103         EXPECT_EQ(coerceLiteral(0), (value - value).unsafeGet());
104         EXPECT_EQ(coerceLiteral(0), (value++).unsafeGet());
105         EXPECT_EQ(coerceLiteral(1), (value--).unsafeGet());
106         EXPECT_EQ(coerceLiteral(1), (++value).unsafeGet());
107         EXPECT_EQ(coerceLiteral(0), (--value).unsafeGet());
108         EXPECT_EQ(coerceLiteral(10), (value += coerceLiteral(10)).unsafeGet());
109         EXPECT_EQ(coerceLiteral(10), value.unsafeGet());
110         EXPECT_EQ(coerceLiteral(100), (value *= coerceLiteral(10)).unsafeGet());
111         EXPECT_EQ(coerceLiteral(100), value.unsafeGet());
112         EXPECT_EQ(coerceLiteral(0), (value -= coerceLiteral(100)).unsafeGet());
113         EXPECT_EQ(coerceLiteral(0), value.unsafeGet());
114         value = 10;
115         EXPECT_EQ(coerceLiteral(10), value.unsafeGet());
116         EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(10)).unsafeGet());
117         EXPECT_EQ(coerceLiteral(10), value.unsafeGet());
118
119         value = std::numeric_limits<type>::min();
120         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - coerceLiteral(1))).hasOverflowed());
121         EXPECT_EQ(true, !((value--).hasOverflowed()));
122         EXPECT_EQ(true, value.hasOverflowed());
123         value = std::numeric_limits<type>::max();
124         EXPECT_EQ(true, !value.hasOverflowed());
125         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + coerceLiteral(1))).hasOverflowed());
126         EXPECT_EQ(true, !(value++).hasOverflowed());
127         EXPECT_EQ(true, value.hasOverflowed());
128         value = std::numeric_limits<type>::max();
129         EXPECT_EQ(true, (value += coerceLiteral(1)).hasOverflowed());
130         EXPECT_EQ(true, value.hasOverflowed());
131
132         value = 10;
133         type _value = 0;
134         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(0)).safeGet(_value));
135         _value = 0;
136         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(0) * value).safeGet(_value));
137         _value = 0;
138         EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
139         _value = 0;
140         EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value));
141         value = 0;
142         _value = 0;
143         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
144         _value = 0;
145         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value));
146         value = 1;
147         _value = 0;
148         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
149         _value = 0;
150         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * value).safeGet(_value));
151         _value = 0;
152         value = 0;
153         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
154         _value = 0;
155         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)0).safeGet(_value));
156         _value = 0;
157         value = 1;
158         EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
159         _value = 0;
160         EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)1).safeGet(_value));
161         _value = 0;
162         value = 2;
163         EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value));
164         _value = 0;
165         EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * (type)2).safeGet(_value));
166         value = 10;
167         EXPECT_EQ(true, (value * Checked<type, RecordOverflow>(std::numeric_limits<type>::max())).hasOverflowed());
168
169
170         Checked<type, OverflowCrashLogger> nvalue; // to hold a not overflowed value.
171         Checked<type, OverflowCrashLogger> ovalue; // to hold an overflowed value.
172         bool unused;
173
174         _value = 75;
175         type _largeValue = 100;
176         type _smallValue = 50;
177
178         value = _smallValue;
179         nvalue = _value;
180         ovalue = _value;
181
182         // Make sure the OverflowCrashLogger is working as expected.
183         EXPECT_EQ(false, (ovalue.hasOverflowed()));
184         EXPECT_EQ(true, (resetOverflow(ovalue), ovalue.hasOverflowed()));
185         EXPECT_EQ(false, (resetOverflow(ovalue), ovalue.didCrash()));
186         EXPECT_EQ(true, (unused = (ovalue == ovalue), ovalue.didCrash()));
187         EXPECT_EQ(false, (resetOverflow(ovalue), ovalue.didCrash()));
188
189         EXPECT_EQ(false, nvalue.hasOverflowed());
190         EXPECT_EQ(false, nvalue.didCrash());
191
192         // Test operator== that should not overflow nor crash.
193         EXPECT_EQ(true, (nvalue == nvalue));
194         EXPECT_EQ(true, (nvalue == Checked<type, OverflowCrashLogger>(_value)));
195         EXPECT_EQ(false, (nvalue == value));
196         EXPECT_EQ(true, (nvalue == _value));
197         EXPECT_EQ(false, (nvalue == Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
198         EXPECT_EQ(false, (nvalue == std::numeric_limits<type>::max()));
199
200         EXPECT_EQ(false, nvalue.hasOverflowed());
201         EXPECT_EQ(false, nvalue.didCrash());
202
203         // Test operator!= that should not overflow nor crash.
204         EXPECT_EQ(false, (nvalue != nvalue));
205         EXPECT_EQ(false, (nvalue != Checked<type, OverflowCrashLogger>(_value)));
206         EXPECT_EQ(true, (nvalue != value));
207         EXPECT_EQ(false, (nvalue != _value));
208         EXPECT_EQ(true, (nvalue != Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
209         EXPECT_EQ(true, (nvalue != std::numeric_limits<type>::max()));
210
211         EXPECT_EQ(false, nvalue.hasOverflowed());
212         EXPECT_EQ(false, nvalue.didCrash());
213
214         // Test operator< that should not overflow nor crash.
215         EXPECT_EQ(false, (nvalue < nvalue));
216         EXPECT_EQ(false, (nvalue < value));
217         EXPECT_EQ(true, (nvalue < Checked<type, OverflowCrashLogger>(_largeValue)));
218         EXPECT_EQ(false, (nvalue < Checked<type, OverflowCrashLogger>(_value)));
219         EXPECT_EQ(false, (nvalue < Checked<type, OverflowCrashLogger>(_smallValue)));
220         EXPECT_EQ(true, (nvalue < _largeValue));
221         EXPECT_EQ(false, (nvalue < _value));
222         EXPECT_EQ(false, (nvalue < _smallValue));
223         EXPECT_EQ(true, (nvalue < Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
224         EXPECT_EQ(true, (nvalue < std::numeric_limits<type>::max()));
225
226         EXPECT_EQ(false, nvalue.hasOverflowed());
227         EXPECT_EQ(false, nvalue.didCrash());
228
229         // Test operator<= that should not overflow nor crash.
230         EXPECT_EQ(true, (nvalue <= nvalue));
231         EXPECT_EQ(false, (nvalue <= value));
232         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(_largeValue)));
233         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(_value)));
234         EXPECT_EQ(false, (nvalue <= Checked<type, OverflowCrashLogger>(_smallValue)));
235         EXPECT_EQ(true, (nvalue <= _largeValue));
236         EXPECT_EQ(true, (nvalue <= _value));
237         EXPECT_EQ(false, (nvalue <= _smallValue));
238         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
239         EXPECT_EQ(true, (nvalue <= std::numeric_limits<type>::max()));
240
241         EXPECT_EQ(false, nvalue.hasOverflowed());
242         EXPECT_EQ(false, nvalue.didCrash());
243
244         // Test operator> that should not overflow nor crash.
245         EXPECT_EQ(false, (nvalue > nvalue));
246         EXPECT_EQ(true, (nvalue > value));
247         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(_largeValue)));
248         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(_value)));
249         EXPECT_EQ(true, (nvalue > Checked<type, OverflowCrashLogger>(_smallValue)));
250         EXPECT_EQ(false, (nvalue > _largeValue));
251         EXPECT_EQ(false, (nvalue > _value));
252         EXPECT_EQ(true, (nvalue > _smallValue));
253         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
254         EXPECT_EQ(false, (nvalue > std::numeric_limits<type>::max()));
255
256         EXPECT_EQ(false, nvalue.hasOverflowed());
257         EXPECT_EQ(false, nvalue.didCrash());
258
259         // Test operator>= that should not overflow nor crash.
260         EXPECT_EQ(true, (nvalue >= nvalue));
261         EXPECT_EQ(true, (nvalue >= value));
262         EXPECT_EQ(false, (nvalue >= Checked<type, OverflowCrashLogger>(_largeValue)));
263         EXPECT_EQ(true, (nvalue >= Checked<type, OverflowCrashLogger>(_value)));
264         EXPECT_EQ(true, (nvalue >= Checked<type, OverflowCrashLogger>(_smallValue)));
265         EXPECT_EQ(false, (nvalue >= _largeValue));
266         EXPECT_EQ(true, (nvalue >= _value));
267         EXPECT_EQ(true, (nvalue >= _smallValue));
268         EXPECT_EQ(false, (nvalue >= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
269         EXPECT_EQ(false, (nvalue >= std::numeric_limits<type>::max()));
270
271         EXPECT_EQ(false, nvalue.hasOverflowed());
272         EXPECT_EQ(false, nvalue.didCrash());
273
274         // Test operator== with an overflowed value.
275         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == ovalue), ovalue.didCrash()));
276         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
277         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == value), ovalue.didCrash()));
278         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == _value), ovalue.didCrash()));
279         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == _value * std::numeric_limits<type>::max()), ovalue.didCrash()));
280         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
281         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == std::numeric_limits<type>::max()), ovalue.didCrash()));
282         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == nvalue), ovalue.didCrash()));
283         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue == ovalue), ovalue.didCrash()));
284
285         EXPECT_EQ(false, nvalue.hasOverflowed());
286
287         // Test operator!= with an overflowed value.
288         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != ovalue), ovalue.didCrash()));
289         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
290         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != value), ovalue.didCrash()));
291         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != _value), ovalue.didCrash()));
292         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != _value * std::numeric_limits<type>::max()), ovalue.didCrash()));
293         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
294         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != std::numeric_limits<type>::max()), ovalue.didCrash()));
295         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != nvalue), ovalue.didCrash()));
296         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue != ovalue), ovalue.didCrash()));
297
298         EXPECT_EQ(false, nvalue.hasOverflowed());
299
300         // Test operator< with an overflowed value.
301         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < ovalue), ovalue.didCrash()));
302         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < value), ovalue.didCrash()));
303         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
304         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
305         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
306         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _largeValue), ovalue.didCrash()));
307         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _value), ovalue.didCrash()));
308         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _smallValue), ovalue.didCrash()));
309         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
310         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < std::numeric_limits<type>::max()), ovalue.didCrash()));
311         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < nvalue), ovalue.didCrash()));
312         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue < ovalue), ovalue.didCrash()));
313
314         EXPECT_EQ(false, nvalue.hasOverflowed());
315
316         // Test operator<= with an overflowed value.
317         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= ovalue), ovalue.didCrash()));
318         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= value), ovalue.didCrash()));
319         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
320         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
321         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
322         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _largeValue), ovalue.didCrash()));
323         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _value), ovalue.didCrash()));
324         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _smallValue), ovalue.didCrash()));
325         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
326         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= std::numeric_limits<type>::max()), ovalue.didCrash()));
327         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= nvalue), ovalue.didCrash()));
328         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue <= ovalue), ovalue.didCrash()));
329
330         EXPECT_EQ(false, nvalue.hasOverflowed());
331
332         // Test operator> with an overflowed value.
333         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > ovalue), ovalue.didCrash()));
334         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > value), ovalue.didCrash()));
335         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
336         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
337         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
338         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _largeValue), ovalue.didCrash()));
339         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _value), ovalue.didCrash()));
340         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _smallValue), ovalue.didCrash()));
341         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
342         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > std::numeric_limits<type>::max()), ovalue.didCrash()));
343         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > nvalue), ovalue.didCrash()));
344         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue > ovalue), ovalue.didCrash()));
345
346         EXPECT_EQ(false, nvalue.hasOverflowed());
347
348         // Test operator>= with an overflowed value.
349         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= ovalue), ovalue.didCrash()));
350         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= value), ovalue.didCrash()));
351         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
352         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
353         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
354         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _largeValue), ovalue.didCrash()));
355         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _value), ovalue.didCrash()));
356         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _smallValue), ovalue.didCrash()));
357         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
358         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= std::numeric_limits<type>::max()), ovalue.didCrash()));
359         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= nvalue), ovalue.didCrash()));
360         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue >= ovalue), ovalue.didCrash()));
361
362         EXPECT_EQ(false, nvalue.hasOverflowed());
363
364         MixedSignednessTester::run();
365     }
366 };
367
368 template <typename type, typename Coercer>
369 class AllowMixedSignednessTest {
370 public:
371     static void run()
372     {
373         Checked<type, RecordOverflow> value;
374         value = 10;
375
376         EXPECT_EQ(coerceLiteral(0), (value + -10).unsafeGet());
377         EXPECT_EQ(0U, (value - 10U).unsafeGet());
378         EXPECT_EQ(coerceLiteral(0), (-10 + value).unsafeGet());
379         EXPECT_EQ(0U, (10U - value).unsafeGet());
380         value = std::numeric_limits<type>::min();
381         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - 1)).hasOverflowed());
382         EXPECT_EQ(true, !(value--).hasOverflowed());
383         EXPECT_EQ(true, value.hasOverflowed());
384         value = std::numeric_limits<type>::max();
385         EXPECT_EQ(true, !value.hasOverflowed());
386         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1)).hasOverflowed());
387         EXPECT_EQ(true, !(value++).hasOverflowed());
388         EXPECT_EQ(true, value.hasOverflowed());
389         value = std::numeric_limits<type>::max();
390         EXPECT_EQ(true, (value += 1).hasOverflowed());
391         EXPECT_EQ(true, value.hasOverflowed());
392         value = std::numeric_limits<type>::min();
393         EXPECT_EQ(true, (value - 1U).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.hasOverflowed());
398         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1U)).hasOverflowed());
399         EXPECT_EQ(true, !(value++).hasOverflowed());
400         EXPECT_EQ(true, value.hasOverflowed());
401         value = std::numeric_limits<type>::max();
402         EXPECT_EQ(true, (value += 1U).hasOverflowed());
403         EXPECT_EQ(true, value.hasOverflowed());
404     }
405 };
406
407 template <typename type, typename Coercer>
408 class IgnoreMixedSignednessTest {
409 public:
410     static void run() { }
411 };
412
413 template <typename type> class CoerceLiteralToUnsigned {
414 public:
415     static unsigned coerce(type x) { return static_cast<unsigned>(x); }
416 };
417     
418 template <typename type> class CoerceLiteralNop {
419 public:
420     static type coerce(type x) { return x; }
421 };
422
423 CheckedArithmeticTest(int8_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
424 CheckedArithmeticTest(int16_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
425 CheckedArithmeticTest(int32_t, CoerceLiteralNop, AllowMixedSignednessTest)
426 CheckedArithmeticTest(uint32_t, CoerceLiteralToUnsigned, AllowMixedSignednessTest)
427 CheckedArithmeticTest(int64_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
428 CheckedArithmeticTest(uint64_t, CoerceLiteralToUnsigned, IgnoreMixedSignednessTest)
429
430 } // namespace TestWebKitAPI