1 // Copyright 2019 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "xfa/fgas/crt/cfgas_decimal.h"
6
7 #include <limits>
8
9 #include "testing/gtest/include/gtest/gtest.h"
10
TEST(CFGAS_Decimal,Empty)11 TEST(CFGAS_Decimal, Empty) {
12 CFGAS_Decimal empty;
13 EXPECT_EQ(L"0", empty.ToWideString());
14 EXPECT_EQ(0.0f, empty.ToFloat());
15 EXPECT_EQ(0.0, empty.ToDouble());
16 }
17
TEST(CFGAS_Decimal,FromInt32)18 TEST(CFGAS_Decimal, FromInt32) {
19 CFGAS_Decimal big(std::numeric_limits<int32_t>::max());
20 CFGAS_Decimal small(std::numeric_limits<int32_t>::min());
21 EXPECT_STREQ(L"2147483647", big.ToWideString().c_str());
22 EXPECT_STREQ(L"-2147483648", small.ToWideString().c_str());
23 }
24
TEST(CFGAS_Decimal,FromUint32)25 TEST(CFGAS_Decimal, FromUint32) {
26 CFGAS_Decimal big(std::numeric_limits<uint32_t>::max());
27 CFGAS_Decimal small(std::numeric_limits<uint32_t>::min());
28 EXPECT_STREQ(L"4294967295", big.ToWideString().c_str());
29 EXPECT_STREQ(L"0", small.ToWideString().c_str());
30 }
31
TEST(CFGAS_Decimal,FromUint64)32 TEST(CFGAS_Decimal, FromUint64) {
33 CFGAS_Decimal big(std::numeric_limits<uint64_t>::max());
34 CFGAS_Decimal small(std::numeric_limits<uint64_t>::min());
35 EXPECT_STREQ(L"18446744073709551615", big.ToWideString().c_str());
36 EXPECT_STREQ(L"0", small.ToWideString().c_str());
37 }
38
TEST(CFGAS_Decimal,FromFloat)39 TEST(CFGAS_Decimal, FromFloat) {
40 WideString big = CFGAS_Decimal(powf(2.0f, 95.0f), 0).ToWideString();
41 WideString big_expected = L"39614081257132168796771975168";
42
43 // Precision may not be the same on all platforms.
44 EXPECT_EQ(big_expected.GetLength(), big.GetLength());
45 EXPECT_STREQ(big_expected.First(8).c_str(), big.First(8).c_str());
46
47 WideString tiny = CFGAS_Decimal(1e20f, 0).ToWideString();
48 WideString tiny_expected = L"100000000000000000000";
49 EXPECT_EQ(tiny_expected.GetLength(), tiny.GetLength());
50 EXPECT_STREQ(tiny_expected.First(8).c_str(), tiny.First(8).c_str());
51
52 WideString teeny = CFGAS_Decimal(1e14f, 4).ToWideString();
53 WideString teeny_expected = L"100000000000000.0000";
54 EXPECT_EQ(teeny_expected.GetLength(), teeny.GetLength());
55 EXPECT_STREQ(teeny_expected.First(8).c_str(), teeny.First(8).c_str());
56 }
57
TEST(CFGAS_Decimal,FromFloatFractional)58 TEST(CFGAS_Decimal, FromFloatFractional) {
59 WideString case1 = CFGAS_Decimal(123.456f, 10).ToWideString();
60 WideString case1_expected = L"123.4560000000";
61
62 // Precision may not be the same on all platforms.
63 EXPECT_EQ(case1_expected.GetLength(), case1.GetLength());
64 EXPECT_STREQ(case1_expected.First(8).c_str(), case1.First(8).c_str());
65 }
66
TEST(CFGAS_Decimal,FromString)67 TEST(CFGAS_Decimal, FromString) {
68 CFGAS_Decimal big(L"100000000000000000000000000");
69 CFGAS_Decimal small(L"-1000000000000000000000000");
70 EXPECT_STREQ(L"100000000000000000000000000", big.ToWideString().c_str());
71 EXPECT_STREQ(L"-1000000000000000000000000", small.ToWideString().c_str());
72 }
73
TEST(CFGAS_Decimal,FromString28Digits)74 TEST(CFGAS_Decimal, FromString28Digits) {
75 CFGAS_Decimal frac(L"32109876543210.0123456890123");
76 EXPECT_STREQ(L"32109876543210.0123456890123", frac.ToWideString().c_str());
77 }
78