Debian Codegraph @

packages/stk/stk_unit_tests/stk_util/parallel/UnitTestOutputStreams.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include "gtest/gtest.h"
36 #include <stk_util/parallel/Parallel.hpp>
37 #include <stk_util/parallel/OutputStreams.hpp>
38 #include <sstream>
39 #include <string>
40
41 TEST(stkOutputStreams, set_outputP0)
42 {
43 std::ostringstream oss;
44 stk::set_outputP0(&oss);
45 std::string expected("hello world");
46 stk::outputP0() << expected;
47
48 const int myProc = stk::parallel_machine_rank(MPI_COMM_WORLD);
49 if (myProc == 0) {
50 EXPECT_EQ(expected, oss.str());
51 }
52 else {
53 EXPECT_EQ(std::string(""), oss.str());
54 }
55
56 stk::reset_default_output_streams();
57 }
58
59 TEST(stkOutputStreams, outputP0_np2)
60 {
61 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { GTEST_SKIP(); }
62
63 const int myProc = stk::parallel_machine_rank(MPI_COMM_WORLD);
64
65 stk::reset_default_output_streams();
66
67 testing::internal::CaptureStdout();
68
69 std::string expected("hello world");
70 stk::outputP0() << expected;
71
72 std::string stdoutString = testing::internal::GetCapturedStdout();
73 if (myProc == 0) {
74 EXPECT_EQ(expected, stdoutString);
75 }
76 else {
77 EXPECT_EQ(std::string(""), stdoutString);
78 }
79
80 stk::reset_default_output_streams();
81 }
82
83 TEST(stkOutputStreams, output_flush)
84 {
85 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 2) { GTEST_SKIP(); }
86
87 const int myProc = stk::parallel_machine_rank(MPI_COMM_WORLD);
88
89 stk::reset_default_output_streams();
90
91 std::ostringstream oss;
92 stk::set_outputP0(&oss);
93
94 stk::output() << "<hello from P"<<std::to_string(myProc)<<">";
95
96 stk::output_flush();
97
98 if (myProc == 0) {
99 EXPECT_EQ(std::string("<hello from P0>\n<hello from P1>\n"), oss.str());
100 }
101 else {
102 EXPECT_EQ(std::string(""), oss.str());
103 }
104
105 stk::reset_default_output_streams();
106 }