2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/Decimal.h"
34 #include "wtf/MathExtras.h"
35 #include "wtf/text/CString.h"
37 #include <gtest/gtest.h>
41 std::ostream
& operator<<(std::ostream
& os
, const Decimal
& decimal
)
43 Decimal::EncodedData data
= decimal
.value();
45 << "encode(" << String::number(data
.coefficient()).ascii().data()
46 << ", " << String::number(data
.exponent()).ascii().data()
47 << ", " << (data
.sign() == Decimal::Negative
? "Negative" : "Positive")
48 << ")=" << decimal
.toString().ascii().data();
51 // Simulate WebCore/html/StepRange
52 class DecimalStepRange
{
58 DecimalStepRange(const Decimal
& minimum
, const Decimal
& maximum
, const Decimal
& step
)
65 Decimal
clampValue(Decimal value
) const
67 const Decimal result
= minimum
+ ((value
- minimum
) / step
).round() * step
;
68 ASSERT(result
.isFinite());
69 return result
> maximum
? result
- step
: result
;
73 class DecimalTest
: public ::testing::Test
{
75 using Sign
= Decimal::Sign
;
76 static const Sign Positive
= Decimal::Positive
;
77 static const Sign Negative
= Decimal::Negative
;
79 Decimal
encode(uint64_t coefficient
, int exponent
, Sign sign
)
81 return Decimal(sign
, exponent
, coefficient
);
84 Decimal
fromString(const String
& string
)
86 return Decimal::fromString(string
);
89 Decimal
stepDown(const String
& minimum
, const String
& maximum
, const String
& step
, const String
& valueString
, int numberOfStepTimes
)
91 DecimalStepRange
stepRange(fromString(minimum
), fromString(maximum
), fromString(step
));
92 Decimal value
= fromString(valueString
);
93 for (int i
= 0; i
< numberOfStepTimes
; ++i
) {
94 value
-= stepRange
.step
;
95 value
= stepRange
.clampValue(value
);
100 Decimal
stepUp(const String
& minimum
, const String
& maximum
, const String
& step
, const String
& valueString
, int numberOfStepTimes
)
102 DecimalStepRange
stepRange(fromString(minimum
), fromString(maximum
), fromString(step
));
103 Decimal value
= fromString(valueString
);
104 for (int i
= 0; i
< numberOfStepTimes
; ++i
) {
105 value
+= stepRange
.step
;
106 value
= stepRange
.clampValue(value
);
112 // FIXME: We should use expectedSign without "Decimal::", however, g++ causes undefined references for DecimalTest::Positive and Negative.
113 #define EXPECT_DECIMAL_ENCODED_DATA_EQ(expectedCoefficient, expectedExponent, expectedSign, decimal) \
114 EXPECT_EQ((expectedCoefficient), (decimal).value().coefficient()); \
115 EXPECT_EQ((expectedExponent), (decimal).value().exponent()); \
116 EXPECT_EQ(Decimal::expectedSign, (decimal).value().sign());
118 #define EXPECT_DECIMAL_STREQ(expected, decimal) EXPECT_STREQ((expected), (decimal).toString().ascii().data())
120 TEST_F(DecimalTest
, Abs
)
122 EXPECT_EQ(encode(0, 0, Positive
), encode(0, 0, Positive
).abs());
123 EXPECT_EQ(encode(0, 0, Positive
), encode(0, 0, Negative
).abs());
125 EXPECT_EQ(encode(0, 10, Positive
), encode(0, 10, Positive
).abs());
126 EXPECT_EQ(encode(0, 10, Positive
), encode(0, 10, Negative
).abs());
128 EXPECT_EQ(encode(0, -10, Positive
), encode(0, -10, Positive
).abs());
129 EXPECT_EQ(encode(0, -10, Positive
), encode(0, -10, Negative
).abs());
131 EXPECT_EQ(encode(1, 0, Positive
), encode(1, 0, Positive
).abs());
132 EXPECT_EQ(encode(1, 0, Positive
), encode(1, 0, Negative
).abs());
134 EXPECT_EQ(encode(1, 10, Positive
), encode(1, 10, Positive
).abs());
135 EXPECT_EQ(encode(1, 10, Positive
), encode(1, 10, Negative
).abs());
137 EXPECT_EQ(encode(1, -10, Positive
), encode(1, -10, Positive
).abs());
138 EXPECT_EQ(encode(1, -10, Positive
), encode(1, -10, Negative
).abs());
141 TEST_F(DecimalTest
, AbsBigExponent
)
143 EXPECT_EQ(encode(1, 1000, Positive
), encode(1, 1000, Positive
).abs());
144 EXPECT_EQ(encode(1, 1000, Positive
), encode(1, 1000, Negative
).abs());
147 TEST_F(DecimalTest
, AbsSmallExponent
)
149 EXPECT_EQ(encode(1, -1000, Positive
), encode(1, -1000, Positive
).abs());
150 EXPECT_EQ(encode(1, -1000, Positive
), encode(1, -1000, Negative
).abs());
153 TEST_F(DecimalTest
, AbsSpecialValues
)
155 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Positive
).abs());
156 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Negative
).abs());
157 EXPECT_EQ(Decimal::nan(), Decimal::nan().abs());
160 TEST_F(DecimalTest
, Add
)
162 EXPECT_EQ(encode(0, 0, Positive
), Decimal(0) + Decimal(0));
163 EXPECT_EQ(Decimal(1), Decimal(2) + Decimal(-1));
164 EXPECT_EQ(Decimal(1), Decimal(-1) + Decimal(2));
165 EXPECT_EQ(encode(100, 0, Positive
), Decimal(99) + Decimal(1));
166 EXPECT_EQ(encode(100, 0, Negative
), Decimal(-50) + Decimal(-50));
167 EXPECT_EQ(encode(UINT64_C(1000000000000000), 35, Positive
), encode(1, 50, Positive
) + Decimal(1));
168 EXPECT_EQ(encode(UINT64_C(1000000000000000), 35, Positive
), Decimal(1) + encode(1, 50, Positive
));
169 EXPECT_EQ(encode(UINT64_C(10000000001), 0, Positive
), encode(1, 10, Positive
) + Decimal(1));
170 EXPECT_EQ(encode(UINT64_C(10000000001), 0, Positive
), Decimal(1) + encode(1, 10, Positive
));
171 EXPECT_EQ(encode(1, 0, Positive
), encode(1, -1022, Positive
) + encode(1, 0, Positive
));
172 EXPECT_EQ(encode(2, -1022, Positive
), encode(1, -1022, Positive
) + encode(1, -1022, Positive
));
175 TEST_F(DecimalTest
, AddBigExponent
)
177 EXPECT_EQ(encode(1, 1022, Positive
), encode(1, 1022, Positive
) + encode(1, 0, Positive
));
178 EXPECT_EQ(encode(2, 1022, Positive
), encode(1, 1022, Positive
) + encode(1, 1022, Positive
));
179 EXPECT_EQ(Decimal::infinity(Positive
), encode(std::numeric_limits
<uint64_t>::max(), 1022, Positive
) + encode(1, 0, Positive
));
180 EXPECT_EQ(encode(1, 1022, Positive
), encode(1, 1022, Positive
) + encode(1, -1000, Positive
));
183 TEST_F(DecimalTest
, AddSmallExponent
)
185 EXPECT_EQ(encode(1, 0, Positive
), encode(1, -1022, Positive
) + encode(1, 0, Positive
));
186 EXPECT_EQ(encode(2, -1022, Positive
), encode(1, -1022, Positive
) + encode(1, -1022, Positive
));
189 TEST_F(DecimalTest
, AddSpecialValues
)
191 const Decimal
Infinity(Decimal::infinity(Positive
));
192 const Decimal
MinusInfinity(Decimal::infinity(Negative
));
193 const Decimal
NaN(Decimal::nan());
194 const Decimal
Ten(10);
196 EXPECT_EQ(Infinity
, Infinity
+ Infinity
);
197 EXPECT_EQ(NaN
, Infinity
+ MinusInfinity
);
198 EXPECT_EQ(NaN
, MinusInfinity
+ Infinity
);
199 EXPECT_EQ(MinusInfinity
, MinusInfinity
+ MinusInfinity
);
201 EXPECT_EQ(Infinity
, Infinity
+ Ten
);
202 EXPECT_EQ(Infinity
, Ten
+ Infinity
);
203 EXPECT_EQ(MinusInfinity
, MinusInfinity
+ Ten
);
204 EXPECT_EQ(MinusInfinity
, Ten
+ MinusInfinity
);
206 EXPECT_EQ(NaN
, NaN
+ NaN
);
207 EXPECT_EQ(NaN
, NaN
+ Ten
);
208 EXPECT_EQ(NaN
, Ten
+ NaN
);
210 EXPECT_EQ(NaN
, NaN
- Infinity
);
211 EXPECT_EQ(NaN
, NaN
- MinusInfinity
);
212 EXPECT_EQ(NaN
, Infinity
- NaN
);
213 EXPECT_EQ(NaN
, MinusInfinity
- NaN
);
216 TEST_F(DecimalTest
, Ceil
)
218 EXPECT_EQ(Decimal(1), Decimal(1).ceil());
219 EXPECT_EQ(Decimal(1), encode(1, -10, Positive
).ceil());
220 EXPECT_EQ(Decimal(2), encode(11, -1, Positive
).ceil());
221 EXPECT_EQ(Decimal(2), encode(13, -1, Positive
).ceil());
222 EXPECT_EQ(Decimal(2), encode(15, -1, Positive
).ceil());
223 EXPECT_EQ(Decimal(2), encode(19, -1, Positive
).ceil());
224 EXPECT_EQ(Decimal(2), encode(151, -2, Positive
).ceil());
225 EXPECT_EQ(Decimal(2), encode(101, -2, Positive
).ceil());
226 EXPECT_EQ(Decimal(1), encode(199, -3, Positive
).ceil());
227 EXPECT_EQ(Decimal(2), encode(199, -2, Positive
).ceil());
228 EXPECT_EQ(Decimal(3), encode(209, -2, Positive
).ceil());
230 EXPECT_EQ(Decimal(-1), Decimal(-1).ceil());
231 EXPECT_EQ(Decimal(0), encode(1, -10, Negative
).ceil());
232 EXPECT_EQ(Decimal(-1), encode(11, -1, Negative
).ceil());
233 EXPECT_EQ(Decimal(-1), encode(13, -1, Negative
).ceil());
234 EXPECT_EQ(Decimal(-1), encode(15, -1, Negative
).ceil());
235 EXPECT_EQ(Decimal(-1), encode(19, -1, Negative
).ceil());
236 EXPECT_EQ(Decimal(-1), encode(151, -2, Negative
).ceil());
237 EXPECT_EQ(Decimal(-1), encode(101, -2, Negative
).ceil());
238 EXPECT_EQ(Decimal(0), encode(199, -3, Negative
).ceil());
239 EXPECT_EQ(Decimal(-1), encode(199, -2, Negative
).ceil());
240 EXPECT_EQ(Decimal(-2), encode(209, -2, Negative
).ceil());
241 EXPECT_EQ(Decimal(1), encode(UINT64_C(123456789012345678), -18, Positive
).ceil());
244 TEST_F(DecimalTest
, CeilingBigExponent
)
246 EXPECT_EQ(encode(1, 1000, Positive
), encode(1, 1000, Positive
).ceil());
247 EXPECT_EQ(encode(1, 1000, Negative
), encode(1, 1000, Negative
).ceil());
250 TEST_F(DecimalTest
, CeilingSmallExponent
)
252 EXPECT_EQ(encode(1, 0, Positive
), encode(1, -1000, Positive
).ceil());
253 EXPECT_EQ(encode(0, 0, Negative
), encode(1, -1000, Negative
).ceil());
256 TEST_F(DecimalTest
, CeilingSpecialValues
)
258 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Positive
).ceil());
259 EXPECT_EQ(Decimal::infinity(Negative
), Decimal::infinity(Negative
).ceil());
260 EXPECT_EQ(Decimal::nan(), Decimal::nan().ceil());
263 TEST_F(DecimalTest
, Compare
)
265 EXPECT_TRUE(Decimal(0) == Decimal(0));
266 EXPECT_TRUE(Decimal(0) != Decimal(1));
267 EXPECT_TRUE(Decimal(0) < Decimal(1));
268 EXPECT_TRUE(Decimal(0) <= Decimal(0));
269 EXPECT_TRUE(Decimal(0) > Decimal(-1));
270 EXPECT_TRUE(Decimal(0) >= Decimal(0));
272 EXPECT_FALSE(Decimal(1) == Decimal(2));
273 EXPECT_FALSE(Decimal(1) != Decimal(1));
274 EXPECT_FALSE(Decimal(1) < Decimal(0));
275 EXPECT_FALSE(Decimal(1) <= Decimal(0));
276 EXPECT_FALSE(Decimal(1) > Decimal(2));
277 EXPECT_FALSE(Decimal(1) >= Decimal(2));
280 TEST_F(DecimalTest
, CompareBigExponent
)
282 EXPECT_TRUE(encode(1, 1000, Positive
) == encode(1, 1000, Positive
));
283 EXPECT_FALSE(encode(1, 1000, Positive
) != encode(1, 1000, Positive
));
284 EXPECT_FALSE(encode(1, 1000, Positive
) < encode(1, 1000, Positive
));
285 EXPECT_TRUE(encode(1, 1000, Positive
) <= encode(1, 1000, Positive
));
286 EXPECT_FALSE(encode(1, 1000, Positive
) > encode(1, 1000, Positive
));
287 EXPECT_TRUE(encode(1, 1000, Positive
) >= encode(1, 1000, Positive
));
289 EXPECT_TRUE(encode(1, 1000, Negative
) == encode(1, 1000, Negative
));
290 EXPECT_FALSE(encode(1, 1000, Negative
) != encode(1, 1000, Negative
));
291 EXPECT_FALSE(encode(1, 1000, Negative
) < encode(1, 1000, Negative
));
292 EXPECT_TRUE(encode(1, 1000, Negative
) <= encode(1, 1000, Negative
));
293 EXPECT_FALSE(encode(1, 1000, Negative
) > encode(1, 1000, Negative
));
294 EXPECT_TRUE(encode(1, 1000, Negative
) >= encode(1, 1000, Negative
));
296 EXPECT_FALSE(encode(2, 1000, Positive
) == encode(1, 1000, Positive
));
297 EXPECT_TRUE(encode(2, 1000, Positive
) != encode(1, 1000, Positive
));
298 EXPECT_FALSE(encode(2, 1000, Positive
) < encode(1, 1000, Positive
));
299 EXPECT_FALSE(encode(2, 1000, Positive
) <= encode(1, 1000, Positive
));
300 EXPECT_TRUE(encode(2, 1000, Positive
) > encode(1, 1000, Positive
));
301 EXPECT_TRUE(encode(2, 1000, Positive
) >= encode(1, 1000, Positive
));
303 EXPECT_FALSE(encode(2, 1000, Negative
) == encode(1, 1000, Negative
));
304 EXPECT_TRUE(encode(2, 1000, Negative
) != encode(1, 1000, Negative
));
305 EXPECT_TRUE(encode(2, 1000, Negative
) < encode(1, 1000, Negative
));
306 EXPECT_TRUE(encode(2, 1000, Negative
) <= encode(1, 1000, Negative
));
307 EXPECT_FALSE(encode(2, 1000, Negative
) > encode(1, 1000, Negative
));
308 EXPECT_FALSE(encode(2, 1000, Negative
) >= encode(1, 1000, Negative
));
311 TEST_F(DecimalTest
, CompareSmallExponent
)
313 EXPECT_TRUE(encode(1, -1000, Positive
) == encode(1, -1000, Positive
));
314 EXPECT_FALSE(encode(1, -1000, Positive
) != encode(1, -1000, Positive
));
315 EXPECT_FALSE(encode(1, -1000, Positive
) < encode(1, -1000, Positive
));
316 EXPECT_TRUE(encode(1, -1000, Positive
) <= encode(1, -1000, Positive
));
317 EXPECT_FALSE(encode(1, -1000, Positive
) > encode(1, -1000, Positive
));
318 EXPECT_TRUE(encode(1, -1000, Positive
) >= encode(1, -1000, Positive
));
320 EXPECT_TRUE(encode(1, -1000, Negative
) == encode(1, -1000, Negative
));
321 EXPECT_FALSE(encode(1, -1000, Negative
) != encode(1, -1000, Negative
));
322 EXPECT_FALSE(encode(1, -1000, Negative
) < encode(1, -1000, Negative
));
323 EXPECT_TRUE(encode(1, -1000, Negative
) <= encode(1, -1000, Negative
));
324 EXPECT_FALSE(encode(1, -1000, Negative
) > encode(1, -1000, Negative
));
325 EXPECT_TRUE(encode(1, -1000, Negative
) >= encode(1, -1000, Negative
));
327 EXPECT_FALSE(encode(2, -1000, Positive
) == encode(1, -1000, Positive
));
328 EXPECT_TRUE(encode(2, -1000, Positive
) != encode(1, -1000, Positive
));
329 EXPECT_FALSE(encode(2, -1000, Positive
) < encode(1, -1000, Positive
));
330 EXPECT_FALSE(encode(2, -1000, Positive
) <= encode(1, -1000, Positive
));
331 EXPECT_TRUE(encode(2, -1000, Positive
) > encode(1, -1000, Positive
));
332 EXPECT_TRUE(encode(2, -1000, Positive
) >= encode(1, -1000, Positive
));
334 EXPECT_FALSE(encode(2, -1000, Negative
) == encode(1, -1000, Negative
));
335 EXPECT_TRUE(encode(2, -1000, Negative
) != encode(1, -1000, Negative
));
336 EXPECT_TRUE(encode(2, -1000, Negative
) < encode(1, -1000, Negative
));
337 EXPECT_TRUE(encode(2, -1000, Negative
) <= encode(1, -1000, Negative
));
338 EXPECT_FALSE(encode(2, -1000, Negative
) > encode(1, -1000, Negative
));
339 EXPECT_FALSE(encode(2, -1000, Negative
) >= encode(1, -1000, Negative
));
342 TEST_F(DecimalTest
, CompareSpecialValues
)
344 const Decimal
Infinity(Decimal::infinity(Positive
));
345 const Decimal
MinusInfinity(Decimal::infinity(Negative
));
346 const Decimal
NaN(Decimal::nan());
347 const Decimal
Zero(Decimal::zero(Positive
));
348 const Decimal
MinusZero(Decimal::zero(Negative
));
349 const Decimal
Ten(10);
351 EXPECT_TRUE(Zero
== Zero
);
352 EXPECT_FALSE(Zero
!= Zero
);
353 EXPECT_FALSE(Zero
< Zero
);
354 EXPECT_TRUE(Zero
<= Zero
);
355 EXPECT_FALSE(Zero
> Zero
);
356 EXPECT_TRUE(Zero
>= Zero
);
358 EXPECT_TRUE(Zero
== MinusZero
);
359 EXPECT_FALSE(Zero
!= MinusZero
);
360 EXPECT_FALSE(Zero
< MinusZero
);
361 EXPECT_TRUE(Zero
<= MinusZero
);
362 EXPECT_FALSE(Zero
> MinusZero
);
363 EXPECT_TRUE(Zero
>= MinusZero
);
365 EXPECT_TRUE(MinusZero
== Zero
);
366 EXPECT_FALSE(MinusZero
!= Zero
);
367 EXPECT_FALSE(MinusZero
< Zero
);
368 EXPECT_TRUE(MinusZero
<= Zero
);
369 EXPECT_FALSE(MinusZero
> Zero
);
370 EXPECT_TRUE(MinusZero
>= Zero
);
372 EXPECT_TRUE(MinusZero
== MinusZero
);
373 EXPECT_FALSE(MinusZero
!= MinusZero
);
374 EXPECT_FALSE(MinusZero
< MinusZero
);
375 EXPECT_TRUE(MinusZero
<= MinusZero
);
376 EXPECT_FALSE(MinusZero
> MinusZero
);
377 EXPECT_TRUE(MinusZero
>= MinusZero
);
379 EXPECT_TRUE(Infinity
== Infinity
);
380 EXPECT_FALSE(Infinity
!= Infinity
);
381 EXPECT_FALSE(Infinity
< Infinity
);
382 EXPECT_TRUE(Infinity
<= Infinity
);
383 EXPECT_FALSE(Infinity
> Infinity
);
384 EXPECT_TRUE(Infinity
>= Infinity
);
386 EXPECT_FALSE(Infinity
== Ten
);
387 EXPECT_TRUE(Infinity
!= Ten
);
388 EXPECT_FALSE(Infinity
< Ten
);
389 EXPECT_FALSE(Infinity
<= Ten
);
390 EXPECT_TRUE(Infinity
> Ten
);
391 EXPECT_TRUE(Infinity
>= Ten
);
393 EXPECT_FALSE(Infinity
== MinusInfinity
);
394 EXPECT_TRUE(Infinity
!= MinusInfinity
);
395 EXPECT_FALSE(Infinity
< MinusInfinity
);
396 EXPECT_FALSE(Infinity
<= MinusInfinity
);
397 EXPECT_TRUE(Infinity
> MinusInfinity
);
398 EXPECT_TRUE(Infinity
>= MinusInfinity
);
400 EXPECT_FALSE(Infinity
== NaN
);
401 EXPECT_FALSE(Infinity
!= NaN
);
402 EXPECT_FALSE(Infinity
< NaN
);
403 EXPECT_FALSE(Infinity
<= NaN
);
404 EXPECT_FALSE(Infinity
> NaN
);
405 EXPECT_FALSE(Infinity
>= NaN
);
407 EXPECT_FALSE(MinusInfinity
== Infinity
);
408 EXPECT_TRUE(MinusInfinity
!= Infinity
);
409 EXPECT_TRUE(MinusInfinity
< Infinity
);
410 EXPECT_TRUE(MinusInfinity
<= Infinity
);
411 EXPECT_FALSE(MinusInfinity
> Infinity
);
412 EXPECT_FALSE(MinusInfinity
>= Infinity
);
414 EXPECT_FALSE(MinusInfinity
== Ten
);
415 EXPECT_TRUE(MinusInfinity
!= Ten
);
416 EXPECT_TRUE(MinusInfinity
< Ten
);
417 EXPECT_TRUE(MinusInfinity
<= Ten
);
418 EXPECT_FALSE(MinusInfinity
> Ten
);
419 EXPECT_FALSE(MinusInfinity
>= Ten
);
421 EXPECT_TRUE(MinusInfinity
== MinusInfinity
);
422 EXPECT_FALSE(MinusInfinity
!= MinusInfinity
);
423 EXPECT_FALSE(MinusInfinity
< MinusInfinity
);
424 EXPECT_TRUE(MinusInfinity
<= MinusInfinity
);
425 EXPECT_FALSE(MinusInfinity
> MinusInfinity
);
426 EXPECT_TRUE(MinusInfinity
>= MinusInfinity
);
428 EXPECT_FALSE(MinusInfinity
== NaN
);
429 EXPECT_FALSE(MinusInfinity
!= NaN
);
430 EXPECT_FALSE(MinusInfinity
< NaN
);
431 EXPECT_FALSE(MinusInfinity
<= NaN
);
432 EXPECT_FALSE(MinusInfinity
> NaN
);
433 EXPECT_FALSE(MinusInfinity
>= NaN
);
435 EXPECT_FALSE(NaN
== Infinity
);
436 EXPECT_FALSE(NaN
!= Infinity
);
437 EXPECT_FALSE(NaN
< Infinity
);
438 EXPECT_FALSE(NaN
<= Infinity
);
439 EXPECT_FALSE(NaN
> Infinity
);
440 EXPECT_FALSE(NaN
>= Infinity
);
442 EXPECT_FALSE(NaN
== Ten
);
443 EXPECT_FALSE(NaN
!= Ten
);
444 EXPECT_FALSE(NaN
< Ten
);
445 EXPECT_FALSE(NaN
<= Ten
);
446 EXPECT_FALSE(NaN
> Ten
);
447 EXPECT_FALSE(NaN
>= Ten
);
449 EXPECT_FALSE(NaN
== MinusInfinity
);
450 EXPECT_FALSE(NaN
!= MinusInfinity
);
451 EXPECT_FALSE(NaN
< MinusInfinity
);
452 EXPECT_FALSE(NaN
<= MinusInfinity
);
453 EXPECT_FALSE(NaN
> MinusInfinity
);
454 EXPECT_FALSE(NaN
>= MinusInfinity
);
456 EXPECT_TRUE(NaN
== NaN
);
457 EXPECT_FALSE(NaN
!= NaN
);
458 EXPECT_FALSE(NaN
< NaN
);
459 EXPECT_TRUE(NaN
<= NaN
);
460 EXPECT_FALSE(NaN
> NaN
);
461 EXPECT_TRUE(NaN
>= NaN
);
464 TEST_F(DecimalTest
, Constructor
)
466 EXPECT_DECIMAL_ENCODED_DATA_EQ(0u, 0, Positive
, encode(0, 0, Positive
));
467 EXPECT_DECIMAL_ENCODED_DATA_EQ(0u, 0, Negative
, encode(0, 0, Negative
));
468 EXPECT_DECIMAL_ENCODED_DATA_EQ(1u, 0, Positive
, encode(1, 0, Positive
));
469 EXPECT_DECIMAL_ENCODED_DATA_EQ(1u, 0, Negative
, encode(1, 0, Negative
));
470 EXPECT_DECIMAL_ENCODED_DATA_EQ(1u, 1022, Positive
, encode(1, 1022, Positive
));
471 EXPECT_DECIMAL_ENCODED_DATA_EQ(1u, 1022, Negative
, encode(1, 1022, Negative
));
472 EXPECT_DECIMAL_ENCODED_DATA_EQ(1u, 1023, Positive
, encode(1, 1023, Positive
));
473 EXPECT_DECIMAL_ENCODED_DATA_EQ(1u, 1023, Negative
, encode(1, 1023, Negative
));
474 EXPECT_TRUE(encode(1, 2000, Positive
).isInfinity());
475 EXPECT_TRUE(encode(1, 2000, Negative
).isInfinity());
476 EXPECT_DECIMAL_ENCODED_DATA_EQ(0u, 0, Positive
, encode(1, -2000, Positive
));
477 EXPECT_DECIMAL_ENCODED_DATA_EQ(0u, 0, Negative
, encode(1, -2000, Negative
));
478 EXPECT_DECIMAL_ENCODED_DATA_EQ(UINT64_C(99999999999999998), 0, Positive
, encode(UINT64_C(99999999999999998), 0, Positive
));
479 EXPECT_DECIMAL_ENCODED_DATA_EQ(UINT64_C(99999999999999998), 0, Negative
, encode(UINT64_C(99999999999999998), 0, Negative
));
480 EXPECT_DECIMAL_ENCODED_DATA_EQ(UINT64_C(99999999999999999), 0, Positive
, encode(UINT64_C(99999999999999999), 0, Positive
));
481 EXPECT_DECIMAL_ENCODED_DATA_EQ(UINT64_C(99999999999999999), 0, Negative
, encode(UINT64_C(99999999999999999), 0, Negative
));
482 EXPECT_DECIMAL_ENCODED_DATA_EQ(UINT64_C(100000000000000000), 0, Positive
, encode(UINT64_C(100000000000000000), 0, Positive
));
483 EXPECT_DECIMAL_ENCODED_DATA_EQ(UINT64_C(100000000000000000), 0, Negative
, encode(UINT64_C(100000000000000000), 0, Negative
));
486 TEST_F(DecimalTest
, Division
)
488 EXPECT_EQ(encode(0, 0, Positive
), Decimal(0) / Decimal(1));
489 EXPECT_EQ(encode(2, 0, Negative
), Decimal(2) / Decimal(-1));
490 EXPECT_EQ(encode(5, -1, Negative
), Decimal(-1) / Decimal(2));
491 EXPECT_EQ(encode(99, 0, Positive
), Decimal(99) / Decimal(1));
492 EXPECT_EQ(Decimal(1), Decimal(-50) / Decimal(-50));
493 EXPECT_EQ(encode(UINT64_C(333333333333333333), -18, Positive
), Decimal(1) / Decimal(3));
494 EXPECT_EQ(encode(UINT64_C(12345678901234), -1, Positive
), encode(UINT64_C(12345678901234), 0, Positive
) / Decimal(10));
495 EXPECT_EQ(encode(UINT64_C(500005000050000500), -18, Positive
), Decimal(50000) / Decimal(99999));
498 TEST_F(DecimalTest
, DivisionBigExponent
)
500 EXPECT_EQ(encode(1, 1022, Positive
), encode(1, 1022, Positive
) / encode(1, 0, Positive
));
501 EXPECT_EQ(encode(1, 0, Positive
), encode(1, 1022, Positive
) / encode(1, 1022, Positive
));
502 EXPECT_EQ(Decimal::infinity(Positive
), encode(1, 1022, Positive
) / encode(1, -1000, Positive
));
505 TEST_F(DecimalTest
, DivisionSmallExponent
)
507 EXPECT_EQ(encode(1, -1022, Positive
), encode(1, -1022, Positive
) / encode(1, 0, Positive
));
508 EXPECT_EQ(encode(1, 0, Positive
), encode(1, -1022, Positive
) / encode(1, -1022, Positive
));
511 TEST_F(DecimalTest
, DivisionSpecialValues
)
513 const Decimal
Infinity(Decimal::infinity(Positive
));
514 const Decimal
MinusInfinity(Decimal::infinity(Negative
));
515 const Decimal
NaN(Decimal::nan());
516 const Decimal
Zero(Decimal::zero(Positive
));
517 const Decimal
MinusZero(Decimal::zero(Negative
));
518 const Decimal
Ten(10);
519 const Decimal
MinusTen(-10);
521 EXPECT_EQ(NaN
, Zero
/ Zero
);
522 EXPECT_EQ(NaN
, Zero
/ MinusZero
);
523 EXPECT_EQ(NaN
, MinusZero
/ Zero
);
524 EXPECT_EQ(NaN
, MinusZero
/ MinusZero
);
526 EXPECT_EQ(Infinity
, Ten
/ Zero
);
527 EXPECT_EQ(MinusInfinity
, Ten
/ MinusZero
);
528 EXPECT_EQ(MinusInfinity
, MinusTen
/ Zero
);
529 EXPECT_EQ(Infinity
, MinusTen
/ MinusZero
);
531 EXPECT_EQ(Infinity
, Infinity
/ Zero
);
532 EXPECT_EQ(MinusInfinity
, Infinity
/ MinusZero
);
533 EXPECT_EQ(MinusInfinity
, MinusInfinity
/ Zero
);
534 EXPECT_EQ(Infinity
, MinusInfinity
/ MinusZero
);
536 EXPECT_EQ(NaN
, Infinity
/ Infinity
);
537 EXPECT_EQ(NaN
, Infinity
/ MinusInfinity
);
538 EXPECT_EQ(NaN
, MinusInfinity
/ Infinity
);
539 EXPECT_EQ(NaN
, MinusInfinity
/ MinusInfinity
);
541 EXPECT_EQ(Zero
, Ten
/ Infinity
);
542 EXPECT_EQ(MinusZero
, Ten
/ MinusInfinity
);
543 EXPECT_EQ(MinusZero
, MinusTen
/ Infinity
);
544 EXPECT_EQ(Zero
, MinusTen
/ MinusInfinity
);
546 EXPECT_EQ(NaN
, NaN
/ NaN
);
547 EXPECT_EQ(NaN
, NaN
/ Ten
);
548 EXPECT_EQ(NaN
, Ten
/ NaN
);
550 EXPECT_EQ(NaN
, NaN
/ Infinity
);
551 EXPECT_EQ(NaN
, NaN
/ MinusInfinity
);
552 EXPECT_EQ(NaN
, Infinity
/ NaN
);
553 EXPECT_EQ(NaN
, MinusInfinity
/ NaN
);
556 TEST_F(DecimalTest
, EncodedData
)
558 EXPECT_EQ(encode(0, 0, Positive
), encode(0, 0, Positive
));
559 EXPECT_EQ(encode(0, 0, Negative
), encode(0, 0, Negative
));
560 EXPECT_EQ(Decimal(1), Decimal(1));
561 EXPECT_EQ(encode(1, 0, Negative
), encode(1, 0, Negative
));
562 EXPECT_EQ(Decimal::infinity(Positive
), encode(1, 2000, Positive
));
563 EXPECT_EQ(Decimal::zero(Positive
), encode(1, -2000, Positive
));
566 TEST_F(DecimalTest
, Floor
)
568 EXPECT_EQ(Decimal(1), Decimal(1).floor());
569 EXPECT_EQ(Decimal(0), encode(1, -10, Positive
).floor());
570 EXPECT_EQ(Decimal(1), encode(11, -1, Positive
).floor());
571 EXPECT_EQ(Decimal(1), encode(13, -1, Positive
).floor());
572 EXPECT_EQ(Decimal(1), encode(15, -1, Positive
).floor());
573 EXPECT_EQ(Decimal(1), encode(19, -1, Positive
).floor());
574 EXPECT_EQ(Decimal(1), encode(193332, -5, Positive
).floor());
575 EXPECT_EQ(Decimal(12), encode(12002, -3, Positive
).floor());
577 EXPECT_EQ(Decimal(-1), Decimal(-1).floor());
578 EXPECT_EQ(Decimal(-1), encode(1, -10, Negative
).floor());
579 EXPECT_EQ(Decimal(-2), encode(11, -1, Negative
).floor());
580 EXPECT_EQ(Decimal(-2), encode(13, -1, Negative
).floor());
581 EXPECT_EQ(Decimal(-2), encode(15, -1, Negative
).floor());
582 EXPECT_EQ(Decimal(-2), encode(19, -1, Negative
).floor());
583 EXPECT_EQ(Decimal(-2), encode(193332, -5, Negative
).floor());
584 EXPECT_EQ(Decimal(-13), encode(12002, -3, Negative
).floor());
587 TEST_F(DecimalTest
, FloorBigExponent
)
589 EXPECT_EQ(encode(1, 1000, Positive
), encode(1, 1000, Positive
).floor());
590 EXPECT_EQ(encode(1, 1000, Negative
), encode(1, 1000, Negative
).floor());
593 TEST_F(DecimalTest
, FloorSmallExponent
)
595 EXPECT_EQ(encode(0, 0, Positive
), encode(1, -1000, Positive
).floor());
596 EXPECT_EQ(encode(1, 0, Negative
), encode(1, -1000, Negative
).floor());
599 TEST_F(DecimalTest
, FloorSpecialValues
)
601 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Positive
).floor());
602 EXPECT_EQ(Decimal::infinity(Negative
), Decimal::infinity(Negative
).floor());
603 EXPECT_EQ(Decimal::nan(), Decimal::nan().floor());
606 TEST_F(DecimalTest
, FromDouble
)
608 EXPECT_EQ(encode(0, 0, Positive
), Decimal::fromDouble(0.0));
609 EXPECT_EQ(encode(0, 0, Negative
), Decimal::fromDouble(-0.0));
610 EXPECT_EQ(encode(1, 0, Positive
), Decimal::fromDouble(1));
611 EXPECT_EQ(encode(1, 0, Negative
), Decimal::fromDouble(-1));
612 EXPECT_EQ(encode(123, 0, Positive
), Decimal::fromDouble(123));
613 EXPECT_EQ(encode(123, 0, Negative
), Decimal::fromDouble(-123));
614 EXPECT_EQ(encode(1, -1, Positive
), Decimal::fromDouble(0.1));
615 EXPECT_EQ(encode(1, -1, Negative
), Decimal::fromDouble(-0.1));
618 TEST_F(DecimalTest
, FromDoubleLimits
)
620 EXPECT_EQ(encode(UINT64_C(2220446049250313), -31, Positive
), Decimal::fromDouble(std::numeric_limits
<double>::epsilon()));
621 EXPECT_EQ(encode(UINT64_C(2220446049250313), -31, Negative
), Decimal::fromDouble(-std::numeric_limits
<double>::epsilon()));
622 EXPECT_EQ(encode(UINT64_C(17976931348623157), 292, Positive
), Decimal::fromDouble(std::numeric_limits
<double>::max()));
623 EXPECT_EQ(encode(UINT64_C(17976931348623157), 292, Negative
), Decimal::fromDouble(-std::numeric_limits
<double>::max()));
624 EXPECT_EQ(encode(UINT64_C(22250738585072014), -324, Positive
), Decimal::fromDouble(std::numeric_limits
<double>::min()));
625 EXPECT_EQ(encode(UINT64_C(22250738585072014), -324, Negative
), Decimal::fromDouble(-std::numeric_limits
<double>::min()));
626 EXPECT_TRUE(Decimal::fromDouble(std::numeric_limits
<double>::infinity()).isInfinity());
627 EXPECT_TRUE(Decimal::fromDouble(-std::numeric_limits
<double>::infinity()).isInfinity());
628 EXPECT_TRUE(Decimal::fromDouble(std::numeric_limits
<double>::quiet_NaN()).isNaN());
629 EXPECT_TRUE(Decimal::fromDouble(-std::numeric_limits
<double>::quiet_NaN()).isNaN());
632 TEST_F(DecimalTest
, FromInt32
)
634 EXPECT_EQ(encode(0, 0, Positive
), Decimal(0));
635 EXPECT_EQ(encode(1, 0, Positive
), Decimal(1));
636 EXPECT_EQ(encode(1, 0, Negative
), Decimal(-1));
637 EXPECT_EQ(encode(100, 0, Positive
), Decimal(100));
638 EXPECT_EQ(encode(100, 0, Negative
), Decimal(-100));
639 EXPECT_EQ(encode(0x7FFFFFFF, 0, Positive
), Decimal(std::numeric_limits
<int32_t>::max()));
640 EXPECT_EQ(encode(0x80000000u
, 0, Negative
), Decimal(std::numeric_limits
<int32_t>::min()));
643 TEST_F(DecimalTest
, FromString
)
645 EXPECT_EQ(encode(0, 0, Positive
), fromString("0"));
646 EXPECT_EQ(encode(0, 0, Negative
), fromString("-0"));
647 EXPECT_EQ(Decimal(1), fromString("1"));
648 EXPECT_EQ(encode(1, 0, Negative
), fromString("-1"));
649 EXPECT_EQ(Decimal(1), fromString("01"));
650 EXPECT_EQ(encode(3, 0, Positive
), fromString("+3"));
651 EXPECT_EQ(encode(0, 3, Positive
), fromString("0E3"));
652 EXPECT_EQ(encode(5, -1, Positive
), fromString(".5"));
653 EXPECT_EQ(encode(100, 0, Positive
), fromString("100"));
654 EXPECT_EQ(encode(100, 0, Negative
), fromString("-100"));
655 EXPECT_EQ(encode(123, -2, Positive
), fromString("1.23"));
656 EXPECT_EQ(encode(123, -2, Negative
), fromString("-1.23"));
657 EXPECT_EQ(encode(123, 8, Positive
), fromString("1.23E10"));
658 EXPECT_EQ(encode(123, 8, Negative
), fromString("-1.23E10"));
659 EXPECT_EQ(encode(123, 8, Positive
), fromString("1.23E+10"));
660 EXPECT_EQ(encode(123, 8, Negative
), fromString("-1.23E+10"));
661 EXPECT_EQ(encode(123, -12, Positive
), fromString("1.23E-10"));
662 EXPECT_EQ(encode(123, -12, Negative
), fromString("-1.23E-10"));
663 EXPECT_EQ(encode(5, -7, Positive
), fromString("0.0000005"));
664 EXPECT_EQ(encode(0, 0, Positive
), fromString("0e9999"));
665 EXPECT_EQ(encode(123, -3, Positive
), fromString("0.123"));
666 EXPECT_EQ(encode(0, -2, Positive
), fromString("00.00"));
667 EXPECT_EQ(encode(1, 2, Positive
), fromString("1E2"));
668 EXPECT_EQ(Decimal::infinity(Positive
), fromString("1E20000"));
669 EXPECT_EQ(Decimal::zero(Positive
), fromString("1E-20000"));
670 EXPECT_EQ(encode(1000, 1023, Positive
), fromString("1E1026"));
671 EXPECT_EQ(Decimal::zero(Positive
), fromString("1E-1026"));
672 EXPECT_EQ(Decimal::infinity(Positive
), fromString("1234567890E1036"));
675 const uint64_t leadingDigitsOf2PowerOf1024
= UINT64_C(17976931348623159);
676 EXPECT_EQ(encode(leadingDigitsOf2PowerOf1024
, 292, Positive
), fromString("179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216"));
679 // These strings are look like proper number, but we don't accept them.
680 TEST_F(DecimalTest
, FromStringLikeNumber
)
682 EXPECT_EQ(Decimal::nan(), fromString(" 123 "));
683 EXPECT_EQ(Decimal::nan(), fromString("1,234"));
686 // fromString doesn't support infinity and NaN.
687 TEST_F(DecimalTest
, FromStringSpecialValues
)
689 EXPECT_EQ(Decimal::nan(), fromString("INF"));
690 EXPECT_EQ(Decimal::nan(), fromString("Infinity"));
691 EXPECT_EQ(Decimal::nan(), fromString("infinity"));
692 EXPECT_EQ(Decimal::nan(), fromString("+Infinity"));
693 EXPECT_EQ(Decimal::nan(), fromString("+infinity"));
694 EXPECT_EQ(Decimal::nan(), fromString("-Infinity"));
695 EXPECT_EQ(Decimal::nan(), fromString("-infinity"));
696 EXPECT_EQ(Decimal::nan(), fromString("NaN"));
697 EXPECT_EQ(Decimal::nan(), fromString("nan"));
698 EXPECT_EQ(Decimal::nan(), fromString("+NaN"));
699 EXPECT_EQ(Decimal::nan(), fromString("+nan"));
700 EXPECT_EQ(Decimal::nan(), fromString("-NaN"));
701 EXPECT_EQ(Decimal::nan(), fromString("-nan"));
704 TEST_F(DecimalTest
, fromStringTruncated
)
706 EXPECT_EQ(Decimal::nan(), fromString("x"));
707 EXPECT_EQ(Decimal::nan(), fromString("0."));
708 EXPECT_EQ(Decimal::nan(), fromString("1x"));
710 EXPECT_EQ(Decimal::nan(), fromString("1Ex"));
711 EXPECT_EQ(Decimal::nan(), fromString("1E2x"));
712 EXPECT_EQ(Decimal::nan(), fromString("1E+x"));
715 TEST_F(DecimalTest
, Multiplication
)
717 EXPECT_EQ(encode(0, 0, Positive
), Decimal(0) * Decimal(0));
718 EXPECT_EQ(encode(2, 0, Negative
), Decimal(2) * Decimal(-1));
719 EXPECT_EQ(encode(2, 0, Negative
), Decimal(-1) * Decimal(2));
720 EXPECT_EQ(encode(99, 0, Positive
), Decimal(99) * Decimal(1));
721 EXPECT_EQ(encode(2500, 0, Positive
), Decimal(-50) * Decimal(-50));
722 EXPECT_EQ(encode(1, 21, Positive
), encode(UINT64_C(10000000000), 0, Positive
) * encode(UINT64_C(100000000000), 0, Positive
));
725 TEST_F(DecimalTest
, MultiplicationBigExponent
)
727 EXPECT_EQ(encode(1, 1022, Positive
), encode(1, 1022, Positive
) * encode(1, 0, Positive
));
728 EXPECT_EQ(Decimal::infinity(Positive
), encode(1, 1022, Positive
) * encode(1, 1022, Positive
));
729 EXPECT_EQ(encode(1, 22, Positive
), encode(1, 1022, Positive
) * encode(1, -1000, Positive
));
732 TEST_F(DecimalTest
, MultiplicationSmallExponent
)
734 EXPECT_EQ(encode(1, -1022, Positive
), encode(1, -1022, Positive
) * encode(1, 0, Positive
));
735 EXPECT_EQ(encode(0, 0, Positive
), encode(1, -1022, Positive
) * encode(1, -1022, Positive
));
738 TEST_F(DecimalTest
, MultiplicationSpecialValues
)
740 const Decimal
Infinity(Decimal::infinity(Positive
));
741 const Decimal
MinusInfinity(Decimal::infinity(Negative
));
742 const Decimal
NaN(Decimal::nan());
743 const Decimal
Ten(10);
744 const Decimal
MinusTen(-10);
745 const Decimal
Zero(Decimal::zero(Positive
));
746 const Decimal
MinusZero(Decimal::zero(Negative
));
748 EXPECT_EQ(Infinity
, Infinity
* Infinity
);
749 EXPECT_EQ(MinusInfinity
, Infinity
* MinusInfinity
);
750 EXPECT_EQ(MinusInfinity
, MinusInfinity
* Infinity
);
751 EXPECT_EQ(Infinity
, MinusInfinity
* MinusInfinity
);
753 EXPECT_EQ(NaN
, Infinity
* Zero
);
754 EXPECT_EQ(NaN
, Zero
* MinusInfinity
);
755 EXPECT_EQ(NaN
, MinusInfinity
* Zero
);
756 EXPECT_EQ(NaN
, MinusInfinity
* Zero
);
758 EXPECT_EQ(NaN
, Infinity
* MinusZero
);
759 EXPECT_EQ(NaN
, MinusZero
* MinusInfinity
);
760 EXPECT_EQ(NaN
, MinusInfinity
* MinusZero
);
761 EXPECT_EQ(NaN
, MinusInfinity
* MinusZero
);
763 EXPECT_EQ(Infinity
, Infinity
* Ten
);
764 EXPECT_EQ(Infinity
, Ten
* Infinity
);
765 EXPECT_EQ(MinusInfinity
, MinusInfinity
* Ten
);
766 EXPECT_EQ(MinusInfinity
, Ten
* MinusInfinity
);
768 EXPECT_EQ(MinusInfinity
, Infinity
* MinusTen
);
769 EXPECT_EQ(MinusInfinity
, MinusTen
* Infinity
);
770 EXPECT_EQ(Infinity
, MinusInfinity
* MinusTen
);
771 EXPECT_EQ(Infinity
, MinusTen
* MinusInfinity
);
773 EXPECT_EQ(NaN
, NaN
* NaN
);
774 EXPECT_EQ(NaN
, NaN
* Ten
);
775 EXPECT_EQ(NaN
, Ten
* NaN
);
777 EXPECT_EQ(NaN
, NaN
* Infinity
);
778 EXPECT_EQ(NaN
, NaN
* MinusInfinity
);
779 EXPECT_EQ(NaN
, Infinity
* NaN
);
780 EXPECT_EQ(NaN
, MinusInfinity
* NaN
);
783 TEST_F(DecimalTest
, Negate
)
785 EXPECT_EQ(encode(0, 0, Negative
), -encode(0, 0, Positive
));
786 EXPECT_EQ(encode(0, 0, Positive
), -encode(0, 0, Negative
));
788 EXPECT_EQ(encode(0, 10, Negative
), -encode(0, 10, Positive
));
789 EXPECT_EQ(encode(0, 10, Positive
), -encode(0, 10, Negative
));
791 EXPECT_EQ(encode(0, -10, Negative
), -encode(0, -10, Positive
));
792 EXPECT_EQ(encode(0, -10, Positive
), -encode(0, -10, Negative
));
794 EXPECT_EQ(encode(1, 0, Negative
), -encode(1, 0, Positive
));
795 EXPECT_EQ(encode(1, 0, Positive
), -encode(1, 0, Negative
));
797 EXPECT_EQ(encode(1, 10, Negative
), -encode(1, 10, Positive
));
798 EXPECT_EQ(encode(1, 10, Positive
), -encode(1, 10, Negative
));
800 EXPECT_EQ(encode(1, -10, Negative
), -encode(1, -10, Positive
));
801 EXPECT_EQ(encode(1, -10, Positive
), -encode(1, -10, Negative
));
804 TEST_F(DecimalTest
, NegateBigExponent
)
806 EXPECT_EQ(encode(1, 1000, Negative
), -encode(1, 1000, Positive
));
807 EXPECT_EQ(encode(1, 1000, Positive
), -encode(1, 1000, Negative
));
810 TEST_F(DecimalTest
, NegateSmallExponent
)
812 EXPECT_EQ(encode(1, -1000, Negative
), -encode(1, -1000, Positive
));
813 EXPECT_EQ(encode(1, -1000, Positive
), -encode(1, -1000, Negative
));
816 TEST_F(DecimalTest
, NegateSpecialValues
)
818 EXPECT_EQ(Decimal::infinity(Negative
), -Decimal::infinity(Positive
));
819 EXPECT_EQ(Decimal::infinity(Positive
), -Decimal::infinity(Negative
));
820 EXPECT_EQ(Decimal::nan(), -Decimal::nan());
823 TEST_F(DecimalTest
, Predicates
)
825 EXPECT_TRUE(Decimal::zero(Positive
).isFinite());
826 EXPECT_FALSE(Decimal::zero(Positive
).isInfinity());
827 EXPECT_FALSE(Decimal::zero(Positive
).isNaN());
828 EXPECT_TRUE(Decimal::zero(Positive
).isPositive());
829 EXPECT_FALSE(Decimal::zero(Positive
).isNegative());
830 EXPECT_FALSE(Decimal::zero(Positive
).isSpecial());
831 EXPECT_TRUE(Decimal::zero(Positive
).isZero());
833 EXPECT_TRUE(Decimal::zero(Negative
).isFinite());
834 EXPECT_FALSE(Decimal::zero(Negative
).isInfinity());
835 EXPECT_FALSE(Decimal::zero(Negative
).isNaN());
836 EXPECT_FALSE(Decimal::zero(Negative
).isPositive());
837 EXPECT_TRUE(Decimal::zero(Negative
).isNegative());
838 EXPECT_FALSE(Decimal::zero(Negative
).isSpecial());
839 EXPECT_TRUE(Decimal::zero(Negative
).isZero());
841 EXPECT_TRUE(Decimal(123).isFinite());
842 EXPECT_FALSE(Decimal(123).isInfinity());
843 EXPECT_FALSE(Decimal(123).isNaN());
844 EXPECT_TRUE(Decimal(123).isPositive());
845 EXPECT_FALSE(Decimal(123).isNegative());
846 EXPECT_FALSE(Decimal(123).isSpecial());
847 EXPECT_FALSE(Decimal(123).isZero());
849 EXPECT_TRUE(Decimal(-123).isFinite());
850 EXPECT_FALSE(Decimal(-123).isInfinity());
851 EXPECT_FALSE(Decimal(-123).isNaN());
852 EXPECT_FALSE(Decimal(-123).isPositive());
853 EXPECT_TRUE(Decimal(-123).isNegative());
854 EXPECT_FALSE(Decimal(-123).isSpecial());
855 EXPECT_FALSE(Decimal(-123).isZero());
858 TEST_F(DecimalTest
, PredicatesSpecialValues
)
860 EXPECT_FALSE(Decimal::infinity(Positive
).isFinite());
861 EXPECT_TRUE(Decimal::infinity(Positive
).isInfinity());
862 EXPECT_FALSE(Decimal::infinity(Positive
).isNaN());
863 EXPECT_TRUE(Decimal::infinity(Positive
).isPositive());
864 EXPECT_FALSE(Decimal::infinity(Positive
).isNegative());
865 EXPECT_TRUE(Decimal::infinity(Positive
).isSpecial());
866 EXPECT_FALSE(Decimal::infinity(Positive
).isZero());
868 EXPECT_FALSE(Decimal::infinity(Negative
).isFinite());
869 EXPECT_TRUE(Decimal::infinity(Negative
).isInfinity());
870 EXPECT_FALSE(Decimal::infinity(Negative
).isNaN());
871 EXPECT_FALSE(Decimal::infinity(Negative
).isPositive());
872 EXPECT_TRUE(Decimal::infinity(Negative
).isNegative());
873 EXPECT_TRUE(Decimal::infinity(Negative
).isSpecial());
874 EXPECT_FALSE(Decimal::infinity(Negative
).isZero());
876 EXPECT_FALSE(Decimal::nan().isFinite());
877 EXPECT_FALSE(Decimal::nan().isInfinity());
878 EXPECT_TRUE(Decimal::nan().isNaN());
879 EXPECT_TRUE(Decimal::nan().isSpecial());
880 EXPECT_FALSE(Decimal::nan().isZero());
883 // LayoutTests/fast/forms/number/number-stepup-stepdown-from-renderer
884 TEST_F(DecimalTest
, RealWorldExampleNumberStepUpStepDownFromRenderer
)
886 EXPECT_DECIMAL_STREQ("10", stepDown("0", "100", "10", "19", 1));
887 EXPECT_DECIMAL_STREQ("90", stepUp("0", "99", "10", "89", 1));
888 EXPECT_DECIMAL_STREQ("1", stepUp("0", "1", "0.33333333333333333", "0", 3)); // step=1/3
889 EXPECT_DECIMAL_STREQ("0.01", stepUp("0", "0.01", "0.0033333333333333333", "0", 3)); // step=1/300
890 EXPECT_DECIMAL_STREQ("1", stepUp("0", "1", "0.003921568627450980", "0", 255)); // step=1/255
891 EXPECT_DECIMAL_STREQ("1", stepUp("0", "1", "0.1", "0", 10));
894 TEST_F(DecimalTest
, RealWorldExampleNumberStepUpStepDownFromRendererRounding
)
896 EXPECT_DECIMAL_STREQ("5.015", stepUp("0", "100", "0.005", "5.005", 2));
897 EXPECT_DECIMAL_STREQ("5.06", stepUp("0", "100", "0.005", "5.005", 11));
898 EXPECT_DECIMAL_STREQ("5.065", stepUp("0", "100", "0.005", "5.005", 12));
900 EXPECT_DECIMAL_STREQ("5.015", stepUp("4", "9", "0.005", "5.005", 2));
901 EXPECT_DECIMAL_STREQ("5.06", stepUp("4", "9", "0.005", "5.005", 11));
902 EXPECT_DECIMAL_STREQ("5.065", stepUp("4", "9", "0.005", "5.005", 12));
905 TEST_F(DecimalTest
, RealWorldExampleRangeStepUpStepDown
)
907 EXPECT_DECIMAL_STREQ("1e+38", stepUp("0", "1E38", "1", "1E38", 9));
908 EXPECT_DECIMAL_STREQ("1e+38", stepDown("0", "1E38", "1", "1E38", 9));
911 TEST_F(DecimalTest
, Remainder
)
913 EXPECT_EQ(encode(21, -1, Positive
), encode(21, -1, Positive
).remainder(3));
914 EXPECT_EQ(Decimal(1), Decimal(10).remainder(3));
915 EXPECT_EQ(Decimal(1), Decimal(10).remainder(-3));
916 EXPECT_EQ(encode(1, 0, Negative
), Decimal(-10).remainder(3));
917 EXPECT_EQ(Decimal(-1), Decimal(-10).remainder(-3));
918 EXPECT_EQ(encode(2, -1, Positive
), encode(102, -1, Positive
).remainder(1));
919 EXPECT_EQ(encode(1, -1, Positive
), Decimal(10).remainder(encode(3, -1, Positive
)));
920 EXPECT_EQ(Decimal(1), encode(36, -1, Positive
).remainder(encode(13, -1, Positive
)));
921 EXPECT_EQ(encode(1, 86, Positive
), (encode(1234, 100, Positive
).remainder(Decimal(3))));
922 EXPECT_EQ(Decimal(500), (Decimal(500).remainder(1000)));
923 EXPECT_EQ(Decimal(-500), (Decimal(-500).remainder(1000)));
926 TEST_F(DecimalTest
, RemainderBigExponent
)
928 EXPECT_EQ(encode(0, 1022, Positive
), encode(1, 1022, Positive
).remainder(encode(1, 0, Positive
)));
929 EXPECT_EQ(encode(0, 1022, Positive
), encode(1, 1022, Positive
).remainder(encode(1, 1022, Positive
)));
930 EXPECT_EQ(Decimal::infinity(Positive
), encode(1, 1022, Positive
).remainder(encode(1, -1000, Positive
)));
933 TEST_F(DecimalTest
, RemainderSmallExponent
)
935 EXPECT_EQ(encode(1, -1022, Positive
), encode(1, -1022, Positive
).remainder(encode(1, 0, Positive
)));
936 EXPECT_EQ(encode(0, -1022, Positive
), encode(1, -1022, Positive
).remainder(encode(1, -1022, Positive
)));
939 TEST_F(DecimalTest
, RemainderSpecialValues
)
941 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Positive
).remainder(1));
942 EXPECT_EQ(Decimal::infinity(Negative
), Decimal::infinity(Negative
).remainder(1));
943 EXPECT_EQ(Decimal::nan(), Decimal::nan().remainder(1));
945 EXPECT_EQ(Decimal::infinity(Negative
), Decimal::infinity(Positive
).remainder(-1));
946 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Negative
).remainder(-1));
947 EXPECT_EQ(Decimal::nan(), Decimal::nan().remainder(-1));
949 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Positive
).remainder(3));
950 EXPECT_EQ(Decimal::infinity(Negative
), Decimal::infinity(Negative
).remainder(3));
951 EXPECT_EQ(Decimal::nan(), Decimal::nan().remainder(3));
953 EXPECT_EQ(Decimal::infinity(Negative
), Decimal::infinity(Positive
).remainder(-1));
954 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Negative
).remainder(-1));
955 EXPECT_EQ(Decimal::nan(), Decimal::nan().remainder(-1));
957 EXPECT_EQ(Decimal::nan(), Decimal(1).remainder(Decimal::infinity(Positive
)));
958 EXPECT_EQ(Decimal::nan(), Decimal(1).remainder(Decimal::infinity(Negative
)));
959 EXPECT_EQ(Decimal::nan(), Decimal(1).remainder(Decimal::nan()));
962 TEST_F(DecimalTest
, Round
)
964 EXPECT_EQ(Decimal(1), (Decimal(9) / Decimal(10)).round());
965 EXPECT_EQ(Decimal(25), (Decimal(5) / fromString("0.200")).round());
966 EXPECT_EQ(Decimal(3), (Decimal(5) / Decimal(2)).round());
967 EXPECT_EQ(Decimal(1), (Decimal(2) / Decimal(3)).round());
968 EXPECT_EQ(Decimal(3), (Decimal(10) / Decimal(3)).round());
969 EXPECT_EQ(Decimal(3), (Decimal(1) / fromString("0.3")).round());
970 EXPECT_EQ(Decimal(10), (Decimal(1) / fromString("0.1")).round());
971 EXPECT_EQ(Decimal(5), (Decimal(1) / fromString("0.2")).round());
972 EXPECT_EQ(Decimal(10), (fromString("10.2") / 1).round());
973 EXPECT_EQ(encode(1234, 100, Positive
), encode(1234, 100, Positive
).round());
975 EXPECT_EQ(Decimal(2), encode(190002, -5, Positive
).round());
976 EXPECT_EQ(Decimal(2), encode(150002, -5, Positive
).round());
977 EXPECT_EQ(Decimal(2), encode(150000, -5, Positive
).round());
978 EXPECT_EQ(Decimal(12), encode(12492, -3, Positive
).round());
979 EXPECT_EQ(Decimal(13), encode(12502, -3, Positive
).round());
981 EXPECT_EQ(Decimal(-2), encode(190002, -5, Negative
).round());
982 EXPECT_EQ(Decimal(-2), encode(150002, -5, Negative
).round());
983 EXPECT_EQ(Decimal(-2), encode(150000, -5, Negative
).round());
984 EXPECT_EQ(Decimal(-12), encode(12492, -3, Negative
).round());
985 EXPECT_EQ(Decimal(-13), encode(12502, -3, Negative
).round());
988 TEST_F(DecimalTest
, RoundSpecialValues
)
990 EXPECT_EQ(Decimal::infinity(Positive
), Decimal::infinity(Positive
).round());
991 EXPECT_EQ(Decimal::infinity(Negative
), Decimal::infinity(Negative
).round());
992 EXPECT_EQ(Decimal::nan(), Decimal::nan().round());
995 TEST_F(DecimalTest
, Subtract
)
997 EXPECT_EQ(encode(0, 0, Positive
), Decimal(0) - Decimal(0));
998 EXPECT_EQ(encode(3, 0, Positive
), Decimal(2) - Decimal(-1));
999 EXPECT_EQ(encode(3, 0, Negative
), Decimal(-1) - Decimal(2));
1000 EXPECT_EQ(encode(98, 0, Positive
), Decimal(99) - Decimal(1));
1001 EXPECT_EQ(encode(0, 0, Positive
), Decimal(-50) - Decimal(-50));
1002 EXPECT_EQ(encode(UINT64_C(1000000000000000), 35, Positive
), encode(1, 50, Positive
) - Decimal(1));
1003 EXPECT_EQ(encode(UINT64_C(1000000000000000), 35, Negative
), Decimal(1) - encode(1, 50, Positive
));
1006 TEST_F(DecimalTest
, SubtractBigExponent
)
1008 EXPECT_EQ(encode(1, 1022, Positive
), encode(1, 1022, Positive
) - encode(1, 0, Positive
));
1009 EXPECT_EQ(encode(0, 0, Positive
), encode(1, 1022, Positive
) - encode(1, 1022, Positive
));
1010 EXPECT_EQ(encode(1, 1022, Positive
), encode(1, 1022, Positive
) + encode(1, -1000, Positive
));
1013 TEST_F(DecimalTest
, SubtractSmallExponent
)
1015 EXPECT_EQ(encode(UINT64_C(10000000000000000), -16, Negative
), encode(1, -1022, Positive
) - encode(1, 0, Positive
));
1016 EXPECT_EQ(encode(0, 0, Positive
), encode(1, -1022, Positive
) - encode(1, -1022, Positive
));
1019 TEST_F(DecimalTest
, SubtractSpecialValues
)
1021 const Decimal
Infinity(Decimal::infinity(Positive
));
1022 const Decimal
MinusInfinity(Decimal::infinity(Negative
));
1023 const Decimal
NaN(Decimal::nan());
1024 const Decimal
Ten(10);
1026 EXPECT_EQ(NaN
, Infinity
- Infinity
);
1027 EXPECT_EQ(Infinity
, Infinity
- MinusInfinity
);
1028 EXPECT_EQ(MinusInfinity
, MinusInfinity
- Infinity
);
1029 EXPECT_EQ(NaN
, MinusInfinity
- MinusInfinity
);
1031 EXPECT_EQ(Infinity
, Infinity
- Ten
);
1032 EXPECT_EQ(MinusInfinity
, Ten
- Infinity
);
1033 EXPECT_EQ(MinusInfinity
, MinusInfinity
- Ten
);
1034 EXPECT_EQ(Infinity
, Ten
- MinusInfinity
);
1036 EXPECT_EQ(NaN
, NaN
- NaN
);
1037 EXPECT_EQ(NaN
, NaN
- Ten
);
1038 EXPECT_EQ(NaN
, Ten
- NaN
);
1040 EXPECT_EQ(NaN
, NaN
- Infinity
);
1041 EXPECT_EQ(NaN
, NaN
- MinusInfinity
);
1042 EXPECT_EQ(NaN
, Infinity
- NaN
);
1043 EXPECT_EQ(NaN
, MinusInfinity
- NaN
);
1046 TEST_F(DecimalTest
, ToDouble
)
1048 EXPECT_EQ(0.0, encode(0, 0, Positive
).toDouble());
1049 EXPECT_EQ(-0.0, encode(0, 0, Negative
).toDouble());
1051 EXPECT_EQ(1.0, encode(1, 0, Positive
).toDouble());
1052 EXPECT_EQ(-1.0, encode(1, 0, Negative
).toDouble());
1054 EXPECT_EQ(0.1, encode(1, -1, Positive
).toDouble());
1055 EXPECT_EQ(-0.1, encode(1, -1, Negative
).toDouble());
1056 EXPECT_EQ(0.3, encode(3, -1, Positive
).toDouble());
1057 EXPECT_EQ(-0.3, encode(3, -1, Negative
).toDouble());
1058 EXPECT_EQ(0.6, encode(6, -1, Positive
).toDouble());
1059 EXPECT_EQ(-0.6, encode(6, -1, Negative
).toDouble());
1060 EXPECT_EQ(0.7, encode(7, -1, Positive
).toDouble());
1061 EXPECT_EQ(-0.7, encode(7, -1, Negative
).toDouble());
1063 EXPECT_EQ(0.01, encode(1, -2, Positive
).toDouble());
1064 EXPECT_EQ(0.001, encode(1, -3, Positive
).toDouble());
1065 EXPECT_EQ(0.0001, encode(1, -4, Positive
).toDouble());
1066 EXPECT_EQ(0.00001, encode(1, -5, Positive
).toDouble());
1068 EXPECT_EQ(1e+308, encode(1, 308, Positive
).toDouble());
1069 EXPECT_EQ(1e-307, encode(1, -307, Positive
).toDouble());
1071 EXPECT_TRUE(std::isinf(encode(1, 1000, Positive
).toDouble()));
1072 EXPECT_EQ(0.0, encode(1, -1000, Positive
).toDouble());
1075 TEST_F(DecimalTest
, ToDoubleSpecialValues
)
1077 EXPECT_TRUE(std::isinf(Decimal::infinity(Decimal::Positive
).toDouble()));
1078 EXPECT_TRUE(std::isinf(Decimal::infinity(Decimal::Negative
).toDouble()));
1079 EXPECT_TRUE(std::isnan(Decimal::nan().toDouble()));
1082 TEST_F(DecimalTest
, ToString
)
1084 EXPECT_DECIMAL_STREQ("0", Decimal::zero(Positive
));
1085 EXPECT_DECIMAL_STREQ("-0", Decimal::zero(Negative
));
1086 EXPECT_DECIMAL_STREQ("1", Decimal(1));
1087 EXPECT_DECIMAL_STREQ("-1", Decimal(-1));
1088 EXPECT_DECIMAL_STREQ("1234567", Decimal(1234567));
1089 EXPECT_DECIMAL_STREQ("-1234567", Decimal(-1234567));
1090 EXPECT_DECIMAL_STREQ("0.5", encode(5, -1, Positive
));
1091 EXPECT_DECIMAL_STREQ("-0.5", encode(5, -1, Negative
));
1092 EXPECT_DECIMAL_STREQ("12.345", encode(12345, -3, Positive
));
1093 EXPECT_DECIMAL_STREQ("-12.345", encode(12345, -3, Negative
));
1094 EXPECT_DECIMAL_STREQ("0.12345", encode(12345, -5, Positive
));
1095 EXPECT_DECIMAL_STREQ("-0.12345", encode(12345, -5, Negative
));
1096 EXPECT_DECIMAL_STREQ("50", encode(50, 0, Positive
));
1097 EXPECT_DECIMAL_STREQ("-50", encode(50, 0, Negative
));
1098 EXPECT_DECIMAL_STREQ("5e+1", encode(5, 1, Positive
));
1099 EXPECT_DECIMAL_STREQ("-5e+1", encode(5, 1, Negative
));
1100 EXPECT_DECIMAL_STREQ("5.678e+103", encode(5678, 100, Positive
));
1101 EXPECT_DECIMAL_STREQ("-5.678e+103", encode(5678, 100, Negative
));
1102 EXPECT_DECIMAL_STREQ("5.678e-97", encode(5678, -100, Positive
));
1103 EXPECT_DECIMAL_STREQ("-5.678e-97", encode(5678, -100, Negative
));
1104 EXPECT_DECIMAL_STREQ("8639999913600001", encode(UINT64_C(8639999913600001), 0, Positive
));
1105 EXPECT_DECIMAL_STREQ("9007199254740991", encode((static_cast<uint64_t>(1) << DBL_MANT_DIG
) - 1, 0, Positive
));
1106 EXPECT_DECIMAL_STREQ("99999999999999999", encode(UINT64_C(99999999999999999), 0, Positive
));
1107 EXPECT_DECIMAL_STREQ("9.9999999999999999e+17", encode(UINT64_C(99999999999999999), 1, Positive
));
1108 EXPECT_DECIMAL_STREQ("9.9999999999999999e+18", encode(UINT64_C(99999999999999999), 2, Positive
));
1109 EXPECT_DECIMAL_STREQ("1e+16", encode(UINT64_C(99999999999999999), -1, Positive
));
1110 EXPECT_DECIMAL_STREQ("1000000000000000", encode(UINT64_C(99999999999999999), -2, Positive
));
1111 EXPECT_DECIMAL_STREQ("1", encode(UINT64_C(99999999999999999), -17, Positive
));
1112 EXPECT_DECIMAL_STREQ("0.001", encode(UINT64_C(99999999999999999), -20, Positive
));
1113 EXPECT_DECIMAL_STREQ("1e-83", encode(UINT64_C(99999999999999999), -100, Positive
));
1116 TEST_F(DecimalTest
, ToStringSpecialValues
)
1118 EXPECT_DECIMAL_STREQ("Infinity", Decimal::infinity(Positive
));
1119 EXPECT_DECIMAL_STREQ("-Infinity", Decimal::infinity(Negative
));
1120 EXPECT_DECIMAL_STREQ("NaN", Decimal::nan());
1123 } // namespace blink