~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/test/intltest/tfsmalls.cpp

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/********************************************************************
3
 
 * COPYRIGHT: 
4
 
 * Copyright (c) 1997-2001, International Business Machines Corporation and
5
 
 * others. All Rights Reserved.
6
 
 ********************************************************************/
7
 
 
8
 
 
9
 
#include "unicode/utypes.h"
10
 
 
11
 
#include "intltest.h"
12
 
#include "tfsmalls.h"
13
 
 
14
 
#include "unicode/msgfmt.h"
15
 
#include "unicode/choicfmt.h"
16
 
 
17
 
#include "unicode/parsepos.h"
18
 
#include "unicode/fieldpos.h"
19
 
#include "unicode/fmtable.h"
20
 
 
21
 
 
22
 
/*static UBool chkstatus( UErrorCode &status, char* msg = NULL )
23
 
{
24
 
    UBool ok = (status == U_ZERO_ERROR);
25
 
    if (!ok) it_errln( msg );
26
 
    return ok;
27
 
}*/
28
 
 
29
 
void test_ParsePosition( void )
30
 
{
31
 
    ParsePosition* pp1 = new ParsePosition();
32
 
    if (pp1 && (pp1->getIndex() == 0)) {
33
 
        it_out << "PP constructor() tested." << endl;
34
 
    }else{
35
 
        it_errln("*** PP getIndex or constructor() result");
36
 
    }
37
 
    delete pp1;
38
 
 
39
 
 
40
 
    {
41
 
        int32_t to = 5;
42
 
        ParsePosition pp2( to );
43
 
        if (pp2.getIndex() == 5) {
44
 
            it_out << "PP getIndex and constructor(int32_t) tested." << endl;
45
 
        }else{
46
 
            it_errln("*** PP getIndex or constructor(int32_t) result");
47
 
        }
48
 
        pp2.setIndex( 3 );
49
 
        if (pp2.getIndex() == 3) {
50
 
            it_out << "PP setIndex tested." << endl;
51
 
        }else{
52
 
            it_errln("*** PP getIndex or setIndex result");
53
 
        }
54
 
    }
55
 
 
56
 
    ParsePosition pp2, pp3;
57
 
    pp2 = 3;
58
 
    pp3 = 5;
59
 
    ParsePosition pp4( pp3 );
60
 
    if ((pp2 != pp3) && (pp3 == pp4)) {
61
 
        it_out << "PP copy contructor, operator== and operator != tested." << endl;
62
 
    }else{
63
 
        it_errln("*** PP operator== or operator != result");
64
 
    }
65
 
 
66
 
    ParsePosition pp5;
67
 
    pp5 = pp4;
68
 
    if ((pp4 == pp5) && (!(pp4 != pp5))) {
69
 
        it_out << "PP operator= tested." << endl;
70
 
    }else{
71
 
        it_errln("*** PP operator= operator== or operator != result");
72
 
    }
73
 
 
74
 
 
75
 
}
76
 
 
77
 
#include "unicode/decimfmt.h"
78
 
 
79
 
void test_FieldPosition_example( void )
80
 
