诸暨麻将添加redis
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

481 lines
15 KiB

  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. #include <map>
  31. #include <memory>
  32. #include <unordered_map>
  33. #include <google/protobuf/stubs/logging.h>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/arena_test_util.h>
  36. #include <google/protobuf/map_test_util.h>
  37. #include <google/protobuf/map_unittest.pb.h>
  38. #include <google/protobuf/unittest.pb.h>
  39. #include <google/protobuf/arena.h>
  40. #include <google/protobuf/map.h>
  41. #include <google/protobuf/map_field_inl.h>
  42. #include <google/protobuf/message.h>
  43. #include <google/protobuf/repeated_field.h>
  44. #include <gtest/gtest.h>
  45. namespace google {
  46. namespace protobuf {
  47. namespace internal {
  48. using unittest::TestAllTypes;
  49. class MapFieldBaseStub : public MapFieldBase {
  50. public:
  51. typedef void InternalArenaConstructable_;
  52. typedef void DestructorSkippable_;
  53. MapFieldBaseStub() {}
  54. explicit MapFieldBaseStub(Arena* arena) : MapFieldBase(arena) {}
  55. // Get underlined repeated field without synchronizing map.
  56. RepeatedPtrField<Message>* InternalRepeatedField() { return repeated_field_; }
  57. bool IsMapClean() {
  58. return state_.load(std::memory_order_relaxed) != STATE_MODIFIED_MAP;
  59. }
  60. bool IsRepeatedClean() {
  61. return state_.load(std::memory_order_relaxed) != STATE_MODIFIED_REPEATED;
  62. }
  63. void SetMapDirty() {
  64. state_.store(STATE_MODIFIED_MAP, std::memory_order_relaxed);
  65. }
  66. void SetRepeatedDirty() {
  67. state_.store(STATE_MODIFIED_REPEATED, std::memory_order_relaxed);
  68. }
  69. bool ContainsMapKey(const MapKey& map_key) const override { return false; }
  70. bool InsertOrLookupMapValue(const MapKey& map_key,
  71. MapValueRef* val) override {
  72. return false;
  73. }
  74. bool DeleteMapValue(const MapKey& map_key) override { return false; }
  75. bool EqualIterator(const MapIterator& a,
  76. const MapIterator& b) const override {
  77. return false;
  78. }
  79. int size() const override { return 0; }
  80. void Clear() override {}
  81. void MapBegin(MapIterator* map_iter) const override {}
  82. void MapEnd(MapIterator* map_iter) const override {}
  83. void MergeFrom(const MapFieldBase& other) override {}
  84. void Swap(MapFieldBase* other) override {}
  85. void InitializeIterator(MapIterator* map_iter) const override {}
  86. void DeleteIterator(MapIterator* map_iter) const override {}
  87. void CopyIterator(MapIterator* this_iterator,
  88. const MapIterator& other_iterator) const override {}
  89. void IncreaseIterator(MapIterator* map_iter) const override {}
  90. };
  91. class MapFieldBasePrimitiveTest : public ::testing::Test {
  92. protected:
  93. typedef unittest::TestMap_MapInt32Int32Entry_DoNotUse EntryType;
  94. typedef MapField<EntryType, int32, int32, WireFormatLite::TYPE_INT32,
  95. WireFormatLite::TYPE_INT32, false>
  96. MapFieldType;
  97. MapFieldBasePrimitiveTest() {
  98. // Get descriptors
  99. map_descriptor_ = unittest::TestMap::descriptor()
  100. ->FindFieldByName("map_int32_int32")
  101. ->message_type();
  102. key_descriptor_ = map_descriptor_->FindFieldByName("key");
  103. value_descriptor_ = map_descriptor_->FindFieldByName("value");
  104. // Build map field
  105. map_field_.reset(new MapFieldType);
  106. map_field_base_ = map_field_.get();
  107. map_ = map_field_->MutableMap();
  108. initial_value_map_[0] = 100;
  109. initial_value_map_[1] = 101;
  110. map_->insert(initial_value_map_.begin(), initial_value_map_.end());
  111. EXPECT_EQ(2, map_->size());
  112. }
  113. std::unique_ptr<MapFieldType> map_field_;
  114. MapFieldBase* map_field_base_;
  115. Map<int32, int32>* map_;
  116. const Descriptor* map_descriptor_;
  117. const FieldDescriptor* key_descriptor_;
  118. const FieldDescriptor* value_descriptor_;
  119. std::map<int32, int32> initial_value_map_; // copy of initial values inserted
  120. };
  121. TEST_F(MapFieldBasePrimitiveTest, SpaceUsedExcludingSelf) {
  122. EXPECT_LT(0, map_field_base_->SpaceUsedExcludingSelf());
  123. }
  124. TEST_F(MapFieldBasePrimitiveTest, GetRepeatedField) {
  125. const RepeatedPtrField<Message>& repeated =
  126. reinterpret_cast<const RepeatedPtrField<Message>&>(
  127. map_field_base_->GetRepeatedField());
  128. EXPECT_EQ(2, repeated.size());
  129. for (int i = 0; i < repeated.size(); i++) {
  130. const Message& message = repeated.Get(i);
  131. int key = message.GetReflection()->GetInt32(message, key_descriptor_);
  132. int value = message.GetReflection()->GetInt32(message, value_descriptor_);
  133. EXPECT_EQ(value, initial_value_map_[key]);
  134. }
  135. }
  136. TEST_F(MapFieldBasePrimitiveTest, MutableRepeatedField) {
  137. RepeatedPtrField<Message>* repeated =
  138. reinterpret_cast<RepeatedPtrField<Message>*>(
  139. map_field_base_->MutableRepeatedField());
  140. EXPECT_EQ(2, repeated->size());
  141. for (int i = 0; i < repeated->size(); i++) {
  142. const Message& message = repeated->Get(i);
  143. int key = message.GetReflection()->GetInt32(message, key_descriptor_);
  144. int value = message.GetReflection()->GetInt32(message, value_descriptor_);
  145. EXPECT_EQ(value, initial_value_map_[key]);
  146. }
  147. }
  148. TEST_F(MapFieldBasePrimitiveTest, Arena) {
  149. // Allocate a large initial block to avoid mallocs during hooked test.
  150. std::vector<char> arena_block(128 * 1024);
  151. ArenaOptions options;
  152. options.initial_block = &arena_block[0];
  153. options.initial_block_size = arena_block.size();
  154. Arena arena(options);
  155. {
  156. // TODO(liujisi): Re-write the test to ensure the memory for the map and
  157. // repeated fields are allocated from arenas.
  158. // NoHeapChecker no_heap;
  159. MapFieldType* map_field = Arena::CreateMessage<MapFieldType>(&arena);
  160. // Set content in map
  161. (*map_field->MutableMap())[100] = 101;
  162. // Trigger conversion to repeated field.
  163. map_field->GetRepeatedField();
  164. }
  165. {
  166. // TODO(liujisi): Re-write the test to ensure the memory for the map and
  167. // repeated fields are allocated from arenas.
  168. // NoHeapChecker no_heap;
  169. MapFieldBaseStub* map_field =
  170. Arena::CreateMessage<MapFieldBaseStub>(&arena);
  171. // Trigger conversion to repeated field.
  172. EXPECT_TRUE(map_field->MutableRepeatedField() != NULL);
  173. }
  174. }
  175. namespace {
  176. enum State { CLEAN, MAP_DIRTY, REPEATED_DIRTY };
  177. } // anonymous namespace
  178. class MapFieldStateTest : public testing::TestWithParam<State> {
  179. public:
  180. protected:
  181. typedef unittest::TestMap_MapInt32Int32Entry_DoNotUse EntryType;
  182. typedef MapField<EntryType, int32, int32, WireFormatLite::TYPE_INT32,
  183. WireFormatLite::TYPE_INT32, false>
  184. MapFieldType;
  185. MapFieldStateTest() : state_(GetParam()) {
  186. // Build map field
  187. map_field_.reset(new MapFieldType());
  188. map_field_base_ = map_field_.get();
  189. Expect(map_field_.get(), MAP_DIRTY, 0, 0, true);
  190. switch (state_) {
  191. case CLEAN:
  192. AddOneStillClean(map_field_.get());
  193. break;
  194. case MAP_DIRTY:
  195. MakeMapDirty(map_field_.get());
  196. break;
  197. case REPEATED_DIRTY:
  198. MakeRepeatedDirty(map_field_.get());
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. void AddOneStillClean(MapFieldType* map_field) {
  205. MapFieldBase* map_field_base = map_field;
  206. Map<int32, int32>* map = map_field->MutableMap();
  207. (*map)[0] = 0;
  208. map_field_base->GetRepeatedField();
  209. Expect(map_field, CLEAN, 1, 1, false);
  210. }
  211. void MakeMapDirty(MapFieldType* map_field) {
  212. Map<int32, int32>* map = map_field->MutableMap();
  213. (*map)[0] = 0;
  214. Expect(map_field, MAP_DIRTY, 1, 0, true);
  215. }
  216. void MakeRepeatedDirty(MapFieldType* map_field) {
  217. MakeMapDirty(map_field);
  218. MapFieldBase* map_field_base = map_field;
  219. map_field_base->MutableRepeatedField();
  220. // We use MutableMap on impl_ because we don't want to disturb the syncing
  221. Map<int32, int32>* map = map_field->impl_.MutableMap();
  222. map->clear();
  223. Expect(map_field, REPEATED_DIRTY, 0, 1, false);
  224. }
  225. void Expect(MapFieldType* map_field, State state, int map_size,
  226. int repeated_size, bool is_repeated_null) {
  227. MapFieldBase* map_field_base = map_field;
  228. MapFieldBaseStub* stub =
  229. reinterpret_cast<MapFieldBaseStub*>(map_field_base);
  230. // We use MutableMap on impl_ because we don't want to disturb the syncing
  231. Map<int32, int32>* map = map_field->impl_.MutableMap();
  232. RepeatedPtrField<Message>* repeated_field = stub->InternalRepeatedField();
  233. switch (state) {
  234. case MAP_DIRTY:
  235. EXPECT_FALSE(stub->IsMapClean());
  236. EXPECT_TRUE(stub->IsRepeatedClean());
  237. break;
  238. case REPEATED_DIRTY:
  239. EXPECT_TRUE(stub->IsMapClean());
  240. EXPECT_FALSE(stub->IsRepeatedClean());
  241. break;
  242. case CLEAN:
  243. EXPECT_TRUE(stub->IsMapClean());
  244. EXPECT_TRUE(stub->IsRepeatedClean());
  245. break;
  246. default:
  247. FAIL();
  248. }
  249. EXPECT_EQ(map_size, map->size());
  250. if (is_repeated_null) {
  251. EXPECT_TRUE(repeated_field == NULL);
  252. } else {
  253. if (repeated_field == nullptr) {
  254. EXPECT_EQ(repeated_size, 0);
  255. } else {
  256. EXPECT_EQ(repeated_size, repeated_field->size());
  257. }
  258. }
  259. }
  260. std::unique_ptr<MapFieldType> map_field_;
  261. MapFieldBase* map_field_base_;
  262. State state_;
  263. };
  264. INSTANTIATE_TEST_SUITE_P(MapFieldStateTestInstance, MapFieldStateTest,
  265. ::testing::Values(CLEAN, MAP_DIRTY, REPEATED_DIRTY));
  266. TEST_P(MapFieldStateTest, GetMap) {
  267. map_field_->GetMap();
  268. if (state_ != MAP_DIRTY) {
  269. Expect(map_field_.get(), CLEAN, 1, 1, false);
  270. } else {
  271. Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
  272. }
  273. }
  274. TEST_P(MapFieldStateTest, MutableMap) {
  275. map_field_->MutableMap();
  276. if (state_ != MAP_DIRTY) {
  277. Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
  278. } else {
  279. Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
  280. }
  281. }
  282. TEST_P(MapFieldStateTest, MergeFromClean) {
  283. MapFieldType other;
  284. AddOneStillClean(&other);
  285. map_field_->MergeFrom(other);
  286. if (state_ != MAP_DIRTY) {
  287. Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
  288. } else {
  289. Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
  290. }
  291. Expect(&other, CLEAN, 1, 1, false);
  292. }
  293. TEST_P(MapFieldStateTest, MergeFromMapDirty) {
  294. MapFieldType other;
  295. MakeMapDirty(&other);
  296. map_field_->MergeFrom(other);
  297. if (state_ != MAP_DIRTY) {
  298. Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
  299. } else {
  300. Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
  301. }
  302. Expect(&other, MAP_DIRTY, 1, 0, true);
  303. }
  304. TEST_P(MapFieldStateTest, MergeFromRepeatedDirty) {
  305. MapFieldType other;
  306. MakeRepeatedDirty(&other);
  307. map_field_->MergeFrom(other);
  308. if (state_ != MAP_DIRTY) {
  309. Expect(map_field_.get(), MAP_DIRTY, 1, 1, false);
  310. } else {
  311. Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
  312. }
  313. Expect(&other, CLEAN, 1, 1, false);
  314. }
  315. TEST_P(MapFieldStateTest, SwapClean) {
  316. MapFieldType other;
  317. AddOneStillClean(&other);
  318. map_field_->Swap(&other);
  319. Expect(map_field_.get(), CLEAN, 1, 1, false);
  320. switch (state_) {
  321. case CLEAN:
  322. Expect(&other, CLEAN, 1, 1, false);
  323. break;
  324. case MAP_DIRTY:
  325. Expect(&other, MAP_DIRTY, 1, 0, true);
  326. break;
  327. case REPEATED_DIRTY:
  328. Expect(&other, REPEATED_DIRTY, 0, 1, false);
  329. break;
  330. default:
  331. break;
  332. }
  333. }
  334. TEST_P(MapFieldStateTest, SwapMapDirty) {
  335. MapFieldType other;
  336. MakeMapDirty(&other);
  337. map_field_->Swap(&other);
  338. Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
  339. switch (state_) {
  340. case CLEAN:
  341. Expect(&other, CLEAN, 1, 1, false);
  342. break;
  343. case MAP_DIRTY:
  344. Expect(&other, MAP_DIRTY, 1, 0, true);
  345. break;
  346. case REPEATED_DIRTY:
  347. Expect(&other, REPEATED_DIRTY, 0, 1, false);
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. TEST_P(MapFieldStateTest, SwapRepeatedDirty) {
  354. MapFieldType other;
  355. MakeRepeatedDirty(&other);
  356. map_field_->Swap(&other);
  357. Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
  358. switch (state_) {
  359. case CLEAN:
  360. Expect(&other, CLEAN, 1, 1, false);
  361. break;
  362. case MAP_DIRTY:
  363. Expect(&other, MAP_DIRTY, 1, 0, true);
  364. break;
  365. case REPEATED_DIRTY:
  366. Expect(&other, REPEATED_DIRTY, 0, 1, false);
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372. TEST_P(MapFieldStateTest, Clear) {
  373. map_field_->Clear();
  374. Expect(map_field_.get(), MAP_DIRTY, 0, 0, false);
  375. }
  376. TEST_P(MapFieldStateTest, SpaceUsedExcludingSelf) {
  377. map_field_base_->SpaceUsedExcludingSelf();
  378. switch (state_) {
  379. case CLEAN:
  380. Expect(map_field_.get(), CLEAN, 1, 1, false);
  381. break;
  382. case MAP_DIRTY:
  383. Expect(map_field_.get(), MAP_DIRTY, 1, 0, true);
  384. break;
  385. case REPEATED_DIRTY:
  386. Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. TEST_P(MapFieldStateTest, GetMapField) {
  393. map_field_base_->GetRepeatedField();
  394. if (state_ != REPEATED_DIRTY) {
  395. Expect(map_field_.get(), CLEAN, 1, 1, false);
  396. } else {
  397. Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
  398. }
  399. }
  400. TEST_P(MapFieldStateTest, MutableMapField) {
  401. map_field_base_->MutableRepeatedField();
  402. if (state_ != REPEATED_DIRTY) {
  403. Expect(map_field_.get(), REPEATED_DIRTY, 1, 1, false);
  404. } else {
  405. Expect(map_field_.get(), REPEATED_DIRTY, 0, 1, false);
  406. }
  407. }
  408. } // namespace internal
  409. } // namespace protobuf
  410. } // namespace google