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