File indexing completed on 2024-05-19 04:32:47

0001 /*
0002  *  Copyright 2005, The University of Toronto
0003  *  Licensed under GPL.
0004  */
0005 
0006 #include "ksttestcase.h"
0007 #include <kstdataobjectcollection.h>
0008 #include <kstamatrix.h>
0009 #include <kstandarddirs.h>
0010 
0011 
0012 static void exitHelper() {
0013   KST::matrixList.clear();
0014   KST::scalarList.clear();
0015   KST::dataObjectList.clear();
0016 }
0017 
0018 
0019 int rc = KstTestSuccess;
0020 
0021 #define doTest(x) testAssert(x, QString("Line %1").arg(__LINE__))
0022 #define doTestD(x, y) testAssert(x, QString("%1: %2").arg(__LINE__).arg(y))
0023 
0024 void testAssert(bool result, const QString& text = "Unknown") {
0025   if (!result) {
0026     KstTestFailed();
0027     printf("Test [%s] failed.\n", text.toLatin1().data());
0028   }
0029 }
0030 
0031 QDomDocument makeDOMElement(const QString& tag, const int nx, const int ny, const double xmin, const double ymin, const double xstep, const double ystep, const int dataSize) {
0032 // Should be some boundary checking in the constructor.
0033   QDomDocument amDOM("amdocument");
0034   QDomElement amElement, child, dataset;
0035   QDomText text;
0036 
0037   amElement = amDOM.createElement("amDOMTest");
0038 
0039   child = amDOM.createElement("tag");
0040   text = amDOM.createTextNode(tag);
0041   child.appendChild(text);
0042   amElement.appendChild(child);
0043 
0044   child = amDOM.createElement("nx");
0045   text = amDOM.createTextNode(QString::number(nx));
0046   child.appendChild(text);
0047   amElement.appendChild(child);
0048 
0049   child = amDOM.createElement("ny");
0050   text = amDOM.createTextNode(QString::number(ny));
0051   child.appendChild(text);
0052   amElement.appendChild(child);
0053 
0054   child = amDOM.createElement("xmin");
0055   text = amDOM.createTextNode(QString::number(xmin));
0056   child.appendChild(text);
0057   amElement.appendChild(child);
0058 
0059   child = amDOM.createElement("ymin");
0060   text = amDOM.createTextNode(QString::number(ymin));
0061   child.appendChild(text);
0062   amElement.appendChild(child);
0063 
0064   child = amDOM.createElement("xstep");
0065   text = amDOM.createTextNode(QString::number(xstep));
0066   child.appendChild(text);
0067   amElement.appendChild(child);
0068 
0069   child = amDOM.createElement("ystep");
0070   text = amDOM.createTextNode(QString::number(ystep));
0071   child.appendChild(text);
0072   amElement.appendChild(child);
0073 
0074 
0075   child = amDOM.createElement("data");
0076   QByteArray qba;
0077   qba.reserve(dataSize*sizeof(double));
0078   QDataStream qds(&qba, QIODevice::WriteOnly);
0079 
0080   for (int i = 0; i < dataSize; i++) {
0081     qds << 1.1;
0082   }
0083   
0084   text = amDOM.createTextNode(QString(qCompress(qba).toBase64()));
0085 
0086   child.appendChild(text);
0087   amElement.appendChild(child);
0088 
0089   amDOM.appendChild(amElement);
0090 
0091   return amDOM;
0092 }
0093 
0094 void doTests() {
0095   bool ok = true;
0096   
0097   QDomNode n = makeDOMElement("amDOM", 0, 0, 0, 0, 1, 1, 9).firstChild();
0098   QDomElement e = n.toElement();
0099 
0100   //basic default constructor values
0101   KstAMatrix* am1 = new KstAMatrix(e);
0102   doTest(am1->tagName().startsWith("amDOM"));
0103   doTest(am1->sampleCount() == 0);
0104   doTest(am1->minValue() == 0);
0105   doTest(am1->maxValue() == 0);
0106   doTest(am1->value(0, 0, &ok) == 0);
0107   doTest(!ok);
0108   doTest(am1->value(10, 10, &ok) == 0); //should be outside the boundaries.
0109   doTest(!ok);
0110   doTest(am1->sampleCount() == 0);
0111   doTest(am1->meanValue() == 0);
0112 
0113   //basic symetrical matrix
0114   n = makeDOMElement("Symetrical", 3, 3, 0, 0, 1, 1, 9).firstChild();
0115   e = n.toElement();
0116 
0117   //basic default constructor values
0118   KstAMatrix* am2 = new KstAMatrix(e);
0119   
0120   doTest(am2->tagName() == "Symetrical");
0121   doTest(am2->resize(3, 3, true));
0122 
0123   for(int i =0 ; i < 3; i++){
0124     for(int j = 0; j < 3; j++){
0125       doTest(am2->value(i, j, &ok) == 1.1);
0126       doTest(ok);
0127     }
0128   }
0129 
0130   doTest(am2->editable());
0131   doTest(am2->xNumSteps() == 3);
0132   doTest(am2->yNumSteps() == 3);
0133   doTest(am2->minX() == 0);
0134   doTest(am2->minY() == 0);
0135   doTest(am2->xStepSize() == 1);
0136   doTest(am2->yStepSize() == 1);
0137   doTest(am2->sampleCount() == 9);
0138 
0139   doTest(am2->setValueRaw(1, 1, 5));
0140   ok = true;
0141   doTest(am2->value(1, 1, &ok) == 5);
0142   doTest(ok);
0143 
0144   am2->blank();
0145 
0146   am2->change(KstObjectTag::fromString(am2->tagName()), 3, 3, 0, 0, 0, 0); //should not be legal
0147   doTest(am2->xNumSteps() == 3);
0148   doTest(am2->yNumSteps() == 3);
0149   doTest(am2->minX() == 0);
0150   doTest(am2->minY() == 0);
0151   doTest(am2->xStepSize() == 0);
0152   doTest(am2->yStepSize() == 0);
0153   doTest(am2->sampleCount() == 9);
0154 
0155   doTest(!am2->setValue(0, 0, 1));
0156   ok = true;
0157   doTest(am2->value(0, 0, &ok) == 0);
0158   doTest(!ok);
0159 
0160   doTest(!am2->setValue(1, 1, 5.0));
0161   doTest(am2->value(1, 1) != 5.0);
0162   doTest(am2->setValueRaw(2, 2, 6.0)); //fails
0163 
0164   KstAMatrix* um1 = new KstAMatrix(KstObjectTag::fromString("Unity"), 3, 3, 0.0, 0.0, 1.0, 1.0);
0165   um1->setEditable(true);
0166   doTest(um1->setValue(0, 0, 1));
0167   doTest(um1->setValue(1, 1, 1));
0168   doTest(um1->setValue(2, 2, 1));
0169 
0170   doTest(um1->value(0, 0, &ok) == 1);
0171   doTest(ok);
0172   doTest(um1->value(0, 1, &ok) == 0);
0173   doTest(ok);
0174   doTest(um1->value(0, 2, &ok) == 0);
0175   doTest(ok);
0176   doTest(um1->value(1, 0, &ok) == 0);
0177   doTest(ok);
0178   doTest(um1->value(1, 1, &ok) == 1);
0179   doTest(ok);
0180   doTest(um1->value(1, 2, &ok) == 0);
0181   doTest(ok);
0182   doTest(um1->value(2, 0, &ok) == 0);
0183   doTest(ok);
0184   doTest(um1->value(2, 1, &ok) == 0);
0185   doTest(ok);
0186   doTest(um1->value(2, 2, &ok) == 1);
0187   doTest(ok);
0188 
0189   doTest(um1->resize(3, 3, false));
0190   um1->zero();
0191   doTest(um1->value(0, 0, &ok) == 0);
0192   doTest(ok);
0193   doTest(um1->value(0, 1, &ok) == 0);
0194   doTest(ok);
0195   doTest(um1->value(0, 2, &ok) == 0);
0196   doTest(ok);
0197   doTest(um1->value(1, 0, &ok) == 0);
0198   doTest(ok);
0199   doTest(um1->value(1, 1, &ok) == 0);
0200   doTest(ok);
0201   doTest(um1->value(1, 2, &ok) == 0);
0202   doTest(ok);
0203   doTest(um1->value(2, 0, &ok) == 0);
0204   doTest(ok);
0205   doTest(um1->value(2, 1, &ok) == 0);
0206   doTest(ok);
0207   doTest(um1->value(2, 2, &ok) == 0);
0208   doTest(ok);
0209   
0210   doTest(um1->setValue(0, 0, 1));
0211   doTest(um1->setValue(1, 1, 1));
0212   doTest(um1->setValue(2, 2, 1));
0213 
0214   doTest(um1->resize(2, 2, false));
0215   doTest(um1->sampleCount() == 4); 
0216 
0217   doTest(um1->value(0, 0, &ok) == 1);
0218   doTest(ok);
0219   doTest(um1->value(0, 1, &ok) == 0);
0220   doTest(ok);
0221   doTest(um1->value(0, 2, &ok) == 0);
0222   doTest(!ok);
0223   doTest(um1->value(1, 0, &ok) == 0);
0224   doTest(ok);
0225   doTest(um1->value(1, 1, &ok) == 1);
0226   doTest(ok);
0227   doTest(um1->value(1, 2, &ok) == 0);
0228   doTest(!ok);
0229 
0230   doTest(um1->resize(4, 4, false));
0231   doTest(um1->value(0, 0, &ok) == 1);
0232   doTest(ok);
0233   doTest(um1->value(0, 1, &ok) == 0);
0234   doTest(ok);
0235   doTest(um1->value(0, 2, &ok) == 0);
0236   doTest(ok);
0237   doTest(um1->value(0, 3, &ok) == 0);
0238   doTest(ok);
0239   doTest(um1->value(1, 0, &ok) == 0);
0240   doTest(ok);
0241   doTest(um1->value(1, 1, &ok) == 1);
0242   doTest(ok);
0243   doTest(um1->value(1, 2, &ok) == 0);
0244   doTest(ok);
0245   doTest(um1->value(1, 3, &ok) == 0);
0246   doTest(ok);
0247   doTest(um1->value(2, 0, &ok) == 0);
0248   doTest(ok);
0249   doTest(um1->value(2, 1, &ok) == 0);
0250   doTest(ok);
0251   doTest(um1->value(2, 2, &ok) == 0);
0252   doTest(ok);
0253   doTest(um1->value(2, 3, &ok) == 0);
0254   doTest(ok);
0255   doTest(um1->value(3, 0, &ok) == 0);
0256   doTest(ok);
0257   doTest(um1->value(3, 1, &ok) == 0);
0258   doTest(ok);
0259   doTest(um1->value(3, 2, &ok) == 0);
0260   doTest(ok);
0261   doTest(um1->value(3, 3, &ok) == 0);
0262   doTest(ok);
0263 
0264 
0265 
0266   doTest(um1->resize(3, 3, false));
0267   doTest(um1->setValue(0, 0, 1.716299));
0268   doTest(um1->setValue(0, 1, -0.485527));
0269   doTest(um1->setValue(0, 2, -0.288690));
0270   doTest(um1->setValue(1, 0, 1.716299));
0271   doTest(um1->setValue(1, 1, NAN));
0272   doTest(um1->setValue(1, 2, -0.274957));
0273   doTest(um1->setValue(2, 0, 1.711721));
0274   doTest(um1->setValue(2, 1, -0.485527));
0275   doTest(um1->setValue(2, 2, -0.293267));
0276 
0277   doTest(um1->value(0, 0) == 1.716299);
0278   doTest(um1->value(0, 1) ==  -0.485527);
0279   doTest(um1->value(0, 2) == -0.288690);
0280   doTest(um1->value(1, 0) == 1.716299);
0281   doTest(um1->value(1, 1) == 0);
0282   doTest(um1->value(1, 2) == -0.274957);
0283   doTest(um1->value(2, 0) == 1.711721);
0284   doTest(um1->value(2, 1) == -0.485527);
0285   doTest(um1->value(2, 2) == -0.293267);
0286 
0287   doTest(um1->minValue() == 0);
0288   doTest(um1->maxValue() == 0);
0289 
0290   KstAMatrix* sm = new KstAMatrix(KstObjectTag::fromString("Spike"), 2, 2, 0.0, 0.0, 1.0, 1.0);
0291   
0292   sm->setEditable(true);
0293   doTest(sm->resize(2, 2, false));
0294   doTest(sm->xNumSteps() == 2);
0295   doTest(sm->yNumSteps() == 2);
0296 
0297   doTest(sm->setValueRaw(0, 0, 0.0));
0298   doTest(sm->setValueRaw(0, 1, 0.1));
0299   doTest(sm->setValueRaw(1, 0, 1.0));
0300   doTest(sm->setValueRaw(1, 1, 1.1));
0301 
0302   sm->calcNoSpikeRange(0);
0303   doTest(sm->minValueNoSpike() == 0.0);
0304   doTest(sm->maxValueNoSpike() == 0.0);
0305 
0306   sm->calcNoSpikeRange(-100);
0307   doTest(sm->minValueNoSpike() == 0.0);
0308   doTest(sm->maxValueNoSpike() == 0.0);
0309   
0310   sm->calcNoSpikeRange(0.9);
0311   doTest(sm->minValueNoSpike() >= 1E+300 );
0312   doTest(sm->maxValueNoSpike() <= -1E+300);
0313 }
0314 
0315 
0316 int main(int argc, char **argv) {
0317   atexit(exitHelper);
0318 
0319   QCoreApplication app(argc, argv);
0320 
0321   doTests();
0322   // Don't put tests in main because we need to ensure that no KstObjects
0323   // remain past the exit handler
0324 
0325   exitHelper(); // need to run it here before app goes away in some cases.
0326   if (rc == KstTestSuccess) {
0327     printf("All tests passed!\n");
0328   }
0329   return -rc;
0330 }