ReactOS 0.4.16-dev-257-g6aa11ac
CodecvtTest Class Reference
Inheritance diagram for CodecvtTest:
Collaboration diagram for CodecvtTest:

Protected Member Functions

void variable_encoding ()
 
void in_out_test ()
 
void length_test ()
 
void imbue_while_reading ()
 
void special_encodings ()
 

Private Member Functions

 CPPUNIT_TEST_SUITE (CodecvtTest)
 
 CPPUNIT_TEST (variable_encoding)
 
 CPPUNIT_TEST (in_out_test)
 
 CPPUNIT_TEST (length_test)
 
 CPPUNIT_TEST (imbue_while_reading)
 
 CPPUNIT_TEST (special_encodings)
 
 CPPUNIT_TEST_SUITE_END ()
 

Private Attributes

 CPPUNIT_STOP_IGNORE
 

Detailed Description

Definition at line 18 of file codecvt_test.cpp.

Member Function Documentation

◆ CPPUNIT_TEST() [1/5]

CodecvtTest::CPPUNIT_TEST ( imbue_while_reading  )
private

◆ CPPUNIT_TEST() [2/5]

CodecvtTest::CPPUNIT_TEST ( in_out_test  )
private

◆ CPPUNIT_TEST() [3/5]

CodecvtTest::CPPUNIT_TEST ( length_test  )
private

◆ CPPUNIT_TEST() [4/5]

CodecvtTest::CPPUNIT_TEST ( special_encodings  )
private

◆ CPPUNIT_TEST() [5/5]

CodecvtTest::CPPUNIT_TEST ( variable_encoding  )
private

◆ CPPUNIT_TEST_SUITE()

CodecvtTest::CPPUNIT_TEST_SUITE ( CodecvtTest  )
private

◆ CPPUNIT_TEST_SUITE_END()

CodecvtTest::CPPUNIT_TEST_SUITE_END ( )
private

◆ imbue_while_reading()

void CodecvtTest::imbue_while_reading ( )
protected

Definition at line 484 of file codecvt_test.cpp.

485{
486#if !defined (STLPORT) || !(defined (_STLP_NO_WCHAR_T) || !defined (_STLP_USE_EXCEPTIONS))
487 {
488 wofstream ofs( "test.txt" );
489 const wchar_t buf[] = L" ";
490 for ( int i = 0; i < 4098; ++i ) {
491 ofs << buf[0];
492 }
493 }
494
495 wifstream ifs("test.txt"); // a file containing 4098 wchars
496
497 ifs.imbue( locale(locale(), new my_codecvt) );
498 ifs.get();
499 ifs.seekg(0);
500 ifs.imbue( locale() );
501 ifs.ignore(4096);
502 int ch = ifs.get();
503 CPPUNIT_CHECK( ch != (int)WEOF );
504#endif
505}
Definition: _locale.h:75
#define CPPUNIT_CHECK(X)
Definition: cppunit_mini.h:195
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define L(x)
Definition: ntvdm.h:50
#define WEOF
Definition: conio.h:185

◆ in_out_test()

void CodecvtTest::in_out_test ( )
protected

Definition at line 382 of file codecvt_test.cpp.

383{
384#if !defined (STLPORT) || !(defined (_STLP_NO_WCHAR_T) || !defined (_STLP_USE_EXCEPTIONS))
385 try {
386 locale loc("");
387
388 typedef codecvt<wchar_t, char, mbstate_t> cdecvt_type;
389 if (has_facet<cdecvt_type>(loc)) {
390 cdecvt_type const& cdect = use_facet<cdecvt_type>(loc);
391 {
392 cdecvt_type::state_type state;
393 memset(&state, 0, sizeof(cdecvt_type::state_type));
394 string from("abcdef");
395 const char* next_from;
396 wchar_t to[1];
397 wchar_t *next_to;
398 cdecvt_type::result res = cdect.in(state, from.data(), from.data() + from.size(), next_from,
399 to, to + sizeof(to) / sizeof(wchar_t), next_to);
400 CPPUNIT_ASSERT( res == cdecvt_type::ok );
401 CPPUNIT_ASSERT( next_from == from.data() + 1 );
402 CPPUNIT_ASSERT( next_to == &to[0] + 1 );
403 CPPUNIT_ASSERT( to[0] == L'a');
404 }
405 {
406 cdecvt_type::state_type state;
407 memset(&state, 0, sizeof(cdecvt_type::state_type));
408 wstring from(L"abcdef");
409 const wchar_t* next_from;
410 char to[1];
411 char *next_to;
412 cdecvt_type::result res = cdect.out(state, from.data(), from.data() + from.size(), next_from,
413 to, to + sizeof(to) / sizeof(char), next_to);
414 CPPUNIT_ASSERT( res == cdecvt_type::ok );
415 CPPUNIT_ASSERT( next_from == from.data() + 1 );
416 CPPUNIT_ASSERT( next_to == &to[0] + 1 );
417 CPPUNIT_ASSERT( to[0] == 'a');
418 }
419 }
420 }
421 catch (runtime_error const&) {
422 }
423 catch (...) {
425 }
426#endif
427}
static int state
Definition: maze.c:121
#define CPPUNIT_FAIL
Definition: cppunit_mini.h:206
#define CPPUNIT_ASSERT(X)
Definition: cppunit_mini.h:200
GLuint res
Definition: glext.h:9613
#define memset(x, y, z)
Definition: compat.h:39
CardRegion * from
Definition: spigame.cpp:19

