summaryrefslogtreecommitdiffstats
path: root/tilemgr/test/test_tile.cc
blob: 18f5b97d4a530c38fb0c259ad5a5423ce978c2b6 (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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
#include "mymalloc/mymalloc.h"
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/

/*************************************************************
 *
 *
 * PURPOSE:
 *   creates Tiles and does ops
 *
 *
 * COMMENTS:
 *   none
 *
 ************************************************************/

static const char rcsid[] = "@(#)cachetamgr,test_tile: $Id: test_tile.cc,v 1.26 2002/08/30 11:51:22 coman Exp $";

#include <stdlib.h>
#include <iostream>
#include <time.h>
#include "ulongtype.hh"
#include "booltype.hh"
#include "chartype.hh"
#include "structtype.hh"

#ifndef bool
  #include <bool.h>
  #define OS_OMIT_BOOL
#endif

#include "adminif.hh"
#include "databaseif.hh"
#include "transactionif.hh"
#include "tilemgr/perstile.hh"
#include "tilemgr/transtile.hh"
#include "typefactory.hh"
#include "raslib/rminit.hh"
#include "raslib/rmdebug.hh"

// global variable for AdminIf because of O2 d_Session::begin()
extern char* myExecArgv0 = "";

RMINITGLOBALS('C')

// perhaps used later
static char O2BenchDBName[256] = "NorbertBase";
static char O2BenchSchemaName[256] = "NorbertSchema";

void
printTime()
{
  struct timespec cur_time;
  clock_gettime(CLOCK_REALTIME, &cur_time);

  cout << asctime(localtime(&(cur_time.tv_sec)));
}

void
testConstructors()
{
  // for for loops
  unsigned long i;
  // Tile used for tests
  Tile* myTile;
  // base types for tiles
  BaseType* ulongType = TypeFactory::mapType("ULong");
  // domain of all Tiles
  r_Minterval dom = 
    r_Minterval(3) << r_Sinterval(1L,10L) << r_Sinterval(11L,20L) 
                   << r_Sinterval(5L,7L);

  // testing constructor executing section operation
  // first create a tile with the whole domain
  // char* containing data for one ULong Tile
  char* tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // transient with value
  cout << "Creating transient ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myTile = new TransTile( dom, ulongType, tileData );

  // now project it
  set<r_Dimension, less<r_Dimension> >* projSet = 
    new set<r_Dimension, less<r_Dimension> >;
  projSet->insert(1);

  r_Minterval projDom = 
    r_Minterval(3) << r_Sinterval(1l, 10l) << r_Sinterval(15l, 15l) 
		   << r_Sinterval(5l, 7l);
  
  Tile* projectedTile = new TransTile(myTile, projDom, projSet);

  projectedTile->printStatus();
  
  delete projSet;
  delete projectedTile;
}

void
testCompression()
{
  // for for loops
  unsigned long i;
  // Tile used for tests
  Tile* myTile;
  // Tile used for inititializing tiles with OP_IDENTITY
  Tile* myContentTile;
  // for contents of tiles
  char* tileData;
  // base types for tiles
  BaseType* ulongType = TypeFactory::mapType("ULong");
  // domain of all Tiles
  r_Minterval dom = 
    r_Minterval(3) << r_Sinterval(1L,10L) << r_Sinterval(11L,20L) 
                   << r_Sinterval(5L,7L);

  cout << "Testing Constructors for Tiles" << endl;
  cout << "------------------------------" << endl << endl;
 
  // ------------------------------------------------------------
  // uncompressed TransTile
  // ------------------------------------------------------------

  // char* containing data for one ULong Tile
  tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // transient with value
  cout << "Creating transient ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myTile = new TransTile( dom, ulongType, tileData );

  cout << "  Printing created Tile: " << endl;
  myTile->printStatus();

  cout << "  Deleting created Tile" << endl << endl;
  delete myTile;

  // ------------------------------------------------------------
  // uncompressed PersTile
  // ------------------------------------------------------------

  // char* containing data for one ULong Tile
  tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // persistent with value
  cout << "Creating persistent ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myTile = new PersTile( dom, ulongType, tileData );

  cout << "  Printing created Tile: " << endl;
  myTile->printStatus();

  cout << "  Deleting created Tile" << endl << endl;
  delete myTile;

  // ------------------------------------------------------------
  // uncompressed TransTile
  // ------------------------------------------------------------

  // char* containing data for one ULong Tile
  tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // transient with value
  cout << "Creating transient ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myContentTile = new TransTile( dom, ulongType, tileData );
  myTile = new TransTile( dom, ulongType );
  myTile->execUnaryOp( Ops::OP_IDENTITY, dom, myContentTile, dom );

  cout << "  Printing created Tile: " << endl;
  myTile->printStatus();

  cout << "  Deleting created Tile" << endl << endl;
  delete myTile;

  // ------------------------------------------------------------
  // uncompressed PersTile
  // ------------------------------------------------------------

  // char* containing data for one ULong Tile
  tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // persistent with value
  cout << "Creating persistent ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myContentTile = new PersTile( dom, ulongType, tileData );
  myTile = new PersTile( dom, ulongType, r_ZLib );
  myTile->execUnaryOp( Ops::OP_IDENTITY, dom, myContentTile, dom );

  cout << "  Printing created Tile: " << endl;
  myTile->printStatus();

  cout << "  Deleting created Tile" << endl << endl;
  delete myTile;

  // ------------------------------------------------------------
  // compressed TransTile
  // ------------------------------------------------------------

  // char* containing data for one ULong Tile
  tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // transient with value
  cout << "Creating compressed transient ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myTile = new TransTile( dom, ulongType, r_ZLib );
  myTile->setContents(tileData);

  cout << "  Physical Size: " << myTile->getPhysicalSize() << endl;

  cout << "  Printing created Tile: " << endl;
  myTile->printStatus();

  cout << "  Physical Size: " << myTile->getPhysicalSize() << endl;

  cout << "  Deleting created Tile" << endl << endl;
  delete myTile;

  // ------------------------------------------------------------
  // compressed PersTile
  // ------------------------------------------------------------

  // char* containing data for one ULong Tile
  tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // persistent with value
  cout << "Creating compressed persistent ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myTile = new PersTile( dom, ulongType, r_ZLib );
  myTile->setContents(tileData);

  cout << "  Physical Size: " << myTile->getPhysicalSize() << endl;

  cout << "  Printing created Tile: " << endl;
  myTile->printStatus();

  cout << "  Physical Size: " << myTile->getPhysicalSize() << endl;

  cout << "  Deleting created Tile" << endl << endl;
  delete myTile;

  // ------------------------------------------------------------
  // compressed TransTile
  // ------------------------------------------------------------

  // char* containing data for one ULong Tile
  tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // transient with value
  cout << "Creating compressed transient ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myContentTile = new TransTile( dom, ulongType, tileData );
  myTile = new TransTile( dom, ulongType, r_ZLib );
  myTile->execUnaryOp( Ops::OP_IDENTITY, dom, myContentTile, dom );

  cout << "  Physical Size: " << myTile->getPhysicalSize() << endl;

  cout << "  Printing created Tile: " << endl;
  myTile->printStatus();

  cout << "  Physical Size: " << myTile->getPhysicalSize() << endl;

  cout << "  Deleting created Tile" << endl << endl;
  delete myTile;

  // ------------------------------------------------------------
  // compressed PersTile
  // ------------------------------------------------------------

  // char* containing data for one ULong Tile
  tileData = (char*)mymalloc(dom.cell_count() * ulongType->getSize());
  for(i = 0; i < dom.cell_count() * ulongType->getSize(); i++)
  {
    if(i % ulongType->getSize() == 3 )
      tileData[i] = 10;
    else
      tileData[i] = 0;
  }

  // persistent with value
  cout << "Creating compressed persistent ULong Tile containing 10 with domain " 
       << dom << ":" << endl;
  myContentTile = new PersTile( dom, ulongType, tileData );
  myTile = new PersTile( dom, ulongType, r_ZLib );
  myTile->execUnaryOp( Ops::OP_IDENTITY, dom, myContentTile, dom );

  cout << "  Physical Size: " << myTile->getPhysicalSize() << endl;

  cout << "  Printing created Tile: " << endl;
  myTile->printStatus();

  cout << "  Physical Size: " << myTile->getPhysicalSize() << endl;

  cout << "  Deleting created Tile" << endl << endl;
  delete myTile;
}

void
testUnaryOps()
{
  // base types for tiles
  BaseType* ulongType = TypeFactory::mapType("ULong");
  BaseType* boolType = TypeFactory::mapType("Bool");;
  BaseType* charType = TypeFactory::mapType("Char");;

  // default cell for filling ULong Tiles
  unsigned long longVal = 4294966295;
  char* ulongCell = (char*)&longVal;

  // default cell for filling Bool Tiles
  unsigned char boolVal = 1;
  char* boolCell = (char*)&boolVal;

  // domain of all Tiles
  r_Minterval dom = 
    r_Minterval(2) << r_Sinterval(1L,10L) << r_Sinterval(11L,20L);

  r_Minterval domOp = 
    r_Minterval(2) << r_Sinterval(1L,5L) << r_Sinterval(11L,15L);

  // domain of one dimensional result
  r_Minterval res1D = 
    r_Minterval(2) << r_Sinterval(1L,10L);

  unsigned char charVal = 20;
  char* charCell = (char*)&charVal;

  // Tiles used for tests
  Tile* transULongTile;
  Tile* transBoolTile;
  Tile* persULongTile;
  Tile* persBoolTile;
  Tile* transCharTile;
  Tile* transCharRes;

  cout << "Testing Unary Operations on Tiles" << endl;
  cout << "---------------------------------" << endl << endl;

  // transient with value
  cout << "Creating Tiles for Unary Operations with domain " 
       << dom << ":" << endl;
  transULongTile = new TransTile( dom, ulongType, ulongCell );
  transBoolTile = new TransTile( dom, boolType, boolCell );
  persULongTile = new PersTile( dom, ulongType, ulongCell );
  persBoolTile = new PersTile( dom, boolType, boolCell );
  transCharTile = new TransTile( dom, charType, charCell );
  transCharRes = new TransTile( domOp, charType, boolCell );


  cout << "Operations with ULong:" << endl << endl;

  cout << "Carrying out NOT on TransTile (should be 1000)" << endl;
  transULongTile->execUnaryOp(Ops::OP_NOT, dom, transULongTile, dom);

  cout << "Contents after operation: " << endl;
  transULongTile->printStatus();

  cout << "Carrying out IDENTITY on TransTile (should be 1000)" << endl;
  transULongTile->execUnaryOp(Ops::OP_IDENTITY, dom, transULongTile, dom);

  cout << "Contents after operation: " << endl;
  transULongTile->printStatus();

  cout << "Carrying out NOT on PersTile (should be 1000)" << endl;
  persULongTile->execUnaryOp(Ops::OP_NOT, dom, persULongTile, dom);

  cout << "Contents after operation: " << endl;
  persULongTile->printStatus();

  cout << "Carrying out IDENTITY on PersTile (should be 1000)" << endl;
  persULongTile->execUnaryOp(Ops::OP_IDENTITY, dom, persULongTile, dom);

  cout << "Contents after operation: " << endl;
  persULongTile->printStatus();

  cout << "Operations with Bool:" << endl << endl;

  cout << "Carrying out NOT on TransTile (should be FALSE)" << endl;
  transBoolTile->execUnaryOp(Ops::OP_NOT, dom, transBoolTile, dom);

  cout << "Contents after operation: " << endl;
  transBoolTile->printStatus();

  cout << "Carrying out IDENTITY on TransTile (should be FALSE)" << endl;
  transBoolTile->execUnaryOp(Ops::OP_IDENTITY, dom, transBoolTile, dom);

  cout << "Contents after operation: " << endl;
  transBoolTile->printStatus();

  cout << "Carrying out NOT on PersTile (should be FALSE)" << endl;
  persBoolTile->execUnaryOp(Ops::OP_NOT, dom, persBoolTile, dom);

  cout << "Contents after operation: " << endl;
  persBoolTile->printStatus();

  cout << "Carrying out IDENTITY on PersTile (should be FALSE)" << endl;
  persBoolTile->execUnaryOp(Ops::OP_IDENTITY, dom, persBoolTile, dom);

  cout << "Contents after operation: " << endl;
  persBoolTile->printStatus();

  cout << "Carrying out IDENTITY on subset of char tile" << endl;
  transCharRes->execUnaryOp(Ops::OP_IDENTITY, domOp, transCharTile, domOp);

  cout << "Contents after operation: " << endl;
  transCharRes->printStatus();

  // Trying to get more interesting values into tile
  unsigned char val = 0;
  for(int i = 0; i < transCharTile->getSize(); i++) {
    transCharTile->setCell(i, (const char*)&val);
    val++;
  }

  cout << "Tile with more interesting values: " << endl;
  transCharTile->printStatus();

  cout << "Carrying out IDENTITY on subset of char tile" << endl;
  transCharRes->execUnaryOp(Ops::OP_IDENTITY, domOp, transCharTile, domOp);

  cout << "Contents after operation: " << endl;
  transCharRes->printStatus();

  // Operation with result of a different dimensionality

  // domain of all Tiles
  r_Minterval horSlice = 
    r_Minterval(2) << r_Sinterval(1L,10L) << r_Sinterval(15L,15L);
  r_Minterval verSlice = 
    r_Minterval(2) << r_Sinterval(5L,5L) << r_Sinterval(11L,20L);
  Tile* transRes1D = new TransTile( res1D, charType, charCell );

  cout << "Carrying out IDENTITY to receive a 1D vertical slice" << endl;
  transRes1D->execUnaryOp(Ops::OP_IDENTITY, res1D, transCharTile, horSlice);

  cout << "Contents after operation: " << endl;
  transRes1D->printStatus();

  cout << "Carrying out IDENTITY to receive a 1D horizontal slice" << endl;
  transRes1D->execUnaryOp(Ops::OP_IDENTITY, res1D, transCharTile, verSlice);

  cout << "Contents after operation: " << endl;
  transRes1D->printStatus();

  cout << "Deleting created Tiles ..." << endl << endl;

  delete transULongTile;
  delete transBoolTile;
  delete persULongTile;
  delete persBoolTile;
  delete transCharRes;
  delete transCharTile;
  delete transRes1D;
}

void
testBinaryOps()
{
  // base types for tiles
  BaseType* ulongType = TypeFactory::mapType("ULong");
  BaseType* boolType = TypeFactory::mapType("Bool");;

  // default cell for filling ULong Tiles
  unsigned long longVal = 1000;
  char* ulongCell = (char*)&longVal;

  // default cell for filling Bool Tiles
  unsigned char boolVal = 1;
  char* boolCell = (char*)&boolVal;

  // domain of all Tiles
  r_Minterval dom = 
    r_Minterval(3) << r_Sinterval(1L,10L) << r_Sinterval(11L,20L) 
		   << r_Sinterval(5L,7L);

  // Tiles used for tests
  Tile* transULongTile;
  Tile* transBoolTile;
  Tile* persULongTile;
  Tile* persBoolTile;
  Tile* constULongTile;
  Tile* constBoolTile;

  cout << "Testing Binary Operations on Tiles" << endl;
  cout << "---------------------------------" << endl << endl;

  cout << "Creating Tiles for Binary Operations with domain " 
       << dom << ":" << endl;
  transULongTile = new TransTile( dom, ulongType, ulongCell );
  transBoolTile = new TransTile( dom, boolType, boolCell );
  persULongTile = new PersTile( dom, ulongType, ulongCell );
  persBoolTile = new PersTile( dom, boolType, boolCell );

  cout << "Creating a TransTile for the two operands..." << endl;
  TransTile ttileOp = TransTile(dom, ulongType, ulongCell);

  cout << "Contents: " << endl;
  ttileOp.printStatus();

  cout << "Creating a TransTile for the result..." << endl;
  TransTile ttileRes = TransTile(dom, ulongType, ulongCell);

  cout << "Carrying out binary operation..." << endl;
  ttileRes.execBinaryOp(Ops::OP_PLUS, dom, &ttileOp, dom, &ttileOp, dom);

  cout << "Contents after operation: " << endl;
  ttileRes.printStatus();

  // testing binary operation with Bool as result
  long bval = 10000;

  // default cell for filling all Tiles
  char* bvalCell = (char*)&bval;

  cout << "Creating a TransTile for the two operands..." << endl;
  TransTile btileOp = TransTile(dom, ulongType, boolCell);

  cout << "Contents: " << endl;
  btileOp.printStatus();

  cout << "Creating a TransTile for the result..." << endl;
  TransTile btileRes = TransTile(dom, boolType, boolCell);

  cout << "Carrying out binary operation..." << endl;
  btileRes.execBinaryOp(Ops::OP_EQUAL, dom, &btileOp, dom, &btileOp, dom);

  cout << "Contents after operation: " << endl;
  btileRes.printStatus();

  cout << "Carrying out PLUS with TransTile and constant (should be 2000)"
       << endl;

  transULongTile->execConstOp(Ops::OP_PLUS, dom, transULongTile, dom,
			      ulongCell, ulongType);

  cout << "Contents after operation: " << endl;
  transULongTile->printStatus();

  delete transULongTile;
  delete transBoolTile;
  delete persULongTile;
  delete persBoolTile;
  delete constULongTile;
  delete constBoolTile;
}

void
testCompTile()
{
//   // size of tiles in one direction
//   const long testSize = 10;
//   // base types for tiles
//   BaseType* ulongType = TypeFactory::mapType("ULong");

//   // default cell for filling ULong Tiles
//   unsigned long longVal = 1000;
//   char* ulongCell = (char*)&longVal;

//   // domain of all Tiles
//   r_Minterval dom = 
//     r_Minterval(2) << r_Sinterval(1L,testSize) << r_Sinterval(1L,testSize);

//   // Tiles used for tests
//   Tile* compTile1;
//   Tile* compTile2;
//   TransTile* transTile;

//   cout << "Testing CompTile" << endl;
//   cout << "----------------" << endl << endl;

//   cout << "Creating Tiles with domain " 
//        << dom << ":" << endl;
//   compTile1 = new CompTile( dom, ulongType, Tile::COMP_ZLIB, ulongCell );
//   compTile2 = new CompTile( dom, ulongType, Tile::COMP_ZLIB, ulongCell );
//   transTile = new TransTile( dom, ulongType, ulongCell );

  // initialize with random values
//   long val;
//   for(int i = 0; i < testSize; i++) {
//     for(int j = 0; j < testSize; j++) {
//       val = (long)(LONG_MIN + ((double)LONG_MAX-LONG_MIN)*drand48());
//       compTile1->setCell(i*testSize+j, (char*)&val);
//       val = (long)(LONG_MIN + ((double)LONG_MAX-LONG_MIN)*drand48());
//       compTile2->setCell(i*testSize+j, (char*)&val);
//     }
//   }

  // initialize with incrementing values.
//   long val;
//   for(int i = 0; i < testSize; i++) {
//     for(int j = 0; j < testSize; j++) {
//       val = i*testSize+j;
//       compTile1->setCell(i*testSize+j, (char*)&val);
//       compTile2->setCell(i*testSize+j, (char*)&val);
//     }
//   }

  // initialize with incrementing values, simulate struct
//   long val;
//   for(int i = 0; i < testSize; i++) {
//     for(int j = 0; j < testSize; j += 3) {
//       val = i*testSize+j;

//       compTile1->setCell(i*testSize+j, (char*)&val);
//       if(j+1 < testSize) 
// 	compTile1->setCell(i*testSize+j+1, (char*)&val+1000000);
//       if(j+2 < testSize) 
// 	compTile1->setCell(i*testSize+j+2, (char*)&val+2000000);

//       compTile2->setCell(i*testSize+j, (char*)&val);
//       if(j+1 < testSize) 
// 	compTile2->setCell(i*testSize+j+1, (char*)&val+1000000);
//       if(j+2 < testSize) 
// 	compTile2->setCell(i*testSize+j+2, (char*)&val+2000000);
//     }
//   }

//   cout << "Contents transTile: " << endl;
//   if(transTile->getSize() <= 1000)
//     transTile->printStatus();

//   cout << "Contents compTile1: " << endl;
//   if(transTile->getSize() <= 1000)
//     compTile1->printStatus();

//   cout << "Contents compTile2: " << endl;
//   if(transTile->getSize() <= 1000)
//     compTile2->printStatus();

  // does only work if compress() is not protected.
//   cout << "Compressing compTiles" << endl;
//   ((CompTile*)compTile1)->compress();
//   ((CompTile*)compTile2)->compress();

//   cout << "Carrying out binary operation..." << endl;
//   transTile->execBinaryOp(Ops::OP_PLUS, dom, compTile1, dom, compTile2, dom);

//   cout << "Contents after operation: " << endl;
//   if(transTile->getSize() <= 1000)
//     transTile->printStatus();

//   delete compTile1;
//   delete compTile2;
//   delete transTile;
}

void
testOpsWithConsts()
{
  // base types for tiles
  BaseType* ulongType = TypeFactory::mapType("ULong");
  BaseType* charType = TypeFactory::mapType("Char");

  StructType* structType = new StructType("TestStruct", 2);
  structType->addElement("Band1", "ULong");
  structType->addElement("Band2", "Char");

  // default cell for filling ULong Tiles
  unsigned long uLongVal = 1000;
  char* ulongCell = (char*)&uLongVal;
  // default cell for filling Char Tiles
  unsigned char charVal = 112;
  char* charCell = (char*)&charVal;
  // default cell for filling Struct Tiles
  unsigned char structCell[5];
  structCell[0] = ulongCell[0];
  structCell[1] = ulongCell[1];
  structCell[2] = ulongCell[2];
  structCell[3] = ulongCell[3];
  structCell[4] = charCell[0];

  // domain of all Tiles
  r_Minterval dom = 
    r_Minterval(2) << r_Sinterval(1L,5L) << r_Sinterval(1L,5L);

  // Tiles used for tests
  cout << "Creating Tiles with domain " 
       << dom << ":" << endl;
  Tile* charTile = new TransTile(dom, charType, charCell);
  Tile* ulongTile = new TransTile(dom, ulongType, ulongCell);
  Tile* structTile = new TransTile(dom, structType, (char*)structCell);

  cout << "Contents of Tiles: " << endl;
  charTile->printStatus();
  ulongTile->printStatus();
  structTile->printStatus();

  cout << "Result Type for Struct and Char: " 
       << (Ops::getResultType(Ops::OP_PLUS, structType, charType))->getTypeName() << endl;

  cout << "Carrying out binary operation with constant on structTile ..." 
       << endl;
  structTile->execConstOp(Ops::OP_PLUS, dom, structTile, dom, charCell, 
			  charType);

  cout << "Contents after operation: " << endl;
  structTile->printStatus();

  cout << "Carrying out binary operation with charTile on structTile ..." 
       << endl;
  structTile->execBinaryOp(Ops::OP_PLUS, dom, structTile, dom, charTile, dom);

  cout << "Contents after operation: " << endl;
  structTile->printStatus();

  cout << "Carrying out binary operation with two structTiles ..." 
       << endl;
  structTile->execBinaryOp(Ops::OP_PLUS, dom, structTile, dom, structTile, dom);

  cout << "Contents after operation: " << endl;
  structTile->printStatus();

  delete charTile;
  delete ulongTile;
  delete structTile;
}

void
testCellAccess()
{
  // base types for tiles
  BaseType* ulongType = TypeFactory::mapType("ULong");
  BaseType* charType = TypeFactory::mapType("Char");

  // default cell for filling ULong Tiles
  unsigned long uLongVal = 1000;
  char* ulongCell = (char*)&uLongVal;
  // default cell for filling Char Tiles
  unsigned char charVal = 112;
  char* charCell = (char*)&charVal;

  // domain of all Tiles
  r_Minterval dom = 
    r_Minterval(2) << r_Sinterval(1L,10L) << r_Sinterval(1L,10L);

  // Tiles used for tests
  cout << "Creating Tiles with domain " 
       << dom << ":" << endl;
  Tile* charTile = new TransTile(dom, charType, charCell);
  Tile* ulongTile = new TransTile(dom, ulongType, ulongCell);

  cout << "Accessing cell by point: ";
  r_Point a = r_Point(2) << 5 << 5;
  cout << (int)*(charTile->getCell(a)) << " "
       << *(unsigned long*)ulongTile->getCell(a) << endl;
  
  delete charTile;
  delete ulongTile;
}

void
testScaleOp(void)
{
  r_Dimension dim = 2;
  r_Dimension i;
  BaseType* charType = TypeFactory::mapType("Char");
  r_Minterval dom = r_Minterval(dim);
  r_Minterval nextDom = r_Minterval(dim);
  r_Point origin(dim);
  r_Point offset(dim);
  unsigned long fullSize;
  float scaleBy, incrementBy, eps;

  for (i=0; i<dim; i++)
  {
    origin[i] = (r_Range)0;
    offset[i] = (r_Range)64;
    dom << r_Sinterval(origin[i] + offset[i], (r_Range)511 + offset[i]);
    nextDom << r_Sinterval((r_Range)512 + offset[i], (r_Range)1023 + offset[i]);
  }

  fullSize = dom.cell_count();
  char *tileValues = (char*)mymalloc(fullSize);
  Tile *charTile = new TransTile(dom, charType, tileValues, fullSize);
  Tile *nextTile = new TransTile(dom, charType);
  memset(charTile->getContents(), 0, dom.cell_count());

  vector<float> scaleFactors(dim);

  incrementBy = 0.05; eps = 1e-2;
  for (scaleBy=incrementBy; scaleBy <= 8-eps; scaleBy += incrementBy)
  {
    vector<float>::iterator iter = scaleFactors.begin();

    cout << "Scale " << dom << " by " << scaleBy << endl;

    for (i=0; i<dim; i++, iter++)
      *iter = scaleBy;

    r_Minterval scaledDom;
    if (charTile->scaleGetDomain(dom, origin, scaleFactors, scaledDom))
    {
      unsigned long scaledSize = scaledDom.cell_count();
      char *scaledValues = (char*)mymalloc(scaledSize);
      Tile *scaledTile = new TransTile(scaledDom, charType, scaledValues, scaledSize);

      r_Minterval nextScaledDom;
      if (nextTile->scaleGetDomain(nextDom, origin, scaleFactors, nextScaledDom))
      {
	cout << "scaledDom: " << scaledDom << ", nextScaledDom: " << nextScaledDom << endl;
	for (i=0; i<dim; i++)
	{
	  if (nextScaledDom[i].low() != scaledDom[i].high() + 1)
	  {
	    cout << "NO SEAMLESS MATCH IN DIM " << i << '!' << endl;
	  }
	}
      }

      scaledTile->execScaleOp(charTile, dom, origin, scaleFactors);

      delete scaledTile;
    }
    else
    {
      cout << "Tile scaled to zero domain" << endl;
    }

    incrementBy = 0.05 * (1 << (int)((scaleBy + eps) / 2));
  }

  delete charTile;
  delete nextTile;
}


/*************************************************************
 * Function name.: int main( int argc, char** argv)
 *
 * Arguments.....: 
 *   argc: number of arguments given to program
 *   argv: array of char* with arguments
 * Return value..: exit status
 * Description...: none
 ************************************************************/

int
main( int argc , char* argv[] )
{
  int i;

  i = 1;
  while (i < argc)
  {
    if (argv[i][0] == '-')
    {
      switch (argv[i][1])
      {
	case 'd':
	  strcpy(O2BenchDBName, argv[++i]); break;
	case 's':
	  strcpy(O2BenchSchemaName, argv[++i]); break;
	case 'h':
	  cout << "Use: " << argv[0] << " [-d dbname -s schemaname -h]" << endl;
	  exit(0);
	default:
	  break;
      }
    }
    i++;
  }

  // variables representing O2 database, ta and session
  DatabaseIf database;
  TransactionIf ta;

  // don't forget to initialize before using AdminIf!
  myExecArgv0 = argv[0];
  AdminIf* myAdmin = AdminIf::instance();

  cout << "RManDebug: " << RManDebug << endl;

  // connect to the database
  cout << "Connecting to database " << O2BenchDBName 
       << "..." << endl;
  database.open( O2BenchDBName );

  ta.begin(&database);

  cout << "These tests were done with:" << endl 
       << "  " << rcsid << endl;
  cout << "This protocol was created on:" << endl << "  ";
  printTime();
  cout << endl;

  // testConstructors();
  // testCompression();
  // testUnaryOps();
  // testBinaryOps();
  // testCompTile();
  // testOpsWithConsts();
  // testAlgebraOps();
  // testCellAccess();
  // testScaleOp();
  testScaleOp();

  ta.commit();

  cout << "Ending O2 session..." << endl;
  database.close();
  delete myAdmin;
}