diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2008-12-16 21:23:04 -0500 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2008-12-16 21:23:04 -0500 |
| commit | fb4d19e42856f5d8a8fb95d6cbc3b2613697cf46 (patch) | |
| tree | d75ebfb730bcbd87cb6f6fce0798b576f5c6856f | |
| parent | 4adfcbc9024706de602eef9ad87f82d7beccd5a5 (diff) | |
| download | sigen-fb4d19e42856f5d8a8fb95d6cbc3b2613697cf46.tar.gz sigen-fb4d19e42856f5d8a8fb95d6cbc3b2613697cf46.tar.xz sigen-fb4d19e42856f5d8a8fb95d6cbc3b2613697cf46.zip | |
Fixes in Hat and test code for it
| -rw-r--r-- | sigcore/Hat.h | 18 | ||||
| -rw-r--r-- | sigcore/test/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | sigcore/test/TestHat.cpp | 144 | ||||
| -rw-r--r-- | sigcore/test/TestHat.h | 38 |
4 files changed, 191 insertions, 12 deletions
diff --git a/sigcore/Hat.h b/sigcore/Hat.h index 7c6cff89..c4e2c7c2 100644 --- a/sigcore/Hat.h +++ b/sigcore/Hat.h @@ -140,22 +140,16 @@ template<class T> inline T Hat<T>::takeAndClear() template<class T> inline void Hat<T>::setCount(const T& key, const int weight) { - if (m_items.contains(key)) - { - m_count -= m_items[key]; - if (!weight) - m_items.remove(key); - } - add(key, weight); + add(key, weight - m_items[key]); } template<class T> inline void Hat<T>::add(const T& key, const int weight) { - if (weight) - { - m_items[key] += weight; - m_count += weight; - } + int nWeight = qMax(weight, -m_items[key]); + m_items[key] += nWeight; + m_count += nWeight; + if (!m_items[key]) + m_items.remove(key); } template<class T> inline int Hat<T>::distinctCount() const diff --git a/sigcore/test/CMakeLists.txt b/sigcore/test/CMakeLists.txt index cbe07287..53b13d3a 100644 --- a/sigcore/test/CMakeLists.txt +++ b/sigcore/test/CMakeLists.txt @@ -3,3 +3,6 @@ SET(libraries ) MAKE_TEST(Fraction libraries) +MAKE_TEST(Hat libraries) +# MAKE_TEST(Matrix libraries) +# MAKE_TEST(Script libraries) diff --git a/sigcore/test/TestHat.cpp b/sigcore/test/TestHat.cpp new file mode 100644 index 00000000..215744a7 --- /dev/null +++ b/sigcore/test/TestHat.cpp @@ -0,0 +1,144 @@ +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +// Header include +#include "TestHat.h" + +// Qt includes +#include <QtTest/QTest> + +void TestHat::initTestCase() +{ + qsrand(QDateTime::currentDateTime().toTime_t()); +} + +void TestHat::pick() +{ + Sigcore::Hat<int> hat; + + hat.add(0, 5); + hat.add(1, 4); + hat.add(2, 3); + hat.add(3, 2); + hat.add(4, 1); + + hat.pick(); + + QCOMPARE(hat.count(), 15); + QCOMPARE(hat.distinctCount(), 5); +} + +void TestHat::take() +{ + Sigcore::Hat<int> hat; + + hat.add(0, 5); + hat.add(1, 4); + hat.add(2, 3); + hat.add(3, 2); + hat.add(4, 1); + + int picked = hat.take(); + + QCOMPARE(hat.count(), 14); + QCOMPARE(hat.count(picked), 4 - picked); +} + +void TestHat::takeAndClear() +{ + Sigcore::Hat<int> hat; + + hat.add(0, 5); + hat.add(1, 4); + hat.add(2, 3); + hat.add(3, 2); + hat.add(4, 1); + + int picked = hat.takeAndClear(); + + QCOMPARE(hat.count(), 15 - 5 + picked); + QCOMPARE(hat.count(picked), 0); +} + +void TestHat::setCount() +{ + Sigcore::Hat<int> hat; + + hat.add(0, 5); + hat.add(1, 4); + hat.add(2, 3); + hat.add(3, 2); + hat.add(4, 1); + + hat.setCount(4, 3); + + QCOMPARE(hat.count(), 17); + QCOMPARE(hat.distinctCount(), 5); + + hat.setCount(4, 0); + + QCOMPARE(hat.count(), 14); + QCOMPARE(hat.distinctCount(), 4); + + hat.setCount(4, 1); + + QCOMPARE(hat.count(), 15); + QCOMPARE(hat.distinctCount(), 5); +} + +void TestHat::add() +{ + Sigcore::Hat<int> hat; + + hat.add(0, 5); + hat.add(1, 4); + hat.add(2, 3); + hat.add(3, 2); + hat.add(4, 1); + + hat.add(4, 3); + + QCOMPARE(hat.count(), 18); + QCOMPARE(hat.distinctCount(), 5); + + hat.add(4, -3); + + QCOMPARE(hat.count(), 15); + QCOMPARE(hat.distinctCount(), 5); + + hat.add(4, -2); + + QCOMPARE(hat.count(), 14); + QCOMPARE(hat.distinctCount(), 4); +} + +void TestHat::chance() +{ + Sigcore::Hat<int> hat; + + hat.add(0, 5); + hat.add(1, 4); + hat.add(2, 3); + hat.add(3, 2); + hat.add(4, 1); + + int picked = hat.pick(); + + QCOMPARE(double(hat.chance(picked)), (5 - picked) / 15.); +} + +QTEST_APPLESS_MAIN(TestHat) diff --git a/sigcore/test/TestHat.h b/sigcore/test/TestHat.h new file mode 100644 index 00000000..b54dc759 --- /dev/null +++ b/sigcore/test/TestHat.h @@ -0,0 +1,38 @@ +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TESTHAT +#define TESTHAT + +// Sigcore includes +#include "../Hat.h" + +class TestHat : public QObject +{ + Q_OBJECT + + private slots: + void initTestCase(); + void pick(); + void take(); + void takeAndClear(); + void setCount(); + void add(); + void chance(); +}; + +#endif |
