diff --git a/include/etl/basic_format_spec.h b/include/etl/basic_format_spec.h index d5a885ff..80e9870e 100644 --- a/include/etl/basic_format_spec.h +++ b/include/etl/basic_format_spec.h @@ -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; @@ -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(b); return *this; @@ -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; @@ -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; @@ -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; @@ -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; @@ -284,7 +291,7 @@ namespace etl //*************************************************************************** /// Gets the base. //*************************************************************************** - uint32_t get_base() const + ETL_CONSTEXPR uint32_t get_base() const { return base_; } @@ -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; @@ -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_; } @@ -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(w); return *this; @@ -320,7 +327,7 @@ namespace etl //*************************************************************************** /// Gets the width. //*************************************************************************** - uint32_t get_width() const + ETL_CONSTEXPR uint32_t get_width() const { return width_; } @@ -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(p); return *this; @@ -338,7 +345,7 @@ namespace etl //*************************************************************************** /// Gets the precision. //*************************************************************************** - uint32_t get_precision() const + ETL_CONSTEXPR uint32_t get_precision() const { return precision_; } @@ -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; @@ -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_; } @@ -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; @@ -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_; } @@ -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; @@ -392,7 +399,7 @@ namespace etl //*************************************************************************** /// Gets the left justify flag. //*************************************************************************** - bool is_left() const + ETL_CONSTEXPR bool is_left() const { return left_justified_; } @@ -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; @@ -410,7 +417,7 @@ namespace etl //*************************************************************************** /// Gets the right justify flag. //*************************************************************************** - bool is_right() const + ETL_CONSTEXPR bool is_right() const { return !left_justified_; } @@ -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; @@ -428,7 +435,7 @@ namespace etl //*************************************************************************** /// Gets the boolalpha flag. //*************************************************************************** - bool is_boolalpha() const + ETL_CONSTEXPR bool is_boolalpha() const { return boolalpha_; } @@ -436,7 +443,7 @@ namespace etl //*************************************************************************** /// 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_) && @@ -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); } diff --git a/include/etl/math_constants.h b/include/etl/math_constants.h index bc76cfcd..29595399 100644 --- a/include/etl/math_constants.h +++ b/include/etl/math_constants.h @@ -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; } } diff --git a/test/test_format_spec.cpp b/test/test_format_spec.cpp new file mode 100644 index 00000000..471ab91b --- /dev/null +++ b/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 +#include +#include + +#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); + } + }; +} + diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index 4323d358..2c947ac9 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -1515,6 +1515,7 @@ + diff --git a/test/vs2019/etl.vcxproj.filters b/test/vs2019/etl.vcxproj.filters index 198c3ed2..36af486d 100644 --- a/test/vs2019/etl.vcxproj.filters +++ b/test/vs2019/etl.vcxproj.filters @@ -1424,6 +1424,9 @@ Source Files + + Source Files +