File: StringTest.cpp

package info (click to toggle)
libpodofo 0.9.0-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,068 kB
  • sloc: cpp: 53,348; ansic: 339; sh: 96; makefile: 59
file content (318 lines) | stat: -rw-r--r-- 13,747 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***************************************************************************
 *   Copyright (C) 2007 by Dominik Seichter                                *
 *   domseichter@web.de                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "StringTest.h"

#include <podofo.h>

#ifndef __clang__

using namespace PoDoFo;

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( StringTest );

inline std::ostream& operator<<(std::ostream& o, const PdfString& s)
{
    return o << s.GetStringUtf8();
}

void StringTest::setUp()
{
}

void StringTest::tearDown()
{
}

void StringTest::testGetStringUtf8()
{
    const std::string src1 = "Hello World!";
    const std::string src2 = src1;
    const std::string src3 = "「Po\tDoFo」は今から日本語も話せます。";

    // Normal ascii string should be converted to UTF8
    PdfString str1( src1.c_str() );
    std::string res1 = str1.GetStringUtf8();

    CPPUNIT_ASSERT_EQUAL_MESSAGE( "testing const char* ASCII -> UTF8", src1, res1 );

    // Normal std::string string should be converted to UTF8
    PdfString str2( src2 );
    std::string res2 = str2.GetStringUtf8();

    CPPUNIT_ASSERT_EQUAL_MESSAGE( "testing std::string ASCII -> UTF8", src2, res2 );

    // UTF8 data in std::string cannot be converted as we do not know it is UTF8
    PdfString str3( src3 );
    std::string res3 = str3.GetStringUtf8();

    CPPUNIT_ASSERT_EQUAL_MESSAGE( "testing std::string UTF8 -> UTF8", (res3 != src3) , true );

    // UTF8 data as pdf_utf8* must be convertible
    PdfString str4( reinterpret_cast<const pdf_utf8*>(src3.c_str()) );
    std::string res4 = str4.GetStringUtf8();

    CPPUNIT_ASSERT_EQUAL_MESSAGE( "testing pdf_utf8* UTF8 -> UTF8", res4, src3 );    
}

void StringTest::testUtf16beContructor()
{
    const char* pszStringJapUtf8 = "「PoDoFo」は今から日本語も話せます。";
    // The same string as a NULL-terminated UTF-8 string. This is a UTF-8 literal, so your editor
    // must be configured to handle this file as UTF-8 to see something sensible below.
    // The same string in UTF16BE encoding
    const char psStringJapUtf16BE[44] = { 0x30, 0x0c, 0x00, 0x50, 0x00, 0x6f, 
                                          0x00, 0x44, 0x00, 0x6f, 0x00, 0x46, 
                                          0x00, 0x6f, 0x30, 0x0d, 0x30, 0x6f, 
                                          0x4e, 0xca, 0x30, 0x4b, 0x30, 0x89, 
                                          0x65, 0xe5, 0x67, 0x2c, 0x8a, 0x9e, 
                                          0x30, 0x82, 0x8a, 0x71, 0x30, 0x5b, 
                                          0x30, 0x7e, 0x30, 0x59, 0x30, 0x02, 
                                          0x00, 0x00 };

    PdfString strUtf8( reinterpret_cast<const pdf_utf8*>(pszStringJapUtf8) );
    PdfString strUtf16( reinterpret_cast<const pdf_utf16be*>(psStringJapUtf16BE), 21 );
    PdfString strUtf16b( reinterpret_cast<const pdf_utf16be*>(psStringJapUtf16BE), 21 );

    /*
    std::cout << std::endl;
    std::cout << "utf8 :" << strUtf8 << "  " << strUtf8.GetCharacterLength() << std::endl;
    std::cout << "utf16:" << strUtf16 << "  " << strUtf16.GetCharacterLength() <<  std::endl;
    std::cout << "wide : ";
    for( int i=0;i<=strUtf16.GetCharacterLength();i++ )
        printf("%04x ", strUtf16.GetUnicode()[i]);
    std::cout << std::endl;


    std::cout << "wide : ";
    for( int i=0;i<=strUtf16.GetCharacterLength();i++ )
        printf("%4i ", i );
    std::cout << std::endl;
    */
    
    // Compare UTF16 to UTF8 string
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "Comparing string length", 
                                  strUtf8.GetCharacterLength(), strUtf16.GetCharacterLength() );

    CPPUNIT_ASSERT_EQUAL_MESSAGE( "Comparing UTF8 and UTF16 string converted to UTF8", 
                                  strUtf8.GetStringUtf8(), strUtf16.GetStringUtf8() );

    CPPUNIT_ASSERT_EQUAL_MESSAGE( "Comparing UTF8 and UTF16 string", strUtf8, strUtf16 );

    // Compare two UTF16 strings
    CPPUNIT_ASSERT_EQUAL( strUtf16.GetCharacterLength(), strUtf16b.GetCharacterLength() );
    CPPUNIT_ASSERT_EQUAL( strUtf16.GetStringUtf8(), strUtf16b.GetStringUtf8() );
    CPPUNIT_ASSERT_EQUAL( strUtf16, strUtf16b );

}