{
81
 
    //***** no error detection yet !!!!!!!
82
 
    //***** this test is for compiler checks and visual verification only.
83
 
    double doubleNum[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789,
84
 
        12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
85
 
    int32_t dNumSize = (int32_t)(sizeof(doubleNum)/sizeof(double));
86
 
 
87
 
    UErrorCode status = U_ZERO_ERROR;
88
 
    DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
89
 
    fmt->setDecimalSeparatorAlwaysShown(TRUE);
90
 
    
91
 
    const int32_t tempLen = 20;
92
 
    char temp[tempLen];
93
 
    
94
 
    for (int32_t i=0; i<dNumSize; i++) {
95
 
        FieldPosition pos(NumberFormat::INTEGER_FIELD);
96
 
        UnicodeString buf;
97
 
        //char fmtText[tempLen];
98
 
        //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
99
 
        UnicodeString res = fmt->format(doubleNum[i], buf, pos);
100
 
        for (int32_t j=0; j<tempLen; j++) temp[j] = '='; // clear with spaces
101
 
        int32_t tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ? 
102
 
            tempLen : (tempLen - pos.getEndIndex());
103
 
        temp[tempOffset] = '\0';
104
 
        it_out << "FP " << temp << res << endl;
105
 
    }
106
 
    delete fmt;
107
 
    
108
 
    it_out << endl;
109
 
 
110
 
}
111
 
 
112
 
void test_FieldPosition( void )
113
 
{
114
 
 
115
 
    FieldPosition fp( 7 );
116
 
 
117
 
    if (fp.getField() == 7) {
118
 
        it_out << "FP constructor(int32_t) and getField tested." << endl;
119
 
    }else{
120
 
        it_errln("*** FP constructor(int32_t) or getField");
121
 
    }
122
 
 
123
 
    FieldPosition* fph = new FieldPosition( 3 );
124
 
    if ( fph->getField() != 3) it_errln("*** FP getField or heap constr.");
125
 
    delete fph;
126
 
 
127
 
    UBool err1 = FALSE;
128
 
    UBool err2 = FALSE;
129
 
    UBool err3 = FALSE;
130
 
    for (int32_t i = -50; i < 50; i++ ) {
131
 
        fp.setField( i+8 );
132
 
        fp.setBeginIndex( i+6 );
133
 
        fp.setEndIndex( i+7 );
134
 
        if (fp.getField() != i+8)  err1 = TRUE;
135
 
        if (fp.getBeginIndex() != i+6) err2 = TRUE;
136
 
        if (fp.getEndIndex() != i+7) err3 = TRUE;
137
 
    }
138
 
    if (!err1) {
139
 
        it_out << "FP setField and getField tested." << endl;
140
 
    }else{
141
 
        it_errln("*** FP setField or getField");
142
 
    }
143
 
    if (!err2) {
144
 
        it_out << "FP setBeginIndex and getBeginIndex tested." << endl;
145
 
    }else{
146
 
        it_errln("*** FP setBeginIndex or getBeginIndex");
147
 
    }
148
 
    if (!err3) {
149
 
        it_out << "FP setEndIndex and getEndIndex tested." << endl;
150
 
    }else{
151
 
        it_errln("*** FP setEndIndex or getEndIndex");
152
 
    }
153
 
 
154
 
    it_out << endl;
155
 
 
156
 
}
157
 
 
158
 
void test_Formattable( void )
159
 
{
160
 
    Formattable* ftp = new Formattable();
161
 
    if (!ftp || !(ftp->getType() == Formattable::kLong) || !(ftp->getLong() == 0)) {
162
 
        it_errln("*** Formattable constructor or getType or getLong");
163
 
    }
164
 
    delete ftp;
165
 
 
166
 
    Formattable fta, ftb;
167
 
    fta.setLong(1); ftb.setLong(2);
168
 
    if ((fta != ftb) || !(fta == ftb)) {
169
 
        it_out << "FT setLong, operator== and operator!= tested." << endl;
170
 
    }else{
171
 
        it_errln("*** Formattable setLong or operator== or !=");
172
 
    }
173
 
    fta = ftb;
174
 
    if ((fta == ftb) || !(fta != ftb)) {
175
 
        it_out << "FT operator= tested." << endl;
176
 
    }else{
177
 
        it_errln("*** FT operator= or operator== or operator!=");
178
 
    }
179
 
    
180
 
    fta.setDouble( 3.0 );
181
 
    if ((fta.getType() == Formattable::kDouble) && (fta.getDouble() == 3.0)) {
182
 
        it_out << "FT set- and getDouble tested." << endl;
183
 
    }else{
184
 
        it_errln("*** FT set- or getDouble");
185
 
    }
186
 
 
187
 
    fta.setDate( 4.0 );
188
 
    if ((fta.getType() == Formattable::kDate) && (fta.getDate() == 4.0)) {
189
 
        it_out << "FT set- and getDate tested." << endl;
190
 
    }else{
191
 
        it_errln("*** FT set- or getDate");
192
 
    }
193
 
 
194
 
    fta.setString("abc");
195
 
    UnicodeString res;
196
 
    if ((fta.getType() == Formattable::kString) && (fta.getString(res) == "abc")) {
197
 
        it_out << "FT set- and getString tested." << endl;
198
 
    }else{
199
 
        it_errln("*** FT set- or getString");
200
 
    }
201
 
 
202
 
 
203
 
    UnicodeString ucs = "unicode-string";
204
 
    UnicodeString* ucs_ptr = new UnicodeString("pointed-to-unicode-string");
205
 
 
206
 
    const Formattable ftarray[] = 
207
 
    {
208
 
        Formattable( 1.0, Formattable::kIsDate ),
209
 
        2.0,
210
 
        (int32_t)3,
211
 
        ucs,
212
 
        ucs_ptr
213
 
    };
214
 
    const int32_t ft_cnt = (int32_t)(sizeof(ftarray) / sizeof(Formattable));
215
 
    Formattable ft_arr( ftarray, ft_cnt );
216
 
    UnicodeString temp;
217
 
    if ((ft_arr[0].getType() == Formattable::kDate)   && (ft_arr[0].getDate()   == 1.0)
218
 
     && (ft_arr[1].getType() == Formattable::kDouble) && (ft_arr[1].getDouble() == 2.0)
219
 
     && (ft_arr[2].getType() == Formattable::kLong)   && (ft_arr[2].getLong()   == (int32_t)3)
220
 
     && (ft_arr[3].getType() == Formattable::kString) && (ft_arr[3].getString(temp) == ucs)
221
 
     && (ft_arr[4].getType() == Formattable::kString) && (ft_arr[4].getString(temp) == *ucs_ptr) ) {
222
 
        it_out << "FT constr. for date, double, long, ustring, ustring* and array tested" << endl;
223
 
    }else{
224
 
        it_errln("*** FT constr. for date, double, long, ustring, ustring* or array");
225
 
    }
226
 
 
227
 
    int32_t res_cnt;
228
 
    const Formattable* res_array = ft_arr.getArray( res_cnt );
229
 
    if (res_cnt == ft_cnt) {
230
 
        UBool same  = TRUE;
231
 
        for (int32_t i = 0; i < res_cnt; i++ ) {
232
 
            if (res_array[i] != ftarray[i]) {
233
 
                same = FALSE;
234
 
            }
235
 
        }
236
 
        if (same) {
237
 
            it_out << "FT getArray tested" << endl;
238
 
        }else{
239
 
            it_errln("*** FT getArray comparison");
240
 
        }
241
 
    }else{
242
 
        it_out << res_cnt << " " << ft_cnt << endl;
243
 
        it_errln("*** FT getArray count");
244
 
    }
245
 
 
246
 
    const Formattable ftarr1[] = { Formattable( (int32_t)1 ), Formattable( (int32_t)2 ) };
247
 
    const Formattable ftarr2[] = { Formattable( (int32_t)3 ), Formattable( (int32_t)4 ) };
248
 
 
249
 
    const int32_t ftarr1_cnt = (int32_t)(sizeof(ftarr1) / sizeof(Formattable));
250
 
    const int32_t ftarr2_cnt = (int32_t)(sizeof(ftarr2) / sizeof(Formattable));
251
 
 
252
 
    ft_arr.setArray( ftarr1, ftarr1_cnt );
253
 
    if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)1)) {
254
 
        it_out << "FT setArray tested" << endl;
255
 
    }else{
256
 
        it_errln("*** FT setArray");
257
 
    }
