2047 |
04 Sep 09 |
peter |
// $Id$ |
2047 |
04 Sep 09 |
peter |
2 |
|
2047 |
04 Sep 09 |
peter |
3 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2012 Peter Johansson |
2047 |
04 Sep 09 |
peter |
5 |
|
2047 |
04 Sep 09 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
2047 |
04 Sep 09 |
peter |
7 |
|
2047 |
04 Sep 09 |
peter |
The yat library is free software; you can redistribute it and/or |
2047 |
04 Sep 09 |
peter |
modify it under the terms of the GNU General Public License as |
2047 |
04 Sep 09 |
peter |
published by the Free Software Foundation; either version 3 of the |
2047 |
04 Sep 09 |
peter |
License, or (at your option) any later version. |
2047 |
04 Sep 09 |
peter |
12 |
|
2047 |
04 Sep 09 |
peter |
The yat library is distributed in the hope that it will be useful, |
2047 |
04 Sep 09 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2047 |
04 Sep 09 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2047 |
04 Sep 09 |
peter |
General Public License for more details. |
2047 |
04 Sep 09 |
peter |
17 |
|
2047 |
04 Sep 09 |
peter |
You should have received a copy of the GNU General Public License |
2047 |
04 Sep 09 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
2047 |
04 Sep 09 |
peter |
20 |
*/ |
2047 |
04 Sep 09 |
peter |
21 |
|
2881 |
18 Nov 12 |
peter |
22 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
23 |
|
2047 |
04 Sep 09 |
peter |
24 |
#include "Suite.h" |
2047 |
04 Sep 09 |
peter |
25 |
|
2047 |
04 Sep 09 |
peter |
26 |
#include "yat/utility/StreamRedirect.h" |
2047 |
04 Sep 09 |
peter |
27 |
|
2047 |
04 Sep 09 |
peter |
28 |
#include <iostream> |
2047 |
04 Sep 09 |
peter |
29 |
#include <sstream> |
2047 |
04 Sep 09 |
peter |
30 |
|
2047 |
04 Sep 09 |
peter |
31 |
using namespace theplu::yat; |
2047 |
04 Sep 09 |
peter |
32 |
using utility::StreamRedirect; |
2047 |
04 Sep 09 |
peter |
33 |
using utility::wStreamRedirect; |
2047 |
04 Sep 09 |
peter |
34 |
|
2047 |
04 Sep 09 |
peter |
35 |
void test1(test::Suite&); |
2047 |
04 Sep 09 |
peter |
36 |
void test2(test::Suite&); |
2048 |
04 Sep 09 |
peter |
37 |
void test3(test::Suite&); |
2047 |
04 Sep 09 |
peter |
38 |
void test_cout(test::Suite&); |
2050 |
06 Sep 09 |
peter |
39 |
void test_ofstream(test::Suite&); |
2047 |
04 Sep 09 |
peter |
40 |
|
2047 |
04 Sep 09 |
peter |
41 |
int main(int argc, char* argv[]) |
4200 |
19 Aug 22 |
peter |
42 |
{ |
2047 |
04 Sep 09 |
peter |
43 |
theplu::yat::test::Suite suite(argc, argv); |
2047 |
04 Sep 09 |
peter |
44 |
|
2047 |
04 Sep 09 |
peter |
45 |
test1(suite); |
2047 |
04 Sep 09 |
peter |
46 |
test2(suite); |
2048 |
04 Sep 09 |
peter |
47 |
test3(suite); |
2050 |
06 Sep 09 |
peter |
48 |
test_ofstream(suite); |
2047 |
04 Sep 09 |
peter |
49 |
|
2047 |
04 Sep 09 |
peter |
// keep this last in case it screws up cout |
2047 |
04 Sep 09 |
peter |
51 |
test_cout(suite); |
2047 |
04 Sep 09 |
peter |
52 |
|
2047 |
04 Sep 09 |
peter |
53 |
return suite.return_value(); |
2047 |
04 Sep 09 |
peter |
54 |
} |
2047 |
04 Sep 09 |
peter |
55 |
|
2047 |
04 Sep 09 |
peter |
56 |
|
2047 |
04 Sep 09 |
peter |
57 |
void test1(test::Suite& suite) |
2047 |
04 Sep 09 |
peter |
58 |
{ |
2047 |
04 Sep 09 |
peter |
59 |
suite.out() << "testing redirecting ostream\n"; |
2047 |
04 Sep 09 |
peter |
60 |
std::ostringstream global; |
2047 |
04 Sep 09 |
peter |
61 |
{ |
2047 |
04 Sep 09 |
peter |
62 |
std::ostringstream local; |
2047 |
04 Sep 09 |
peter |
63 |
StreamRedirect switcher(global, local); |
2047 |
04 Sep 09 |
peter |
64 |
std::string msg("Hello World!"); |
2047 |
04 Sep 09 |
peter |
65 |
global << msg << std::flush; |
2047 |
04 Sep 09 |
peter |
66 |
if (local.str()!=msg) { |
2047 |
04 Sep 09 |
peter |
67 |
suite.add(false); |
2047 |
04 Sep 09 |
peter |
68 |
suite.err() << "error: expected: " << msg << "\n" |
2047 |
04 Sep 09 |
peter |
69 |
<< " found: " << local.str() << "\n" |
2047 |
04 Sep 09 |
peter |
70 |
<< " var global: " << global.str() << "\n"; |
4200 |
19 Aug 22 |
peter |
71 |
|
2047 |
04 Sep 09 |
peter |
72 |
suite.out() << "exit local scope" << std::endl; |
2047 |
04 Sep 09 |
peter |
73 |
} |
2047 |
04 Sep 09 |
peter |
74 |
} |
2047 |
04 Sep 09 |
peter |
75 |
suite.out() << "exit test1: status: " << suite.ok() << std::endl; |
2047 |
04 Sep 09 |
peter |
76 |
} |
2047 |
04 Sep 09 |
peter |
77 |
|
2047 |
04 Sep 09 |
peter |
78 |
void test2(test::Suite& suite) |
2047 |
04 Sep 09 |
peter |
79 |
{ |
2047 |
04 Sep 09 |
peter |
80 |
suite.out() << "testing redirecting wostream\n"; |
2047 |
04 Sep 09 |
peter |
81 |
std::wostringstream global; |
2047 |
04 Sep 09 |
peter |
82 |
{ |
2047 |
04 Sep 09 |
peter |
83 |
std::wostringstream local; |
2047 |
04 Sep 09 |
peter |
84 |
wStreamRedirect switcher(global, local); |
2047 |
04 Sep 09 |
peter |
85 |
wchar_t x='x'; |
2047 |
04 Sep 09 |
peter |
86 |
std::wstring msg(x, 3);; |
2047 |
04 Sep 09 |
peter |
87 |
global << msg << std::flush; |
2047 |
04 Sep 09 |
peter |
88 |
if (local.str()!=msg) { |
2047 |
04 Sep 09 |
peter |
89 |
suite.add(false); |
2047 |
04 Sep 09 |
peter |
90 |
suite.err() << "failed\n"; |
2047 |
04 Sep 09 |
peter |
91 |
} |
2047 |
04 Sep 09 |
peter |
92 |
} |
2047 |
04 Sep 09 |
peter |
93 |
suite.out() << "exit test1: status: " << suite.ok() << std::endl; |
2047 |
04 Sep 09 |
peter |
94 |
} |
2047 |
04 Sep 09 |
peter |
95 |
|
2047 |
04 Sep 09 |
peter |
96 |
|
2048 |
04 Sep 09 |
peter |
97 |
void test3(test::Suite& suite) |
2048 |
04 Sep 09 |
peter |
98 |
{ |
2048 |
04 Sep 09 |
peter |
99 |
suite.out() << "testing active=false\n"; |
2048 |
04 Sep 09 |
peter |
100 |
std::ostringstream* os1 = new std::ostringstream; |
2048 |
04 Sep 09 |
peter |
101 |
std::ostringstream* os2 = new std::ostringstream; |
2048 |
04 Sep 09 |
peter |
102 |
StreamRedirect* sr = new StreamRedirect(*os1, *os2, false); |
2048 |
04 Sep 09 |
peter |
103 |
delete os1; |
2048 |
04 Sep 09 |
peter |
104 |
delete os2; |
2048 |
04 Sep 09 |
peter |
105 |
delete sr; |
2048 |
04 Sep 09 |
peter |
106 |
} |
2048 |
04 Sep 09 |
peter |
107 |
|
2048 |
04 Sep 09 |
peter |
108 |
|
2050 |
06 Sep 09 |
peter |
109 |
void test_ofstream(test::Suite& suite) |
2050 |
06 Sep 09 |
peter |
110 |
{ |
2050 |
06 Sep 09 |
peter |
111 |
suite.out() << "testing redirect to ofstream\n"; |
2050 |
06 Sep 09 |
peter |
112 |
std::ostringstream my_out; |
2050 |
06 Sep 09 |
peter |
113 |
std::string msg("some message"); |
2050 |
06 Sep 09 |
peter |
114 |
std::string tmp_file("tmp.txt"); |
2050 |
06 Sep 09 |
peter |
115 |
{ |
2050 |
06 Sep 09 |
peter |
116 |
StreamRedirect sr(my_out, tmp_file.c_str()); |
2050 |
06 Sep 09 |
peter |
117 |
my_out << msg; |
2050 |
06 Sep 09 |
peter |
118 |
} |
2050 |
06 Sep 09 |
peter |
119 |
if (my_out.str().size()) { |
2050 |
06 Sep 09 |
peter |
120 |
suite.err() << "my_out contains: '" << my_out.str() |
2050 |
06 Sep 09 |
peter |
121 |
<< "'\n expected empty\n"; |
2050 |
06 Sep 09 |
peter |
122 |
suite.add(false); |
2050 |
06 Sep 09 |
peter |
123 |
} |
2050 |
06 Sep 09 |
peter |
124 |
std::ifstream is(tmp_file.c_str()); |
2050 |
06 Sep 09 |
peter |
125 |
std::string line; |
2050 |
06 Sep 09 |
peter |
126 |
getline(is, line); |
2050 |
06 Sep 09 |
peter |
127 |
if (line!=msg) { |
2050 |
06 Sep 09 |
peter |
128 |
suite.add(false); |
2050 |
06 Sep 09 |
peter |
129 |
suite.err() << "expected to find '" << msg << "' in file `" |
4200 |
19 Aug 22 |
peter |
130 |
<< tmp_file << "'\nfound: '" << line << "'\n"; |
2050 |
06 Sep 09 |
peter |
131 |
} |
2050 |
06 Sep 09 |
peter |
// cleaning up |
2050 |
06 Sep 09 |
peter |
133 |
unlink(tmp_file.c_str()); |
2050 |
06 Sep 09 |
peter |
134 |
} |
2050 |
06 Sep 09 |
peter |
135 |
|
2050 |
06 Sep 09 |
peter |
136 |
|
2047 |
04 Sep 09 |
peter |
137 |
void test_cout(test::Suite& suite) |
2047 |
04 Sep 09 |
peter |
138 |
{ |
2047 |
04 Sep 09 |
peter |
139 |
suite.out() << "testing redirecting cout\n"; |
2047 |
04 Sep 09 |
peter |
140 |
std::ostringstream oss; |
2047 |
04 Sep 09 |
peter |
141 |
StreamRedirect switcher(std::cout, oss); |
2047 |
04 Sep 09 |
peter |
142 |
std::string msg("Hello World!"); |
2047 |
04 Sep 09 |
peter |
143 |
std::cout << msg << std::flush; |
2047 |
04 Sep 09 |
peter |
144 |
if (oss.str()!=msg) { |
2047 |
04 Sep 09 |
peter |
145 |
suite.add(false); |
2047 |
04 Sep 09 |
peter |
146 |
suite.err() << "error: expected: " << msg << "\n" |
2047 |
04 Sep 09 |
peter |
147 |
<< " found: " << oss.str() << "\n"; |
2047 |
04 Sep 09 |
peter |
148 |
} |
2047 |
04 Sep 09 |
peter |
149 |
suite.out() << "exit test_cout: status: " << suite.ok() << std::endl; |
2047 |
04 Sep 09 |
peter |
150 |
} |