source: trunk/test/stream_redirect_test.cc @ 2048

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

adding bool parameter in constructors and fixed some docs typos

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1// $Id: stream_redirect_test.cc 2048 2009-09-04 14:47:18Z 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&);
37
38int main(int argc, char* argv[])
39{ 
40  theplu::yat::test::Suite suite(argc, argv);
41
42  test1(suite);
43  test2(suite);
44  test3(suite);
45
46  // keep this last in case it screws up cout
47  test_cout(suite);
48
49  return suite.return_value();
50}
51
52
53void test1(test::Suite& suite)
54{
55  suite.out() << "testing redirecting ostream\n";
56  std::ostringstream global;
57  {
58    std::ostringstream local;
59    StreamRedirect switcher(global, local);
60    std::string msg("Hello World!");
61    global << msg << std::flush;
62    if (local.str()!=msg) {
63      suite.add(false);
64      suite.err() << "error: expected: " << msg << "\n"
65                  << "       found:    " << local.str() << "\n"
66                  << "   var global:   " << global.str() << "\n";
67     
68      suite.out() << "exit local scope" << std::endl;
69    }
70  }
71  suite.out() << "exit test1: status: " << suite.ok() << std::endl;
72}
73
74void test2(test::Suite& suite)
75{
76  suite.out() << "testing redirecting wostream\n";
77  std::wostringstream global;
78  {
79    std::wostringstream local;
80    wStreamRedirect switcher(global, local);
81    wchar_t x='x';
82    std::wstring msg(x, 3);;
83    global << msg << std::flush;
84    if (local.str()!=msg) {
85      suite.add(false);
86      suite.err() << "failed\n";
87    }
88  }
89  suite.out() << "exit test1: status: " << suite.ok() << std::endl;
90}
91
92
93void test3(test::Suite& suite)
94{
95  suite.out() << "testing active=false\n";
96  std::ostringstream* os1 = new std::ostringstream;
97  std::ostringstream* os2 = new std::ostringstream;
98  StreamRedirect* sr = new StreamRedirect(*os1, *os2, false);
99  delete os1;
100  delete os2;
101  delete sr;
102}
103
104
105void test_cout(test::Suite& suite)
106{
107  suite.out() << "testing redirecting cout\n";
108  std::ostringstream oss;
109  StreamRedirect switcher(std::cout, oss);
110  std::string msg("Hello World!");
111  std::cout << msg << std::flush;
112  if (oss.str()!=msg) {
113    suite.add(false);
114    suite.err() << "error: expected: " << msg << "\n"
115                << "       found:    " << oss.str() << "\n";
116  }
117  suite.out() << "exit test_cout: status: " << suite.ok() << std::endl;
118}
Note: See TracBrowser for help on using the repository browser.