issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / tests / cppunit / file_reporter.h
1 //$$CDS-header$$
2
3 /*
4  * Copyright (c) 2003, 2004
5  * Zdenek Nemec
6  *
7  * This material is provided "as is", with absolutely no warranty expressed
8  * or implied. Any use is at your own risk.
9  *
10  * Permission to use or copy this software for any purpose is hereby granted
11  * without fee, provided the above notices are retained on all copies.
12  * Permission to modify the code and to distribute modified code is granted,
13  * provided the above notices are retained, and a notice that the code was
14  * modified is included with the above copyright notice.
15  *
16  */
17
18 /* $Id$ */
19
20 #ifndef CDS_CPPUNIT_MINIFILEREPORTERINTERFACE_H_
21 #define CDS_CPPUNIT_MINIFILEREPORTERINTERFACE_H_
22
23 #include <stdio.h>
24
25 #include <cds/os/timer.h>
26
27 namespace CppUnitMini {
28     typedef cds::OS::Timer    Timer;
29     //
30     // CppUnit mini file(stream) reporter
31     //
32     class FileReporter : public CppUnitMini::Reporter {
33     private:
34       FileReporter(const FileReporter&);
35       FileReporter& operator=(const FileReporter&);
36     public:
37       // reporting to stderr
38       explicit FileReporter(bool doMonitor = false):
39           m_numErrors(0), m_numIgnored(0), m_numExplicit(0), m_numTests(0), _myStream(false),
40           m_failed(false), m_doMonitor(doMonitor)
41       { _file = stderr; }
42
43       // reporting to the file with the given name
44       explicit FileReporter(const char* file, bool doMonitor = false):
45           m_numErrors(0), m_numIgnored(0), m_numExplicit(0), m_numTests(0), _myStream(true),
46           m_failed(false), m_doMonitor(doMonitor)
47       {
48     #ifndef _STLP_USE_SAFE_STRING_FUNCTIONS
49         _file = fopen(file, "w");
50     #else
51         fopen_s(&_file, file, "w");
52     #endif
53       }
54
55       // reporting to the given file
56       explicit FileReporter(FILE* stream, bool doMonitor = false):
57           m_numErrors(0), m_numIgnored(0), m_numExplicit(0), m_numTests(0), _myStream(false),
58           m_failed(false), m_doMonitor(doMonitor)
59       { _file = stream; }
60
61       virtual ~FileReporter() {
62         if (_myStream)
63           fclose(_file);
64         else
65           fflush(_file);
66       }
67
68       virtual void error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line) {
69         // Error might be called several times between 2 progress calls, we shouldn't however consider
70         // that a test failed twice so we simply keep the info that test failed, number of failed tests
71         // is computed later in end method.
72         m_failed = true;
73         fprintf(_file, "\n\n%s(%d) : %s(%s);", in_file, in_line, in_macroName, in_macro);
74         fflush(_file);
75       }
76
77       virtual void message( const char *msg )
78       { fprintf(_file, "\n\t%s", msg ); fflush(_file); }
79
80       virtual void progress(const char *in_className, const char *in_shortTestName, bool ignored, bool explicitTest) {
81         if (m_doMonitor)
82           m_testTimer.reset();
83
84         ++m_numTests;
85         m_failed = false;
86         if (ignored)
87           ++m_numIgnored;
88         fprintf(_file, "%s::%s", in_className, in_shortTestName);
89         if (ignored) {
90           const char *ignoredReason;
91           if (explicitTest) {
92             ++m_numExplicit;
93             ignoredReason = " EXPLICIT";
94           }
95           else
96             ignoredReason = " IGNORED";
97
98           fprintf(_file, "%s", ignoredReason);
99         }
100       }
101
102       virtual void end() {
103         if (m_doMonitor) {
104           fprintf(_file, " %f msec", m_testTimer.duration());
105         }
106         if (m_failed) {
107           ++m_numErrors;
108         }
109         fprintf(_file, "\n");
110         fflush(_file);
111       }
112
113       virtual void printSummary() {
114         if (m_numErrors > 0) {
115           fprintf(_file, "\nThere were errors! %d of %d tests", m_numErrors, m_numTests);
116         }
117         else {
118           fprintf(_file, "\nOK %d tests", m_numTests);
119         }
120
121         if (m_numIgnored > 0) {
122           fprintf(_file, ", %d ignored", m_numIgnored);
123         }
124
125         if (m_numExplicit > 0) {
126           fprintf(_file, " (%d explicit)", m_numExplicit);
127         }
128
129         fprintf(_file, "\n\n");
130         fflush(_file);
131       }
132     private:
133       int m_numErrors;
134       int m_numIgnored;
135       int m_numExplicit;
136       int m_numTests;
137       // flag whether we own '_file' and are thus responsible for releasing it in the destructor
138       bool  _myStream;
139       bool m_failed;
140       bool m_doMonitor;
141       Timer m_testTimer;
142       FILE* _file;
143     };
144 }
145 #endif // #ifndef CDS_CPPUNIT_MINIFILEREPORTERINTERFACE_H_