| 1 | /* |
| 2 | * Copyright Andrey Semashev 2022. |
| 3 | * Distributed under the Boost Software License, Version 1.0. |
| 4 | * (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | * http://www.boost.org/LICENSE_1_0.txt) |
| 6 | */ |
| 7 | /*! |
| 8 | * \file fclose_deleter_test.cpp |
| 9 | * \author Andrey Semashev |
| 10 | * \date 21.09.2022 |
| 11 | * |
| 12 | * This file contains tests for \c boost::fclose_deleter. |
| 13 | */ |
| 14 | |
| 15 | #include <boost/core/fclose_deleter.hpp> |
| 16 | #include <cstdio> |
| 17 | #include <cstddef> |
| 18 | #include <boost/config.hpp> |
| 19 | #include <boost/move/unique_ptr.hpp> |
| 20 | #include <boost/smart_ptr/shared_ptr.hpp> |
| 21 | #if !defined(BOOST_NO_CXX11_SMART_PTR) |
| 22 | #include <memory> |
| 23 | #endif |
| 24 | |
| 25 | boost::movelib::unique_ptr< std::FILE, boost::fclose_deleter > make_boost_unique_file(const char* filename) |
| 26 | { |
| 27 | return boost::movelib::unique_ptr< std::FILE, boost::fclose_deleter >(std::fopen(filename: filename, modes: "w" )); |
| 28 | } |
| 29 | |
| 30 | boost::shared_ptr< std::FILE > make_boost_shared_file(const char* filename) |
| 31 | { |
| 32 | return boost::shared_ptr< std::FILE >(std::fopen(filename: filename, modes: "w" ), boost::fclose_deleter()); |
| 33 | } |
| 34 | |
| 35 | #if !defined(BOOST_NO_CXX11_SMART_PTR) |
| 36 | std::unique_ptr< std::FILE, boost::fclose_deleter > make_std_unique_file(const char* filename) |
| 37 | { |
| 38 | return std::unique_ptr< std::FILE, boost::fclose_deleter >(std::fopen(filename: filename, modes: "w" )); |
| 39 | } |
| 40 | |
| 41 | std::shared_ptr< std::FILE > make_std_shared_file(const char* filename) |
| 42 | { |
| 43 | return std::shared_ptr< std::FILE >(std::fopen(filename: filename, modes: "w" ), boost::fclose_deleter()); |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | int main() |
| 48 | { |
| 49 | const char* const filename = "fcd_test.txt" ; |
| 50 | |
| 51 | std::FILE* file = std::fopen(filename: filename, modes: "w" ); |
| 52 | if (file) |
| 53 | { |
| 54 | boost::fclose_deleter()(file); |
| 55 | file = NULL; |
| 56 | } |
| 57 | |
| 58 | make_boost_unique_file(filename); |
| 59 | make_boost_shared_file(filename); |
| 60 | |
| 61 | #if !defined(BOOST_NO_CXX11_SMART_PTR) |
| 62 | make_std_unique_file(filename); |
| 63 | make_std_shared_file(filename); |
| 64 | #endif |
| 65 | |
| 66 | // Test if the deleter can be called on a NULL pointer |
| 67 | boost::shared_ptr< std::FILE >(static_cast< std::FILE* >(NULL), boost::fclose_deleter()); |
| 68 | #if !defined(BOOST_NO_CXX11_SMART_PTR) |
| 69 | std::shared_ptr< std::FILE >(static_cast< std::FILE* >(NULL), boost::fclose_deleter()); |
| 70 | #endif |
| 71 | |
| 72 | std::remove(filename: filename); |
| 73 | } |
| 74 | |