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

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

Issue 1822353002: Revert of wtf/CheckedArithmetic.h delegates to base/numerics/safe_math.h. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
(Empty)
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 "wtf/CheckedArithmetic.h"
27
28 #include "testing/gtest/include/gtest/gtest.h"
29
30 namespace WTF {
31
32 #define CheckedArithmeticTest(type, coerceLiteral, MixedSignednessTest) \
33 TEST(CheckedArithmeticTest, Checked_##type) \
34 { \
35 Checked<type, RecordOverflow> value; \
36 EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \
37 EXPECT_EQ(std::numeric_limits<type>::max(), (value + std::numeric_limits <type>::max()).unsafeGet()); \
38 EXPECT_EQ(std::numeric_limits<type>::max(), (std::numeric_limits<type>:: max() + value).unsafeGet()); \
39 EXPECT_EQ(std::numeric_limits<type>::min(), (value + std::numeric_limits <type>::min()).unsafeGet()); \
40 EXPECT_EQ(std::numeric_limits<type>::min(), (std::numeric_limits<type>:: min() + value).unsafeGet()); \
41 EXPECT_EQ(coerceLiteral(0), (value * coerceLiteral(0)).unsafeGet()); \
42 EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) * value).unsafeGet()); \
43 EXPECT_EQ(coerceLiteral(0), (value * value).unsafeGet()); \
44 EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(0)).unsafeGet()); \
45 EXPECT_EQ(coerceLiteral(0), (coerceLiteral(0) - value).unsafeGet()); \
46 EXPECT_EQ(coerceLiteral(0), (value - value).unsafeGet()); \
47 EXPECT_EQ(coerceLiteral(0), (value++).unsafeGet()); \
48 EXPECT_EQ(coerceLiteral(1), (value--).unsafeGet()); \
49 EXPECT_EQ(coerceLiteral(1), (++value).unsafeGet()); \
50 EXPECT_EQ(coerceLiteral(0), (--value).unsafeGet()); \
51 EXPECT_EQ(coerceLiteral(10), (value += coerceLiteral(10)).unsafeGet()); \
52 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
53 EXPECT_EQ(coerceLiteral(100), (value *= coerceLiteral(10)).unsafeGet()); \
54 EXPECT_EQ(coerceLiteral(100), value.unsafeGet()); \
55 EXPECT_EQ(coerceLiteral(0), (value -= coerceLiteral(100)).unsafeGet()); \
56 EXPECT_EQ(coerceLiteral(0), value.unsafeGet()); \
57 value = 10; \
58 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
59 EXPECT_EQ(coerceLiteral(0), (value - coerceLiteral(10)).unsafeGet()); \
60 EXPECT_EQ(coerceLiteral(10), value.unsafeGet()); \
61 value = std::numeric_limits<type>::min(); \
62 EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - coerceLiteral(1)) ).hasOverflowed()); \
63 EXPECT_EQ(true, !((value--).hasOverflowed())); \
64 EXPECT_EQ(true, value.hasOverflowed()); \
65 value = std::numeric_limits<type>::max(); \
66 EXPECT_EQ(true, !value.hasOverflowed()); \
67 EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + coerceLiteral(1)) ).hasOverflowed()); \
68 EXPECT_EQ(true, !(value++).hasOverflowed()); \
69 EXPECT_EQ(true, value.hasOverflowed()); \
70 value = std::numeric_limits<type>::max(); \
71 EXPECT_EQ(true, (value += coerceLiteral(1)).hasOverflowed()); \
72 EXPECT_EQ(true, value.hasOverflowed()); \
73 value = 10; \
74 type _value = 0; \
75 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(0)).safeGet(_value)); \
76 _value = 0; \
77 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(0) * value).safeGet(_value)); \
78 _value = 0; \
79 EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, Reco rdOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
80 _value = 0; \
81 EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverfl ow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \
82 value = 0; \
83 _value = 0; \
84 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
85 _value = 0; \
86 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \
87 value = 1; \
88 _value = 0; \
89 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
90 _value = 0; \
91 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(std::numeric_limits<type>::max()) * value).safeGet(_value)); \
92 _value = 0; \
93 value = 0; \
94 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
95 _value = 0; \
96 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(std::numeric_limits<type>::max()) * (type)0).safeGet(_value)); \
97 _value = 0; \
98 value = 1; \
99 EXPECT_EQ(true, CheckedState::DidNotOverflow == (value * Checked<type, R ecordOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
100 _value = 0; \
101 EXPECT_EQ(true, CheckedState::DidNotOverflow == (Checked<type, RecordOve rflow>(std::numeric_limits<type>::max()) * (type)1).safeGet(_value)); \
102 _value = 0; \
103 value = 2; \
104 EXPECT_EQ(true, CheckedState::DidOverflow == (value * Checked<type, Reco rdOverflow>(std::numeric_limits<type>::max())).safeGet(_value)); \
105 _value = 0; \
106 EXPECT_EQ(true, CheckedState::DidOverflow == (Checked<type, RecordOverfl ow>(std::numeric_limits<type>::max()) * (type)2).safeGet(_value)); \
107 value = 10; \
108 EXPECT_EQ(true, (value * Checked<type, RecordOverflow>(std::numeric_limi ts<type>::max())).hasOverflowed()); \
109 MixedSignednessTest(EXPECT_EQ(coerceLiteral(0), (value + -10).unsafeGet( ))); \
110 MixedSignednessTest(EXPECT_EQ(0U, (value - 10U).unsafeGet())); \
111 MixedSignednessTest(EXPECT_EQ(coerceLiteral(0), (-10 + value).unsafeGet( ))); \
112 MixedSignednessTest(EXPECT_EQ(0U, (10U - value).unsafeGet())); \
113 value = std::numeric_limits<type>::min(); \
114 MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value - 1)).hasOverflowed())); \
115 MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \
116 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
117 value = std::numeric_limits<type>::max(); \
118 MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \
119 MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1)).hasOverflowed())); \
120 MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \
121 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
122 value = std::numeric_limits<type>::max(); \
123 MixedSignednessTest(EXPECT_EQ(true, (value += 1).hasOverflowed())); \
124 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
125 value = std::numeric_limits<type>::min(); \
126 MixedSignednessTest(EXPECT_EQ(true, (value - 1U).hasOverflowed())); \
127 MixedSignednessTest(EXPECT_EQ(true, !(value--).hasOverflowed())); \
128 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
129 value = std::numeric_limits<type>::max(); \
130 MixedSignednessTest(EXPECT_EQ(true, !value.hasOverflowed())); \
131 MixedSignednessTest(EXPECT_EQ(true, (Checked<type, RecordOverflow>(value + 1U)).hasOverflowed())); \
132 MixedSignednessTest(EXPECT_EQ(true, !(value++).hasOverflowed())); \
133 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
134 value = std::numeric_limits<type>::max(); \
135 MixedSignednessTest(EXPECT_EQ(true, (value += 1U).hasOverflowed())); \
136 MixedSignednessTest(EXPECT_EQ(true, value.hasOverflowed())); \
137 }
138
139 #define CoerceLiteralToUnsigned(x) x##U
140 #define CoerceLiteralNop(x) x
141 #define AllowMixedSignednessTest(x) x
142 #define IgnoreMixedSignednessTest(x)
143 CheckedArithmeticTest(int8_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
144 CheckedArithmeticTest(int16_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
145 CheckedArithmeticTest(int32_t, CoerceLiteralNop, AllowMixedSignednessTest)
146 CheckedArithmeticTest(uint32_t, CoerceLiteralToUnsigned, AllowMixedSignednessTes t)
147 CheckedArithmeticTest(int64_t, CoerceLiteralNop, IgnoreMixedSignednessTest)
148 CheckedArithmeticTest(uint64_t, CoerceLiteralToUnsigned, IgnoreMixedSignednessTe st)
149
150 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/CheckedArithmetic.h ('k') | third_party/WebKit/Source/wtf/CheckedNumeric.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698