Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: third_party/WebKit/Source/wtf/CheckedArithmeticTest.cpp

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 11 matching lines...) Expand all
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "wtf/CheckedArithmetic.h" 26 #include "wtf/CheckedArithmetic.h"
27 27
28 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
29 29
30 namespace WTF { 30 namespace WTF {
31 31
32 #define CheckedArithmeticTest(type, coerceLiteral, MixedSignednessTest) \ 32 #define CheckedArithmeticTest(type, coerceLiteral, MixedSignednessTest) \
33 TEST(CheckedArithmeticTest, Checked_##type) \ 33 TEST(CheckedArithmeticTest, Checked_##type) { \
34 { \ 34 Checked<type, RecordOverflow> value; \
35 Checked<type, RecordOverflow> value; \ 35 EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \
36 EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \ 36 EXPECT_EQ(std::numeric_limits<type>::max(), \
37 EXPECT_EQ(std::numeric_limits<type>::max(), (value + std::numeric_limits <type>::max()).unsafeGet()); \ 37 (value + std::numeric_limits<type>::max()).unsafeGet()); \
38 EXPECT_EQ(std::numeric_limits<type>::max(), (std::numeric_limits<type>:: max() + value).unsafeGet()); \ 38 EXPECT_EQ(std::numeric_limits<type>::max(), \
39 EXPECT_EQ(std::numeric_limits<type>::min(), (value + std::numeric_limits <type>::min()).unsafeGet()); \ 39 (std::numeric_limits<type>::max() + value).unsafeGet()); \
40 EXPECT_EQ(std::numeric_limits<type>::min(), (std::numeric_limits<type>:: min() + value).unsafeGet()); \ 40 EXPECT_EQ(std::numeric_limits<type>::min(), \
41 EXPECT_EQ(coerceLiteral(0), (value * coerceLiteral(0)).unsafeGet()); \ 41 (value + std::numeric_limits<type>::min()).unsafeGet()); \
42 EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) * value).unsafeGet()); \ 42 EXPECT_EQ(std::numeric_limits<type>::min(), \
43 EXPECT_EQ(coerceLiteral(0), (value * value).unsafeGet()); \ 43 (std::numeric_limits<type>::min() + value).unsafeGet()); \
44 EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(0)).unsafeGet()); \ 44 EXPECT_EQ(coerceLiteral(0), (value * coerceLiteral(0)).unsafeGet()); \
45 EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) - value).unsafeGet()); \ 45 EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) * value).unsafeGet()); \
46 EXPECT_EQ(coerceLiteral(0), (value - value).unsafeGet()); \ 46 EXPECT_EQ(coerceLiteral(0), (value * value).unsafeGet()); \
47 EXPECT_EQ(coerceLiteral(0), (value++).unsafeGet()); \ 47 EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(0)).unsafeGet()); \
48 EXPECT_EQ(coerceLiteral(1), (value--).unsafeGet()); \ 48 EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) - value).unsafeGet()); \
49 EXPECT_EQ(coerceLiteral(1), (++value).unsafeGet()); \ 49 EXPECT_EQ(coerceLiteral(0), (value - value).unsafeGet()); \
50 EXPECT_EQ(coerceLiteral(0), (--value).unsafeGet()); \ 50 EXPECT_EQ(coerceLiteral(0), (value++).unsafeGet()); \
51 EXPECT_EQ(coerceLiteral(10), (value += coerceLiteral(10)).unsafeGet()); \ 51 EXPECT_EQ(coerceLiteral(1), (value--).unsafeGet()); \
52 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \ 52 EXPECT_EQ(coerceLiteral(1), (++value).unsafeGet()); \
53 EXPECT_EQ(coerceLiteral(100), (value *= coerceLiteral(10)).unsafeGet()); \ 53 EXPECT_EQ(coerceLiteral(0), (--value).unsafeGet()); \
54 EXPECT_EQ(coerceLiteral(100), value.unsafeGet()); \ 54 EXPECT_EQ(coerceLiteral(10), (value += coerceLiteral(10)).unsafeGet()); \
55 EXPECT_EQ(coerceLiteral(0), (value -= coerceLiteral(100)).unsafeGet()); \ 55 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
56 EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \ 56 EXPECT_EQ(coerceLiteral(100), (value *= coerceLiteral(10)).unsafeGet()); \
57 value = 10; \ 57 EXPECT_EQ(coerceLiteral(100), value.unsafeGet()); \
58 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \ 58 EXPECT_EQ(coerceLiteral(0), (value -= coerceLiteral(100)).unsafeGet()); \
59 EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(10)).unsafeGet()); \ 59 EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \
60 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \ 60 value = 10; \
61 value = std::numeric_limits<type>::min(); \ 61 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
62 EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - coerceLiteral(1)) ).hasOverflowed()); \ 62 EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(10)).unsafeGet()); \
63 EXPECT_EQ(true, !((value--).hasOverflowed())); \ 63 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
64 EXPECT_EQ(true, value.hasOverflowed()); \ 64 value = std::numeric_limits<type>::min(); \
65 value = std::numeric_limits<type>::max(); \ 65 EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - coerceLiteral(1))) \
66 EXPECT_EQ(true, !value.hasOverflowed()); \ 66 .hasOverflowed()); \
67 EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + coerceLiteral(1)) ).hasOverflowed()); \ 67 EXPECT_EQ(true, !((value--).hasOverflowed())); \
68 EXPECT_EQ(true, !(value++).hasOverflowed()); \ 68 EXPECT_EQ(true, value.hasOverflowed()); \
69 EXPECT_EQ(true, value.hasOverflowed()); \ 69 value = std::numeric_limits<type>::max(); \
70 value = std::numeric_limits<type>::max(); \ 70 EXPECT_EQ(true, !value.hasOverflowed()); \
71 EXPECT_EQ(true, (value += coerceLiteral(1)).hasOverflowed()); \ 71 EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + coerceLiteral(1))) \
72 EXPECT_EQ(true, value.hasOverflowed()); \ 72 .hasOverflowed()); \
73 value = 10; \ 73 EXPECT_EQ(true, !(value++).hasOverflowed()); \
74 type _value = 0; \ 74 EXPECT_EQ(true, value.hasOverflowed()); \
75 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(0)).safeGet(_value)); \ 75 value = std::numeric_limits<type>::max(); \
76 _value = 0; \ 76 EXPECT_EQ(true, (value += coerceLiteral(1)).hasOverflowed()); \
77 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(0) * value).safeGet(_value)); \ 77 EXPECT_EQ(true, value.hasOverflowed()); \
78 _value = 0; \ 78 value = 10; \
79 EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, Reco rdOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \ 79 type _value = 0; \
80 _value = 0; \ 80 EXPECT_EQ(true, \
81 EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverfl ow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \ 81 CheckedState::DidNotOverflow == \
82 value = 0; \ 82 (value * Checked<type, RecordOverflow>(0)).safeGet(_value)); \
83 _value = 0; \ 83 _value = 0; \
84 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \ 84 EXPECT_EQ(true, \
85 _value = 0; \ 85 CheckedState::DidNotOverflow == \
86 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \ 86 (Checked<type, RecordOverflow>(0) * value).safeGet(_value)); \
87 value = 1; \ 87 _value = 0; \
88 _value = 0; \ 88 EXPECT_EQ(true, CheckedState::DidOverflow == \
89 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \ 89 (value * Checked<type, RecordOverflow>( \
90 _value = 0; \ 90 std::numeric_limits<type>::max())) \
91 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \ 91 .safeGet(_value)); \
92 _value = 0; \ 92 _value = 0; \
93 value = 0; \ 93 EXPECT_EQ( \
94 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \ 94 true, \
95 _value = 0; \ 95 CheckedState::DidOverflow == \
96 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(std::numeric_limits<type>::max()) * (type)0).safeGet(_value)); \ 96 (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * \
97 _value = 0; \ 97 value) \
98 value = 1; \ 98 .safeGet(_value)); \
99 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \ 99 value = 0; \
100 _value = 0; \ 100 _value = 0; \
101 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(std::numeric_limits<type>::max()) * (type)1).safeGet(_value)); \ 101 EXPECT_EQ(true, CheckedState::DidNotOverflow == \
102 _value = 0; \ 102 (value * Checked<type, RecordOverflow>( \
103 value = 2; \ 103 std::numeric_limits<type>::max())) \
104 EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, Reco rdOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \ 104 .safeGet(_value)); \
105 _value = 0; \ 105 _value = 0; \
106 EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverfl ow>(std::numeric_limits<type>::max()) * (type)2).safeGet(_value)); \ 106 EXPECT_EQ( \
107 value = 10; \ 107 true, \
108 EXPECT_EQ(true, (value * Checked<type, RecordOverflow>(std::numeric_limi ts<type>::max())).hasOverflowed()); \ 108 CheckedState::DidNotOverflow == \
109 MixedSignednessTest(EXPECT_EQ(coerceLiteral(0), (value + -10).unsafeGet( ))); \ 109 (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * \
110 MixedSignednessTest(EXPECT_EQ(0U, (value - 10U).unsafeGet())); \ 110 value) \
111 MixedSignednessTest(EXPECT_EQ(coerceLiteral(0), (-10 + value).unsafeGet( ))); \ 111 .safeGet(_value)); \
112 MixedSignednessTest(EXPECT_EQ(0U, (10U - value).unsafeGet())); \ 112 value = 1; \
113 value = std::numeric_limits<type>::min(); \ 113 _value = 0; \
114 MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - 1)).hasOverflowed())); \ 114 EXPECT_EQ(true, CheckedState::DidNotOverflow == \
115 MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \ 115 (value * Checked<type, RecordOverflow>( \
116 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \ 116 std::numeric_limits<type>::max())) \
117 value = std::numeric_limits<type>::max(); \ 117 .safeGet(_value)); \
118 MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \ 118 _value = 0; \
119 MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1)).hasOverflowed())); \ 119 EXPECT_EQ( \
120 MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \ 120 true, \
121 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \ 121 CheckedState::DidNotOverflow == \
122 value = std::numeric_limits<type>::max(); \ 122 (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * \
123 MixedSignednessTest(EXPECT_EQ(true, (value += 1).hasOverflowed())); \ 123 value) \
124 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \ 124 .safeGet(_value)); \
125 value = std::numeric_limits<type>::min(); \ 125 _value = 0; \
126 MixedSignednessTest(EXPECT_EQ(true, (value - 1U).hasOverflowed())); \ 126 value = 0; \
127 MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \ 127 EXPECT_EQ(true, CheckedState::DidNotOverflow == \
128 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \ 128 (value * Checked<type, RecordOverflow>( \
129 value = std::numeric_limits<type>::max(); \ 129 std::numeric_limits<type>::max())) \
130 MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \ 130 .safeGet(_value)); \
131 MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1U)).hasOverflowed())); \ 131 _value = 0; \
132 MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \ 132 EXPECT_EQ( \
133 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \ 133 true, \
134 value = std::numeric_limits<type>::max(); \ 134 CheckedState::DidNotOverflow == \
135 MixedSignednessTest(EXPECT_EQ(true, (value += 1U).hasOverflowed())); \ 135 (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * \
136 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \ 136 (type)0) \
137 } 137 .safeGet(_value)); \
138 _value = 0; \
139 value = 1; \
140 EXPECT_EQ(true, CheckedState::DidNotOverflow == \
141 (value * Checked<type, RecordOverflow>( \
142 std::numeric_limits<type>::max())) \
143 .safeGet(_value)); \
144 _value = 0; \
145 EXPECT_EQ( \
146 true, \
147 CheckedState::DidNotOverflow == \
148 (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * \
149 (type)1) \
150 .safeGet(_value)); \
151 _value = 0; \
152 value = 2; \
153 EXPECT_EQ(true, CheckedState::DidOverflow == \
154 (value * Checked<type, RecordOverflow>( \
155 std::numeric_limits<type>::max())) \
156 .safeGet(_value)); \
157 _value = 0; \
158 EXPECT_EQ( \
159 true, \
160 CheckedState::DidOverflow == \
161 (Checked<type, RecordOverflow>(std::numeric_limits<type>::max()) * \
162 (type)2) \
163 .safeGet(_value)); \
164 value = 10; \
165 EXPECT_EQ(true, (value * Checked<type, RecordOverflow>( \
166 std::numeric_limits<type>::max())) \
167 .hasOverflowed()); \
168 MixedSignednessTest( \
169 EXPECT_EQ(coerceLiteral(0), (value + -10).unsafeGet())); \
170 MixedSignednessTest(EXPECT_EQ(0U, (value - 10U).unsafeGet())); \
171 MixedSignednessTest( \
172 EXPECT_EQ(coerceLiteral(0), (-10 + value).unsafeGet())); \
173 MixedSignednessTest(EXPECT_EQ(0U, (10U - value).unsafeGet())); \
174 value = std::numeric_limits<type>::min(); \
175 MixedSignednessTest(EXPECT_EQ( \
176 true, (Checked<type, RecordOverflow>(value - 1)).hasOverflowed())); \
177 MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \
178 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
179 value = std::numeric_limits<type>::max(); \
180 MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \
181 MixedSignednessTest(EXPECT_EQ( \
182 true, (Checked<type, RecordOverflow>(value + 1)).hasOverflowed())); \
183 MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \
184 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
185 value = std::numeric_limits<type>::max(); \
186 MixedSignednessTest(EXPECT_EQ(true, (value += 1).hasOverflowed())); \
187 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
188 value = std::numeric_limits<type>::min(); \
189 MixedSignednessTest(EXPECT_EQ(true, (value - 1U).hasOverflowed())); \
190 MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \
191 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
192 value = std::numeric_limits<type>::max(); \
193 MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \
194 MixedSignednessTest(EXPECT_EQ( \
195 true, (Checked<type, RecordOverflow>(value + 1U)).hasOverflowed())); \
196 MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \
197 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
198 value = std::numeric_limits<type>::max(); \
199 MixedSignednessTest(EXPECT_EQ(true, (value += 1U).hasOverflowed())); \
200 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
201 }
138 202
139 #define CoerceLiteralToUnsigned(x) x##U 203 #define CoerceLiteralToUnsigned(x) x##U
140 #define CoerceLiteralNop(x) x 204 #define CoerceLiteralNop(x) x
141 #define AllowMixedSignednessTest(x) x 205 #define AllowMixedSignednessTest(x) x
142 #define IgnoreMixedSignednessTest(x) 206 #define IgnoreMixedSignednessTest(x)
143 CheckedArithmeticTest(int8_t, CoerceLiteralNop, IgnoreMixedSignednessTest) 207 CheckedArithmeticTest(int8_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
144 CheckedArithmeticTest(int16_t, CoerceLiteralNop, IgnoreMixedSignednessTest) 208 CheckedArithmeticTest(int16_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
145 CheckedArithmeticTest(int32_t, CoerceLiteralNop, AllowMixedSignednessTest) 209 CheckedArithmeticTest(int32_t,
146 CheckedArithmeticTest(uint32_t, CoerceLiteralToUnsigned, AllowMixedSignednessTes t) 210 CoerceLiteralNop,
147 CheckedArithmeticTest(int64_t, CoerceLiteralNop, IgnoreMixedSignednessTest) 211 AllowMixedSignednessTest)
148 CheckedArithmeticTest(uint64_t, CoerceLiteralToUnsigned, IgnoreMixedSignednessTe st) 212 CheckedArithmeticTest(uint32_t,
213 CoerceLiteralToUnsigned,
214 AllowMixedSignednessTest)
215 CheckedArithmeticTest(int64_t,
216 CoerceLiteralNop,
217 IgnoreMixedSignednessTest)
218 CheckedArithmeticTest(uint64_t,
219 CoerceLiteralToUnsigned,
220 IgnoreMixedSignednessTest)
149 221
150 } // namespace WTF 222 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/CheckedArithmetic.h ('k') | third_party/WebKit/Source/wtf/Compiler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698