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#include <google/protobuf/map_test_util_impl.h> 32#include <google/protobuf/descriptor.h> 33#include <google/protobuf/message.h> 34 35namespace google { 36namespace protobuf { 37 38class MapTestUtil { 39 public: 40 // Set every field in the TestMap message to a unique value. 41 static void SetMapFields(UNITTEST::TestMap* message); 42 43 // Set every field in the TestArenaMap message to a unique value. 44 static void SetArenaMapFields(UNITTEST::TestArenaMap* message); 45 46 // Set every field in the message to a default value. 47 static void SetMapFieldsInitialized(UNITTEST::TestMap* message); 48 49 // Modify all the map fields of the message (which should already have been 50 // initialized with SetMapFields()). 51 static void ModifyMapFields(UNITTEST::TestMap* message); 52 53 // Check that all fields have the values that they should have after 54 // SetMapFields() is called. 55 static void ExpectMapFieldsSet(const UNITTEST::TestMap& message); 56 57 // Check that all fields have the values that they should have after 58 // SetMapFields() is called for TestArenaMap. 59 static void ExpectArenaMapFieldsSet(const UNITTEST::TestArenaMap& message); 60 61 // Check that all fields have the values that they should have after 62 // SetMapFieldsInitialized() is called. 63 static void ExpectMapFieldsSetInitialized(const UNITTEST::TestMap& message); 64 65 // Expect that the message is modified as would be expected from 66 // ModifyMapFields(). 67 static void ExpectMapFieldsModified(const UNITTEST::TestMap& message); 68 69 // Check that all fields are empty. 70 static void ExpectClear(const UNITTEST::TestMap& message); 71 72 // Check that all map fields have the given size. 73 static void ExpectMapsSize(const UNITTEST::TestMap& message, int size); 74 75 // Get pointers of map entries at given index. 76 static std::vector<const Message*> GetMapEntries( 77 const UNITTEST::TestMap& message, int index); 78 79 // Get pointers of map entries from release. 80 static std::vector<const Message*> GetMapEntriesFromRelease( 81 UNITTEST::TestMap* message); 82}; 83 84inline void MapTestUtil::SetMapFields(UNITTEST::TestMap* message) { 85 MapTestUtilImpl::SetMapFields<UNITTEST::MapEnum, UNITTEST::MAP_ENUM_BAR, 86 UNITTEST::MAP_ENUM_BAZ>(message); 87} 88 89inline void MapTestUtil::SetArenaMapFields(UNITTEST::TestArenaMap* message) { 90 MapTestUtilImpl::SetArenaMapFields<UNITTEST::MapEnum, UNITTEST::MAP_ENUM_BAR, 91 UNITTEST::MAP_ENUM_BAZ>(message); 92} 93 94inline void MapTestUtil::SetMapFieldsInitialized(UNITTEST::TestMap* message) { 95 MapTestUtilImpl::SetMapFieldsInitialized(message); 96} 97 98inline void MapTestUtil::ModifyMapFields(UNITTEST::TestMap* message) { 99 MapTestUtilImpl::ModifyMapFields<UNITTEST::MapEnum, UNITTEST::MAP_ENUM_FOO>( 100 message); 101} 102 103inline void MapTestUtil::ExpectClear(const UNITTEST::TestMap& message) { 104 MapTestUtilImpl::ExpectClear(message); 105} 106 107inline void MapTestUtil::ExpectMapFieldsSet(const UNITTEST::TestMap& message) { 108 MapTestUtilImpl::ExpectMapFieldsSet<UNITTEST::MapEnum, UNITTEST::MAP_ENUM_BAR, 109 UNITTEST::MAP_ENUM_BAZ>(message); 110} 111 112inline void MapTestUtil::ExpectArenaMapFieldsSet( 113 const UNITTEST::TestArenaMap& message) { 114 MapTestUtilImpl::ExpectArenaMapFieldsSet< 115 UNITTEST::MapEnum, UNITTEST::MAP_ENUM_BAR, UNITTEST::MAP_ENUM_BAZ>( 116 message); 117} 118 119inline void MapTestUtil::ExpectMapFieldsSetInitialized( 120 const UNITTEST::TestMap& message) { 121 MapTestUtilImpl::ExpectMapFieldsSetInitialized<UNITTEST::MapEnum, 122 UNITTEST::MAP_ENUM_FOO>( 123 message); 124} 125 126inline void MapTestUtil::ExpectMapFieldsModified( 127 const UNITTEST::TestMap& message) { 128 MapTestUtilImpl::ExpectMapFieldsModified< 129 UNITTEST::MapEnum, UNITTEST::MAP_ENUM_BAR, UNITTEST::MAP_ENUM_FOO>( 130 message); 131} 132 133inline void MapTestUtil::ExpectMapsSize(const UNITTEST::TestMap& message, 134 int size) { 135 const Descriptor* descriptor = message.GetDescriptor(); 136 137 EXPECT_EQ(size, message.GetReflection()->FieldSize( 138 message, descriptor->FindFieldByName("map_int32_int32"))); 139 EXPECT_EQ(size, message.GetReflection()->FieldSize( 140 message, descriptor->FindFieldByName("map_int64_int64"))); 141 EXPECT_EQ(size, 142 message.GetReflection()->FieldSize( 143 message, descriptor->FindFieldByName("map_uint32_uint32"))); 144 EXPECT_EQ(size, 145 message.GetReflection()->FieldSize( 146 message, descriptor->FindFieldByName("map_uint64_uint64"))); 147 EXPECT_EQ(size, 148 message.GetReflection()->FieldSize( 149 message, descriptor->FindFieldByName("map_sint32_sint32"))); 150 EXPECT_EQ(size, 151 message.GetReflection()->FieldSize( 152 message, descriptor->FindFieldByName("map_sint64_sint64"))); 153 EXPECT_EQ(size, 154 message.GetReflection()->FieldSize( 155 message, descriptor->FindFieldByName("map_fixed32_fixed32"))); 156 EXPECT_EQ(size, 157 message.GetReflection()->FieldSize( 158 message, descriptor->FindFieldByName("map_fixed64_fixed64"))); 159 EXPECT_EQ(size, 160 message.GetReflection()->FieldSize( 161 message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); 162 EXPECT_EQ(size, 163 message.GetReflection()->FieldSize( 164 message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); 165 EXPECT_EQ(size, message.GetReflection()->FieldSize( 166 message, descriptor->FindFieldByName("map_int32_float"))); 167 EXPECT_EQ(size, 168 message.GetReflection()->FieldSize( 169 message, descriptor->FindFieldByName("map_int32_double"))); 170 EXPECT_EQ(size, message.GetReflection()->FieldSize( 171 message, descriptor->FindFieldByName("map_bool_bool"))); 172 EXPECT_EQ(size, 173 message.GetReflection()->FieldSize( 174 message, descriptor->FindFieldByName("map_string_string"))); 175 EXPECT_EQ(size, message.GetReflection()->FieldSize( 176 message, descriptor->FindFieldByName("map_int32_bytes"))); 177 EXPECT_EQ( 178 size, 179 message.GetReflection()->FieldSize( 180 message, descriptor->FindFieldByName("map_int32_foreign_message"))); 181} 182 183inline std::vector<const Message*> MapTestUtil::GetMapEntries( 184 const UNITTEST::TestMap& message, int index) { 185 const Descriptor* descriptor = message.GetDescriptor(); 186 std::vector<const Message*> result; 187 188 result.push_back(&message.GetReflection()->GetRepeatedMessage( 189 message, descriptor->FindFieldByName("map_int32_int32"), index)); 190 result.push_back(&message.GetReflection()->GetRepeatedMessage( 191 message, descriptor->FindFieldByName("map_int64_int64"), index)); 192 result.push_back(&message.GetReflection()->GetRepeatedMessage( 193 message, descriptor->FindFieldByName("map_uint32_uint32"), index)); 194 result.push_back(&message.GetReflection()->GetRepeatedMessage( 195 message, descriptor->FindFieldByName("map_uint64_uint64"), index)); 196 result.push_back(&message.GetReflection()->GetRepeatedMessage( 197 message, descriptor->FindFieldByName("map_sint32_sint32"), index)); 198 result.push_back(&message.GetReflection()->GetRepeatedMessage( 199 message, descriptor->FindFieldByName("map_sint64_sint64"), index)); 200 result.push_back(&message.GetReflection()->GetRepeatedMessage( 201 message, descriptor->FindFieldByName("map_fixed32_fixed32"), index)); 202 result.push_back(&message.GetReflection()->GetRepeatedMessage( 203 message, descriptor->FindFieldByName("map_fixed64_fixed64"), index)); 204 result.push_back(&message.GetReflection()->GetRepeatedMessage( 205 message, descriptor->FindFieldByName("map_sfixed32_sfixed32"), index)); 206 result.push_back(&message.GetReflection()->GetRepeatedMessage( 207 message, descriptor->FindFieldByName("map_sfixed64_sfixed64"), index)); 208 result.push_back(&message.GetReflection()->GetRepeatedMessage( 209 message, descriptor->FindFieldByName("map_int32_float"), index)); 210 result.push_back(&message.GetReflection()->GetRepeatedMessage( 211 message, descriptor->FindFieldByName("map_int32_double"), index)); 212 result.push_back(&message.GetReflection()->GetRepeatedMessage( 213 message, descriptor->FindFieldByName("map_bool_bool"), index)); 214 result.push_back(&message.GetReflection()->GetRepeatedMessage( 215 message, descriptor->FindFieldByName("map_string_string"), index)); 216 result.push_back(&message.GetReflection()->GetRepeatedMessage( 217 message, descriptor->FindFieldByName("map_int32_bytes"), index)); 218 result.push_back(&message.GetReflection()->GetRepeatedMessage( 219 message, descriptor->FindFieldByName("map_int32_enum"), index)); 220 result.push_back(&message.GetReflection()->GetRepeatedMessage( 221 message, descriptor->FindFieldByName("map_int32_foreign_message"), 222 index)); 223 224 return result; 225} 226 227inline std::vector<const Message*> MapTestUtil::GetMapEntriesFromRelease( 228 UNITTEST::TestMap* message) { 229 const Descriptor* descriptor = message->GetDescriptor(); 230 std::vector<const Message*> result; 231 232 result.push_back(message->GetReflection()->ReleaseLast( 233 message, descriptor->FindFieldByName("map_int32_int32"))); 234 result.push_back(message->GetReflection()->ReleaseLast( 235 message, descriptor->FindFieldByName("map_int64_int64"))); 236 result.push_back(message->GetReflection()->ReleaseLast( 237 message, descriptor->FindFieldByName("map_uint32_uint32"))); 238 result.push_back(message->GetReflection()->ReleaseLast( 239 message, descriptor->FindFieldByName("map_uint64_uint64"))); 240 result.push_back(message->GetReflection()->ReleaseLast( 241 message, descriptor->FindFieldByName("map_sint32_sint32"))); 242 result.push_back(message->GetReflection()->ReleaseLast( 243 message, descriptor->FindFieldByName("map_sint64_sint64"))); 244 result.push_back(message->GetReflection()->ReleaseLast( 245 message, descriptor->FindFieldByName("map_fixed32_fixed32"))); 246 result.push_back(message->GetReflection()->ReleaseLast( 247 message, descriptor->FindFieldByName("map_fixed64_fixed64"))); 248 result.push_back(message->GetReflection()->ReleaseLast( 249 message, descriptor->FindFieldByName("map_sfixed32_sfixed32"))); 250 result.push_back(message->GetReflection()->ReleaseLast( 251 message, descriptor->FindFieldByName("map_sfixed64_sfixed64"))); 252 result.push_back(message->GetReflection()->ReleaseLast( 253 message, descriptor->FindFieldByName("map_int32_float"))); 254 result.push_back(message->GetReflection()->ReleaseLast( 255 message, descriptor->FindFieldByName("map_int32_double"))); 256 result.push_back(message->GetReflection()->ReleaseLast( 257 message, descriptor->FindFieldByName("map_bool_bool"))); 258 result.push_back(message->GetReflection()->ReleaseLast( 259 message, descriptor->FindFieldByName("map_string_string"))); 260 result.push_back(message->GetReflection()->ReleaseLast( 261 message, descriptor->FindFieldByName("map_int32_bytes"))); 262 result.push_back(message->GetReflection()->ReleaseLast( 263 message, descriptor->FindFieldByName("map_int32_enum"))); 264 result.push_back(message->GetReflection()->ReleaseLast( 265 message, descriptor->FindFieldByName("map_int32_foreign_message"))); 266 267 return result; 268} 269 270} // namespace protobuf 271} // namespace google 272