source: trunk/test/stream_redirect_test.cc @ 2047

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

closes #520 and refs #521. Implements the interface defined in #520 which should be extended to also include the interface discussed in #521

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