PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/src/ProtocolBuffers.Test/TextFormatTest.cs

https://code.google.com/p/protobuf-csharp-port/
C# | 571 lines | 467 code | 43 blank | 61 comment | 0 complexity | 6734e6908ef5759c20df5a80ecc44871 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-2.0
  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 System.Text;
  37. using Google.ProtocolBuffers.TestProtos;
  38. using Microsoft.VisualStudio.TestTools.UnitTesting;
  39. using System.Globalization;
  40. using System.Threading;
  41. namespace Google.ProtocolBuffers
  42. {
  43. [TestClass]
  44. public class TextFormatTest
  45. {
  46. private static readonly string AllFieldsSetText = TestUtil.ReadTextFromFile("text_format_unittest_data.txt");
  47. private static readonly string AllExtensionsSetText =
  48. TestUtil.ReadTextFromFile("text_format_unittest_extensions_data.txt");
  49. /// <summary>
  50. /// Note that this is slightly different to the Java - 123.0 becomes 123, and 1.23E17 becomes 1.23E+17.
  51. /// Both of these differences can be parsed by the Java and the C++, and we can parse their output too.
  52. /// </summary>
  53. private const string ExoticText =
  54. "repeated_int32: -1\n" +
  55. "repeated_int32: -2147483648\n" +
  56. "repeated_int64: -1\n" +
  57. "repeated_int64: -9223372036854775808\n" +
  58. "repeated_uint32: 4294967295\n" +
  59. "repeated_uint32: 2147483648\n" +
  60. "repeated_uint64: 18446744073709551615\n" +
  61. "repeated_uint64: 9223372036854775808\n" +
  62. "repeated_double: 123\n" +
  63. "repeated_double: 123.5\n" +
  64. "repeated_double: 0.125\n" +
  65. "repeated_double: 1.23E+17\n" +
  66. "repeated_double: 1.235E+22\n" +
  67. "repeated_double: 1.235E-18\n" +
  68. "repeated_double: 123.456789\n" +
  69. "repeated_double: Infinity\n" +
  70. "repeated_double: -Infinity\n" +
  71. "repeated_double: NaN\n" +
  72. "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"" +
  73. "\\341\\210\\264\"\n" +
  74. "repeated_bytes: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\\376\"\n";
  75. private const string MessageSetText =
  76. "[protobuf_unittest.TestMessageSetExtension1] {\n" +
  77. " i: 123\n" +
  78. "}\n" +
  79. "[protobuf_unittest.TestMessageSetExtension2] {\n" +
  80. " str: \"foo\"\n" +
  81. "}\n";
  82. /// <summary>
  83. /// Print TestAllTypes and compare with golden file.
  84. /// </summary>
  85. [TestMethod]
  86. public void PrintMessage()
  87. {
  88. TestUtil.TestInMultipleCultures(() =>
  89. {
  90. string text = TextFormat.PrintToString(TestUtil.GetAllSet());
  91. Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n"),
  92. text.Replace("\r\n", "\n"));
  93. });
  94. }
  95. /// <summary>
  96. /// Print TestAllExtensions and compare with golden file.
  97. /// </summary>
  98. [TestMethod]
  99. public void PrintExtensions()
  100. {
  101. string text = TextFormat.PrintToString(TestUtil.GetAllExtensionsSet());
  102. Assert.AreEqual(AllExtensionsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
  103. }
  104. /// <summary>
  105. /// Test printing of unknown fields in a message.
  106. /// </summary>
  107. [TestMethod]
  108. public void PrintUnknownFields()
  109. {
  110. TestEmptyMessage message =
  111. TestEmptyMessage.CreateBuilder()
  112. .SetUnknownFields(
  113. UnknownFieldSet.CreateBuilder()
  114. .AddField(5,
  115. UnknownField.CreateBuilder()
  116. .AddVarint(1)
  117. .AddFixed32(2)
  118. .AddFixed64(3)
  119. .AddLengthDelimited(ByteString.CopyFromUtf8("4"))
  120. .AddGroup(
  121. UnknownFieldSet.CreateBuilder()
  122. .AddField(10,
  123. UnknownField.CreateBuilder()
  124. .AddVarint(5)
  125. .Build())
  126. .Build())
  127. .Build())
  128. .AddField(8,
  129. UnknownField.CreateBuilder()
  130. .AddVarint(1)
  131. .AddVarint(2)
  132. .AddVarint(3)
  133. .Build())
  134. .AddField(15,
  135. UnknownField.CreateBuilder()
  136. .AddVarint(0xABCDEF1234567890L)
  137. .AddFixed32(0xABCD1234)
  138. .AddFixed64(0xABCDEF1234567890L)
  139. .Build())
  140. .Build())
  141. .Build();
  142. Assert.AreEqual(
  143. "5: 1\n" +
  144. "5: 0x00000002\n" +
  145. "5: 0x0000000000000003\n" +
  146. "5: \"4\"\n" +
  147. "5 {\n" +
  148. " 10: 5\n" +
  149. "}\n" +
  150. "8: 1\n" +
  151. "8: 2\n" +
  152. "8: 3\n" +
  153. "15: 12379813812177893520\n" +
  154. "15: 0xabcd1234\n" +
  155. "15: 0xabcdef1234567890\n",
  156. TextFormat.PrintToString(message));
  157. }
  158. /// <summary>
  159. /// Helper to construct a ByteString from a string containing only 8-bit
  160. /// characters. The characters are converted directly to bytes, *not*
  161. /// encoded using UTF-8.
  162. /// </summary>
  163. private static ByteString Bytes(string str)
  164. {
  165. return ByteString.CopyFrom(Encoding.GetEncoding(28591).GetBytes(str));
  166. }
  167. [TestMethod]
  168. public void PrintExotic()
  169. {
  170. IMessage message = TestAllTypes.CreateBuilder()
  171. // Signed vs. unsigned numbers.
  172. .AddRepeatedInt32(-1)
  173. .AddRepeatedUint32(uint.MaxValue)
  174. .AddRepeatedInt64(-1)
  175. .AddRepeatedUint64(ulong.MaxValue)
  176. .AddRepeatedInt32(1 << 31)
  177. .AddRepeatedUint32(1U << 31)
  178. .AddRepeatedInt64(1L << 63)
  179. .AddRepeatedUint64(1UL << 63)
  180. // Floats of various precisions and exponents.
  181. .AddRepeatedDouble(123)
  182. .AddRepeatedDouble(123.5)
  183. .AddRepeatedDouble(0.125)
  184. .AddRepeatedDouble(123e15)
  185. .AddRepeatedDouble(123.5e20)
  186. .AddRepeatedDouble(123.5e-20)
  187. .AddRepeatedDouble(123.456789)
  188. .AddRepeatedDouble(Double.PositiveInfinity)
  189. .AddRepeatedDouble(Double.NegativeInfinity)
  190. .AddRepeatedDouble(Double.NaN)
  191. // Strings and bytes that needing escaping.
  192. .AddRepeatedString("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u1234")
  193. .AddRepeatedBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u00fe"))
  194. .Build();
  195. Assert.AreEqual(ExoticText, message.ToString());
  196. }
  197. [TestMethod]
  198. public void PrintMessageSet()
  199. {
  200. TestMessageSet messageSet =
  201. TestMessageSet.CreateBuilder()
  202. .SetExtension(
  203. TestMessageSetExtension1.MessageSetExtension,
  204. TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
  205. .SetExtension(
  206. TestMessageSetExtension2.MessageSetExtension,
  207. TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
  208. .Build();
  209. Assert.AreEqual(MessageSetText, messageSet.ToString());
  210. }
  211. // =================================================================
  212. [TestMethod]
  213. public void Parse()
  214. {
  215. TestUtil.TestInMultipleCultures(() =>
  216. {
  217. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  218. TextFormat.Merge(AllFieldsSetText, builder);
  219. TestUtil.AssertAllFieldsSet(builder.Build());
  220. });
  221. }
  222. [TestMethod]
  223. public void ParseReader()
  224. {
  225. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  226. TextFormat.Merge(new StringReader(AllFieldsSetText), builder);
  227. TestUtil.AssertAllFieldsSet(builder.Build());
  228. }
  229. [TestMethod]
  230. public void ParseExtensions()
  231. {
  232. TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
  233. TextFormat.Merge(AllExtensionsSetText,
  234. TestUtil.CreateExtensionRegistry(),
  235. builder);
  236. TestUtil.AssertAllExtensionsSet(builder.Build());
  237. }
  238. [TestMethod]
  239. public void ParseCompatibility()
  240. {
  241. string original = "repeated_float: inf\n" +
  242. "repeated_float: -inf\n" +
  243. "repeated_float: nan\n" +
  244. "repeated_float: inff\n" +
  245. "repeated_float: -inff\n" +
  246. "repeated_float: nanf\n" +
  247. "repeated_float: 1.0f\n" +
  248. "repeated_float: infinityf\n" +
  249. "repeated_float: -Infinityf\n" +
  250. "repeated_double: infinity\n" +
  251. "repeated_double: -infinity\n" +
  252. "repeated_double: nan\n";
  253. string canonical = "repeated_float: Infinity\n" +
  254. "repeated_float: -Infinity\n" +
  255. "repeated_float: NaN\n" +
  256. "repeated_float: Infinity\n" +
  257. "repeated_float: -Infinity\n" +
  258. "repeated_float: NaN\n" +
  259. "repeated_float: 1\n" + // Java has 1.0; this is fine
  260. "repeated_float: Infinity\n" +
  261. "repeated_float: -Infinity\n" +
  262. "repeated_double: Infinity\n" +
  263. "repeated_double: -Infinity\n" +
  264. "repeated_double: NaN\n";
  265. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  266. TextFormat.Merge(original, builder);
  267. Assert.AreEqual(canonical, builder.Build().ToString());
  268. }
  269. [TestMethod]
  270. public void ParseExotic()
  271. {
  272. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  273. TextFormat.Merge(ExoticText, builder);
  274. // Too lazy to check things individually. Don't try to debug this
  275. // if testPrintExotic() is Assert.Failing.
  276. Assert.AreEqual(ExoticText, builder.Build().ToString());
  277. }
  278. [TestMethod]
  279. public void ParseMessageSet()
  280. {
  281. ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
  282. extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension);
  283. extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension);
  284. TestMessageSet.Builder builder = TestMessageSet.CreateBuilder();
  285. TextFormat.Merge(MessageSetText, extensionRegistry, builder);
  286. TestMessageSet messageSet = builder.Build();
  287. Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension1.MessageSetExtension));
  288. Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
  289. Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension2.MessageSetExtension));
  290. Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);
  291. }
  292. [TestMethod]
  293. public void ParseNumericEnum()
  294. {
  295. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  296. TextFormat.Merge("optional_nested_enum: 2", builder);
  297. Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, builder.OptionalNestedEnum);
  298. }
  299. [TestMethod]
  300. public void ParseAngleBrackets()
  301. {
  302. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  303. TextFormat.Merge("OptionalGroup: < a: 1 >", builder);
  304. Assert.IsTrue(builder.HasOptionalGroup);
  305. Assert.AreEqual(1, builder.OptionalGroup.A);
  306. }
  307. [TestMethod]
  308. public void ParseComment()
  309. {
  310. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  311. TextFormat.Merge(
  312. "# this is a comment\n" +
  313. "optional_int32: 1 # another comment\n" +
  314. "optional_int64: 2\n" +
  315. "# EOF comment", builder);
  316. Assert.AreEqual(1, builder.OptionalInt32);
  317. Assert.AreEqual(2, builder.OptionalInt64);
  318. }
  319. private static void AssertParseError(string error, string text)
  320. {
  321. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  322. try
  323. {
  324. TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder);
  325. Assert.Fail("Expected parse exception.");
  326. }
  327. catch (FormatException e)
  328. {
  329. Assert.AreEqual(error, e.Message);
  330. }
  331. }
  332. [TestMethod]
  333. public void ParseErrors()
  334. {
  335. AssertParseError(
  336. "1:16: Expected \":\".",
  337. "optional_int32 123");
  338. AssertParseError(
  339. "1:23: Expected identifier.",
  340. "optional_nested_enum: ?");
  341. AssertParseError(
  342. "1:18: Couldn't parse integer: Number must be positive: -1",
  343. "optional_uint32: -1");
  344. AssertParseError(
  345. "1:17: Couldn't parse integer: Number out of range for 32-bit signed " +
  346. "integer: 82301481290849012385230157",
  347. "optional_int32: 82301481290849012385230157");
  348. AssertParseError(
  349. "1:16: Expected \"true\" or \"false\".",
  350. "optional_bool: maybe");
  351. AssertParseError(
  352. "1:18: Expected string.",
  353. "optional_string: 123");
  354. AssertParseError(
  355. "1:18: String missing ending quote.",
  356. "optional_string: \"ueoauaoe");
  357. AssertParseError(
  358. "1:18: String missing ending quote.",
  359. "optional_string: \"ueoauaoe\n" +
  360. "optional_int32: 123");
  361. AssertParseError(
  362. "1:18: Invalid escape sequence: '\\z'",
  363. "optional_string: \"\\z\"");
  364. AssertParseError(
  365. "1:18: String missing ending quote.",
  366. "optional_string: \"ueoauaoe\n" +
  367. "optional_int32: 123");
  368. AssertParseError(
  369. "1:2: Extension \"nosuchext\" not found in the ExtensionRegistry.",
  370. "[nosuchext]: 123");
  371. AssertParseError(
  372. "1:20: Extension \"protobuf_unittest.optional_int32_extension\" " +
  373. "not found in the ExtensionRegistry.",
  374. "[protobuf_unittest.optional_int32_extension]: 123");
  375. AssertParseError(
  376. "1:1: Message type \"protobuf_unittest.TestAllTypes\" has no field " +
  377. "named \"nosuchfield\".",
  378. "nosuchfield: 123");
  379. AssertParseError(
  380. "1:21: Expected \">\".",
  381. "OptionalGroup < a: 1");
  382. AssertParseError(
  383. "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " +
  384. "value named \"NO_SUCH_VALUE\".",
  385. "optional_nested_enum: NO_SUCH_VALUE");
  386. AssertParseError(
  387. "1:23: Enum type \"protobuf_unittest.TestAllTypes.NestedEnum\" has no " +
  388. "value with number 123.",
  389. "optional_nested_enum: 123");
  390. // Delimiters must match.
  391. AssertParseError(
  392. "1:22: Expected identifier.",
  393. "OptionalGroup < a: 1 }");
  394. AssertParseError(
  395. "1:22: Expected identifier.",
  396. "OptionalGroup { a: 1 >");
  397. }
  398. // =================================================================
  399. private static ByteString Bytes(params byte[] bytes)
  400. {
  401. return ByteString.CopyFrom(bytes);
  402. }
  403. private delegate void FormattingAction();
  404. private static void AssertFormatException(FormattingAction action)
  405. {
  406. try
  407. {
  408. action();
  409. Assert.Fail("Should have thrown an exception.");
  410. }
  411. catch (FormatException)
  412. {
  413. // success
  414. }
  415. }
  416. [TestMethod]
  417. public void Escape()
  418. {
  419. // Escape sequences.
  420. Assert.AreEqual("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
  421. TextFormat.EscapeBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"")));
  422. Assert.AreEqual("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"",
  423. TextFormat.EscapeText("\0\u0001\u0007\b\f\n\r\t\v\\\'\""));
  424. Assert.AreEqual(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\""),
  425. TextFormat.UnescapeBytes("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
  426. Assert.AreEqual("\0\u0001\u0007\b\f\n\r\t\v\\\'\"",
  427. TextFormat.UnescapeText("\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\""));
  428. // Unicode handling.
  429. Assert.AreEqual("\\341\\210\\264", TextFormat.EscapeText("\u1234"));
  430. Assert.AreEqual("\\341\\210\\264", TextFormat.EscapeBytes(Bytes(0xe1, 0x88, 0xb4)));
  431. Assert.AreEqual("\u1234", TextFormat.UnescapeText("\\341\\210\\264"));
  432. Assert.AreEqual(Bytes(0xe1, 0x88, 0xb4), TextFormat.UnescapeBytes("\\341\\210\\264"));
  433. Assert.AreEqual("\u1234", TextFormat.UnescapeText("\\xe1\\x88\\xb4"));
  434. Assert.AreEqual(Bytes(0xe1, 0x88, 0xb4), TextFormat.UnescapeBytes("\\xe1\\x88\\xb4"));
  435. // Errors.
  436. AssertFormatException(() => TextFormat.UnescapeText("\\x"));
  437. AssertFormatException(() => TextFormat.UnescapeText("\\z"));
  438. AssertFormatException(() => TextFormat.UnescapeText("\\"));
  439. }
  440. [TestMethod]
  441. public void ParseInteger()
  442. {
  443. Assert.AreEqual(0, TextFormat.ParseInt32("0"));
  444. Assert.AreEqual(1, TextFormat.ParseInt32("1"));
  445. Assert.AreEqual(-1, TextFormat.ParseInt32("-1"));
  446. Assert.AreEqual(12345, TextFormat.ParseInt32("12345"));
  447. Assert.AreEqual(-12345, TextFormat.ParseInt32("-12345"));
  448. Assert.AreEqual(2147483647, TextFormat.ParseInt32("2147483647"));
  449. Assert.AreEqual(-2147483648, TextFormat.ParseInt32("-2147483648"));
  450. Assert.AreEqual(0u, TextFormat.ParseUInt32("0"));
  451. Assert.AreEqual(1u, TextFormat.ParseUInt32("1"));
  452. Assert.AreEqual(12345u, TextFormat.ParseUInt32("12345"));
  453. Assert.AreEqual(2147483647u, TextFormat.ParseUInt32("2147483647"));
  454. Assert.AreEqual(2147483648U, TextFormat.ParseUInt32("2147483648"));
  455. Assert.AreEqual(4294967295U, TextFormat.ParseUInt32("4294967295"));
  456. Assert.AreEqual(0L, TextFormat.ParseInt64("0"));
  457. Assert.AreEqual(1L, TextFormat.ParseInt64("1"));
  458. Assert.AreEqual(-1L, TextFormat.ParseInt64("-1"));
  459. Assert.AreEqual(12345L, TextFormat.ParseInt64("12345"));
  460. Assert.AreEqual(-12345L, TextFormat.ParseInt64("-12345"));
  461. Assert.AreEqual(2147483647L, TextFormat.ParseInt64("2147483647"));
  462. Assert.AreEqual(-2147483648L, TextFormat.ParseInt64("-2147483648"));
  463. Assert.AreEqual(4294967295L, TextFormat.ParseInt64("4294967295"));
  464. Assert.AreEqual(4294967296L, TextFormat.ParseInt64("4294967296"));
  465. Assert.AreEqual(9223372036854775807L, TextFormat.ParseInt64("9223372036854775807"));
  466. Assert.AreEqual(-9223372036854775808L, TextFormat.ParseInt64("-9223372036854775808"));
  467. Assert.AreEqual(0uL, TextFormat.ParseUInt64("0"));
  468. Assert.AreEqual(1uL, TextFormat.ParseUInt64("1"));
  469. Assert.AreEqual(12345uL, TextFormat.ParseUInt64("12345"));
  470. Assert.AreEqual(2147483647uL, TextFormat.ParseUInt64("2147483647"));
  471. Assert.AreEqual(4294967295uL, TextFormat.ParseUInt64("4294967295"));
  472. Assert.AreEqual(4294967296uL, TextFormat.ParseUInt64("4294967296"));
  473. Assert.AreEqual(9223372036854775807UL, TextFormat.ParseUInt64("9223372036854775807"));
  474. Assert.AreEqual(9223372036854775808UL, TextFormat.ParseUInt64("9223372036854775808"));
  475. Assert.AreEqual(18446744073709551615UL, TextFormat.ParseUInt64("18446744073709551615"));
  476. // Hex
  477. Assert.AreEqual(0x1234abcd, TextFormat.ParseInt32("0x1234abcd"));
  478. Assert.AreEqual(-0x1234abcd, TextFormat.ParseInt32("-0x1234abcd"));
  479. Assert.AreEqual(0xffffffffffffffffUL, TextFormat.ParseUInt64("0xffffffffffffffff"));
  480. Assert.AreEqual(0x7fffffffffffffffL,
  481. TextFormat.ParseInt64("0x7fffffffffffffff"));
  482. // Octal
  483. Assert.AreEqual(342391, TextFormat.ParseInt32("01234567"));
  484. // Out-of-range
  485. AssertFormatException(() => TextFormat.ParseInt32("2147483648"));
  486. AssertFormatException(() => TextFormat.ParseInt32("-2147483649"));
  487. AssertFormatException(() => TextFormat.ParseUInt32("4294967296"));
  488. AssertFormatException(() => TextFormat.ParseUInt32("-1"));
  489. AssertFormatException(() => TextFormat.ParseInt64("9223372036854775808"));
  490. AssertFormatException(() => TextFormat.ParseInt64("-9223372036854775809"));
  491. AssertFormatException(() => TextFormat.ParseUInt64("18446744073709551616"));
  492. AssertFormatException(() => TextFormat.ParseUInt64("-1"));
  493. AssertFormatException(() => TextFormat.ParseInt32("abcd"));
  494. }
  495. [TestMethod]
  496. public void ParseLongString()
  497. {
  498. string longText =
  499. "123456789012345678901234567890123456789012345678901234567890" +
  500. "123456789012345678901234567890123456789012345678901234567890" +
  501. "123456789012345678901234567890123456789012345678901234567890" +
  502. "123456789012345678901234567890123456789012345678901234567890" +
  503. "123456789012345678901234567890123456789012345678901234567890" +
  504. "123456789012345678901234567890123456789012345678901234567890" +
  505. "123456789012345678901234567890123456789012345678901234567890" +
  506. "123456789012345678901234567890123456789012345678901234567890" +
  507. "123456789012345678901234567890123456789012345678901234567890" +
  508. "123456789012345678901234567890123456789012345678901234567890" +
  509. "123456789012345678901234567890123456789012345678901234567890" +
  510. "123456789012345678901234567890123456789012345678901234567890" +
  511. "123456789012345678901234567890123456789012345678901234567890" +
  512. "123456789012345678901234567890123456789012345678901234567890" +
  513. "123456789012345678901234567890123456789012345678901234567890" +
  514. "123456789012345678901234567890123456789012345678901234567890";
  515. TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
  516. TextFormat.Merge("optional_string: \"" + longText + "\"", builder);
  517. Assert.AreEqual(longText, builder.OptionalString);
  518. }
  519. }
  520. }