1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* Copyright 2005, The University of Toronto
* Licensed under GPL.
*/
#include "ksttestcase.h"
#include <kstdataobjectcollection.h>
#include <kstamatrix.h>
#include <kstandarddirs.h>
static void exitHelper() {
KST::matrixList.clear();
KST::scalarList.clear();
KST::dataObjectList.clear();
}
int rc = KstTestSuccess;
#define doTest(x) testAssert(x, QString("Line %1").arg(__LINE__))
#define doTestD(x, y) testAssert(x, QString("%1: %2").arg(__LINE__).arg(y))
void testAssert(bool result, const QString& text = "Unknown") {
if (!result) {
KstTestFailed();
printf("Test [%s] failed.\n", text.toLatin1().data());
}
}
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) {
// Should be some boundary checking in the constructor.
QDomDocument amDOM("amdocument");
QDomElement amElement, child, dataset;
QDomText text;
amElement = amDOM.createElement("amDOMTest");
child = amDOM.createElement("tag");
text = amDOM.createTextNode(tag);
child.appendChild(text);
amElement.appendChild(child);
child = amDOM.createElement("nx");
text = amDOM.createTextNode(QString::number(nx));
child.appendChild(text);
amElement.appendChild(child);
child = amDOM.createElement("ny");
text = amDOM.createTextNode(QString::number(ny));
child.appendChild(text);
amElement.appendChild(child);
child = amDOM.createElement("xmin");
text = amDOM.createTextNode(QString::number(xmin));
child.appendChild(text);
amElement.appendChild(child);
child = amDOM.createElement("ymin");
text = amDOM.createTextNode(QString::number(ymin));
child.appendChild(text);
amElement.appendChild(child);
child = amDOM.createElement("xstep");
text = amDOM.createTextNode(QString::number(xstep));
child.appendChild(text);
amElement.appendChild(child);
child = amDOM.createElement("ystep");
text = amDOM.createTextNode(QString::number(ystep));
child.appendChild(text);
amElement.appendChild(child);
child = amDOM.createElement("data");
QByteArray qba;
qba.reserve(dataSize*sizeof(double));
QDataStream qds(&qba, QIODevice::WriteOnly);
for (int i = 0; i < dataSize; i++) {
qds << 1.1;
}
text = amDOM.createTextNode(QString(qCompress(qba).toBase64()));
child.appendChild(text);
amElement.appendChild(child);
amDOM.appendChild(amElement);
return amDOM;
}
void doTests() {
bool ok = true;
QDomNode n = makeDOMElement("amDOM", 0, 0, 0, 0, 1, 1, 9).firstChild();
QDomElement e = n.toElement();
//basic default constructor values
KstAMatrix* am1 = new KstAMatrix(e);
doTest(am1->tagName().startsWith("amDOM"));
doTest(am1->sampleCount() == 0);
doTest(am1->minValue() == 0);
doTest(am1->maxValue() == 0);
doTest(am1->value(0, 0, &ok) == 0);
doTest(!ok);
doTest(am1->value(10, 10, &ok) == 0); //should be outside the boundaries.
doTest(!ok);
doTest(am1->sampleCount() == 0);
doTest(am1->meanValue() == 0);
//basic symetrical matrix
n = makeDOMElement("Symetrical", 3, 3, 0, 0, 1, 1, 9).firstChild();
e = n.toElement();
//basic default constructor values
KstAMatrix* am2 = new KstAMatrix(e);
doTest(am2->tagName() == "Symetrical");
doTest(am2->resize(3, 3, true));
for(int i =0 ; i < 3; i++){
for(int j = 0; j < 3; j++){
doTest(am2->value(i, j, &ok) == 1.1);
doTest(ok);
}
}
doTest(am2->editable());
doTest(am2->xNumSteps() == 3);
doTest(am2->yNumSteps() == 3);
doTest(am2->minX() == 0);
doTest(am2->minY() == 0);
doTest(am2->xStepSize() == 1);
doTest(am2->yStepSize() == 1);
doTest(am2->sampleCount() == 9);
doTest(am2->setValueRaw(1, 1, 5));
ok = true;
doTest(am2->value(1, 1, &ok) == 5);
doTest(ok);
am2->blank();
am2->change(KstObjectTag::fromString(am2->tagName()), 3, 3, 0, 0, 0, 0); //should not be legal
doTest(am2->xNumSteps() == 3);
doTest(am2->yNumSteps() == 3);
doTest(am2->minX() == 0);
doTest(am2->minY() == 0);
doTest(am2->xStepSize() == 0);
doTest(am2->yStepSize() == 0);
doTest(am2->sampleCount() == 9);
doTest(!am2->setValue(0, 0, 1));
ok = true;
doTest(am2->value(0, 0, &ok) == 0);
doTest(!ok);
doTest(!am2->setValue(1, 1, 5.0));
doTest(am2->value(1, 1) != 5.0);
doTest(am2->setValueRaw(2, 2, 6.0)); //fails
KstAMatrix* um1 = new KstAMatrix(KstObjectTag::fromString("Unity"), 3, 3, 0.0, 0.0, 1.0, 1.0);
um1->setEditable(true);
doTest(um1->setValue(0, 0, 1));
doTest(um1->setValue(1, 1, 1));
doTest(um1->setValue(2, 2, 1));
doTest(um1->value(0, 0, &ok) == 1);
doTest(ok);
doTest(um1->value(0, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(0, 2, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 1, &ok) == 1);
doTest(ok);
doTest(um1->value(1, 2, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 2, &ok) == 1);
doTest(ok);
doTest(um1->resize(3, 3, false));
um1->zero();
doTest(um1->value(0, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(0, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(0, 2, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 2, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 2, &ok) == 0);
doTest(ok);
doTest(um1->setValue(0, 0, 1));
doTest(um1->setValue(1, 1, 1));
doTest(um1->setValue(2, 2, 1));
doTest(um1->resize(2, 2, false));
doTest(um1->sampleCount() == 4);
doTest(um1->value(0, 0, &ok) == 1);
doTest(ok);
doTest(um1->value(0, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(0, 2, &ok) == 0);
doTest(!ok);
doTest(um1->value(1, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 1, &ok) == 1);
doTest(ok);
doTest(um1->value(1, 2, &ok) == 0);
doTest(!ok);
doTest(um1->resize(4, 4, false));
doTest(um1->value(0, 0, &ok) == 1);
doTest(ok);
doTest(um1->value(0, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(0, 2, &ok) == 0);
doTest(ok);
doTest(um1->value(0, 3, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 1, &ok) == 1);
doTest(ok);
doTest(um1->value(1, 2, &ok) == 0);
doTest(ok);
doTest(um1->value(1, 3, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 2, &ok) == 0);
doTest(ok);
doTest(um1->value(2, 3, &ok) == 0);
doTest(ok);
doTest(um1->value(3, 0, &ok) == 0);
doTest(ok);
doTest(um1->value(3, 1, &ok) == 0);
doTest(ok);
doTest(um1->value(3, 2, &ok) == 0);
doTest(ok);
doTest(um1->value(3, 3, &ok) == 0);
doTest(ok);
doTest(um1->resize(3, 3, false));
doTest(um1->setValue(0, 0, 1.716299));
doTest(um1->setValue(0, 1, -0.485527));
doTest(um1->setValue(0, 2, -0.288690));
doTest(um1->setValue(1, 0, 1.716299));
doTest(um1->setValue(1, 1, NAN));
doTest(um1->setValue(1, 2, -0.274957));
doTest(um1->setValue(2, 0, 1.711721));
doTest(um1->setValue(2, 1, -0.485527));
doTest(um1->setValue(2, 2, -0.293267));
doTest(um1->value(0, 0) == 1.716299);
doTest(um1->value(0, 1) == -0.485527);
doTest(um1->value(0, 2) == -0.288690);
doTest(um1->value(1, 0) == 1.716299);
doTest(um1->value(1, 1) == 0);
doTest(um1->value(1, 2) == -0.274957);
doTest(um1->value(2, 0) == 1.711721);
doTest(um1->value(2, 1) == -0.485527);
doTest(um1->value(2, 2) == -0.293267);
doTest(um1->minValue() == 0);
doTest(um1->maxValue() == 0);
KstAMatrix* sm = new KstAMatrix(KstObjectTag::fromString("Spike"), 2, 2, 0.0, 0.0, 1.0, 1.0);
sm->setEditable(true);
doTest(sm->resize(2, 2, false));
doTest(sm->xNumSteps() == 2);
doTest(sm->yNumSteps() == 2);
doTest(sm->setValueRaw(0, 0, 0.0));
doTest(sm->setValueRaw(0, 1, 0.1));
doTest(sm->setValueRaw(1, 0, 1.0));
doTest(sm->setValueRaw(1, 1, 1.1));
sm->calcNoSpikeRange(0);
doTest(sm->minValueNoSpike() == 0.0);
doTest(sm->maxValueNoSpike() == 0.0);
sm->calcNoSpikeRange(-100);
doTest(sm->minValueNoSpike() == 0.0);
doTest(sm->maxValueNoSpike() == 0.0);
sm->calcNoSpikeRange(0.9);
doTest(sm->minValueNoSpike() >= 1E+300 );
doTest(sm->maxValueNoSpike() <= -1E+300);
}
int main(int argc, char **argv) {
atexit(exitHelper);
QCoreApplication app(argc, argv);
doTests();
// Don't put tests in main because we need to ensure that no KstObjects
// remain past the exit handler
exitHelper(); // need to run it here before app goes away in some cases.
if (rc == KstTestSuccess) {
printf("All tests passed!\n");
}
return -rc;
}
|