From 042e5ee28c45350185049dcc49beb3cdfd6cfa2f Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Mon, 25 Nov 2019 14:36:53 +0100 Subject: [PATCH] Test Copy & Swap of stackstrings --- include/boost/nowide/stackstring.hpp | 8 ++-- test/test_convert.cpp | 67 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/include/boost/nowide/stackstring.hpp b/include/boost/nowide/stackstring.hpp index d8aa590..96fa5d9 100644 --- a/include/boost/nowide/stackstring.hpp +++ b/include/boost/nowide/stackstring.hpp @@ -47,18 +47,18 @@ namespace nowide { } } - void swap(basic_stackstring &other) + friend void swap(basic_stackstring &lhs, basic_stackstring &rhs) { - std::swap(mem_buffer_, other.mem_buffer_); + std::swap(lhs.mem_buffer_, rhs.mem_buffer_); for(size_t i = 0; i < buffer_size; i++) - std::swap(buffer_[i], other.buffer_[i]); + std::swap(lhs.buffer_[i], rhs.buffer_[i]); } basic_stackstring &operator=(basic_stackstring const &other) { if(this != &other) { basic_stackstring tmp(other); - swap(tmp); + swap(*this, tmp); } return *this; } diff --git a/test/test_convert.cpp b/test/test_convert.cpp index 4f38e71..6c2d409 100644 --- a/test/test_convert.cpp +++ b/test/test_convert.cpp @@ -95,6 +95,7 @@ int main() TEST(*s.c_str() == '\0'); } { + TEST(whello.size() >= 3); boost::nowide::basic_stackstring sw; TEST(sw.convert(hello.c_str())); TEST(sw.c_str() == whello); @@ -102,6 +103,7 @@ int main() TEST(sw.c_str() == whello); } { + TEST(whello.size() < 5); boost::nowide::basic_stackstring sw; TEST(sw.convert(hello.c_str())); TEST(sw.c_str() == whello); @@ -109,6 +111,7 @@ int main() TEST(sw.c_str() == whello); } { + TEST(hello.size() >= 5); boost::nowide::basic_stackstring sw; TEST(sw.convert(whello.c_str())); TEST(sw.c_str() == hello); @@ -116,12 +119,76 @@ int main() TEST(sw.c_str() == hello); } { + TEST(hello.size() < 10); boost::nowide::basic_stackstring sw; TEST(sw.convert(whello.c_str())); TEST(sw.c_str() == hello); TEST(sw.convert(whello.c_str(), whello.c_str() + whello.size())); TEST(sw.c_str() == hello); } + { + typedef boost::nowide::basic_stackstring stackstring; + const std::string heapVal = hello; + TEST(heapVal.size() >= 5); // Will be put on heap + const std::wstring wtest = L"test"; + const std::string stackVal = "test"; + TEST(stackVal.size() < 5); // Will be put on stack + stackstring heap; + TEST(heap.convert(whello.c_str())); + stackstring stack; + TEST(stack.convert(wtest.c_str())); + + { + stackstring sw2(heap), sw3; + sw3 = heap; + TEST(sw2.c_str() == heapVal); + TEST(sw3.c_str() == heapVal); + } + { + stackstring sw2(stack), sw3; + sw3 = stack; + TEST(sw2.c_str() == stackVal); + TEST(sw3.c_str() == stackVal); + } + { + stackstring sw2(stack); + sw2 = heap; + TEST(sw2.c_str() == heapVal); + } + { + stackstring sw2(heap); + sw2 = stack; + TEST(sw2.c_str() == stackVal); + } + { + stackstring sw2(heap), sw3(stack); + swap(sw2, sw3); + TEST(sw2.c_str() == stackVal); + TEST(sw3.c_str() == heapVal); + swap(sw2, sw3); + TEST(sw2.c_str() == heapVal); + TEST(sw3.c_str() == stackVal); + } + { + stackstring sw2(heap), sw3(heap); + sw3.c_str()[0] = 'z'; + const std::string val2 = sw3.c_str(); + swap(sw2, sw3); + TEST(sw2.c_str() == val2); + TEST(sw3.c_str() == heapVal); + } + { + stackstring sw2(stack), sw3(stack); + sw3.c_str()[0] = 'z'; + const std::string val2 = sw3.c_str(); + swap(sw2, sw3); + TEST(sw2.c_str() == val2); + TEST(sw3.c_str() == stackVal); + } + // Sanity check + TEST(stack.c_str() == stackVal); + TEST(heap.c_str() == heapVal); + } std::cout << "- Substitutions" << std::endl; run_all(boost::nowide::widen, boost::nowide::narrow); }