]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/doc/examples/exception_api.run-fail.cpp
1 // (C) Copyright 2015 Boost.Test team.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
9 #define BOOST_TEST_MODULE example
10 #include <boost/test/included/unit_test.hpp>
14 //! Computes the histogram of the words in a text file
15 class FileWordHistogram
18 //!@throw std::exception if the file does not exist
19 FileWordHistogram(std::string filename
) : is_processed(false), fileStream_(filename
) {
20 if(!fileStream_
.is_open()) throw std::runtime_error("Cannot open the file");
23 //! @returns true on success, false otherwise
25 if(!is_processed
) return true;
32 //!@pre process has been called with status success
33 //!@throw std::logic_error if preconditions not met
34 std::map
<std::string
, std::size_t>
37 throw std::runtime_error("\"process\" has not been called or was not successful");
43 std::ifstream fileStream_
;
44 std::map
<std::string
, std::size_t> histogram
;
47 BOOST_AUTO_TEST_CASE( test_throw_behaviour
)
49 // __FILE__ is accessible, no exception expected
50 BOOST_REQUIRE_NO_THROW( FileWordHistogram(__FILE__
) );
52 // ".. __FILE__" does not exist, API says std::exception, and implementation
53 // raises std::runtime_error child of std::exception
54 BOOST_CHECK_THROW( FileWordHistogram(".." __FILE__
), std::exception
);
57 FileWordHistogram
instance(__FILE__
);
59 // api says "std::logic_error", implementation is wrong.
60 // std::runtime_error not a child of std::logic_error, not intercepted
62 BOOST_CHECK_THROW(instance
.result(), std::logic_error
);