1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/stream.hpp"
34 #include <atf-c++.hpp>
36 #include "utils/fs/path.hpp"
38 namespace fs = utils::fs;
41 ATF_TEST_CASE_WITHOUT_HEAD(open_ostream__stdout);
42 ATF_TEST_CASE_BODY(open_ostream__stdout)
44 const pid_t pid = atf::utils::fork();
46 std::auto_ptr< std::ostream > output = utils::open_ostream(
47 fs::path("/dev/stdout"));
48 (*output) << "Message to stdout\n";
50 std::exit(EXIT_SUCCESS);
52 atf::utils::wait(pid, EXIT_SUCCESS, "Message to stdout\n", "");
56 ATF_TEST_CASE_WITHOUT_HEAD(open_ostream__stderr);
57 ATF_TEST_CASE_BODY(open_ostream__stderr)
59 const pid_t pid = atf::utils::fork();
61 std::auto_ptr< std::ostream > output = utils::open_ostream(
62 fs::path("/dev/stderr"));
63 (*output) << "Message to stderr\n";
65 std::exit(EXIT_SUCCESS);
67 atf::utils::wait(pid, EXIT_SUCCESS, "", "Message to stderr\n");
71 ATF_TEST_CASE_WITHOUT_HEAD(open_ostream__other);
72 ATF_TEST_CASE_BODY(open_ostream__other)
74 const pid_t pid = atf::utils::fork();
76 std::auto_ptr< std::ostream > output = utils::open_ostream(
77 fs::path("some-file.txt"));
78 (*output) << "Message to other file\n";
80 std::exit(EXIT_SUCCESS);
82 atf::utils::wait(pid, EXIT_SUCCESS, "", "");
83 atf::utils::compare_file("some-file.txt", "Message to other file\n");
87 ATF_TEST_CASE_WITHOUT_HEAD(stream_length__empty);
88 ATF_TEST_CASE_BODY(stream_length__empty)
90 std::istringstream input("");
91 ATF_REQUIRE_EQ(0, utils::stream_length(input));
95 ATF_TEST_CASE_WITHOUT_HEAD(stream_length__some);
96 ATF_TEST_CASE_BODY(stream_length__some)
98 const std::string contents(8192, 'x');
99 std::istringstream input(contents);
102 static_cast< std::string::size_type >(utils::stream_length(input)));
106 ATF_TEST_CASE_WITHOUT_HEAD(read_file__ok);
107 ATF_TEST_CASE_BODY(read_file__ok)
109 const char* contents = "These are\nsome file contents";
110 atf::utils::create_file("input.txt", contents);
111 ATF_REQUIRE_EQ(contents, utils::read_file(fs::path("input.txt")));
115 ATF_TEST_CASE_WITHOUT_HEAD(read_file__missing_file);
116 ATF_TEST_CASE_BODY(read_file__missing_file)
118 ATF_REQUIRE_THROW_RE(std::runtime_error,
119 "Failed to open 'foo.txt' for read",
120 utils::read_file(fs::path("foo.txt")));
124 ATF_TEST_CASE_WITHOUT_HEAD(read_stream__empty);
125 ATF_TEST_CASE_BODY(read_stream__empty)
127 std::istringstream input("");
128 ATF_REQUIRE_EQ("", utils::read_stream(input));
132 ATF_TEST_CASE_WITHOUT_HEAD(read_stream__some);
133 ATF_TEST_CASE_BODY(read_stream__some)
135 std::string contents;
136 for (int i = 0; i < 1000; i++)
137 contents += "abcdef";
138 std::istringstream input(contents);
139 ATF_REQUIRE_EQ(contents, utils::read_stream(input));
143 ATF_INIT_TEST_CASES(tcs)
145 ATF_ADD_TEST_CASE(tcs, open_ostream__stdout);
146 ATF_ADD_TEST_CASE(tcs, open_ostream__stderr);
147 ATF_ADD_TEST_CASE(tcs, open_ostream__other);
149 ATF_ADD_TEST_CASE(tcs, stream_length__empty);
150 ATF_ADD_TEST_CASE(tcs, stream_length__some);
152 ATF_ADD_TEST_CASE(tcs, read_file__ok);
153 ATF_ADD_TEST_CASE(tcs, read_file__missing_file);
155 ATF_ADD_TEST_CASE(tcs, read_stream__empty);
156 ATF_ADD_TEST_CASE(tcs, read_stream__some);