• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  * COPYRIGHT:
3  * Copyright (c) 1997-2005, International Business Machines Corporation and
4  * others. All Rights Reserved.
5  ********************************************************************/
6 /*   file name:  strtest.cpp
7 *   encoding:   US-ASCII
8 *   tab size:   8 (not used)
9 *   indentation:4
10 *
11 *   created on: 1999nov22
12 *   created by: Markus W. Scherer
13 */
14 
15 #include "unicode/utypes.h"
16 #include "unicode/putil.h"
17 #include "intltest.h"
18 #include "strtest.h"
19 #include "unicode/ustring.h"
20 
21 #if defined(U_WINDOWS) && defined(_MSC_VER)
22 #include <vector>
23 using namespace std;
24 #endif
25 
~StringTest()26 StringTest::~StringTest() {}
27 
TestEndian(void)28 void StringTest::TestEndian(void) {
29     union {
30         uint8_t byte;
31         uint16_t word;
32     } u;
33     u.word=0x0100;
34     if(U_IS_BIG_ENDIAN!=u.byte) {
35         errln("TestEndian: U_IS_BIG_ENDIAN needs to be fixed in platform.h");
36     }
37 }
38 
TestSizeofTypes(void)39 void StringTest::TestSizeofTypes(void) {
40     if(U_SIZEOF_WCHAR_T!=sizeof(wchar_t)) {
41         errln("TestSizeofWCharT: U_SIZEOF_WCHAR_T!=sizeof(wchar_t) - U_SIZEOF_WCHAR_T needs to be fixed in platform.h");
42     }
43 #ifdef U_INT64_T_UNAVAILABLE
44     errln("int64_t and uint64_t are undefined.");
45 #else
46     if(8!=sizeof(int64_t)) {
47         errln("TestSizeofTypes: 8!=sizeof(int64_t) - int64_t needs to be fixed in platform.h");
48     }
49     if(8!=sizeof(uint64_t)) {
50         errln("TestSizeofTypes: 8!=sizeof(uint64_t) - uint64_t needs to be fixed in platform.h");
51     }
52 #endif
53     if(8!=sizeof(double)) {
54         errln("8!=sizeof(double) - putil.c code may not work");
55     }
56     if(4!=sizeof(int32_t)) {
57         errln("4!=sizeof(int32_t)");
58     }
59     if(4!=sizeof(uint32_t)) {
60         errln("4!=sizeof(uint32_t)");
61     }
62     if(2!=sizeof(int16_t)) {
63         errln("2!=sizeof(int16_t)");
64     }
65     if(2!=sizeof(uint16_t)) {
66         errln("2!=sizeof(uint16_t)");
67     }
68     if(2!=sizeof(UChar)) {
69         errln("2!=sizeof(UChar)");
70     }
71     if(1!=sizeof(int8_t)) {
72         errln("1!=sizeof(int8_t)");
73     }
74     if(1!=sizeof(uint8_t)) {
75         errln("1!=sizeof(uint8_t)");
76     }
77     if(1!=sizeof(UBool)) {
78         errln("1!=sizeof(UBool)");
79     }
80 }
81 
TestCharsetFamily(void)82 void StringTest::TestCharsetFamily(void) {
83     unsigned char c='A';
84     if( U_CHARSET_FAMILY==U_ASCII_FAMILY && c!=0x41 ||
85         U_CHARSET_FAMILY==U_EBCDIC_FAMILY && c!=0xc1
86     ) {
87         errln("TestCharsetFamily: U_CHARSET_FAMILY needs to be fixed in platform.h");
88     }
89 }
90 
91 U_STRING_DECL(ustringVar, "aZ0 -", 5);
92 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)93 void StringTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
94     if(exec) {
95         logln("TestSuite Character and String Test: ");
96     }
97     switch(index) {
98     case 0:
99         name="TestEndian";
100         if(exec) {
101             TestEndian();
102         }
103         break;
104     case 1:
105         name="TestSizeofTypes";
106         if(exec) {
107             TestSizeofTypes();
108         }
109         break;
110     case 2:
111         name="TestCharsetFamily";
112         if(exec) {
113             TestCharsetFamily();
114         }
115         break;
116     case 3:
117         name="Test_U_STRING";
118         if(exec) {
119             U_STRING_INIT(ustringVar, "aZ0 -", 5);
120             if( sizeof(ustringVar)/sizeof(*ustringVar)!=6 ||
121                 ustringVar[0]!=0x61 ||
122                 ustringVar[1]!=0x5a ||
123                 ustringVar[2]!=0x30 ||
124                 ustringVar[3]!=0x20 ||
125                 ustringVar[4]!=0x2d ||
126                 ustringVar[5]!=0
127             ) {
128                 errln("Test_U_STRING: U_STRING_DECL with U_STRING_INIT does not work right! "
129                       "See putil.h and utypes.h with platform.h.");
130             }
131         }
132         break;
133     case 4:
134         name="Test_UNICODE_STRING";
135         if(exec) {
136             UnicodeString ustringVar=UNICODE_STRING("aZ0 -", 5);
137             if( ustringVar.length()!=5 ||
138                 ustringVar[0]!=0x61 ||
139                 ustringVar[1]!=0x5a ||
140                 ustringVar[2]!=0x30 ||
141                 ustringVar[3]!=0x20 ||
142                 ustringVar[4]!=0x2d
143             ) {
144                 errln("Test_UNICODE_STRING: UNICODE_STRING does not work right! "
145                       "See unistr.h and utypes.h with platform.h.");
146             }
147         }
148         break;
149     case 5:
150         name="Test_UNICODE_STRING_SIMPLE";
151         if(exec) {
152             UnicodeString ustringVar=UNICODE_STRING_SIMPLE("aZ0 -");
153             if( ustringVar.length()!=5 ||
154                 ustringVar[0]!=0x61 ||
155                 ustringVar[1]!=0x5a ||
156                 ustringVar[2]!=0x30 ||
157                 ustringVar[3]!=0x20 ||
158                 ustringVar[4]!=0x2d
159             ) {
160                 errln("Test_UNICODE_STRING_SIMPLE: UNICODE_STRING_SIMPLE does not work right! "
161                       "See unistr.h and utypes.h with platform.h.");
162             }
163         }
164         break;
165     case 6:
166         name="Test_UTF8_COUNT_TRAIL_BYTES";
167         if(exec) {
168             if(UTF8_COUNT_TRAIL_BYTES(0x7F) != 0
169                 || UTF8_COUNT_TRAIL_BYTES(0xC0) != 1
170                 || UTF8_COUNT_TRAIL_BYTES(0xE0) != 2
171                 || UTF8_COUNT_TRAIL_BYTES(0xF0) != 3)
172             {
173                 errln("Test_UTF8_COUNT_TRAIL_BYTES: UTF8_COUNT_TRAIL_BYTES does not work right! "
174                       "See utf8.h.");
175             }
176         }
177         break;
178     case 7:
179         name="TestSTLCompatibility";
180         if(exec) {
181 #if defined(U_WINDOWS) && defined(_MSC_VER)
182             /* Just make sure that it compiles with STL's placement new usage. */
183             vector<UnicodeString> myvect;
184             myvect.push_back(UnicodeString("blah"));
185 #endif
186         }
187         break;
188     default:
189         name="";
190         break;
191     }
192 }
193