Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round-to-nearest for to_chars #342

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ set(test_sources
fixed_point/elastic/overflow/saturated.cpp
static_integer.cpp
static_number.cpp
static_number/to_chars.cpp
)

# source files containing demos and examples of CNL;
Expand Down
31 changes: 31 additions & 0 deletions src/test/static_number/to_chars.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

// Copyright John McFarlane 2015 - 2018.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file ../LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <cnl/static_number.h>

#include <gtest/gtest.h>

#include <array>
#include <string>

namespace {
TEST(static_number, to_chars_narrower)
{
auto const num_chars = 4;
auto buffer = std::array<char, num_chars+1>{};
auto chars_first = buffer.data();
auto chars_last = chars_first+num_chars;
auto result = cnl::to_chars(
chars_first,
chars_last,
cnl::static_number<16, -10, cnl::native_rounding_tag>{20.45697});
ASSERT_EQ(std::errc{}, result.ec);
ASSERT_EQ(chars_last, result.ptr);
*chars_last = '\0';

ASSERT_EQ(std::string{"20.5"}, chars_first);
}
}