1 /*
2 * Copyright 2004, The University of Toronto
3 * Licensed under GPL.
4 */
5
6 #include "ksttestcase.h"
7 #include <kstdataobjectcollection.h>
8 #include <ksthistogram.h>
9 #include <kstvector.h>
10
exitHelper()11 static void exitHelper() {
12 KST::vectorList.clear();
13 KST::scalarList.clear();
14 KST::dataObjectList.clear();
15 }
16
17
18 int rc = KstTestSuccess;
19
20
21 #define doTest(x) testAssert(x, QString("Line %1").arg(__LINE__))
22 #define doTestD(x, y) testAssert(x, QString("%1: %2").arg(__LINE__).arg(y))
23
testAssert(bool result,const QString & text="Unknown")24 void testAssert(bool result, const QString& text = "Unknown") {
25 if (!result) {
26 KstTestFailed();
27 printf("Test [%s] failed.\n", text.toLatin1().data());
28 }
29 }
30
31
32 #define dumpPoints(histogram, n) do { \
33 for (int i = 0; i < n*4; ++i) { \
34 printf("%.15f, %.15f\n", histogram->vX()->value(i), histogram->vY()->value(i)); \
35 } } while(0)
36
doTests()37 void doTests() {
38 KstVectorPtr vp = KstVector::generateVector(0, 10, 100, KstObjectTag::fromString("V1"));
39 KstHistogramPtr h1 = new KstHistogram("H1", vp, 0, 10, 10, KST_HS_NUMBER);
40 KST::dataObjectList.append(h1.data());
41 doTest(h1->propertyString() == "Histogram: V1");
42 doTest(!h1->realTimeAutoBin()); // should be false by default
43 doTest(h1->nBins() == 10);
44 h1->update(0);
45 doTest(h1->vMin() == 0.0);
46 doTest(h1->vMax() == 10.0);
47 doTest(h1->vNumSamples() == 100);
48 int count = 0;
49 for (int i=0; i<10; i++) {
50 count += int(h1->vY()->value(i));
51 }
52 h1->setRealTimeAutoBin(true);
53 doTest(h1->realTimeAutoBin());
54 //dumpPoints(h1, 10);
55 doTest(count == 100); // should account for the whole vector
56 h1->setNBins(11);
57 doTest(!h1->realTimeAutoBin());
58 doTest(h1->nBins() == 11);
59 doTest(h1->vMin() == 0.0);
60 doTest(h1->vMax() == 10.0);
61 doTest(h1->vNumSamples() == 100);
62 h1->update(0);
63 count = 0;
64 for (int i=0; i<11; i++) {
65 count += int(h1->vY()->value(i));
66 }
67 //dumpPoints(h1, 11);
68 doTest(count == 100); // should still account for the whole vector
69 h1->setNBins(9);
70 doTest(h1->nBins() == 9);
71 doTest(h1->vMin() == 0.0);
72 doTest(h1->vMax() == 10.0);
73 doTest(h1->vNumSamples() == 100);
74 h1->update(0);
75 count = 0;
76 for (int i=0; i<9; i++) {
77 count += int(h1->vY()->value(i));
78 }
79 //dumpPoints(h1, 9);
80 doTest(count == 100); // should still account for the whole vector
81 // min > max
82 h1 = new KstHistogram("H2", vp, 10, 0, 10, KST_HS_NUMBER);
83 doTest(h1->nBins() == 10);
84 doTest(h1->xMin() == 0.0);
85 doTest(h1->xMax() == 10.0);
86 doTest(h1->vMin() == 0.0);
87 doTest(h1->vMax() == 10.0);
88 doTest(h1->vNumSamples() == 100);
89 // min == max
90 h1 = new KstHistogram("H3", vp, 10, 10, 2, KST_HS_NUMBER);
91 doTest(h1->nBins() == 2);
92 doTest(h1->xMin() == 9.0);
93 doTest(h1->xMax() == 11.0);
94 doTest(h1->vMin() == 0.0);
95 doTest(h1->vMax() == 10.0);
96 doTest(h1->vNumSamples() == 100);
97 // max < min
98 h1 = new KstHistogram("H4", vp, 11, 9, 1, KST_HS_NUMBER);
99 doTest(h1->nBins() == 2);
100 doTest(h1->xMax()==11);
101 doTest(h1->xMin()==9);
102 doTest(h1->vMin() == 0.0);
103 doTest(h1->vMax() == 10.0);
104 doTest(h1->vNumSamples() == 100);
105 // set to max == min
106 h1->setXRange(10, 10);
107 doTest(h1->xMin() == 9.0);
108 doTest(h1->xMax() == 11.0);
109 // set to max > min
110 h1->setXRange(1,2);
111 doTest(h1->xMax() - h1->xMin() ==1.0);
112 h1->setXRange(8, 10);
113 doTest(h1->xMin() == 8.0);
114 doTest(h1->xMax() == 10.0);
115 }
116
117
main(int argc,char ** argv)118 int main(int argc, char **argv) {
119 atexit(exitHelper);
120
121 QCoreApplication app(argc, argv);
122
123 doTests();
124
125 exitHelper(); // before app dies
126 if (rc == KstTestSuccess) {
127 printf("All tests passed!\n");
128 }
129 return -rc;
130 }
131
132 // vim: ts=2 sw=2 et
133