[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 |
|
---|
| 34 | namespace std::test
|
---|
| 35 | {
|
---|
| 36 | bool string_test::run()
|
---|
| 37 | {
|
---|
| 38 | test_construction_and_assignment();
|
---|
| 39 |
|
---|
| 40 | return true;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | const char* string_test::name()
|
---|
| 44 | {
|
---|
| 45 | return "string";
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | void string_test::test_construction_and_assignment()
|
---|
| 49 | {
|
---|
| 50 | const char* check1 = "hello";
|
---|
| 51 |
|
---|
| 52 | std::string str1{"hello"};
|
---|
| 53 | test_eq(
|
---|
| 54 | "size of string",
|
---|
| 55 | str1.size(), 6ul
|
---|
| 56 | );
|
---|
| 57 | test_eq(
|
---|
| 58 | "initialization from a cstring literal",
|
---|
| 59 | str1.begin(), str1.end(),
|
---|
| 60 | check1, check1 + 6
|
---|
| 61 | );
|
---|
| 62 |
|
---|
| 63 | std::string str2{str1};
|
---|
| 64 | test_eq(
|
---|
| 65 | "copy constructor",
|
---|
| 66 | str1.begin(), str1.end(),
|
---|
| 67 | str2.begin(), str2.end()
|
---|
| 68 | );
|
---|
| 69 |
|
---|
| 70 | std::string str3{std::move(str1)};
|
---|
| 71 | test_eq(
|
---|
| 72 | "move constructor equality",
|
---|
| 73 | str2.begin(), str2.end(),
|
---|
| 74 | str3.begin(), str3.end()
|
---|
| 75 | );
|
---|
| 76 | test_eq(
|
---|
| 77 | "move constructor source empty",
|
---|
| 78 | str1.size(), 0ul
|
---|
| 79 | );
|
---|
| 80 |
|
---|
| 81 | std::string str4{};
|
---|
| 82 | test_eq(
|
---|
| 83 | "default constrcutor empty",
|
---|
| 84 | str4.size(), 0ul
|
---|
| 85 | );
|
---|
| 86 |
|
---|
| 87 | str4.assign(str3, 2ul, 2ul);
|
---|
| 88 | test_eq(
|
---|
| 89 | "assign substring to empty string",
|
---|
| 90 | str4.begin(), str4.end(),
|
---|
| 91 | str3.begin() + 2, str3.begin() + 4
|
---|
| 92 | );
|
---|
| 93 |
|
---|
| 94 | std::string str5{str3.begin() + 2, str3.begin() + 4};
|
---|
| 95 | test_eq(
|
---|
| 96 | "constructor from a pair of iterators",
|
---|
| 97 | str5.begin(), str5.end(),
|
---|
| 98 | str3.begin() + 2, str3.begin() + 4
|
---|
| 99 | );
|
---|
| 100 | }
|
---|
| 101 | }
|
---|