| 1 | #include "machdefs.h"
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include <math.h>
 | 
|---|
| 4 | #include <iostream.h>
 | 
|---|
| 5 | 
 | 
|---|
| 6 | #include "tarrinit.h"
 | 
|---|
| 7 | #include "array.h"
 | 
|---|
| 8 | #include "timing.h"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | static int test_ac();
 | 
|---|
| 11 | static int test_oso(int r, int c);
 | 
|---|
| 12 | static int test_odo(int r, int c);
 | 
|---|
| 13 | 
 | 
|---|
| 14 | static int prtlev = 0;
 | 
|---|
| 15 | static int nprt = 100;
 | 
|---|
| 16 | 
 | 
|---|
| 17 | template <class T>
 | 
|---|
| 18 | void getMinMax(TArray<T>& a, T& min, T& max)
 | 
|---|
| 19 | {
 | 
|---|
| 20 |   min = a[0];
 | 
|---|
| 21 |   max = a[0];
 | 
|---|
| 22 |   for(uint_8 k=1; k<a.Size(); k++) {
 | 
|---|
| 23 |     if (a[k]<min)  min = a[k];
 | 
|---|
| 24 |     else if (a[k]>max)  max = a[k];
 | 
|---|
| 25 |   }
 | 
|---|
| 26 | }
 | 
|---|
| 27 | 
 | 
|---|
| 28 | int main(int narg, char* arg[])
 | 
