| 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 snprintf_test.cpp |
| 9 | * \author Andrey Semashev |
| 10 | * \date 06.12.2022 |
| 11 | * |
| 12 | * This file contains tests for \c boost::core::snprintf. |
| 13 | */ |
| 14 | |
| 15 | #include <boost/core/snprintf.hpp> |
| 16 | #include <cstddef> |
| 17 | #include <cstring> |
| 18 | #include <boost/core/lightweight_test.hpp> |
| 19 | |
| 20 | void test_snprintf() |
| 21 | { |
| 22 | char buf[11]; |
| 23 | std::memset(s: buf, c: 0xFF, n: sizeof(buf)); |
| 24 | |
| 25 | std::size_t buf_size = sizeof(buf) - 1u; |
| 26 | |
| 27 | int res = boost::core::snprintf(s: buf, maxlen: buf_size, format: "%s" , "0123" ); |
| 28 | BOOST_TEST_EQ(res, 4); |
| 29 | int cmp_res = std::memcmp(s1: buf, s2: "0123" , n: sizeof("0123" )); |
| 30 | BOOST_TEST_EQ(cmp_res, 0); |
| 31 | |
| 32 | std::memset(s: buf, c: 0xFF, n: sizeof(buf)); |
| 33 | |
| 34 | // Suppress compiler checks for buffer overflow |
| 35 | const char* volatile str = "0123456789" ; |
| 36 | |
| 37 | res = boost::core::snprintf(s: buf, maxlen: buf_size, format: "%s" , str); |
| 38 | BOOST_TEST_GE(res, 10); |
| 39 | cmp_res = std::memcmp(s1: buf, s2: "012345678" , n: sizeof("012345678" )); |
| 40 | BOOST_TEST_EQ(cmp_res, 0); |
| 41 | BOOST_TEST_EQ(buf[10], static_cast< char >(~static_cast< char >(0))); |
| 42 | |
| 43 | std::memset(s: buf, c: 0xFF, n: sizeof(buf)); |
| 44 | |
| 45 | res = boost::core::snprintf(s: buf, maxlen: 0, format: "%s" , str); |
| 46 | BOOST_TEST_GE(res, 0); |
| 47 | BOOST_TEST_EQ(buf[0], static_cast< char >(~static_cast< char >(0))); |
| 48 | } |
| 49 | |
| 50 | void test_swprintf() |
| 51 | { |
| 52 | wchar_t buf[11]; |
| 53 | std::memset(s: buf, c: 0xFF, n: sizeof(buf)); |
| 54 | |
| 55 | std::size_t buf_size = sizeof(buf) / sizeof(*buf) - 1u; |
| 56 | |
| 57 | int res = boost::core::swprintf(s: buf, n: buf_size, format: L"%ls" , L"0123" ); |
| 58 | BOOST_TEST_EQ(res, 4); |
| 59 | int cmp_res = std::memcmp(s1: buf, s2: L"0123" , n: sizeof(L"0123" )); |
| 60 | BOOST_TEST_EQ(cmp_res, 0); |
| 61 | |
| 62 | std::memset(s: buf, c: 0xFF, n: sizeof(buf)); |
| 63 | |
| 64 | // Suppress compiler checks for buffer overflow |
| 65 | const wchar_t* volatile str = L"0123456789" ; |
| 66 | |
| 67 | res = boost::core::swprintf(s: buf, n: buf_size, format: L"%ls" , str); |
| 68 | BOOST_TEST_LT(res, 0); |
| 69 | // swprintf may or may not write to the buffer in case of overflow. |
| 70 | // E.g. glibc 2.35 doesn't and libc on MacOS 11 does. |
| 71 | BOOST_TEST_EQ(buf[10], static_cast< wchar_t >(~static_cast< wchar_t >(0))); |
| 72 | |
| 73 | std::memset(s: buf, c: 0xFF, n: sizeof(buf)); |
| 74 | |
| 75 | res = boost::core::swprintf(s: buf, n: 0, format: L"%ls" , str); |
| 76 | BOOST_TEST_LT(res, 0); |
| 77 | BOOST_TEST_EQ(buf[0], static_cast< wchar_t >(~static_cast< wchar_t >(0))); |
| 78 | } |
| 79 | |
| 80 | int main() |
| 81 | { |
| 82 | test_snprintf(); |
| 83 | test_swprintf(); |
| 84 | |
| 85 | return boost::report_errors(); |
| 86 | } |
| 87 | |