source: trunk/test/stream_redirect_test.cc @ 2050

Last change on this file since 2050 was 2050, checked in by Peter, 12 years ago

refs #521. added constructor taking ostream and string. It does not take char* (as ofstream does) because I don't see the point with that. Needed to split member variable ios_ into a ostream* and istream* because the two cases need separate treatment (ostream needs to be flushed in destructor).

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