summaryrefslogtreecommitdiffstats
path: root/sigcore/test/TestHat.cpp
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2008-12-16 21:23:04 -0500
committerBen Boeckel <MathStuf@gmail.com>2008-12-16 21:23:04 -0500
commitfb4d19e42856f5d8a8fb95d6cbc3b2613697cf46 (patch)
treed75ebfb730bcbd87cb6f6fce0798b576f5c6856f /sigcore/test/TestHat.cpp
parent4adfcbc9024706de602eef9ad87f82d7beccd5a5 (diff)
downloadsigen-fb4d19e42856f5d8a8fb95d6cbc3b2613697cf46.tar.gz
sigen-fb4d19e42856f5d8a8fb95d6cbc3b2613697cf46.tar.xz
sigen-fb4d19e42856f5d8a8fb95d6cbc3b2613697cf46.zip
Fixes in Hat and test code for it
Diffstat (limited to 'sigcore/test/TestHat.cpp')
-rw-r--r--sigcore/test/TestHat.cpp144
1 files changed, 144 insertions, 0 deletions
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)