File indexing completed on 2024-05-19 04:34:08

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  ***************************************************************************/
0011 
0012 #include "testhistogram.h"
0013 
0014 #include <QtTest>
0015 
0016 #include <generatedvector.h>
0017 #include <datacollection.h>
0018 #include <objectstore.h>
0019 
0020 
0021 #include <histogram.h>
0022 
0023 
0024 static Kst::ObjectStore _store;
0025 
0026 void TestHistogram::cleanupTestCase() {
0027   _store.clear();
0028 }
0029 
0030 void TestHistogram::testHistogram() {
0031   Kst::GeneratedVectorPtr gvp = Kst::kst_cast<Kst::GeneratedVector>(_store.createObject<Kst::GeneratedVector>());
0032   Q_ASSERT(gvp);
0033   QCOMPARE(gvp->length(), 1);
0034 
0035   gvp->changeRange(0, 10, 100);
0036   QCOMPARE(gvp->max(), 0.0);
0037   QCOMPARE(gvp->length(), 100);
0038 
0039   gvp->internalUpdate();
0040   QCOMPARE(gvp->max(), 10.0);
0041   QCOMPARE(gvp->length(), 100);
0042 
0043   Kst::VectorPtr vp(gvp);
0044   QCOMPARE(vp->max(), 10.0);
0045 
0046   Kst::HistogramPtr h1 = Kst::kst_cast<Kst::Histogram>(_store.createObject<Kst::Histogram>());
0047   h1->change(vp, 0, 10, 10, Kst::Histogram::Number);
0048 
0049   QVERIFY(!h1->realTimeAutoBin()); // should be false by default
0050   QCOMPARE(h1->numberOfBins(), 10);
0051 
0052   h1->writeLock();
0053   h1->internalUpdate();
0054   h1->unlock();
0055 
0056   QCOMPARE(h1->vMin(), 0.0);
0057   QCOMPARE(h1->vMax(), 10.0);
0058   QCOMPARE(h1->vNumSamples(), 100);
0059   int count = 0;
0060   for (int i=0; i<10; i++) {
0061     count += int(h1->vY()->value(i));
0062   }
0063   h1->setRealTimeAutoBin(true);
0064   QVERIFY(h1->realTimeAutoBin());
0065   //dumpPoints(h1, 10);
0066   QCOMPARE(count, 100); // should account for the whole vector
0067   h1->setNumberOfBins(11);
0068   QVERIFY(!h1->realTimeAutoBin());
0069   QCOMPARE(h1->numberOfBins(), 11);
0070   QCOMPARE(h1->vMin(), 0.0);
0071   QCOMPARE(h1->vMax(), 10.0);
0072   QCOMPARE(h1->vNumSamples(), 100);
0073 
0074   h1->writeLock();
0075   h1->internalUpdate();
0076   h1->unlock();
0077 
0078   count = 0;
0079   for (int i=0; i<11; i++) {
0080     count += int(h1->vY()->value(i));
0081   }
0082   //dumpPoints(h1, 11);
0083   QCOMPARE(count, 100); // should still account for the whole vector
0084   h1->setNumberOfBins(9);
0085   QCOMPARE(h1->numberOfBins(), 9);
0086   QCOMPARE(h1->vMin(), 0.0);
0087   QCOMPARE(h1->vMax(), 10.0);
0088   QCOMPARE(h1->vNumSamples(), 100);
0089 
0090   h1->writeLock();
0091   h1->internalUpdate();
0092   h1->unlock();
0093 
0094   count = 0;
0095   for (int i=0; i<9; i++) {
0096     count += int(h1->vY()->value(i));
0097   }
0098   //dumpPoints(h1, 9);
0099   QCOMPARE(count, 100); // should still account for the whole vector
0100   // min > max
0101 
0102   h1 = Kst::kst_cast<Kst::Histogram>(_store.createObject<Kst::Histogram>());
0103   h1->change(vp, 10, 0, 10, Kst::Histogram::Number);
0104 
0105 
0106   QCOMPARE(h1->numberOfBins(), 10);
0107   QCOMPARE(h1->xMin(), 0.0);
0108   QCOMPARE(h1->xMax(), 10.0);
0109   QCOMPARE(h1->vMin(), 0.0);
0110   QCOMPARE(h1->vMax(), 10.0);
0111   QCOMPARE(h1->vNumSamples(), 100);
0112 
0113   // min == max
0114   h1 = Kst::kst_cast<Kst::Histogram>(_store.createObject<Kst::Histogram>());
0115   h1->change(vp, 10, 10, 2, Kst::Histogram::Number);
0116 
0117   QCOMPARE(h1->numberOfBins(), 2);
0118   QCOMPARE(h1->xMin(), 9.0);
0119   QCOMPARE(h1->xMax(), 11.0);
0120   QCOMPARE(h1->vMin(), 0.0);
0121   QCOMPARE(h1->vMax(), 10.0);
0122   QCOMPARE(h1->vNumSamples(), 100);
0123 
0124   // max < min
0125   h1 = Kst::kst_cast<Kst::Histogram>(_store.createObject<Kst::Histogram>());
0126   h1->change(vp, 11, 9, 1, Kst::Histogram::Number);
0127 
0128   QCOMPARE(h1->numberOfBins(), 2);
0129   QCOMPARE(h1->xMax(), 11.0);
0130   QCOMPARE(h1->xMin(), 9.0);
0131   QCOMPARE(h1->vMin(), 0.0);
0132   QCOMPARE(h1->vMax(), 10.0);
0133   QCOMPARE(h1->vNumSamples(), 100);
0134   // set to max == min
0135   h1->setXRange(10, 10);
0136   QCOMPARE(h1->xMin(), 9.0);
0137   QCOMPARE(h1->xMax(), 11.0);
0138   // set to max > min
0139   h1->setXRange(1,2);
0140   QCOMPARE(h1->xMax() - h1->xMin(), 1.0);
0141   h1->setXRange(8, 10);
0142   QCOMPARE(h1->xMin(), 8.0);
0143   QCOMPARE(h1->xMax(), 10.0);
0144 }
0145 
0146 #ifdef KST_USE_QTEST_MAIN
0147 QTEST_MAIN(TestHistogram)
0148 #endif
0149 
0150 // vim: ts=2 sw=2 et