Fix use of null term, added C++17 and 20 tests

This commit is contained in:
Krystian Stasiowski
2020-01-07 13:06:06 -05:00
parent 4c043f1922
commit 34ba5ea870
2 changed files with 366 additions and 9 deletions

View File

@ -186,14 +186,14 @@ public:
void void
term_impl() noexcept term_impl() noexcept
{ {
Traits::assign(data_[size_], 0); Traits::assign(data_[size_], CharT());
} }
smallest_width<N> size_{0}; smallest_width<N> size_{0};
#ifdef BOOST_STATIC_STRING_ALLOW_UNINIT_MEM #ifdef BOOST_STATIC_STRING_ALLOW_UNINIT_MEM
CharT data_[N + 1]; CharT data_[N + 1];
#else #else
CharT data_[N + 1]{0}; CharT data_[N + 1]{};
#endif #endif
}; };
@ -235,7 +235,7 @@ public:
term_impl() noexcept { } term_impl() noexcept { }
private: private:
static constexpr CharT null_{0}; static constexpr CharT null_{};
}; };
template<typename CharT, typename Traits> template<typename CharT, typename Traits>
@ -285,13 +285,14 @@ public:
void void
term_impl() noexcept term_impl() noexcept
{ {
// This requires the null terminator to be 0
Traits::assign(data_[size_impl()], 0); Traits::assign(data_[size_impl()], 0);
} }
#ifdef BOOST_STATIC_STRING_ALLOW_UNINIT_MEM #ifdef BOOST_STATIC_STRING_ALLOW_UNINIT_MEM
CharT data_[N + 1]; CharT data_[N + 1];
#else #else
CharT data_[N + 1]{0}; CharT data_[N + 1]{};
#endif #endif
}; };
#endif #endif

View File

