summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--sigcore/CMakeLists.txt2
-rw-r--r--sigcore/test/CMakeLists.txt5
-rw-r--r--sigcore/test/TestFraction.cpp187
-rw-r--r--sigcore/test/TestFraction.h50
-rw-r--r--test.cmake16
6 files changed, 261 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ba0ecd7a..718ba821 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,6 +12,7 @@ FIND_PACKAGE(QCA2)
# FIND_PACKAGE(LibKDEGames)
INCLUDE(doxygen.cmake)
+INCLUDE(test.cmake)
SET(SIGEN_VERSION_MAJOR 0)
SET(SIGEN_VERSION_MINOR 0)
diff --git a/sigcore/CMakeLists.txt b/sigcore/CMakeLists.txt
index bf570239..d0ef426d 100644
--- a/sigcore/CMakeLists.txt
+++ b/sigcore/CMakeLists.txt
@@ -32,6 +32,8 @@ TARGET_LINK_LIBRARIES(sigcore LINK_INTERFACE_LIBRARIES
${QT_QTCORE_LIBRARY}
)
+ADD_SUBDIRECTORY(test)
+
INSTALL(
TARGETS
sigcore
diff --git a/sigcore/test/CMakeLists.txt b/sigcore/test/CMakeLists.txt
new file mode 100644
index 00000000..cbe07287
--- /dev/null
+++ b/sigcore/test/CMakeLists.txt
@@ -0,0 +1,5 @@
+SET(libraries
+ sigcore
+)
+
+MAKE_TEST(Fraction libraries)
diff --git a/sigcore/test/TestFraction.cpp b/sigcore/test/TestFraction.cpp
new file mode 100644
index 00000000..e8818ba0
--- /dev/null
+++ b/sigcore/test/TestFraction.cpp
@@ -0,0 +1,187 @@
+/*
+ * 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 "TestFraction.h"
+
+// Qt includes
+#include <QtTest/QTest>
+
+void TestFraction::set()
+{
+ Sigcore::Fraction frac(1, 1);
+
+ QCOMPARE(frac.numerator(), 1);
+ QCOMPARE(frac.denominator(), 1);
+
+ frac.set(3, 4);
+
+ QCOMPARE(frac.numerator(), 3);
+ QCOMPARE(frac.denominator(), 4);
+}
+
+void TestFraction::setNumerator()
+{
+ Sigcore::Fraction frac(5, 8);
+
+ frac.setNumerator(4);
+
+ QCOMPARE(frac.numerator(), 4);
+}
+
+void TestFraction::setDenominator()
+{
+ Sigcore::Fraction frac(5, 8);
+
+ frac.setDenominator(4);
+
+ QCOMPARE(frac.denominator(), 4);
+}
+
+void TestFraction::reduce()
+{
+ Sigcore::Fraction frac(5, 8);
+
+ frac.reduce();
+
+ QCOMPARE(frac.numerator(), 5);
+ QCOMPARE(frac.denominator(), 8);
+
+ frac.set(2, 4);
+ frac.reduce();
+
+ QCOMPARE(frac.numerator(), 1);
+ QCOMPARE(frac.denominator(), 2);
+
+ frac.set(-2, 4);
+ frac.reduce();
+
+ QCOMPARE(frac.numerator(), -1);
+ QCOMPARE(frac.denominator(), 2);
+}
+
+void TestFraction::assignment()
+{
+ Sigcore::Fraction frac1(5, 8);
+ Sigcore::Fraction frac2(3, 4);
+
+ frac1 = frac2;
+
+ QCOMPARE(frac1.numerator(), 3);
+ QCOMPARE(frac1.denominator(), 4);
+}
+
+void TestFraction::conversion()
+{
+ Sigcore::Fraction frac(5, 8);
+
+ QVERIFY(frac == .625);
+}
+
+void TestFraction::addition()
+{
+ Sigcore::Fraction frac1(5, 8);
+ Sigcore::Fraction frac2(3, 4);
+
+ frac1 = frac1 + frac2;
+
+ QCOMPARE(frac1.numerator(), 11);
+ QCOMPARE(frac1.denominator(), 8);
+}
+
+void TestFraction::subtraction()
+{
+ Sigcore::Fraction frac1(5, 8);
+ Sigcore::Fraction frac2(3, 4);
+
+ frac1 = frac1 - frac2;
+
+ QCOMPARE(frac1.numerator(), -1);
+ QCOMPARE(frac1.denominator(), 8);
+}
+
+void TestFraction::multiplication()
+{
+ Sigcore::Fraction frac1(5, 8);
+ Sigcore::Fraction frac2(3, 4);
+
+ frac1 = frac1 * frac2;
+
+ QCOMPARE(frac1.numerator(), 15);
+ QCOMPARE(frac1.denominator(), 32);
+}
+
+void TestFraction::division()
+{
+ Sigcore::Fraction frac1(5, 8);
+ Sigcore::Fraction frac2(3, 4);
+
+ frac1 = frac1 / frac2;
+
+ QCOMPARE(frac1.numerator(), 5);
+ QCOMPARE(frac1.denominator(), 6);
+}
+
+void TestFraction::modulo()
+{
+ Sigcore::Fraction frac1(7, 8);
+ Sigcore::Fraction frac2(1, 2);
+
+ frac1 = frac1 % frac2;
+
+ QCOMPARE(frac1.numerator(), 3);
+ QCOMPARE(frac1.denominator(), 8);
+}
+
+void TestFraction::equal()
+{
+ Sigcore::Fraction frac1(6, 8);
+ Sigcore::Fraction frac2(3, 4);
+
+ QCOMPARE(frac1, frac2);
+}
+
+void TestFraction::less()
+{
+ Sigcore::Fraction frac1(5, 8);
+ Sigcore::Fraction frac2(3, 4);
+
+ QVERIFY(frac1 < frac2);
+}
+
+void TestFraction::greater()
+{
+ Sigcore::Fraction frac1(7, 8);
+ Sigcore::Fraction frac2(3, 4);
+
+ QVERIFY(frac1 > frac2);
+}
+
+void TestFraction::normalize()
+{
+ Sigcore::Fraction frac(-2, -1);
+
+ QCOMPARE(frac.numerator(), 2);
+ QCOMPARE(frac.denominator(), 1);
+
+ frac.set(2, -1);
+
+ QCOMPARE(frac.numerator(), -2);
+ QCOMPARE(frac.denominator(), 1);
+}
+
+QTEST_APPLESS_MAIN(TestFraction)
diff --git a/sigcore/test/TestFraction.h b/sigcore/test/TestFraction.h
new file mode 100644
index 00000000..2eb720a1
--- /dev/null
+++ b/sigcore/test/TestFraction.h
@@ -0,0 +1,50 @@
+/*
+ * 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 TESTFRACTION
+#define TESTFRACTION
+
+// Sigcore includes
+#include "../Fraction.h"
+
+class TestFraction : public QObject
+{
+ Q_OBJECT
+
+ private slots:
+ void set();
+ void setNumerator();
+ void setDenominator();
+
+ void reduce();
+
+ void assignment();
+ void conversion();
+ void addition();
+ void subtraction();
+ void multiplication();
+ void division();
+ void modulo();
+
+ void equal();
+ void less();
+ void greater();
+
+ void normalize();
+};
+
+#endif
diff --git a/test.cmake b/test.cmake
new file mode 100644
index 00000000..5cbf5563
--- /dev/null
+++ b/test.cmake
@@ -0,0 +1,16 @@
+SET(TESTING OFF CACHE BOOL "Build tests")
+
+IF (TESTING)
+ ENABLE_TESTING()
+ENDIF (TESTING)
+
+MACRO(MAKE_TEST class libraries)
+ KDE4_ADD_UNIT_TEST(test-${class}
+ Test${class}.cpp
+ )
+ TARGET_LINK_LIBRARIES(test-${class}
+ ${QT_QTCORE_LIBRARY}
+ ${QT_QTTEST_LIBRARY}
+ ${${libraries}}
+ )
+ENDMACRO(MAKE_TEST)