-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix broken buffer estimator in to_chars_static
- assumed decimal - sometimes got it wrong - potentially got it badly wrong - now uses lookup table to ensure the right number of characters to represent in integer of a given number of binary digits - table generator: https://godbolt.org/z/sYc7v185T - correctness: ~95% - probably of causing buffer overrun: < 1%
- Loading branch information
John McFarlane
committed
Jan 29, 2022
1 parent
fba5a8e
commit 5f75b2a
Showing
6 changed files
with
118 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
// Copyright John McFarlane 2022. | ||
// 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/_impl/charconv/to_chars.h> | ||
|
||
#include <cnl/elastic_scaled_integer.h> | ||
|
||
#include <cnl/_impl/type_traits/identical.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace { | ||
using cnl::_impl::identical; | ||
|
||
TEST(to_chars_static, elastic_scaled_123_456) // NOLINT | ||
{ | ||
using namespace cnl::literals; | ||
constexpr auto expected{cnl::to_chars_static_result<10>{{'1', '2', '3', '.', '4', '5', '6'}, 7}}; | ||
constexpr auto actual{cnl::to_chars_static(123.456_cnl)}; | ||
#if !defined(_MSC_VER) | ||
static_assert(identical(expected, actual)); | ||
#endif | ||
ASSERT_EQ(expected, actual); | ||
} | ||
|
||
TEST(to_chars_static, elastic_scaled_12345_6) // NOLINT | ||
{ | ||
using namespace cnl::literals; | ||
constexpr auto expected{cnl::to_chars_static_result<8>{{'1', '2', '3', '4', '5', '.', '6'}, 7}}; | ||
constexpr auto actual{cnl::to_chars_static(12345.6_cnl)}; | ||
#if !defined(_MSC_VER) | ||
static_assert(identical(expected, actual)); | ||
#endif | ||
ASSERT_EQ(expected, actual); | ||
} | ||
|
||
TEST(to_chars_static, elastic_scaled_123456_) // NOLINT | ||
{ | ||
using namespace cnl::literals; | ||
constexpr auto expected{cnl::to_chars_static_result<7>{{'1', '2', '3', '4', '5', '6'}, 6}}; | ||
constexpr auto actual{cnl::to_chars_static(123456._cnl)}; | ||
#if !defined(_MSC_VER) | ||
static_assert(identical(expected, actual)); | ||
#endif | ||
ASSERT_EQ(expected, actual); | ||
} | ||
|
||
TEST(to_chars_static, elastic_scaled_n123456_) | ||
{ | ||
using namespace cnl::literals; | ||
constexpr auto expected{cnl::to_chars_static_result<7>{{'-', '1', '2', '3', '4', '5', '6'}, 7}}; | ||
constexpr auto actual{cnl::to_chars_static(-123456._cnl)}; | ||
#if !defined(_MSC_VER) | ||
static_assert(identical(expected, actual)); | ||
#endif | ||
ASSERT_EQ(expected, actual); | ||
} | ||
} |