File: test-qtextcodec-convert.cpp

package info (click to toggle)
goldendict-webengine 23.02.05-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,148 kB
  • sloc: cpp: 58,537; javascript: 9,942; ansic: 9,242; xml: 41; makefile: 15; sh: 9
file content (49 lines) | stat: -rw-r--r-- 1,269 bytes parent folder | download
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
#include <QTest>
#include <QDate>
#include "../iconv.hh"
#include <string>
#include "../wstring_qt.hh"

//used to test Iconv.cc
class testQTextCodec : public QObject
{
  Q_OBJECT
private slots:
  void testConvert();
  void testToWstring();
  void testToUtf8();
};

void testQTextCodec::testConvert()
{
  Iconv conv( "utf-8", Iconv::GdWchar );
  const char s[]  = { 0x61, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00 };
  void const * in = &s[ 0 ];
  size_t len      = 12;
  QString r       = conv.convert( in, len );
  QCOMPARE( r, "abc" );
}

void testQTextCodec::testToWstring()
{
  const char s[] = { 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x63 };
  gd::wstring r1 = Iconv::toWstring( "UTF-32BE", s, 12 );

  QCOMPARE( r1.size(), 3 );
  QCOMPARE( r1, U"abc" );
  char32_t * arr = (char32_t*)r1.c_str ();
  QCOMPARE( arr[ 0 ], 0x00000061 );
}

void testQTextCodec::testToUtf8()
{
  const char s[] = { 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x63 };
  std::string r1 = Iconv::toUtf8 ( "UTF-32BE", s, 12 );

  QCOMPARE( r1.size(), 3 );
  QCOMPARE( r1, u8"abc" );
  char * arr = (char*)r1.c_str ();
  QCOMPARE( arr[ 0 ], 0x61 );
}
QTEST_MAIN(testQTextCodec)
#include "test-qtextcodec-convert.moc"