From 34eb75ce9884b8ebd6520504c1dfc9b623a0b227 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 4 May 2024 18:02:47 +0300 Subject: [PATCH] Add test_to_chars.cpp --- test/CMakeLists.txt | 1 + test/Jamfile.v2 | 1 + test/test_to_chars.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 test/test_to_chars.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f1d1bc1c..73722553 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,6 +23,7 @@ boost_test(TYPE run SOURCES test_comparison.cpp COMPILE_DEFINITIONS BOOST_UUID_N boost_test(TYPE run SOURCES test_include1.cpp test_include2.cpp) boost_test(TYPE run SOURCES test_io.cpp LINK_LIBRARIES Boost::lexical_cast Boost::predef) +boost_test(TYPE run SOURCES test_to_chars.cpp) boost_test(TYPE run SOURCES test_uuid_clock.cpp) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index a7ec2211..81e2e34b 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -67,6 +67,7 @@ run test_comparison.cpp : : : BOOST_UUID_NO_SIMD BOOST_UUID_REPO # test uuid_io.hpp run test_io.cpp ; +run test_to_chars.cpp ; # test uuid_clock diff --git a/test/test_to_chars.cpp b/test/test_to_chars.cpp new file mode 100644 index 00000000..c03881c6 --- /dev/null +++ b/test/test_to_chars.cpp @@ -0,0 +1,63 @@ +// Copyright 2009 Andy Tompkins 2009 +// Copyright 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +using namespace boost::uuids; + +template void test( uuid const& u, Ch const* expected ) +{ + int const N = 56; + + for( int n = 0; n < 36; ++n ) + { + Ch buffer[ N ]; + + std::fill( buffer + 0, buffer + N, '@' ); + + // insufficient buffer size; must fail + BOOST_TEST_NOT( to_chars( u, buffer + 0, buffer + n ) ); + + // make sure nothing is written beyond the end + BOOST_TEST( std::basic_string( buffer + n, buffer + N ) == std::basic_string( N - n, '@' ) ); + } + + for( int n = 36; n < 48; ++n ) + { + Ch buffer[ N ]; + + std::fill( buffer + 0, buffer + N, '@' ); + + // sufficient buffer size; must succeed + BOOST_TEST( to_chars( u, buffer + 0, buffer + n ) ); + + // check that the correct string is produced + BOOST_TEST( std::basic_string( buffer + 0, buffer + 36 ) == std::basic_string( expected ) ); + + // make sure nothing is written beyond the end + BOOST_TEST( std::basic_string( buffer + n, buffer + N ) == std::basic_string( N - n, '@' ) ); + } +} + +int main() +{ + uuid const u1 = {{ 0 }}; + uuid const u2 = {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }}; + uuid const u3 = {{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }}; + + test( u1, "00000000-0000-0000-0000-000000000000" ); + test( u2, "00010203-0405-0607-0809-0a0b0c0d0e0f" ); + test( u3, "12345678-90ab-cdef-1234-567890abcdef" ); + + test( u1, L"00000000-0000-0000-0000-000000000000" ); + test( u2, L"00010203-0405-0607-0809-0a0b0c0d0e0f" ); + test( u3, L"12345678-90ab-cdef-1234-567890abcdef" ); + + return boost::report_errors(); +}