File indexing completed on 2024-05-19 04:32:48
0001 /* 0002 * Copyright 2005, The University of Toronto 0003 * Licensed under GPL. 0004 */ 0005 0006 #include "ksttestcase.h" 0007 // #include <kstdataobjectcollection.h> 0008 #include <kstsmatrix.h> 0009 0010 0011 0012 static void exitHelper() { 0013 KST::matrixList.clear(); 0014 KST::scalarList.clear(); 0015 } 0016 0017 0018 int rc = KstTestSuccess; 0019 0020 #define doTest(x) testAssert(x, QString("Line %1").arg(__LINE__)) 0021 #define doTestD(x, y) testAssert(x, QString("%1: %2").arg(__LINE__).arg(y)) 0022 0023 void testAssert(bool result, const QString& text = "Unknown") { 0024 if (!result) { 0025 KstTestFailed(); 0026 printf("Test [%s] failed.\n", text.toLatin1().data()); 0027 } 0028 } 0029 0030 QDomDocument makeDOMElem(const QString& tag, const int nx, const int ny, const double xmin, const double ymin, const double xstep, const double ystep, const double gradzmin, const double gradzmax, const bool xdirection) { 0031 // Should be some boundary checking in the constructor. 0032 QDomDocument smDOM("smdocument"); 0033 QDomElement smElement, child, dataset; 0034 QDomText text; 0035 0036 smElement = smDOM.createElement("smDOMTest"); 0037 0038 child = smDOM.createElement("tag"); 0039 text = smDOM.createTextNode(tag); 0040 child.appendChild(text); 0041 smElement.appendChild(child); 0042 0043 child = smDOM.createElement("nx"); 0044 text = smDOM.createTextNode(QString::number(nx)); 0045 child.appendChild(text); 0046 smElement.appendChild(child); 0047 0048 child = smDOM.createElement("ny"); 0049 text = smDOM.createTextNode(QString::number(ny)); 0050 child.appendChild(text); 0051 smElement.appendChild(child); 0052 0053 child = smDOM.createElement("xmin"); 0054 text = smDOM.createTextNode(QString::number(xmin)); 0055 child.appendChild(text); 0056 smElement.appendChild(child); 0057 0058 child = smDOM.createElement("ymin"); 0059 text = smDOM.createTextNode(QString::number(ymin)); 0060 child.appendChild(text); 0061 smElement.appendChild(child); 0062 0063 child = smDOM.createElement("xstep"); 0064 text = smDOM.createTextNode(QString::number(xstep)); 0065 child.appendChild(text); 0066 smElement.appendChild(child); 0067 0068 child = smDOM.createElement("ystep"); 0069 text = smDOM.createTextNode(QString::number(ystep)); 0070 child.appendChild(text); 0071 smElement.appendChild(child); 0072 0073 child = smDOM.createElement("gradzmin"); 0074 text = smDOM.createTextNode(QString::number(gradzmin)); 0075 child.appendChild(text); 0076 smElement.appendChild(child); 0077 0078 child = smDOM.createElement("gradzmax"); 0079 text = smDOM.createTextNode(QString::number(gradzmax)); 0080 child.appendChild(text); 0081 smElement.appendChild(child); 0082 0083 child = smDOM.createElement("xdirection"); 0084 text = smDOM.createTextNode(QString::number(xdirection)); 0085 child.appendChild(text); 0086 smElement.appendChild(child); 0087 0088 smDOM.appendChild(smElement); 0089 0090 return smDOM; 0091 } 0092 0093 void doTests() { 0094 bool ok = true; 0095 0096 QDomNode n = makeDOMElem("smDOM", 0, 0, 0, 0, 1, 1, 1, 1, true).firstChild(); 0097 QDomElement e = n.toElement(); 0098 0099 //basic default constructor values 0100 KstSMatrix* sm1 = new KstSMatrix(e); 0101 doTest(sm1->tagName().startsWith("smDOM")); 0102 doTest(sm1->sampleCount() == 1); 0103 doTest(sm1->minValue() == 0); 0104 doTest(sm1->maxValue() == 0); 0105 sm1->value(0, 0, &ok); // undefined value 0106 doTest(ok); 0107 doTest(sm1->value(10, 10, &ok) == 0); //should be outside the boundaries. 0108 doTest(!ok); 0109 doTest(sm1->sampleCount() == 1); 0110 // meaningless doTest(sm1->meanValue() == 0); 0111 0112 //basic symetrical matrix 0113 n = makeDOMElem("Symetrical", 3, 3, 0, 0, 1, 1, 1, 1, true).firstChild(); 0114 e = n.toElement(); 0115 0116 //basic default constructor values 0117 KstSMatrix* sm2 = new KstSMatrix(e); 0118 0119 doTest(sm2->tagName() == "Symetrical"); 0120 doTest(sm2->resize(3, 3, true)); 0121 0122 doTest(sm2->editable()); 0123 doTest(sm2->xNumSteps() == 3); 0124 doTest(sm2->yNumSteps() == 3); 0125 doTest(sm2->minX() == 0); 0126 doTest(sm2->minY() == 0); 0127 doTest(sm2->xStepSize() == 1); 0128 doTest(sm2->yStepSize() == 1); 0129 doTest(sm2->sampleCount() == 9); 0130 0131 doTest(sm2->setValueRaw(1, 1, 5)); 0132 ok = true; 0133 doTest(sm2->value(1, 1, &ok) == 5); 0134 doTest(ok); 0135 0136 sm2->blank(); 0137 0138 sm2->change(KstObjectTag::fromString(sm2->tagName()), 3, 3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, false); //should not be legal 0139 doTest(sm2->xNumSteps() == 3); 0140 doTest(sm2->yNumSteps() == 3); 0141 doTest(sm2->minX() == 0); 0142 doTest(sm2->minY() == 0); 0143 // meaningless 0144 //doTest(sm2->xStepSize() == 0); 0145 //doTest(sm2->yStepSize() == 0); 0146 doTest(sm2->sampleCount() == 9); 0147 0148 doTest(sm2->setValue(0, 0, 1.0)); 0149 ok = true; 0150 doTest(sm2->value(0, 0, &ok) == 1.0); 0151 doTest(ok); 0152 0153 doTest(!sm2->setValue(1, 1, 5.0)); 0154 doTest(sm2->value(1, 1) != 5.0); 0155 doTest(sm2->setValueRaw(2, 2, 6.0)); //fails 0156 0157 KstSMatrix* um1 = new KstSMatrix(KstObjectTag::fromString("Unity"), 3, 3, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, false); 0158 0159 um1->setEditable(true); 0160 doTest(um1->resize(3, 3, false)); 0161 doTest(um1->setValueRaw(0, 0, 1.0)); 0162 doTest(um1->setValueRaw(1, 1, 1.0)); 0163 doTest(um1->setValueRaw(2, 2, 1.0)); 0164 doTest(um1->value(0, 0, &ok) == 1.0); 0165 doTest(um1->value(1, 1, &ok) == 1.0); 0166 doTest(um1->value(2, 2, &ok) == 1.0); 0167 doTest(ok); 0168 0169 doTest(um1->setValue(0, 0, 1.716299)); 0170 doTest(um1->setValue(0, 1, -0.485527)); 0171 doTest(um1->setValue(0, 2, -0.288690)); 0172 doTest(um1->setValue(1, 0, 1.716299)); 0173 doTest(um1->setValue(1, 1, NAN)); 0174 doTest(um1->setValue(1, 2, -0.274957)); 0175 doTest(um1->setValue(2, 0, 1.711721)); 0176 doTest(um1->setValue(2, 1, -0.485527)); 0177 doTest(um1->setValue(2, 2, -0.293267)); 0178 0179 doTest(um1->minValue() == 0); 0180 doTest(um1->maxValue() == 0); 0181 0182 um1->calcNoSpikeRange(1.9); //this wil produce no change because the variable _NS has no default 0183 doTest(um1->maxValueNoSpike() == 0.0); 0184 doTest(um1->minValueNoSpike() == 0.0); 0185 0186 0187 // KTempFile tf(locateLocal("tmp", "kst-smatrix"), "xml"); 0188 // QFile *qf = tf.file(); 0189 // QTextStream ts(qf); 0190 // sm2->save(ts, " "); 0191 // tf.close(); 0192 // QFile::remove(tf.name()); 0193 0194 } 0195 0196 0197 int main(int argc, char **argv) { 0198 atexit(exitHelper); 0199 0200 QCoreApplication app(argc, argv); 0201 0202 doTests(); 0203 // Don't put tests in main because we need to ensure that no KstObjects 0204 // remain past the exit handler 0205 0206 exitHelper(); // need to run it here before kapp goes away in some cases. 0207 if (rc == KstTestSuccess) { 0208 printf("All tests passed!\n"); 0209 } 0210 return -rc; 0211 }