QMCPACK
test_infostream.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2017 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, mdewing@anl.gov Argonne National Laboratory
8 //
9 // File created by: Mark Dewing, mdewing@anl.gov Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
16 #include <stdio.h>
17 #include <string>
18 #include <vector>
19 #include <sstream>
20 #include <fstream>
21 
22 TEST_CASE("InfoStream basic", "[utilities]")
23 {
24  std::ostringstream out;
25  InfoStream info(&out);
26  info.getStream() << "test";
27  REQUIRE(out.str() == "test");
28  REQUIRE(info.isActive());
29 
30  info.pause();
31  info.getStream() << "empty";
32  REQUIRE(out.str() == "test");
33  REQUIRE(!info.isActive());
34 
35  info.resume();
36  info.getStream() << "second";
37  REQUIRE(out.str() == "testsecond");
38 }
39 
40 TEST_CASE("InfoStream redirect to new stream", "[utilities]")
41 {
42  std::ostringstream out1;
43  std::ostringstream out2;
44  InfoStream info1(&out1);
45  InfoStream info2(&out2);
46 
47  info1.getStream() << "test1";
48  info2.getStream() << "test2";
49 
50  info1.redirectToSameStream(info2);
51  info1.getStream() << "test3";
52 
53  REQUIRE(out2.str() == "test2test3");
54  REQUIRE(out1.str() == "test1");
55 }
56 
57 TEST_CASE("InfoStream redirect to file", "[utilities]")
58 {
59  std::ostringstream out;
60  InfoStream info(&out);
61  info.redirectToFile("test_infostream.log");
62  info.getStream() << "test";
63  info.flush();
64 
65  std::ifstream in("test_infostream.log");
66  std::string s;
67  in >> s;
68  REQUIRE(s == "test");
69 }
70 
71 TEST_CASE("InfoStream double pause", "[utilities]")
72 {
73  std::ostringstream out;
74  InfoStream info(&out);
75  info.pause();
76  info.pause();
77  info.resume();
78  info.getStream() << "test";
79  REQUIRE(out.str() == "test");
80 }
81 
82 TEST_CASE("InfoStream shutOff", "[utilities]")
83 {
84  std::ostringstream out;
85  InfoStream info(&out);
86  info.shutOff();
87  info.pause();
88  info.resume();
89  info.getStream() << "test";
90  REQUIRE(out.str() == "");
91 }
92 
93 TEST_CASE("InfoStream operator", "[utilities]")
94 {
95  std::ostringstream out;
96  InfoStream info(&out);
97  info << "test";
98  info << 1;
99  REQUIRE(out.str() == "test1");
100 }
bool isActive() const
check if the stream is active
Definition: InfoStream.h:51
Interface to output streams.
Definition: InfoStream.h:28
std::ostream & getStream(const std::string &tag="")
returns current stream
Definition: InfoStream.h:37
TEST_CASE("InfoStream basic", "[utilities]")
void resume()
Continue output on the stream used before pausing.
Definition: InfoStream.cpp:47
void redirectToFile(const std::string &fname)
Open a file and output to that file.
Definition: InfoStream.cpp:60
Declaration of InfoStream class.
REQUIRE(std::filesystem::exists(filename))
void flush()
flush stream buffer
Definition: InfoStream.cpp:39
void redirectToSameStream(InfoStream &info)
Copy a stream.
Definition: InfoStream.cpp:70
void shutOff()
Permanently turn off the stream.
Definition: InfoStream.cpp:53
void pause()
Stop output (redirect to a null stream)
Definition: InfoStream.cpp:41