aboutsummaryrefslogtreecommitdiffstats
path: root/tests/string_view_tests.cpp
blob: c382cf0b0abed9f84d769e09829b336137a9b298 (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
/////////////////////////////////////////////////////////////////////////////// 
// 
// 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_view.h>
#include <vector>
#include <cstdlib>

using namespace std;
using namespace Guide;

SUITE(string_view_tests)
{

    TEST(TestLiteralConstruction)
	{
        cwstring_view<> v = ensure_z(L"Hello");

        CHECK(5 == v.length());

#ifdef CONFIRM_COMPILATION_ERRORS
        wstring_view<> v2 = ensure0(L"Hello");
#endif
	}

    TEST(TestConstructFromStdString)
    {
        std::string s = "Hello there world";
        cstring_view<> v = s;
        CHECK(v.length() == s.length());
    }

    TEST(TestConstructFromStdVector)
    {
        std::vector<char> vec('h', 5);
        string_view<> v = vec;
        CHECK(v.length() == vec.size());
    }

	TEST(TestStackArrayConstruction)
	{
        wchar_t stack_string[] = L"Hello";

        {
            cwstring_view<> v = ensure_z(stack_string);
            CHECK(v.length() == 5);
            CHECK(v.used_length() == v.length());
        }

        {
            cwstring_view<> v = stack_string;
            CHECK(v.length() == 6);
            CHECK(v.used_length() == v.length());
        }

        {
            wstring_view<> v = ensure_z(stack_string);
            CHECK(v.length() == 5);
            CHECK(v.used_length() == v.length());
        }

        {
            wstring_view<> v = stack_string;
            CHECK(v.length() == 6);
            CHECK(v.used_length() == v.length());
        }
	}

    TEST(TestConversionToConst)
    {
        char stack_string[] = "Hello";
        string_view<> v = ensure_z(stack_string);
        cstring_view<> v2 = v; 
        CHECK(v.length() == v2.length());
    }

    TEST(TestConversionFromConst)
    {
        char stack_string[] = "Hello";
        cstring_view<> v = ensure_z(stack_string);
#ifdef CONFIRM_COMPILATION_ERRORS
        string_view<> v2 = v;
        string_view<> v3 = "Hello";
#endif
    }
}

int main(int, const char *[])
{
    return UnitTest::RunAllTests();
}