Added copyright and license
[libcds.git] / tests / cppunit / file_reporter.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 /*
32  * Copyright (c) 2003, 2004
33  * Zdenek Nemec
34  *
35  * This material is provided "as is", with absolutely no warranty expressed
36  * or implied. Any use is at your own risk.
37  *
38  * Permission to use or copy this software for any purpose is hereby granted
39  * without fee, provided the above notices are retained on all copies.
40  * Permission to modify the code and to distribute modified code is granted,
41  * provided the above notices are retained, and a notice that the code was
42  * modified is included with the above copyright notice.
43  *
44  */
45
46 /* $Id$ */
47
48 #ifndef CDS_CPPUNIT_MINIFILEREPORTERINTERFACE_H_
49 #define CDS_CPPUNIT_MINIFILEREPORTERINTERFACE_H_
50
51 #include <stdio.h>
52
53 #include <cds/os/timer.h>
54
55 namespace CppUnitMini {
56     typedef cds::OS::Timer    Timer;
57     //
58     // CppUnit mini file(stream) reporter
59     //
60     class FileReporter : public CppUnitMini::Reporter {
61     private:
62       FileReporter(const FileReporter&);
63       FileReporter& operator=(const FileReporter&);
64     public:
65       // reporting to stderr
66       explicit FileReporter(bool doMonitor = false):
67           m_numErrors(0), m_numIgnored(0), m_numExplicit(0), m_numTests(0), _myStream(false),
68           m_failed(false), m_doMonitor(doMonitor)
69       { _file = stderr; }
70
71       // reporting to the file with the given name
72       explicit FileReporter(const char* file, bool doMonitor = false):
73           m_numErrors(0), m_numIgnored(0), m_numExplicit(0), m_numTests(0), _myStream(true),
74           m_failed(false), m_doMonitor(doMonitor)
75       {
76     #ifndef _STLP_USE_SAFE_STRING_FUNCTIONS
77         _file = fopen(file, "w");
78     #else
79         fopen_s(&_file, file, "w");
80     #endif
81       }
82
83       // reporting to the given file
84       explicit FileReporter(FILE* stream, bool doMonitor = false):
85           m_numErrors(0), m_numIgnored(0), m_numExplicit(0), m_numTests(0), _myStream(false),
86           m_failed(false), m_doMonitor(doMonitor)
87       { _file = stream; }
88
89       virtual ~FileReporter() {
90         if (_myStream)
91           fclose(_file);
92         else
93           fflush(_file);
94       }
95
96       virtual void error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line) {
97         // Error might be called several times between 2 progress calls, we shouldn't however consider
98         // that a test failed twice so we simply keep the info that test failed, number of failed tests
99         // is computed later in end method.
100         m_failed = true;
101         fprintf(_file, "\n\n%s(%d) : %s(%s);", in_file, in_line, in_macroName, in_macro);
102         fflush(_file);
103       }
104
105       virtual void message( const char *msg )
106       { fprintf(_file, "\n\t%s", msg ); fflush(_file); }
107
108       virtual void progress(const char *in_className, const char *in_shortTestName, bool ignored, bool explicitTest) {
109         if (m_doMonitor)
110           m_testTimer.reset();
111
112         ++m_numTests;
113         m_failed = false;
114         if (ignored)
115           ++m_numIgnored;
116         fprintf(_file, "%s::%s", in_className, in_shortTestName);
117         if (ignored) {
118           const char *ignoredReason;
119           if (explicitTest) {
120             ++m_numExplicit;
121             ignoredReason = " EXPLICIT";
122           }
123           else
124             ignoredReason = " IGNORED";
125
126           fprintf(_file, "%s", ignoredReason);
127         }
128       }
129
130       virtual void end() {
131         if (m_doMonitor) {
132           fprintf(_file, " %f msec", m_testTimer.duration());
133         }
134         if (m_failed) {
135           ++m_numErrors;
136         }
137         fprintf(_file, "\n");
138         fflush(_file);
139       }
140
141       virtual void printSummary() {
142         if (m_numErrors > 0) {
143           fprintf(_file, "\nThere were errors! %d of %d tests", m_numErrors, m_numTests);
144         }
145         else {
146           fprintf(_file, "\nOK %d tests", m_numTests);
147         }
148
149         if (m_numIgnored > 0) {
150           fprintf(_file, ", %d ignored", m_numIgnored);
151         }
152
153         if (m_numExplicit > 0) {
154           fprintf(_file, " (%d explicit)", m_numExplicit);
155         }
156
157         fprintf(_file, "\n\n");
158         fflush(_file);
159       }
160     private:
161       int m_numErrors;
162       int m_numIgnored;
163       int m_numExplicit;
164       int m_numTests;
165       // flag whether we own '_file' and are thus responsible for releasing it in the destructor
166       bool  _myStream;
167       bool m_failed;
168       bool m_doMonitor;
169       Timer m_testTimer;
170       FILE* _file;
171     };
172 }
173 #endif // #ifndef CDS_CPPUNIT_MINIFILEREPORTERINTERFACE_H_