summaryrefslogtreecommitdiff
path: root/examples/ClockerPlugIn/ClockerModel.cpp
blob: f3b123066b3775eea5d1bc078f6ed78693c28a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// //////////////////////////////////////////////////////////////////////////
// Implementation file ClockerModel.cpp for class ClockerModel
// (c)Copyright 2000, Baptiste Lepilleur.
// Created: 2002/06/14
// //////////////////////////////////////////////////////////////////////////
#include "ClockerModel.h"
#include <cppunit/config/SourcePrefix.h>


ClockerModel::ClockerModel()
    : m_testCaseCount( 0 )
    , m_totalTestCaseTime( 0 )
{
}


ClockerModel::~ClockerModel()
{
}


void 
ClockerModel::setExpectedTestCount( int count )
{
  m_tests.reserve( count );
}


void 
ClockerModel::enterTest( CPPUNIT_NS::Test *test,
                         bool isSuite )
{
  m_currentPath.add( test );

  int testIndex = m_tests.size();
  if ( !m_testIndexes.empty() )
    m_tests[ m_testIndexes.top() ].m_childIndexes.push_back( testIndex );
  m_testIndexes.push( testIndex );
  m_testToIndexes.insert( TestToIndexes::value_type( test, testIndex ) );

  TestInfo info;
  info.m_timer.start();
  info.m_path = m_currentPath;
  info.m_isSuite = isSuite;

  m_tests.push_back( info );

  if ( !isSuite )
    ++m_testCaseCount;
}


void 
ClockerModel::exitTest( CPPUNIT_NS::Test *test,
                        bool isSuite )
{
  m_tests[ m_testIndexes.top() ].m_timer.finish();
  if ( !isSuite )
    m_totalTestCaseTime += m_tests.back().m_timer.elapsedTime();

  m_currentPath.up();
  m_testIndexes.pop();
}


double 
ClockerModel::totalElapsedTime() const
{
  return m_tests[0].m_timer.elapsedTime();
}


double 
ClockerModel::averageTestCaseTime() const
{
  double average = 0;
  if ( m_testCaseCount > 0 )
    average = m_totalTestCaseTime / m_testCaseCount;
  return average;
}


double 
ClockerModel::testTimeFor( int testIndex ) const
{
  return m_tests[ testIndex ].m_timer.elapsedTime();
}


std::string 
ClockerModel::timeStringFor( double time )
{
  char buffer[320];
  const char *format;
  if ( time < 1 )
    format = "%2.3f";
  else if ( time < 10 )
    format = "%3.2f";
  else if (time < 100 )
    format = "%4.1f";
  else
    format = "%6f";

  ::sprintf( buffer, format, time );

  return buffer;
}


bool 
ClockerModel::isSuite( int testIndex ) const
{
  return m_tests[ testIndex ].m_isSuite;
}


const CPPUNIT_NS::TestPath &
ClockerModel::testPathFor( int testIndex ) const
{
  return m_tests[ testIndex ].m_path;
}


int 
ClockerModel::indexOf( CPPUNIT_NS::Test *test ) const
{
  TestToIndexes::const_iterator itFound = m_testToIndexes.find( test );
  if ( itFound != m_testToIndexes.end() )
    return itFound->second;
  return -1;
}


int 
ClockerModel::childCountFor( int testIndex ) const
{
  return m_tests[ testIndex ].m_childIndexes.size();
}


int 
ClockerModel::childAtFor( int testIndex, 
                          int chidIndex ) const
{
  return m_tests[ testIndex ].m_childIndexes[ chidIndex ];
}