OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 #include <type_traits> | 9 #include <type_traits> |
10 | 10 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 TEST_EXPECTED_VALUE(-1, CheckedNumeric<Dst>(-1) % 2); | 113 TEST_EXPECTED_VALUE(-1, CheckedNumeric<Dst>(-1) % 2); |
114 TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(-1) % -2); | 114 TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(-1) % -2); |
115 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2); | 115 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2); |
116 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); | 116 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); |
117 // Test all the different modulus combinations. | 117 // Test all the different modulus combinations. |
118 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); | 118 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); |
119 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); | 119 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); |
120 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); | 120 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
121 CheckedNumeric<Dst> checked_dst = 1; | 121 CheckedNumeric<Dst> checked_dst = 1; |
122 TEST_EXPECTED_VALUE(0, checked_dst %= 1); | 122 TEST_EXPECTED_VALUE(0, checked_dst %= 1); |
| 123 // Test that div by 0 is avoided but returns invalid result. |
| 124 TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) % 0); |
123 } | 125 } |
124 | 126 |
125 // Unsigned integer arithmetic. | 127 // Unsigned integer arithmetic. |
126 template <typename Dst> | 128 template <typename Dst> |
127 static void TestSpecializedArithmetic( | 129 static void TestSpecializedArithmetic( |
128 const char* dst, | 130 const char* dst, |
129 int line, | 131 int line, |
130 typename std::enable_if<numeric_limits<Dst>::is_integer && | 132 typename std::enable_if<numeric_limits<Dst>::is_integer && |
131 !numeric_limits<Dst>::is_signed, | 133 !numeric_limits<Dst>::is_signed, |
132 int>::type = 0) { | 134 int>::type = 0) { |
(...skipping 15 matching lines...) Expand all Loading... |
148 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); | 150 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
149 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) % 2); | 151 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) % 2); |
150 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2); | 152 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2); |
151 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); | 153 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); |
152 // Test all the different modulus combinations. | 154 // Test all the different modulus combinations. |
153 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); | 155 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); |
154 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); | 156 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); |
155 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); | 157 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
156 CheckedNumeric<Dst> checked_dst = 1; | 158 CheckedNumeric<Dst> checked_dst = 1; |
157 TEST_EXPECTED_VALUE(0, checked_dst %= 1); | 159 TEST_EXPECTED_VALUE(0, checked_dst %= 1); |
| 160 // Test that div by 0 is avoided but returns invalid result. |
| 161 TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) % 0); |
158 } | 162 } |
159 | 163 |
160 // Floating point arithmetic. | 164 // Floating point arithmetic. |
161 template <typename Dst> | 165 template <typename Dst> |
162 void TestSpecializedArithmetic( | 166 void TestSpecializedArithmetic( |
163 const char* dst, | 167 const char* dst, |
164 int line, | 168 int line, |
165 typename std::enable_if<numeric_limits<Dst>::is_iec559, int>::type = 0) { | 169 typename std::enable_if<numeric_limits<Dst>::is_iec559, int>::type = 0) { |
166 typedef numeric_limits<Dst> DstLimits; | 170 typedef numeric_limits<Dst> DstLimits; |
167 TEST_EXPECTED_SUCCESS(-CheckedNumeric<Dst>(DstLimits::min())); | 171 TEST_EXPECTED_SUCCESS(-CheckedNumeric<Dst>(DstLimits::min())); |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 | 759 |
756 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); | 760 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); |
757 EXPECT_TRUE(too_large.IsValid()); | 761 EXPECT_TRUE(too_large.IsValid()); |
758 too_large += d; | 762 too_large += d; |
759 EXPECT_FALSE(too_large.IsValid()); | 763 EXPECT_FALSE(too_large.IsValid()); |
760 too_large -= d; | 764 too_large -= d; |
761 EXPECT_FALSE(too_large.IsValid()); | 765 EXPECT_FALSE(too_large.IsValid()); |
762 too_large /= d; | 766 too_large /= d; |
763 EXPECT_FALSE(too_large.IsValid()); | 767 EXPECT_FALSE(too_large.IsValid()); |
764 } | 768 } |
OLD | NEW |