• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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_basic.h"
8 #include "testing/fx_string_testhelpers.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace {
12 
ReferenceGetBits32(const uint8_t * pData,int bitpos,int nbits)13 uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) {
14   int result = 0;
15   for (int i = 0; i < nbits; i++) {
16     if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8)))
17       result |= 1 << (nbits - i - 1);
18   }
19   return result;
20 }
21 
22 }  // namespace
23 
TEST(fxge,GetBits32)24 TEST(fxge, GetBits32) {
25   unsigned char data[] = {0xDE, 0x3F, 0xB1, 0x7C, 0x12, 0x9A, 0x04, 0x56};
26   for (int nbits = 1; nbits <= 32; ++nbits) {
27     for (int bitpos = 0; bitpos < (int)sizeof(data) * 8 - nbits; ++bitpos) {
28       EXPECT_EQ(ReferenceGetBits32(data, bitpos, nbits),
29                 GetBits32(data, bitpos, nbits));
30     }
31   }
32 }
33 
TEST(fxcrt,FX_atonum)34 TEST(fxcrt, FX_atonum) {
35   int i;
36   EXPECT_TRUE(FX_atonum("10", &i));
37   EXPECT_EQ(10, i);
38 
39   EXPECT_TRUE(FX_atonum("-10", &i));
40   EXPECT_EQ(-10, i);
41 
42   EXPECT_TRUE(FX_atonum("+10", &i));
43   EXPECT_EQ(10, i);
44 
45   EXPECT_TRUE(FX_atonum("-2147483648", &i));
46   EXPECT_EQ(std::numeric_limits<int>::min(), i);
47 
48   EXPECT_TRUE(FX_atonum("2147483647", &i));
49   EXPECT_EQ(2147483647, i);
50 
51   // Value overflows.
52   EXPECT_TRUE(FX_atonum("-2147483649", &i));
53   EXPECT_EQ(0, i);
54 
55   // Value overflows.
56   EXPECT_TRUE(FX_atonum("+2147483648", &i));
57   EXPECT_EQ(0, i);
58 
59   // Value overflows.
60   EXPECT_TRUE(FX_atonum("4223423494965252", &i));
61   EXPECT_EQ(0, i);
62 
63   // No explicit sign will allow the number to go negative. This is for things
64   // like the encryption Permissions flag (Table 3.20 PDF 1.7 spec)
65   EXPECT_TRUE(FX_atonum("4294965252", &i));
66   EXPECT_EQ(-2044, i);
67 
68   EXPECT_TRUE(FX_atonum("-4294965252", &i));
69   EXPECT_EQ(0, i);
70 
71   EXPECT_TRUE(FX_atonum("+4294965252", &i));
72   EXPECT_EQ(0, i);
73 
74   float f;
75   EXPECT_FALSE(FX_atonum("3.24", &f));
76   EXPECT_FLOAT_EQ(3.24f, f);
77 }
78