TextFormatTest.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.IO;
  36. using Google.ProtocolBuffers.TestProtos;
  37. using Xunit;
  38. namespace Google.ProtocolBuffers
  39. {
  40. public class TextFormatTest
  41. {
  42. private static readonly string AllFieldsSetText = TestResources.text_format_unittest_data;
  43. private static readonly string AllExtensionsSetText = TestResources.text_format_unittest_extensions_data;
  44. /// <summary>
  45. /// Note that this is slightly different to the Java - 123.0 becomes 123, and 1.23E17 becomes 1.23E+17.
  46. /// Both of these differences can be parsed by the Java and the C++, and we can parse their output too.
  47. /// </summary>
  48. private const string ExoticText =
  49. "repeated_int32: -1\n" +
  50. "repeated_int32: -2147483648\n" +
  51. "repeated_int64: -1\n" +
  52. "repeated_int64: -9223372036854775808\n" +
  53. "repeated_uint32: 4294967295\n" +
  54. "repeated_uint32: 2147483648\n" +
  55. "repeated_uint64: 18446744073709551615\n" +
  56. "repeated_uint64: 9223372036854775808\n" +
  57. "repeated_double: 123\n" +
  58. "repeated_double: 123.5\n" +
  59. "repeated_double: 0.125\n" +
  60. "repeated_double: 1.23E+17\n" +
  61. "repeated_double: 1.235E+22\n" +
  62. "repeated_double: 1.235E-18\n" +
  63. "repeated_double: 123.456789\n" +
  64. "repeated_double: Infinity\n" +
  65. "repeated_double: -Infinity\n" +
  66. "repeated_double: NaN\n" +
  67. "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"" +
  68. "\\341\\210\\264\"\n" +
  69. "repeated_bytes: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\376\"\n";
  70. private const string MessageSetText =
  71. "[protobuf_unittest.TestMessageSetExtension1] {\n" +
  72. " i: 123\n" +
  73. "}\n" +
  74. "[protobuf_unittest.TestMessageSetExtension2] {\n" +
  75. " str: \"foo\"\n" +
  76. "}\n";
  77. /// <summary>
  78. /// Print TestAllTypes and compare with golden file.
  79. /// </summary>
  80. [Fact]
  81. public void PrintMessage()
  82. {
  83. TestUtil.TestInMultipleCultures(() =>
  84. {
  85. string text = TextFormat.PrintToString(TestUtil.GetAllSet());
  86. Assert.Equal(AllFieldsSetText.Replace("\r\n", "\n").Trim(),
  87. text.Replace("\r\n", "\n").Trim());
  88. });
  89. }
  90. /// <summary>
  91. /// Tests that a builder prints the same way as a message.
  92. /// </summary>
  93. [Fact]
  94. public void PrintBuilder()
  95. {
  96. TestUtil.TestInMultipleCultures(() =>
  97. {
  98. string messageText = TextFormat.PrintToString(TestUtil.GetAllSet());
  99. string builderText = TextFormat.PrintToString(TestUtil.GetAllSet().ToBuilder());
  100. Assert.Equal(messageText, builderText);
  101. });
  102. }
  103. /// <summary>
  104. /// Print TestAllExtensions and compare with golden file.
  105. /// </summary>
  106. [Fact]
  107. public void PrintExtensions()
  108. {
  109. string text = TextFormat.PrintToString(TestUtil.GetAllExtensionsSet());
  110. Assert.Equal(AllExtensionsSetText.Replace("\r\n", "\n").Trim(), text.Replace("\r\n", "\n").Trim());
  111. }
  112. /// <summary>
  113. /// Test printing of unknown fields in a message.
  114. /// </summary>
  115. [Fact]
  116. public void PrintUnknownFields()
  117. {
  118. TestEmptyMessage message =
  119. TestEmptyMessage.CreateBuilder()
  120. .SetUnknownFields(
  121. UnknownFieldSet.CreateBuilder()
  122. .AddField(5,
  123. UnknownField.CreateBuilder()
  124. .AddVarint(1)
  125. .AddFixed32(2)
  126. .AddFixed64(3)
  127. .AddLengthDelimited(ByteString.CopyFromUtf8("4"))
  128. .AddGroup(
  129. UnknownFieldSet.CreateBuilder()
  130. .AddField(10,
  131. UnknownField.CreateBuilder()
  132. .AddVarint(5)
  133. .Build())
  134. .Build())
  135. .Build())
  136. .AddField(8,
  137. UnknownField.CreateBuilder()
  138. .AddVarint(1)
  139. .AddVarint(2)
  140. .AddVarint(3)
  141. .Build())
  142. .AddField(15,
  143. UnknownField.CreateBuilder()
  144. .AddVarint(0xABCDEF1234567890L)
  145. .AddFixed32(0xABCD1234)
  146. .AddFixed64(0xABCDEF1234567890L)
  147. .Build())
  148. .Build())
  149. .Build();
  150. Assert.Equal(
  151. "5: 1\n" +
  152. "5: 0x00000002\n" +
  153. "5: 0x0000000000000003\n" +
  154. "5: \"4\"\n" +
  155. "5 {\n" +
  156. " 10: 5\n" +
  157. "}\n" +
  158. "8: 1\n" +
  159. "8: 2\n" +
  160. "8: 3\n" +
  161. "15: 12379813812177893520\n" +
  162. "15: 0xabcd1234\n" +
  163. "15: 0xabcdef1234567890\n",
  164. TextFormat.PrintToString(message));
  165. }
  166. /// <summary>
  167. /// Helper to construct a ByteString from a string containing only 8-bit
  168. /// characters. The characters are converted directly to bytes, *not*
  169. /// encoded using UTF-8.
  170. /// </summary>
  171. private static ByteString Bytes(string str)
  172. {
  173. byte[] bytes = new byte[str.Length];
  174. for (int i = 0; i < bytes.Length; i++)
  175. bytes[i] = (byte)str[i];
  176. return ByteString.CopyFrom(bytes);
  177. }
  178. [Fact]
  179. public void PrintExotic()
  180. {
  181. IMessage message = TestAllTypes.CreateBuilder()
  182. // Signed vs. unsigned numbers.
  183. .AddRepeatedInt32(-1)
  184. .AddRepeatedUint32(uint.MaxValue)
  185. .AddRepeatedInt64(-1)
  186. .AddRepeatedUint64(ulong.MaxValue)
  187. .AddRepeatedInt32(1 << 31)
  188. .AddRepeatedUint32(1U << 31)
  189. .AddRepeatedInt64(1L << 63)
  190. .AddRepeatedUint64(1UL << 63)
  191. // Floats of various precisions and exponents.
  192. .AddRepeatedDouble(123)
  193. .AddRepeatedDouble(123.5)
  194. .AddRepeatedDouble(0.125)
  195. .AddRepeatedDouble(123e15)
  196. .AddRepeatedDouble(123.5e20)
  197. .AddRepeatedDouble(123.5e-20)
  198. .AddRepeatedDouble(123.456789)
  199. .AddRepeatedDouble(Double.PositiveInfinity)
  200. .AddRepeatedDouble(Double.NegativeInfinity)
  201. .AddRepeatedDouble(Double.NaN)
  202. // Strings and bytes that needing escaping.
  203. .AddRepeatedString("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u1234")
  204. .AddRepeatedBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u00fe"))
  205. .Build();
  206. Assert.Equal(ExoticText, message.ToString());
  207. }
  208. [Fact]
  209. public void PrintMessageSet()
  210. {
  211. TestMessageSet messageSet =
  212. TestMessageSet.CreateBuilder()
  213. .SetExtension(
  214. TestMessageSetExtension1.MessageSetExtension,
  215. TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
  216. .SetExtension(
  217. TestMessageSetExtension2.MessageSetExtension,
  218. TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
  219. .Build();
  220. Assert.Equal(MessageSetText, messageSet.ToString());
  221. }
  222. // =================================================================
  223. [Fact]
  224. public void Parse()
  225. {
  226. TestUtil.TestInMultipleCultures(() =>
  227. {
  228. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  229. TextFormat.Merge(AllFieldsSetText, builder);
  230. TestUtil.AssertAllFieldsSet(builder.Build());
  231. });
  232. }
  233. [Fact]
  234. public void ParseReader()
  235. {
  236. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  237. TextFormat.Merge(new StringReader(AllFieldsSetText), builder);
  238. TestUtil.AssertAllFieldsSet(builder.Build());
  239. }
  240. [Fact]
  241. public void ParseExtensions()
  242. {
  243. TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
  244. TextFormat.Merge(AllExtensionsSetText,
  245. TestUtil.CreateExtensionRegistry(),
  246. builder);
  247. TestUtil.AssertAllExtensionsSet(builder.Build());
  248. }
  249. [Fact]
  250. public void ParseCompatibility()
  251. {
  252. string original = "repeated_float: inf\n" +
  253. "repeated_float: -inf\n" +
  254. "repeated_float: nan\n" +
  255. "repeated_float: inff\n" +
  256. "repeated_float: -inff\n" +
  257. "repeated_float: nanf\n" +
  258. "repeated_float: 1.0f\n" +
  259. "repeated_float: infinityf\n" +
  260. "repeated_float: -Infinityf\n" +
  261. "repeated_double: infinity\n" +
  262. "repeated_double: -infinity\n" +
  263. "repeated_double: nan\n";
  264. string canonical = "repeated_float: Infinity\n" +
  265. "repeated_float: -Infinity\n" +
  266. "repeated_float: NaN\n" +
  267. "repeated_float: Infinity\n" +
  268. "repeated_float: -Infinity\n" +
  269. "repeated_float: NaN\n" +
  270. "repeated_float: 1\n" + // Java has 1.0; this is fine
  271. "repeated_float: Infinity\n" +
  272. "repeated_float: -Infinity\n" +
  273. "repeated_double: Infinity\n" +
  274. "repeated_double: -Infinity\n" +
  275. "repeated_double: NaN\n";
  276. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  277. TextFormat.Merge(original, builder);
  278. Assert.Equal(canonical, builder.Build().ToString());
  279. }
  280. [Fact]
  281. public void ParseExotic()
  282. {
  283. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  284. TextFormat.Merge(ExoticText, builder);
  285. // Too lazy to check things individually. Don't try to debug this
  286. // if testPrintExotic() is Assert.Failing.
  287. Assert.Equal(ExoticText, builder.Build().ToString());
  288. }
  289. [Fact]
  290. public void ParseMessageSet()
  291. {
  292. ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
  293. extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension);
  294. extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension);
  295. TestMessageSet.Builder builder = TestMessageSet.CreateBuilder();
  296. TextFormat.Merge(MessageSetText, extensionRegistry, builder);
  297. TestMessageSet messageSet = builder.Build();
  298. Assert.True(messageSet.HasExtension(TestMessageSetExtension1.MessageSetExtension));
  299. Assert.Equal(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
  300. Assert.True(messageSet.HasExtension(TestMessageSetExtension2.MessageSetExtension));
  301. Assert.Equal("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);
  302. }
  303. [Fact]
  304. public void ParseNumericEnum()
  305. {
  306. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  307. TextFormat.Merge("optional_nested_enum: 2", builder);
  308. Assert.Equal(TestAllTypes.Types.NestedEnum.BAR, builder.OptionalNestedEnum);
  309. }
  310. [Fact]
  311. public void ParseAngleBrackets()
  312. {
  313. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  314. TextFormat.Merge("OptionalGroup: < a: 1 >", builder);
  315. Assert.True(builder.HasOptionalGroup);
  316. Assert.Equal(1, builder.OptionalGroup.A);
  317. }
  318. [Fact]
  319. public void ParseComment()
  320. {
  321. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  322. TextFormat.Merge(
  323. "# this is a comment\n" +
  324. "optional_int32: 1 # another comment\n" +
  325. "optional_int64: 2\n" +
  326. "# EOF comment", builder);
  327. Assert.Equal(1, builder.OptionalInt32);
  328. Assert.Equal(2, builder.OptionalInt64);
  329. }
  330. private static void AssertParseError(string error, string text)
  331. {
  332. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  333. Exception exception = Assert.Throws<FormatException>(() => TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder));
  334. Assert.Equal(error, exception.Message);
  335. }
  336. [Fact]
  337. public void ParseErrors()
  338. {
  339. AssertParseError(
  340. "1:16: Expected \":\".",
  341. "optional_int32 123");
  342. AssertParseError(
  343. "1:23: Expected identifier.",
  344. "optional_nested_enum: ?");
  345. AssertParseError(
  346. "1:18: Couldn't parse integer: Number must be positive: -1",
  347. "optional_uint32: -1");
  348. AssertParseError(
  349. "1:17: Couldn't parse integer: Number out of range for 32-bit signed " +
  350. "integer: 82301481290849012385230157",
  351. "optional_int32: 82301481290849012385230157");
  352. AssertParseError(
  353. "1:16: Expected \"true\" or \"false\".",
  354. "optional_bool: maybe");
  355. AssertParseError(
  356. "1:18: Expected string.",
  357. "optional_string: 123");
  358. AssertParseError(
  359. "1:18: String missing ending quote.",
  360. "optional_string: \"ueoauaoe");
  361. AssertParseError(
  362. "1:18: String missing ending quote.",
  363. "optional_string: \"ueoauaoe\n" +
  364. "optional_int32: 123");
  365. AssertParseError(
  366. "1:18: Invalid escape sequence: '\\z'",
  367. "optional_string: \"\\z\"");
  368. AssertParseError(
  369. "1:18: String missing ending quote.",
  370. "optional_string: \"ueoauaoe\n" +
  371. "optional_int32: 123");
  372. AssertParseError(
  373. "1:2: Extension \"nosuchext\" not found in the ExtensionRegistry.",
  374. "[nosuchext]: 123");
  375. AssertParseError(
  376. "1:20: Extension \"protobuf_unittest.optional_int32_extension\" " +
  377. "not found in the ExtensionRegistry.",
  378. "[protobuf_unittest.optional_int32_extension]: 123");
  379. AssertParseError(
  380. "1:1: Message type \"protobuf_unittest.TestAllTypes\" has no field " +
  381. "named \"nosuchfield\".",
  382. "nosuchfield: 123");
  383. AssertParseError(
  384. "1:21: Expected \">\".",
  385. "OptionalGroup < a: 1");
  386. AssertParseError(
  387. "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " +
  388. "value named \"NO_SUCH_VALUE\".",
  389. "optional_nested_enum: NO_SUCH_VALUE");
  390. AssertParseError(
  391. "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " +
  392. "value with number 123.",
  393. "optional_nested_enum: 123");
  394. // Delimiters must match.
  395. AssertParseError(
  396. "1:22: Expected identifier.",
  397. "OptionalGroup < a: 1 }");
  398. AssertParseError(
  399. "1:22: Expected identifier.",
  400. "OptionalGroup { a: 1 >");
  401. }
  402. // =================================================================
  403. private static ByteString Bytes(params byte[] bytes)
  404. {
  405. return ByteString.CopyFrom(bytes);
  406. }
  407. [Fact]
  408. public void Escape()
  409. {
  410. // Escape sequences.
  411. Assert.Equal("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
  412. TextFormat.EscapeBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"")));
  413. Assert.Equal("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
  414. TextFormat.EscapeText("\0\u0001\u0007\b\f\n\r\t\v\\\'\""));
  415. Assert.Equal(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\""),
  416. TextFormat.UnescapeBytes("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
  417. Assert.Equal("\0\u0001\u0007\b\f\n\r\t\v\\\'\"",
  418. TextFormat.UnescapeText("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
  419. // Unicode handling.
  420. Assert.Equal("\\341\\210\\264", TextFormat.EscapeText("\u1234"));
  421. Assert.Equal("\\341\\210\\264", TextFormat.EscapeBytes(Bytes(0xe1, 0x88, 0xb4)));
  422. Assert.Equal("\u1234", TextFormat.UnescapeText("\\341\\210\\264"));
  423. Assert.Equal(Bytes(0xe1, 0x88, 0xb4), TextFormat.UnescapeBytes("\\341\\210\\264"));
  424. Assert.Equal("\u1234", TextFormat.UnescapeText("\\xe1\\x88\\xb4"));
  425. Assert.Equal(Bytes(0xe1, 0x88, 0xb4), TextFormat.UnescapeBytes("\\xe1\\x88\\xb4"));
  426. // Errors.
  427. Assert.Throws<FormatException>(() => TextFormat.UnescapeText("\\x"));
  428. Assert.Throws<FormatException>(() => TextFormat.UnescapeText("\\z"));
  429. Assert.Throws<FormatException>(() => TextFormat.UnescapeText("\\"));
  430. }
  431. [Fact]
  432. public void ParseInteger()
  433. {
  434. Assert.Equal(0, TextFormat.ParseInt32("0"));
  435. Assert.Equal(1, TextFormat.ParseInt32("1"));
  436. Assert.Equal(-1, TextFormat.ParseInt32("-1"));
  437. Assert.Equal(12345, TextFormat.ParseInt32("12345"));
  438. Assert.Equal(-12345, TextFormat.ParseInt32("-12345"));
  439. Assert.Equal(2147483647, TextFormat.ParseInt32("2147483647"));
  440. Assert.Equal(-2147483648, TextFormat.ParseInt32("-2147483648"));
  441. Assert.Equal(0u, TextFormat.ParseUInt32("0"));
  442. Assert.Equal(1u, TextFormat.ParseUInt32("1"));
  443. Assert.Equal(12345u, TextFormat.ParseUInt32("12345"));
  444. Assert.Equal(2147483647u, TextFormat.ParseUInt32("2147483647"));
  445. Assert.Equal(2147483648U, TextFormat.ParseUInt32("2147483648"));
  446. Assert.Equal(4294967295U, TextFormat.ParseUInt32("4294967295"));
  447. Assert.Equal(0L, TextFormat.ParseInt64("0"));
  448. Assert.Equal(1L, TextFormat.ParseInt64("1"));
  449. Assert.Equal(-1L, TextFormat.ParseInt64("-1"));
  450. Assert.Equal(12345L, TextFormat.ParseInt64("12345"));
  451. Assert.Equal(-12345L, TextFormat.ParseInt64("-12345"));
  452. Assert.Equal(2147483647L, TextFormat.ParseInt64("2147483647"));
  453. Assert.Equal(-2147483648L, TextFormat.ParseInt64("-2147483648"));
  454. Assert.Equal(4294967295L, TextFormat.ParseInt64("4294967295"));
  455. Assert.Equal(4294967296L, TextFormat.ParseInt64("4294967296"));
  456. Assert.Equal(9223372036854775807L, TextFormat.ParseInt64("9223372036854775807"));
  457. Assert.Equal(-9223372036854775808L, TextFormat.ParseInt64("-9223372036854775808"));
  458. Assert.Equal(0uL, TextFormat.ParseUInt64("0"));
  459. Assert.Equal(1uL, TextFormat.ParseUInt64("1"));
  460. Assert.Equal(12345uL, TextFormat.ParseUInt64("12345"));
  461. Assert.Equal(2147483647uL, TextFormat.ParseUInt64("2147483647"));
  462. Assert.Equal(4294967295uL, TextFormat.ParseUInt64("4294967295"));
  463. Assert.Equal(4294967296uL, TextFormat.ParseUInt64("4294967296"));
  464. Assert.Equal(9223372036854775807UL, TextFormat.ParseUInt64("9223372036854775807"));
  465. Assert.Equal(9223372036854775808UL, TextFormat.ParseUInt64("9223372036854775808"));
  466. Assert.Equal(18446744073709551615UL, TextFormat.ParseUInt64("18446744073709551615"));
  467. // Hex
  468. Assert.Equal(0x1234abcd, TextFormat.ParseInt32("0x1234abcd"));
  469. Assert.Equal(-0x1234abcd, TextFormat.ParseInt32("-0x1234abcd"));
  470. Assert.Equal(0xffffffffffffffffUL, TextFormat.ParseUInt64("0xffffffffffffffff"));
  471. Assert.Equal(0x7fffffffffffffffL,
  472. TextFormat.ParseInt64("0x7fffffffffffffff"));
  473. // Octal
  474. Assert.Equal(342391, TextFormat.ParseInt32("01234567"));
  475. // Out-of-range
  476. Assert.Throws<FormatException>(() => TextFormat.ParseInt32("2147483648"));
  477. Assert.Throws<FormatException>(() => TextFormat.ParseInt32("-2147483649"));
  478. Assert.Throws<FormatException>(() => TextFormat.ParseUInt32("4294967296"));
  479. Assert.Throws<FormatException>(() => TextFormat.ParseUInt32("-1"));
  480. Assert.Throws<FormatException>(() => TextFormat.ParseInt64("9223372036854775808"));
  481. Assert.Throws<FormatException>(() => TextFormat.ParseInt64("-9223372036854775809"));
  482. Assert.Throws<FormatException>(() => TextFormat.ParseUInt64("18446744073709551616"));
  483. Assert.Throws<FormatException>(() => TextFormat.ParseUInt64("-1"));
  484. Assert.Throws<FormatException>(() => TextFormat.ParseInt32("abcd"));
  485. }
  486. [Fact]
  487. public void ParseLongString()
  488. {
  489. string longText =
  490. "123456789012345678901234567890123456789012345678901234567890" +
  491. "123456789012345678901234567890123456789012345678901234567890" +
  492. "123456789012345678901234567890123456789012345678901234567890" +
  493. "123456789012345678901234567890123456789012345678901234567890" +
  494. "123456789012345678901234567890123456789012345678901234567890" +
  495. "123456789012345678901234567890123456789012345678901234567890" +
  496. "123456789012345678901234567890123456789012345678901234567890" +
  497. "123456789012345678901234567890123456789012345678901234567890" +
  498. "123456789012345678901234567890123456789012345678901234567890" +
  499. "123456789012345678901234567890123456789012345678901234567890" +
  500. "123456789012345678901234567890123456789012345678901234567890" +
  501. "123456789012345678901234567890123456789012345678901234567890" +
  502. "123456789012345678901234567890123456789012345678901234567890" +
  503. "123456789012345678901234567890123456789012345678901234567890" +
  504. "123456789012345678901234567890123456789012345678901234567890" +
  505. "123456789012345678901234567890123456789012345678901234567890";
  506. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  507. TextFormat.Merge("optional_string: \"" + longText + "\"", builder);
  508. Assert.Equal(longText, builder.OptionalString);
  509. }
  510. }
  511. }