GSL/tests/string_span_tests.cpp

1217 lines
30 KiB
C++
Raw Normal View History

2015-08-20 21:09:14 -04:00
///////////////////////////////////////////////////////////////////////////////
2015-12-02 15:28:43 -05:00
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#endif
2019-12-04 15:30:58 -05:00
#if __clang__ || __GNUC__
//disable warnings from gtest
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wglobal-constructors"
#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
#pragma GCC diagnostic ignored "-Wcovered-switch-default"
#endif
#include <gtest/gtest.h>
#include <gsl/gsl_assert> // for Expects, fail_fast (ptr only)
#include <gsl/pointers> // for owner
#include <gsl/span> // for span, dynamic_extent
#include <gsl/string_span> // for basic_string_span, operator==, ensure_z
#include <algorithm> // for move, find
#include <cstddef> // for size_t
#include <map> // for map
#include <string> // for basic_string, string, char_traits, operat...
#include <type_traits> // for remove_reference<>::type
#include <vector> // for vector, allocator
2015-08-20 21:09:14 -04:00
using namespace std;
using namespace gsl;
2015-08-20 21:09:14 -04:00
// Generic string functions
namespace generic
{
template <typename CharT>
auto strlen(const CharT* s)
{
auto p = s;
while (*p) ++p;
return p - s;
}
template <typename CharT>
auto strnlen(const CharT* s, std::size_t n)
{
return std::find(s, s + n, CharT{0}) - s;
}
} // namespace generic
2019-12-03 17:32:25 -05:00
namespace
{
template <typename T>
T move_wrapper(T&& t)
{
return std::move(t);
}
2019-12-04 15:30:58 -05:00
// not used otherwise
#ifdef CONFIRM_COMPILATION_ERRORS
2019-12-03 17:32:25 -05:00
template <class T>
T create()
{
return T{};
}
template <class T>
void use(basic_string_span<T, gsl::dynamic_extent>)
{
}
2019-12-04 15:30:58 -05:00
#endif
2019-12-03 17:32:25 -05:00
czstring_span<> CreateTempName(string_span<> span)
{
Expects(span.size() > 1);
int last = 0;
if (span.size() > 4) {
span[0] = 't';
span[1] = 'm';
span[2] = 'p';
last = 3;
}
span[last] = '\0';
auto ret = span.subspan(0, 4);
return {ret};
}
cwzstring_span<> CreateTempNameW(wstring_span<> span)
{
Expects(span.size() > 1);
int last = 0;
if (span.size() > 4) {
span[0] = L't';
span[1] = L'm';
span[2] = L'p';
last = 3;
}
span[last] = L'\0';
auto ret = span.subspan(0, 4);
return {ret};
}
cu16zstring_span<> CreateTempNameU16(u16string_span<> span)
{
Expects(span.size() > 1);
int last = 0;
if (span.size() > 4) {
span[0] = u't';
span[1] = u'm';
span[2] = u'p';
last = 3;
}
span[last] = u'\0';
auto ret = span.subspan(0, 4);
return {ret};
}
cu32zstring_span<> CreateTempNameU32(u32string_span<> span)
{
Expects(span.size() > 1);
int last = 0;
if (span.size() > 4) {
span[0] = U't';
span[1] = U'm';
span[2] = U'p';
last = 3;
}
span[last] = U'\0';
auto ret = span.subspan(0, 4);
return {ret};
}
} // namespace
TEST(string_span_tests, TestLiteralConstruction)
2015-08-20 21:09:14 -04:00
{
cwstring_span<> v = ensure_z(L"Hello");
2019-12-03 17:32:25 -05:00
EXPECT_EQ(5, v.length());
2015-08-20 21:09:14 -04:00
#ifdef CONFIRM_COMPILATION_ERRORS
wstring_span<> v2 = ensure0(L"Hello");
2015-08-20 21:09:14 -04:00
#endif
}
2015-08-20 21:09:14 -04:00
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, TestConstructFromStdString)
{
std::string s = "Hello there world";
cstring_span<> v = s;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(v.length(), static_cast<cstring_span<>::index_type>(s.length()));
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, TestConstructFromStdVector)
{
std::vector<char> vec(5, 'h');
string_span<> v{vec};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(v.length(), static_cast<string_span<>::index_type>(vec.size()));
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, TestStackArrayConstruction)
{
wchar_t stack_string[] = L"Hello";
2015-08-20 21:09:14 -04:00
{
cwstring_span<> v = ensure_z(stack_string);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(v.length(), 5);
2015-08-20 21:09:14 -04:00
}
2015-11-30 15:24:00 -05:00
{
cwstring_span<> v = stack_string;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(v.length(), 5);
2015-11-24 02:05:31 -05:00
}
2015-08-20 21:09:14 -04:00
{
wstring_span<> v = ensure_z(stack_string);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(v.length(), 5);
}
2015-08-20 21:09:14 -04:00
{
wstring_span<> v = stack_string;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(v.length(), 5);
2015-08-20 21:09:14 -04:00
}
}
2015-08-20 21:09:14 -04:00
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, TestConstructFromConstCharPointer)
{
const char* s = "Hello";
cstring_span<> v = ensure_z(s);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(v.length(), 5);
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, TestConversionToConst)
{
char stack_string[] = "Hello";
string_span<> v = ensure_z(stack_string);
cstring_span<> v2 = v;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(v.length(), v2.length());
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, TestConversionFromConst)
{
char stack_string[] = "Hello";
cstring_span<> v = ensure_z(stack_string);
(void) v;
2015-08-20 21:09:14 -04:00
#ifdef CONFIRM_COMPILATION_ERRORS
string_span<> v2 = v;
string_span<> v3 = "Hello";
2015-08-20 21:09:14 -04:00
#endif
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, TestToString)
{
auto s = gsl::to_string(cstring_span<>{});
2019-12-04 15:30:58 -05:00
EXPECT_EQ(s.length(), static_cast<size_t>(0));
char stack_string[] = "Hello";
cstring_span<> v = ensure_z(stack_string);
auto s2 = gsl::to_string(v);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(static_cast<cstring_span<>::index_type>(s2.length()), v.length());
2019-12-04 15:30:58 -05:00
EXPECT_EQ(s2.length(), static_cast<size_t>(5));
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, TestToBasicString)
{
auto s = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(
cstring_span<>{});
2019-12-04 15:30:58 -05:00
EXPECT_EQ(s.length(), static_cast<size_t>(0));
char stack_string[] = "Hello";
cstring_span<> v = ensure_z(stack_string);
auto s2 = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(v);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(static_cast<cstring_span<>::index_type>(s2.length()), v.length());
2019-12-04 15:30:58 -05:00
EXPECT_EQ(s2.length(), static_cast<size_t>(5));
}
2015-11-30 15:24:00 -05:00
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, EqualityAndImplicitConstructors)
{
2016-09-04 02:29:10 -04:00
{
cstring_span<> span = "Hello";
cstring_span<> span1;
2016-09-04 02:29:10 -04:00
// comparison to empty span
2019-12-03 17:32:25 -05:00
EXPECT_NE(span1, span);
EXPECT_NE(span, span1);
2016-09-04 02:29:10 -04:00
}
2015-11-30 15:24:00 -05:00
{
cstring_span<> span = "Hello";
cstring_span<> span1 = "Hello1";
2015-12-14 14:36:20 -05:00
// comparison to different span
2019-12-03 17:32:25 -05:00
EXPECT_NE(span1, span);
EXPECT_NE(span, span1);
}
2015-12-14 14:36:20 -05:00
{
cstring_span<> span = "Hello";
2015-11-30 15:24:00 -05:00
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
const char ar1[] = "Hello";
const char ar2[10] = "Hello";
const char* ptr = "Hello";
const std::string str = "Hello";
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
gsl::span<const char> sp = ensure_z("Hello");
2015-11-30 15:24:00 -05:00
// comparison to literal
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, cstring_span<>("Hello"));
2015-11-30 15:24:00 -05:00
// comparison to static array with no null termination
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, cstring_span<>(ar));
2015-11-30 15:24:00 -05:00
// comparison to static array with null at the end
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, cstring_span<>(ar1));
2015-11-30 15:24:00 -05:00
// comparison to static array with null in the middle
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, cstring_span<>(ar2));
2015-11-30 15:24:00 -05:00
// comparison to null-terminated c string
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, cstring_span<>(ptr, 5));
2015-11-30 15:24:00 -05:00
// comparison to string
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, cstring_span<>(str));
2015-11-30 15:24:00 -05:00
// comparison to vector of charaters with no null termination
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, cstring_span<>(vec));
2015-11-30 15:24:00 -05:00
// comparison to span
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, cstring_span<>(sp));
// comparison to string_span
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, span);
}
2015-11-24 02:05:31 -05:00
{
char ar[] = {'H', 'e', 'l', 'l', 'o'};
2015-11-24 02:05:31 -05:00
string_span<> span = ar;
2015-11-24 02:05:31 -05:00
char ar1[] = "Hello";
char ar2[10] = "Hello";
char* ptr = ar;
std::string str = "Hello";
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
gsl::span<char> sp = ensure_z(ar1);
2015-11-24 02:05:31 -05:00
// comparison to static array with no null termination
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, string_span<>(ar));
2015-11-24 02:05:31 -05:00
// comparison to static array with null at the end
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, string_span<>(ar1));
2015-11-24 02:05:31 -05:00
// comparison to static array with null in the middle
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, string_span<>(ar2));
2015-11-24 02:05:31 -05:00
// comparison to null-terminated c string
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, string_span<>(ptr, 5));
2015-11-24 02:05:31 -05:00
// comparison to string
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, string_span<>(str));
2015-11-24 02:05:31 -05:00
// comparison to vector of charaters with no null termination
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, string_span<>(vec));
// comparison to span
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, string_span<>(sp));
// comparison to string_span
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, span);
}
2015-11-24 02:05:31 -05:00
{
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
const char ar1[] = "Hello";
const char ar2[10] = "Hello";
const std::string str = "Hello";
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const gsl::span<const char> sp = ensure_z("Hello");
cstring_span<> span = "Hello";
2015-11-30 15:24:00 -05:00
// const span, const other type
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, "Hello");
EXPECT_EQ(span, ar);
EXPECT_EQ(span, ar1);
EXPECT_EQ(span, ar2);
#ifdef CONFIRM_COMPILATION_ERRORS
const char* ptr = "Hello";
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, ptr);
#endif
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, str);
EXPECT_EQ(span, vec);
EXPECT_EQ(span, sp);
EXPECT_EQ("Hello", span);
EXPECT_EQ(ar, span);
EXPECT_EQ(ar1, span);
EXPECT_EQ(ar2, span);
#ifdef CONFIRM_COMPILATION_ERRORS
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ptr, span);
#endif
2019-12-03 17:32:25 -05:00
EXPECT_EQ(str, span);
EXPECT_EQ(vec, span);
EXPECT_EQ(sp, span);
// const span, non-const other type
char _ar[] = {'H', 'e', 'l', 'l', 'o'};
char _ar1[] = "Hello";
char _ar2[10] = "Hello";
char* _ptr = _ar;
std::string _str = "Hello";
std::vector<char> _vec = {'H', 'e', 'l', 'l', 'o'};
gsl::span<char> _sp{_ar, 5};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, _ar);
EXPECT_EQ(span, _ar1);
EXPECT_EQ(span, _ar2);
#ifdef CONFIRM_COMPILATION_ERRORS
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, _ptr);
#endif
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span, _str);
EXPECT_EQ(span, _vec);
EXPECT_EQ(span, _sp);
2015-11-30 15:24:00 -05:00
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_ar, span);
EXPECT_EQ(_ar1, span);
EXPECT_EQ(_ar2, span);
#ifdef CONFIRM_COMPILATION_ERRORS
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_ptr, span);
#endif
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_str, span);
EXPECT_EQ(_vec, span);
EXPECT_EQ(_sp, span);
string_span<> _span{_ptr, 5};
2015-11-30 15:24:00 -05:00
// non-const span, non-const other type
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_span, _ar);
EXPECT_EQ(_span, _ar1);
EXPECT_EQ(_span, _ar2);
#ifdef CONFIRM_COMPILATION_ERRORS
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_span, _ptr);
#endif
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_span, _str);
EXPECT_EQ(_span, _vec);
EXPECT_EQ(_span, _sp);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_ar, _span);
EXPECT_EQ(_ar1, _span);
EXPECT_EQ(_ar2, _span);
#ifdef CONFIRM_COMPILATION_ERRORS
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_ptr, _span);
#endif
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_str, _span);
EXPECT_EQ(_vec, _span);
EXPECT_EQ(_sp, _span);
// non-const span, const other type
2015-11-30 15:24:00 -05:00
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_span, "Hello");
EXPECT_EQ(_span, ar);
EXPECT_EQ(_span, ar1);
EXPECT_EQ(_span, ar2);
#ifdef CONFIRM_COMPILATION_ERRORS
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_span, ptr);
#endif
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_span, str);
EXPECT_EQ(_span, vec);
EXPECT_EQ(_span, sp);
EXPECT_EQ("Hello", _span);
EXPECT_EQ(ar, _span);
EXPECT_EQ(ar1, _span);
EXPECT_EQ(ar2, _span);
#ifdef CONFIRM_COMPILATION_ERRORS
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ptr, _span);
2015-11-24 02:05:31 -05:00
#endif
2019-12-03 17:32:25 -05:00
EXPECT_EQ(str, _span);
EXPECT_EQ(vec, _span);
EXPECT_EQ(sp, _span);
// two spans
2019-12-03 17:32:25 -05:00
EXPECT_EQ(_span, span);
EXPECT_EQ(span, _span);
}
2015-11-30 15:24:00 -05:00
{
std::vector<char> str1 = {'H', 'e', 'l', 'l', 'o'};
cstring_span<> span1 = str1;
std::vector<char> str2 = std::move(str1);
cstring_span<> span2 = str2;
2015-11-30 15:24:00 -05:00
// comparison of spans from the same vector before and after move (ok)
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span1, span2);
2015-11-30 15:24:00 -05:00
}
}
2015-11-30 15:24:00 -05:00
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, ComparisonAndImplicitConstructors)
{
2015-11-24 02:05:31 -05:00
{
cstring_span<> span = "Hello";
2015-11-24 02:05:31 -05:00
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
const char ar1[] = "Hello";
const char ar2[10] = "Hello";
const char* ptr = "Hello";
const std::string str = "Hello";
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
2015-11-24 02:05:31 -05:00
// comparison to literal
2019-12-03 17:32:25 -05:00
EXPECT_LT(span, cstring_span<>("Helloo"));
EXPECT_GT(span, cstring_span<>("Hell"));
2015-11-24 02:05:31 -05:00
// comparison to static array with no null termination
2019-12-03 17:32:25 -05:00
EXPECT_GE(span, cstring_span<>(ar));
2015-11-24 02:05:31 -05:00
// comparison to static array with null at the end
2019-12-03 17:32:25 -05:00
EXPECT_LE(span, cstring_span<>(ar1));
2015-11-24 02:05:31 -05:00
// comparison to static array with null in the middle
2019-12-03 17:32:25 -05:00
EXPECT_GE(span, cstring_span<>(ar2));
2015-11-24 02:05:31 -05:00
// comparison to null-terminated c string
2019-12-03 17:32:25 -05:00
EXPECT_LE(span, cstring_span<>(ptr, 5));
2015-11-24 02:05:31 -05:00
// comparison to string
2019-12-03 17:32:25 -05:00
EXPECT_GE(span, cstring_span<>(str));
2015-11-24 02:05:31 -05:00
// comparison to vector of charaters with no null termination
2019-12-03 17:32:25 -05:00
EXPECT_LE(span, cstring_span<>(vec));
}
2015-11-24 02:05:31 -05:00
{
char ar[] = {'H', 'e', 'l', 'l', 'o'};
2015-11-24 02:05:31 -05:00
string_span<> span = ar;
2015-11-24 02:05:31 -05:00
char larr[] = "Hell";
char rarr[] = "Helloo";
2015-11-24 02:05:31 -05:00
char ar1[] = "Hello";
char ar2[10] = "Hello";
char* ptr = ar;
std::string str = "Hello";
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
2015-11-24 02:05:31 -05:00
// comparison to static array with no null termination
2019-12-03 17:32:25 -05:00
EXPECT_LE(span, string_span<>(ar));
EXPECT_LT(span, string_span<>(rarr));
EXPECT_GT(span, string_span<>(larr));
2015-11-24 02:05:31 -05:00
// comparison to static array with null at the end
2019-12-03 17:32:25 -05:00
EXPECT_GE(span, string_span<>(ar1));
2015-11-24 02:05:31 -05:00
// comparison to static array with null in the middle
2019-12-03 17:32:25 -05:00
EXPECT_LE(span, string_span<>(ar2));
2015-11-24 02:05:31 -05:00
// comparison to null-terminated c string
2019-12-03 17:32:25 -05:00
EXPECT_GE(span, string_span<>(ptr, 5));
2015-11-24 02:05:31 -05:00
// comparison to string
2019-12-03 17:32:25 -05:00
EXPECT_LE(span, string_span<>(str));
2015-11-24 02:05:31 -05:00
// comparison to vector of charaters with no null termination
2019-12-03 17:32:25 -05:00
EXPECT_GE(span, string_span<>(vec));
2015-11-24 02:05:31 -05:00
}
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, ConstrutorsEnsureZ)
{
// remove z from literals
2015-11-30 15:24:00 -05:00
{
cstring_span<> sp = "hello";
2019-12-03 17:32:25 -05:00
EXPECT_EQ(sp.length(), 5);
}
2015-11-30 15:24:00 -05:00
// take the string as is
{
auto str = std::string("hello");
cstring_span<> sp = str;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(sp.length(), 5);
}
2015-11-30 15:24:00 -05:00
// ensure z on c strings
{
gsl::owner<char*> ptr = new char[3];
2015-11-30 15:24:00 -05:00
ptr[0] = 'a';
ptr[1] = 'b';
ptr[2] = '\0';
2015-11-30 15:24:00 -05:00
string_span<> span = ensure_z(ptr);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 2);
2015-11-30 15:24:00 -05:00
delete[] ptr;
2015-11-30 15:24:00 -05:00
}
}
2015-11-30 15:24:00 -05:00
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, Constructors)
{
// creating cstring_span
2015-11-24 02:05:31 -05:00
// from span of a final extent
{
span<const char, 6> sp = "Hello";
cstring_span<> span = sp;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 6);
}
2015-12-14 14:36:20 -05:00
// from const span of a final extent to non-const string_span
2015-12-14 14:36:20 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
{
span<const char, 6> sp = "Hello";
string_span<> span = sp;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 6);
}
2015-12-14 14:36:20 -05:00
#endif
// from string temporary
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
{
cstring_span<> span = std::string("Hello");
}
2015-11-30 15:24:00 -05:00
#endif
// default
{
cstring_span<> span;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 0);
}
// from string literal
{
cstring_span<> span = "Hello";
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const static array
{
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
cstring_span<> span = ar;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from non-const static array
{
char ar[] = {'H', 'e', 'l', 'l', 'o'};
cstring_span<> span = ar;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const ptr and length
{
const char* ptr = "Hello";
cstring_span<> span{ptr, 5};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const ptr and length, include 0
{
const char* ptr = "Hello";
cstring_span<> span{ptr, 6};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 6);
}
// from const ptr and length, 0 inside
{
const char* ptr = "He\0lo";
cstring_span<> span{ptr, 5};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
// from non-const ptr and length
{
char ar[] = {'H', 'e', 'l', 'l', 'o'};
char* ptr = ar;
cstring_span<> span{ptr, 5};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
// from non-const ptr and length, 0 inside
{
char ar[] = {'H', 'e', '\0', 'l', 'o'};
char* ptr = ar;
cstring_span<> span{ptr, 5};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const string
{
const std::string str = "Hello";
const cstring_span<> span = str;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from non-const string
{
std::string str = "Hello";
const cstring_span<> span = str;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const vector
{
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const cstring_span<> span = vec;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from non-const vector
{
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const cstring_span<> span = vec;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const span
{
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const span<const char> inner = vec;
const cstring_span<> span = inner;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from non-const span
{
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const span<char> inner = vec;
const cstring_span<> span = inner;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const string_span
{
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const cstring_span<> tmp = vec;
const cstring_span<> span = tmp;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from non-const string_span
{
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
string_span<> tmp = vec;
cstring_span<> span = tmp;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// creating string_span
2015-11-30 15:24:00 -05:00
// from string literal
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
string_span<> span = "Hello";
2015-11-30 15:24:00 -05:00
#endif
}
2015-11-30 15:24:00 -05:00
// from const static array
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
const char ar[] = {'H', 'e', 'l', 'l', 'o'};
string_span<> span = ar;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-30 15:24:00 -05:00
#endif
}
2015-11-30 15:24:00 -05:00
// from non-const static array
{
char ar[] = {'H', 'e', 'l', 'l', 'o'};
string_span<> span = ar;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const ptr and length
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
const char* ptr = "Hello";
string_span<> span{ptr, 5};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-30 15:24:00 -05:00
#endif
}
2015-11-30 15:24:00 -05:00
// from non-const ptr and length
{
char ar[] = {'H', 'e', 'l', 'l', 'o'};
char* ptr = ar;
string_span<> span{ptr, 5};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const string
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
const std::string str = "Hello";
string_span<> span = str;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-30 15:24:00 -05:00
#endif
}
2015-11-30 15:24:00 -05:00
// from non-const string
{
std::string str = "Hello";
string_span<> span = str;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const vector
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
string_span<> span = vec;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-30 15:24:00 -05:00
#endif
}
2015-11-30 15:24:00 -05:00
// from non-const vector
{
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
string_span<> span = vec;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from const span
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const span<const char> inner = vec;
string_span<> span = inner;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-30 15:24:00 -05:00
#endif
}
2015-11-30 15:24:00 -05:00
// from non-const span
{
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
span<char> inner = vec;
string_span<> span = inner;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from non-const span of non-const data from const vector
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const span<char> inner = vec;
string_span<> span = inner;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-30 15:24:00 -05:00
#endif
}
2015-11-30 15:24:00 -05:00
// from const string_span
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
cstring_span<> tmp = vec;
string_span<> span = tmp;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-30 15:24:00 -05:00
#endif
}
2015-11-30 15:24:00 -05:00
// from non-const string_span
{
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const string_span<> tmp = vec;
const string_span<> span = tmp;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
}
2015-11-30 15:24:00 -05:00
// from non-const string_span from const vector
{
2015-11-30 15:24:00 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
const std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
string_span<> tmp = vec;
string_span<> span = tmp;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-30 15:24:00 -05:00
#endif
}
// from const string_span of non-const data
2015-11-24 02:05:31 -05:00
{
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
const string_span<> tmp = vec;
const string_span<> span = tmp;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-24 02:05:31 -05:00
}
}
2015-11-24 02:05:31 -05:00
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, MoveConstructors)
{
// move string_span
{
cstring_span<> span = "Hello";
const auto span1 = std::move(span);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span1.length(), 5);
}
{
cstring_span<> span = "Hello";
const auto span1 = move_wrapper(std::move(span));
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span1.length(), 5);
}
2015-11-24 02:05:31 -05:00
{
cstring_span<> span = "Hello";
const auto span1 = move_wrapper(std::move(span));
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span1.length(), 5);
}
2015-11-24 02:05:31 -05:00
// move span
{
span<const char> span = ensure_z("Hello");
const cstring_span<> span1 = std::move(span);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span1.length(), 5);
}
{
span<const char> span = ensure_z("Hello");
const cstring_span<> span2 = move_wrapper(std::move(span));
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span2.length(), 5);
}
2015-11-24 02:05:31 -05:00
// move string
{
2015-11-24 02:05:31 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
std::string str = "Hello";
string_span<> span = std::move(str);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-24 02:05:31 -05:00
#endif
}
{
2015-11-24 02:05:31 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
std::string str = "Hello";
string_span<> span = move_wrapper<std::string>(std::move(str));
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-24 02:05:31 -05:00
#endif
}
{
2015-11-24 02:05:31 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
use<char>(create<string>());
2015-11-24 02:05:31 -05:00
#endif
}
2015-11-24 02:05:31 -05:00
// move container
{
2015-11-24 02:05:31 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
string_span<> span = std::move(vec);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-24 02:05:31 -05:00
#endif
}
{
2015-11-24 02:05:31 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
string_span<> span = move_wrapper<std::vector<char>>(std::move(vec));
2019-12-03 17:32:25 -05:00
EXPECT_EQ(span.length(), 5);
2015-11-24 02:05:31 -05:00
#endif
}
{
2015-12-14 14:36:20 -05:00
#ifdef CONFIRM_COMPILATION_ERRORS
use<char>(create<std::vector<char>>());
2015-11-24 02:05:31 -05:00
#endif
}
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, Conversion)
{
#ifdef CONFIRM_COMPILATION_ERRORS
cstring_span<> span = "Hello";
cwstring_span<> wspan{span};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(wspan.length(), 5);
#endif
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, zstring)
{
// create zspan from zero terminated string
{
char buf[1];
buf[0] = '\0';
zstring_span<> zspan({buf, 1});
2019-12-03 17:32:25 -05:00
EXPECT_EQ(generic::strlen(zspan.assume_z()), 0);
EXPECT_EQ(zspan.as_string_span().size(), 0);
EXPECT_EQ(zspan.ensure_z().size(), 0);
}
// create zspan from non-zero terminated string
{
char buf[1];
buf[0] = 'a';
auto workaround_macro = [&]() { const zstring_span<> zspan({buf, 1}); };
2019-12-03 17:32:25 -05:00
EXPECT_DEATH(workaround_macro(), ".*");
}
// usage scenario: create zero-terminated temp file name and pass to a legacy API
{
char buf[10];
auto name = CreateTempName({buf, 10});
if (!name.empty()) {
czstring<> str = name.assume_z();
2019-12-03 17:32:25 -05:00
EXPECT_EQ(generic::strlen(str), 3);
EXPECT_EQ(*(str + 3), '\0');
}
}
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, wzstring)
{
// create zspan from zero terminated string
{
wchar_t buf[1];
buf[0] = L'\0';
wzstring_span<> zspan({buf, 1});
2019-12-03 17:32:25 -05:00
EXPECT_EQ(generic::strnlen(zspan.assume_z(), 1), 0);
EXPECT_EQ(zspan.as_string_span().size(), 0);
EXPECT_EQ(zspan.ensure_z().size(), 0);
}
// create zspan from non-zero terminated string
{
wchar_t buf[1];
buf[0] = L'a';
const auto workaround_macro = [&]() { const wzstring_span<> zspan({buf, 1}); };
2019-12-03 17:32:25 -05:00
EXPECT_DEATH(workaround_macro(), ".*");
2016-08-09 21:07:17 -04:00
}
// usage scenario: create zero-terminated temp file name and pass to a legacy API
2016-08-09 21:07:17 -04:00
{
wchar_t buf[10];
const auto name = CreateTempNameW({buf, 10});
if (!name.empty()) {
cwzstring<> str = name.assume_z();
2019-12-03 17:32:25 -05:00
EXPECT_EQ(generic::strnlen(str, 10), 3);
EXPECT_EQ(*(str + 3), L'\0');
}
}
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, u16zstring)
{
// create zspan from zero terminated string
{
char16_t buf[1];
buf[0] = L'\0';
u16zstring_span<> zspan({buf, 1});
2019-12-03 17:32:25 -05:00
EXPECT_EQ(generic::strnlen(zspan.assume_z(), 1), 0);
EXPECT_EQ(zspan.as_string_span().size(), 0);
EXPECT_EQ(zspan.ensure_z().size(), 0);
}
// create zspan from non-zero terminated string
{
char16_t buf[1];
buf[0] = u'a';
const auto workaround_macro = [&]() { const u16zstring_span<> zspan({buf, 1}); };
2019-12-03 17:32:25 -05:00
EXPECT_DEATH(workaround_macro(), ".*");
}
// usage scenario: create zero-terminated temp file name and pass to a legacy API
{
char16_t buf[10];
const auto name = CreateTempNameU16({buf, 10});
if (!name.empty()) {
cu16zstring<> str = name.assume_z();
2019-12-03 17:32:25 -05:00
EXPECT_EQ(generic::strnlen(str, 10), 3);
EXPECT_EQ(*(str + 3), L'\0');
}
}
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, u32zstring)
{
// create zspan from zero terminated string
{
char32_t buf[1];
buf[0] = L'\0';
u32zstring_span<> zspan({buf, 1});
2019-12-03 17:32:25 -05:00
EXPECT_EQ(generic::strnlen(zspan.assume_z(), 1), 0);
EXPECT_EQ(zspan.as_string_span().size(), 0);
EXPECT_EQ(zspan.ensure_z().size(), 0);
}
// create zspan from non-zero terminated string
{
char32_t buf[1];
buf[0] = u'a';
const auto workaround_macro = [&]() { const u32zstring_span<> zspan({buf, 1}); };
2019-12-03 17:32:25 -05:00
EXPECT_DEATH(workaround_macro(), ".*");
}
// usage scenario: create zero-terminated temp file name and pass to a legacy API
{
char32_t buf[10];
const auto name = CreateTempNameU32({buf, 10});
if (!name.empty()) {
cu32zstring<> str = name.assume_z();
2019-12-03 17:32:25 -05:00
EXPECT_EQ(generic::strnlen(str, 10), 3);
EXPECT_EQ(*(str + 3), L'\0');
}
}
2015-08-20 21:09:14 -04:00
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, Issue305)
{
std::map<gsl::cstring_span<>, int> foo = {{"foo", 0}, {"bar", 1}};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(foo["foo"], 0);
EXPECT_EQ(foo["bar"], 1);
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, char16_t_type)
{
gsl::cu16string_span<> ss1 = gsl::ensure_z(u"abc");
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss1.size(), 3);
EXPECT_EQ(ss1.size_bytes(), 6);
std::u16string s1 = gsl::to_string(ss1);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(s1, u"abc");
std::u16string s2 = u"abc";
gsl::u16string_span<> ss2 = s2;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss2.size(), 3);
gsl::u16string_span<> ss3 = ss2.subspan(1, 1);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss3.size(), 1);
EXPECT_EQ(ss3[0], u'b');
char16_t buf[4]{u'a', u'b', u'c', u'\0'};
gsl::u16string_span<> ss4{buf, 4};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss4[3], u'\0');
gsl::cu16zstring_span<> ss5(u"abc");
2019-12-03 17:32:25 -05:00
EXPECT_EQ((ss5.as_string_span().size()), 3);
gsl::cu16string_span<> ss6 = ss5.as_string_span();
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss6, ss1);
std::vector<char16_t> v7 = {u'a', u'b', u'c'};
gsl::cu16string_span<> ss7{v7};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss7, ss1);
gsl::cu16string_span<> ss8 = gsl::ensure_z(u"abc");
gsl::cu16string_span<> ss9 = gsl::ensure_z(u"abc");
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss8, ss9);
ss9 = gsl::ensure_z(u"abd");
2019-12-03 17:32:25 -05:00
EXPECT_LT(ss8, ss9);
EXPECT_LE(ss8, ss9);
EXPECT_NE(ss8, ss9);
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, char32_t_type)
{
gsl::cu32string_span<> ss1 = gsl::ensure_z(U"abc");
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss1.size(), 3);
EXPECT_EQ(ss1.size_bytes(), 12);
std::u32string s1 = gsl::to_string(ss1);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(s1, U"abc");
std::u32string s2 = U"abc";
gsl::u32string_span<> ss2 = s2;
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss2.size(), 3);
gsl::u32string_span<> ss3 = ss2.subspan(1, 1);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss3.size(), 1);
EXPECT_EQ(ss3[0], U'b');
char32_t buf[4]{U'a', U'b', U'c', U'\0'};
gsl::u32string_span<> ss4{buf, 4};
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss4[3], u'\0');
gsl::cu32zstring_span<> ss5(U"abc");
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss5.as_string_span().size(), 3);
gsl::cu32string_span<> ss6 = ss5.as_string_span();
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss6, ss1);
gsl::cu32string_span<> ss8 = gsl::ensure_z(U"abc");
gsl::cu32string_span<> ss9 = gsl::ensure_z(U"abc");
2019-12-03 17:32:25 -05:00
EXPECT_EQ(ss8, ss9);
ss9 = gsl::ensure_z(U"abd");
2019-12-03 17:32:25 -05:00
EXPECT_LT(ss8, ss9);
EXPECT_LE(ss8, ss9);
EXPECT_NE(ss8, ss9);
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, as_bytes)
{
cwzstring_span<> v(L"qwerty");
const auto s = v.as_string_span();
const auto bs = as_bytes(s);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(static_cast<const void*>(bs.data()), static_cast<const void*>(s.data()));
EXPECT_EQ(bs.size(), s.size_bytes());
}
2019-12-03 17:32:25 -05:00
TEST(string_span_tests, as_writeable_bytes)
{
wchar_t buf[]{L"qwerty"};
wzstring_span<> v(buf);
const auto s = v.as_string_span();
const auto bs = as_writeable_bytes(s);
2019-12-03 17:32:25 -05:00
EXPECT_EQ(static_cast<const void*>(bs.data()), static_cast<const void*>(s.data()));
EXPECT_EQ(bs.size(), s.size_bytes());
}
2019-12-04 15:30:58 -05:00
#if __clang__ || __GNUC__
#pragma GCC diagnostic pop
#endif