source: src/unittests/ChronosUnitTest.cpp@ 6aa233

Last change on this file since 6aa233 was 6aa233, checked in by Frederik Heber <heber@…>, 15 years ago

FIX: last test in ChronosTest::dummyTest() had numerical rounding problem.

  • sequence of summing of array timings and in Chronos differed, hence we got rounding issues. Now we check against numeric_limits<double>::epsilon().
  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * ChronosUnitTest.cpp
10 */
11
12
13// include config.h
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17
18using namespace std;
19
20#include <cppunit/CompilerOutputter.h>
21#include <cppunit/extensions/TestFactoryRegistry.h>
22#include <cppunit/ui/text/TestRunner.h>
23
24#include <limits>
25
26#include "Chronos.hpp"
27#include "Info.hpp"
28
29#include "ChronosUnitTest.hpp"
30
31#ifdef HAVE_TESTRUNNER
32#include "UnitTestMain.hpp"
33#endif /*HAVE_TESTRUNNER*/
34
35/********************************************** Test classes **************************************/
36
37// Registers the fixture into the 'registry'
38CPPUNIT_TEST_SUITE_REGISTRATION( ChronosTest );
39
40
41void dummy()
42{
43 Info FunctionInfo(__func__);
44 for (int i=0;i<10000000;++i)
45 std::cout << "";
46}
47
48void dummy_two()
49{
50 Info FunctionInfo(__func__);
51 for (int i=0;i<1000000;++i)
52 std::cout << "";
53}
54
55
56void ChronosTest::setUp()
57{
58}
59
60void ChronosTest::tearDown()
61{
62 Chronos::purgeInstance();
63}
64
65/**
66 * UnitTest for Chronos()
67 */
68void ChronosTest::InstanceTest()
69{
70 // check that we get a Non-NULL pointer
71 CPPUNIT_ASSERT(Chronos::getPointer());
72}
73
74/**
75 * UnitTest for Chronos()
76 */
77void ChronosTest::InfoTest()
78{
79 Info FunctionInfo(__func__);
80 CPPUNIT_ASSERT_EQUAL( (size_t) 1, Info::NumberInfos );
81}
82
83/**
84 * UnitTest for Chronos()
85 */
86void ChronosTest::dummyTest()
87{
88 double timings[4];
89 // first dummy
90 dummy();
91 CPPUNIT_ASSERT( Chronos::getInstance().TimeRunning.find(std::string("dummy"))
92 != Chronos::getInstance().TimeRunning.end() );
93 CPPUNIT_ASSERT_EQUAL( (size_t) 1, Chronos::getInstance().SumUpTotalFunctions() );
94 timings[0] = Chronos::getInstance().AccountedTime[std::string("dummy")];
95 std::cout << "Timing[0]: " << timings[0] << std::endl;
96 CPPUNIT_ASSERT(timings[0] > 0.);
97 dummy();
98 // second call goes to same entry
99 CPPUNIT_ASSERT_EQUAL( (size_t) 1, Chronos::getInstance().SumUpTotalFunctions() );
100 timings[1] = Chronos::getInstance().AccountedTime[std::string("dummy")] - timings[0];
101 std::cout << "Timing[1]: " << timings[1] << std::endl;
102 CPPUNIT_ASSERT(timings[1] > 0.);
103
104
105 // second dummy
106 dummy_two();
107 CPPUNIT_ASSERT( Chronos::getInstance().TimeRunning.find(std::string("dummy_two"))
108 != Chronos::getInstance().TimeRunning.end() );
109 CPPUNIT_ASSERT_EQUAL( (size_t) 2, Chronos::getInstance().SumUpTotalFunctions() );
110 timings[2] = Chronos::getInstance().AccountedTime[std::string("dummy_two")];
111 std::cout << "Timing[2]: " << timings[2] << std::endl;
112 CPPUNIT_ASSERT(timings[2] > 0.);
113
114 // "inline" dummy
115 {
116 Info DummyInfo("dummy_three");
117 for (int i=0;i<1000000;++i)
118 std::cout << "";
119 }
120 CPPUNIT_ASSERT( Chronos::getInstance().TimeRunning.find(std::string("dummy_three"))
121 != Chronos::getInstance().TimeRunning.end() );
122 CPPUNIT_ASSERT_EQUAL( (size_t) 3, Chronos::getInstance().SumUpTotalFunctions() );
123 timings[3] = Chronos::getInstance().AccountedTime[std::string("dummy_three")];
124 std::cout << "Timing[3]: " << timings[3] << std::endl;
125 CPPUNIT_ASSERT(timings[3] > 0.);
126
127 // check summing of times
128 CPPUNIT_ASSERT( fabs(timings[0] + timings[1] + timings[2] + timings[3]- Chronos::getInstance().SumUpTotalTime()) < numeric_limits<double>::epsilon());
129
130 std::cout << Chronos::getInstance() << std::endl;
131}
Note: See TracBrowser for help on using the repository browser.