| 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 | cout << ra << endl;
|
|---|
| 79 | TArray<r_4> rb(5,3);
|
|---|
| 80 | rb = RegularSequence(20., .5);
|
|---|
| 81 | TMatrix<int_4> mx(3,5);
|
|---|
| 82 | cout << " TArray<r_4> rb(5,3); rb = RegularSequence(20., .5); rb : " << endl;
|
|---|
| 83 | cout << rb << endl;
|
|---|
| 84 | mx = rb;
|
|---|
| 85 | cout << " TMatrix<int_4> mx(3,5); mx = rb; mx: " << endl;
|
|---|
| 86 | cout << mx << endl;
|
|---|
| 87 | TArray<r_4> rc(10);
|
|---|
| 88 | rc = 3.1415;
|
|---|
| 89 | TMatrix<r_8> mx2(rc);
|
|---|
| 90 | cout << " TArray<r_4> rc(10) = 3.1415 , TMatrix<r_8> mx2(rc) rc : " << endl;
|
|---|
| 91 | cout << mx2 << endl;
|
|---|
| 92 | TMatrix<int_4> mx3(3,5);
|
|---|
| 93 | cout << " Trying TMatrix<int_4> mx3(3,5); mx3 = rc; ?? " << endl;
|
|---|
| 94 | mx3 = rc;
|
|---|
| 95 | return(0);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | int test_oso(int nr, int nc)
|
|---|
| 99 | {
|
|---|
| 100 | cout << "\n -----> Testing TArray operation TArray<int_4>(nx=" << nr << ",ny="
|
|---|
| 101 | << nc << " )" << endl;
|
|---|
| 102 |
|
|---|
| 103 | int rc = 0;
|
|---|
| 104 | int min,max;
|
|---|
| 105 |
|
|---|
| 106 | TArray<int_4> a(nr,nc);
|
|---|
| 107 | a = 20;
|
|---|
| 108 | TArray<int_4> b(nr,nc);
|
|---|
| 109 | b = 9;
|
|---|
| 110 | TArray<int_4> c = a-2*b;
|
|---|
| 111 |
|
|---|
| 112 | if (prtlev > 0) {
|
|---|
| 113 | cout << " A = \n " << a << endl;
|
|---|
| 114 | cout << " B = \n " << b << endl;
|
|---|
| 115 | cout << " C = A-2*B= \n " << c << endl;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | getMinMax(c, min, max);
|
|---|
| 119 | if ((min != 2) || (max != 2)) {
|
|---|
| 120 | cout << "!!! ERROR Test C=A+2*B Min=" << min << " Max=" << max << endl;
|
|---|
| 121 | rc += 1;
|
|---|
| 122 | }
|
|---|
| 123 | else cout << " OK Test C=A+2*B OK " << endl;
|
|---|
| 124 |
|
|---|
| 125 | c = (a*4+1).DivElt(b);
|
|---|
| 126 | if (prtlev > 0) cout << " C = (A*4+1)/B = \n" << c << endl;
|
|---|
| 127 | getMinMax(c, min, max);
|
|---|
| 128 | if ((min != 9) || (max != 9)) {
|
|---|
| 129 | cout << "!!! ERROR Test C = (A*4+1)/B Min=" << min << " Max=" << max << endl;
|
|---|
| 130 | rc += 2;
|
|---|
| 131 | }
|
|---|
| 132 | else cout << " OK Test C = (A*4+1)/B OK " << endl;
|
|---|
| 133 |
|
|---|
| 134 | a = RegularSequence(1, 1);
|
|---|
| 135 | b = RegularSequence(nr*nc, -1);
|
|---|
| 136 | c = (a*2)+(b*2);
|
|---|
| 137 | if (prtlev > 0) {
|
|---|
| 138 | cout << " A = \n " << a << endl;
|
|---|
| 139 | cout << " B = \n " << b << endl;
|
|---|
| 140 | cout << " C = 2A-2*B= \n " << c << endl;
|
|---|
| 141 | }
|
|---|
| 142 | c /= (nr*nc+1);
|
|---|
| 143 | getMinMax(c, min, max);
|
|---|
| 144 | if ((min != 2) || (max != 2)) {
|
|---|
| 145 | cout << "!!! ERROR Test3 C=2A+2*B Min=" << min << " Max=" << max << endl;
|
|---|
| 146 | rc += 1;
|
|---|
| 147 | }
|
|---|
| 148 | else cout << " OK Test3 C=2A+2*B OK " << endl;
|
|---|
| 149 |
|
|---|
| 150 | return(rc);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | int test_odo(int nr, int nc)
|
|---|
| 154 | {
|
|---|
| 155 | cout << "\n -----> Testing TMatrix operation TMatrix<r_4>(nr=" << nr << ",nc="
|
|---|
| 156 | << nc << " )" << endl;
|
|---|
| 157 | cout << " A CMemoryMapping - B FortranMemoryMapping " << endl;
|
|---|
| 158 |
|
|---|
| 159 | int rc = 0;
|
|---|
| 160 | r_4 min,max;
|
|---|
| 161 |
|
|---|
| 162 | TMatrix<r_4> a(nr,nc,BaseArray::CMemoryMapping);
|
|---|
| 163 | a = 20;
|
|---|
| 164 | TMatrix<r_4> b(nr,nc,BaseArray::FortranMemoryMapping);
|
|---|
| 165 | b = 9;
|
|---|
| 166 | TMatrix<r_4> c(nr,nc,BaseArray::CMemoryMapping);
|
|---|
| 167 |
|
|---|
| 168 | if (prtlev > 0) {
|
|---|
| 169 | cout << " A = \n " << a << endl;
|
|---|
| 170 | cout << " B = \n " << b << endl;
|
|---|
| 171 | }
|
|---|
| 172 | c = a-(b*2.0f);
|
|---|
| 173 | if (prtlev > 0) cout << " C = A-2*B= \n " << c << endl;
|
|---|
| 174 |
|
|---|
| 175 | getMinMax(c, min, max);
|
|---|
| 176 | if ((fabs(min-2.) > 0.0001) || (fabs(max-2.) > 0.0001)) {
|
|---|
| 177 | cout << "!!! ERROR Test C=A+2*B Min=" << min << " Max=" << max << endl;
|
|---|
| 178 | rc += 1;
|
|---|
| 179 | }
|
|---|
| 180 | else cout << " OK Test C=A+2*B OK " << endl;
|
|---|
| 181 |
|
|---|
| 182 | c = (a*4.0f+5.5f).DivElt(b);
|
|---|
| 183 | if (prtlev > 0) cout << " C = (A*4+12)/B = \n" << c << endl;
|
|---|
| 184 | getMinMax(c, min, max);
|
|---|
| 185 | if ((fabs(min-9.5) > 0.0001) || (fabs(max-9.5) > 0.0001)) {
|
|---|
| 186 | cout << "!!! ERROR Test C = (A*4+12)/B Min=" << min << " Max=" << max << endl;
|
|---|
| 187 | rc += 2;
|
|---|
| 188 | }
|
|---|
| 189 | else cout << " OK Test C = (A*4+12)/B OK " << endl;
|
|---|
| 190 |
|
|---|
| 191 | a = RegularSequence(1, 1);
|
|---|
| 192 | b = RegularSequence(nr*nc, -1);
|
|---|
| 193 | c = (a*2.f)+(b*2.f);
|
|---|
| 194 | if (prtlev > 0) {
|
|---|
| 195 | cout << " A = \n " << a << endl;
|
|---|
| 196 | cout << " B = \n " << b << endl;
|
|---|
| 197 | cout << " C = 2A-2*B= \n " << c << endl;
|
|---|
| 198 | }
|
|---|
| 199 | c /= (nr*nc+1);
|
|---|
| 200 | getMinMax(c, min, max);
|
|---|
| 201 | if ((fabs(min-2.) > 0.0001) || (fabs(max-2.) > 0.0001)) {
|
|---|
| 202 | cout << "!!! ERROR Test3 C=2A+2*B Min=" << min << " Max=" << max << endl;
|
|---|
| 203 | rc += 1;
|
|---|
| 204 | }
|
|---|
| 205 | else cout << " OK Test3 C=2A+2*B OK " << endl;
|
|---|
| 206 |
|
|---|
| 207 | return(rc);
|
|---|
| 208 |
|
|---|
| 209 | }
|
|---|