source: mainline/uspace/lib/cpp/src/internal/test/string.cpp@ b1b500b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1b500b was 9315761, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added tests for string::append and string::operator+=

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[c20cccb]1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <string>
30#include <initializer_list>
31#include <internal/test/tests.hpp>
32#include <cstdio>
33
34namespace std::test
35{
36 bool string_test::run()
37 {
38 test_construction_and_assignment();
[9315761]39 test_append();
[c20cccb]40
41 return true;
42 }
43
44 const char* string_test::name()
45 {
46 return "string";
47 }
48
49 void string_test::test_construction_and_assignment()
50 {
51 const char* check1 = "hello";
52
53 std::string str1{"hello"};
54 test_eq(
55 "size of string",
56 str1.size(), 6ul
57 );
58 test_eq(
59 "initialization from a cstring literal",
60 str1.begin(), str1.end(),
61 check1, check1 + 6
62 );
63
64 std::string str2{str1};
65 test_eq(
66 "copy constructor",
67 str1.begin(), str1.end(),
68 str2.begin(), str2.end()
69 );
70
71 std::string str3{std::move(str1)};
72 test_eq(
73 "move constructor equality",
74 str2.begin(), str2.end(),
75 str3.begin(), str3.end()
76 );
77 test_eq(
78 "move constructor source empty",
79 str1.size(), 0ul
80 );
81
82 std::string str4{};
83 test_eq(
[9315761]84 "default constructor empty",
[c20cccb]85 str4.size(), 0ul
86 );
87
88 str4.assign(str3, 2ul, 2ul);
89 test_eq(
[9315761]90 "assign substring to an empty string",
[c20cccb]91 str4.begin(), str4.end(),
92 str3.begin() + 2, str3.begin() + 4
93 );
94
95 std::string str5{str3.begin() + 2, str3.begin() + 4};
96 test_eq(
97 "constructor from a pair of iterators",
98 str5.begin(), str5.end(),
99 str3.begin() + 2, str3.begin() + 4
100 );
101 }
[9315761]102
103 void string_test::test_append()
104 {
105 std::string check{"hello, world"};
106
107 std::string str1{"hello, "};
108 str1.append("world");
109 test_eq(
110 "append cstring",
111 str1.begin(), str1.end(),
112 check.begin(), check.end()
113 );
114
115 std::string str2{"hello, "};
116 str2.append(std::string{"world"});
117 test_eq(
118 "append rvalue string",
119 str2.begin(), str2.end(),
120 check.begin(), check.end()
121 );
122
123 std::string str3{"hello, "};
124 std::string apendee{"world"};
125 str3.append(apendee);
126 test_eq(
127 "append lvalue string",
128 str3.begin(), str3.end(),
129 check.begin(), check.end()
130 );
131
132 std::string str4{"hello, "};
133 str4.append(apendee.begin(), apendee.end());
134 test_eq(
135 "append iterator range",
136 str4.begin(), str4.end(),
137 check.begin(), check.end()
138 );
139
140 std::string str5{"hello, "};
141 str5.append({'w', 'o', 'r', 'l', 'd', '\0'});
142 test_eq(
143 "append initializer list",
144 str5.begin(), str5.end(),
145 check.begin(), check.end()
146 );
147
148 std::string str6{"hello, "};
149 str6 += "world";
150 test_eq(
151 "append using +=",
152 str6.begin(), str6.end(),
153 check.begin(), check.end()
154 );
155 }
[c20cccb]156}
Note: See TracBrowser for help on using the repository browser.