Compile warning (-Wsign-compare) on 32-bits at WebCore/platform/FileSystem.cpp
[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
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         bool unused;
172
173         _value = 75;
174         type _largeValue = 100;
175         type _smallValue = 50;
176
177         value = _smallValue;
178         nvalue = _value;
179         ovalue = _value;
180
181         // Make sure the OverflowCrashLogger is working as expected.
182         EXPECT_EQ(false, (ovalue.hasOverflowed()));
183         EXPECT_EQ(true, (resetOverflow(ovalue), ovalue.hasOverflowed()));
184         EXPECT_EQ(false, (resetOverflow(ovalue), ovalue.didCrash()));
185         EXPECT_EQ(true, (unused = (ovalue == ovalue), ovalue.didCrash()));
186         EXPECT_EQ(false, (resetOverflow(ovalue), ovalue.didCrash()));
187
188         EXPECT_EQ(false, nvalue.hasOverflowed());
189         EXPECT_EQ(false, nvalue.didCrash());
190
191         // Test operator== that should not overflow nor crash.
192         EXPECT_EQ(true, (nvalue == nvalue));
193         EXPECT_EQ(true, (nvalue == Checked<type, OverflowCrashLogger>(_value)));
194         EXPECT_EQ(false, (nvalue == value));
195         EXPECT_EQ(true, (nvalue == _value));
196         EXPECT_EQ(false, (nvalue == Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
197         EXPECT_EQ(false, (nvalue == std::numeric_limits<type>::max()));
198
199         EXPECT_EQ(false, nvalue.hasOverflowed());
200         EXPECT_EQ(false, nvalue.didCrash());
201
202         // Test operator!= that should not overflow nor crash.
203         EXPECT_EQ(false, (nvalue != nvalue));
204         EXPECT_EQ(false, (nvalue != Checked<type, OverflowCrashLogger>(_value)));
205         EXPECT_EQ(true, (nvalue != value));
206         EXPECT_EQ(false, (nvalue != _value));
207         EXPECT_EQ(true, (nvalue != Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
208         EXPECT_EQ(true, (nvalue != std::numeric_limits<type>::max()));
209
210         EXPECT_EQ(false, nvalue.hasOverflowed());
211         EXPECT_EQ(false, nvalue.didCrash());
212
213         // Test operator< that should not overflow nor crash.
214         EXPECT_EQ(false, (nvalue < nvalue));
215         EXPECT_EQ(false, (nvalue < value));
216         EXPECT_EQ(true, (nvalue < Checked<type, OverflowCrashLogger>(_largeValue)));
217         EXPECT_EQ(false, (nvalue < Checked<type, OverflowCrashLogger>(_value)));
218         EXPECT_EQ(false, (nvalue < Checked<type, OverflowCrashLogger>(_smallValue)));
219         EXPECT_EQ(true, (nvalue < _largeValue));
220         EXPECT_EQ(false, (nvalue < _value));
221         EXPECT_EQ(false, (nvalue < _smallValue));
222         EXPECT_EQ(true, (nvalue < Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
223         EXPECT_EQ(true, (nvalue < std::numeric_limits<type>::max()));
224
225         EXPECT_EQ(false, nvalue.hasOverflowed());
226         EXPECT_EQ(false, nvalue.didCrash());
227
228         // Test operator<= that should not overflow nor crash.
229         EXPECT_EQ(true, (nvalue <= nvalue));
230         EXPECT_EQ(false, (nvalue <= value));
231         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(_largeValue)));
232         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(_value)));
233         EXPECT_EQ(false, (nvalue <= Checked<type, OverflowCrashLogger>(_smallValue)));
234         EXPECT_EQ(true, (nvalue <= _largeValue));
235         EXPECT_EQ(true, (nvalue <= _value));
236         EXPECT_EQ(false, (nvalue <= _smallValue));
237         EXPECT_EQ(true, (nvalue <= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
238         EXPECT_EQ(true, (nvalue <= std::numeric_limits<type>::max()));
239
240         EXPECT_EQ(false, nvalue.hasOverflowed());
241         EXPECT_EQ(false, nvalue.didCrash());
242
243         // Test operator> that should not overflow nor crash.
244         EXPECT_EQ(false, (nvalue > nvalue));
245         EXPECT_EQ(true, (nvalue > value));
246         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(_largeValue)));
247         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(_value)));
248         EXPECT_EQ(true, (nvalue > Checked<type, OverflowCrashLogger>(_smallValue)));
249         EXPECT_EQ(false, (nvalue > _largeValue));
250         EXPECT_EQ(false, (nvalue > _value));
251         EXPECT_EQ(true, (nvalue > _smallValue));
252         EXPECT_EQ(false, (nvalue > Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
253         EXPECT_EQ(false, (nvalue > std::numeric_limits<type>::max()));
254
255         EXPECT_EQ(false, nvalue.hasOverflowed());
256         EXPECT_EQ(false, nvalue.didCrash());
257
258         // Test operator>= that should not overflow nor crash.
259         EXPECT_EQ(true, (nvalue >= nvalue));
260         EXPECT_EQ(true, (nvalue >= value));
261         EXPECT_EQ(false, (nvalue >= Checked<type, OverflowCrashLogger>(_largeValue)));
262         EXPECT_EQ(true, (nvalue >= Checked<type, OverflowCrashLogger>(_value)));
263         EXPECT_EQ(true, (nvalue >= Checked<type, OverflowCrashLogger>(_smallValue)));
264         EXPECT_EQ(false, (nvalue >= _largeValue));
265         EXPECT_EQ(true, (nvalue >= _value));
266         EXPECT_EQ(true, (nvalue >= _smallValue));
267         EXPECT_EQ(false, (nvalue >= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())));
268         EXPECT_EQ(false, (nvalue >= std::numeric_limits<type>::max()));
269
270         EXPECT_EQ(false, nvalue.hasOverflowed());
271         EXPECT_EQ(false, nvalue.didCrash());
272
273         // Test operator== with an overflowed value.
274         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == ovalue), ovalue.didCrash()));
275         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
276         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == value), ovalue.didCrash()));
277         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == _value), ovalue.didCrash()));
278         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == _value * std::numeric_limits<type>::max()), ovalue.didCrash()));
279         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
280         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == std::numeric_limits<type>::max()), ovalue.didCrash()));
281         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue == nvalue), ovalue.didCrash()));
282         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue == ovalue), ovalue.didCrash()));
283
284         EXPECT_EQ(false, nvalue.hasOverflowed());
285
286         // Test operator!= with an overflowed value.
287         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != ovalue), ovalue.didCrash()));
288         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
289         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != value), ovalue.didCrash()));
290         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != _value), ovalue.didCrash()));
291         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != _value * std::numeric_limits<type>::max()), ovalue.didCrash()));
292         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
293         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != std::numeric_limits<type>::max()), ovalue.didCrash()));
294         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue != nvalue), ovalue.didCrash()));
295         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue != ovalue), ovalue.didCrash()));
296
297         EXPECT_EQ(false, nvalue.hasOverflowed());
298
299         // Test operator< with an overflowed value.
300         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < ovalue), ovalue.didCrash()));
301         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < value), ovalue.didCrash()));
302         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
303         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
304         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
305         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _largeValue), ovalue.didCrash()));
306         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _value), ovalue.didCrash()));
307         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < _smallValue), ovalue.didCrash()));
308         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
309         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < std::numeric_limits<type>::max()), ovalue.didCrash()));
310         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue < nvalue), ovalue.didCrash()));
311         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue < ovalue), ovalue.didCrash()));
312
313         EXPECT_EQ(false, nvalue.hasOverflowed());
314
315         // Test operator<= with an overflowed value.
316         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= ovalue), ovalue.didCrash()));
317         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= value), ovalue.didCrash()));
318         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
319         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
320         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
321         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _largeValue), ovalue.didCrash()));
322         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _value), ovalue.didCrash()));
323         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= _smallValue), ovalue.didCrash()));
324         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
325         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= std::numeric_limits<type>::max()), ovalue.didCrash()));
326         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue <= nvalue), ovalue.didCrash()));
327         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue <= ovalue), ovalue.didCrash()));
328
329         EXPECT_EQ(false, nvalue.hasOverflowed());
330
331         // Test operator> with an overflowed value.
332         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > ovalue), ovalue.didCrash()));
333         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > value), ovalue.didCrash()));
334         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
335         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
336         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
337         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _largeValue), ovalue.didCrash()));
338         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _value), ovalue.didCrash()));
339         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > _smallValue), ovalue.didCrash()));
340         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
341         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > std::numeric_limits<type>::max()), ovalue.didCrash()));
342         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue > nvalue), ovalue.didCrash()));
343         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue > ovalue), ovalue.didCrash()));
344
345         EXPECT_EQ(false, nvalue.hasOverflowed());
346
347         // Test operator>= with an overflowed value.
348         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= ovalue), ovalue.didCrash()));
349         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= value), ovalue.didCrash()));
350         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_largeValue)), ovalue.didCrash()));
351         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_value)), ovalue.didCrash()));
352         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(_smallValue)), ovalue.didCrash()));
353         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _largeValue), ovalue.didCrash()));
354         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _value), ovalue.didCrash()));
355         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= _smallValue), ovalue.didCrash()));
356         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= Checked<type, OverflowCrashLogger>(std::numeric_limits<type>::max())), ovalue.didCrash()));
357         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= std::numeric_limits<type>::max()), ovalue.didCrash()));
358         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (ovalue >= nvalue), ovalue.didCrash()));
359         EXPECT_EQ(true, (resetOverflow(ovalue), unused = (nvalue >= ovalue), ovalue.didCrash()));
360
361         EXPECT_EQ(false, nvalue.hasOverflowed());
362
363         MixedSignednessTester::run();
364     }
365 };
366
367 template <typename type, typename Coercer>
368 class AllowMixedSignednessTest {
369 public:
370     static void run()
371     {
372         Checked<type, RecordOverflow> value;
373         value = 10;
374
375         EXPECT_EQ(coerceLiteral(0), (value + -10).unsafeGet());
376         EXPECT_EQ(0U, (value - 10U).unsafeGet());
377         EXPECT_EQ(coerceLiteral(0), (-10 + value).unsafeGet());
378         EXPECT_EQ(0U, (10U - value).unsafeGet());
379         value = std::numeric_limits<type>::min();
380         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - 1)).hasOverflowed());
381         EXPECT_EQ(true, !(value--).hasOverflowed());
382         EXPECT_EQ(true, value.hasOverflowed());
383         value = std::numeric_limits<type>::max();
384         EXPECT_EQ(true, !value.hasOverflowed());
385         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1)).hasOverflowed());
386         EXPECT_EQ(true, !(value++).hasOverflowed());
387         EXPECT_EQ(true, value.hasOverflowed());
388         value = std::numeric_limits<type>::max();
389         EXPECT_EQ(true, (value += 1).hasOverflowed());
390         EXPECT_EQ(true, value.hasOverflowed());
391         value = std::numeric_limits<type>::min();
392         EXPECT_EQ(true, (value - 1U).hasOverflowed());
393         EXPECT_EQ(true, !(value--).hasOverflowed());
394         EXPECT_EQ(true, value.hasOverflowed());
395         value = std::numeric_limits<type>::max();
396         EXPECT_EQ(true, !value.hasOverflowed());
397         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1U)).hasOverflowed());
398         EXPECT_EQ(true, !(value++).hasOverflowed());
399         EXPECT_EQ(true, value.hasOverflowed());
400         value = std::numeric_limits<type>::max();
401         EXPECT_EQ(true, (value += 1U).hasOverflowed());
402         EXPECT_EQ(true, value.hasOverflowed());
403     }
404 };
405
406 template <typename type, typename Coercer>
407 class IgnoreMixedSignednessTest {
408 public:
409     static void run() { }
410 };
411
412 template <typename type> class CoerceLiteralToUnsigned {
413 public:
414     static unsigned coerce(type x) { return static_cast<unsigned>(x); }
415 };
416     
417 template <typename type> class CoerceLiteralNop {
418 public:
419     static type coerce(type x) { return x; }
420 };
421
422 CheckedArithmeticTest(int8_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
423 CheckedArithmeticTest(int16_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
424 CheckedArithmeticTest(int32_t, CoerceLiteralNop, AllowMixedSignednessTest)
425 CheckedArithmeticTest(uint32_t, CoerceLiteralToUnsigned, AllowMixedSignednessTest)
426 CheckedArithmeticTest(int64_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
427 CheckedArithmeticTest(uint64_t, CoerceLiteralToUnsigned, IgnoreMixedSignednessTest)
428
429 TEST(CheckedArithmeticTest, IsInBounds)
430 {
431     // bigger precision, signed, signed
432     EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<int16_t>::max()));
433     EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<int16_t>::min()));
434
435     // bigger precision, unsigned, signed
436     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int32_t>::max()));
437     EXPECT_FALSE(WTF::isInBounds<uint32_t>(std::numeric_limits<int16_t>::min()));
438
439     EXPECT_FALSE(WTF::isInBounds<uint32_t>((int32_t)-1));
440     EXPECT_FALSE(WTF::isInBounds<uint16_t>((int32_t)-1));
441     EXPECT_FALSE(WTF::isInBounds<unsigned long>((int)-1));
442
443     EXPECT_TRUE(WTF::isInBounds<uint32_t>((int32_t)1));
444     EXPECT_TRUE(WTF::isInBounds<uint32_t>((int16_t)1));
445     EXPECT_TRUE(WTF::isInBounds<unsigned>((int)1));
446
447     EXPECT_TRUE(WTF::isInBounds<uint32_t>((int32_t)0));
448     EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)0));
449     EXPECT_TRUE(WTF::isInBounds<uint32_t>((int16_t)0));
450     EXPECT_TRUE(WTF::isInBounds<unsigned>((int)0));
451
452     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int32_t>::max()));
453     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<int16_t>::max()));
454     EXPECT_TRUE(WTF::isInBounds<unsigned>(std::numeric_limits<int>::max()));
455
456     // bigger precision, signed, unsigned
457     EXPECT_TRUE(WTF::isInBounds<int32_t>(std::numeric_limits<uint16_t>::max()));
458     EXPECT_FALSE(WTF::isInBounds<int32_t>(std::numeric_limits<uint32_t>::max()));
459     EXPECT_TRUE(WTF::isInBounds<int32_t>((uint32_t)0));
460
461     // bigger precision, unsigned, unsigned
462     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<uint16_t>::max()));
463     EXPECT_TRUE(WTF::isInBounds<uint32_t>(std::numeric_limits<uint16_t>::min()));
464
465     // lower precision, signed signed
466     EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<int32_t>::max()));
467     EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<int32_t>::min()));
468     EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)-1));
469     EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)0));
470     EXPECT_TRUE(WTF::isInBounds<int16_t>((int32_t)1));
471     // lower precision, unsigned, signed
472     EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<int32_t>::max()));
473     EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<int32_t>::min()));
474     EXPECT_FALSE(WTF::isInBounds<uint16_t>((int32_t)-1));
475     EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)0));
476     EXPECT_TRUE(WTF::isInBounds<uint16_t>((int32_t)1));
477     // lower precision, signed, unsigned
478     EXPECT_FALSE(WTF::isInBounds<int16_t>(std::numeric_limits<uint32_t>::max()));
479     EXPECT_TRUE(WTF::isInBounds<int16_t>((uint32_t)0));
480     EXPECT_TRUE(WTF::isInBounds<int16_t>((uint32_t)1));
481     // lower precision, unsigned, unsigned
482     EXPECT_FALSE(WTF::isInBounds<uint16_t>(std::numeric_limits<uint32_t>::max()));
483     EXPECT_TRUE(WTF::isInBounds<uint16_t>((uint32_t)0));
484     EXPECT_TRUE(WTF::isInBounds<uint16_t>((uint32_t)1));
485 }
486
487 } // namespace TestWebKitAPI