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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/***************************************************************************
* Copyright (C) 2008 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 "EncodingTest.h"
#include <podofo.h>
#include <ostream>
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( EncodingTest );
using namespace PoDoFo;
inline std::ostream& operator<<(std::ostream& o, const PdfVariant& s)
{
std::string str;
s.ToString(str);
return o << str;
}
void EncodingTest::setUp()
{
}
void EncodingTest::tearDown()
{
}
void EncodingTest::testDifferences()
{
PdfEncodingDifference difference;
// Newly created encoding should be empty
CPPUNIT_ASSERT_EQUAL( static_cast<int>(difference.GetCount()), 0 );
// Adding 0 should work
difference.AddDifference( 0, PdfName("A") );
CPPUNIT_ASSERT_EQUAL( static_cast<int>(difference.GetCount()), 1 );
// Adding 255 should work
difference.AddDifference( 255, PdfName("B") );
CPPUNIT_ASSERT_EQUAL( static_cast<int>(difference.GetCount()), 2 );
// Adding out of range should throw exception
CPPUNIT_ASSERT_THROW( difference.AddDifference( -1, PdfName("C") );, PdfError );
CPPUNIT_ASSERT_THROW( difference.AddDifference( 256, PdfName("D") );, PdfError );
CPPUNIT_ASSERT_EQUAL( static_cast<int>(difference.GetCount()), 2 );
// Convert to array
PdfArray data;
PdfArray expected;
expected.push_back( static_cast<pdf_int64>(0LL) );
expected.push_back( PdfName("A") );
expected.push_back( static_cast<pdf_int64>(255LL) );
expected.push_back( PdfName("B") );
difference.ToArray( data );
CPPUNIT_ASSERT_EQUAL( data.GetSize(), expected.GetSize() );
for( unsigned int i=0;i<data.GetSize(); i++ )
CPPUNIT_ASSERT_EQUAL( data[i], expected[i] );
// Test replace
expected.Clear();
expected.push_back( static_cast<pdf_int64>(0LL) );
expected.push_back( PdfName("A") );
expected.push_back( static_cast<pdf_int64>(255LL) );
expected.push_back( PdfName("X") );
difference.AddDifference( 255, PdfName("X") );
difference.ToArray( data );
CPPUNIT_ASSERT_EQUAL( data.GetSize(), expected.GetSize() );
for( unsigned int i=0;i<data.GetSize(); i++ )
CPPUNIT_ASSERT_EQUAL( data[i], expected[i] );
// Test more complicated array
expected.Clear();
expected.push_back( static_cast<pdf_int64>(0LL) );
expected.push_back( PdfName("A") );
expected.push_back( PdfName("B") );
expected.push_back( PdfName("C") );
expected.push_back( static_cast<pdf_int64>(4LL) );
expected.push_back( PdfName("D") );
expected.push_back( PdfName("E") );
expected.push_back( static_cast<pdf_int64>(9LL) );
expected.push_back( PdfName("F") );
expected.push_back( static_cast<pdf_int64>(255LL) );
expected.push_back( PdfName("X") );
difference.AddDifference( 1, PdfName("B") );
difference.AddDifference( 2, PdfName("C") );
difference.AddDifference( 4, PdfName("D") );
difference.AddDifference( 5, PdfName("E") );
difference.AddDifference( 9, PdfName("F") );
difference.ToArray( data );
CPPUNIT_ASSERT_EQUAL( data.GetSize(), expected.GetSize() );
for( unsigned int i=0;i<data.GetSize(); i++ )
CPPUNIT_ASSERT_EQUAL( data[i], expected[i] );
// Test if contains works correctly
PdfName name;
pdf_utf16be value;
CPPUNIT_ASSERT_EQUAL( difference.Contains( 0, name, value ), true );
CPPUNIT_ASSERT_EQUAL( name, PdfName("A") );
#ifdef PODOFO_IS_LITTLE_ENDIAN
CPPUNIT_ASSERT_EQUAL( static_cast<int>(value), 0x4100 );
#else
CPPUNIT_ASSERT_EQUAL( static_cast<int>(value), 0x0041 );
#endif //PODOFO_IS_LITTLE_ENDIAN
CPPUNIT_ASSERT_EQUAL( difference.Contains( 9, name, value ), true );
CPPUNIT_ASSERT_EQUAL( name, PdfName("F") );
#ifdef PODOFO_IS_LITTLE_ENDIAN
CPPUNIT_ASSERT_EQUAL( static_cast<int>(value), 0x4600 );
#else
CPPUNIT_ASSERT_EQUAL( static_cast<int>(value), 0x0046 );
#endif //PODOFO_IS_LITTLE_ENDIAN
CPPUNIT_ASSERT_EQUAL( difference.Contains( 255, name, value ), true );
CPPUNIT_ASSERT_EQUAL( name, PdfName("X") );
#ifdef PODOFO_IS_LITTLE_ENDIAN
CPPUNIT_ASSERT_EQUAL( static_cast<int>(value), 0x5800 );
#else
CPPUNIT_ASSERT_EQUAL( static_cast<int>(value), 0x0058 );
#endif //PODOFO_IS_LITTLE_ENDIAN
CPPUNIT_ASSERT_EQUAL( difference.Contains( 100, name,value ), false );
}
void EncodingTest::testDifferencesObject()
{
PdfMemDocument doc;
PdfEncodingDifference difference;
difference.AddDifference( 1, PdfName("B") );
difference.AddDifference( 2, PdfName("C") );
difference.AddDifference( 4, PdfName("D") );
difference.AddDifference( 5, PdfName("E") );
difference.AddDifference( 9, PdfName("F") );
PdfDifferenceEncoding encoding( difference, PdfDifferenceEncoding::eBaseEncoding_MacRoman, &doc );
// Check for encoding key
PdfObject* pObj = doc.GetObjects().CreateObject();
encoding.AddToDictionary( pObj->GetDictionary() );
CPPUNIT_ASSERT_EQUAL( true, pObj->GetDictionary().HasKey( PdfName("Encoding") ) );
PdfObject* pKey = pObj->GetDictionary().GetKey( PdfName("Encoding") );
CPPUNIT_ASSERT_EQUAL( true, pKey->IsReference() );
PdfObject* pEncoding = doc.GetObjects().GetObject( pKey->GetReference() );
// Test BaseEncoding
PdfObject* pBase = pEncoding->GetDictionary().GetKey( PdfName("BaseEncoding" ) );
CPPUNIT_ASSERT_EQUAL( pBase->GetName(), PdfName("MacRomanEncoding") );
// Test differences
PdfObject* pDiff = pEncoding->GetDictionary().GetKey( PdfName("Differences" ) );
PdfArray expected;
expected.push_back( static_cast<pdf_int64>(1LL) );
expected.push_back( PdfName("B") );
expected.push_back( PdfName("C") );
expected.push_back( static_cast<pdf_int64>(4LL) );
expected.push_back( PdfName("D") );
expected.push_back( PdfName("E") );
expected.push_back( static_cast<pdf_int64>(9LL) );
expected.push_back( PdfName("F") );
const PdfArray & data = pDiff->GetArray();
CPPUNIT_ASSERT_EQUAL( data.GetSize(), expected.GetSize() );
for( unsigned int i=0;i<data.GetSize(); i++ )
CPPUNIT_ASSERT_EQUAL( data[i], expected[i] );
}
void EncodingTest::testDifferencesEncoding()
{
PdfMemDocument doc;
// Create a differences encoding where A and B are exchanged
PdfEncodingDifference difference;
difference.AddDifference( 0x0041, PdfName("B") );
difference.AddDifference( 0x0042, PdfName("A") );
PdfDifferenceEncoding encoding( difference, PdfDifferenceEncoding::eBaseEncoding_WinAnsi, &doc );
PdfString unicodeStr = encoding.ConvertToUnicode( PdfString("BAAB"), NULL );
CPPUNIT_ASSERT_EQUAL( PdfString("ABBA"), unicodeStr );
PdfRefCountedBuffer encodingStr = encoding.ConvertToEncoding( PdfString("ABBA"), NULL );
CPPUNIT_ASSERT_EQUAL( static_cast<size_t>(4), encodingStr.GetSize() );
CPPUNIT_ASSERT_EQUAL( memcmp("BAAB", encodingStr.GetBuffer(), encodingStr.GetSize()), 0 );
}
void EncodingTest::testUnicodeNames()
{
// List of items which are defined twice and cause
// other ids to be returned than those which where send in
const char* pszDuplicated[] = {
"Delta",
"fraction",
"hyphen",
"macron",
"mu",
"Omega",
"periodcentered",
"scedilla",
"Scedilla",
"space",
"tcommaaccent",
"Tcommaaccent",
"exclamsmall",
"dollaroldstyle",
"zerooldstyle",
"oneoldstyle",
"twooldstyle",
"threeoldstyle",
"fouroldstyle",
"fiveoldstyle",
"sixoldstyle",
"sevenoldstyle",
"eightoldstyle",
"nineoldstyle",
"ampersandsmall",
"questionsmall",
NULL
};
int nCount = 0;
for( int i = 0;i<=0xFFFF; i++ )
{
PdfName name = PdfDifferenceEncoding::UnicodeIDToName( static_cast<pdf_utf16be>(i) );
pdf_utf16be id = PdfDifferenceEncoding::NameToUnicodeID( name );
bool bFound = false;
const char** pszDup = pszDuplicated;
while( *pszDup )
{
if( PdfName( *pszDup ) == name )
{
bFound = true;
break;
}
++pszDup;
}
if( !bFound )
{
// Does not work because of 2 many duplicates...
//CPPUNIT_ASSERT_EQUAL_MESSAGE( name.GetName(), id, static_cast<pdf_utf16be>(i) );
if( id == static_cast<pdf_utf16be>(i) )
++nCount;
}
}
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Compared codes count", 65422, nCount );
}
void EncodingTest::testGetCharCode()
{
std::string msg;
bool ret;
PdfWinAnsiEncoding cWinAnsiEncoding;
ret = outofRangeHelper( &cWinAnsiEncoding, msg, "PdfWinAnsiEncoding" );
CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, ret, true );
PdfMacRomanEncoding cMacRomanEncoding;
ret = outofRangeHelper( &cMacRomanEncoding, msg, "PdfMacRomanEncoding" );
CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, ret, true );
PdfIdentityEncoding cIdentityEncoding;
ret = outofRangeHelper( &cIdentityEncoding, msg, "PdfIdentityEncoding" );
CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, ret, true );
PdfVecObjects vec;
vec.SetAutoDelete( true );
PdfEncodingDifference difference;
difference.AddDifference( 0x0041, PdfName("B") );
difference.AddDifference( 0x0042, PdfName("A") );
PdfDifferenceEncoding cDifferenceEncoding( difference, PdfDifferenceEncoding::eBaseEncoding_WinAnsi, &vec );
ret = outofRangeHelper( &cDifferenceEncoding, msg, "PdfDifferenceEncoding" );
CPPUNIT_ASSERT_EQUAL_MESSAGE( msg, ret, true );
#ifdef PODOFO_IS_LITTLE_ENDIAN
CPPUNIT_ASSERT_EQUAL( static_cast<pdf_utf16be>(0x4200), cDifferenceEncoding.GetCharCode( 0x0041 ) );
CPPUNIT_ASSERT_EQUAL( static_cast<pdf_utf16be>(0x4100), cDifferenceEncoding.GetCharCode( 0x0042 ) );
#else
CPPUNIT_ASSERT_EQUAL( static_cast<pdf_utf16be>(0x0042), cDifferenceEncoding.GetCharCode( 0x0041 ) );
CPPUNIT_ASSERT_EQUAL( static_cast<pdf_utf16be>(0x0041), cDifferenceEncoding.GetCharCode( 0x0042 ) );
#endif // PODOFO_IS_LITTLE_ENDIAN
}
bool EncodingTest::outofRangeHelper( PdfEncoding* pEncoding, std::string & rMsg, const char* pszName )
{
bool exception = false;
try {
pEncoding->GetCharCode( pEncoding->GetFirstChar() );
}
catch( const PdfError & rError )
{
// This may not throw!
rMsg = "pEncoding->GetCharCode( pEncoding->GetFirstChar() ) failed";
return false;
}
try {
pEncoding->GetCharCode( pEncoding->GetFirstChar() - 1 );
}
catch( const PdfError & rError )
{
// This has to throw!
exception = true;
}
if( !exception )
{
rMsg = "pEncoding->GetCharCode( pEncoding->GetFirstChar() - 1 ); failed";
return false;
}
try {
pEncoding->GetCharCode( pEncoding->GetLastChar() );
}
catch( const PdfError & rError )
{
// This may not throw!
rMsg = "pEncoding->GetCharCode( pEncoding->GetLastChar() ); failed";
return false;
}
exception = false;
try {
pEncoding->GetCharCode( pEncoding->GetLastChar() + 1 );
}
catch( const PdfError & rError )
{
// This has to throw!
exception = true;
}
if( !exception )
{
rMsg = "pEncoding->GetCharCode( pEncoding->GetLastChar() + 1 ); failed";
return false;
}
PdfEncoding::const_iterator it = pEncoding->begin();
int nCount = pEncoding->GetFirstChar();
while( it != pEncoding->end() )
{
CPPUNIT_ASSERT_EQUAL_MESSAGE( pszName, *it, pEncoding->GetCharCode( nCount ) );
++nCount;
++it;
}
return true;
}
|