diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2015-11-02 13:27:57 -0500 |
---|---|---|
committer | Dan Sinclair <dsinclair@chromium.org> | 2015-11-02 13:27:57 -0500 |
commit | 52b0b525dca3d982a04b77fa6d0913aff1e5fd9c (patch) | |
tree | 4b22704e8eb802dc0503d1c4bd4183a828a1edbe | |
parent | 71fca3fffc40f63b28c0085add17ac73b5a066ef (diff) | |
download | pdfium-52b0b525dca3d982a04b77fa6d0913aff1e5fd9c.tar.xz |
Add test for StringToCode and StringToWideString
This CL adds a test case for the StringToCode and
StringToWideString methods in fpdf_font.cpp. In order
to do so, it moves the methods to be private methods of CPDF_ToUnicodeMap and
make the tests friends of the class.
R=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1428593005 .
-rw-r--r-- | BUILD.gn | 1 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_font/font_int.h | 7 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_font/fpdf_font.cpp | 28 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_font/fpdf_font_unittest.cpp | 31 | ||||
-rw-r--r-- | pdfium.gyp | 1 |
5 files changed, 57 insertions, 11 deletions
@@ -714,6 +714,7 @@ static_library("formfiller") { test("pdfium_unittests") { sources = [ + "core/src/fpdfapi/fpdf_font/fpdf_font_unittest.cpp", "core/src/fxcodec/codec/fx_codec_jpx_unittest.cpp", "core/src/fxcrt/fx_basic_bstring_unittest.cpp", "core/src/fxcrt/fx_basic_memmgr_unittest.cpp", diff --git a/core/src/fpdfapi/fpdf_font/font_int.h b/core/src/fpdfapi/fpdf_font/font_int.h index 48e58d6345..559027b283 100644 --- a/core/src/fpdfapi/fpdf_font/font_int.h +++ b/core/src/fpdfapi/fpdf_font/font_int.h @@ -174,6 +174,13 @@ class CPDF_ToUnicodeMap { std::map<FX_DWORD, FX_DWORD> m_Map; CPDF_CID2UnicodeMap* m_pBaseMap; CFX_WideTextBuf m_MultiCharBuf; + + private: + friend class fpdf_font_StringToCode_Test; + friend class fpdf_font_StringToWideString_Test; + + static FX_DWORD StringToCode(const CFX_ByteStringC& str); + static CFX_WideString StringToWideString(const CFX_ByteStringC& str); }; class CPDF_FontCharMap : public CFX_CharMap { public: diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp index 543816b03a..0529d3d35b 100644 --- a/core/src/fpdfapi/fpdf_font/fpdf_font.cpp +++ b/core/src/fpdfapi/fpdf_font/fpdf_font.cpp @@ -508,7 +508,9 @@ FX_DWORD CPDF_ToUnicodeMap::ReverseLookup(FX_WCHAR unicode) { } return 0; } -static FX_DWORD _StringToCode(const CFX_ByteStringC& str) { + +// Static. +FX_DWORD CPDF_ToUnicodeMap::StringToCode(const CFX_ByteStringC& str) { const FX_CHAR* buf = str.GetCStr(); int len = str.GetLength(); if (len == 0) { @@ -539,7 +541,7 @@ static FX_DWORD _StringToCode(const CFX_ByteStringC& str) { } return result; } -static CFX_WideString _StringDataAdd(CFX_WideString str) { +static CFX_WideString StringDataAdd(CFX_WideString str) { CFX_WideString ret; int len = str.GetLength(); FX_WCHAR value = 1; @@ -557,7 +559,10 @@ static CFX_WideString _StringDataAdd(CFX_WideString str) { } return ret; } -static CFX_WideString _StringToWideString(const CFX_ByteStringC& str) { + +// Static. +CFX_WideString CPDF_ToUnicodeMap::StringToWideString( + const CFX_ByteStringC& str) { const FX_CHAR* buf = str.GetCStr(); int len = str.GetLength(); if (len == 0) { @@ -579,6 +584,7 @@ static CFX_WideString _StringToWideString(const CFX_ByteStringC& str) { break; } ch = ch * 16 + digit; + byte_pos++; if (byte_pos == 4) { result += ch; @@ -608,9 +614,9 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { if (word.IsEmpty() || word == FX_BSTRC("endbfchar")) { break; } - FX_DWORD srccode = _StringToCode(word); + FX_DWORD srccode = StringToCode(word); word = parser.GetWord(); - CFX_WideString destcode = _StringToWideString(word); + CFX_WideString destcode = StringToWideString(word); int len = destcode.GetLength(); if (len == 0) { continue; @@ -631,9 +637,9 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { break; } high = parser.GetWord(); - FX_DWORD lowcode = _StringToCode(low); + FX_DWORD lowcode = StringToCode(low); FX_DWORD highcode = - (lowcode & 0xffffff00) | (_StringToCode(high) & 0xff); + (lowcode & 0xffffff00) | (StringToCode(high) & 0xff); if (highcode == (FX_DWORD)-1) { break; } @@ -641,7 +647,7 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { if (start == FX_BSTRC("[")) { for (FX_DWORD code = lowcode; code <= highcode; code++) { CFX_ByteString dest = parser.GetWord(); - CFX_WideString destcode = _StringToWideString(dest); + CFX_WideString destcode = StringToWideString(dest); int len = destcode.GetLength(); if (len == 0) { continue; @@ -656,11 +662,11 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { } parser.GetWord(); } else { - CFX_WideString destcode = _StringToWideString(start); + CFX_WideString destcode = StringToWideString(start); int len = destcode.GetLength(); FX_DWORD value = 0; if (len == 1) { - value = _StringToCode(start); + value = StringToCode(start); for (FX_DWORD code = lowcode; code <= highcode; code++) { m_Map[code] = value++; } @@ -670,7 +676,7 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { if (code == lowcode) { retcode = destcode; } else { - retcode = _StringDataAdd(destcode); + retcode = StringDataAdd(destcode); } m_Map[code] = m_MultiCharBuf.GetLength() * 0x10000 + 0xffff; m_MultiCharBuf.AppendChar(retcode.GetLength()); diff --git a/core/src/fpdfapi/fpdf_font/fpdf_font_unittest.cpp b/core/src/fpdfapi/fpdf_font/fpdf_font_unittest.cpp new file mode 100644 index 0000000000..9cc529c3df --- /dev/null +++ b/core/src/fpdfapi/fpdf_font/fpdf_font_unittest.cpp @@ -0,0 +1,31 @@ +// Copyright 2015 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/gtest/include/gtest/gtest.h" + +#include "font_int.h" + +TEST(fpdf_font, StringToCode) { + EXPECT_EQ(0, CPDF_ToUnicodeMap::StringToCode("")); + EXPECT_EQ(194, CPDF_ToUnicodeMap::StringToCode("<c2")); + EXPECT_EQ(162, CPDF_ToUnicodeMap::StringToCode("<A2")); + EXPECT_EQ(2802, CPDF_ToUnicodeMap::StringToCode("<Af2")); + EXPECT_EQ(12, CPDF_ToUnicodeMap::StringToCode("12")); + EXPECT_EQ(128, CPDF_ToUnicodeMap::StringToCode("128")); +} + +TEST(fpdf_font, StringToWideString) { + EXPECT_EQ(L"", CPDF_ToUnicodeMap::StringToWideString("")); + EXPECT_EQ(L"", CPDF_ToUnicodeMap::StringToWideString("1234")); + + EXPECT_EQ(L"", CPDF_ToUnicodeMap::StringToWideString("<c2")); + + CFX_WideString res = L"\xc2ab"; + EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2ab")); + EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2abab")); + EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2ab 1234")); + + res += L"\xfaab"; + EXPECT_EQ(res, CPDF_ToUnicodeMap::StringToWideString("<c2abFaAb")); +} diff --git a/pdfium.gyp b/pdfium.gyp index 0eebb713bc..208a464d87 100644 --- a/pdfium.gyp +++ b/pdfium.gyp @@ -714,6 +714,7 @@ '<(DEPTH)' ], 'sources': [ + 'core/src/fpdfapi/fpdf_font/fpdf_font_unittest.cpp', 'core/src/fxcodec/codec/fx_codec_jpx_unittest.cpp', 'core/src/fxcrt/fx_basic_bstring_unittest.cpp', 'core/src/fxcrt/fx_basic_memmgr_unittest.cpp', |