◆ length_test()

void CodecvtTest::length_test ( )
protected

Definition at line 429 of file codecvt_test.cpp.

430{
431#if !defined (STLPORT) || !(defined (_STLP_NO_WCHAR_T) || !defined (_STLP_USE_EXCEPTIONS))
432 try {
433 locale loc("");
434
435 typedef codecvt<wchar_t, char, mbstate_t> cdecvt_type;
436 if (has_facet<cdecvt_type>(loc)) {
437 cdecvt_type const& cdect = use_facet<cdecvt_type>(loc);
438 {
439 cdecvt_type::state_type state;
440 memset(&state, 0, sizeof(cdecvt_type::state_type));
441 string from("abcdef");
442 int res = cdect.length(state, from.data(), from.data() + from.size(), from.size());
443 CPPUNIT_ASSERT( (size_t)res == from.size() );
444 }
445 }
446 }
447 catch (runtime_error const&) {
448 }
449 catch (...) {
451 }
452#endif
453}

◆ special_encodings()

void CodecvtTest::special_encodings ( )
protected

Definition at line 507 of file codecvt_test.cpp.

508{
509#if !defined (STLPORT) || (!defined (_STLP_NO_WCHAR_T) && defined (_STLP_USE_EXCEPTIONS))
510 {
512 codecvt<wchar_t, char, mbstate_t> const& cvt = use_facet<codecvt<wchar_t, char, mbstate_t> >(loc);
514 memset(&state, 0, sizeof(mbstate_t));
515 char c = '0';
516 const char *from_next;
517 wchar_t wc;
518 wchar_t *to_next;
519 CPPUNIT_ASSERT( cvt.in(state, &c, &c + 1, from_next, &wc, &wc, to_next) == codecvt_base::ok );
520 CPPUNIT_ASSERT( to_next == &wc );
521 CPPUNIT_ASSERT( cvt.in(state, &c, &c + 1, from_next, &wc, &wc + 1, to_next) == codecvt_base::ok );
522 CPPUNIT_ASSERT( wc == L'0' );
523 CPPUNIT_ASSERT( to_next == &wc + 1 );
524 }
525 try
526 {
527 wstring cp936_wstr;
528 const string cp936_str = "\xd6\xd0\xb9\xfa\xc9\xe7\xbb\xe1\xbf\xc6\xd1\xa7\xd4\xba\xb7\xa2\xb2\xbc\x32\x30\x30\x38\xc4\xea\xa1\xb6\xbe\xad\xbc\xc3\xc0\xb6\xc6\xa4\xca\xe9\xa1\xb7\xd6\xb8\xb3\xf6\xa3\xac\x32\x30\x30\x37\xc4\xea\xd6\xd0\xb9\xfa\xbe\xad\xbc\xc3\xd4\xf6\xb3\xa4\xd3\xc9\xc6\xab\xbf\xec\xd7\xaa\xcf\xf2\xb9\xfd\xc8\xc8\xb5\xc4\xc7\xf7\xca\xc6\xc3\xf7\xcf\xd4\xd4\xa4\xbc\xc6\xc8\xab\xc4\xea\x47\x44\x50\xd4\xf6\xcb\xd9\xbd\xab\xb4\xef\x31\x31\x2e\x36\x25\xa1\xa3";
529 locale loc(locale::classic(), ".936", locale::ctype);
530 codecvt<wchar_t, char, mbstate_t> const& cvt = use_facet<codecvt<wchar_t, char, mbstate_t> >(loc);
532 memset(&state, 0, sizeof(mbstate_t));
533
535
536 {
537 wchar_t wbuf[4096];
538 // Check we will have enough room for the generated wide string generated from the whole char buffer:
539 int len = cvt.length(state, cp936_str.data(), cp936_str.data() + cp936_str.size(), sizeof(wbuf) / sizeof(wchar_t));
540 CPPUNIT_ASSERT( cp936_str.size() == (size_t)len );
541
542 const char *from_next;
543 wchar_t *to_next;
544 res = cvt.in(state, cp936_str.data(), cp936_str.data() + cp936_str.size(), from_next,
545 wbuf, wbuf + sizeof(wbuf) / sizeof(wchar_t), to_next);
547 CPPUNIT_ASSERT( from_next == cp936_str.data() + cp936_str.size() );
548 cp936_wstr.assign(wbuf, to_next);
549 }
550
551 {
552 const wchar_t *from_next;
553 char buf[4096];
554 char *to_next;
555 res = cvt.out(state, cp936_wstr.data(), cp936_wstr.data() + cp936_wstr.size(), from_next,
556 buf, buf + sizeof(buf), to_next);
558 CPPUNIT_CHECK( string(buf, to_next) == cp936_str );
559 }
560 }
561 catch (const runtime_error&)
562 {
563 CPPUNIT_MESSAGE("Not enough platform localization support to check 936 code page encoding.");
564 }
565 try
566 {
567 const string utf8_str = "\xe4\xb8\xad\xe5\x9b\xbd\xe7\xa4\xbe\xe4\xbc\x9a\xe7\xa7\x91\xe5\xad\xa6\xe9\x99\xa2\xe5\x8f\x91\xe5\xb8\x83\x32\x30\x30\x38\xe5\xb9\xb4\xe3\x80\x8a\xe7\xbb\x8f\xe6\xb5\x8e\xe8\x93\x9d\xe7\x9a\xae\xe4\xb9\xa6\xe3\x80\x8b\xe6\x8c\x87\xe5\x87\xba\xef\xbc\x8c\x32\x30\x30\x37\xe5\xb9\xb4\xe4\xb8\xad\xe5\x9b\xbd\xe7\xbb\x8f\xe6\xb5\x8e\xe5\xa2\x9e\xe9\x95\xbf\xe7\x94\xb1\xe5\x81\x8f\xe5\xbf\xab\xe8\xbd\xac\xe5\x90\x91\xe8\xbf\x87\xe7\x83\xad\xe7\x9a\x84\xe8\xb6\x8b\xe5\x8a\xbf\xe6\x98\x8e\xe6\x98\xbe\xe9\xa2\x84\xe8\xae\xa1\xe5\x85\xa8\xe5\xb9\xb4\x47\x44\x50\xe5\xa2\x9e\xe9\x80\x9f\xe5\xb0\x86\xe8\xbe\xbe\x31\x31\x2e\x36\x25\xe3\x80\x82";
568 wstring utf8_wstr;
570 codecvt<wchar_t, char, mbstate_t> const& cvt = use_facet<codecvt<wchar_t, char, mbstate_t> >(loc);
572 memset(&state, 0, sizeof(mbstate_t));
573
575
576 {
577 wchar_t wbuf[4096];
578 // Check we will have enough room for the wide string generated from the whole char buffer:
579 int len = cvt.length(state, utf8_str.data(), utf8_str.data() + utf8_str.size(), sizeof(wbuf) / sizeof(wchar_t));
580 CPPUNIT_ASSERT( utf8_str.size() == (size_t)len );
581
582 const char *from_next;
583 wchar_t *to_next;
584 res = cvt.in(state, utf8_str.data(), utf8_str.data() + utf8_str.size(), from_next,
585 wbuf, wbuf + sizeof(wbuf) / sizeof(wchar_t), to_next);
587 CPPUNIT_ASSERT( from_next == utf8_str.data() + utf8_str.size() );
588 utf8_wstr.assign(wbuf, to_next);
589
590 // Try to read one char after the other:
591 wchar_t wc;
592 const char* from = utf8_str.data();
593 const char* from_end = from + utf8_str.size();
594 from_next = utf8_str.data();
595 size_t length = 1;
596 size_t windex = 0;
597 while (from + length <= from_end) {
598 res = cvt.in(state, from, from + length, from_next,
599 &wc, &wc + 1, to_next);
600 switch (res) {
601 case codecvt_base::ok:
602 // reset length:
603 from = from_next;
604 length = 1;
605 CPPUNIT_ASSERT( wc == utf8_wstr[windex++] );
606 wc = 0;
607 break;
609 if (from_next == from)
610 // from_next hasn't move so we have to pass more chars
611 ++length;
612 else
613 // char between from and from_next has been eaten, we simply restart
614 // conversion from from_next:
615 from = from_next;
616 continue;
620 //break;
621 }
622 }
623 CPPUNIT_ASSERT( windex == utf8_wstr.size() );
624 }
625
626 {
627 const wchar_t *from_next;
628 char buf[4096];
629 char *to_next;
630 res = cvt.out(state, utf8_wstr.data(), utf8_wstr.data() + utf8_wstr.size(), from_next,
631 buf, buf + sizeof(buf), to_next);
633 CPPUNIT_CHECK( string(buf, to_next) == utf8_str );
634 }
635
636 {
637 // Check that an obviously wrong UTF8 encoded string is correctly detected:
638 const string bad_utf8_str("\xdf\xdf\xdf\xdf\xdf");
639 wchar_t wc;
640 const char *from_next;
641 wchar_t *to_next;
642 res = cvt.in(state, bad_utf8_str.data(), bad_utf8_str.data() + bad_utf8_str.size(), from_next,
643 &wc, &wc + 1, to_next);
645 }
646 }
647 catch (const runtime_error&)
648 {
649 CPPUNIT_MESSAGE("Not enough platform localization support to check UTF8 encoding.");
650 }
651#endif
652}
size_type size() const
Definition: _string.h:400
const _CharT * data() const
Definition: _string.h:950
static const locale &_STLP_CALL classic()
Definition: locale.cpp:404
#define CPPUNIT_MESSAGE(m)
Definition: cppunit_mini.h:223
static char * cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag)
Definition: fcvtbuf.c:45
const GLubyte * c
Definition: glext.h:8905
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
GLenum GLsizei len
Definition: glext.h:6722

