diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | ce599e4f9f94b4eb00c1b5edb85bce5431ab3df2 (patch) | |
tree | d3bb9f5d25a2dc09ca81adecf39621d871534297 /kbruch/testcases | |
download | tdeedu-ce599e4f9f94b4eb00c1b5edb85bce5431ab3df2.tar.gz tdeedu-ce599e4f9f94b4eb00c1b5edb85bce5431ab3df2.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdeedu@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kbruch/testcases')
-rw-r--r-- | kbruch/testcases/Makefile.am | 13 | ||||
-rw-r--r-- | kbruch/testcases/README | 76 | ||||
-rw-r--r-- | kbruch/testcases/kbruch_test.cpp | 37 | ||||
-rw-r--r-- | kbruch/testcases/primenumber_test.cpp | 115 | ||||
-rw-r--r-- | kbruch/testcases/ratio_test.cpp | 367 |
5 files changed, 608 insertions, 0 deletions
diff --git a/kbruch/testcases/Makefile.am b/kbruch/testcases/Makefile.am new file mode 100644 index 00000000..5e9e8950 --- /dev/null +++ b/kbruch/testcases/Makefile.am @@ -0,0 +1,13 @@ +check_PROGRAMS = kbruch_test + +kbruch_test_SOURCES = task.cpp ratio.cpp ratio_test.cpp primenumber.cpp primenumber_test.cpp kbruch_test.cpp + +kbruch_test_LDADD = -lboost_unit_test_framework-gcc $(LIB_KDEUI) + +# the library search path. +kbruch_test_LDFLAGS = $(all_libraries) $(KDE_RPATH) + +EXTRA_DIST = primenumber.cpp primenumber.h ratio.cpp ratio.h task.cpp task.h primenumber_test.cpp ratio_test.cpp kbruch_test.cpp + +# set the include path for X, qt and KDE +INCLUDES= $(all_includes) diff --git a/kbruch/testcases/README b/kbruch/testcases/README new file mode 100644 index 00000000..cece8143 --- /dev/null +++ b/kbruch/testcases/README @@ -0,0 +1,76 @@ +In this directory I will store the testcases for KBruch. A file containing a +test suite for a "class" has the following file name: + +"class"_test.cpp + +In this file there is a class (struct) named "class"_test_suite and the class +containing the test cases. This class is called "class"_test. Every member +function of the original class is tested in a seperated member function of the +"class"_test class. Those functions are named + +"class"_test::test_"memberFunction"() {...} + +Before you can compile it, you have to make symlinks from the source directory +to this directory. The simplest way is to use the following bash script: + +-----SNIP----- + +#!/bin/bash + +# save the current directory +# +current=`pwd` + +# delete all symbolic links +# +rm `find . -type l` + +# now go the source directory +# +cd ../src + +echo "Createing symlinks to the source files..." + +# make a symlink from every *.cpp file to this directory +# +for a in `ls *.cpp` +do + ln -s ../src/$a ../testcases/$a +done + +# make a symlink from every *.h file to this directory +# +for a in `ls *.h` +do + ln -s ../src/$a ../testcases/$a +done + +# and back to the testcases directory +# +cd $current + +-----SNIP----- + +And you need the boost library installed. I think this will be the hardest +thing. You can get boost from http://www.boost.org/ + +If you want to compile the whole tests not under Linux using gcc, you maybe +have to edit the Makefile.am in this directory to correctly link against +libboost_unit_test_framework-YOURCOMPILER. Edit the following line in +Makefile.am: + +kbruch_test_LDADD = -lboost_unit_test_framework-gcc $(LIB_KDEUI) + +I hope someone finds a portable way to do this Makefile stuff. Please fix it, +I'm not a Automake/Autoconf guru! + +You can compile the test program by typing: + +make check + +If you were able to compile it, you can execute the test case program +kbruch_test from within this directory. + +I hope this helps a little bit. + +Sebastian Stein <seb.kde@hpfsc.de> diff --git a/kbruch/testcases/kbruch_test.cpp b/kbruch/testcases/kbruch_test.cpp new file mode 100644 index 00000000..b4e978d1 --- /dev/null +++ b/kbruch/testcases/kbruch_test.cpp @@ -0,0 +1,37 @@ +/*************************************************************************** + + begin : 2004-05-25 + copyright : (C) 2004 by Sebastian Stein + email : seb.kde@hpfsc.de + + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +// for BOOST testing +#include <boost/test/unit_test.hpp> +using boost::unit_test_framework::test_suite; + +// the test classes +#include "primenumber_test.cpp" +#include "ratio_test.cpp" + +// test program entry point +test_suite* init_unit_test_suite(int /* argc */, char** /* argv */) +{ + // create the top test suite + std::auto_ptr<test_suite> top_test_suite(BOOST_TEST_SUITE("Master test suite")); + + // add test suites to the top test suite + top_test_suite->add(new primenumber_test_suite()); + top_test_suite->add(new ratio_test_suite()); + + return top_test_suite.release(); +} diff --git a/kbruch/testcases/primenumber_test.cpp b/kbruch/testcases/primenumber_test.cpp new file mode 100644 index 00000000..c0e322fb --- /dev/null +++ b/kbruch/testcases/primenumber_test.cpp @@ -0,0 +1,115 @@ +/*************************************************************************** + + begin : 2004-05-25 + copyright : (C) 2004 by Sebastian Stein + email : seb.kde@hpfsc.de + + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +// for BOOST testing +#include <boost/test/unit_test.hpp> +using boost::unit_test_framework::test_suite; +using boost::unit_test_framework::test_case; + +// the class to be tested +#include "primenumber.h" + + +class primenumber_test +{ + public: + + // constructor + primenumber_test() + { + } + + /** test the prime number algorithm */ + void test_isPrimeNumber() + { + BOOST_REQUIRE(m_primenumber.isPrimeNumber(0) == 0); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(2) == 1); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(3) == 1); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(4) == 0); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(5) == 1); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(6) == 0); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(7) == 1); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(8) == 0); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(9) == 0); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(23) == 1); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(9) == 0); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(9) == 0); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(6) == 0); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(101) == 1); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(323) == 0); // 17 * 19 + BOOST_REQUIRE(m_primenumber.isPrimeNumber(1001) == 0); // 7 * 143 + BOOST_REQUIRE(m_primenumber.isPrimeNumber(1002) == 0); // 2 * 501 + BOOST_REQUIRE(m_primenumber.isPrimeNumber(3) == 1); + BOOST_REQUIRE(m_primenumber.isPrimeNumber(2) == 1); + } + + /** test the get_first() function */ + void test_get_first() + { + BOOST_REQUIRE(m_primenumber.get_first() == 2); + } + + /** test the move and get functions */ + void test_move_get_func() + { + m_primenumber.move_first(); + BOOST_REQUIRE(m_primenumber.get_current() == 2); + BOOST_REQUIRE(m_primenumber.get_next() == 3); + + m_primenumber.move_forward(); + BOOST_REQUIRE(m_primenumber.get_current() == 5); + + m_primenumber.move_back(); + BOOST_REQUIRE(m_primenumber.get_current() == 3); + + unsigned int tmp = m_primenumber.get_last(); + m_primenumber.move_last(); + BOOST_REQUIRE(m_primenumber.get_current() == tmp); + + m_primenumber.move_forward(); + BOOST_REQUIRE(m_primenumber.get_last() != tmp); + } + + private: + + // instance of primenumber + primenumber m_primenumber; +}; + +class primenumber_test_suite : public test_suite +{ + public: + + primenumber_test_suite() : test_suite("primenumber_test_suite") + { + // create an instance of the test cases class + boost::shared_ptr<primenumber_test> instance(new primenumber_test()); + + // create the test cases + test_case* isPrimeNumber_test_case = BOOST_CLASS_TEST_CASE( + &primenumber_test::test_isPrimeNumber, instance ); + test_case* get_first_test_case = BOOST_CLASS_TEST_CASE( + &primenumber_test::test_get_first, instance ); + test_case* move_get_func_test_case = BOOST_CLASS_TEST_CASE( + &primenumber_test::test_move_get_func, instance ); + + // add the test cases to the test suite + add(isPrimeNumber_test_case); + add(get_first_test_case); + add(move_get_func_test_case); + } +}; diff --git a/kbruch/testcases/ratio_test.cpp b/kbruch/testcases/ratio_test.cpp new file mode 100644 index 00000000..bdc4419b --- /dev/null +++ b/kbruch/testcases/ratio_test.cpp @@ -0,0 +1,367 @@ +/*************************************************************************** + + begin : 2004-06-03 + copyright : (C) 2004 by Sebastian Stein + email : seb.kde@hpfsc.de + + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +// for BOOST testing +#include <boost/test/unit_test.hpp> +using boost::unit_test_framework::test_suite; +using boost::unit_test_framework::test_case; + +// the class to be tested +#include "ratio.h" + + +class ratio_test +{ + public: + + // constructor + ratio_test() + { + } + + /** test the constructor of class ratio */ + void test_constructor() + { + // check basic initialisation + ratio * ratio_a = new ratio(); + BOOST_REQUIRE(ratio_a->numerator() == 0); + BOOST_REQUIRE(ratio_a->denominator() == 1); + + // a denominator with value 0 is never allowed + ratio * ratio_b = new ratio(2, 0); + BOOST_REQUIRE(ratio_b->numerator() == 2); + BOOST_REQUIRE(ratio_b->denominator() == 1); // division by zero not allowed + + // a new ratio should always be reduced + ratio * ratio_c = new ratio(2, 4); + BOOST_REQUIRE(ratio_c->numerator() == 1); + BOOST_REQUIRE(ratio_c->denominator() == 2); + + // check copy constructor + ratio * ratio_d = new ratio(*ratio_c); + BOOST_REQUIRE(ratio_d->numerator() == 1); + BOOST_REQUIRE(ratio_d->denominator() == 2); + + // the copy constructor is not allowed to reduce the new ratio + ratio_d->setNumerator(4, false); + ratio * ratio_e = new ratio(*ratio_d); + BOOST_REQUIRE(ratio_e->numerator() == 4); + BOOST_REQUIRE(ratio_e->denominator() == 2); + + delete(ratio_a); + delete(ratio_b); + delete(ratio_c); + delete(ratio_d); + delete(ratio_e); + + return; + } + + /** test set and get functions */ + void test_setAndGet() + { + // check setNumerator() and numerator() (get) + ratio * ratio_a = new ratio(); + ratio_a->setNumerator(10); + BOOST_REQUIRE(ratio_a->numerator() == 10); + BOOST_REQUIRE(ratio_a->denominator() == 1); + + // check setDenominator() and denominator() (get) + ratio_a->setDenominator(7); + BOOST_REQUIRE(ratio_a->numerator() == 10); + BOOST_REQUIRE(ratio_a->denominator() == 7); + + // now check if the ratio gets reduced + ratio_a->setNumerator(14); + BOOST_REQUIRE(ratio_a->numerator() == 2); + BOOST_REQUIRE(ratio_a->denominator() == 1); + + // lets do the same, but with out reducing + ratio_a->setNumerator(14, false); + ratio_a->setDenominator(7, false); + BOOST_REQUIRE(ratio_a->numerator() == 14); + BOOST_REQUIRE(ratio_a->denominator() == 7); + + // now the = operator + ratio * ratio_b = new ratio(); + *ratio_b = *ratio_a; + BOOST_REQUIRE(ratio_b->numerator() == 14); + BOOST_REQUIRE(ratio_b->denominator() == 7); + + // make sure we didn't just copied the pointers + BOOST_REQUIRE(ratio_a != ratio_b); + + delete(ratio_a); + delete(ratio_b); + + return; + } + + /** test the = operator */ + void test_operatorSet() + { + ratio ratio_a; + ratio_a = 8; + BOOST_REQUIRE(ratio_a.numerator() == 8); + BOOST_REQUIRE(ratio_a.denominator() == 1); + + ratio ratio_b(2, 3); + ratio_a = ratio_b; + BOOST_REQUIRE(ratio_a.numerator() == 2); + BOOST_REQUIRE(ratio_a.denominator() == 3); + + return; + } + + /** test the + and - operator */ + void test_operatorAddSub() + { + ratio * ratio_a = new ratio(); + ratio_a->setNumerator(4, false); + ratio_a->setDenominator(2, false); + + ratio * ratio_b = new ratio(); + ratio_b->setNumerator(4, false); + ratio_b->setDenominator(8, false); + + // check + operator + ratio ratio_c = *ratio_a + *ratio_b; + BOOST_REQUIRE(ratio_c.numerator() == 5); + BOOST_REQUIRE(ratio_c.denominator() == 2); + + // it should be the same if we change the addends + ratio_c = *ratio_b + *ratio_a; + BOOST_REQUIRE(ratio_c.numerator() == 5); + BOOST_REQUIRE(ratio_c.denominator() == 2); + + // check - operator + ratio_c = *ratio_b - *ratio_a; + BOOST_REQUIRE(ratio_c.numerator() == -3); + BOOST_REQUIRE(ratio_c.denominator() == 2); + + // it should not be the same if we change the subtrahends + ratio_c = *ratio_a - *ratio_b; + BOOST_REQUIRE(ratio_c.numerator() == 3); + BOOST_REQUIRE(ratio_c.denominator() == 2); + + // now check if we can get 0/1 + *ratio_a = *ratio_b; + ratio_c = *ratio_a - *ratio_b; + BOOST_REQUIRE(ratio_c.numerator() == 0); + BOOST_REQUIRE(ratio_c.denominator() == 1); + + delete(ratio_a); + delete(ratio_b); + + return; + } + + /** test the * and / operator */ + void test_operatorMulDiv() + { + ratio * ratio_a = new ratio(); + ratio_a->setNumerator(4, false); + ratio_a->setDenominator(2, false); + + ratio * ratio_b = new ratio(); + ratio_b->setNumerator(4, false); + ratio_b->setDenominator(16, false); + + // check * operator + ratio ratio_c = *ratio_a * *ratio_b; + BOOST_REQUIRE(ratio_c.numerator() == 1); + BOOST_REQUIRE(ratio_c.denominator() == 2); + + // it should be the same if we change the addends + ratio_c = *ratio_b * *ratio_a; + BOOST_REQUIRE(ratio_c.numerator() == 1); + BOOST_REQUIRE(ratio_c.denominator() == 2); + + // check / operator + ratio_c = *ratio_b / *ratio_a; + BOOST_REQUIRE(ratio_c.numerator() == 1); + BOOST_REQUIRE(ratio_c.denominator() == 8); + + // it should not be the same if we change the subtrahends + ratio_c = *ratio_a / *ratio_b; + BOOST_REQUIRE(ratio_c.numerator() == 8); + BOOST_REQUIRE(ratio_c.denominator() == 1); + + // now check if we can get 0/1 + *ratio_a = 0; + ratio_c = *ratio_a * *ratio_b; + BOOST_REQUIRE(ratio_c.numerator() == 0); + BOOST_REQUIRE(ratio_c.denominator() == 1); + ratio_c = *ratio_a / *ratio_b; + BOOST_REQUIRE(ratio_c.numerator() == 0); + BOOST_REQUIRE(ratio_c.denominator() == 1); + + delete(ratio_a); + delete(ratio_b); + + return; + } + + /** test function reziproc() */ + void test_reziproc() + { + ratio ratio_a(2, 3); + ratio_a.reziproc(); + BOOST_REQUIRE(ratio_a.numerator() == 3); + BOOST_REQUIRE(ratio_a.denominator() == 2); + + return; + } + + /** test function reduce() */ + void test_reduce() + { + ratio ratio_a; + ratio_a.setNumerator(51, false); + ratio_a.setDenominator(17, false); + ratio_a.reduce(); + BOOST_REQUIRE(ratio_a.numerator() == 3); + BOOST_REQUIRE(ratio_a.denominator() == 1); + + return; + } + + /** test operator compare */ + void test_operatorCompare() + { + ratio ratio_a(2, 3); + ratio ratio_b(2, 3); + ratio ratio_c(-2, -3); + ratio ratio_d(2, -3); + ratio ratio_e(-2, 3); + + BOOST_REQUIRE((ratio_a == ratio_a) == true); + BOOST_REQUIRE((ratio_a == ratio_b) == true); + BOOST_REQUIRE((ratio_a == ratio_c) == true); + BOOST_REQUIRE((ratio_a == ratio_d) == false); + BOOST_REQUIRE((ratio_a == ratio_e) == false); + + return; + } + + /** test operator smaller */ + void test_operatorSmaller() + { + ratio ratio_a(2, 3); + ratio ratio_b(2, 3); + ratio ratio_c(-2, -3); + ratio ratio_d(2, -3); + ratio ratio_e(-2, 3); + + BOOST_REQUIRE((ratio_a < ratio_a) == false); + BOOST_REQUIRE((ratio_a < ratio_b) == false); + BOOST_REQUIRE((ratio_a < ratio_c) == false); + BOOST_REQUIRE((ratio_a < ratio_d) == false); + BOOST_REQUIRE((ratio_a < ratio_e) == false); + + ratio_a = ratio(-2, 3); + + BOOST_REQUIRE((ratio_a < ratio_a) == false); + BOOST_REQUIRE((ratio_a < ratio_b) == true); + BOOST_REQUIRE((ratio_a < ratio_c) == true); + BOOST_REQUIRE((ratio_a < ratio_d) == false); + BOOST_REQUIRE((ratio_a < ratio_e) == false); + + BOOST_REQUIRE((ratio(5, 8) < ratio(2, 1)) == true); + + return; + } + + /** test operator bigger */ + void test_operatorBigger() + { + ratio ratio_a(2, 3); + ratio ratio_b(2, 3); + ratio ratio_c(-2, -3); + ratio ratio_d(2, -3); + ratio ratio_e(-2, 3); + ratio ratio_f(-4, 3); + + BOOST_REQUIRE((ratio_a > ratio_a) == false); + BOOST_REQUIRE((ratio_a > ratio_b) == false); + BOOST_REQUIRE((ratio_a > ratio_c) == false); + BOOST_REQUIRE((ratio_a > ratio_d) == true); + BOOST_REQUIRE((ratio_a > ratio_e) == true); + BOOST_REQUIRE((ratio_a > ratio_f) == true); + + ratio_a = ratio(-2, 3); + + BOOST_REQUIRE((ratio_a > ratio_a) == false); + BOOST_REQUIRE((ratio_a > ratio_b) == false); + BOOST_REQUIRE((ratio_a > ratio_c) == false); + BOOST_REQUIRE((ratio_a > ratio_d) == false); + BOOST_REQUIRE((ratio_a > ratio_e) == false); + BOOST_REQUIRE((ratio_a > ratio_f) == true); + + return; + } + + private: + + // instance of primenumber + ratio m_ratio; +}; + +class ratio_test_suite : public test_suite +{ + public: + + ratio_test_suite() : test_suite("ratio_test_suite") + { + // create an instance of the test cases class + boost::shared_ptr<ratio_test> instance(new ratio_test()); + + // create the test cases + test_case* constructor_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_constructor, instance ); + test_case* setAndGet_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_setAndGet, instance ); + test_case* operatorSet_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_operatorSet, instance ); + test_case* operatorAddSub_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_operatorAddSub, instance ); + test_case* operatorMulDiv_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_operatorMulDiv, instance ); + test_case* reziproc_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_reziproc, instance ); + test_case* reduce_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_reduce, instance ); + test_case* operatorCompare_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_operatorCompare, instance ); + test_case* operatorSmaller_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_operatorSmaller, instance ); + test_case* operatorBigger_test_case = BOOST_CLASS_TEST_CASE( + &ratio_test::test_operatorBigger, instance ); + + // add the test cases to the test suite + add(constructor_test_case); + add(setAndGet_test_case); + add(operatorSet_test_case); + add(operatorAddSub_test_case); + add(operatorMulDiv_test_case); + add(reziproc_test_case); + add(reduce_test_case); + add(operatorCompare_test_case); + add(operatorSmaller_test_case); + add(operatorBigger_test_case); + } +}; |