00001 #include "Logger.h"
00002 #include "MainWindow.h"
00003 #include <qmath.h>
00004 #include <QtTest/QtTest>
00005 #include "Test/TestMatrix.h"
00006
00007 QTEST_MAIN (TestMatrix)
00008
00009 TestMatrix::TestMatrix(QObject *parent) :
00010 QObject(parent)
00011 {
00012 }
00013
00014 void TestMatrix::cleanupTestCase ()
00015 {
00016 }
00017
00018 void TestMatrix::initTestCase ()
00019 {
00020 const QString NO_ERROR_REPORT_LOG_FILE;
00021 const QString NO_REGRESSION_OPEN_FILE;
00022 const bool NO_GNUPLOT_LOG_FILES = false;
00023 const bool NO_REGRESSION_IMPORT = false;
00024 const bool NO_RESET = false;
00025 const bool DEBUG_FLAG = false;
00026 const QStringList NO_LOAD_STARTUP_FILES;
00027
00028 initializeLogging ("engauge_test",
00029 "engauge_test.log",
00030 DEBUG_FLAG);
00031
00032 MainWindow w (NO_ERROR_REPORT_LOG_FILE,
00033 NO_REGRESSION_OPEN_FILE,
00034 NO_GNUPLOT_LOG_FILES,
00035 NO_REGRESSION_IMPORT,
00036 NO_RESET,
00037 NO_LOAD_STARTUP_FILES);
00038 w.show ();
00039 }
00040
00041 void TestMatrix::testDeterminant ()
00042 {
00043 Matrix m (3);
00044 double a00 = 1, a01 = 2, a10 = 3, a11 = 4;
00045
00046 m.set (0, 0, a00);
00047 m.set (0, 1, a01);
00048 m.set (1, 0, a10);
00049 m.set (1, 1, a11);
00050 QVERIFY ((m.determinant () == a00 * a11 - a01 * a10));
00051 }
00052
00053 void TestMatrix::testInverse ()
00054 {
00055 bool success = true;
00056 int row, col;
00057
00058
00059
00060 Matrix before (3);
00061 int counter = 0;
00062 for (row = 0; row < 3; row++) {
00063 for (col = 0; col < 3; col++) {
00064 before.set (row, col, ++counter);
00065 }
00066 }
00067 before.set (2, 2, 10);
00068
00069 Matrix after = before.inverse ();
00070
00071 Matrix product = before * after;
00072 Matrix identity (3);
00073 for (row = 0; row < 3; row++) {
00074 for (col = 0; col < 3; col++) {
00075 if (qAbs (product.get (row, col) - identity.get (row, col)) > 0.00001) {
00076 success = false;
00077 break;
00078 }
00079 }
00080 }
00081
00082 QVERIFY (success);
00083 }
00084
00085 void TestMatrix::testInverse2 ()
00086 {
00087 bool success = true;
00088 int row, col;
00089
00090
00091 Matrix before (2);
00092 before.set (0, 0, 2);
00093 before.set (0, 1, 1);
00094 before.set (1, 0, 1);
00095 before.set (1, 1, 1);
00096
00097 Matrix after = before.inverse ();
00098
00099 Matrix product = before * after;
00100 Matrix identity (2);
00101 for (row = 0; row < 2; row++) {
00102 for (col = 0; col < 2; col++) {
00103 if (qAbs (product.get (row, col) - identity.get (row, col)) > 0.00001) {
00104 success = false;
00105 break;
00106 }
00107 }
00108 }
00109
00110 QVERIFY (success);
00111 }
00112
00113 void TestMatrix::testMultiplyNonSquareMatrix ()
00114 {
00115 bool success = true;
00116 int row, col;
00117
00118
00119 Matrix before (2, 3);
00120 int counter = 0;
00121 for (row = 0; row < 2; row++) {
00122 for (col = 0; col < 3; col++) {
00123 before.set (row, col, ++counter);
00124 }
00125 }
00126
00127
00128 Matrix afterGot = before * before.transpose ();
00129 Matrix afterWanted (2);
00130
00131 if (afterGot.rows () == afterWanted.rows () &&
00132 afterGot.cols () == afterWanted.cols ()) {
00133
00134 afterWanted.set (0, 0, 1 * 1 + 2 * 2 + 3 * 3);
00135 afterWanted.set (0, 1, 1 * 4 + 2 * 5 + 3 * 6);
00136 afterWanted.set (1, 0, 4 * 1 + 5 * 2 + 6 * 3);
00137 afterWanted.set (1, 1, 4 * 4 + 5 * 5 + 6 * 6);
00138
00139 for (row = 0; row < 2; row++) {
00140 for (col = 0; col < 2; col++) {
00141 if (qAbs (afterWanted.get (row, col) - afterGot.get (row, col)) > 0.0001) {
00142 success = false;
00143 break;
00144 }
00145 }
00146 }
00147 } else {
00148 success = false;
00149 }
00150
00151 QVERIFY (success);
00152 }
00153
00154 void TestMatrix::testMultiplyNonSquareMatrixAndVector ()
00155 {
00156 bool success = true;
00157 int row, col;
00158
00159
00160 Matrix before (2, 3);
00161 QVector<double> vec (3);
00162 int counter = 0;
00163 for (row = 0; row < 2; row++) {
00164 for (col = 0; col < 3; col++) {
00165 before.set (row, col, ++counter);
00166 vec [col] = col + 1;
00167 }
00168 }
00169
00170
00171 QVector<double> afterGot = before * vec;
00172 QVector<double> afterWanted (2);
00173
00174 if (afterGot.size () == afterWanted.size ()) {
00175
00176 afterWanted [0] = 1 * 1 + 2 * 2 + 3 * 3;
00177 afterWanted [1] = 4 * 1 + 5 * 2 + 6 * 3;
00178
00179 for (row = 0; row < 2; row++) {
00180 if (qAbs (afterWanted [row] - afterGot [row]) > 0.0001) {
00181 success = false;
00182 break;
00183 }
00184 }
00185 } else {
00186 success = false;
00187 }
00188
00189 QVERIFY (success);
00190 }
00191
00192 void TestMatrix::testMultiplySquareMatrix ()
00193 {
00194 bool success = true;
00195 int row, col;
00196
00197
00198 Matrix before (3);
00199 int counter = 0;
00200 for (row = 0; row < 3; row++) {
00201 for (col = 0; col < 3; col++) {
00202 before.set (row, col, ++counter);
00203 }
00204 }
00205
00206
00207 Matrix afterGot = before * before;
00208 Matrix afterWanted (3);
00209
00210 if (afterGot.rows() == afterWanted.rows() &&
00211 afterGot.cols() == afterWanted.cols()) {
00212
00213 afterWanted.set (0, 0, 1 * 1 + 2 * 4 + 3 * 7);
00214 afterWanted.set (0, 1, 1 * 2 + 2 * 5 + 3 * 8);
00215 afterWanted.set (0, 2, 1 * 3 + 2 * 6 + 3 * 9);
00216 afterWanted.set (1, 0, 4 * 1 + 5 * 4 + 6 * 7);
00217 afterWanted.set (1, 1, 4 * 2 + 5 * 5 + 6 * 8);
00218 afterWanted.set (1, 2, 4 * 3 + 5 * 6 + 6 * 9);
00219 afterWanted.set (2, 0, 7 * 1 + 8 * 4 + 9 * 7);
00220 afterWanted.set (2, 1, 7 * 2 + 8 * 5 + 9 * 8);
00221 afterWanted.set (2, 2, 7 * 3 + 8 * 6 + 9 * 9);
00222
00223 for (row = 0; row < 3; row++) {
00224 for (col = 0; col < 3; col++) {
00225 if (qAbs (afterWanted.get (row, col) - afterGot.get (row, col)) > 0.0001) {
00226 success = false;
00227 break;
00228 }
00229 }
00230 }
00231 } else {
00232 success = false;
00233 }
00234
00235 QVERIFY (success);
00236 }
00237
00238 void TestMatrix::testMultiplySquareMatrixAndVector ()
00239 {
00240 bool success = true;
00241 int row, col;
00242
00243
00244 Matrix before (3);
00245 QVector<double> vec (3);
00246 int counter = 0;
00247 for (row = 0; row < 3; row++) {
00248 for (col = 0; col < 3; col++) {
00249 before.set (row, col, ++counter);
00250 }
00251 vec [row] = row + 1;
00252 }
00253
00254
00255 QVector<double> afterGot = before * vec;
00256 QVector<double> afterWanted (3);
00257
00258 if (afterGot.size() == afterWanted.size()) {
00259
00260 afterWanted [0] = 1 * 1 + 2 * 2 + 3 * 3;
00261 afterWanted [1] = 4 * 1 + 5 * 2 + 6 * 3;
00262 afterWanted [2] = 7 * 1 + 8 * 2 + 9 * 3;
00263
00264 for (row = 0; row < 3; row++) {
00265 if (qAbs (afterWanted [row] - afterGot [row]) > 0.0001) {
00266 success = false;
00267 break;
00268 }
00269 }
00270 } else {
00271 success = false;
00272 }
00273
00274 QVERIFY (success);
00275 }
00276
00277 void TestMatrix::testTranspose ()
00278 {
00279 bool success = true;
00280 int row, col;
00281
00282
00283 Matrix before (3);
00284 int counter = 0;
00285 for (row = 0; row < 3; row++) {
00286 for (col = 0; col < 3; col++) {
00287 before.set (row, col, ++counter);
00288 }
00289 }
00290
00291 Matrix after = before.transpose ();
00292 for (row = 0; row < 3; row++) {
00293 for (col = 0; col < 3; col++) {
00294 if (before.get (row, col) != after.get (col, row)) {
00295 success = false;
00296 break;
00297 }
00298 }
00299 }
00300
00301 QVERIFY (success);
00302 }