◆ variable_encoding()

void CodecvtTest::variable_encoding ( )
protected

Definition at line 289 of file codecvt_test.cpp.

290{
291#if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES)
292 //We first generate the file used for test:
293 const char* fileName = "test_file.txt";
294 {
295 ofstream ostr(fileName);
296 //Maybe we simply do not have write access to repository
297 CPPUNIT_ASSERT( ostr.good() );
298 for (int i = 0; i < 2048; ++i) {
299 ostr << "0123456789";
300 }
301 CPPUNIT_ASSERT( ostr.good() );
302 }
303
304 {
305 ifstream istr(fileName);
306 CPPUNIT_ASSERT( istr.good() );
307 CPPUNIT_ASSERT( !istr.eof() );
308
309 eater_codecvt codec(1);
310 locale loc(locale::classic(), &codec);
311
312 istr.imbue(loc);
313 CPPUNIT_ASSERT( istr.good() );
314 CPPUNIT_ASSERT( (int)istr.tellg() == 0 );
315
316 int theoricalPos = 0;
317 do {
318 int c = istr.get();
320 break;
321 }
322 ++theoricalPos;
323 if (c == 'a') {
324 ++theoricalPos;
325 }
326
327 CPPUNIT_ASSERT( (int)istr.tellg() == theoricalPos );
328 }
329 while (!istr.eof());
330
331 CPPUNIT_ASSERT( istr.eof() );
332 }
333
334# if 0
335 /* This test is broken, not sure if it is really possible to get a position in
336 * a locale having a codecvt such as generator_codecvt. Maybe generator_codecvt
337 * is not a valid theorical example of codecvt implementation. */
338 {
339 ifstream istr(fileName);
340 CPPUNIT_ASSERT( istr.good() );
341 CPPUNIT_ASSERT( !istr.eof() );
342
343 generator_codecvt codec(1);
344 locale loc(locale::classic(), &codec);
345
346 istr.imbue(loc);
347 CPPUNIT_ASSERT( istr.good() );
348 CPPUNIT_ASSERT( (int)istr.tellg() == 0 );
349
350 int theoricalPos = 0;
351 int theoricalTellg;
352 do {
353 char c = istr.get();
354 if (c == char_traits<char>::eof()) {
355 break;
356 }
357 switch (c) {
358 case 'a':
359 case 'b':
360 theoricalTellg = -1;
361 break;
362 case 'c':
363 ++theoricalPos;
364 default:
365 ++theoricalPos;
366 theoricalTellg = theoricalPos;
367 break;
368 }
369
370 if ((int)istr.tellg() != theoricalTellg) {
371 CPPUNIT_ASSERT( (int)istr.tellg() == theoricalTellg );
372 }
373 }
374 while (!istr.eof());
375
376 CPPUNIT_ASSERT( istr.eof() );
377 }
378# endif
379#endif
380}

Member Data Documentation

◆ CPPUNIT_STOP_IGNORE

CodecvtTest::CPPUNIT_STOP_IGNORE
private

Definition at line 25 of file codecvt_test.cpp.


The documentation for this class was generated from the following file: