Let TestWebKitAPI work for chromium
[WebKit.git] / Tools / TestWebKitAPI / Tests / WTF / CheckedArithmeticOperations.cpp
1 /*
2  * Copyright (C) 2011 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 #define CheckedArithmeticTest(type, coerceLiteral, MixedSignednessTest) \
32     TEST(WTF, Checked_##type) \
33     { \
34         Checked<type, RecordOverflow> value; \
35         EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \
36         EXPECT_EQ(std::numeric_limits<type>::max(), (value + std::numeric_limits<type>::max()).unsafeGet()); \
37         EXPECT_EQ(std::numeric_limits<type>::max(), (std::numeric_limits<type>::max() + value).unsafeGet()); \
38         EXPECT_EQ(std::numeric_limits<type>::min(), (value + std::numeric_limits<type>::min()).unsafeGet()); \
39         EXPECT_EQ(std::numeric_limits<type>::min(), (std::numeric_limits<type>::min() + value).unsafeGet()); \
40         EXPECT_EQ(coerceLiteral(0), (value * coerceLiteral(0)).unsafeGet()); \
41         EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) * value).unsafeGet()); \
42         EXPECT_EQ(coerceLiteral(0), (value * value).unsafeGet()); \
43         EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(0)).unsafeGet()); \
44         EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) - value).unsafeGet()); \
45         EXPECT_EQ(coerceLiteral(0), (value - value).unsafeGet()); \
46         EXPECT_EQ(coerceLiteral(0), (value++).unsafeGet()); \
47         EXPECT_EQ(coerceLiteral(1), (value--).unsafeGet()); \
48         EXPECT_EQ(coerceLiteral(1), (++value).unsafeGet()); \
49         EXPECT_EQ(coerceLiteral(0), (--value).unsafeGet()); \
50         EXPECT_EQ(coerceLiteral(10), (value += coerceLiteral(10)).unsafeGet()); \
51         EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
52         EXPECT_EQ(coerceLiteral(100), (value *= coerceLiteral(10)).unsafeGet()); \
53         EXPECT_EQ(coerceLiteral(100), value.unsafeGet()); \
54         EXPECT_EQ(coerceLiteral(0), (value -= coerceLiteral(100)).unsafeGet()); \
55         EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \
56         value = 10; \
57         EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
58         EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(10)).unsafeGet()); \
59         EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
60         value = std::numeric_limits<type>::min(); \
61         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - coerceLiteral(1))).hasOverflowed()); \
62         EXPECT_EQ(true, !((value--).hasOverflowed())); \
63         EXPECT_EQ(true, value.hasOverflowed()); \
64         value = std::numeric_limits<type>::max(); \
65         EXPECT_EQ(true, !value.hasOverflowed()); \
66         EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + coerceLiteral(1))).hasOverflowed()); \
67         EXPECT_EQ(true, !(value++).hasOverflowed()); \
68         EXPECT_EQ(true, value.hasOverflowed()); \
69         value = std::numeric_limits<type>::max(); \
70         EXPECT_EQ(true, (value += coerceLiteral(1)).hasOverflowed()); \
71         EXPECT_EQ(true, value.hasOverflowed()); \
72         value = 10; \
73         MixedSignednessTest(EXPECT_EQ(coerceLiteral(0), (value + -10).unsafeGet())); \
74         MixedSignednessTest(EXPECT_EQ(0U, (value - 10U).unsafeGet())); \
75         MixedSignednessTest(EXPECT_EQ(coerceLiteral(0), (-10 + value).unsafeGet())); \
76         MixedSignednessTest(EXPECT_EQ(0U, (10U - value).unsafeGet())); \
77         value = std::numeric_limits<type>::min(); \
78         MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - 1)).hasOverflowed())); \
79         MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \
80         MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
81         value = std::numeric_limits<type>::max(); \
82         MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \
83         MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1)).hasOverflowed())); \
84         MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \
85         MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
86         value = std::numeric_limits<type>::max(); \
87         MixedSignednessTest(EXPECT_EQ(true, (value += 1).hasOverflowed())); \
88         MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
89         value = std::numeric_limits<type>::min(); \
90         MixedSignednessTest(EXPECT_EQ(true, (value - 1U).hasOverflowed())); \
91         MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \
92         MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
93         value = std::numeric_limits<type>::max(); \
94         MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \
95         MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1U)).hasOverflowed())); \
96         MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \
97         MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
98         value = std::numeric_limits<type>::max(); \
99         MixedSignednessTest(EXPECT_EQ(true, (value += 1U).hasOverflowed())); \
100         MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
101     }
102
103 #define CoerceLiteralToUnsigned(x) x##U
104 #define CoerceLiteralNop(x) x
105 #define AllowMixedSignednessTest(x) x
106 #define IgnoreMixedSignednessTest(x)
107 CheckedArithmeticTest(int8_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
108 CheckedArithmeticTest(int16_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
109 CheckedArithmeticTest(int32_t, CoerceLiteralNop, AllowMixedSignednessTest)
110 CheckedArithmeticTest(uint32_t, CoerceLiteralToUnsigned, AllowMixedSignednessTest)
111 CheckedArithmeticTest(int64_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
112 CheckedArithmeticTest(uint64_t, CoerceLiteralToUnsigned, IgnoreMixedSignednessTest)
113
114 } // namespace TestWebKitAPI