| OLD | NEW |
| (Empty) | |
| 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // 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 |
| 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. |
| 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 // arenastring_unittest.cc is not open-sourced. Do not include in open-source |
| 32 // distribution. |
| 33 |
| 34 // Based on mvels@'s frankenstring. |
| 35 |
| 36 #include <google/protobuf/arenastring.h> |
| 37 |
| 38 #include <string> |
| 39 #include <memory> |
| 40 #ifndef _SHARED_PTR_H |
| 41 #include <google/protobuf/stubs/shared_ptr.h> |
| 42 #endif |
| 43 #include <cstdlib> |
| 44 |
| 45 #include <google/protobuf/stubs/common.h> |
| 46 #include <gtest/gtest.h> |
| 47 |
| 48 namespace google { |
| 49 using google::protobuf::internal::ArenaString; |
| 50 using google::protobuf::internal::ArenaStringPtr; |
| 51 |
| 52 namespace protobuf { |
| 53 |
| 54 |
| 55 static string WrapString(const char* value) { |
| 56 return value; |
| 57 } |
| 58 |
| 59 // Test ArenaStringPtr with arena == NULL. |
| 60 TEST(ArenaStringPtrTest, ArenaStringPtrOnHeap) { |
| 61 ArenaStringPtr field; |
| 62 ::std::string default_value = "default"; |
| 63 field.UnsafeSetDefault(&default_value); |
| 64 EXPECT_EQ(string("default"), field.Get(&default_value)); |
| 65 field.Set(&default_value, WrapString("Test short"), NULL); |
| 66 EXPECT_EQ(string("Test short"), field.Get(&default_value)); |
| 67 field.Set(&default_value, WrapString("Test long long long long value"), NULL); |
| 68 EXPECT_EQ(string("Test long long long long value"), field.Get(&default_value))
; |
| 69 field.Set(&default_value, string(""), NULL); |
| 70 field.Destroy(&default_value, NULL); |
| 71 |
| 72 ArenaStringPtr field2; |
| 73 field2.UnsafeSetDefault(&default_value); |
| 74 ::std::string* mut = field2.Mutable(&default_value, NULL); |
| 75 EXPECT_EQ(mut, field2.Mutable(&default_value, NULL)); |
| 76 EXPECT_EQ(mut, &field2.Get(&default_value)); |
| 77 EXPECT_NE(&default_value, mut); |
| 78 EXPECT_EQ(string("default"), *mut); |
| 79 *mut = "Test long long long long value"; // ensure string allocates storage |
| 80 EXPECT_EQ(string("Test long long long long value"), field2.Get(&default_value)
); |
| 81 field2.Destroy(&default_value, NULL); |
| 82 } |
| 83 |
| 84 TEST(ArenaStringPtrTest, ArenaStringPtrOnArena) { |
| 85 google::protobuf::Arena arena; |
| 86 ArenaStringPtr field; |
| 87 ::std::string default_value = "default"; |
| 88 field.UnsafeSetDefault(&default_value); |
| 89 EXPECT_EQ(string("default"), field.Get(&default_value)); |
| 90 field.Set(&default_value, WrapString("Test short"), &arena); |
| 91 EXPECT_EQ(string("Test short"), field.Get(&default_value)); |
| 92 field.Set(&default_value, WrapString("Test long long long long value"), &arena
); |
| 93 EXPECT_EQ(string("Test long long long long value"), |
| 94 field.Get(&default_value)); |
| 95 field.Set(&default_value, string(""), &arena); |
| 96 field.Destroy(&default_value, &arena); |
| 97 |
| 98 ArenaStringPtr field2; |
| 99 field2.UnsafeSetDefault(&default_value); |
| 100 ::std::string* mut = field2.Mutable(&default_value, &arena); |
| 101 EXPECT_EQ(mut, field2.Mutable(&default_value, &arena)); |
| 102 EXPECT_EQ(mut, &field2.Get(&default_value)); |
| 103 EXPECT_NE(&default_value, mut); |
| 104 EXPECT_EQ(string("default"), *mut); |
| 105 *mut = "Test long long long long value"; // ensure string allocates storage |
| 106 EXPECT_EQ(string("Test long long long long value"), |
| 107 field2.Get(&default_value)); |
| 108 field2.Destroy(&default_value, &arena); |
| 109 } |
| 110 |
| 111 } // namespace protobuf |
| 112 } // namespace google |
| OLD | NEW |