File: tut_helpers.h

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (207 lines) | stat: -rw-r--r-- 4,534 bytes parent folder | download | duplicates (4)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef _TUT_HELPERS_H_
#define _TUT_HELPERS_H_

#ifdef _WINDOWS
#include <windows.h>
#else
#include <sys/time.h>
#endif

#include "tut.h"

/*
namespace tut
{
  template <class Data,int MaxTestsInGroup = 50>
  class test_group_with_desc : public test_group<Data, MaxTestsInGroup>
  {
    const char* desc_;

  public:

    test_group_with_desc(const char* name, const char *desc)
    : test_group(name), desc_(desc)
    {}
  }
}
*/

namespace tut
{
  class test_time_point
  {
#ifdef _WINDOWS
    long ticks_;
    test_time_point(DWORD t)
    : ticks_(t)
    {}
  public:
    test_time_point()
    : ticks_(GetTickCount())
    {}
    friend test_time_point operator - (const test_time_point& t1,
                                       const test_time_point& t2);

    friend std::ostream& operator << (ostream& os, const test_time_point& ttp);
#else
  public:
    long ticks_;
    test_time_point(long t)
    : ticks_(t)
    {}
    test_time_point()
    {
      struct timeval tv;
      gettimeofday(&tv, NULL);
      ticks_= tv.tv_sec * 1000 + tv.tv_usec / 1000;
    }
#endif

  };

  inline test_time_point operator - (const test_time_point& t1,
                              const test_time_point& t2)
  {
    return test_time_point(t1.ticks_ - t2.ticks_);
  }

  inline std::ostream& operator << (std::ostream& os, const test_time_point& ttp)
  {
    long sec= ttp.ticks_ / 1000;
    long ms=  ttp.ticks_ % 1000;

    os << sec << "." << ms << " sec" << std::endl;

    return os;
  }

  /*
    debug_stringbuf is used by debug_ostream to
    perform output to an OS debugging output facility
  */

  class debug_stringbuf :
    public std::basic_stringbuf<char, std::char_traits<char> >
  {
  public:

    virtual int sync()
    {
#ifdef _WINDOWS
      OutputDebugString(str().c_str());
#else
      std::cerr << str();  // todo: change this?
#endif
      str("");  // set empty string - clean the buffer
      return 0;
    }
  };

  class debug_ostream : public std::ostream
  {
  public:
    debug_ostream()
    : std::ostream(new debug_stringbuf())
    {}

    ~debug_ostream()
    {
      delete rdbuf();
    }
  };

  class mysql_reporter : public tut::callback
  {
    std::ostream *os;

    size_t per_group_failed;
    size_t per_group_passed;

    test_time_point start_time;
    Global_test_parameters *test_params;

  public:
    size_t total_failed;
    size_t total_passed;

    mysql_reporter(Global_test_parameters *p)
    : os(NULL), test_params(p)
    {
      if(test_params->get_output_mode() == Global_test_parameters::DEBUGGER)
      {
        os= new debug_ostream;
      }
      else
      {
        os= &std::cout;
      }
    }

    ~mysql_reporter()
    {
      if(os != &std::cout)
        delete os;
    }


    void run_started()
    {
      total_failed= total_passed= 0;
      start_time= test_time_point();

      if(group_base::get_disabled_groups_count() > 0)
        *os << "*** Disabled modules: " << group_base::get_disabled_groups_count() << std::endl;
    }

    void run_completed()
    {
      *os << std::endl << "Run complete. [";
      if(test_params->report_passed())
        *os << "passed: " << total_passed;

      if(total_failed > 0)
        *os <<  " failed: " << total_failed;

      *os << "]"
        << std::endl << "Duration: " << (test_time_point() - start_time)
        << std::endl;
    }

    void group_started(const std::string& name)
    {
      per_group_failed= 0;
      per_group_passed= 0;
      *os << std::endl << "Module " << name << std::endl << std::endl;
    }

    void test_completed(const test_result& tr)
    {
      if((tr.result == test_result::ok))
      {
        per_group_passed++;
        if(test_params->report_passed())
          *os << tr.test << ": passed" << std::endl;
      }
      else
      {
        per_group_failed++;
        *os << tr.test << ": failed (" << tr.message << ")" << std::endl;
      }
    }

    void group_completed(const std::string& name)
    {
      *os << std::endl << "Module Results [";
      if(test_params->report_passed())
        *os << "passed: " << per_group_passed;
      if(per_group_failed > 0)
        *os << " failed: " << per_group_failed;
      *os << "]" << std::endl;

      total_failed += per_group_failed;
      total_passed += per_group_passed;
    }
  };
};

#endif // _TUT_HELPERS_H_