• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 <limits>
6 
7 #include "core/fxcrt/fx_number.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 
TEST(fxnumber,Default)10 TEST(fxnumber, Default) {
11   FX_Number number;
12   EXPECT_TRUE(number.IsInteger());
13   EXPECT_FALSE(number.IsSigned());
14   EXPECT_EQ(0, number.GetSigned());
15   EXPECT_FLOAT_EQ(0.0f, number.GetFloat());
16 }
17 
TEST(fxnumber,FromSigned)18 TEST(fxnumber, FromSigned) {
19   FX_Number number(-128);
20   EXPECT_TRUE(number.IsInteger());
21   EXPECT_TRUE(number.IsSigned());
22   EXPECT_EQ(-128, number.GetSigned());
23   EXPECT_FLOAT_EQ(-128.0f, number.GetFloat());
24 
25   // Show that assignment works.
26   FX_Number number2 = number;
27   EXPECT_TRUE(number2.IsInteger());
28   EXPECT_TRUE(number2.IsSigned());
29   EXPECT_EQ(-128, number2.GetSigned());
30   EXPECT_FLOAT_EQ(-128.0f, number2.GetFloat());
31 }
32 
TEST(fxnumber,FromFloat)33 TEST(fxnumber, FromFloat) {
34   FX_Number number(-100.001f);
35   EXPECT_FALSE(number.IsInteger());
36   EXPECT_TRUE(number.IsSigned());
37   EXPECT_EQ(-100, number.GetSigned());
38   EXPECT_FLOAT_EQ(-100.001f, number.GetFloat());
39 
40   // Show that assignment works.
41   FX_Number number2 = number;
42   EXPECT_FALSE(number2.IsInteger());
43   EXPECT_TRUE(number2.IsSigned());
44   EXPECT_EQ(-100, number2.GetSigned());
45   EXPECT_FLOAT_EQ(-100.001f, number2.GetFloat());
46 }
47 
TEST(fxnumber,FromStringUnsigned)48 TEST(fxnumber, FromStringUnsigned) {
49   {
50     FX_Number number("");
51     EXPECT_TRUE(number.IsInteger());
52     EXPECT_FALSE(number.IsSigned());
53   }
54   {
55     FX_Number number("0");
56     EXPECT_TRUE(number.IsInteger());
57     EXPECT_FALSE(number.IsSigned());
58   }
59   {
60     FX_Number number("10");
61     EXPECT_TRUE(number.IsInteger());
62     EXPECT_FALSE(number.IsSigned());
63   }
64   {
65     FX_Number number("4294967295");
66     EXPECT_TRUE(number.IsInteger());
67     EXPECT_FALSE(number.IsSigned());
68   }
69   {
70     // Value overflows.
71     FX_Number number("4223423494965252");
72     EXPECT_TRUE(number.IsInteger());
73     EXPECT_FALSE(number.IsSigned());
74   }
75   {
76     // No explicit sign will allow the number to go negative if we retrieve
77     // it as a signed value. This is needed for things like the encryption
78     // Permissions flag (Table 3.20 PDF 1.7 spec)
79     FX_Number number("4294965252");
80     EXPECT_EQ(-2044, number.GetSigned());
81   }
82 }
83 
TEST(fxnumber,FromStringSigned)84 TEST(fxnumber, FromStringSigned) {
85   {
86     FX_Number number("-0");
87     EXPECT_TRUE(number.IsInteger());
88     EXPECT_TRUE(number.IsSigned());
89     EXPECT_EQ(0, number.GetSigned());
90   }
91   {
92     FX_Number number("+0");
93     EXPECT_TRUE(number.IsInteger());
94     EXPECT_TRUE(number.IsSigned());
95     EXPECT_EQ(0, number.GetSigned());
96   }
97   {
98     FX_Number number("-10");
99     EXPECT_TRUE(number.IsInteger());
100     EXPECT_TRUE(number.IsSigned());
101     EXPECT_EQ(-10, number.GetSigned());
102   }
103   {
104     FX_Number number("+10");
105     EXPECT_TRUE(number.IsInteger());
106     EXPECT_TRUE(number.IsSigned());
107     EXPECT_EQ(10, number.GetSigned());
108   }
109   {
110     FX_Number number("-2147483648");
111     EXPECT_TRUE(number.IsInteger());
112     EXPECT_TRUE(number.IsSigned());
113     EXPECT_EQ(std::numeric_limits<int32_t>::min(), number.GetSigned());
114   }
115   {
116     FX_Number number("+2147483647");
117     EXPECT_TRUE(number.IsInteger());
118     EXPECT_TRUE(number.IsSigned());
119     EXPECT_EQ(std::numeric_limits<int32_t>::max(), number.GetSigned());
120   }
121   {
122     // Value underflows.
123     FX_Number number("-2147483649");
124     EXPECT_EQ(0, number.GetSigned());
125   }
126   {
127     // Value overflows.
128     FX_Number number("+2147483648");
129     EXPECT_EQ(0, number.GetSigned());
130   }
131 }
132 
TEST(fxnumber,FromStringFloat)133 TEST(fxnumber, FromStringFloat) {
134   FX_Number number("3.24");
135   EXPECT_FLOAT_EQ(3.24f, number.GetFloat());
136 }
137