string_span_tests.cpp revision fa056f67e8c0c3ef27df5da241c714cc0f45d71a
1/////////////////////////////////////////////////////////////////////////////// 2// 3// Copyright (c) 2015 Microsoft Corporation. All rights reserved. 4// 5// This code is licensed under the MIT License (MIT). 6// 7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 8// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 9// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 10// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 11// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 12// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 13// THE SOFTWARE. 14// 15/////////////////////////////////////////////////////////////////////////////// 16 17#include <UnitTest++/UnitTest++.h> 18#include <string_span.h> 19#include <vector> 20#include <cstdlib> 21 22using namespace std; 23using namespace gsl; 24 25 26SUITE(string_span_tests) 27{ 28 29 TEST(TestLiteralConstruction) 30 { 31 cwstring_span<> v = ensure_z(L"Hello"); 32 33 CHECK(5 == v.length()); 34 35#ifdef CONFIRM_COMPILATION_ERRORS 36 wstring_span<> v2 = ensure0(L"Hello"); 37#endif 38 } 39 40 TEST(TestConstructFromStdString) 41 { 42 std::string s = "Hello there world"; 43 cstring_span<> v = s; 44 CHECK(v.length() == static_cast<cstring_span<>::size_type>(s.length())); 45 } 46 47 TEST(TestConstructFromStdVector) 48 { 49 std::vector<char> vec(5, 'h'); 50 string_span<> v = vec; 51 CHECK(v.length() == static_cast<string_span<>::size_type>(vec.size())); 52 } 53 54 TEST(TestStackArrayConstruction) 55 { 56 wchar_t stack_string[] = L"Hello"; 57 58 { 59 cwstring_span<> v = ensure_z(stack_string); 60 CHECK(v.length() == 5); 61 CHECK(v.used_length() == v.length()); 62 } 63 64 { 65 cwstring_span<> v = stack_string; 66 CHECK(v.length() == 6); 67 CHECK(v.used_length() == v.length()); 68 } 69 70 { 71 wstring_span<> v = ensure_z(stack_string); 72 CHECK(v.length() == 5); 73 CHECK(v.used_length() == v.length()); 74 } 75 76 { 77 wstring_span<> v = stack_string; 78 CHECK(v.length() == 6); 79 CHECK(v.used_length() == v.length()); 80 } 81 } 82 83 TEST(TestConstructFromConstCharPointer) 84 { 85 const char* s = "Hello"; 86 cstring_span<> v = ensure_z(s); 87 CHECK(v.length() == 5); 88 CHECK(v.used_length() == v.length()); 89 } 90 91 TEST(TestConversionToConst) 92 { 93 char stack_string[] = "Hello"; 94 string_span<> v = ensure_z(stack_string); 95 cstring_span<> v2 = v; 96 CHECK(v.length() == v2.length()); 97 } 98 99 TEST(TestConversionFromConst) 100 { 101 char stack_string[] = "Hello"; 102 cstring_span<> v = ensure_z(stack_string); 103 (void)v; 104#ifdef CONFIRM_COMPILATION_ERRORS 105 string_span<> v2 = v; 106 string_span<> v3 = "Hello"; 107#endif 108 } 109 110 TEST(TestToString) 111 { 112 auto s = gsl::to_string(cstring_span<>{}); 113 CHECK(s.length() == 0); 114 115 char stack_string[] = "Hello"; 116 cstring_span<> v = ensure_z(stack_string); 117 auto s2 = gsl::to_string(v); 118 CHECK(static_cast<cstring_span<>::size_type>(s2.length()) == v.length()); 119 CHECK(s2.length() == 5); 120 } 121} 122 123int main(int, const char *[]) 124{ 125 return UnitTest::RunAllTests(); 126} 127