public void ParseInteger() { Assert.AreEqual(0, TextFormat.ParseInt32("0")); Assert.AreEqual(1, TextFormat.ParseInt32("1")); Assert.AreEqual(-1, TextFormat.ParseInt32("-1")); Assert.AreEqual(12345, TextFormat.ParseInt32("12345")); Assert.AreEqual(-12345, TextFormat.ParseInt32("-12345")); Assert.AreEqual(2147483647, TextFormat.ParseInt32("2147483647")); Assert.AreEqual(-2147483648, TextFormat.ParseInt32("-2147483648")); Assert.AreEqual(0, TextFormat.ParseUInt32("0")); Assert.AreEqual(1, TextFormat.ParseUInt32("1")); Assert.AreEqual(12345, TextFormat.ParseUInt32("12345")); Assert.AreEqual(2147483647, TextFormat.ParseUInt32("2147483647")); Assert.AreEqual(2147483648U, TextFormat.ParseUInt32("2147483648")); Assert.AreEqual(4294967295U, TextFormat.ParseUInt32("4294967295")); Assert.AreEqual(0L, TextFormat.ParseInt64("0")); Assert.AreEqual(1L, TextFormat.ParseInt64("1")); Assert.AreEqual(-1L, TextFormat.ParseInt64("-1")); Assert.AreEqual(12345L, TextFormat.ParseInt64("12345")); Assert.AreEqual(-12345L, TextFormat.ParseInt64("-12345")); Assert.AreEqual(2147483647L, TextFormat.ParseInt64("2147483647")); Assert.AreEqual(-2147483648L, TextFormat.ParseInt64("-2147483648")); Assert.AreEqual(4294967295L, TextFormat.ParseInt64("4294967295")); Assert.AreEqual(4294967296L, TextFormat.ParseInt64("4294967296")); Assert.AreEqual(9223372036854775807L, TextFormat.ParseInt64("9223372036854775807")); Assert.AreEqual(-9223372036854775808L, TextFormat.ParseInt64("-9223372036854775808")); Assert.AreEqual(0L, TextFormat.ParseUInt64("0")); Assert.AreEqual(1L, TextFormat.ParseUInt64("1")); Assert.AreEqual(12345L, TextFormat.ParseUInt64("12345")); Assert.AreEqual(2147483647L, TextFormat.ParseUInt64("2147483647")); Assert.AreEqual(4294967295L, TextFormat.ParseUInt64("4294967295")); Assert.AreEqual(4294967296L, TextFormat.ParseUInt64("4294967296")); Assert.AreEqual(9223372036854775807UL, TextFormat.ParseUInt64("9223372036854775807")); Assert.AreEqual(9223372036854775808UL, TextFormat.ParseUInt64("9223372036854775808")); Assert.AreEqual(18446744073709551615UL, TextFormat.ParseUInt64("18446744073709551615")); // Hex Assert.AreEqual(0x1234abcd, TextFormat.ParseInt32("0x1234abcd")); Assert.AreEqual(-0x1234abcd, TextFormat.ParseInt32("-0x1234abcd")); Assert.AreEqual(0xffffffffffffffffUL, TextFormat.ParseUInt64("0xffffffffffffffff")); Assert.AreEqual(0x7fffffffffffffffL, TextFormat.ParseInt64("0x7fffffffffffffff")); // Octal Assert.AreEqual(342391, TextFormat.ParseInt32("01234567")); // Out-of-range AssertFormatException(() => TextFormat.ParseInt32("2147483648")); AssertFormatException(() => TextFormat.ParseInt32("-2147483649")); AssertFormatException(() => TextFormat.ParseUInt32("4294967296")); AssertFormatException(() => TextFormat.ParseUInt32("-1")); AssertFormatException(() => TextFormat.ParseInt64("9223372036854775808")); AssertFormatException(() => TextFormat.ParseInt64("-9223372036854775809")); AssertFormatException(() => TextFormat.ParseUInt64("18446744073709551616")); AssertFormatException(() => TextFormat.ParseUInt64("-1")); AssertFormatException(() => TextFormat.ParseInt32("abcd")); }
/// <summary> /// If the next token is a 32-bit signed integer, consume it and return its /// value. Otherwise, throw a FormatException. /// </summary> public int ConsumeInt32() { try { int result = TextFormat.ParseInt32(currentToken); NextToken(); return(result); } catch (FormatException e) { throw CreateIntegerParseException(e); } }