--- /dev/null
+/** @file
+ * @brief unit test for time related functions.
+ *
+ * @copyright Copyright © 2001-2008 by Intra2net AG
+ * @license commercial
+ * @contact info@intra2net.com
+ *
+ */
+
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <timefunc.hxx>
+#include <filefunc.hxx>
+
+using namespace std;
+using namespace I2n;
+using namespace CppUnit;
+
+namespace
+{
+
+
+} // eo namespace <anonymous>
+
+
+class TestTimeFunc : public TestFixture
+{
+ CPPUNIT_TEST_SUITE(TestTimeFunc);
+
+ CPPUNIT_TEST(AddIntervalsDisjoint);
+ CPPUNIT_TEST(AddIntervalsInclude);
+ CPPUNIT_TEST(AddIntervalsEmbrace);
+ CPPUNIT_TEST(AddIntervalsJoin1);
+ CPPUNIT_TEST(AddIntervalsJoin1b);
+ CPPUNIT_TEST(AddIntervalsJoin2);
+
+ CPPUNIT_TEST(SubIntervalsDisjoint);
+ CPPUNIT_TEST(SubIntervalsExact);
+ CPPUNIT_TEST(SubIntervalsSplit1);
+ CPPUNIT_TEST(SubIntervalsCutFront);
+ CPPUNIT_TEST(SubIntervalsCutBack);
+ CPPUNIT_TEST(SubIntervalsCutMore);
+
+ CPPUNIT_TEST(IntervalComparisons);
+
+ CPPUNIT_TEST_SUITE_END();
+
+protected:
+
+ typedef std::list< std::string > StringList;
+
+ std::set<std::string> used_check_files;
+
+ std::string get_check_file_path(std::string tag)
+ {
+ std::string result;
+ result= "__unittest__" + tag + ".dat";
+ used_check_files.insert(result);
+ return result;
+ } // eo get_check_file_path
+
+
+ void remove_check_files()
+ {
+ for (std::set<std::string>::iterator it= used_check_files.begin();
+ it != used_check_files.end();
+ ++it)
+ {
+ std::string filepath(*it);
+ if (path_exists(filepath))
+ {
+ unlink(filepath);
+ }
+ //TODO
+ }
+ used_check_files.clear();
+ } // eo remove_check_files
+
+
+
+public:
+
+ void setUp()
+ {
+ } // eo setUp
+
+
+ void tearDown()
+ {
+ remove_check_files();
+ } // eo tearDown
+
+
+ /*
+ * the tests:
+ */
+
+
+ void AddIntervalsDisjoint()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval( 10, 100 ) );
+ intervals.add( Interval( 600, 620 ) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 2u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
+
+ CPPUNIT_ASSERT_EQUAL( 600u, intervals.back().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 620u, intervals.back().upper_bound() );
+ } // eo AddIntervalsDisjoint()
+
+
+
+ void AddIntervalsInclude()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval( 10, 100 ) );
+ intervals.add( Interval( 10, 80 ) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
+ } // eo AddIntervalsInclude()
+
+
+
+ void AddIntervalsEmbrace()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval( 10, 100 ) );
+ intervals.add( Interval( 5, 120 ) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 5u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 120u, intervals.front().upper_bound() );
+ } // eo AddIntervalsEmbrace()
+
+
+
+ void AddIntervalsJoin1()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval( 10, 100 ) );
+ intervals.add( Interval( 60, 120 ) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 120u, intervals.front().upper_bound() );
+ } // eo AddIntervalsJoin1()
+
+
+
+ void AddIntervalsJoin1b()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval( 10, 100 ) );
+ intervals.add( Interval( 100, 120 ) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 120u, intervals.front().upper_bound() );
+ } // eo AddIntervalsJoin1b()
+
+
+
+ void AddIntervalsJoin2()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval( 10, 100 ) );
+ intervals.add( Interval( 200, 250 ) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 2u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
+ CPPUNIT_ASSERT_EQUAL( 200u, intervals.back().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 250u, intervals.back().upper_bound() );
+
+ // now add the gap; the intervals should collapse to one covering all:
+ intervals.add( Interval(100, 200) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 250u, intervals.front().upper_bound() );
+ } // eo AddIntervalsJoin2()
+
+
+
+ void SubIntervalsDisjoint()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval(10, 100) );
+ intervals.sub( Interval(0, 10) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
+ } // eo SubIntervalsDisjoint()
+
+
+
+ void SubIntervalsExact()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval(10, 100) );
+ intervals.sub( Interval(10, 100) );
+
+ CPPUNIT_ASSERT_EQUAL( true, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 0u, intervals.size() );
+ } // eo SubIntervalsExact()
+
+
+
+ void SubIntervalsSplit1()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval(10, 100) );
+ intervals.sub( Interval(20, 40) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 2u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 20u, intervals.front().upper_bound() );
+
+ CPPUNIT_ASSERT_EQUAL( 40u, intervals.back().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 100u, intervals.back().upper_bound() );
+ } // eo SubIntervalsSplit1()
+
+
+ void SubIntervalsCutFront()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval(10, 100) );
+ intervals.sub( Interval(10, 20) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 20u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 100u, intervals.front().upper_bound() );
+ } // eo SubIntervalsCutFront()
+
+
+ void SubIntervalsCutBack()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval(10, 100) );
+ intervals.sub( Interval(87, 100) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 10u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 87u, intervals.front().upper_bound() );
+ } // eo SubIntervalsCutBack()
+
+
+
+ void SubIntervalsCutMore()
+ {
+ Intervals intervals;
+
+ intervals.add( Interval( 10, 100) );
+ intervals.add( Interval(110, 200) );
+ intervals.add( Interval(210, 300) );
+
+ // this should remove the first 2 intervals and cut the third:
+ intervals.sub( Interval(8, 220) );
+
+ CPPUNIT_ASSERT_EQUAL( false, intervals.empty() );
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals.size() );
+
+ CPPUNIT_ASSERT_EQUAL( 220u, intervals.front().lower_bound() );
+ CPPUNIT_ASSERT_EQUAL( 300u, intervals.front().upper_bound() );
+ } // eo SubIntervalsCutMore()
+
+
+ void IntervalComparisons()
+ {
+ Intervals intervals1;
+ Intervals intervals2;
+
+ intervals1.add( Interval( 10, 120) );
+
+ intervals2.add( Interval( 10, 110 ) );
+ intervals2.add( Interval( 100, 120 ) );
+
+ CPPUNIT_ASSERT_EQUAL( 1u, intervals2.size() );
+
+ CPPUNIT_ASSERT( intervals1 == intervals2 );
+ CPPUNIT_ASSERT_EQUAL( true, intervals1.contains( intervals2 ));
+ CPPUNIT_ASSERT_EQUAL( true, intervals2.contains( intervals1 ));
+
+ intervals2.sub( Interval( 40, 50) );
+
+ CPPUNIT_ASSERT( intervals1 != intervals2 );
+ CPPUNIT_ASSERT_EQUAL( true, intervals1.contains( intervals2 ));
+ CPPUNIT_ASSERT_EQUAL( false, intervals2.contains( intervals1 ));
+ } // eo IntervalComparisons()
+
+
+}; // eo class TestTimeFunc
+
+CPPUNIT_TEST_SUITE_REGISTRATION(TestTimeFunc);