诸暨麻将添加redis
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1192 Zeilen
40 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. // Author: kenton@google.com (Kenton Varda)
  31. #include <iostream>
  32. #include <string>
  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_lite_test_util.h>
  37. #include <google/protobuf/map_lite_unittest.pb.h>
  38. #include <google/protobuf/test_util_lite.h>
  39. #include <google/protobuf/unittest_lite.pb.h>
  40. #include <google/protobuf/io/coded_stream.h>
  41. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  42. #include <google/protobuf/wire_format_lite.h>
  43. #include <gtest/gtest.h>
  44. #include <google/protobuf/stubs/strutil.h>
  45. namespace google {
  46. namespace protobuf {
  47. // Helper methods to test parsing merge behavior.
  48. void ExpectMessageMerged(const unittest::TestAllTypesLite& message) {
  49. EXPECT_EQ(message.optional_int32(), 3);
  50. EXPECT_EQ(message.optional_int64(), 2);
  51. EXPECT_EQ(message.optional_string(), "hello");
  52. }
  53. void AssignParsingMergeMessages(unittest::TestAllTypesLite* msg1,
  54. unittest::TestAllTypesLite* msg2,
  55. unittest::TestAllTypesLite* msg3) {
  56. msg1->set_optional_int32(1);
  57. msg2->set_optional_int64(2);
  58. msg3->set_optional_int32(3);
  59. msg3->set_optional_string("hello");
  60. }
  61. void SetAllTypesInEmptyMessageUnknownFields(
  62. unittest::TestEmptyMessageLite* empty_message) {
  63. protobuf_unittest::TestAllTypesLite message;
  64. TestUtilLite::ExpectClear(message);
  65. TestUtilLite::SetAllFields(&message);
  66. std::string data = message.SerializeAsString();
  67. empty_message->ParseFromString(data);
  68. }
  69. void SetSomeTypesInEmptyMessageUnknownFields(
  70. unittest::TestEmptyMessageLite* empty_message) {
  71. protobuf_unittest::TestAllTypesLite message;
  72. TestUtilLite::ExpectClear(message);
  73. message.set_optional_int32(101);
  74. message.set_optional_int64(102);
  75. message.set_optional_uint32(103);
  76. message.set_optional_uint64(104);
  77. std::string data = message.SerializeAsString();
  78. empty_message->ParseFromString(data);
  79. }
  80. TEST(Lite, AllLite1) {
  81. std::string data;
  82. {
  83. protobuf_unittest::TestAllTypesLite message, message2, message3;
  84. TestUtilLite::ExpectClear(message);
  85. TestUtilLite::SetAllFields(&message);
  86. message2.CopyFrom(message);
  87. data = message.SerializeAsString();
  88. message3.ParseFromString(data);
  89. TestUtilLite::ExpectAllFieldsSet(message);
  90. TestUtilLite::ExpectAllFieldsSet(message2);
  91. TestUtilLite::ExpectAllFieldsSet(message3);
  92. TestUtilLite::ModifyRepeatedFields(&message);
  93. TestUtilLite::ExpectRepeatedFieldsModified(message);
  94. message.Clear();
  95. TestUtilLite::ExpectClear(message);
  96. }
  97. }
  98. TEST(Lite, AllLite2) {
  99. std::string data;
  100. {
  101. protobuf_unittest::TestAllExtensionsLite message, message2, message3;
  102. TestUtilLite::ExpectExtensionsClear(message);
  103. TestUtilLite::SetAllExtensions(&message);
  104. message2.CopyFrom(message);
  105. std::string extensions_data = message.SerializeAsString();
  106. message3.ParseFromString(extensions_data);
  107. TestUtilLite::ExpectAllExtensionsSet(message);
  108. TestUtilLite::ExpectAllExtensionsSet(message2);
  109. TestUtilLite::ExpectAllExtensionsSet(message3);
  110. TestUtilLite::ModifyRepeatedExtensions(&message);
  111. TestUtilLite::ExpectRepeatedExtensionsModified(message);
  112. message.Clear();
  113. TestUtilLite::ExpectExtensionsClear(message);
  114. }
  115. }
  116. TEST(Lite, AllLite3) {
  117. std::string data, packed_data;
  118. {
  119. protobuf_unittest::TestPackedTypesLite message, message2, message3;
  120. TestUtilLite::ExpectPackedClear(message);
  121. TestUtilLite::SetPackedFields(&message);
  122. message2.CopyFrom(message);
  123. packed_data = message.SerializeAsString();
  124. message3.ParseFromString(packed_data);
  125. TestUtilLite::ExpectPackedFieldsSet(message);
  126. TestUtilLite::ExpectPackedFieldsSet(message2);
  127. TestUtilLite::ExpectPackedFieldsSet(message3);
  128. TestUtilLite::ModifyPackedFields(&message);
  129. TestUtilLite::ExpectPackedFieldsModified(message);
  130. message.Clear();
  131. TestUtilLite::ExpectPackedClear(message);
  132. }
  133. {
  134. protobuf_unittest::TestPackedExtensionsLite message, message2, message3;
  135. TestUtilLite::ExpectPackedExtensionsClear(message);
  136. TestUtilLite::SetPackedExtensions(&message);
  137. message2.CopyFrom(message);
  138. std::string packed_extensions_data = message.SerializeAsString();
  139. EXPECT_EQ(packed_extensions_data, packed_data);
  140. message3.ParseFromString(packed_extensions_data);
  141. TestUtilLite::ExpectPackedExtensionsSet(message);
  142. TestUtilLite::ExpectPackedExtensionsSet(message2);
  143. TestUtilLite::ExpectPackedExtensionsSet(message3);
  144. TestUtilLite::ModifyPackedExtensions(&message);
  145. TestUtilLite::ExpectPackedExtensionsModified(message);
  146. message.Clear();
  147. TestUtilLite::ExpectPackedExtensionsClear(message);
  148. }
  149. }
  150. TEST(Lite, AllLite5) {
  151. std::string data;
  152. {
  153. // Test that if an optional or required message/group field appears multiple
  154. // times in the input, they need to be merged.
  155. unittest::TestParsingMergeLite::RepeatedFieldsGenerator generator;
  156. unittest::TestAllTypesLite* msg1;
  157. unittest::TestAllTypesLite* msg2;
  158. unittest::TestAllTypesLite* msg3;
  159. #define ASSIGN_REPEATED_FIELD(FIELD) \
  160. msg1 = generator.add_##FIELD(); \
  161. msg2 = generator.add_##FIELD(); \
  162. msg3 = generator.add_##FIELD(); \
  163. AssignParsingMergeMessages(msg1, msg2, msg3)
  164. ASSIGN_REPEATED_FIELD(field1);
  165. ASSIGN_REPEATED_FIELD(field2);
  166. ASSIGN_REPEATED_FIELD(field3);
  167. ASSIGN_REPEATED_FIELD(ext1);
  168. ASSIGN_REPEATED_FIELD(ext2);
  169. #undef ASSIGN_REPEATED_FIELD
  170. #define ASSIGN_REPEATED_GROUP(FIELD) \
  171. msg1 = generator.add_##FIELD()->mutable_field1(); \
  172. msg2 = generator.add_##FIELD()->mutable_field1(); \
  173. msg3 = generator.add_##FIELD()->mutable_field1(); \
  174. AssignParsingMergeMessages(msg1, msg2, msg3)
  175. ASSIGN_REPEATED_GROUP(group1);
  176. ASSIGN_REPEATED_GROUP(group2);
  177. #undef ASSIGN_REPEATED_GROUP
  178. std::string buffer;
  179. generator.SerializeToString(&buffer);
  180. unittest::TestParsingMergeLite parsing_merge;
  181. parsing_merge.ParseFromString(buffer);
  182. // Required and optional fields should be merged.
  183. ExpectMessageMerged(parsing_merge.required_all_types());
  184. ExpectMessageMerged(parsing_merge.optional_all_types());
  185. ExpectMessageMerged(
  186. parsing_merge.optionalgroup().optional_group_all_types());
  187. ExpectMessageMerged(parsing_merge.GetExtension(
  188. unittest::TestParsingMergeLite::optional_ext));
  189. // Repeated fields should not be merged.
  190. EXPECT_EQ(parsing_merge.repeated_all_types_size(), 3);
  191. EXPECT_EQ(parsing_merge.repeatedgroup_size(), 3);
  192. EXPECT_EQ(parsing_merge.ExtensionSize(
  193. unittest::TestParsingMergeLite::repeated_ext),
  194. 3);
  195. }
  196. }
  197. TEST(Lite, AllLite6) {
  198. std::string data;
  199. // Test unknown fields support for lite messages.
  200. {
  201. protobuf_unittest::TestAllTypesLite message, message2;
  202. protobuf_unittest::TestEmptyMessageLite empty_message;
  203. TestUtilLite::ExpectClear(message);
  204. TestUtilLite::SetAllFields(&message);
  205. data = message.SerializeAsString();
  206. ASSERT_TRUE(empty_message.ParseFromString(data));
  207. data.clear();
  208. data = empty_message.SerializeAsString();
  209. EXPECT_TRUE(message2.ParseFromString(data));
  210. data = message2.SerializeAsString();
  211. TestUtilLite::ExpectAllFieldsSet(message2);
  212. message.Clear();
  213. TestUtilLite::ExpectClear(message);
  214. }
  215. }
  216. TEST(Lite, AllLite7) {
  217. std::string data;
  218. {
  219. protobuf_unittest::TestAllExtensionsLite message, message2;
  220. protobuf_unittest::TestEmptyMessageLite empty_message;
  221. TestUtilLite::ExpectExtensionsClear(message);
  222. TestUtilLite::SetAllExtensions(&message);
  223. data = message.SerializeAsString();
  224. empty_message.ParseFromString(data);
  225. data.clear();
  226. data = empty_message.SerializeAsString();
  227. message2.ParseFromString(data);
  228. data = message2.SerializeAsString();
  229. TestUtilLite::ExpectAllExtensionsSet(message2);
  230. message.Clear();
  231. TestUtilLite::ExpectExtensionsClear(message);
  232. }
  233. }
  234. TEST(Lite, AllLite8) {
  235. std::string data;
  236. {
  237. protobuf_unittest::TestPackedTypesLite message, message2;
  238. protobuf_unittest::TestEmptyMessageLite empty_message;
  239. TestUtilLite::ExpectPackedClear(message);
  240. TestUtilLite::SetPackedFields(&message);
  241. data = message.SerializeAsString();
  242. empty_message.ParseFromString(data);
  243. data.clear();
  244. data = empty_message.SerializeAsString();
  245. message2.ParseFromString(data);
  246. data = message2.SerializeAsString();
  247. TestUtilLite::ExpectPackedFieldsSet(message2);
  248. message.Clear();
  249. TestUtilLite::ExpectPackedClear(message);
  250. }
  251. }
  252. TEST(Lite, AllLite9) {
  253. std::string data;
  254. {
  255. protobuf_unittest::TestPackedExtensionsLite message, message2;
  256. protobuf_unittest::TestEmptyMessageLite empty_message;
  257. TestUtilLite::ExpectPackedExtensionsClear(message);
  258. TestUtilLite::SetPackedExtensions(&message);
  259. data = message.SerializeAsString();
  260. empty_message.ParseFromString(data);
  261. data.clear();
  262. data = empty_message.SerializeAsString();
  263. message2.ParseFromString(data);
  264. data = message2.SerializeAsString();
  265. TestUtilLite::ExpectPackedExtensionsSet(message2);
  266. message.Clear();
  267. TestUtilLite::ExpectPackedExtensionsClear(message);
  268. }
  269. }
  270. TEST(Lite, AllLite10) {
  271. std::string data;
  272. {
  273. // Test Unknown fields swap
  274. protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2;
  275. SetAllTypesInEmptyMessageUnknownFields(&empty_message);
  276. SetSomeTypesInEmptyMessageUnknownFields(&empty_message2);
  277. data = empty_message.SerializeAsString();
  278. std::string data2 = empty_message2.SerializeAsString();
  279. empty_message.Swap(&empty_message2);
  280. EXPECT_EQ(data, empty_message2.SerializeAsString());
  281. EXPECT_EQ(data2, empty_message.SerializeAsString());
  282. }
  283. }
  284. TEST(Lite, AllLite11) {
  285. std::string data;
  286. {
  287. // Test unknown fields swap with self
  288. protobuf_unittest::TestEmptyMessageLite empty_message;
  289. SetAllTypesInEmptyMessageUnknownFields(&empty_message);
  290. data = empty_message.SerializeAsString();
  291. empty_message.Swap(&empty_message);
  292. EXPECT_EQ(data, empty_message.SerializeAsString());
  293. }
  294. }
  295. TEST(Lite, AllLite12) {
  296. std::string data;
  297. {
  298. // Test MergeFrom with unknown fields
  299. protobuf_unittest::TestAllTypesLite message, message2;
  300. protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2;
  301. message.set_optional_int32(101);
  302. message.add_repeated_int32(201);
  303. message.set_optional_nested_enum(unittest::TestAllTypesLite::BAZ);
  304. message2.set_optional_int64(102);
  305. message2.add_repeated_int64(202);
  306. message2.set_optional_foreign_enum(unittest::FOREIGN_LITE_BAZ);
  307. data = message.SerializeAsString();
  308. empty_message.ParseFromString(data);
  309. data = message2.SerializeAsString();
  310. empty_message2.ParseFromString(data);
  311. message.MergeFrom(message2);
  312. empty_message.MergeFrom(empty_message2);
  313. data = empty_message.SerializeAsString();
  314. message2.ParseFromString(data);
  315. // We do not compare the serialized output of a normal message and a lite
  316. // message because the order of fields do not match. We convert lite message
  317. // back into normal message, then compare.
  318. EXPECT_EQ(message.SerializeAsString(), message2.SerializeAsString());
  319. }
  320. }
  321. TEST(Lite, AllLite13) {
  322. std::string data;
  323. {
  324. // Test unknown enum value
  325. protobuf_unittest::TestAllTypesLite message;
  326. std::string buffer;
  327. {
  328. io::StringOutputStream output_stream(&buffer);
  329. io::CodedOutputStream coded_output(&output_stream);
  330. internal::WireFormatLite::WriteTag(
  331. protobuf_unittest::TestAllTypesLite::kOptionalNestedEnumFieldNumber,
  332. internal::WireFormatLite::WIRETYPE_VARINT, &coded_output);
  333. coded_output.WriteVarint32(10);
  334. internal::WireFormatLite::WriteTag(
  335. protobuf_unittest::TestAllTypesLite::kRepeatedNestedEnumFieldNumber,
  336. internal::WireFormatLite::WIRETYPE_VARINT, &coded_output);
  337. coded_output.WriteVarint32(20);
  338. }
  339. message.ParseFromString(buffer);
  340. data = message.SerializeAsString();
  341. EXPECT_EQ(data, buffer);
  342. }
  343. }
  344. TEST(Lite, AllLite14) {
  345. std::string data;
  346. {
  347. // Test Clear with unknown fields
  348. protobuf_unittest::TestEmptyMessageLite empty_message;
  349. SetAllTypesInEmptyMessageUnknownFields(&empty_message);
  350. empty_message.Clear();
  351. EXPECT_EQ(0, empty_message.unknown_fields().size());
  352. }
  353. }
  354. // Tests for map lite =============================================
  355. TEST(Lite, AllLite15) {
  356. std::string data;
  357. {
  358. // Accessors
  359. protobuf_unittest::TestMapLite message;
  360. MapLiteTestUtil::SetMapFields(&message);
  361. MapLiteTestUtil::ExpectMapFieldsSet(message);
  362. MapLiteTestUtil::ModifyMapFields(&message);
  363. MapLiteTestUtil::ExpectMapFieldsModified(message);
  364. }
  365. }
  366. TEST(Lite, AllLite16) {
  367. std::string data;
  368. {
  369. // SetMapFieldsInitialized
  370. protobuf_unittest::TestMapLite message;
  371. MapLiteTestUtil::SetMapFieldsInitialized(&message);
  372. MapLiteTestUtil::ExpectMapFieldsSetInitialized(message);
  373. }
  374. }
  375. TEST(Lite, AllLite17) {
  376. std::string data;
  377. {
  378. // Clear
  379. protobuf_unittest::TestMapLite message;
  380. MapLiteTestUtil::SetMapFields(&message);
  381. message.Clear();
  382. MapLiteTestUtil::ExpectClear(message);
  383. }
  384. }
  385. TEST(Lite, AllLite18) {
  386. std::string data;
  387. {
  388. // ClearMessageMap
  389. protobuf_unittest::TestMessageMapLite message;
  390. // Creates a TestAllTypes with default value
  391. TestUtilLite::ExpectClear((*message.mutable_map_int32_message())[0]);
  392. }
  393. }
  394. TEST(Lite, AllLite19) {
  395. std::string data;
  396. {
  397. // CopyFrom
  398. protobuf_unittest::TestMapLite message1, message2;
  399. MapLiteTestUtil::SetMapFields(&message1);
  400. message2.CopyFrom(message1);
  401. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  402. // Copying from self should be a no-op.
  403. message2.CopyFrom(message2);
  404. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  405. }
  406. }
  407. TEST(Lite, AllLite20) {
  408. std::string data;
  409. {
  410. // CopyFromMessageMap
  411. protobuf_unittest::TestMessageMapLite message1, message2;
  412. (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
  413. (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
  414. message1.CopyFrom(message2);
  415. // Checks repeated field is overwritten.
  416. EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
  417. EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
  418. }
  419. }
  420. TEST(Lite, AllLite21) {
  421. std::string data;
  422. {
  423. // SwapWithEmpty
  424. protobuf_unittest::TestMapLite message1, message2;
  425. MapLiteTestUtil::SetMapFields(&message1);
  426. MapLiteTestUtil::ExpectMapFieldsSet(message1);
  427. MapLiteTestUtil::ExpectClear(message2);
  428. message1.Swap(&message2);
  429. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  430. MapLiteTestUtil::ExpectClear(message1);
  431. }
  432. }
  433. TEST(Lite, AllLite22) {
  434. std::string data;
  435. {
  436. // SwapWithSelf
  437. protobuf_unittest::TestMapLite message;
  438. MapLiteTestUtil::SetMapFields(&message);
  439. MapLiteTestUtil::ExpectMapFieldsSet(message);
  440. message.Swap(&message);
  441. MapLiteTestUtil::ExpectMapFieldsSet(message);
  442. }
  443. }
  444. TEST(Lite, AllLite23) {
  445. std::string data;
  446. {
  447. // SwapWithOther
  448. protobuf_unittest::TestMapLite message1, message2;
  449. MapLiteTestUtil::SetMapFields(&message1);
  450. MapLiteTestUtil::SetMapFields(&message2);
  451. MapLiteTestUtil::ModifyMapFields(&message2);
  452. message1.Swap(&message2);
  453. MapLiteTestUtil::ExpectMapFieldsModified(message1);
  454. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  455. }
  456. }
  457. TEST(Lite, AllLite24) {
  458. std::string data;
  459. {
  460. // CopyConstructor
  461. protobuf_unittest::TestMapLite message1;
  462. MapLiteTestUtil::SetMapFields(&message1);
  463. protobuf_unittest::TestMapLite message2(message1);
  464. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  465. }
  466. }
  467. TEST(Lite, AllLite25) {
  468. std::string data;
  469. {
  470. // CopyAssignmentOperator
  471. protobuf_unittest::TestMapLite message1;
  472. MapLiteTestUtil::SetMapFields(&message1);
  473. protobuf_unittest::TestMapLite message2;
  474. message2 = message1;
  475. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  476. // Make sure that self-assignment does something sane.
  477. message2.operator=(message2);
  478. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  479. }
  480. }
  481. TEST(Lite, AllLite26) {
  482. std::string data;
  483. {
  484. // NonEmptyMergeFrom
  485. protobuf_unittest::TestMapLite message1, message2;
  486. MapLiteTestUtil::SetMapFields(&message1);
  487. // This field will test merging into an empty spot.
  488. (*message2.mutable_map_int32_int32())[1] = 1;
  489. message1.mutable_map_int32_int32()->erase(1);
  490. // This tests overwriting.
  491. (*message2.mutable_map_int32_double())[1] = 1;
  492. (*message1.mutable_map_int32_double())[1] = 2;
  493. message1.MergeFrom(message2);
  494. MapLiteTestUtil::ExpectMapFieldsSet(message1);
  495. }
  496. }
  497. TEST(Lite, AllLite27) {
  498. std::string data;
  499. {
  500. // MergeFromMessageMap
  501. protobuf_unittest::TestMessageMapLite message1, message2;
  502. (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
  503. (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
  504. message1.MergeFrom(message2);
  505. // Checks repeated field is overwritten.
  506. EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
  507. EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
  508. }
  509. }
  510. TEST(Lite, AllLite28) {
  511. std::string data;
  512. {
  513. // Test the generated SerializeWithCachedSizesToArray()
  514. protobuf_unittest::TestMapLite message1, message2;
  515. std::string data;
  516. MapLiteTestUtil::SetMapFields(&message1);
  517. int size = message1.ByteSize();
  518. data.resize(size);
  519. ::google::protobuf::uint8* start = reinterpret_cast<::google::protobuf::uint8*>(::google::protobuf::string_as_array(&data));
  520. ::google::protobuf::uint8* end = message1.SerializeWithCachedSizesToArray(start);
  521. EXPECT_EQ(size, end - start);
  522. EXPECT_TRUE(message2.ParseFromString(data));
  523. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  524. }
  525. }
  526. TEST(Lite, AllLite29) {
  527. std::string data;
  528. {
  529. // Test the generated SerializeWithCachedSizes()
  530. protobuf_unittest::TestMapLite message1, message2;
  531. MapLiteTestUtil::SetMapFields(&message1);
  532. int size = message1.ByteSize();
  533. std::string data;
  534. data.resize(size);
  535. {
  536. // Allow the output stream to buffer only one byte at a time.
  537. io::ArrayOutputStream array_stream(::google::protobuf::string_as_array(&data), size, 1);
  538. io::CodedOutputStream output_stream(&array_stream);
  539. message1.SerializeWithCachedSizes(&output_stream);
  540. EXPECT_FALSE(output_stream.HadError());
  541. EXPECT_EQ(size, output_stream.ByteCount());
  542. }
  543. EXPECT_TRUE(message2.ParseFromString(data));
  544. MapLiteTestUtil::ExpectMapFieldsSet(message2);
  545. }
  546. }
  547. TEST(Lite, AllLite32) {
  548. std::string data;
  549. {
  550. // Proto2UnknownEnum
  551. protobuf_unittest::TestEnumMapPlusExtraLite from;
  552. (*from.mutable_known_map_field())[0] =
  553. protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE;
  554. (*from.mutable_unknown_map_field())[0] =
  555. protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE;
  556. std::string data;
  557. from.SerializeToString(&data);
  558. protobuf_unittest::TestEnumMapLite to;
  559. EXPECT_TRUE(to.ParseFromString(data));
  560. EXPECT_EQ(0, to.unknown_map_field().size());
  561. EXPECT_FALSE(to.mutable_unknown_fields()->empty());
  562. ASSERT_EQ(1, to.known_map_field().size());
  563. EXPECT_EQ(protobuf_unittest::PROTO2_MAP_ENUM_FOO_LITE,
  564. to.known_map_field().at(0));
  565. data.clear();
  566. from.Clear();
  567. to.SerializeToString(&data);
  568. EXPECT_TRUE(from.ParseFromString(data));
  569. ASSERT_EQ(1, from.known_map_field().size());
  570. EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE,
  571. from.known_map_field().at(0));
  572. ASSERT_EQ(1, from.unknown_map_field().size());
  573. EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE,
  574. from.unknown_map_field().at(0));
  575. }
  576. }
  577. TEST(Lite, AllLite33) {
  578. std::string data;
  579. {
  580. // StandardWireFormat
  581. protobuf_unittest::TestMapLite message;
  582. std::string data = "\x0A\x04\x08\x01\x10\x01";
  583. EXPECT_TRUE(message.ParseFromString(data));
  584. ASSERT_EQ(1, message.map_int32_int32().size());
  585. EXPECT_EQ(1, message.map_int32_int32().at(1));
  586. }
  587. }
  588. TEST(Lite, AllLite34) {
  589. std::string data;
  590. {
  591. // UnorderedWireFormat
  592. protobuf_unittest::TestMapLite message;
  593. // put value before key in wire format
  594. std::string data = "\x0A\x04\x10\x01\x08\x02";
  595. EXPECT_TRUE(message.ParseFromString(data));
  596. ASSERT_EQ(1, message.map_int32_int32().size());
  597. ASSERT_NE(message.map_int32_int32().find(2),
  598. message.map_int32_int32().end());
  599. EXPECT_EQ(1, message.map_int32_int32().at(2));
  600. }
  601. }
  602. TEST(Lite, AllLite35) {
  603. std::string data;
  604. {
  605. // DuplicatedKeyWireFormat
  606. protobuf_unittest::TestMapLite message;
  607. // Two key fields in wire format
  608. std::string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
  609. EXPECT_TRUE(message.ParseFromString(data));
  610. ASSERT_EQ(1, message.map_int32_int32().size());
  611. EXPECT_EQ(1, message.map_int32_int32().at(2));
  612. }
  613. }
  614. TEST(Lite, AllLite36) {
  615. std::string data;
  616. {
  617. // DuplicatedValueWireFormat
  618. protobuf_unittest::TestMapLite message;
  619. // Two value fields in wire format
  620. std::string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
  621. EXPECT_TRUE(message.ParseFromString(data));
  622. ASSERT_EQ(1, message.map_int32_int32().size());
  623. EXPECT_EQ(2, message.map_int32_int32().at(1));
  624. }
  625. }
  626. TEST(Lite, AllLite37) {
  627. std::string data;
  628. {
  629. // MissedKeyWireFormat
  630. protobuf_unittest::TestMapLite message;
  631. // No key field in wire format
  632. std::string data = "\x0A\x02\x10\x01";
  633. EXPECT_TRUE(message.ParseFromString(data));
  634. ASSERT_EQ(1, message.map_int32_int32().size());
  635. ASSERT_NE(message.map_int32_int32().find(0),
  636. message.map_int32_int32().end());
  637. EXPECT_EQ(1, message.map_int32_int32().at(0));
  638. }
  639. }
  640. TEST(Lite, AllLite38) {
  641. std::string data;
  642. {
  643. // MissedValueWireFormat
  644. protobuf_unittest::TestMapLite message;
  645. // No value field in wire format
  646. std::string data = "\x0A\x02\x08\x01";
  647. EXPECT_TRUE(message.ParseFromString(data));
  648. ASSERT_EQ(1, message.map_int32_int32().size());
  649. ASSERT_NE(message.map_int32_int32().find(1),
  650. message.map_int32_int32().end());
  651. EXPECT_EQ(0, message.map_int32_int32().at(1));
  652. }
  653. }
  654. TEST(Lite, AllLite39) {
  655. std::string data;
  656. {
  657. // UnknownFieldWireFormat
  658. protobuf_unittest::TestMapLite message;
  659. // Unknown field in wire format
  660. std::string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
  661. EXPECT_TRUE(message.ParseFromString(data));
  662. ASSERT_EQ(1, message.map_int32_int32().size());
  663. EXPECT_EQ(3, message.map_int32_int32().at(2));
  664. }
  665. }
  666. TEST(Lite, AllLite40) {
  667. std::string data;
  668. {
  669. // CorruptedWireFormat
  670. protobuf_unittest::TestMapLite message;
  671. // corrupted data in wire format
  672. std::string data = "\x0A\x06\x08\x02\x11\x03";
  673. EXPECT_FALSE(message.ParseFromString(data));
  674. }
  675. }
  676. TEST(Lite, AllLite41) {
  677. std::string data;
  678. {
  679. // IsInitialized
  680. protobuf_unittest::TestRequiredMessageMapLite map_message;
  681. // Add an uninitialized message.
  682. (*map_message.mutable_map_field())[0];
  683. EXPECT_FALSE(map_message.IsInitialized());
  684. // Initialize uninitialized message
  685. (*map_message.mutable_map_field())[0].set_a(0);
  686. (*map_message.mutable_map_field())[0].set_b(0);
  687. (*map_message.mutable_map_field())[0].set_c(0);
  688. EXPECT_TRUE(map_message.IsInitialized());
  689. }
  690. }
  691. TEST(Lite, AllLite42) {
  692. std::string data;
  693. {
  694. // Check that adding more values to enum does not corrupt message
  695. // when passed through an old client.
  696. protobuf_unittest::V2MessageLite v2_message;
  697. v2_message.set_int_field(800);
  698. // Set enum field to the value not understood by the old client.
  699. v2_message.set_enum_field(protobuf_unittest::V2_SECOND);
  700. std::string v2_bytes = v2_message.SerializeAsString();
  701. protobuf_unittest::V1MessageLite v1_message;
  702. v1_message.ParseFromString(v2_bytes);
  703. EXPECT_TRUE(v1_message.IsInitialized());
  704. EXPECT_EQ(v1_message.int_field(), v2_message.int_field());
  705. // V1 client does not understand V2_SECOND value, so it discards it and
  706. // uses default value instead.
  707. EXPECT_EQ(v1_message.enum_field(), protobuf_unittest::V1_FIRST);
  708. // However, when re-serialized, it should preserve enum value.
  709. std::string v1_bytes = v1_message.SerializeAsString();
  710. protobuf_unittest::V2MessageLite same_v2_message;
  711. same_v2_message.ParseFromString(v1_bytes);
  712. EXPECT_EQ(v2_message.int_field(), same_v2_message.int_field());
  713. EXPECT_EQ(v2_message.enum_field(), same_v2_message.enum_field());
  714. }
  715. }
  716. // Test that when parsing a oneof, we can successfully clear whatever already
  717. // happened to be stored in the oneof.
  718. TEST(Lite, AllLite43) {
  719. protobuf_unittest::TestOneofParsingLite message1;
  720. message1.set_oneof_int32(17);
  721. std::string serialized;
  722. EXPECT_TRUE(message1.SerializeToString(&serialized));
  723. // Submessage
  724. {
  725. protobuf_unittest::TestOneofParsingLite message2;
  726. message2.mutable_oneof_submessage();
  727. io::CodedInputStream input_stream(
  728. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
  729. EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
  730. EXPECT_EQ(17, message2.oneof_int32());
  731. }
  732. // String
  733. {
  734. protobuf_unittest::TestOneofParsingLite message2;
  735. message2.set_oneof_string("string");
  736. io::CodedInputStream input_stream(
  737. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
  738. EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
  739. EXPECT_EQ(17, message2.oneof_int32());
  740. }
  741. // Bytes
  742. {
  743. protobuf_unittest::TestOneofParsingLite message2;
  744. message2.set_oneof_bytes("bytes");
  745. io::CodedInputStream input_stream(
  746. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()), serialized.size());
  747. EXPECT_TRUE(message2.MergeFromCodedStream(&input_stream));
  748. EXPECT_EQ(17, message2.oneof_int32());
  749. }
  750. }
  751. // Verify that we can successfully parse fields of various types within oneof
  752. // fields. We also verify that we can parse the same data twice into the same
  753. // message.
  754. TEST(Lite, AllLite44) {
  755. // Int32
  756. {
  757. protobuf_unittest::TestOneofParsingLite original;
  758. original.set_oneof_int32(17);
  759. std::string serialized;
  760. EXPECT_TRUE(original.SerializeToString(&serialized));
  761. protobuf_unittest::TestOneofParsingLite parsed;
  762. for (int i = 0; i < 2; ++i) {
  763. io::CodedInputStream input_stream(
  764. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  765. serialized.size());
  766. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  767. EXPECT_EQ(17, parsed.oneof_int32());
  768. }
  769. }
  770. // Submessage
  771. {
  772. protobuf_unittest::TestOneofParsingLite original;
  773. original.mutable_oneof_submessage()->set_optional_int32(5);
  774. std::string serialized;
  775. EXPECT_TRUE(original.SerializeToString(&serialized));
  776. protobuf_unittest::TestOneofParsingLite parsed;
  777. for (int i = 0; i < 2; ++i) {
  778. io::CodedInputStream input_stream(
  779. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  780. serialized.size());
  781. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  782. EXPECT_EQ(5, parsed.oneof_submessage().optional_int32());
  783. }
  784. }
  785. // String
  786. {
  787. protobuf_unittest::TestOneofParsingLite original;
  788. original.set_oneof_string("string");
  789. std::string serialized;
  790. EXPECT_TRUE(original.SerializeToString(&serialized));
  791. protobuf_unittest::TestOneofParsingLite parsed;
  792. for (int i = 0; i < 2; ++i) {
  793. io::CodedInputStream input_stream(
  794. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  795. serialized.size());
  796. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  797. EXPECT_EQ("string", parsed.oneof_string());
  798. }
  799. }
  800. // Bytes
  801. {
  802. protobuf_unittest::TestOneofParsingLite original;
  803. original.set_oneof_bytes("bytes");
  804. std::string serialized;
  805. EXPECT_TRUE(original.SerializeToString(&serialized));
  806. protobuf_unittest::TestOneofParsingLite parsed;
  807. for (int i = 0; i < 2; ++i) {
  808. io::CodedInputStream input_stream(
  809. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  810. serialized.size());
  811. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  812. EXPECT_EQ("bytes", parsed.oneof_bytes());
  813. }
  814. }
  815. // Enum
  816. {
  817. protobuf_unittest::TestOneofParsingLite original;
  818. original.set_oneof_enum(protobuf_unittest::V2_SECOND);
  819. std::string serialized;
  820. EXPECT_TRUE(original.SerializeToString(&serialized));
  821. protobuf_unittest::TestOneofParsingLite parsed;
  822. for (int i = 0; i < 2; ++i) {
  823. io::CodedInputStream input_stream(
  824. reinterpret_cast<const ::google::protobuf::uint8*>(serialized.data()),
  825. serialized.size());
  826. EXPECT_TRUE(parsed.MergeFromCodedStream(&input_stream));
  827. EXPECT_EQ(protobuf_unittest::V2_SECOND, parsed.oneof_enum());
  828. }
  829. }
  830. std::cout << "PASS" << std::endl;
  831. }
  832. TEST(Lite, AllLite45) {
  833. // Test unknown fields are not discarded upon parsing.
  834. std::string data = "\20\1"; // varint 1 with field number 2
  835. protobuf_unittest::ForeignMessageLite a;
  836. EXPECT_TRUE(a.ParseFromString(data));
  837. io::CodedInputStream input_stream(
  838. reinterpret_cast<const ::google::protobuf::uint8*>(data.data()), data.size());
  839. EXPECT_TRUE(a.MergePartialFromCodedStream(&input_stream));
  840. std::string serialized = a.SerializeAsString();
  841. EXPECT_EQ(serialized.substr(0, 2), data);
  842. EXPECT_EQ(serialized.substr(2), data);
  843. }
  844. // The following two tests check for wire compatibility between packed and
  845. // unpacked repeated fields. There used to be a bug in the generated parsing
  846. // code that caused us to calculate the highest possible tag number without
  847. // taking into account that a repeated field might not be in the packed (or
  848. // unpacked) state we expect. These tests specifically check for that issue by
  849. // making sure we can parse repeated fields when the tag is higher than we would
  850. // expect.
  851. TEST(Lite, AllLite46) {
  852. protobuf_unittest::PackedInt32 packed;
  853. packed.add_repeated_int32(42);
  854. std::string serialized;
  855. ASSERT_TRUE(packed.SerializeToString(&serialized));
  856. protobuf_unittest::NonPackedInt32 non_packed;
  857. ASSERT_TRUE(non_packed.ParseFromString(serialized));
  858. ASSERT_EQ(1, non_packed.repeated_int32_size());
  859. EXPECT_EQ(42, non_packed.repeated_int32(0));
  860. }
  861. TEST(Lite, AllLite47) {
  862. protobuf_unittest::NonPackedFixed32 non_packed;
  863. non_packed.add_repeated_fixed32(42);
  864. std::string serialized;
  865. ASSERT_TRUE(non_packed.SerializeToString(&serialized));
  866. protobuf_unittest::PackedFixed32 packed;
  867. ASSERT_TRUE(packed.ParseFromString(serialized));
  868. ASSERT_EQ(1, packed.repeated_fixed32_size());
  869. EXPECT_EQ(42, packed.repeated_fixed32(0));
  870. }
  871. TEST(Lite, MapCrash) {
  872. // See b/113635730
  873. Arena arena;
  874. auto msg = Arena::CreateMessage<protobuf_unittest::TestMapLite>(&arena);
  875. // Payload for the map<string, Enum> with a enum varint that's longer >
  876. // 10 bytes. This causes a parse fail and a subsequent delete. field 16
  877. // (map<int32, MapEnumLite>) tag = 128+2 = \202 \1
  878. // 13 long \15
  879. // int32 key = 1 (\10 \1)
  880. // MapEnumLite value = too long varint (parse error)
  881. EXPECT_FALSE(msg->ParseFromString(
  882. "\202\1\15\10\1\200\200\200\200\200\200\200\200\200\200\1"));
  883. }
  884. TEST(Lite, CorrectEnding) {
  885. protobuf_unittest::TestAllTypesLite msg;
  886. {
  887. // All proto wireformat parsers should act the same on parsing data in as
  888. // much as it concerns the parsing, ie. not the interpretation of the data.
  889. // TestAllTypesLite is not a group inside another message. So in practice
  890. // will not encounter an end-group tag. However the parser should behave
  891. // like any wire format parser should.
  892. static const char kWireFormat[] = "\204\1";
  893. io::CodedInputStream cis(reinterpret_cast<const uint8*>(kWireFormat), 2);
  894. // The old CodedInputStream parser got an optimization (ReadTagNoLastTag)
  895. // for non-group messages (like TestAllTypesLite) which made it not accept
  896. // end-group. This is not a real big deal, but I think going forward its
  897. // good to have all parse loops behave 'exactly' the same.
  898. EXPECT_TRUE(msg.MergePartialFromCodedStream(&cis));
  899. EXPECT_FALSE(cis.ConsumedEntireMessage());
  900. EXPECT_TRUE(cis.LastTagWas(132));
  901. }
  902. {
  903. // This is an incomplete end-group tag. This should be a genuine parse
  904. // failure.
  905. static const char kWireFormat[] = "\214";
  906. io::CodedInputStream cis(reinterpret_cast<const uint8*>(kWireFormat), 1);
  907. // Unfortunately the old parser detects a parse error in ReadTag and returns
  908. // 0 (as it states 0 is an invalid tag). However 0 is not an invalid tag
  909. // as it can be used to terminate the stream, so this returns true.
  910. EXPECT_FALSE(msg.MergePartialFromCodedStream(&cis));
  911. }
  912. }
  913. TEST(Lite, DebugString) {
  914. protobuf_unittest::TestAllTypesLite message1, message2;
  915. EXPECT_TRUE(HasPrefixString(message1.DebugString(), "MessageLite at 0x"));
  916. EXPECT_TRUE(HasPrefixString(message2.DebugString(), "MessageLite at 0x"));
  917. // DebugString() and ShortDebugString() are the same for now.
  918. EXPECT_EQ(message1.DebugString(), message1.ShortDebugString());
  919. // Even identical lite protos should have different DebugString() output. Part
  920. // of the reason for including the memory address is so that we get some
  921. // non-determinism, which should make it easier for us to change the output
  922. // later without breaking any code.
  923. EXPECT_NE(message1.DebugString(), message2.DebugString());
  924. }
  925. TEST(Lite, EnumValueToName) {
  926. EXPECT_EQ("FOREIGN_LITE_FOO", protobuf_unittest::ForeignEnumLite_Name(
  927. protobuf_unittest::FOREIGN_LITE_FOO));
  928. EXPECT_EQ("FOREIGN_LITE_BAR", protobuf_unittest::ForeignEnumLite_Name(
  929. protobuf_unittest::FOREIGN_LITE_BAR));
  930. EXPECT_EQ("FOREIGN_LITE_BAZ", protobuf_unittest::ForeignEnumLite_Name(
  931. protobuf_unittest::FOREIGN_LITE_BAZ));
  932. EXPECT_EQ("", protobuf_unittest::ForeignEnumLite_Name(0));
  933. EXPECT_EQ("", protobuf_unittest::ForeignEnumLite_Name(999));
  934. }
  935. TEST(Lite, NestedEnumValueToName) {
  936. EXPECT_EQ("FOO", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(
  937. protobuf_unittest::TestAllTypesLite::FOO));
  938. EXPECT_EQ("BAR", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(
  939. protobuf_unittest::TestAllTypesLite::BAR));
  940. EXPECT_EQ("BAZ", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(
  941. protobuf_unittest::TestAllTypesLite::BAZ));
  942. EXPECT_EQ("", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(0));
  943. EXPECT_EQ("", protobuf_unittest::TestAllTypesLite::NestedEnum_Name(999));
  944. }
  945. TEST(Lite, EnumNameToValue) {
  946. protobuf_unittest::ForeignEnumLite value;
  947. ASSERT_TRUE(
  948. protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_FOO", &value));
  949. EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_FOO, value);
  950. ASSERT_TRUE(
  951. protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_BAR", &value));
  952. EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_BAR, value);
  953. ASSERT_TRUE(
  954. protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_BAZ", &value));
  955. EXPECT_EQ(protobuf_unittest::FOREIGN_LITE_BAZ, value);
  956. // Non-existent values
  957. EXPECT_FALSE(protobuf_unittest::ForeignEnumLite_Parse("E", &value));
  958. EXPECT_FALSE(
  959. protobuf_unittest::ForeignEnumLite_Parse("FOREIGN_LITE_C", &value));
  960. EXPECT_FALSE(protobuf_unittest::ForeignEnumLite_Parse("G", &value));
  961. }
  962. TEST(Lite, NestedEnumNameToValue) {
  963. protobuf_unittest::TestAllTypesLite::NestedEnum value;
  964. ASSERT_TRUE(
  965. protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("FOO", &value));
  966. EXPECT_EQ(protobuf_unittest::TestAllTypesLite::FOO, value);
  967. ASSERT_TRUE(
  968. protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("BAR", &value));
  969. EXPECT_EQ(protobuf_unittest::TestAllTypesLite::BAR, value);
  970. ASSERT_TRUE(
  971. protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("BAZ", &value));
  972. EXPECT_EQ(protobuf_unittest::TestAllTypesLite::BAZ, value);
  973. // Non-existent values
  974. EXPECT_FALSE(
  975. protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("A", &value));
  976. EXPECT_FALSE(
  977. protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("C", &value));
  978. EXPECT_FALSE(
  979. protobuf_unittest::TestAllTypesLite::NestedEnum_Parse("G", &value));
  980. }
  981. TEST(Lite, AliasedEnum) {
  982. // Enums with allow_alias = true can have multiple entries with the same
  983. // value.
  984. EXPECT_EQ("FOO1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
  985. protobuf_unittest::DupEnum::FOO1));
  986. EXPECT_EQ("FOO1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
  987. protobuf_unittest::DupEnum::FOO2));
  988. EXPECT_EQ("BAR1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
  989. protobuf_unittest::DupEnum::BAR1));
  990. EXPECT_EQ("BAR1", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
  991. protobuf_unittest::DupEnum::BAR2));
  992. EXPECT_EQ("BAZ", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(
  993. protobuf_unittest::DupEnum::BAZ));
  994. EXPECT_EQ("", protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Name(999));
  995. protobuf_unittest::DupEnum::TestEnumWithDupValueLite value;
  996. ASSERT_TRUE(
  997. protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Parse("FOO1", &value));
  998. EXPECT_EQ(protobuf_unittest::DupEnum::FOO1, value);
  999. value = static_cast<protobuf_unittest::DupEnum::TestEnumWithDupValueLite>(0);
  1000. ASSERT_TRUE(
  1001. protobuf_unittest::DupEnum::TestEnumWithDupValueLite_Parse("FOO2", &value));
  1002. EXPECT_EQ(protobuf_unittest::DupEnum::FOO2, value);
  1003. }
  1004. } // namespace protobuf
  1005. } // namespace google