void StringTest::testWCharConstructor()
{
    CPPUNIT_ASSERT_EQUAL( PdfString("Hallo World"), PdfString(L"Hallo World") );
    CPPUNIT_ASSERT_EQUAL( PdfString(L"Hallo World"), PdfString(L"Hallo World") );
}

void StringTest::testEscapeBrackets()
{
    // Test balanced brackets ansi
    const char* pszAscii       = "Hello (balanced) World";
    const char* pszAsciiExpect = "(Hello \\(balanced\\) World)";

    PdfString   sAscii( pszAscii );
    PdfVariant  varAscii( sAscii );
    std::string strAscii;
    varAscii.ToString( strAscii );

    CPPUNIT_ASSERT_EQUAL( strAscii == pszAsciiExpect, true );

    // Test un-balanced brackets ansi
    const char* pszAscii2       = "Hello ((unbalanced World";
    const char* pszAsciiExpect2 = "(Hello \\(\\(unbalanced World)";

    PdfString   sAscii2( pszAscii2 );
    PdfVariant  varAscii2( sAscii2 );
    std::string strAscii2;
    varAscii2.ToString( strAscii2 );

    CPPUNIT_ASSERT_EQUAL( strAscii2 == pszAsciiExpect2, true );

    // Test balanced brackets unicode
    const char* pszUnic       = "Hello (balanced) World";
    const char pszUnicExpect[]= { 0x28, 0xFE, 0xFF, 0x00, 0x48, 0x00, 0x65, 0x00, 
                                  0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x20, 0x00, 
                                  0x5C, 0x28, 0x00, 0x62, 0x00, 0x61, 0x00, 0x6C, 
                                  0x00, 0x61, 0x00, 0x6E, 0x00, 0x63, 0x00, 0x65, 
                                  0x00, 0x64, 0x00, 0x5C, 0x29, 0x00, 0x20, 0x00, 
                                  0x57, 0x00, 0x6F, 0x00, 0x72, 0x00, 0x6C, 0x00, 
                                  0x64, 0x29, 0x00, 0x00 };
    
    // Force unicode string
    PdfString   sUnic( reinterpret_cast<const pdf_utf8*>(pszUnic) );
    PdfVariant  varUnic( sUnic );
    std::string strUnic;
    varUnic.ToString( strUnic );

    CPPUNIT_ASSERT_EQUAL( memcmp( strUnic.c_str(), pszUnicExpect, strUnic.length() ) == 0, true );

    // Test un-balanced brackets unicode
    const char* pszUnic2       = "Hello ((unbalanced World";
    const char pszUnicExpect2[]= { 0x28, 0xFE, 0xFF, 0x00, 0x48, 0x00, 0x65, 0x00, 
                                   0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00, 0x20, 0x00, 
                                   0x5C, 0x28, 0x00, 0x5C, 0x28, 0x00, 0x75, 0x00, 
                                   0x6E, 0x00, 0x62, 0x00, 0x61, 0x00, 0x6C, 0x00, 
                                   0x61, 0x00, 0x6E, 0x00, 0x63, 0x00, 0x65, 0x00, 
                                   0x64, 0x00, 0x20, 0x00, 0x57, 0x00, 0x6F, 0x00, 
                                   0x72, 0x00, 0x6C, 0x00, 0x64, 0x29 };
    
    // Force unicode string
    PdfString   sUnic2( reinterpret_cast<const pdf_utf8*>(pszUnic2) );
    PdfVariant  varUnic2( sUnic2 );
    std::string strUnic2;
    varUnic2.ToString( strUnic2 );

    CPPUNIT_ASSERT_EQUAL( memcmp( strUnic2.c_str(), pszUnicExpect2, strUnic2.length() ) == 0, true );

    // Test reading the unicode string back in
    PdfVariant varRead;
    PdfTokenizer tokenizer( strUnic2.c_str(), strUnic2.length() );
    tokenizer.GetNextVariant( varRead, NULL );
    CPPUNIT_ASSERT_EQUAL( varRead.GetString() == sUnic2, true );
}

