|
| 1 | +//===--- OwnedStringTest.cpp ----------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/Basic/OwnedString.h" |
| 14 | +#include "gtest/gtest.h" |
| 15 | + |
| 16 | +using namespace swift; |
| 17 | + |
| 18 | +TEST(OwnedStringTest, char_pointer_empty) { |
| 19 | + const char *data = ""; |
| 20 | + const size_t length = strlen(data); |
| 21 | + OwnedString ownedString(data); |
| 22 | + |
| 23 | + EXPECT_EQ(length, ownedString.size()); |
| 24 | + EXPECT_TRUE(ownedString.empty()); |
| 25 | + |
| 26 | + OwnedString copy = ownedString.copy(); |
| 27 | + EXPECT_EQ(length, copy.size()); |
| 28 | + EXPECT_TRUE(copy.empty()); |
| 29 | + |
| 30 | + StringRef str = copy.str(); |
| 31 | + EXPECT_EQ("", str); |
| 32 | + EXPECT_EQ(length, str.size()); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(OwnedStringTest, char_pointer_non_empty) { |
| 36 | + const char *data = "string"; |
| 37 | + const size_t length = strlen(data); |
| 38 | + OwnedString ownedString(data); |
| 39 | + |
| 40 | + EXPECT_EQ(length, ownedString.size()); |
| 41 | + EXPECT_FALSE(ownedString.empty()); |
| 42 | + |
| 43 | + OwnedString copy = ownedString.copy(); |
| 44 | + EXPECT_EQ(length, copy.size()); |
| 45 | + EXPECT_FALSE(copy.empty()); |
| 46 | + |
| 47 | + StringRef str = copy.str(); |
| 48 | + EXPECT_EQ("string", str); |
| 49 | + EXPECT_EQ(length, strlen(str.data())); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(OwnedStringTest, char_pointer_length_equal) { |
| 53 | + const char *data = "string"; |
| 54 | + size_t length = strlen(data); |
| 55 | + OwnedString ownedString(data, length); |
| 56 | + |
| 57 | + EXPECT_EQ(length, ownedString.size()); |
| 58 | + EXPECT_FALSE(ownedString.empty()); |
| 59 | + |
| 60 | + OwnedString copy = ownedString.copy(); |
| 61 | + EXPECT_EQ(length, copy.size()); |
| 62 | + EXPECT_FALSE(copy.empty()); |
| 63 | + |
| 64 | + // Make sure we correctly copied the data and that it is null |
| 65 | + // terminated. |
| 66 | + StringRef str = copy.str(); |
| 67 | + EXPECT_EQ("string", str); |
| 68 | + EXPECT_EQ(length, strlen(str.data())); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(OwnedStringTest, char_pointer_length_nonzero) { |
| 72 | + const char *data = "string"; |
| 73 | + const size_t length = 1; |
| 74 | + OwnedString ownedString(data, length); |
| 75 | + |
| 76 | + EXPECT_EQ(length, ownedString.size()); |
| 77 | + EXPECT_FALSE(ownedString.empty()); |
| 78 | + |
| 79 | + OwnedString copy = ownedString.copy(); |
| 80 | + EXPECT_EQ(length, copy.size()); |
| 81 | + EXPECT_FALSE(copy.empty()); |
| 82 | + |
| 83 | + // Make sure we correctly copied the data and that it is null |
| 84 | + // terminated. |
| 85 | + StringRef str = copy.str(); |
| 86 | + EXPECT_EQ("s", str); |
| 87 | + EXPECT_EQ(1, strlen(str.data())); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(OwnedStringTest, char_pointer_length_zero) { |
| 91 | + const char *data = "string"; |
| 92 | + const size_t length = 0; |
| 93 | + OwnedString ownedString(data, length); |
| 94 | + |
| 95 | + EXPECT_EQ(length, ownedString.size()); |
| 96 | + EXPECT_TRUE(ownedString.empty()); |
| 97 | + |
| 98 | + OwnedString copy = ownedString.copy(); |
| 99 | + EXPECT_EQ(length, copy.size()); |
| 100 | + EXPECT_TRUE(copy.empty()); |
| 101 | +} |
| 102 | + |
| 103 | +TEST(OwnedStringTest, copy_original_new_different) { |
| 104 | + // Initialize a mutable string. |
| 105 | + const char *original = "string"; |
| 106 | + const size_t length = strlen(original); |
| 107 | + char *data = static_cast<char *>(malloc(length + 1)); |
| 108 | + memcpy(data, original, length); |
| 109 | + data[length] = '\0'; |
| 110 | + |
| 111 | + // Create an OwnedString. |
| 112 | + OwnedString ownedString(data, length); |
| 113 | + |
| 114 | + EXPECT_EQ(length, ownedString.size()); |
| 115 | + EXPECT_FALSE(ownedString.empty()); |
| 116 | + |
| 117 | + // Copy the string |
| 118 | + OwnedString copy = ownedString.copy(); |
| 119 | + EXPECT_EQ(length, copy.size()); |
| 120 | + EXPECT_FALSE(copy.empty()); |
| 121 | + |
| 122 | + // Make sure we correctly copied the data and that it is null |
| 123 | + // terminated. |
| 124 | + StringRef str = copy.str(); |
| 125 | + EXPECT_EQ("string", str); |
| 126 | + EXPECT_EQ(length, strlen(str.data())); |
| 127 | + |
| 128 | + // Make sure updating the original pointer doesn't affect the copy. |
| 129 | + data[0] = 'a'; |
| 130 | + |
| 131 | + EXPECT_EQ("string", str); |
| 132 | +} |
| 133 | + |
| 134 | +TEST(OwnedStringTest, copy_constructor_original_not_copy) { |
| 135 | + // Initialize a mutable string. |
| 136 | + const char *original = "string"; |
| 137 | + const size_t length = strlen(original); |
| 138 | + char *data = static_cast<char *>(malloc(length + 1)); |
| 139 | + memcpy(data, original, length); |
| 140 | + data[length] = '\0'; |
| 141 | + |
| 142 | + // Create an OwnedString. |
| 143 | + OwnedString ownedString(data, length); |
| 144 | + |
| 145 | + EXPECT_EQ(length, ownedString.size()); |
| 146 | + EXPECT_FALSE(ownedString.empty()); |
| 147 | + |
| 148 | + // Copy the string |
| 149 | + OwnedString copy = OwnedString(ownedString); |
| 150 | + EXPECT_EQ(length, copy.size()); |
| 151 | + EXPECT_FALSE(copy.empty()); |
| 152 | + |
| 153 | + // Make sure we correctly copied the data and that it is null |
| 154 | + // terminated. |
| 155 | + StringRef str = copy.str(); |
| 156 | + EXPECT_EQ("string", str); |
| 157 | + EXPECT_EQ(length, strlen(str.data())); |
| 158 | + |
| 159 | + // Make sure updating the original pointer doesn't affect the copy. |
| 160 | + data[0] = 'a'; |
| 161 | + |
| 162 | + EXPECT_EQ("atring", str); |
| 163 | +} |
| 164 | + |
| 165 | +TEST(OwnedStringTest, copy_constructor_original_copy) { |
| 166 | + // Initialize a mutable string. |
| 167 | + const char *original = "string"; |
| 168 | + const size_t length = strlen(original); |
| 169 | + char *data = static_cast<char *>(malloc(length + 1)); |
| 170 | + memcpy(data, original, length); |
| 171 | + data[length] = '\0'; |
| 172 | + |
| 173 | + // Create an OwnedString. |
| 174 | + OwnedString ownedString(data, length); |
| 175 | + |
| 176 | + EXPECT_EQ(length, ownedString.size()); |
| 177 | + EXPECT_FALSE(ownedString.empty()); |
| 178 | + |
| 179 | + // Copy the string |
| 180 | + OwnedString copy = OwnedString(ownedString.copy()); |
| 181 | + EXPECT_EQ(length, copy.size()); |
| 182 | + EXPECT_FALSE(copy.empty()); |
| 183 | + |
| 184 | + // Make sure we correctly copied the data and that it is null |
| 185 | + // terminated. |
| 186 | + StringRef str = copy.str(); |
| 187 | + EXPECT_EQ("string", str); |
| 188 | + EXPECT_EQ(length, strlen(str.data())); |
| 189 | + |
| 190 | + // Make sure updating the original pointer doesn't affect the copy. |
| 191 | + data[0] = 'a'; |
| 192 | + |
| 193 | + EXPECT_EQ("string", str); |
| 194 | +} |
0 commit comments