OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <limits> |
| 6 |
5 #include "core/fxcrt/include/fx_basic.h" | 7 #include "core/fxcrt/include/fx_basic.h" |
6 #include "testing/fx_string_testhelpers.h" | 8 #include "testing/fx_string_testhelpers.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 | 10 |
9 namespace { | 11 namespace { |
10 | 12 |
11 uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) { | 13 uint32_t ReferenceGetBits32(const uint8_t* pData, int bitpos, int nbits) { |
12 int result = 0; | 14 int result = 0; |
13 for (int i = 0; i < nbits; i++) { | 15 for (int i = 0; i < nbits; i++) { |
14 if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) | 16 if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) |
15 result |= 1 << (nbits - i - 1); | 17 result |= 1 << (nbits - i - 1); |
16 } | 18 } |
17 return result; | 19 return result; |
18 } | 20 } |
19 | 21 |
20 } // namespace | 22 } // namespace |
21 | 23 |
22 TEST(fxge, GetBits32) { | 24 TEST(fxge, GetBits32) { |
23 unsigned char data[] = {0xDE, 0x3F, 0xB1, 0x7C, 0x12, 0x9A, 0x04, 0x56}; | 25 unsigned char data[] = {0xDE, 0x3F, 0xB1, 0x7C, 0x12, 0x9A, 0x04, 0x56}; |
24 for (int nbits = 1; nbits <= 32; ++nbits) { | 26 for (int nbits = 1; nbits <= 32; ++nbits) { |
25 for (int bitpos = 0; bitpos < (int)sizeof(data) * 8 - nbits; ++bitpos) { | 27 for (int bitpos = 0; bitpos < (int)sizeof(data) * 8 - nbits; ++bitpos) { |
26 EXPECT_EQ(ReferenceGetBits32(data, bitpos, nbits), | 28 EXPECT_EQ(ReferenceGetBits32(data, bitpos, nbits), |
27 GetBits32(data, bitpos, nbits)); | 29 GetBits32(data, bitpos, nbits)); |
28 } | 30 } |
29 } | 31 } |
30 } | 32 } |
| 33 |
| 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 } |
OLD | NEW |