File indexing completed on 2024-05-19 04:32:48
0001 /* 0002 * Copyright 2004, The University of Toronto 0003 * Licensed under GPL. 0004 */ 0005 0006 #include "ksttestcase.h" 0007 #include <kstdataobjectcollection.h> 0008 #include <ksthistogram.h> 0009 #include <kstvector.h> 0010 0011 static void exitHelper() { 0012 KST::vectorList.clear(); 0013 KST::scalarList.clear(); 0014 KST::dataObjectList.clear(); 0015 } 0016 0017 0018 int rc = KstTestSuccess; 0019 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 0032 #define dumpPoints(histogram, n) do { \ 0033 for (int i = 0; i < n*4; ++i) { \ 0034 printf("%.15f, %.15f\n", histogram->vX()->value(i), histogram->vY()->value(i)); \ 0035 } } while(0) 0036 0037 void doTests() { 0038 KstVectorPtr vp = KstVector::generateVector(0, 10, 100, KstObjectTag::fromString("V1")); 0039 KstHistogramPtr h1 = new KstHistogram("H1", vp, 0, 10, 10, KST_HS_NUMBER); 0040 KST::dataObjectList.append(h1.data()); 0041 doTest(h1->propertyString() == "Histogram: V1"); 0042 doTest(!h1->realTimeAutoBin()); // should be false by default 0043 doTest(h1->nBins() == 10); 0044 h1->update(0); 0045 doTest(h1->vMin() == 0.0); 0046 doTest(h1->vMax() == 10.0); 0047 doTest(h1->vNumSamples() == 100); 0048 int count = 0; 0049 for (int i=0; i<10; i++) { 0050 count += int(h1->vY()->value(i)); 0051 } 0052 h1->setRealTimeAutoBin(true); 0053 doTest(h1->realTimeAutoBin()); 0054 //dumpPoints(h1, 10); 0055 doTest(count == 100); // should account for the whole vector 0056 h1->setNBins(11); 0057 doTest(!h1->realTimeAutoBin()); 0058 doTest(h1->nBins() == 11); 0059 doTest(h1->vMin() == 0.0); 0060 doTest(h1->vMax() == 10.0); 0061 doTest(h1->vNumSamples() == 100); 0062 h1->update(0); 0063 count = 0; 0064 for (int i=0; i<11; i++) { 0065 count += int(h1->vY()->value(i)); 0066 } 0067 //dumpPoints(h1, 11); 0068 doTest(count == 100); // should still account for the whole vector 0069 h1->setNBins(9); 0070 doTest(h1->nBins() == 9); 0071 doTest(h1->vMin() == 0.0); 0072 doTest(h1->vMax() == 10.0); 0073 doTest(h1->vNumSamples() == 100); 0074 h1->update(0); 0075 count = 0; 0076 for (int i=0; i<9; i++) { 0077 count += int(h1->vY()->value(i)); 0078 } 0079 //dumpPoints(h1, 9); 0080 doTest(count == 100); // should still account for the whole vector 0081 // min > max 0082 h1 = new KstHistogram("H2", vp, 10, 0, 10, KST_HS_NUMBER); 0083 doTest(h1->nBins() == 10); 0084 doTest(h1->xMin() == 0.0); 0085 doTest(h1->xMax() == 10.0); 0086 doTest(h1->vMin() == 0.0); 0087 doTest(h1->vMax() == 10.0); 0088 doTest(h1->vNumSamples() == 100); 0089 // min == max 0090 h1 = new KstHistogram("H3", vp, 10, 10, 2, KST_HS_NUMBER); 0091 doTest(h1->nBins() == 2); 0092 doTest(h1->xMin() == 9.0); 0093 doTest(h1->xMax() == 11.0); 0094 doTest(h1->vMin() == 0.0); 0095 doTest(h1->vMax() == 10.0); 0096 doTest(h1->vNumSamples() == 100); 0097 // max < min 0098 h1 = new KstHistogram("H4", vp, 11, 9, 1, KST_HS_NUMBER); 0099 doTest(h1->nBins() == 2); 0100 doTest(h1->xMax()==11); 0101 doTest(h1->xMin()==9); 0102 doTest(h1->vMin() == 0.0); 0103 doTest(h1->vMax() == 10.0); 0104 doTest(h1->vNumSamples() == 100); 0105 // set to max == min 0106 h1->setXRange(10, 10); 0107 doTest(h1->xMin() == 9.0); 0108 doTest(h1->xMax() == 11.0); 0109 // set to max > min 0110 h1->setXRange(1,2); 0111 doTest(h1->xMax() - h1->xMin() ==1.0); 0112 h1->setXRange(8, 10); 0113 doTest(h1->xMin() == 8.0); 0114 doTest(h1->xMax() == 10.0); 0115 } 0116 0117 0118 int main(int argc, char **argv) { 0119 atexit(exitHelper); 0120 0121 QCoreApplication app(argc, argv); 0122 0123 doTests(); 0124 0125 exitHelper(); // before app dies 0126 if (rc == KstTestSuccess) { 0127 printf("All tests passed!\n"); 0128 } 0129 return -rc; 0130 } 0131 0132 // vim: ts=2 sw=2 et