Skip to content

Commit

Permalink
format_spec may be constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellbelove committed Nov 26, 2020
1 parent dceb56d commit fa9d959
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 37 deletions.
61 changes: 34 additions & 27 deletions include/etl/basic_format_spec.h
Expand Up @@ -211,15 +211,22 @@ namespace etl
/// Upper case (for hex) = true
/// Left Justified = false
//***************************************************************************
basic_format_spec()
ETL_CONSTEXPR basic_format_spec()
: base_(10U)
, width_(0U)
, precision_(0U)
, upper_case_(false)
, left_justified_(false)
, boolalpha_(false)
, show_base_(false)
, fill_(typename TString::value_type(' '))
{
clear();
}

//***************************************************************************
/// Clears the format spec back to default.
//***************************************************************************
void clear()
ETL_CONSTEXPR void clear()
{
base_ = 10U;
width_ = 0U;
Expand All @@ -235,7 +242,7 @@ namespace etl
/// Sets the base.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& base(uint32_t b)
ETL_CONSTEXPR basic_format_spec& base(uint32_t b)
{
base_ = static_cast<uint_least8_t>(b);
return *this;
Expand All @@ -245,7 +252,7 @@ namespace etl
/// Sets the base to binary.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& binary()
ETL_CONSTEXPR basic_format_spec& binary()
{
base(2);
return *this;
Expand All @@ -255,7 +262,7 @@ namespace etl
/// Sets the base to octal.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& octal()
ETL_CONSTEXPR basic_format_spec& octal()
{
base(8);
return *this;
Expand All @@ -265,7 +272,7 @@ namespace etl
/// Sets the base to decimal.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& decimal()
ETL_CONSTEXPR basic_format_spec& decimal()
{
base(10);
return *this;
Expand All @@ -275,7 +282,7 @@ namespace etl
/// Sets the base to hex.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& hex()
ETL_CONSTEXPR basic_format_spec& hex()
{
base(16);
return *this;
Expand All @@ -284,7 +291,7 @@ namespace etl
//***************************************************************************
/// Gets the base.
//***************************************************************************
uint32_t get_base() const
ETL_CONSTEXPR uint32_t get_base() const
{
return base_;
}
Expand All @@ -293,7 +300,7 @@ namespace etl
/// Sets the show base flag.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& show_base(bool b)
ETL_CONSTEXPR basic_format_spec& show_base(bool b)
{
show_base_ = b;
return *this;
Expand All @@ -302,7 +309,7 @@ namespace etl
//***************************************************************************
/// Gets the show base flag.
//***************************************************************************
bool is_show_base() const
ETL_CONSTEXPR bool is_show_base() const
{
return show_base_;
}
Expand All @@ -311,7 +318,7 @@ namespace etl
/// Sets the width.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& width(uint32_t w)
ETL_CONSTEXPR basic_format_spec& width(uint32_t w)
{
width_ = static_cast<uint_least8_t>(w);
return *this;
Expand All @@ -320,7 +327,7 @@ namespace etl
//***************************************************************************
/// Gets the width.
//***************************************************************************
uint32_t get_width() const
ETL_CONSTEXPR uint32_t get_width() const
{
return width_;
}
Expand All @@ -329,7 +336,7 @@ namespace etl
/// Sets the precision.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& precision(uint32_t p)
ETL_CONSTEXPR basic_format_spec& precision(uint32_t p)
{
precision_ = static_cast<uint_least8_t>(p);
return *this;
Expand All @@ -338,7 +345,7 @@ namespace etl
//***************************************************************************
/// Gets the precision.
//***************************************************************************
uint32_t get_precision() const
ETL_CONSTEXPR uint32_t get_precision() const
{
return precision_;
}
Expand All @@ -347,7 +354,7 @@ namespace etl
/// Sets the upper case flag.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& upper_case(bool u)
ETL_CONSTEXPR basic_format_spec& upper_case(bool u)
{
upper_case_ = u;
return *this;
Expand All @@ -356,7 +363,7 @@ namespace etl
//***************************************************************************
/// Gets the upper case flag.
//***************************************************************************
bool is_upper_case() const
ETL_CONSTEXPR bool is_upper_case() const
{
return upper_case_;
}
Expand All @@ -365,7 +372,7 @@ namespace etl
/// Sets the fill character.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& fill(typename TString::value_type c)
ETL_CONSTEXPR basic_format_spec& fill(typename TString::value_type c)
{
fill_ = c;
return *this;
Expand All @@ -374,7 +381,7 @@ namespace etl
//***************************************************************************
/// Gets the fill character.
//***************************************************************************
typename TString::value_type get_fill() const
ETL_CONSTEXPR typename TString::value_type get_fill() const
{
return fill_;
}
Expand All @@ -383,7 +390,7 @@ namespace etl
/// Sets the left justify flag.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& left()
ETL_CONSTEXPR basic_format_spec& left()
{
left_justified_ = true;
return *this;
Expand All @@ -392,7 +399,7 @@ namespace etl
//***************************************************************************
/// Gets the left justify flag.
//***************************************************************************
bool is_left() const
ETL_CONSTEXPR bool is_left() const
{
return left_justified_;
}
Expand All @@ -401,7 +408,7 @@ namespace etl
/// Sets the right justify flag.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& right()
ETL_CONSTEXPR basic_format_spec& right()
{
left_justified_ = false;
return *this;
Expand All @@ -410,7 +417,7 @@ namespace etl
//***************************************************************************
/// Gets the right justify flag.
//***************************************************************************
bool is_right() const
ETL_CONSTEXPR bool is_right() const
{
return !left_justified_;
}
Expand All @@ -419,7 +426,7 @@ namespace etl
/// Sets the bool alpha flag.
/// \return A reference to the basic_format_spec.
//***************************************************************************
basic_format_spec& boolalpha(bool b)
ETL_CONSTEXPR basic_format_spec& boolalpha(bool b)
{
boolalpha_ = b;
return *this;
Expand All @@ -428,15 +435,15 @@ namespace etl
//***************************************************************************
/// Gets the boolalpha flag.
//***************************************************************************
bool is_boolalpha() const
ETL_CONSTEXPR bool is_boolalpha() const
{
return boolalpha_;
}

//***************************************************************************
/// Equality operator.
//***************************************************************************
friend bool operator ==(const basic_format_spec& lhs, const basic_format_spec& rhs)
ETL_CONSTEXPR friend bool operator ==(const basic_format_spec& lhs, const basic_format_spec& rhs)
{
return (lhs.base_ == rhs.base_) &&
(lhs.width_ == rhs.width_) &&
Expand All @@ -451,7 +458,7 @@ namespace etl
//***************************************************************************
/// Inequality operator.
//***************************************************************************
friend bool operator !=(const basic_format_spec& lhs, const basic_format_spec& rhs)
ETL_CONSTEXPR friend bool operator !=(const basic_format_spec& lhs, const basic_format_spec& rhs)
{
return !(lhs == rhs);
}
Expand Down
20 changes: 10 additions & 10 deletions include/etl/math_constants.h
Expand Up @@ -35,16 +35,16 @@ namespace etl
{
namespace math
{
const double pi = 3.14159265358979;
const double pi_reciprocal = 0.31830988618379;
const double pi_squared = 9.86960440108936;
const double e = 2.71828182845905;
const double e_reciprocal = 0.36787944117144;
const double e_squared = 7.38905609893065;
const double root2 = 1.41421356237310;
const double root2_reciprocal = 0.70710678118655;
const double euler = 0.57721566490153;
const double golden_ratio = 1.61803398874989;
ETL_CONSTANT double pi = 3.14159265358979;
ETL_CONSTANT double pi_reciprocal = 0.31830988618379;
ETL_CONSTANT double pi_squared = 9.86960440108936;
ETL_CONSTANT double e = 2.71828182845905;
ETL_CONSTANT double e_reciprocal = 0.36787944117144;
ETL_CONSTANT double e_squared = 7.38905609893065;
ETL_CONSTANT double root2 = 1.41421356237310;
ETL_CONSTANT double root2_reciprocal = 0.70710678118655;
ETL_CONSTANT double euler = 0.57721566490153;
ETL_CONSTANT double golden_ratio = 1.61803398874989;
}
}

Expand Down
105 changes: 105 additions & 0 deletions test/test_format_spec.cpp
@@ -0,0 +1,105 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2019 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#include "UnitTest++/UnitTest++.h"

#include <ostream>
#include <sstream>
#include <iomanip>

#include "etl/format_spec.h"
#include "etl/wformat_spec.h"
#include "etl/u16format_spec.h"
#include "etl/u32format_spec.h"

namespace
{
SUITE(test_format_spec)
{
//*************************************************************************
TEST(test_default_format)
{
etl::format_spec format;

CHECK_EQUAL(10, format.get_base());
CHECK_EQUAL(' ', format.get_fill());
CHECK_EQUAL(0, format.get_precision());
CHECK_EQUAL(0, format.get_width());
CHECK_EQUAL(false, format.is_boolalpha());
CHECK_EQUAL(false, format.is_left());
CHECK_EQUAL(true, format.is_right());
CHECK_EQUAL(false, format.is_show_base());
CHECK_EQUAL(false, format.is_upper_case());
}

//*************************************************************************
TEST(test_format)
{
etl::format_spec format;

format.base(16).boolalpha(true).fill('?').left().precision(6).show_base(true).upper_case(true).width(10);

CHECK_EQUAL(16, format.get_base());
CHECK_EQUAL('?', format.get_fill());
CHECK_EQUAL(6, format.get_precision());
CHECK_EQUAL(10, format.get_width());
CHECK_EQUAL(true, format.is_boolalpha());
CHECK_EQUAL(true, format.is_left());
CHECK_EQUAL(false, format.is_right());
CHECK_EQUAL(true, format.is_show_base());
CHECK_EQUAL(true, format.is_upper_case());
}

//*************************************************************************
TEST(test_format_constexpr)
{
constexpr etl::format_spec format = etl::format_spec().base(16).boolalpha(true).fill('?').left().precision(6).show_base(true).upper_case(true).width(10);

constexpr int base = format.get_base();
constexpr char fill = format.get_fill();
constexpr int precision = format.get_precision();
constexpr int width = format.get_width();
constexpr bool boolalpha = format.is_boolalpha();
constexpr bool left = format.is_left();
constexpr bool right = format.is_right();
constexpr bool show_base = format.is_show_base();
constexpr bool upper_case = format.is_upper_case();

CHECK_EQUAL(16, base);
CHECK_EQUAL('?', fill);
CHECK_EQUAL(6, precision);
CHECK_EQUAL(10, width);
CHECK_EQUAL(true, boolalpha);
CHECK_EQUAL(true, left);
CHECK_EQUAL(false, right);
CHECK_EQUAL(true, show_base);
CHECK_EQUAL(true, upper_case);
}
};
}

1 change: 1 addition & 0 deletions test/vs2019/etl.vcxproj
Expand Up @@ -1515,6 +1515,7 @@
<ClCompile Include="..\test_delegate.cpp" />
<ClCompile Include="..\test_delegate_service.cpp" />
<ClCompile Include="..\test_flags.cpp" />
<ClCompile Include="..\test_format_spec.cpp" />
<ClCompile Include="..\test_forward_list_shared_pool.cpp" />
<ClCompile Include="..\test_bit_stream.cpp" />
<ClCompile Include="..\test_indirect_vector.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions test/vs2019/etl.vcxproj.filters
Expand Up @@ -1424,6 +1424,9 @@
<ClCompile Include="..\test_atomic_clang_sync.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_format_spec.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\library.properties">
Expand Down

0 comments on commit fa9d959

Please sign in to comment.