void StringTest::testWriteEscapeSequences()
{
    TestWriteEscapeSequences("(Hello\\nWorld)", "(Hello\\nWorld)");
    TestWriteEscapeSequences("(Hello\nWorld)", "(Hello\\nWorld)");
    TestWriteEscapeSequences("(Hello\012World)", "(Hello\\nWorld)");
    TestWriteEscapeSequences("(Hello\\012World)", "(Hello\\nWorld)");

    TestWriteEscapeSequences("(Hello\\rWorld)", "(Hello\\rWorld)");
    TestWriteEscapeSequences("(Hello\rWorld)", "(Hello\\rWorld)");
    TestWriteEscapeSequences("(Hello\015World)", "(Hello\\rWorld)");
    TestWriteEscapeSequences("(Hello\\015World)", "(Hello\\rWorld)");

    TestWriteEscapeSequences("(Hello\\tWorld)", "(Hello\\tWorld)");
    TestWriteEscapeSequences("(Hello\tWorld)", "(Hello\\tWorld)");
    TestWriteEscapeSequences("(Hello\011World)", "(Hello\\tWorld)");
    TestWriteEscapeSequences("(Hello\\011World)", "(Hello\\tWorld)");

    TestWriteEscapeSequences("(Hello\\fWorld)", "(Hello\\fWorld)");
    TestWriteEscapeSequences("(Hello\fWorld)", "(Hello\\fWorld)");
    TestWriteEscapeSequences("(Hello\014World)", "(Hello\\fWorld)");
    TestWriteEscapeSequences("(Hello\\014World)", "(Hello\\fWorld)");

    TestWriteEscapeSequences("(Hello\\(World)", "(Hello\\(World)");
    TestWriteEscapeSequences("(Hello\\050World)", "(Hello\\(World)");

    TestWriteEscapeSequences("(Hello\\)World)", "(Hello\\)World)");
    TestWriteEscapeSequences("(Hello\\051World)", "(Hello\\)World)");

    TestWriteEscapeSequences("(Hello\\\\World)", "(Hello\\\\World)");
    TestWriteEscapeSequences("(Hello\\\134World)", "(Hello\\\\World)");

    // Special case, \ at end of line
    TestWriteEscapeSequences("(Hello\\\nWorld)", "(HelloWorld)");


    TestWriteEscapeSequences("(Hello\003World)", "(Hello\003World)");
}

void StringTest::TestWriteEscapeSequences(const char* pszSource, const char* pszExpected)
{
    PdfVariant  variant;
    std::string ret;
    std::string expected = pszExpected;

    printf("Testing with value: %s\n", pszSource );
    PdfTokenizer tokenizer( pszSource, strlen( pszSource ) );

    tokenizer.GetNextVariant( variant, NULL );
    CPPUNIT_ASSERT_EQUAL( variant.GetDataType(), ePdfDataType_String );

    variant.ToString( ret );
    printf("   -> Convert To String: %s\n", ret.c_str() );

    CPPUNIT_ASSERT_EQUAL( expected, ret );

}

void StringTest::testEmptyString()
{
    const char* pszEmpty = "";
    std::string sEmpty;
    std::string sEmpty2( pszEmpty );

    PdfString str1;
    PdfString str2( sEmpty );
    PdfString str3( sEmpty2 );
    PdfString str4( pszEmpty );
    PdfString str5( pszEmpty, 0, false );
    PdfString str6( reinterpret_cast<const pdf_utf8*>(pszEmpty) );
    PdfString str7( reinterpret_cast<const pdf_utf8*>(pszEmpty), 0 );
    PdfString str8( reinterpret_cast<const pdf_utf16be*>(L""), 0 );

    CPPUNIT_ASSERT( !str1.IsValid() );

    CPPUNIT_ASSERT( str2.IsValid() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str2.GetLength() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str2.GetCharacterLength() );
    CPPUNIT_ASSERT_EQUAL( std::string(), str2.GetStringUtf8() );
    CPPUNIT_ASSERT_EQUAL( std::string(""), str2.GetStringUtf8() );

    CPPUNIT_ASSERT( str3.IsValid() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str3.GetLength() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str3.GetCharacterLength() );
    CPPUNIT_ASSERT_EQUAL( std::string(), str3.GetStringUtf8() );
    CPPUNIT_ASSERT_EQUAL( std::string(""), str3.GetStringUtf8() );

    CPPUNIT_ASSERT( str4.IsValid() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str4.GetLength() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str4.GetCharacterLength() );
    CPPUNIT_ASSERT_EQUAL( std::string(), str4.GetStringUtf8() );
    CPPUNIT_ASSERT_EQUAL( std::string(""), str4.GetStringUtf8() );

    CPPUNIT_ASSERT( str5.IsValid() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str5.GetLength() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str5.GetCharacterLength() );
    CPPUNIT_ASSERT_EQUAL( std::string(), str5.GetStringUtf8() );
    CPPUNIT_ASSERT_EQUAL( std::string(""), str5.GetStringUtf8() );

    CPPUNIT_ASSERT( str6.IsValid() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str6.GetLength() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str6.GetCharacterLength() );
    CPPUNIT_ASSERT_EQUAL( std::string(), str6.GetStringUtf8() );
    CPPUNIT_ASSERT_EQUAL( std::string(""), str6.GetStringUtf8() );

    CPPUNIT_ASSERT( str7.IsValid() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str7.GetLength() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str7.GetCharacterLength() );
    CPPUNIT_ASSERT_EQUAL( std::string(), str7.GetStringUtf8() );
    CPPUNIT_ASSERT_EQUAL( std::string(""), str7.GetStringUtf8() );

    CPPUNIT_ASSERT( str8.IsValid() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str8.GetLength() );
    CPPUNIT_ASSERT_EQUAL( static_cast<pdf_long>(0), str8.GetCharacterLength() );
    CPPUNIT_ASSERT_EQUAL( std::string(), str8.GetStringUtf8() );
    CPPUNIT_ASSERT_EQUAL( std::string(""), str8.GetStringUtf8() );

}

#endif // __clang__