@ -36,11 +36,64 @@ testConstantEvaluation()
{ {
#ifdef BOOST_STATIC_STRING_CPP20_CONSTEXPR_USED #ifdef BOOST_STATIC_STRING_CPP20_CONSTEXPR_USED
// c++20 constexpr tests // c++20 constexpr tests
return true; cstatic_string a;
#elif defined(BOOST_STATIC_STRING_CPP17_CONSTEXPR_USED) cstatic_string b(1, 'a');
// c++17 constexpr tests cstatic_string c(b, 0);
cstatic_string d(b, 0, 1);
cstatic_string e("a", 1);
cstatic_string f("a");
//cstatic_string g(f.begin(), f.end());
cstatic_string h(f);
cstatic_string i({'a'});
/* // assignment
a = b;
a = "a";
a = 'a';
a = {'a'};
// assign
a.assign(b);
a.assign(b, 0, 1);
a.assign("a", 1);
a.assign("a");
a.assign(b.begin(), b.end());
a.assign({'a'});
a.assign(1, 'a');
// element access
{
auto j = a.at(0);
}
{
auto j = a[0];
}
{
auto j = a.front();
}
{
auto j = a.back();
}
{
auto j = a.data();
}
{
auto j = a.c_str();
}
{
auto j = a.begin();
}
{
auto j = a.cbegin();
}
{
auto j = a.end();
}
{
auto j = a.cend();
}
// reverse iterators
{ {
auto j = a.rbegin(); auto j = a.rbegin();
} }
@ -52,7 +105,310 @@ testConstantEvaluation()
} }
{ {
auto j = a.crend(); auto j = a.crend();
}*/ }
// capacity and size
auto j = cstatic_string().size() +
cstatic_string().empty() +
cstatic_string().length() +
cstatic_string().max_size() +
cstatic_string().capacity();
// clear
a.clear();
// insert
a.insert(a.begin(), 1, 'a');
a.insert(0, a.begin());
a.insert(0, a.begin(), 1);
a.insert(a.begin(), 'a');
a.insert(a.begin(), {'a'});
// erase
a.erase();
a.erase(a.begin());
a.erase(a.begin(), a.end());
// push
a.push_back('a');
a.pop_back();
// append
a.append(1, 'a');
a.append("a", 1);
a.append("a");
a.append(a.begin(), a.end());
a.append({'a'});
// append operator
a += 'a';
a += "a";
a += {'a'};
// compare
a.compare(b);
a.compare(0, 1, b);
a.compare(0, 1, b, 0, 1);
a.compare("a");
a.compare(0, 1, "a");
a.compare(0, 1, "a", 1);
// substr
a.substr(0);
// subview
a.subview(0);
// copy
char k[20]{};
a.copy(k, 1, 0);
// resize
a.resize(1);
a.resize(1, 'a');
// swap
a.swap(b);
// replace
a.replace(0, 1, a);
a.replace(0, 1, a, 0, 1);
a.replace(0, 1, a.data(), 1);
a.replace(0, 1, a.data());
a.replace(0, 1, 1, 'a');
a.replace(a.begin(), a.end(), a);
a.replace(a.begin(), a.end(), a.data(), 1);
a.replace(a.begin(), a.end(), a.data());
a.replace(a.begin(), a.end(), 1, 'a');
a.replace(a.begin(), a.end(), a.begin(), a.end());
a.replace(a.begin(), a.end(), {'a'});
#ifdef BOOST_STATIC_STRING_USE_IS_CONST_EVAL
a.replace(a.begin(), a.end(), "a");
a.replace(a.begin(), a.end(), "a", 1);
#endif
// find
a.find(a);
a.find("a", 0, 1);
a.find("a", 0);
a.find('a', 0);
// rfind
a.rfind(a);
a.rfind("a", 0, 1);
a.rfind("a", 0);
a.rfind('a', 0);
// find_first_of
a.find_first_of(a);
a.find_first_of("a", 0, 1);
a.find_first_of("a", 0);
a.find_first_of('a', 0);
// find_first_not_of
a.find_first_not_of(a);
a.find_first_not_of("a", 0, 1);
a.find_first_not_of("a", 0);
a.find_first_not_of('a', 0);
// starts_with
a.starts_with('a');
a.starts_with("a");
// ends_with
a.ends_with('a');
a.ends_with("a");
return true;
#elif defined(BOOST_STATIC_STRING_CPP17_CONSTEXPR_USED)
//c++17 constexpr tests
// ctors
cstatic_string a;
cstatic_string b(1, 'a');
cstatic_string c(b, 0);
cstatic_string d(b, 0, 1);
cstatic_string e("a", 1);
cstatic_string f("a");
//cstatic_string g(f.begin(), f.end());
cstatic_string h(f);
cstatic_string i({'a'});
// assignment
a = b;
a = "a";
a = 'a';
a = {'a'};
// assign
a.assign(b);
a.assign(b, 0, 1);
a.assign("a", 1);
a.assign("a");
a.assign(b.begin(), b.end());
a.assign({'a'});
a.assign(1, 'a');
// element access
{
auto j = a.at(0);
}
{
auto j = a[0];
}
{
auto j = a.front();
}
{
auto j = a.back();
}
{
auto j = a.data();
}
{
auto j = a.c_str();
}
{
auto j = a.begin();
}
{
auto j = a.cbegin();
}
{
auto j = a.end();
}
{
auto j = a.cend();
}
// reverse iterators
{
auto j = a.rbegin();
}
{
auto j = a.crbegin();
}
{
auto j = a.rend();
}
{
auto j = a.crend();
}
// capacity and size
auto j = cstatic_string().size() +
cstatic_string().empty() +
cstatic_string().length() +
cstatic_string().max_size() +
cstatic_string().capacity();
// clear
a.clear();
// insert
a.insert(a.begin(), 1, 'a');
a.insert(0, a.begin());
a.insert(0, a.begin(), 1);
a.insert(a.begin(), 'a');
a.insert(a.begin(), {'a'});
// erase
a.erase();
a.erase(a.begin());
a.erase(a.begin(), a.end());
// push
a.push_back('a');
a.pop_back();
// append
a.append(1, 'a');
a.append("a", 1);
a.append("a");
a.append(a.begin(), a.end());
a.append({'a'});
// append operator
a += 'a';
a += "a";
a += {'a'};
// compare
a.compare(b);
a.compare(0, 1, b);
a.compare(0, 1, b, 0, 1);
a.compare("a");
a.compare(0, 1, "a");
a.compare(0, 1, "a", 1);
// substr
a.substr(0);
// subview
a.subview(0);
// copy
char k[20]{};
a.copy(k, 1, 0);
// resize
a.resize(1);
a.resize(1, 'a');
// swap
a.swap(b);
// replace
a.replace(0, 1, a);
a.replace(0, 1, a, 0, 1);
a.replace(0, 1, a.data(), 1);
a.replace(0, 1, a.data());
a.replace(0, 1, 1, 'a');
a.replace(a.begin(), a.end(), a);
a.replace(a.begin(), a.end(), a.data(), 1);
a.replace(a.begin(), a.end(), a.data());
a.replace(a.begin(), a.end(), 1, 'a');
a.replace(a.begin(), a.end(), a.begin(), a.end());
a.replace(a.begin(), a.end(), {'a'});
#ifdef BOOST_STATIC_STRING_USE_IS_CONST_EVAL
a.replace(a.begin(), a.end(), "a");
a.replace(a.begin(), a.end(), "a", 1);
#endif
// find
a.find(a);
a.find("a", 0, 1);
a.find("a", 0);
a.find('a', 0);
// rfind
a.rfind(a);
a.rfind("a", 0, 1);
a.rfind("a", 0);
a.rfind('a', 0);
// find_first_of
a.find_first_of(a);
a.find_first_of("a", 0, 1);
a.find_first_of("a", 0);
a.find_first_of('a', 0);
// find_first_not_of
a.find_first_not_of(a);
a.find_first_not_of("a", 0, 1);
a.find_first_not_of("a", 0);
a.find_first_not_of('a', 0);
// starts_with
a.starts_with('a');
a.starts_with("a");
// ends_with
a.ends_with('a');
a.ends_with("a");
return true; return true;
#elif defined(BOOST_STATIC_STRING_CPP14_CONSTEXPR_USED) #elif defined(BOOST_STATIC_STRING_CPP14_CONSTEXPR_USED)
// c++14 constexpr tests // c++14 constexpr tests