blob: efdf0ff5be67f4edc98584aad037b0550368b7bd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
///////////////////////////////////////////////////////////////////////////////
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <string_span.h>
#include <vector>
#include <cstdlib>
using namespace std;
using namespace gsl;
SUITE(string_span_tests)
{
TEST(TestLiteralConstruction)
{
cwstring_span<> v = ensure_z(L"Hello");
CHECK(5 == v.length());
#ifdef CONFIRM_COMPILATION_ERRORS
wstring_span<> v2 = ensure0(L"Hello");
#endif
}
TEST(TestConstructFromStdString)
{
std::string s = "Hello there world";
cstring_span<> v = s;
CHECK(v.length() == static_cast<cstring_span<>::size_type>(s.length()));
}
TEST(TestConstructFromStdVector)
{
std::vector<char> vec(5, 'h');
string_span<> v = vec;
CHECK(v.length() == static_cast<string_span<>::size_type>(vec.size()));
}
TEST(TestStackArrayConstruction)
{
wchar_t stack_string[] = L"Hello";
{
cwstring_span<> v = ensure_z(stack_string);
CHECK(v.length() == 5);
CHECK(v.used_length() == v.length());
}
{
cwstring_span<> v = stack_string;
CHECK(v.length() == 6);
CHECK(v.used_length() == v.length());
}
{
wstring_span<> v = ensure_z(stack_string);
CHECK(v.length() == 5);
CHECK(v.used_length() == v.length());
}
{
wstring_span<> v = stack_string;
CHECK(v.length() == 6);
CHECK(v.used_length() == v.length());
}
}
TEST(TestConstructFromConstCharPointer)
{
const char* s = "Hello";
cstring_span<> v = ensure_z(s);
CHECK(v.length() == 5);
CHECK(v.used_length() == v.length());
}
TEST(TestConversionToConst)
{
char stack_string[] = "Hello";
string_span<> v = ensure_z(stack_string);
cstring_span<> v2 = v;
CHECK(v.length() == v2.length());
}
TEST(TestConversionFromConst)
{
char stack_string[] = "Hello";
cstring_span<> v = ensure_z(stack_string);
(void)v;
#ifdef CONFIRM_COMPILATION_ERRORS
string_span<> v2 = v;
string_span<> v3 = "Hello";
#endif
}
TEST(TestToString)
{
auto s = gsl::to_string(cstring_span<>{});
CHECK(s.length() == 0);
char stack_string[] = "Hello";
cstring_span<> v = ensure_z(stack_string);
auto s2 = gsl::to_string(v);
CHECK(static_cast<cstring_span<>::size_type>(s2.length()) == v.length());
CHECK(s2.length() == 5);
}
}
int main(int, const char *[])
{
return UnitTest::RunAllTests();
}
|