258
 
 
259
 
    Formattable* ft_dynarr = new Formattable[ftarr2_cnt];
260
 
    for (int32_t i = 0; i < ftarr2_cnt; i++ ) {
261
 
        ft_dynarr[i] = ftarr2[i];
262
 
    }
263
 
    if ((ft_dynarr[0].getType() == Formattable::kLong) && (ft_dynarr[0].getLong() == (int32_t)3)
264
 
     && (ft_dynarr[1].getType() == Formattable::kLong) && (ft_dynarr[1].getLong() == (int32_t)4)) {
265
 
        it_out << "FT operator= and array operations tested" << endl;
266
 
    }else{
267
 
        it_errln("*** FT operator= or array operations");
268
 
    }
269
 
 
270
 
    ft_arr.adoptArray( ft_dynarr, ftarr2_cnt );
271
 
    if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)3)
272
 
     && (ft_arr[1].getType() == Formattable::kLong) && (ft_arr[1].getLong() == (int32_t)4)) {
273
 
        it_out << "FT adoptArray tested" << endl;
274
 
    }else{
275
 
        it_errln("*** FT adoptArray or operator[]");
276
 
    }
277
 
 
278
 
    ft_arr.setLong(0);   // calls 'dispose' and deletes adopted array !
279
 
 
280
 
    UnicodeString* ucs_dyn = new UnicodeString("ttt");
281
 
    UnicodeString tmp2;
282
 
 
283
 
    fta.adoptString( ucs_dyn );
284
 
    if ((fta.getType() == Formattable::kString) && (fta.getString(tmp2) == "ttt")) {
285
 
        it_out << "FT adoptString tested" << endl;
286
 
    }else{
287
 
        it_errln("*** FT adoptString or getString");
288
 
    }
289
 
    fta.setLong(0);   // calls 'dispose' and deletes adopted string !
290
 
 
291
 
    it_out << endl;
292
 
 
293
 
}
294
 
 
295
 
void TestFormatSmallClasses::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
296
 
{
297
 
    switch (index) {
298
 
        case 0: name = "pp"; 
299
 
                if (exec) logln("TestSuite Format/SmallClasses/ParsePosition (f/chc/sma/pp): ");
300
 
                if (exec) test_ParsePosition(); 
301
 
                break;
302
 
        case 1: name = "fp"; 
303
 
                if (exec) logln("TestSuite Format/SmallClasses/FieldPosition (f/chc/sma/fp): ");
304
 
                if (exec) test_FieldPosition(); 
305
 
                break;
306
 
        case 2: name = "fpe"; 
307
 
                if (exec) logln("TestSuite Format/SmallClasses/FieldPositionExample (f/chc/sma/fpe): ");
308
 
                if (exec) test_FieldPosition_example(); 
309
 
                break;
310
 
        case 3: name = "ft"; 
311
 
                if (exec) logln("TestSuite Format/SmallClasses/Formattable (f/chc/sma/ft): ");
312
 
                if (exec) test_Formattable(); 
313
 
                break;
314
 
        default: name = ""; break; //needed to end loop
315
 
    }
316
 
}