From fb4d19e42856f5d8a8fb95d6cbc3b2613697cf46 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Tue, 16 Dec 2008 21:23:04 -0500 Subject: Fixes in Hat and test code for it --- sigcore/test/TestHat.cpp | 144 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 sigcore/test/TestHat.cpp (limited to 'sigcore/test/TestHat.cpp') 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 + * + * 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 . + */ + +// Header include +#include "TestHat.h" + +// Qt includes +#include + +void TestHat::initTestCase() +{ + qsrand(QDateTime::currentDateTime().toTime_t()); +} + +void TestHat::pick() +{ + Sigcore::Hat 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 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 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 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 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 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) -- cgit