Debian Codegraph @

Source/Tests/winchar.cpp

raw
1 // Reviewed for Unicode support by Jim Park -- 08/13/2007
2 #include <cppunit/extensions/HelperMacros.h>
3 #include "../util.h"
4 #include "../winchar.h"
5
6 #include <time.h>
7 #include <stdlib.h>
8
9 // macro for fixing endianness
10 #define _x(x) FIX_ENDIAN_INT16(WCHAR(x))
11
12 // BUGBUG: These tests currently run as Ansi, it would be better if it respected defenv['UNICODE']
13
14 // BUGBUG: WinWStrDupFromWC is unable to test WCToUTF16LEHlpr because it is behind #ifdef MAKENSIS
15
16 // TODO write equal() for WINWCHAR -- http://subcommanderblog.wordpress.com/2009/01/10/cppunit_assert_equal-and-custom-data-types/
17
18 class WinCharTest : public CppUnit::TestFixture {
19
20 CPPUNIT_TEST_SUITE( WinCharTest );
21 CPPUNIT_TEST( testFromTchar );
22 CPPUNIT_TEST( testStrCpy );
23 CPPUNIT_TEST( testStrNCpy );
24 CPPUNIT_TEST( testStrLen );
25 CPPUNIT_TEST( testStrCmp );
26 CPPUNIT_TEST( testStrDup );
27 CPPUNIT_TEST( testWin32Emulation );
28 CPPUNIT_TEST_SUITE_END();
29
30 public:
31 void setUp() {
32 }
33
34 void testFromTchar() {
35 WINWCHAR test[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 };
36
37 WINWCHAR *dyn = WinWStrDupFromTChar(_T("test"));
38 CPPUNIT_ASSERT_EQUAL( 0, memcmp(test, dyn, sizeof(test)) );
39 free(dyn);
40
41 dyn = WinWStrDupFromChar("test");
42 CPPUNIT_ASSERT_EQUAL( 0, memcmp(test, dyn, sizeof(test)) );
43 free(dyn);
44
45 dyn = WinWStrDupFromWC(L"test");
46 CPPUNIT_ASSERT_EQUAL( 0, memcmp(test, dyn, sizeof(test)) );
47 free(dyn);
48 }
49
50 void testStrCpy() {
51 WINWCHAR a[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 };
52 WINWCHAR b[5];
53
54 CPPUNIT_ASSERT( !WinWStrCmp(b, WinWStrCpy(b, a)) );
55 CPPUNIT_ASSERT_EQUAL( 0, memcmp(a, b, 5 * sizeof(WINWCHAR)) );
56 }
57
58 void testStrNCpy() {
59 WINWCHAR a1[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 };
60 WINWCHAR b[5];
61
62 CPPUNIT_ASSERT( !WinWStrCmp(b, WinWStrNCpy(b, a1, 5)) );
63 CPPUNIT_ASSERT_EQUAL( 0, memcmp(a1, b, 5 * sizeof(WINWCHAR)) );
64
65 WINWCHAR a2[] = { _x('t'), _x('e'), 0, 0, 0 };
66
67 CPPUNIT_ASSERT( !WinWStrCmp(b, WinWStrNCpy(b, a2, 5)) );
68 CPPUNIT_ASSERT_EQUAL( 0, memcmp(a2, b, 5 * sizeof(WINWCHAR)) );
69
70 CPPUNIT_ASSERT( !WinWStrCmp(b, WinWStrNCpy(b, a1, 2)) );
71 CPPUNIT_ASSERT_EQUAL( 0, memcmp(a2, b, 5 * sizeof(WINWCHAR)) );
72 }
73
74 void testStrLen() {
75 WINWCHAR test[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 };
76
77 CPPUNIT_ASSERT_EQUAL( (size_t) 4, WinWStrLen(test) );
78 }
79
80 static int simplifyNumber(int n) {
81 if (n < 0)
82 return -1;
83 if (n > 0)
84 return 1;
85 return 0;
86 }
87
88 void testStrCmp() {
89 char a[] = "a";
90 WINWCHAR wa[] = { _x('a'), 0 };
91 char b[] = "b";
92 WINWCHAR wb[] = { _x('b'), 0 };
93 char empty[] = "";
94 WINWCHAR wempty[] = { 0 };
95
96 #define TEST_STR_CMP(x, y) \
97 CPPUNIT_ASSERT_EQUAL(\
98 simplifyNumber(strcmp(x, y)), \
99 simplifyNumber(WinWStrCmp(w##x, w##y)) \
100 )
101
102 TEST_STR_CMP(a, b);
103 TEST_STR_CMP(b, a);
104 TEST_STR_CMP(a, a);
105 TEST_STR_CMP(b, b);
106 TEST_STR_CMP(a, empty);
107 TEST_STR_CMP(empty, b);
108 TEST_STR_CMP(empty, empty);
109 }
110
111 void testStrDup() {
112 WINWCHAR a[] = { _x('a'), _x('b'), _x('c'), 0 };
113
114 WINWCHAR *b = WinWStrDupFromWinWStr(a);
115
116 CPPUNIT_ASSERT_EQUAL( 0, WinWStrCmp(a, b) );
117
118 free(b);
119 }
120
121 void testWin32Emulation() {
122 char nb[42], narrowTest[] = "Test";
123 wchar_t wb[42], wideTest[] = L"Test";
124 int c, c2;
125
126 c = MultiByteToWideChar(0, 0, "Test", -1, NULL, 0);
127 CPPUNIT_ASSERT( c == 5 );
128 c = MultiByteToWideChar(0, 0, "Test", c2 = 3, NULL, 0);
129 CPPUNIT_ASSERT( c == c2 );
130 c = MultiByteToWideChar(0, 0, "Test", c2 = 4, NULL, 0);
131 CPPUNIT_ASSERT( c == c2 );
132 #ifndef __APPLE__ // BUGBUG: This is broken on MacOS 14 & 15
133 c = MultiByteToWideChar(0, 0, "Test", c2 = 5, NULL, 0);
134 CPPUNIT_ASSERT( c == c2 );
135 #endif
136 MultiByteToWideChar(0, 0, "Test", -1, wb, COUNTOF(wb));
137 CPPUNIT_ASSERT_EQUAL( 0, memcmp(wideTest, wb, 5 * sizeof(*wb)) );
138
139 c = WideCharToMultiByte(0, 0, L"Test", -1, NULL, 0, NULL, NULL);
140 CPPUNIT_ASSERT( c == 5 );
141 c = WideCharToMultiByte(0, 0, L"Test", c2 = 3, NULL, 0, NULL, NULL);
142 CPPUNIT_ASSERT( c == c2 );
143 c = WideCharToMultiByte(0, 0, L"Test", c2 = 4, NULL, 0, NULL, NULL);
144 CPPUNIT_ASSERT( c == c2 );
145 #ifndef __APPLE__ // BUGBUG: This is broken on MacOS 14 & 15
146 c = WideCharToMultiByte(0, 0, L"Test", c2 = 5, NULL, 0, NULL, NULL);
147 CPPUNIT_ASSERT( c == c2 );
148 #endif
149 WideCharToMultiByte(0, 0, L"Test", -1, nb, COUNTOF(nb), NULL, NULL);
150 CPPUNIT_ASSERT_EQUAL( 0, memcmp(narrowTest, nb, 5 * sizeof(*nb)) );
151 }
152
153 };
154
155 CPPUNIT_TEST_SUITE_REGISTRATION( WinCharTest );