Merge remote-tracking branch 'origin/feature/to_string' into development

# Conflicts:
#	include/etl/version.h
#	support/Release notes.txt
#	test/vs2017/etl.vcxproj.filters
This commit is contained in:
John Wellbelove 2019-04-08 19:37:41 +01:00
parent 75a7ee0b55
commit 7844142c19
20 changed files with 2061 additions and 1 deletions

View File

@ -55,6 +55,8 @@ namespace etl
{
public:
typedef istring base_type;
typedef istring::value_type value_type;
static const size_t MAX_SIZE = MAX_SIZE_;

204
include/etl/format_spec.h Normal file
View File

@ -0,0 +1,204 @@
///\file
/******************************************************************************
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.
******************************************************************************/
#ifndef ETL_FORMAT_SPEC_INCLUDED
#define ETL_FORMAT_SPEC_INCLUDED
///\ingroup string
#include "platform.h"
#include "type_traits.h"
#include "static_assert.h"
namespace etl
{
template <typename TString>
class format_spec
{
public:
//***************************************************************************
/// Default constructor.
/// Sets:-
/// Base = 10
/// Width = 0
/// Upper case (for hex) = true
/// Left Justified = false
//***************************************************************************
format_spec()
: base_(10)
, width_(0)
, upper_case_(true)
, left_justified_(false)
, fill_(typename TString::value_type(' '))
{
}
//***************************************************************************
/// Sets the base.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& base(const uint32_t b)
{
base_ = static_cast<uint_least8_t>(b);
return *this;
}
//***************************************************************************
/// Sets the base to binary.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& binary()
{
base(2);
return *this;
}
//***************************************************************************
/// Sets the base to octal.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& octal()
{
base(8);
return *this;
}
//***************************************************************************
/// Sets the base to decimal.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& decimal()
{
base(10);
return *this;
}
//***************************************************************************
/// Sets the base to hex.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& hex()
{
base(16);
return *this;
}
//***************************************************************************
/// Gets the base.
//***************************************************************************
uint32_t base() const
{
return base_;
}
//***************************************************************************
/// Sets the width.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& width(const uint32_t w)
{
width_ = static_cast<uint_least8_t>(w);
return *this;
}
//***************************************************************************
/// Gets the width.
//***************************************************************************
uint32_t width() const
{
return width_;
}
//***************************************************************************
/// Sets the upper case flag.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& upper_case(const bool u)
{
upper_case_ = u;
return *this;
}
//***************************************************************************
/// Gets the upper case flag.
//***************************************************************************
bool upper_case() const
{
return upper_case_;
}
//***************************************************************************
/// Sets the fill character.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& fill(const typename TString::value_type c)
{
fill_ = c;
return *this;
}
//***************************************************************************
/// Gets the fill character.
//***************************************************************************
typename TString::value_type fill() const
{
return fill_;
}
//***************************************************************************
/// Sets the left justify flag.
/// \return A reference to the format_spec.
//***************************************************************************
format_spec& left_justified(const bool l)
{
left_justified_ = l;
return *this;
}
//***************************************************************************
/// Gets the left justify flag.
//***************************************************************************
bool left_justified() const
{
return left_justified_;
}
private:
uint_least8_t base_;
uint_least8_t width_;
bool upper_case_;
bool left_justified_;
typename TString::value_type fill_;
};
}
#endif

60
include/etl/negative.h Normal file
View File

@ -0,0 +1,60 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2018 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.
******************************************************************************/
#ifndef ETL_NEGATIVE_INCLUDED
#define ETL_NEGATIVE_INCLUDED
#include "type_traits.h"
namespace etl
{
//***************************************************************************
// For signed types.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_signed<T>::value, bool>::type
is_negative(const T value)
{
return (value < T(0));
}
//***************************************************************************
// For unsigned types.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_unsigned<T>::value, T>::type
is_negative(const T)
{
return false;
}
}
#endif

View File

@ -0,0 +1,120 @@
///\file
/******************************************************************************
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.
******************************************************************************/
#ifndef ETL_TO_STRING_HELPER_INCLUDED
#define ETL_TO_STRING_HELPER_INCLUDED
///\ingroup private
#include "../platform.h"
#include "../absolute.h"
#include "../negative.h"
#include "../format_spec.h"
#include "../type_traits.h"
#include "../stl/algorithm.h"
#include "../stl/iterator.h"
namespace etl
{
//***************************************************************************
/// Helper function for integrals.
//***************************************************************************
template <typename T, typename TIString>
typename etl::enable_if<etl::is_integral<T>::value, TIString&>::type
to_string_helper(T value,
TIString& str,
const etl::format_spec<TIString>& format,
const bool append)
{
typedef typename TIString::value_type type;
typedef typename TIString::iterator iterator;
const bool negative = etl::is_negative(value);
if (!append)
{
str.clear();
}
iterator start = str.end();
if (value == 0)
{
str.push_back(type('0'));
}
else
{
// Extract the digits, in reverse order.
while (value != 0)
{
T remainder = etl::absolute(value % T(format.base()));
str.push_back((remainder > 9) ? (format.upper_case() ? type('A' + (remainder - 10)) : type('a' + (remainder - 10))) : type('0' + remainder));
value = value / T(format.base());
}
// If number is negative, append '-'
if ((format.base() == 10) && negative)
{
str.push_back(type('-'));
}
// Reverse the string we appended.
std::reverse(start, str.end());
}
// Do we have a width specification?
if (format.width() != 0)
{
uint32_t length = static_cast<uint32_t>(std::distance(start, str.end()));
// Is the length of the string less than the width?
if (length < format.width())
{
uint32_t fill_length = format.width() - length;
if (format.left_justified())
{
// Insert fill characters on the right.
str.insert(str.end(), fill_length, format.fill());
}
else
{
// Insert fill characters on the left.
str.insert(start, fill_length, format.fill());
}
}
}
return str;
}
}
#endif

149
include/etl/to_string.h Normal file
View File

@ -0,0 +1,149 @@
///\file
/******************************************************************************
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.
******************************************************************************/
#ifndef ETL_TO_U32STRING_INCLUDED
#define ETL_TO_U32STRING_INCLUDED
///\ingroup string
#include "platform.h"
#include "type_traits.h"
#include "cstring.h"
#include "format_spec.h"
#include "private/to_string_helper.h"
namespace etl
{
//***************************************************************************
/// For signed integrals less than 64 bits. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
!etl::is_same<T, int64_t>::value, const etl::istring&>::type
to_string(const T value, etl::istring& str, const bool append = false)
{
etl::format_spec<etl::istring> format;
return to_string_helper(int32_t(value), str, format, append);
}
//***************************************************************************
/// For signed integrals less than 64 bits. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
!etl::is_same<T, int64_t>::value, const etl::istring&>::type
to_string(const T value, etl::istring& str, const etl::format_spec<etl::istring>& format, const bool append = false)
{
return to_string_helper(int32_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned integrals less then 64 bits. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
!etl::is_same<T, uint64_t>::value, const etl::istring&>::type
to_string(const T value, etl::istring& str, const bool append = false)
{
etl::format_spec<etl::istring> format;
return to_string_helper(uint32_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned integrals less than 64 bits. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
!etl::is_same<T, uint64_t>::value, const etl::istring&>::type
to_string(const T value, etl::istring& str, const etl::format_spec<etl::istring>& format, const bool append = false)
{
return to_string_helper(uint32_t(value), str, format, append);
}
//***************************************************************************
/// For signed 64 bit integrals. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
etl::is_same<T, int64_t>::value, const etl::istring&>::type
to_string(const T value, etl::istring& str, const bool append = false)
{
etl::format_spec<etl::istring> format;
return to_string_helper(int64_t(value), str, format, append);
}
//***************************************************************************
/// For signed 64 bit integrals. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
etl::is_same<T, int64_t>::value, const etl::istring&>::type
to_string(const T value, etl::istring& str, const etl::format_spec<etl::istring>& format, const bool append = false)
{
return to_string_helper(int64_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned 64 bit integrals. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
etl::is_same<T, uint64_t>::value, const etl::istring&>::type
to_string(const T value, etl::istring& str, const bool append = false)
{
etl::format_spec<etl::istring> format;
return to_string_helper(uint64_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned 64 bit integrals. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
etl::is_same<T, uint64_t>::value, const etl::istring&>::type
to_string(const T value, etl::istring& str, const etl::format_spec<etl::istring>& format, const bool append = false)
{
return to_string_helper(uint64_t(value), str, format, append);
}
}
#endif

149
include/etl/to_u16string.h Normal file
View File

@ -0,0 +1,149 @@
///\file
/******************************************************************************
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.
******************************************************************************/
#ifndef ETL_TO_U16STRING_INCLUDED
#define ETL_TO_U16STRING_INCLUDED
///\ingroup string
#include "platform.h"
#include "type_traits.h"
#include "u16string.h"
#include "format_spec.h"
#include "private/to_string_helper.h"
namespace etl
{
//***************************************************************************
/// For signed integrals less than 64 bits. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
!etl::is_same<T, int64_t>::value, etl::iu16string&>::type
to_u16string(const T value, etl::iu16string& str, const bool append = false)
{
etl::format_spec<etl::iu16string> format;
return to_string_helper(int32_t(value), str, format, append);
}
//***************************************************************************
/// For signed integrals less than 64 bits. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
!etl::is_same<T, int64_t>::value, etl::iu16string&>::type
to_u16string(const T value, etl::iu16string& str, const etl::format_spec<etl::iu16string>& format, const bool append = false)
{
return to_string_helper(int32_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned integrals less then 64 bits. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
!etl::is_same<T, uint64_t>::value, etl::iu16string&>::type
to_u16string(const T value, etl::iu16string& str, const bool append = false)
{
etl::format_spec<etl::iu16string> format;
return to_string_helper(uint32_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned integrals less than 64 bits. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
!etl::is_same<T, uint64_t>::value, etl::iu16string&>::type
to_u16string(const T value, etl::iu16string& str, const etl::format_spec<etl::iu16string>& format, const bool append = false)
{
return to_string_helper(uint32_t(value), str, format, append);
}
//***************************************************************************
/// For signed 64 bit integrals. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
etl::is_same<T, int64_t>::value, etl::iu16string&>::type
to_u16string(const T value, etl::iu16string& str, const bool append = false)
{
etl::format_spec<etl::iu16string> format;
return to_string_helper(int64_t(value), str, format, append);
}
//***************************************************************************
/// For signed 64 bit integrals. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
etl::is_same<T, int64_t>::value, etl::iu16string&>::type
to_u16string(const T value, etl::iu16string& str, const etl::format_spec<etl::iu16string>& format, const bool append = false)
{
return to_string_helper(int64_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned 64 bit integrals. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
etl::is_same<T, uint64_t>::value, etl::iu16string&>::type
to_u16string(const T value, etl::iu16string& str, const bool append = false)
{
etl::format_spec<etl::iu16string> format;
return to_string_helper(uint64_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned 64 bit integrals. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
etl::is_same<T, uint64_t>::value, etl::iu16string&>::type
to_u16string(const T value, etl::iu16string& str, const etl::format_spec<etl::iu16string>& format, const bool append = false)
{
return to_string_helper(uint64_t(value), str, format, append);
}
}
#endif

149
include/etl/to_u32string.h Normal file
View File

@ -0,0 +1,149 @@
///\file
/******************************************************************************
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.
******************************************************************************/
#ifndef ETL_TO_U32STRING_INCLUDED
#define ETL_TO_U32STRING_INCLUDED
///\ingroup string
#include "platform.h"
#include "type_traits.h"
#include "u32string.h"
#include "format_spec.h"
#include "private/to_string_helper.h"
namespace etl
{
//***************************************************************************
/// For signed integrals less than 64 bits. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
!etl::is_same<T, int64_t>::value, etl::iu32string&>::type
to_u32string(const T value, etl::iu32string& str, const bool append = false)
{
etl::format_spec<etl::iu32string> format;
return to_string_helper(int32_t(value), str, format, append);
}
//***************************************************************************
/// For signed integrals less than 64 bits. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
!etl::is_same<T, int64_t>::value, etl::iu32string&>::type
to_u32string(const T value, etl::iu32string& str, const etl::format_spec<etl::iu32string>& format, const bool append = false)
{
return to_string_helper(int32_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned integrals less then 64 bits. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
!etl::is_same<T, uint64_t>::value, etl::iu32string&>::type
to_u32string(const T value, etl::iu32string& str, const bool append = false)
{
etl::format_spec<etl::iu32string> format;
return to_string_helper(uint32_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned integrals less than 64 bits. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
!etl::is_same<T, uint64_t>::value, etl::iu32string&>::type
to_u32string(const T value, etl::iu32string& str, const etl::format_spec<etl::iu32string>& format, const bool append = false)
{
return to_string_helper(uint32_t(value), str, format, append);
}
//***************************************************************************
/// For signed 64 bit integrals. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
etl::is_same<T, int64_t>::value, etl::iu32string&>::type
to_u32string(const T value, etl::iu32string& str, const bool append = false)
{
etl::format_spec<etl::iu32string> format;
return to_string_helper(int64_t(value), str, format, append);
}
//***************************************************************************
/// For signed 64 bit integrals. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
etl::is_same<T, int64_t>::value, etl::iu32string&>::type
to_u32string(const T value, etl::iu32string& str, const etl::format_spec<etl::iu32string>& format, const bool append = false)
{
return to_string_helper(int64_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned 64 bit integrals. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
etl::is_same<T, uint64_t>::value, etl::iu32string&>::type
to_u32string(const T value, etl::iu32string& str, const bool append = false)
{
etl::format_spec<etl::iu32string> format;
return to_string_helper(uint64_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned 64 bit integrals. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
etl::is_same<T, uint64_t>::value, etl::iu32string&>::type
to_u32string(const T value, etl::iu32string& str, const etl::format_spec<etl::iu32string>& format, const bool append = false)
{
return to_string_helper(uint64_t(value), str, format, append);
}
}
#endif

149
include/etl/to_wstring.h Normal file
View File

@ -0,0 +1,149 @@
///\file
/******************************************************************************
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.
******************************************************************************/
#ifndef ETL_TO_WSTRING_INCLUDED
#define ETL_TO_WSTRING_INCLUDED
///\ingroup string
#include "platform.h"
#include "type_traits.h"
#include "wstring.h"
#include "format_spec.h"
#include "private/to_string_helper.h"
namespace etl
{
//***************************************************************************
/// For signed integrals less than 64 bits. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
!etl::is_same<T, int64_t>::value, etl::iwstring&>::type
to_wstring(const T value, etl::iwstring& str, const bool append = false)
{
etl::format_spec<etl::iwstring> format;
return to_string_helper(int32_t(value), str, format, append);
}
//***************************************************************************
/// For signed integrals less than 64 bits. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
!etl::is_same<T, int64_t>::value, etl::iwstring&>::type
to_wstring(const T value, etl::iwstring& str, const etl::format_spec<etl::iwstring>& format, const bool append = false)
{
return to_string_helper(int32_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned integrals less then 64 bits. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
!etl::is_same<T, uint64_t>::value, etl::iwstring&>::type
to_wstring(const T value, etl::iwstring& str, const bool append = false)
{
etl::format_spec<etl::iwstring> format;
return to_string_helper(uint32_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned integrals less than 64 bits. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
!etl::is_same<T, uint64_t>::value, etl::iwstring&>::type
to_wstring(const T value, etl::iwstring& str, const etl::format_spec<etl::iwstring>& format, const bool append = false)
{
return to_string_helper(uint32_t(value), str, format, append);
}
//***************************************************************************
/// For signed 64 bit integrals. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
etl::is_same<T, int64_t>::value, etl::iwstring&>::type
to_wstring(const T value, etl::iwstring& str, const bool append = false)
{
etl::format_spec<etl::iwstring> format;
return to_string_helper(int64_t(value), str, format, append);
}
//***************************************************************************
/// For signed 64 bit integrals. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_signed<T>::value &&
etl::is_same<T, int64_t>::value, etl::iwstring&>::type
to_wstring(const T value, etl::iwstring& str, const etl::format_spec<etl::iwstring>& format, const bool append = false)
{
return to_string_helper(int64_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned 64 bit integrals. Default format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
etl::is_same<T, uint64_t>::value, etl::iwstring&>::type
to_wstring(const T value, etl::iwstring& str, const bool append = false)
{
etl::format_spec<etl::iwstring> format;
return to_string_helper(uint64_t(value), str, format, append);
}
//***************************************************************************
/// For unsigned 64 bit integrals. Supplied format spec.
//***************************************************************************
template <typename T>
typename etl::enable_if<etl::is_integral<T>::value &&
etl::is_unsigned<T>::value &&
etl::is_same<T, uint64_t>::value, etl::iwstring&>::type
to_wstring(const T value, etl::iwstring& str, const etl::format_spec<etl::iwstring>& format, const bool append = false)
{
return to_string_helper(uint64_t(value), str, format, append);
}
}
#endif

View File

@ -55,6 +55,8 @@ namespace etl
{
public:
typedef iu16string base_type;
typedef iu16string::value_type value_type;
static const size_t MAX_SIZE = MAX_SIZE_;

View File

@ -55,6 +55,8 @@ namespace etl
{
public:
typedef iu32string base_type;
typedef iu32string::value_type value_type;
static const size_t MAX_SIZE = MAX_SIZE_;

View File

@ -38,7 +38,7 @@ SOFTWARE.
///\ingroup utilities
#define ETL_VERSION_MAJOR 14
#define ETL_VERSION_MINOR 17
#define ETL_VERSION_MINOR 18
#define ETL_VERSION_PATCH 0
#define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH)

View File

@ -55,6 +55,8 @@ namespace etl
{
public:
typedef iwstring base_type;
typedef iwstring::value_type value_type;
static const size_t MAX_SIZE = MAX_SIZE_;

View File

@ -1,3 +1,8 @@
===============================================================================
14.18.0
Added etl::to_string, etl::to_wstring, etl::to_u16string and etl::to_u32string
plus formating support for integrals.
===============================================================================
14.17.0
Added C++11 variadic templates for etl::observer.

View File

@ -197,6 +197,7 @@
<Unit filename="../../include/etl/flat_multiset.h" />
<Unit filename="../../include/etl/flat_set.h" />
<Unit filename="../../include/etl/fnv_1.h" />
<Unit filename="../../include/etl/format_spec.h" />
<Unit filename="../../include/etl/forward_list.h" />
<Unit filename="../../include/etl/frame_check_sequence.h" />
<Unit filename="../../include/etl/fsm.h" />
@ -247,6 +248,7 @@
<Unit filename="../../include/etl/priority_queue.h" />
<Unit filename="../../include/etl/private/ivectorpointer.h" />
<Unit filename="../../include/etl/private/pvoidvector.h" />
<Unit filename="../../include/etl/private/to_string_helper.h" />
<Unit filename="../../include/etl/private/vector_base.h" />
<Unit filename="../../include/etl/profiles/arduino_arm.h" />
<Unit filename="../../include/etl/profiles/armv5.h" />
@ -297,6 +299,10 @@
<Unit filename="../../include/etl/task.h" />
<Unit filename="../../include/etl/temp.h" />
<Unit filename="../../include/etl/timer.h" />
<Unit filename="../../include/etl/to_string.h" />
<Unit filename="../../include/etl/to_u16string.h" />
<Unit filename="../../include/etl/to_u32string.h" />
<Unit filename="../../include/etl/to_wstring.h" />
<Unit filename="../../include/etl/type_def.h" />
<Unit filename="../../include/etl/type_lookup.h" />
<Unit filename="../../include/etl/type_lookup_generator.h" />
@ -428,6 +434,10 @@
<Unit filename="../test_string_view.cpp" />
<Unit filename="../test_string_wchar_t.cpp" />
<Unit filename="../test_task_scheduler.cpp" />
<Unit filename="../test_to_string.cpp" />
<Unit filename="../test_to_u16string.cpp" />
<Unit filename="../test_to_u32string.cpp" />
<Unit filename="../test_to_wstring.cpp" />
<Unit filename="../test_type_def.cpp" />
<Unit filename="../test_type_lookup.cpp" />
<Unit filename="../test_type_select.cpp" />

253
test/test_to_string.cpp Normal file
View File

@ -0,0 +1,253 @@
/******************************************************************************
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++.h"
#include <ostream>
#include "etl/to_string.h"
#include "etl/cstring.h"
#include "etl/format_spec.h"
#undef STR
#define STR(x) x
namespace
{
typedef etl::format_spec<etl::istring> Format;
std::ostream& operator << (std::ostream& os, const etl::istring& str)
{
for (auto c : str)
{
os << c;
}
return os;
}
SUITE(test_to_string)
{
//*************************************************************************
TEST(test_default_format_no_append)
{
etl::string<20> str;
CHECK_EQUAL(etl::string<20>(STR("0")), etl::to_string(uint8_t(0), str));
CHECK_EQUAL(etl::string<20>(STR("0")), etl::to_string(uint16_t(0), str));
CHECK_EQUAL(etl::string<20>(STR("0")), etl::to_string(uint32_t(0), str));
CHECK_EQUAL(etl::string<20>(STR("0")), etl::to_string(uint64_t(0), str));
CHECK_EQUAL(etl::string<20>(STR("128")), etl::to_string(uint8_t(128), str));
CHECK_EQUAL(etl::string<20>(STR("32768")), etl::to_string(uint16_t(32768), str));
CHECK_EQUAL(etl::string<20>(STR("2147483648")), etl::to_string(uint32_t(2147483648ul), str));
CHECK_EQUAL(etl::string<20>(STR("9223372036854775808")), etl::to_string(uint64_t(9223372036854775808ull), str));
CHECK_EQUAL(etl::string<20>(STR("127")), etl::to_string(int8_t(127), str));
CHECK_EQUAL(etl::string<20>(STR("32767")), etl::to_string(int16_t(32767), str));
CHECK_EQUAL(etl::string<20>(STR("2147483647")), etl::to_string(int32_t(2147483647ll), str));
CHECK_EQUAL(etl::string<20>(STR("9223372036854775807")), etl::to_string(int64_t(9223372036854775807ll), str));
CHECK_EQUAL(etl::string<20>(STR("-128")), etl::to_string(int8_t(-128), str));
CHECK_EQUAL(etl::string<20>(STR("-32768")), etl::to_string(int16_t(-32768), str));
CHECK_EQUAL(etl::string<20>(STR("-2147483648")), etl::to_string(int32_t(-2147483648ll), str));
CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str));
}
//*************************************************************************
TEST(test_default_format_append)
{
etl::string<120> str;
CHECK_EQUAL(etl::string<120>(STR("0")), etl::to_string(uint8_t(0), str, true));
CHECK_EQUAL(etl::string<120>(STR("00")), etl::to_string(uint16_t(0), str, true));
CHECK_EQUAL(etl::string<120>(STR("000")), etl::to_string(uint32_t(0), str, true));
CHECK_EQUAL(etl::string<120>(STR("0000")), etl::to_string(uint64_t(0), str, true));
CHECK_EQUAL(etl::string<120>(STR("0000128")), etl::to_string(uint8_t(128), str, true));
CHECK_EQUAL(etl::string<120>(STR("000012832768")), etl::to_string(uint16_t(32768), str, true));
CHECK_EQUAL(etl::string<120>(STR("0000128327682147483648")), etl::to_string(uint32_t(2147483648ul), str, true));
CHECK_EQUAL(etl::string<120>(STR("00001283276821474836489223372036854775808")), etl::to_string(uint64_t(9223372036854775808ull), str, true));
CHECK_EQUAL(etl::string<120>(STR("00001283276821474836489223372036854775808127")), etl::to_string(int8_t(127), str, true));
CHECK_EQUAL(etl::string<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_string(int16_t(32767), str, true));
CHECK_EQUAL(etl::string<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_string(int32_t(2147483647ll), str, true));
CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_string(int64_t(9223372036854775807ll), str, true));
CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_string(int8_t(-128), str, true));
CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_string(int16_t(-32768), str, true));
CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_string(int32_t(-2147483648ll), str, true));
CHECK_EQUAL(etl::string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, true));
}
//*************************************************************************
TEST(test_format_right_justified_no_append)
{
etl::string<20> str;
Format format = Format().base(10).width(20).fill(STR('#'));
CHECK_EQUAL(etl::string<20>(STR("###################0")), etl::to_string(uint8_t(0), str, format));
CHECK_EQUAL(etl::string<20>(STR("###################0")), etl::to_string(uint16_t(0), str, format));
CHECK_EQUAL(etl::string<20>(STR("###################0")), etl::to_string(uint32_t(0), str, format));
CHECK_EQUAL(etl::string<20>(STR("###################0")), etl::to_string(uint64_t(0), str, format));
CHECK_EQUAL(etl::string<20>(STR("#################128")), etl::to_string(uint8_t(128), str, format));
CHECK_EQUAL(etl::string<20>(STR("###############32768")), etl::to_string(uint16_t(32768), str, format));
CHECK_EQUAL(etl::string<20>(STR("##########2147483648")), etl::to_string(uint32_t(2147483648ul), str, format));
CHECK_EQUAL(etl::string<20>(STR("#9223372036854775808")), etl::to_string(uint64_t(9223372036854775808ull), str, format));
CHECK_EQUAL(etl::string<20>(STR("#################127")), etl::to_string(int8_t(127), str, format));
CHECK_EQUAL(etl::string<20>(STR("###############32767")), etl::to_string(int16_t(32767), str, format));
CHECK_EQUAL(etl::string<20>(STR("##########2147483647")), etl::to_string(int32_t(2147483647ll), str, format));
CHECK_EQUAL(etl::string<20>(STR("#9223372036854775807")), etl::to_string(int64_t(9223372036854775807ll), str, format));
CHECK_EQUAL(etl::string<20>(STR("################-128")), etl::to_string(int8_t(-128), str, format));
CHECK_EQUAL(etl::string<20>(STR("##############-32768")), etl::to_string(int16_t(-32768), str, format));
CHECK_EQUAL(etl::string<20>(STR("#########-2147483648")), etl::to_string(int32_t(-2147483648ll), str, format));
CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format));
}
//*************************************************************************
TEST(test_format_left_justified_no_append)
{
etl::string<20> str;
Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true);
CHECK_EQUAL(etl::string<20>(STR("0###################")), etl::to_string(uint8_t(0), str, format));
CHECK_EQUAL(etl::string<20>(STR("0###################")), etl::to_string(uint16_t(0), str, format));
CHECK_EQUAL(etl::string<20>(STR("0###################")), etl::to_string(uint32_t(0), str, format));
CHECK_EQUAL(etl::string<20>(STR("0###################")), etl::to_string(uint64_t(0), str, format));
CHECK_EQUAL(etl::string<20>(STR("128#################")), etl::to_string(uint8_t(128), str, format));
CHECK_EQUAL(etl::string<20>(STR("32768###############")), etl::to_string(uint16_t(32768), str, format));
CHECK_EQUAL(etl::string<20>(STR("2147483648##########")), etl::to_string(uint32_t(2147483648ul), str, format));
CHECK_EQUAL(etl::string<20>(STR("9223372036854775808#")), etl::to_string(uint64_t(9223372036854775808ull), str, format));
CHECK_EQUAL(etl::string<20>(STR("127#################")), etl::to_string(int8_t(127), str, format));
CHECK_EQUAL(etl::string<20>(STR("32767###############")), etl::to_string(int16_t(32767), str, format));
CHECK_EQUAL(etl::string<20>(STR("2147483647##########")), etl::to_string(int32_t(2147483647ll), str, format));
CHECK_EQUAL(etl::string<20>(STR("9223372036854775807#")), etl::to_string(int64_t(9223372036854775807ll), str, format));
CHECK_EQUAL(etl::string<20>(STR("-128################")), etl::to_string(int8_t(-128), str, format));
CHECK_EQUAL(etl::string<20>(STR("-32768##############")), etl::to_string(int16_t(-32768), str, format));
CHECK_EQUAL(etl::string<20>(STR("-2147483648#########")), etl::to_string(int32_t(-2147483648ll), str, format));
CHECK_EQUAL(etl::string<20>(STR("-9223372036854775808")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, format));
}
//*************************************************************************
TEST(test_binary_format_no_append)
{
etl::string<64> str;
CHECK_EQUAL(etl::string<64>(STR("00000000")), etl::to_string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("0000000000000000")), etl::to_string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("00000000000000000000000000000000")), etl::to_string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(uint64_t(0), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("10000000")), etl::to_string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("1000000000000000")), etl::to_string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("10000000000000000000000000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(uint64_t(9223372036854775808ull), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("01111111")), etl::to_string(int8_t(127), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("0111111111111111")), etl::to_string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("01111111111111111111111111111111")), etl::to_string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_string(int64_t(9223372036854775807ll), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("10000000")), etl::to_string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("1000000000000000")), etl::to_string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("10000000000000000000000000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0'))));
}
//*************************************************************************
TEST(test_octal_format_no_append)
{
etl::string<22> str;
CHECK_EQUAL(etl::string<22>(STR("000")), etl::to_string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("000000")), etl::to_string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("00000000000")), etl::to_string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("0000000000000000000000")), etl::to_string(uint64_t(0), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("200")), etl::to_string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("100000")), etl::to_string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("20000000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("1000000000000000000000")), etl::to_string(uint64_t(9223372036854775808ull), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("177")), etl::to_string(int8_t(127), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("077777")), etl::to_string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("17777777777")), etl::to_string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("0777777777777777777777")), etl::to_string(int64_t(9223372036854775807ll), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("200")), etl::to_string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("100000")), etl::to_string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("20000000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::string<22>(STR("1000000000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0'))));
}
//*************************************************************************
TEST(test_hex_format_no_append)
{
etl::string<16> str;
CHECK_EQUAL(etl::string<16>(STR("00")), etl::to_string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("0000")), etl::to_string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("00000000")), etl::to_string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("0000000000000000")), etl::to_string(uint64_t(0), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("80")), etl::to_string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("8000")), etl::to_string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("80000000")), etl::to_string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("8000000000000000")), etl::to_string(uint64_t(9223372036854775808ull), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("7F")), etl::to_string(int8_t(127), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("7FFF")), etl::to_string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("7FFFFFFF")), etl::to_string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_string(int64_t(9223372036854775807ll), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("80")), etl::to_string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("8000")), etl::to_string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("80000000")), etl::to_string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::string<16>(STR("8000000000000000")), etl::to_string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0'))));
}
//*************************************************************************
TEST(test_named_format_no_append)
{
etl::string<17> str;
CHECK_EQUAL(etl::string<17>(STR("11110001001000000")), etl::to_string(123456, str, Format().binary()));
CHECK_EQUAL(etl::string<17>(STR("361100")), etl::to_string(123456, str, Format().octal()));
CHECK_EQUAL(etl::string<17>(STR("123456")), etl::to_string(123456, str, Format().decimal()));
CHECK_EQUAL(etl::string<17>(STR("1E240")), etl::to_string(123456, str, Format().hex()));
}
};
}

253
test/test_to_u16string.cpp Normal file
View File

@ -0,0 +1,253 @@
/******************************************************************************
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++.h"
#include <ostream>
#include "etl/to_u16string.h"
#include "etl/u16string.h"
#include "etl/format_spec.h"
#undef STR
#define STR(x) u##x
namespace
{
typedef etl::format_spec<etl::iu16string> Format;
std::ostream& operator << (std::ostream& os, const etl::iu16string& str)
{
for (auto c : str)
{
os << c;
}
return os;
}
SUITE(test_to_u16string)
{
//*************************************************************************
TEST(test_default_format_no_append)
{
etl::u16string<20> str;
CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_u16string(uint8_t(0), str));
CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_u16string(uint16_t(0), str));
CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_u16string(uint32_t(0), str));
CHECK_EQUAL(etl::u16string<20>(STR("0")), etl::to_u16string(uint64_t(0), str));
CHECK_EQUAL(etl::u16string<20>(STR("128")), etl::to_u16string(uint8_t(128), str));
CHECK_EQUAL(etl::u16string<20>(STR("32768")), etl::to_u16string(uint16_t(32768), str));
CHECK_EQUAL(etl::u16string<20>(STR("2147483648")), etl::to_u16string(uint32_t(2147483648ul), str));
CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775808")), etl::to_u16string(uint64_t(9223372036854775808ull), str));
CHECK_EQUAL(etl::u16string<20>(STR("127")), etl::to_u16string(int8_t(127), str));
CHECK_EQUAL(etl::u16string<20>(STR("32767")), etl::to_u16string(int16_t(32767), str));
CHECK_EQUAL(etl::u16string<20>(STR("2147483647")), etl::to_u16string(int32_t(2147483647ll), str));
CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775807")), etl::to_u16string(int64_t(9223372036854775807ll), str));
CHECK_EQUAL(etl::u16string<20>(STR("-128")), etl::to_u16string(int8_t(-128), str));
CHECK_EQUAL(etl::u16string<20>(STR("-32768")), etl::to_u16string(int16_t(-32768), str));
CHECK_EQUAL(etl::u16string<20>(STR("-2147483648")), etl::to_u16string(int32_t(-2147483648ll), str));
CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str));
}
//*************************************************************************
TEST(test_default_format_append)
{
etl::u16string<120> str;
CHECK_EQUAL(etl::u16string<120>(STR("0")), etl::to_u16string(uint8_t(0), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("00")), etl::to_u16string(uint16_t(0), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("000")), etl::to_u16string(uint32_t(0), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("0000")), etl::to_u16string(uint64_t(0), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("0000128")), etl::to_u16string(uint8_t(128), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("000012832768")), etl::to_u16string(uint16_t(32768), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("0000128327682147483648")), etl::to_u16string(uint32_t(2147483648ul), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("00001283276821474836489223372036854775808")), etl::to_u16string(uint64_t(9223372036854775808ull), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("00001283276821474836489223372036854775808127")), etl::to_u16string(int8_t(127), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_u16string(int16_t(32767), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_u16string(int32_t(2147483647ll), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_u16string(int64_t(9223372036854775807ll), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_u16string(int8_t(-128), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_u16string(int16_t(-32768), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_u16string(int32_t(-2147483648ll), str, true));
CHECK_EQUAL(etl::u16string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, true));
}
//*************************************************************************
TEST(test_format_right_justified_no_append)
{
etl::u16string<20> str;
Format format = Format().base(10).width(20).fill(STR('#'));
CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_u16string(uint8_t(0), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_u16string(uint16_t(0), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_u16string(uint32_t(0), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("###################0")), etl::to_u16string(uint64_t(0), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("#################128")), etl::to_u16string(uint8_t(128), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("###############32768")), etl::to_u16string(uint16_t(32768), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("##########2147483648")), etl::to_u16string(uint32_t(2147483648ul), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("#9223372036854775808")), etl::to_u16string(uint64_t(9223372036854775808ull), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("#################127")), etl::to_u16string(int8_t(127), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("###############32767")), etl::to_u16string(int16_t(32767), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("##########2147483647")), etl::to_u16string(int32_t(2147483647ll), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("#9223372036854775807")), etl::to_u16string(int64_t(9223372036854775807ll), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("################-128")), etl::to_u16string(int8_t(-128), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("##############-32768")), etl::to_u16string(int16_t(-32768), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("#########-2147483648")), etl::to_u16string(int32_t(-2147483648ll), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, format));
}
//*************************************************************************
TEST(test_format_left_justified_no_append)
{
etl::u16string<20> str;
Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true);
CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_u16string(uint8_t(0), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_u16string(uint16_t(0), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_u16string(uint32_t(0), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("0###################")), etl::to_u16string(uint64_t(0), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("128#################")), etl::to_u16string(uint8_t(128), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("32768###############")), etl::to_u16string(uint16_t(32768), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("2147483648##########")), etl::to_u16string(uint32_t(2147483648ul), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775808#")), etl::to_u16string(uint64_t(9223372036854775808ull), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("127#################")), etl::to_u16string(int8_t(127), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("32767###############")), etl::to_u16string(int16_t(32767), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("2147483647##########")), etl::to_u16string(int32_t(2147483647ll), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("9223372036854775807#")), etl::to_u16string(int64_t(9223372036854775807ll), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("-128################")), etl::to_u16string(int8_t(-128), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("-32768##############")), etl::to_u16string(int16_t(-32768), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("-2147483648#########")), etl::to_u16string(int32_t(-2147483648ll), str, format));
CHECK_EQUAL(etl::u16string<20>(STR("-9223372036854775808")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, format));
}
//*************************************************************************
TEST(test_binary_format_no_append)
{
etl::u16string<64> str;
CHECK_EQUAL(etl::u16string<64>(STR("00000000")), etl::to_u16string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("0000000000000000")), etl::to_u16string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("00000000000000000000000000000000")), etl::to_u16string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_u16string(uint64_t(0), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("10000000")), etl::to_u16string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000")), etl::to_u16string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("10000000000000000000000000000000")), etl::to_u16string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u16string(uint64_t(9223372036854775808ull), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("01111111")), etl::to_u16string(int8_t(127), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("0111111111111111")), etl::to_u16string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("01111111111111111111111111111111")), etl::to_u16string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_u16string(int64_t(9223372036854775807ll), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("10000000")), etl::to_u16string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000")), etl::to_u16string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("10000000000000000000000000000000")), etl::to_u16string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0'))));
}
//*************************************************************************
TEST(test_octal_format_no_append)
{
etl::u16string<22> str;
CHECK_EQUAL(etl::u16string<22>(STR("000")), etl::to_u16string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("000000")), etl::to_u16string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("00000000000")), etl::to_u16string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("0000000000000000000000")), etl::to_u16string(uint64_t(0), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("200")), etl::to_u16string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("100000")), etl::to_u16string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("20000000000")), etl::to_u16string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("1000000000000000000000")), etl::to_u16string(uint64_t(9223372036854775808ull), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("177")), etl::to_u16string(int8_t(127), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("077777")), etl::to_u16string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("17777777777")), etl::to_u16string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("0777777777777777777777")), etl::to_u16string(int64_t(9223372036854775807ll), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("200")), etl::to_u16string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("100000")), etl::to_u16string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("20000000000")), etl::to_u16string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<22>(STR("1000000000000000000000")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0'))));
}
//*************************************************************************
TEST(test_hex_format_no_append)
{
etl::u16string<16> str;
CHECK_EQUAL(etl::u16string<16>(STR("00")), etl::to_u16string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("0000")), etl::to_u16string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("00000000")), etl::to_u16string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("0000000000000000")), etl::to_u16string(uint64_t(0), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("80")), etl::to_u16string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("8000")), etl::to_u16string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("80000000")), etl::to_u16string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("8000000000000000")), etl::to_u16string(uint64_t(9223372036854775808ull), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("7F")), etl::to_u16string(int8_t(127), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("7FFF")), etl::to_u16string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("7FFFFFFF")), etl::to_u16string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_u16string(int64_t(9223372036854775807ll), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("80")), etl::to_u16string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("8000")), etl::to_u16string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("80000000")), etl::to_u16string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u16string<16>(STR("8000000000000000")), etl::to_u16string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0'))));
}
//*************************************************************************
TEST(test_named_format_no_append)
{
etl::u16string<17> str;
CHECK_EQUAL(etl::u16string<17>(STR("11110001001000000")), etl::to_u16string(123456, str, Format().binary()));
CHECK_EQUAL(etl::u16string<17>(STR("361100")), etl::to_u16string(123456, str, Format().octal()));
CHECK_EQUAL(etl::u16string<17>(STR("123456")), etl::to_u16string(123456, str, Format().decimal()));
CHECK_EQUAL(etl::u16string<17>(STR("1E240")), etl::to_u16string(123456, str, Format().hex()));
}
};
}

254
test/test_to_u32string.cpp Normal file
View File

@ -0,0 +1,254 @@
/******************************************************************************
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++.h"
#include <ostream>
#include "etl/to_u32string.h"
#include "etl/u32string.h"
#include "etl/format_spec.h"
#undef STR
#define STR(x) U##x
namespace
{
typedef etl::format_spec<etl::iu32string> Format;
std::ostream& operator << (std::ostream& os, const etl::iu32string& str)
{
for (auto c : str)
{
os << c;
}
return os;
}
SUITE(test_to_u32string)
{
//*************************************************************************
TEST(test_default_format_no_append)
{
etl::u32string<20> str;
CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_u32string(uint8_t(0), str));
CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_u32string(uint16_t(0), str));
CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_u32string(uint32_t(0), str));
CHECK_EQUAL(etl::u32string<20>(STR("0")), etl::to_u32string(uint64_t(0), str));
CHECK_EQUAL(etl::u32string<20>(STR("128")), etl::to_u32string(uint8_t(128), str));
CHECK_EQUAL(etl::u32string<20>(STR("32768")), etl::to_u32string(uint16_t(32768), str));
CHECK_EQUAL(etl::u32string<20>(STR("2147483648")), etl::to_u32string(uint32_t(2147483648ul), str));
CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775808")), etl::to_u32string(uint64_t(9223372036854775808ull), str));
CHECK_EQUAL(etl::u32string<20>(STR("127")), etl::to_u32string(int8_t(127), str));
CHECK_EQUAL(etl::u32string<20>(STR("32767")), etl::to_u32string(int16_t(32767), str));
CHECK_EQUAL(etl::u32string<20>(STR("2147483647")), etl::to_u32string(int32_t(2147483647ll), str));
CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775807")), etl::to_u32string(int64_t(9223372036854775807ll), str));
CHECK_EQUAL(etl::u32string<20>(STR("-128")), etl::to_u32string(int8_t(-128), str));
CHECK_EQUAL(etl::u32string<20>(STR("-32768")), etl::to_u32string(int16_t(-32768), str));
CHECK_EQUAL(etl::u32string<20>(STR("-2147483648")), etl::to_u32string(int32_t(-2147483648ll), str));
CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str));
}
//*************************************************************************
TEST(test_default_format_append)
{
etl::u32string<120> str;
CHECK_EQUAL(etl::u32string<120>(STR("0")), etl::to_u32string(uint8_t(0), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("00")), etl::to_u32string(uint16_t(0), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("000")), etl::to_u32string(uint32_t(0), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("0000")), etl::to_u32string(uint64_t(0), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("0000128")), etl::to_u32string(uint8_t(128), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("000012832768")), etl::to_u32string(uint16_t(32768), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("0000128327682147483648")), etl::to_u32string(uint32_t(2147483648ul), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("00001283276821474836489223372036854775808")), etl::to_u32string(uint64_t(9223372036854775808ull), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("00001283276821474836489223372036854775808127")), etl::to_u32string(int8_t(127), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_u32string(int16_t(32767), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_u32string(int32_t(2147483647ll), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_u32string(int64_t(9223372036854775807ll), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_u32string(int8_t(-128), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_u32string(int16_t(-32768), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_u32string(int32_t(-2147483648ll), str, true));
CHECK_EQUAL(etl::u32string<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, true));
}
//*************************************************************************
TEST(test_format_right_justified_no_append)
{
etl::u32string<20> str;
Format format = Format().base(10).width(20).fill(STR('#'));
CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_u32string(uint8_t(0), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_u32string(uint16_t(0), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_u32string(uint32_t(0), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("###################0")), etl::to_u32string(uint64_t(0), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("#################128")), etl::to_u32string(uint8_t(128), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("###############32768")), etl::to_u32string(uint16_t(32768), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("##########2147483648")), etl::to_u32string(uint32_t(2147483648ul), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("#9223372036854775808")), etl::to_u32string(uint64_t(9223372036854775808ull), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("#################127")), etl::to_u32string(int8_t(127), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("###############32767")), etl::to_u32string(int16_t(32767), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("##########2147483647")), etl::to_u32string(int32_t(2147483647ll), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("#9223372036854775807")), etl::to_u32string(int64_t(9223372036854775807ll), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("################-128")), etl::to_u32string(int8_t(-128), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("##############-32768")), etl::to_u32string(int16_t(-32768), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("#########-2147483648")), etl::to_u32string(int32_t(-2147483648ll), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, format));
}
//*************************************************************************
TEST(test_format_left_justified_no_append)
{
etl::u32string<20> str;
Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true);
CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_u32string(uint8_t(0), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_u32string(uint16_t(0), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_u32string(uint32_t(0), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("0###################")), etl::to_u32string(uint64_t(0), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("128#################")), etl::to_u32string(uint8_t(128), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("32768###############")), etl::to_u32string(uint16_t(32768), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("2147483648##########")), etl::to_u32string(uint32_t(2147483648ul), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775808#")), etl::to_u32string(uint64_t(9223372036854775808ull), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("127#################")), etl::to_u32string(int8_t(127), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("32767###############")), etl::to_u32string(int16_t(32767), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("2147483647##########")), etl::to_u32string(int32_t(2147483647ll), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("9223372036854775807#")), etl::to_u32string(int64_t(9223372036854775807ll), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("-128################")), etl::to_u32string(int8_t(-128), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("-32768##############")), etl::to_u32string(int16_t(-32768), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("-2147483648#########")), etl::to_u32string(int32_t(-2147483648ll), str, format));
CHECK_EQUAL(etl::u32string<20>(STR("-9223372036854775808")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, format));
}
//*************************************************************************
TEST(test_binary_format_no_append)
{
etl::u32string<64> str;
CHECK_EQUAL(etl::u32string<64>(STR("00000000")), etl::to_u32string(uint8_t(0), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("0000000000000000")), etl::to_u32string(uint16_t(0), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("00000000000000000000000000000000")), etl::to_u32string(uint32_t(0), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_u32string(uint64_t(0), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("10000000")), etl::to_u32string(uint8_t(128), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000")), etl::to_u32string(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("10000000000000000000000000000000")), etl::to_u32string(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u32string(uint64_t(9223372036854775808ull), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("01111111")), etl::to_u32string(int8_t(127), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("0111111111111111")), etl::to_u32string(int16_t(32767), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("01111111111111111111111111111111")), etl::to_u32string(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_u32string(int64_t(9223372036854775807ll), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("10000000")), etl::to_u32string(int8_t(-128), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000")), etl::to_u32string(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("10000000000000000000000000000000")), etl::to_u32string(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0'))));
}
//*************************************************************************
TEST(test_octal_format_no_append)
{
etl::u32string<22> str;
CHECK_EQUAL(etl::u32string<22>(STR("000")), etl::to_u32string(uint8_t(0), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("000000")), etl::to_u32string(uint16_t(0), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("00000000000")), etl::to_u32string(uint32_t(0), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("0000000000000000000000")), etl::to_u32string(uint64_t(0), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("200")), etl::to_u32string(uint8_t(128), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("100000")), etl::to_u32string(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("20000000000")), etl::to_u32string(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("1000000000000000000000")), etl::to_u32string(uint64_t(9223372036854775808ull), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("177")), etl::to_u32string(int8_t(127), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("077777")), etl::to_u32string(int16_t(32767), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("17777777777")), etl::to_u32string(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("0777777777777777777777")), etl::to_u32string(int64_t(9223372036854775807ll), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("200")), etl::to_u32string(int8_t(-128), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("100000")), etl::to_u32string(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("20000000000")), etl::to_u32string(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<22>(STR("1000000000000000000000")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0'))));
}
//*************************************************************************
TEST(test_hex_format_no_append)
{
etl::u32string<16> str;
CHECK_EQUAL(etl::u32string<16>(STR("00")), etl::to_u32string(uint8_t(0), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("0000")), etl::to_u32string(uint16_t(0), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("00000000")), etl::to_u32string(uint32_t(0), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("0000000000000000")), etl::to_u32string(uint64_t(0), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("80")), etl::to_u32string(uint8_t(128), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("8000")), etl::to_u32string(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("80000000")), etl::to_u32string(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("8000000000000000")), etl::to_u32string(uint64_t(9223372036854775808ull), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("7F")), etl::to_u32string(int8_t(127), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("7FFF")), etl::to_u32string(int16_t(32767), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("7FFFFFFF")), etl::to_u32string(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_u32string(int64_t(9223372036854775807ll), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("80")), etl::to_u32string(int8_t(-128), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("8000")), etl::to_u32string(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("80000000")), etl::to_u32string(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::u32string<16>(STR("8000000000000000")), etl::to_u32string(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0'))));
}
//*************************************************************************
TEST(test_named_format_no_append)
{
etl::u32string<17> str;
CHECK_EQUAL(etl::u32string<17>(STR("11110001001000000")), etl::to_u32string(123456, str, Format().binary()));
CHECK_EQUAL(etl::u32string<17>(STR("361100")), etl::to_u32string(123456, str, Format().octal()));
CHECK_EQUAL(etl::u32string<17>(STR("123456")), etl::to_u32string(123456, str, Format().decimal()));
CHECK_EQUAL(etl::u32string<17>(STR("1E240")), etl::to_u32string(123456, str, Format().hex()));
}
};
}

253
test/test_to_wstring.cpp Normal file
View File

@ -0,0 +1,253 @@
/******************************************************************************
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++.h"
#include <ostream>
#include "etl/to_wstring.h"
#include "etl/wstring.h"
#include "etl/format_spec.h"
#undef STR
#define STR(x) L##x
namespace
{
typedef etl::format_spec<etl::iwstring> Format;
std::ostream& operator << (std::ostream& os, const etl::iwstring& str)
{
for (auto c : str)
{
os << c;
}
return os;
}
SUITE(test_to_wstring)
{
//*************************************************************************
TEST(test_default_format_no_append)
{
etl::wstring<20> str;
CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_wstring(uint8_t(0), str));
CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_wstring(uint16_t(0), str));
CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_wstring(uint32_t(0), str));
CHECK_EQUAL(etl::wstring<20>(STR("0")), etl::to_wstring(uint64_t(0), str));
CHECK_EQUAL(etl::wstring<20>(STR("128")), etl::to_wstring(uint8_t(128), str));
CHECK_EQUAL(etl::wstring<20>(STR("32768")), etl::to_wstring(uint16_t(32768), str));
CHECK_EQUAL(etl::wstring<20>(STR("2147483648")), etl::to_wstring(uint32_t(2147483648ul), str));
CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775808")), etl::to_wstring(uint64_t(9223372036854775808ull), str));
CHECK_EQUAL(etl::wstring<20>(STR("127")), etl::to_wstring(int8_t(127), str));
CHECK_EQUAL(etl::wstring<20>(STR("32767")), etl::to_wstring(int16_t(32767), str));
CHECK_EQUAL(etl::wstring<20>(STR("2147483647")), etl::to_wstring(int32_t(2147483647ll), str));
CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775807")), etl::to_wstring(int64_t(9223372036854775807ll), str));
CHECK_EQUAL(etl::wstring<20>(STR("-128")), etl::to_wstring(int8_t(-128), str));
CHECK_EQUAL(etl::wstring<20>(STR("-32768")), etl::to_wstring(int16_t(-32768), str));
CHECK_EQUAL(etl::wstring<20>(STR("-2147483648")), etl::to_wstring(int32_t(-2147483648ll), str));
CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str));
}
//*************************************************************************
TEST(test_default_format_append)
{
etl::wstring<120> str;
CHECK_EQUAL(etl::wstring<120>(STR("0")), etl::to_wstring(uint8_t(0), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("00")), etl::to_wstring(uint16_t(0), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("000")), etl::to_wstring(uint32_t(0), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("0000")), etl::to_wstring(uint64_t(0), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("0000128")), etl::to_wstring(uint8_t(128), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("000012832768")), etl::to_wstring(uint16_t(32768), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("0000128327682147483648")), etl::to_wstring(uint32_t(2147483648ul), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("00001283276821474836489223372036854775808")), etl::to_wstring(uint64_t(9223372036854775808ull), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("00001283276821474836489223372036854775808127")), etl::to_wstring(int8_t(127), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("0000128327682147483648922337203685477580812732767")), etl::to_wstring(int16_t(32767), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("00001283276821474836489223372036854775808127327672147483647")), etl::to_wstring(int32_t(2147483647ll), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807")), etl::to_wstring(int64_t(9223372036854775807ll), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128")), etl::to_wstring(int8_t(-128), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768")), etl::to_wstring(int16_t(-32768), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648")), etl::to_wstring(int32_t(-2147483648ll), str, true));
CHECK_EQUAL(etl::wstring<120>(STR("000012832768214748364892233720368547758081273276721474836479223372036854775807-128-32768-2147483648-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, true));
}
//*************************************************************************
TEST(test_format_right_justified_no_append)
{
etl::wstring<20> str;
Format format = Format().base(10).width(20).fill(STR('#'));
CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_wstring(uint8_t(0), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_wstring(uint16_t(0), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_wstring(uint32_t(0), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("###################0")), etl::to_wstring(uint64_t(0), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("#################128")), etl::to_wstring(uint8_t(128), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("###############32768")), etl::to_wstring(uint16_t(32768), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("##########2147483648")), etl::to_wstring(uint32_t(2147483648ul), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("#9223372036854775808")), etl::to_wstring(uint64_t(9223372036854775808ull), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("#################127")), etl::to_wstring(int8_t(127), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("###############32767")), etl::to_wstring(int16_t(32767), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("##########2147483647")), etl::to_wstring(int32_t(2147483647ll), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("#9223372036854775807")), etl::to_wstring(int64_t(9223372036854775807ll), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("################-128")), etl::to_wstring(int8_t(-128), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("##############-32768")), etl::to_wstring(int16_t(-32768), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("#########-2147483648")), etl::to_wstring(int32_t(-2147483648ll), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, format));
}
//*************************************************************************
TEST(test_format_left_justified_no_append)
{
etl::wstring<20> str;
Format format = Format().base(10).width(20).fill(STR('#')).left_justified(true);
CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_wstring(uint8_t(0), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_wstring(uint16_t(0), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_wstring(uint32_t(0), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("0###################")), etl::to_wstring(uint64_t(0), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("128#################")), etl::to_wstring(uint8_t(128), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("32768###############")), etl::to_wstring(uint16_t(32768), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("2147483648##########")), etl::to_wstring(uint32_t(2147483648ul), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775808#")), etl::to_wstring(uint64_t(9223372036854775808ull), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("127#################")), etl::to_wstring(int8_t(127), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("32767###############")), etl::to_wstring(int16_t(32767), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("2147483647##########")), etl::to_wstring(int32_t(2147483647ll), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("9223372036854775807#")), etl::to_wstring(int64_t(9223372036854775807ll), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("-128################")), etl::to_wstring(int8_t(-128), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("-32768##############")), etl::to_wstring(int16_t(-32768), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("-2147483648#########")), etl::to_wstring(int32_t(-2147483648ll), str, format));
CHECK_EQUAL(etl::wstring<20>(STR("-9223372036854775808")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, format));
}
//*************************************************************************
TEST(test_binary_format_no_append)
{
etl::wstring<64> str;
CHECK_EQUAL(etl::wstring<64>(STR("00000000")), etl::to_wstring(uint8_t(0), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("0000000000000000")), etl::to_wstring(uint16_t(0), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("00000000000000000000000000000000")), etl::to_wstring(uint32_t(0), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("0000000000000000000000000000000000000000000000000000000000000000")), etl::to_wstring(uint64_t(0), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("10000000")), etl::to_wstring(uint8_t(128), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000")), etl::to_wstring(uint16_t(32768), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("10000000000000000000000000000000")), etl::to_wstring(uint32_t(2147483648ul), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_wstring(uint64_t(9223372036854775808ull), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("01111111")), etl::to_wstring(int8_t(127), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("0111111111111111")), etl::to_wstring(int16_t(32767), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("01111111111111111111111111111111")), etl::to_wstring(int32_t(2147483647ll), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("0111111111111111111111111111111111111111111111111111111111111111")), etl::to_wstring(int64_t(9223372036854775807ll), str, Format().base(2).width(64).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("10000000")), etl::to_wstring(int8_t(-128), str, Format().base(2).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000")), etl::to_wstring(int16_t(-32768), str, Format().base(2).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("10000000000000000000000000000000")), etl::to_wstring(int32_t(-2147483648ll), str, Format().base(2).width(32).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<64>(STR("1000000000000000000000000000000000000000000000000000000000000000")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, Format().base(2).width(64).fill(STR('0'))));
}
//*************************************************************************
TEST(test_octal_format_no_append)
{
etl::wstring<22> str;
CHECK_EQUAL(etl::wstring<22>(STR("000")), etl::to_wstring(uint8_t(0), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("000000")), etl::to_wstring(uint16_t(0), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("00000000000")), etl::to_wstring(uint32_t(0), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("0000000000000000000000")), etl::to_wstring(uint64_t(0), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("200")), etl::to_wstring(uint8_t(128), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("100000")), etl::to_wstring(uint16_t(32768), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("20000000000")), etl::to_wstring(uint32_t(2147483648ul), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("1000000000000000000000")), etl::to_wstring(uint64_t(9223372036854775808ull), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("177")), etl::to_wstring(int8_t(127), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("077777")), etl::to_wstring(int16_t(32767), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("17777777777")), etl::to_wstring(int32_t(2147483647ll), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("0777777777777777777777")), etl::to_wstring(int64_t(9223372036854775807ll), str, Format().base(8).width(22).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("200")), etl::to_wstring(int8_t(-128), str, Format().base(8).width(3).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("100000")), etl::to_wstring(int16_t(-32768), str, Format().base(8).width(6).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("20000000000")), etl::to_wstring(int32_t(-2147483648ll), str, Format().base(8).width(11).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<22>(STR("1000000000000000000000")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, Format().base(8).width(22).fill(STR('0'))));
}
//*************************************************************************
TEST(test_hex_format_no_append)
{
etl::wstring<16> str;
CHECK_EQUAL(etl::wstring<16>(STR("00")), etl::to_wstring(uint8_t(0), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("0000")), etl::to_wstring(uint16_t(0), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("00000000")), etl::to_wstring(uint32_t(0), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("0000000000000000")), etl::to_wstring(uint64_t(0), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("80")), etl::to_wstring(uint8_t(128), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("8000")), etl::to_wstring(uint16_t(32768), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("80000000")), etl::to_wstring(uint32_t(2147483648ul), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("8000000000000000")), etl::to_wstring(uint64_t(9223372036854775808ull), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("7F")), etl::to_wstring(int8_t(127), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("7FFF")), etl::to_wstring(int16_t(32767), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("7FFFFFFF")), etl::to_wstring(int32_t(2147483647ll), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("7FFFFFFFFFFFFFFF")), etl::to_wstring(int64_t(9223372036854775807ll), str, Format().base(16).width(16).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("80")), etl::to_wstring(int8_t(-128), str, Format().base(16).width(2).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("8000")), etl::to_wstring(int16_t(-32768), str, Format().base(16).width(4).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("80000000")), etl::to_wstring(int32_t(-2147483648ll), str, Format().base(16).width(8).fill(STR('0'))));
CHECK_EQUAL(etl::wstring<16>(STR("8000000000000000")), etl::to_wstring(int64_t(-9223372036854775807ll - 1), str, Format().base(16).width(16).fill(STR('0'))));
}
//*************************************************************************
TEST(test_named_format_no_append)
{
etl::wstring<17> str;
CHECK_EQUAL(etl::wstring<17>(STR("11110001001000000")), etl::to_wstring(123456, str, Format().binary()));
CHECK_EQUAL(etl::wstring<17>(STR("361100")), etl::to_wstring(123456, str, Format().octal()));
CHECK_EQUAL(etl::wstring<17>(STR("123456")), etl::to_wstring(123456, str, Format().decimal()));
CHECK_EQUAL(etl::wstring<17>(STR("1E240")), etl::to_wstring(123456, str, Format().hex()));
}
};
}

View File

@ -371,12 +371,15 @@
<ClInclude Include="..\..\include\etl\crc32_c.h" />
<ClInclude Include="..\..\include\etl\cumulative_moving_average.h" />
<ClInclude Include="..\..\include\etl\c\ecl_timer.h" />
<ClInclude Include="..\..\include\etl\format_spec.h" />
<ClInclude Include="..\..\include\etl\frame_check_sequence.h" />
<ClInclude Include="..\..\include\etl\fsm.h" />
<ClInclude Include="..\..\include\etl\fsm_generator.h" />
<ClInclude Include="..\..\include\etl\callback_service.h" />
<ClInclude Include="..\..\include\etl\largest_generator.h" />
<ClInclude Include="..\..\include\etl\absolute.h" />
<ClInclude Include="..\..\include\etl\negative.h" />
<ClInclude Include="..\..\include\etl\private\to_string_helper.h" />
<ClInclude Include="..\..\include\etl\queue_spsc_locked.h" />
<ClInclude Include="..\..\include\etl\scaled_rounding.h" />
<ClInclude Include="..\..\include\etl\state_chart.h" />
@ -434,6 +437,10 @@
<ClInclude Include="..\..\include\etl\string_view.h" />
<ClInclude Include="..\..\include\etl\task.h" />
<ClInclude Include="..\..\include\etl\timer.h" />
<ClInclude Include="..\..\include\etl\to_string.h" />
<ClInclude Include="..\..\include\etl\to_u16string.h" />
<ClInclude Include="..\..\include\etl\to_u32string.h" />
<ClInclude Include="..\..\include\etl\to_wstring.h" />
<ClInclude Include="..\..\include\etl\type_lookup.h" />
<ClInclude Include="..\..\include\etl\type_lookup_generator.h" />
<ClInclude Include="..\..\include\etl\type_select.h" />
@ -745,6 +752,10 @@
<ClCompile Include="..\test_string_view.cpp" />
<ClCompile Include="..\test_string_wchar_t.cpp" />
<ClCompile Include="..\test_task_scheduler.cpp" />
<ClCompile Include="..\test_to_string.cpp" />
<ClCompile Include="..\test_to_u16string.cpp" />
<ClCompile Include="..\test_to_u32string.cpp" />
<ClCompile Include="..\test_to_wstring.cpp" />
<ClCompile Include="..\test_type_def.cpp" />
<ClCompile Include="..\test_type_lookup.cpp" />
<ClCompile Include="..\test_type_select.cpp" />

View File

@ -732,6 +732,27 @@
<ClInclude Include="..\..\include\etl\queue_spsc_locked.h">
<Filter>ETL\Containers</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\to_string.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\negative.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\format_spec.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\to_u16string.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\to_u32string.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\to_wstring.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\to_string_helper.h">
<Filter>ETL\Private</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\main.cpp">
@ -1163,12 +1184,24 @@
<ClCompile Include="..\test_queue_spsc_locked_small.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_to_string.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_vector_external_buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_vector_pointer_external_buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_to_wstring.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_to_u16string.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\test_to_u32string.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\..\library.properties">