summaryrefslogtreecommitdiff
path: root/cpp/test/XMLTransform/transform/Validate.cpp
blob: f8ed0fd0efc243f4f8b91adbf0e4b33206be817b (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <Freeze/Freeze.h>
#include <TestCommon.h>
#include <IntByteMap.h>
#include <IntShortMap.h>
#include <IntIntMap.h>
#include <IntLongMap.h>
#include <IntFloatMap.h>
#include <IntDoubleMap.h>
#include <IntSeq1MapNew.h>
#include <IntSeq2MapNew.h>
#include <IntSeq3MapNew.h>
#include <IntSeq4MapNew.h>
#include <IntE1MapNew.h>
#include <IntD1MapNew.h>
#include <IntS1MapNew.h>
#include <IntC1MapNew.h>

#include <fstream>

using namespace std;
using namespace Freeze;
using namespace Ice;

#define NUM_KEYS        50
#define NUM_ELEMENTS    50

static void
usage(const char* n)
{
    cerr << "Usage: " << n << " [options]\n";
    cerr <<        
        "Options:\n"
        "--db-dir DIR         Use directory DIR for the database environment.\n"
        ;
}

static void
validatePrimitive(const CommunicatorPtr& communicator, const string& envName)
{  
    cout << "validating primitive transformations... " << flush;
    
    //
    // Validate byte to short transformation
    //
  
    {
	IntShortMap map(communicator, envName, "byteToShort", false);
	for(IntShortMap::iterator p = map.begin(); p != map.end(); ++p)
	{
	    test(p->second == p->first);
	}
    }

    //
    // Validate short to int transformation
    //
   
    {
	IntIntMap map(communicator, envName, "shortToInt", false);
	for(IntIntMap::iterator p = map.begin(); p != map.end(); ++p)
	{
	    test(p->second == p->first);
	}
    }
        
    //
    // Validate int to long transformation
    //
    
    {
	IntLongMap map(communicator, envName, "intToLong", false);
	for(IntLongMap::iterator p = map.begin(); p != map.end(); ++p)
	{
	    test(p->second == p->first);
	}
    }
    
    //
    // Validate long to byte transformation
    //
    {
	IntByteMap map(communicator, envName, "longToByte", false);
	for(IntByteMap::iterator p = map.begin(); p != map.end(); ++p)
	{
	    test(p->second == p->first);
	}
    }
   
    {
	IntDoubleMap map(communicator, envName, "floatToDouble", false);
	for(IntDoubleMap::iterator p = map.begin(); p != map.end(); ++p)
	{
	    test(p->second - p->first <= 0.001);
	}
    }

    cout << "ok" << endl;
    
}

static void
validatePrimitiveSequence(const CommunicatorPtr& communicator, const string& envName)
{
    Int i;

    cout << "validating primitive sequence transformations... " << flush;

    //
    // Validate byte to short sequence transformation
    //
   
    {
	IntSeq1Map map(communicator, envName, "byteToShortSeq", false);
	test(map.size() == 1);
	IntSeq1Map::iterator p = map.find(0);
	test(p != map.end());
	const Test::Seq1& seq = p->second;
	test(seq.size() == NUM_ELEMENTS);
	for(i = 0; i < NUM_ELEMENTS; i++)
	{
	    test(seq[i] == i);
	}
    }
    
    //
    // Validate short to int sequence transformation
    //
 
    {
	IntSeq2Map map(communicator, envName, "shortToIntSeq", false);
	test(map.size() == 1);
	IntSeq2Map::iterator p = map.find(0);
	test(p != map.end());
	const Test::Seq2& seq = p->second;
	test(seq.size() == NUM_ELEMENTS);
	for(i = 0; i < NUM_ELEMENTS; i++)
	{
	    test(seq[i] == i);
	}
    }
    
    //
    // Validate int to long sequence transformation
    //
   
    {
	IntSeq3Map map(communicator, envName, "intToLongSeq", false);
	test(map.size() == 1);
	IntSeq3Map::iterator p = map.find(0);
	test(p != map.end());
	const Test::Seq3& seq = p->second;
	test(seq.size() == NUM_ELEMENTS);
	for(i = 0; i < NUM_ELEMENTS; i++)
	{
	    test(seq[i] == i);
	}
    }

    //
    // Validate long to byte sequence transformation
    //    
    {
	IntSeq4Map map(communicator, envName, "longToByteSeq", false);
	test(map.size() == 1);
	IntSeq4Map::iterator p = map.find(0);
	test(p != map.end());
	const Test::Seq4& seq = p->second;
	test(seq.size() == NUM_ELEMENTS);
	for(i = 0; i < NUM_ELEMENTS; i++)
	{
	    test(seq[i] == i);
	}
    }

    cout << "ok" << endl;
}

static void
validateEnum(const CommunicatorPtr& communicator, const string& envName)
{
    cout << "validating enumeration transformations... " << flush;

   
    //
    // Validate removal of E1::three
    //
    {
	IntE1Map map(communicator, envName, "enum", false);
	test(map.size() == 2);
	IntE1Map::iterator p;
	p = map.find(0);
	test(p != map.end());
	test(p->second == Test::one);
	p = map.find(1);
	test(p != map.end());
	test(p->second == Test::two);
    }

    cout << "ok" << endl;
}

static void
validateDictionary(const CommunicatorPtr& communicator, const string& envName)
{
    cout << "validating dictionary transformations... " << flush;

    //
    // Validate removal of E1::three
    //
    {
	IntD1Map map(communicator, envName, "dict", false);
	IntD1Map::iterator p = map.find(0);
	test(p != map.end());
	const Test::D1& dict = p->second;
	test(dict.size() == 2);
	Test::D1::const_iterator q;
	q = dict.find("one");
	test(q != dict.end());
	test(q->second == Test::one);
	q = dict.find("two");
	test(q != dict.end());
	test(q->second == Test::two);
    }
    cout << "ok" << endl;
}

static void
validateStruct(const CommunicatorPtr& communicator, const string& envName)
{
    cout << "validating struct transformations... " << flush;

   
    //
    // Validate S1
    //
    {
	IntS1Map map(communicator, envName, "struct", false);
	for(IntS1Map::iterator p = map.begin(); p != map.end(); ++p)
	{
	    test(p->second.i == p->first);
	    test(p->second.s == 0);
	}
    }
    cout << "ok" << endl;
   
}

static void
validateClass(const CommunicatorPtr& communicator, const string& envName)
{
    communicator->addObjectFactory(Test::C1::ice_factory(), Test::C1::ice_staticId());
    communicator->addObjectFactory(Test::C2::ice_factory(), Test::C2::ice_staticId());

    cout << "validating class transformations... " << flush;

    {
	IntC1Map map(communicator, envName, "class", false);
	for(IntC1Map::iterator p = map.begin(); p != map.end(); ++p)
	{
	    Test::C2Ptr c2 = Test::C2Ptr::dynamicCast(p->second);
	    test(c2);
	    test(c2->s.size() == 1 && c2->s[0] == '0' + p->first);
	    test(c2->b == p->first);
	    test(c2->l == p->first);
	    test(c2->i == p->first);
	    if(c2->s == "3")
	    {
		//
		// Test facets
		//
		Ice::ObjectPtr obj;
		Test::C1Ptr c1;
		
		obj = c2->ice_findFacet("c1-0");
		test(obj);
		c1 = Test::C1Ptr::dynamicCast(obj);
		test(c1);
		test(c1->s == "c1-0");
		test(c1->b == 0);
		
		obj = c2->ice_findFacet("c1-1");
		test(obj);
		c1 = Test::C1Ptr::dynamicCast(obj);
		test(c1);
		test(c1->s == "c1-1");
		test(c1->b == 1);
		
		obj = c2->ice_findFacet("c1-2");
		test(obj);
		Test::C2Ptr c2Facet = Test::C2Ptr::dynamicCast(obj);
		test(c2Facet);
		test(c2Facet->s == "c1-2");
		test(c2Facet->b == 2);
		test(c2Facet->i == 2);
		test(c2Facet->l == 2);
		
		obj = c2Facet->ice_findFacet("c2-0");
		test(obj);
		c1 = Test::C1Ptr::dynamicCast(obj);
		test(c1);
		test(c1->s == "c2-0");
		test(c1->b == 0);
	    }
	}
    }

    cout << "ok" << endl;

    communicator->removeObjectFactory(Test::C1::ice_staticId());
    communicator->removeObjectFactory(Test::C2::ice_staticId());   
}

static void
validateEvictor(const CommunicatorPtr& communicator, const string& envName)
{
    Freeze::EvictorPtr evictor;

    communicator->addObjectFactory(Test::C1::ice_factory(), Test::C1::ice_staticId());
    communicator->addObjectFactory(Test::C2::ice_factory(), Test::C2::ice_staticId());

    cout << "validating evictor transformations... " << flush;

  
    //
    // Validate C2
    //
    evictor = Freeze::createEvictor(communicator, envName, "evictor", false);
    {
	EvictorIteratorPtr iter = evictor->getIterator();
	while(iter->hasNext())
	{
	    Current current;
	    LocalObjectPtr cookie;
	    
	    current.id = iter->next();
	    
	    ObjectPtr object = evictor->locate(current, cookie);
	    test(object);
	    
	    Test::C2Ptr c2 = Test::C2Ptr::dynamicCast(object);
	    test(c2);
	    test(c2->s.size() == 1 && c2->s == current.id.name);
	    int i = current.id.name[0] - '0';
	    test(c2->b == i);
	    test(c2->l == i);
	    test(c2->i == i);
	    if(c2->s == "3")
	    {
		//
		// Test facets
		//
		Ice::ObjectPtr obj;
		Test::C1Ptr c1;
		
		obj = c2->ice_findFacet("c1-0");
		test(obj);
		c1 = Test::C1Ptr::dynamicCast(obj);
		test(c1);
		test(c1->s == "c1-0");
		test(c1->b == 0);
		
		obj = c2->ice_findFacet("c1-1");
		test(obj);
		c1 = Test::C1Ptr::dynamicCast(obj);
		test(c1);
		test(c1->s == "c1-1");
		test(c1->b == 1);
		
		obj = c2->ice_findFacet("c1-2");
		test(obj);
		Test::C2Ptr c2Facet = Test::C2Ptr::dynamicCast(obj);
		test(c2Facet);
		test(c2Facet->s == "c1-2");
		test(c2Facet->b == 2);
		test(c2Facet->i == 2);
		test(c2Facet->l == 2);
		
		obj = c2Facet->ice_findFacet("c2-0");
		test(obj);
		c1 = Test::C1Ptr::dynamicCast(obj);
		test(c1);
		test(c1->s == "c2-0");
		test(c1->b == 0);
	    }
	    
	    evictor->finished(current, object, cookie);
	}
	iter->destroy();
    }
    
    evictor->deactivate("");
    
    cout << "ok" << endl;

    communicator->removeObjectFactory(Test::C1::ice_staticId());
    communicator->removeObjectFactory(Test::C2::ice_staticId());
}

static int
run(int argc, char* argv[], const CommunicatorPtr& communicator)
{
    string envName = "db";

    int idx = 1;
    while(idx < argc)
    {
        if(strcmp(argv[idx], "--db-dir") == 0)
        {
            if(idx + 1 >= argc)
            {
                cerr << argv[0] << ": argument expected for`" << argv[idx] << "'" << endl;
                usage(argv[0]);
                return EXIT_FAILURE;
            }

            envName = argv[idx + 1];
            for(int i = idx ; i + 2 < argc ; ++i)
            {
                argv[i] = argv[i + 2];
            }
            argc -= 2;
        }
        else if(argv[idx][0] == '-')
        {
            cerr << argv[0] << ": unknown option `" << argv[idx] << "'" << endl;
            usage(argv[0]);
            return EXIT_FAILURE;
        }
        else
        {
            ++idx;
        }
    }

    try
    {
        validatePrimitive(communicator, envName);
        validatePrimitiveSequence(communicator, envName);
        validateEnum(communicator, envName);
        validateDictionary(communicator, envName);
        validateStruct(communicator, envName);
        validateClass(communicator, envName);
        validateEvictor(communicator, envName);
    }
    catch(Exception& ex)
    {
	cerr << "Caught Exception" << endl;
	cerr << ex << endl;
    }
    catch(...)
    {
	cerr << "Caught exception" << endl;
        throw;
    }

    return EXIT_SUCCESS;
}

int
main(int argc, char* argv[])
{
    int status;
    Ice::CommunicatorPtr communicator;

    try
    {
        communicator = Ice::initialize(argc, argv);
        status = run(argc, argv, communicator);
    }
    catch(const Exception& ex)
    {
        cerr << ex << endl;
        status = EXIT_FAILURE;
    }

    try
    {
        communicator->destroy();
    }
    catch(const Exception& ex)
    {
        cerr << ex << endl;
        status = EXIT_FAILURE;
    }

    return status;
}