|---|
| 29 | {
 | 
|---|
| 30 | 
 | 
|---|
| 31 |   SophyaInit();
 | 
|---|
| 32 |   InitTim();   // Initializing the CPU timer
 | 
|---|
| 33 | 
 | 
|---|
| 34 |   if (narg < 2) {
 | 
|---|
| 35 |     cout << " carrt ac/oso/odo [NRow=5] [NCols=10] [prtlev=0] [nprtmax=100] \n"
 | 
|---|
| 36 |          << "   ac : array conversion test   \n" 
 | 
|---|
| 37 |          << "   oso : operation same memory organisation \n" 
 | 
|---|
| 38 |          << "   odo : operation different memory organisation \n" << endl;
 | 
|---|
| 39 |     exit(0);
 | 
|---|
| 40 |   }
 | 
|---|
| 41 | 
 | 
|---|
| 42 |   string opt = arg[1];
 | 
|---|
| 43 |   int nr = 5;
 | 
|---|
| 44 |   int nc = 10;
 | 
|---|
| 45 |   if (narg > 2) nr = atoi(arg[2]);
 | 
|---|
| 46 |   if (narg > 3) nc = atoi(arg[3]);
 | 
|---|
| 47 |   if (narg > 4) prtlev = atoi(arg[4]);
 | 
|---|
| 48 |   if (narg > 5) nprt = atoi(arg[5]);
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   BaseArray::SetMaxPrint(nprt, prtlev);
 | 
|---|
| 51 |   
 | 
|---|
| 52 |   PrtTim(" Start of Test ");
 | 
|---|
| 53 |   try {
 | 
|---|
| 54 |     if (opt == "ac") test_ac();
 | 
|---|
| 55 |     else if (opt == "oso") test_oso(nr, nc);
 | 
|---|
| 56 |     else if (opt == "odo") test_odo(nr, nc);
 | 
|---|
| 57 |   }
 | 
|---|
| 58 |   catch (PThrowable & exc) {
 | 
|---|
| 59 |     cerr << " catched Exception " << exc.Msg() << endl;
 | 
|---|
| 60 |   }  
 | 
|---|
| 61 |   catch (...) {
 | 
|---|
| 62 |     cerr << " catched unknown (...) exception " << endl; 
 | 
|---|
| 63 |   }  
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   PrtTim(" End of Test ");
 | 
|---|
| 66 | 
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | 
 | 
|---|
| 70 | int test_ac()
 | 
|---|
| 71 | {
 | 
|---|
| 72 |     cout << "\n -----> Testing TArray Conversion <---- " << endl;
 | 
|---|
| 73 |      TArray<int_4> ia(7,5);   
 | 
|---|
| 74 |      ia = RegularSequence(10., 2.);
 | 
|---|
| 75 |      TArray<r_4> ra(7,5);   
 | 
|---|
| 76 |      ra = ia;
 | 
|---|
| 77 |      cout << " ra = ia(= RegularSequence(10., 2.)) = \n " << ra << endl;
 | 
|---|
| 78 |      TArray<r_4> rb(5,3);
 | 
|---|
| 79 |      rb =  RegularSequence(20., .5);  
 | 
|---|
| 80 |      TMatrix<int_4> mx(3,5);
 | 
|---|
| 81 |      cout << " TArray<r_4> rb(5,3); rb =  RegularSequence(20., .5); rb : " << endl;
 | 
|---|
| 82 |      cout << rb << endl;
 | 
|---|
| 83 |      mx = rb;
 | 
|---|
| 84 |      cout << " TMatrix<int_4> mx(3,5);       mx = rb;  mx: " << endl;
 | 
|---|
| 85 |      cout << mx << endl;
 | 
|---|
| 86 |      TArray<r_4> rc(10);
 | 
|---|
| 87 |      rc = 3.1415;
 | 
|---|
| 88 |      TMatrix<r_8> mx2(rc);
 | 
|---|
| 89 |      cout << "   TArray<r_4> rc(10) = 3.1415 , TMatrix<r_8> mx2(rc) rc : " << endl;
 | 
|---|
| 90 |      cout << mx2 << endl;
 | 
|---|
| 91 |      TMatrix<int_4> mx3(3,5);
 | 
|---|
| 92 |      cout << " Trying      TMatrix<int_4> mx3(3,5);  mx3 = rc; ?? " << endl;
 | 
|---|
| 93 |      mx3 = rc;
 | 
|---|
| 94 |      return(0);
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | int test_oso(int nr, int nc)
 | 
|---|
| 98 | {
 | 
|---|
| 99 |   cout << "\n -----> Testing TArray operation TArray<int_4>(nx=" << nr << ",ny=" 
 | 
|---|
| 100 |        << nc << " )" << endl;
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   int rc = 0;
 | 
|---|
| 103 |   int min,max;
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   TArray<int_4> a(nr,nc);
 | 
|---|
| 106 |   a = 20;
 | 
|---|
| 107 |   TArray<int_4> b(nr,nc);
 | 
|---|
| 108 |   b = 9;
 | 
|---|
| 109 |   TArray<int_4> c = a-2*b;
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   if (prtlev > 0) {
 | 
|---|
| 112 |     cout << " A = \n " << a << endl;
 | 
|---|
| 113 |     cout << " B = \n " << b << endl;
 | 
|---|
| 114 |     cout << " C = A-2*B= \n " << c << endl;
 | 
|---|
| 115 |   }
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   getMinMax(c, min, max);
 | 
|---|
| 118 |   if ((min != 2) || (max != 2)) {
 | 
|---|
| 119 |     cout << "!!! ERROR Test C=A+2*B  Min=" << min << " Max=" << max << endl;
 | 
|---|
| 120 |     rc += 1;
 | 
|---|
| 121 |   }
 | 
|---|
| 122 |   else cout << " OK   Test C=A+2*B  OK " << endl; 
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   c = (a*4+1).DivElt(b);
 | 
|---|
| 125 |   if (prtlev > 0) cout << " C = (A*4+1)/B = \n" << c << endl;
 | 
|---|
| 126 |   getMinMax(c, min, max);
 | 
|---|
| 127 |   if ((min != 9) || (max != 9)) {
 | 
|---|
| 128 |     cout << "!!! ERROR Test C = (A*4+1)/B  Min=" << min << " Max=" << max << endl;
 | 
|---|
| 129 |     rc += 2;
 | 
|---|
| 130 |   }
 | 
|---|
| 131 |   else cout << " OK   Test C = (A*4+1)/B  OK " << endl; 
 | 
|---|
| 132 |   
 | 
|---|
| 133 |   a = RegularSequence(1, 1);
 | 
|---|
| 134 |   b = RegularSequence(nr*nc, -1);
 | 
|---|
| 135 |   c = (a*2)+(b*2);
 | 
|---|
| 136 |   if (prtlev > 0) {
 | 
|---|
| 137 |     cout << " A = \n " << a << endl;
 | 
|---|
| 138 |     cout << " B = \n " << b << endl;
 | 
|---|
| 139 |     cout << " C = 2A-2*B= \n " << c << endl;
 | 
|---|
| 140 |   }
 | 
|---|
| 141 |   c /= (nr*nc+1);
 | 
|---|
| 142 |   getMinMax(c, min, max);
 | 
|---|
| 143 |   if ((min != 2) || (max != 2)) {
 | 
|---|
| 144 |     cout << "!!! ERROR Test3 C=2A+2*B  Min=" << min << " Max=" << max << endl;
 | 
|---|
| 145 |     rc += 1;
 | 
|---|
| 146 |   }
 | 
|---|
| 147 |   else cout << " OK   Test3 C=2A+2*B  OK " << endl; 
 | 
|---|
| 148 |   
 | 
|---|
| 149 |   return(rc);
 | 
|---|
| 150 | }
 | 
|---|
| 151 | 
 | 
|---|
| 152 | int test_odo(int nr, int nc)
 | 
|---|
| 153 | {
 | 
|---|
| 154 |   cout << "\n -----> Testing TMatrix operation TMatrix<r_4>(nr=" << nr << ",nc=" 
 | 
|---|
| 155 |        << nc << " )" << endl;
 | 
|---|
| 156 |   cout << "   A CMemoryMapping  - B FortranMemoryMapping " << endl;
 | 
|---|
| 157 | 
 | 
|---|
| 158 |   int rc = 0;
 | 
|---|
| 159 |   r_4 min,max;
 | 
|---|
| 160 | 
 | 
|---|
| 161 |   TMatrix<r_4> a(nr,nc,BaseArray::CMemoryMapping);
 | 
|---|
| 162 |   a = 20;
 | 
|---|
| 163 |   TMatrix<r_4> b(nr,nc,BaseArray::FortranMemoryMapping);
 | 
|---|
| 164 |   b = 9;
 | 
|---|
| 165 |   TMatrix<r_4> c(nr,nc,BaseArray::CMemoryMapping);
 | 
|---|
| 166 | 
 | 
|---|
| 167 |   if (prtlev > 0) {
 | 
|---|
| 168 |     cout << " A = \n " << a << endl;
 | 
|---|
| 169 |     cout << " B = \n " << b << endl;
 | 
|---|
| 170 |   }
 | 
|---|
| 171 |   c = a-(b*2.0f);
 | 
|---|
| 172 |   if (prtlev > 0) cout << " C = A-2*B= \n " << c << endl;
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   getMinMax(c, min, max);
 | 
|---|
| 175 |   if ((fabs(min-2.) > 0.0001) || (fabs(max-2.) > 0.0001)) {
 | 
|---|
| 176 |     cout << "!!! ERROR Test C=A+2*B  Min=" << min << " Max=" << max << endl;
 | 
|---|
| 177 |     rc += 1;
 | 
|---|
| 178 |   }
 | 
|---|
| 179 |   else cout << " OK   Test C=A+2*B  OK " << endl; 
 | 
|---|
| 180 | 
 | 
|---|
| 181 |   c = (a*4.0f+5.5f).DivElt(b);
 | 
|---|
| 182 |   if (prtlev > 0) cout << " C = (A*4+12)/B = \n" << c << endl;
 | 
|---|
| 183 |   getMinMax(c, min, max);
 | 
|---|
| 184 |   if ((fabs(min-9.5) > 0.0001) || (fabs(max-9.5) > 0.0001)) {
 | 
|---|
| 185 |     cout << "!!! ERROR Test C = (A*4+12)/B  Min=" << min << " Max=" << max << endl;
 | 
|---|
| 186 |     rc += 2;
 | 
|---|
| 187 |   }
 | 
|---|
| 188 |   else cout << " OK   Test C = (A*4+12)/B  OK " << endl; 
 | 
|---|
| 189 | 
 | 
|---|
| 190 |   a = RegularSequence(1, 1);
 | 
|---|
| 191 |   b = RegularSequence(nr*nc, -1);
 | 
|---|
| 192 |   c = (a*2.f)+(b*2.f);
 | 
|---|
| 193 |   if (prtlev > 0) {
 | 
|---|
| 194 |     cout << " A = \n " << a << endl;
 | 
|---|
| 195 |     cout << " B = \n " << b << endl;
 | 
|---|
| 196 |     cout << " C = 2A-2*B= \n " << c << endl;
 | 
|---|
| 197 |   }
 | 
|---|
| 198 |   c /= (nr*nc+1);
 | 
|---|
| 199 |   getMinMax(c, min, max);
 | 
|---|
| 200 |   if ((fabs(min-2.) > 0.0001) || (fabs(max-2.) > 0.0001)) {
 | 
|---|
| 201 |     cout << "!!! ERROR Test3 C=2A+2*B  Min=" << min << " Max=" << max << endl;
 | 
|---|
| 202 |     rc += 1;
 | 
|---|
| 203 |   }
 | 
|---|
| 204 |   else cout << " OK   Test3 C=2A+2*B  OK " << endl; 
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   return(rc);
 | 
|---|
| 207 | 
 | 
|---|
| 208 | }
 | 
|---|