• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(CFGASDecimalTest,Empty)13 TEST(CFGASDecimalTest, 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(CFGASDecimalTest,FromInt32)20 TEST(CFGASDecimalTest, FromInt32) {
21   CFGAS_Decimal big(std::numeric_limits<int32_t>::max());
22   CFGAS_Decimal small(std::numeric_limits<int32_t>::min());
23   EXPECT_EQ(L"2147483647", big.ToWideString());
24   EXPECT_EQ(L"-2147483648", small.ToWideString());
25 }
26 
TEST(CFGASDecimalTest,FromUint32)27 TEST(CFGASDecimalTest, FromUint32) {
28   CFGAS_Decimal big(std::numeric_limits<uint32_t>::max());
29   CFGAS_Decimal small(std::numeric_limits<uint32_t>::min());
30   EXPECT_EQ(L"4294967295", big.ToWideString());
31   EXPECT_EQ(L"0", small.ToWideString());
32 }
33 
TEST(CFGASDecimalTest,FromUint64)34 TEST(CFGASDecimalTest, FromUint64) {
35   CFGAS_Decimal big(std::numeric_limits<uint64_t>::max());
36   CFGAS_Decimal small(std::numeric_limits<uint64_t>::min());
37   EXPECT_EQ(L"18446744073709551615", big.ToWideString());
38   EXPECT_EQ(L"0", small.ToWideString());
39 }
40 
TEST(CFGASDecimalTest,FromFloat)41 TEST(CFGASDecimalTest, 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_EQ(big_expected.First(8).c_str(), big.First(8));
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_EQ(tiny_expected.First(8).c_str(), tiny.First(8));
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_EQ(teeny_expected.First(8).c_str(), teeny.First(8));
58 }
59 
TEST(CFGASDecimalTest,FromFloatFractional)60 TEST(CFGASDecimalTest, 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_EQ(case1_expected.First(8).c_str(), case1.First(8));
67 }
68 
TEST(CFGASDecimalTest,FromString)69 TEST(CFGASDecimalTest, FromString) {
70   CFGAS_Decimal big(L"100000000000000000000000000");
71   CFGAS_Decimal small(L"-1000000000000000000000000");
72   EXPECT_EQ(L"100000000000000000000000000", big.ToWideString());
73   EXPECT_EQ(L"-1000000000000000000000000", small.ToWideString());
74 }
75 
TEST(CFGASDecimalTest,FromString28Digits)76 TEST(CFGASDecimalTest, FromString28Digits) {
77   CFGAS_Decimal frac(L"32109876543210.0123456890123");
78   EXPECT_EQ(L"32109876543210.0123456890123", frac.ToWideString());
79 }
80