From 16c5a2684deff3c6f02334b58d83d63845b8e096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gro=C3=9F?= Date: Sun, 18 Oct 2026 18:09:07 +0200 Subject: [PATCH] fix: read PostgreSQL 16's 0x/0o/0b integers and digit separators Integer bounds and elements threw on text PostgreSQL 16 and 18 read: `[0x1F,)` and `{1_000,0o17,0b101}`. Every integer ParseValue (Int32Range, Int64Range, Int16Set, Int32Set, Int64Set, and through them the multiranges and wrapper arities) now goes through IntegerLiteral, measured on PostgreSQL 16 and 18: a prefix in either case, one underscore after it or between digits, the magnitude checked before the sign so the minimum is reachable in every base, and an OverflowException past the type as before. `0x`, `0x1__F`, `0x1F_`, `_1`, `00x1` and `0o8` still throw, and the provider never reaches the grammar. IntegerLiteralGrammarTests holds the measured table for int2/int4/int8 (36 of 61 rows fail with the fix reverted; the other 25 are refusals). LiteralCorpusProbeTests gains the edge cases; its 20 #127 candidates are removed as fixed, and the hex spelling of (,max] joins the #132 candidates. Closes #127 Found-By: OT Defect-Class: LIT Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + docs/serialization.md | 4 + src/CodoMetis.ValueRanges/Int32Range.cs | 4 +- src/CodoMetis.ValueRanges/Int64Range.cs | 4 +- .../Internals/IntegerLiteral.cs | 79 +++++++++ src/CodoMetis.ValueRanges/Sets/Int16Set.cs | 7 +- src/CodoMetis.ValueRanges/Sets/Int32Set.cs | 7 +- src/CodoMetis.ValueRanges/Sets/Int64Set.cs | 7 +- .../LiteralCorpusProbeTests.cs | 18 +- .../IntegerLiteralGrammarTests.cs | 122 ++++++++++++++ test/oracles/candidates.json | 154 ++---------------- test/oracles/claims.json | 18 ++ 12 files changed, 273 insertions(+), 152 deletions(-) create mode 100644 src/CodoMetis.ValueRanges/Internals/IntegerLiteral.cs create mode 100644 test/CodoMetis.ValueRanges.Tests/IntegerLiteralGrammarTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f66ae3e..50051a8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ Nothing was removed or renamed. Some calls answer differently, text that parsed - EF Core: `Any` or `Count` with a predicate over an empty captured list of value sets, or of ranges beside a `Contains`, returns `false` or `0`, as in memory. It threw `InvalidOperationException` ("Empty collections are not supported as inline query roots") ([EF Core](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#match-a-column-against-a-list-of-sets)). - Docs: an exclusive `infinity` or `-infinity` date bound excludes the date extreme, which stands for the infinity, so `[2026-01-01,infinity)` is `[2026-01-01,9999-12-30]` from a column and from the literal alike. Ask other tools to write an open end as absent or inclusive ([Domain width](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/architecture.md#domain-width-and-the-boundary)). - Docs: a temporal literal with more than six fraction digits keeps them in memory, while PostgreSQL reads the same text rounded to the microsecond and the EF Core package stores it truncated, so the three can be 1 µs apart ([Literals](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/serialization.md#dates-and-times-are-strict-iso-8601)). +- Literals: integer bounds and elements read PostgreSQL 16's `0x`, `0o` and `0b` prefixes and single underscores between digits, in `Int32Range`, `Int64Range`, their multiranges, `Int16Set`, `Int32Set`, `Int64Set` and the wrapper sets. `[0x1F,)` and `{1_000}` threw `FormatException` ([Literals](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/serialization.md#parse-a-literal)). ## [9.1.0] — 2026-10-17 diff --git a/docs/serialization.md b/docs/serialization.md index 67d157f6..b392b2ef 100644 --- a/docs/serialization.md +++ b/docs/serialization.md @@ -95,6 +95,10 @@ if (Int32Range.TryParse(userInput, null, out var range)) `Parse` and `TryParse` read the invariant literal and **ignore the provider**, as `Guid.Parse` does. The text is PostgreSQL's, and it has no culture. +Integer bounds and elements follow PostgreSQL 16's integer grammar. You can write them in decimal, +or in hexadecimal, octal or binary after `0x`, `0o` or `0b`, with single underscores between digits: +`[0x1F,1_000)` is `[31,1000)`. PostgreSQL 15 and older read decimal digits only. + Decimal bounds follow PostgreSQL's `numeric` grammar: sign, digits, point and exponent. A group separator or a currency symbol fails (`FormatException`). diff --git a/src/CodoMetis.ValueRanges/Int32Range.cs b/src/CodoMetis.ValueRanges/Int32Range.cs index 49b1cd68..16f2b332 100644 --- a/src/CodoMetis.ValueRanges/Int32Range.cs +++ b/src/CodoMetis.ValueRanges/Int32Range.cs @@ -221,8 +221,10 @@ public static Int32Range CreateFinite( }; /// + /// PostgreSQL 16\'s integer grammar — decimal, or 0x, 0o or 0b digits, with single + /// underscores between digits (); the provider is ignored. public static int ParseValue(ReadOnlySpan s, IFormatProvider? provider) - => int.Parse(s, NumberStyles.Integer, provider ?? CultureInfo.InvariantCulture); + => IntegerLiteral.Parse(s); /// /// Parses a PostgreSQL range literal (e.g. [1,10], empty, (,)) diff --git a/src/CodoMetis.ValueRanges/Int64Range.cs b/src/CodoMetis.ValueRanges/Int64Range.cs index 1f5b4428..82389bbf 100644 --- a/src/CodoMetis.ValueRanges/Int64Range.cs +++ b/src/CodoMetis.ValueRanges/Int64Range.cs @@ -228,8 +228,10 @@ public static Int64Range CreateFinite( } /// + /// PostgreSQL 16\'s integer grammar — decimal, or 0x, 0o or 0b digits, with single + /// underscores between digits (); the provider is ignored. public static long ParseValue(ReadOnlySpan s, IFormatProvider? provider) - => long.Parse(s, NumberStyles.Integer, provider ?? CultureInfo.InvariantCulture); + => IntegerLiteral.Parse(s); /// /// Parses a PostgreSQL range literal (e.g. [1,10], empty, (,)) diff --git a/src/CodoMetis.ValueRanges/Internals/IntegerLiteral.cs b/src/CodoMetis.ValueRanges/Internals/IntegerLiteral.cs new file mode 100644 index 00000000..d0460c10 --- /dev/null +++ b/src/CodoMetis.ValueRanges/Internals/IntegerLiteral.cs @@ -0,0 +1,79 @@ +using System.Globalization; +using System.Numerics; + +namespace CodoMetis.ValueRanges.Internals; + +/// +/// An integer bound or element as PostgreSQL 16's int2, int4 and int8 input reads +/// it: an optional sign, then decimal digits, or 0x, 0o or 0b (either case) and +/// digits in that base, with single underscores between digits (and one straight after the prefix), +/// around which is skipped. Until 10.0.0 only the decimal form was +/// read, so [0x1F,) and {1_000}, which the server reads, threw here (#127). +/// +/// +/// Measured on PostgreSQL 16 and 18, which agree: 0x_1F, 0_1 and -0x80000000 +/// (as int4) are read; 0x, 0x1__F, 0x1F_, _1, 00x1, +/// 0x-1, 0o8 and 1e3 are refused. The magnitude is checked against the type +/// before the sign is applied, so the minimum is reachable in every base. Too large a magnitude +/// throws , as the BCL integer parsers do. The provider never reaches +/// it. Shared by the integer ranges' and sets' ParseValue, which every literal, JSON element, +/// TypeConverter and EF Core JSON document reaches. +/// +internal static class IntegerLiteral +{ + internal static T Parse(ReadOnlySpan s) where T : IBinaryInteger, IMinMaxValue + { + // The decimal form without separators, which is nearly every literal. + if (T.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value)) return value; + + var text = LiteralWhiteSpace.Trim(s); + var negative = text.Length > 0 && text[0] == '-'; + if (text.Length > 0 && text[0] is '+' or '-') text = text[1..]; + + var radix = text.Length > 1 && text[0] == '0' + ? text[1] switch { 'x' or 'X' => 16, 'o' or 'O' => 8, 'b' or 'B' => 2, _ => 10 } + : 10; + if (radix != 10) text = text[2..]; + + // Each underscore needs a digit after it, and a digit before it unless it follows the prefix. + UInt128 magnitude = 0; + var digits = 0; + var underscore = false; + var limit = negative ? UInt128.CreateTruncating(T.MaxValue) + 1 : UInt128.CreateTruncating(T.MaxValue); + foreach (var c in text) + { + if (c == '_') + { + if (underscore || digits == 0 && radix == 10) throw Invalid(s); + underscore = true; + continue; + } + + var digit = DigitValue(c); + if (digit >= radix) throw Invalid(s); + + underscore = false; + digits++; + magnitude = magnitude * (uint)radix + (uint)digit; + if (magnitude > limit) throw new OverflowException($"Value was either too large or too small for {typeof(T).Name}: '{LiteralExcerpt.Of(s)}'."); + } + + if (digits == 0 || underscore) throw Invalid(s); + + return negative + ? T.CreateTruncating(Int128.CreateTruncating(UInt128.Zero - magnitude)) + : T.CreateTruncating(magnitude); + } + + private static int DigitValue(char c) => c switch + { + >= '0' and <= '9' => c - '0', + >= 'a' and <= 'f' => c - 'a' + 10, + >= 'A' and <= 'F' => c - 'A' + 10, + _ => int.MaxValue + }; + + private static FormatException Invalid(ReadOnlySpan s) + => new("Expected an integer: an optional sign, then decimal digits, or 0x, 0o or 0b and digits in that base, " + + $"with single underscores between digits, as PostgreSQL 16 reads it: '{LiteralExcerpt.Of(s)}'."); +} diff --git a/src/CodoMetis.ValueRanges/Sets/Int16Set.cs b/src/CodoMetis.ValueRanges/Sets/Int16Set.cs index 3da5e305..c457d2e7 100644 --- a/src/CodoMetis.ValueRanges/Sets/Int16Set.cs +++ b/src/CodoMetis.ValueRanges/Sets/Int16Set.cs @@ -68,8 +68,10 @@ static Int16Set IValueSetFactory.FromTrusted(ImmutableArray.Enumerator GetEnumerator() => _elements.GetEnumerator(); /// + /// PostgreSQL 16\'s integer grammar — decimal, or 0x, 0o or 0b digits, with single + /// underscores between digits (); the provider is ignored. public static short ParseValue(ReadOnlySpan s, IFormatProvider? provider) - => short.Parse(s, NumberStyles.Integer, provider ?? CultureInfo.InvariantCulture); + => IntegerLiteral.Parse(s); /// /// Parses a PostgreSQL array literal (e.g. {1,2}, {}) into a @@ -192,7 +194,8 @@ static Int16Set IValueSetFactory, TElement>.FromTru public ImmutableArray.Enumerator GetEnumerator() => _elements.GetEnumerator(); /// - /// Parses an element with the family's grammar — an optionally signed integer (, invariant), exactly as + /// Parses an element with the family's grammar — an integer as PostgreSQL 16 reads it (decimal, + /// or 0x/0o/0b, with _ between digits), exactly as /// reads it — and hands the element the parsed /// 's invariant text via , which re-runs /// its validation: corrupt data throws rather than materializing. diff --git a/src/CodoMetis.ValueRanges/Sets/Int32Set.cs b/src/CodoMetis.ValueRanges/Sets/Int32Set.cs index f7466a09..5ce4916b 100644 --- a/src/CodoMetis.ValueRanges/Sets/Int32Set.cs +++ b/src/CodoMetis.ValueRanges/Sets/Int32Set.cs @@ -68,8 +68,10 @@ static Int32Set IValueSetFactory.FromTrusted(ImmutableArray public ImmutableArray.Enumerator GetEnumerator() => _elements.GetEnumerator(); /// + /// PostgreSQL 16\'s integer grammar — decimal, or 0x, 0o or 0b digits, with single + /// underscores between digits (); the provider is ignored. public static int ParseValue(ReadOnlySpan s, IFormatProvider? provider) - => int.Parse(s, NumberStyles.Integer, provider ?? CultureInfo.InvariantCulture); + => IntegerLiteral.Parse(s); /// /// Parses a PostgreSQL array literal (e.g. {1,2}, {}) into an @@ -187,7 +189,8 @@ static Int32Set IValueSetFactory, TElement>.FromTru public ImmutableArray.Enumerator GetEnumerator() => _elements.GetEnumerator(); /// - /// Parses an element with the family's grammar — an optionally signed integer (, invariant), exactly as + /// Parses an element with the family's grammar — an integer as PostgreSQL 16 reads it (decimal, + /// or 0x/0o/0b, with _ between digits), exactly as /// reads it — and hands the element the parsed /// 's invariant text via , which re-runs /// its validation: corrupt data throws rather than materializing. diff --git a/src/CodoMetis.ValueRanges/Sets/Int64Set.cs b/src/CodoMetis.ValueRanges/Sets/Int64Set.cs index ebf52e8b..306400c3 100644 --- a/src/CodoMetis.ValueRanges/Sets/Int64Set.cs +++ b/src/CodoMetis.ValueRanges/Sets/Int64Set.cs @@ -68,8 +68,10 @@ static Int64Set IValueSetFactory.FromTrusted(ImmutableArray.Enumerator GetEnumerator() => _elements.GetEnumerator(); /// + /// PostgreSQL 16\'s integer grammar — decimal, or 0x, 0o or 0b digits, with single + /// underscores between digits (); the provider is ignored. public static long ParseValue(ReadOnlySpan s, IFormatProvider? provider) - => long.Parse(s, NumberStyles.Integer, provider ?? CultureInfo.InvariantCulture); + => IntegerLiteral.Parse(s); /// /// Parses a PostgreSQL array literal (e.g. {1,2}, {}) into an @@ -187,7 +189,8 @@ static Int64Set IValueSetFactory, TElement>.FromTru public ImmutableArray.Enumerator GetEnumerator() => _elements.GetEnumerator(); /// - /// Parses an element with the family's grammar — an optionally signed integer (, invariant), exactly as + /// Parses an element with the family's grammar — an integer as PostgreSQL 16 reads it (decimal, + /// or 0x/0o/0b, with _ between digits), exactly as /// reads it — and hands the element the parsed /// 's invariant text via , which re-runs /// its validation: corrupt data throws rather than materializing. diff --git a/test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/LiteralCorpusProbeTests.cs b/test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/LiteralCorpusProbeTests.cs index a39d6a62..e2241bad 100644 --- a/test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/LiteralCorpusProbeTests.cs +++ b/test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/LiteralCorpusProbeTests.cs @@ -523,9 +523,10 @@ private sealed record ElementCorpus(string[] Ascending, (string Text, string Fam private static ElementCorpus Elements(Kind kind) => kind switch { - Kind.Int2 => Integers("32767", "-32768", "32768", "-32769"), - Kind.Int4 => Integers("2147483647", "-2147483648", "2147483648", "-2147483649"), - Kind.Int8 => Integers("9223372036854775807", "-9223372036854775808", "9223372036854775808", "-9223372036854775809"), + Kind.Int2 => Integers("32767", "-32768", "32768", "-32769", "0x7FFF", "-0x8000", "0x8000"), + Kind.Int4 => Integers("2147483647", "-2147483648", "2147483648", "-2147483649", "0x7FFFFFFF", "-0x80000000", "0x80000000"), + Kind.Int8 => Integers("9223372036854775807", "-9223372036854775808", "9223372036854775808", "-9223372036854775809", + "0x7FFFFFFFFFFFFFFF", "-0x8000000000000000", "0x8000000000000000"), Kind.Numeric => new(["1.5", "2", "5.25", "9"], [ ("1.5", "element"), ("+1.5", "sign"), ("-1.5", "sign"), ("-0", "sign"), (".5", "point"), ("5.", "point"), @@ -597,11 +598,20 @@ private sealed record ElementCorpus(string[] Ascending, (string Text, string Fam _ => throw new ArgumentOutOfRangeException(nameof(kind)) }; - private static ElementCorpus Integers(string max, string min, string aboveMax, string belowMin) => new(["1", "2", "5", "9"], + private static ElementCorpus Integers(string max, string min, string aboveMax, string belowMin, string hexMax, string hexMin, string hexAboveMax) => new(["1", "2", "5", "9"], [ ("1", "element"), ("+1", "sign"), ("-1", "sign"), ("-0", "sign"), ("007", "leading zeros"), ("1e3", "exponent"), ("1.0", "fraction"), ("1.5", "fraction"), (max, "extreme"), (min, "extreme"), (aboveMax, "overflow"), (belowMin, "overflow"), ("0x1F", "non-decimal integer"), ("0o17", "non-decimal integer"), ("0b101", "non-decimal integer"), ("1_000", "digit separator integer"), + // PostgreSQL 16's integer grammar at its edges (#127), measured on 16 and 18: a prefix in either + // case, a sign before it, one underscore after it, the extremes in hex with the sign applied last. + ("0X1F", "non-decimal integer"), ("-0x1F", "non-decimal integer"), ("+0b101", "non-decimal integer"), ("0x_1F", "non-decimal integer"), + ("0o_17", "non-decimal integer"), ("0x000000000000001F", "non-decimal integer"), (hexMax, "non-decimal extreme"), (hexMin, "non-decimal extreme"), + (hexAboveMax, "non-decimal overflow"), ("0_1", "digit separator integer"), ("1_000_0", "digit separator integer"), + ("0x", "malformed non-decimal integer"), ("0x1__F", "malformed non-decimal integer"), ("0x1F_", "malformed non-decimal integer"), + ("00x1", "malformed non-decimal integer"), ("0x-1", "malformed non-decimal integer"), ("0o8", "malformed non-decimal integer"), + ("0b2", "malformed non-decimal integer"), ("0x 1F", "malformed non-decimal integer"), ("0_x1", "malformed non-decimal integer"), + ("_1", "malformed digit separator"), ("1_", "malformed digit separator"), ("1__000", "malformed digit separator"), ("-_1", "malformed digit separator"), ("\uFF11", "unicode digit"), ("\u0663", "unicode digit"), ("1 2", "malformed element"), ("- 1", "malformed element"), ("NaN", "numeric special"), ("infinity", "numeric special") ]); diff --git a/test/CodoMetis.ValueRanges.Tests/IntegerLiteralGrammarTests.cs b/test/CodoMetis.ValueRanges.Tests/IntegerLiteralGrammarTests.cs new file mode 100644 index 00000000..e4ce1a47 --- /dev/null +++ b/test/CodoMetis.ValueRanges.Tests/IntegerLiteralGrammarTests.cs @@ -0,0 +1,122 @@ +using System.Globalization; + +namespace CodoMetis.ValueRanges.Tests; + +/// +/// The integer bound and element grammar is PostgreSQL 16's int2/int4/int8 input: +/// decimal, or 0x, 0o or 0b digits, with single underscores between digits. Until +/// 10.0.0 only the decimal form was read, so [0x1F,) and {1_000} threw (#127). +/// +/// +/// Every row is PostgreSQL's answer, measured on 16 and 18 (they agree) with +/// pg_input_is_valid(text, 'int2' / 'int4' / 'int8') and text::int8::text; the last +/// column is the int8 value, or where int8 refuses the text. +/// LiteralCorpusProbeTests (integration) asks a live server about the same forms in a bound +/// and an element for every integer range, multirange and set type. +/// +[TestClass] +public class IntegerLiteralGrammarTests +{ + [TestMethod] + [DataRow("0x1F", true, true, "31")] + [DataRow("0X1F", true, true, "31")] + [DataRow("0x1f", true, true, "31")] + [DataRow("-0x1F", true, true, "-31")] + [DataRow("+0x1F", true, true, "31")] + [DataRow("0x", false, false, null)] + [DataRow("0x_1F", true, true, "31")] + [DataRow("0x1_F", true, true, "31")] + [DataRow("0x1__F", false, false, null)] + [DataRow("0x1F_", false, false, null)] + [DataRow("_1", false, false, null)] + [DataRow("1_", false, false, null)] + [DataRow("1__000", false, false, null)] + [DataRow("1_000", true, true, "1000")] + [DataRow("1_000_000", false, true, "1000000")] + [DataRow("0_1", true, true, "1")] + [DataRow("01_0", true, true, "10")] + [DataRow("0o17", true, true, "15")] + [DataRow("0O17", true, true, "15")] + [DataRow("0o8", false, false, null)] + [DataRow("0o_17", true, true, "15")] + [DataRow("0b101", true, true, "5")] + [DataRow("0B101", true, true, "5")] + [DataRow("0b2", false, false, null)] + [DataRow("0b_1", true, true, "1")] + [DataRow("00x1", false, false, null)] + [DataRow("0x 1F", false, false, null)] + [DataRow(" 0x1F ", true, true, "31")] + [DataRow("0x7FFFFFFF", false, true, "2147483647")] + [DataRow("0x80000000", false, false, "2147483648")] + [DataRow("-0x80000000", false, true, "-2147483648")] + [DataRow("-0x80000001", false, false, "-2147483649")] + [DataRow("0xFFFFFFFF", false, false, "4294967295")] + [DataRow("0x0", true, true, "0")] + [DataRow("-0", true, true, "0")] + [DataRow("0x-1", false, false, null)] + [DataRow("- 0x1", false, false, null)] + [DataRow("0xG", false, false, null)] + [DataRow("1e3", false, false, null)] + [DataRow("0x1F.0", false, false, null)] + [DataRow("\u0663", false, false, null)] + [DataRow("0x1F\u00A0", false, false, null)] + [DataRow("0x000000000000001F", true, true, "31")] + [DataRow("1_0x", false, false, null)] + [DataRow("0b", false, false, null)] + [DataRow("0o", false, false, null)] + [DataRow("-_1", false, false, null)] + [DataRow("+_1", false, false, null)] + [DataRow("0_x1", false, false, null)] + [DataRow("0x7FFFFFFFFFFFFFFF", false, false, "9223372036854775807")] + [DataRow("-0x8000000000000000", false, false, "-9223372036854775808")] + [DataRow("0x8000000000000000", false, false, null)] + [DataRow("0x7FFF", true, true, "32767")] + [DataRow("-0x8000", true, true, "-32768")] + [DataRow("0x8000", false, true, "32768")] + [DataRow("-0b1000000000000000", true, true, "-32768")] + [DataRow("0o77777", true, true, "32767")] + public void ParseValue_ReadsWhatPostgresReads(string text, bool int2, bool int4, string? int8) + { + Assert.AreEqual(int8, Read(() => Int64Range.ParseValue(text, null)), "Int64Range"); + Assert.AreEqual(int8, Read(() => Int64Set.ParseValue(text, null)), "Int64Set"); + Assert.AreEqual(int4 ? int8 : null, Read(() => Int32Range.ParseValue(text, null)), "Int32Range"); + Assert.AreEqual(int4 ? int8 : null, Read(() => Int32Set.ParseValue(text, null)), "Int32Set"); + Assert.AreEqual(int2 ? int8 : null, Read(() => Int16Set.ParseValue(text, null)), "Int16Set"); + } + + /// + /// The forms reach every literal: a range bound, a multirange bound and a set element, each read + /// as the number it spells. + /// + [TestMethod] + public void Literals_ReadTheForms() + { + Assert.AreEqual(Int32Range.CreateUnboundedEnd(31), Int32Range.Parse("[0x1F,)", CultureInfo.InvariantCulture)); + Assert.AreEqual(Int64Range.CreateFinite(-15, 1_000), Int64Range.Parse("[-0o17,1_000]", CultureInfo.InvariantCulture)); + Assert.AreEqual(Int32Set.From(1000, 15, 5), Int32Set.Parse("{1_000,0o17,0b101}", CultureInfo.InvariantCulture)); + Assert.AreEqual(Int16Set.From(short.MinValue), Int16Set.Parse("{-0x8000}", CultureInfo.InvariantCulture)); + Assert.AreEqual( + RangeSet.From([Int32Range.CreateFinite(1, 2)]), + RangeSet.Parse("{[0b1,0x2]}", CultureInfo.InvariantCulture)); + } + + /// A magnitude too large for the type overflows, as the decimal form always has. + [TestMethod] + [DataRow("0x80000000")] + [DataRow("-0x80000001")] + [DataRow("0b1_0000_0000_0000_0000_0000_0000_0000_0000")] + public void ParseValue_Overflows_BeyondTheType(string text) + => Assert.ThrowsExactly(() => Int32Range.ParseValue(text, null)); + + private static string? Read(Func parse) where T : IFormattable + { + try + { + return parse().ToString(null, CultureInfo.InvariantCulture); + } + catch (Exception ex) when (ex is FormatException or OverflowException) + { + return null; + } + } +} diff --git a/test/oracles/candidates.json b/test/oracles/candidates.json index 1e7406d9..d2d36f53 100644 --- a/test/oracles/candidates.json +++ b/test/oracles/candidates.json @@ -1,144 +1,4 @@ [ - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int16Set digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int16Set non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int16Set digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int16Set non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int32Range digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int32Range non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int32Set digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int32Set non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int32Set digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int32Set non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int64Range digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int64Range non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int64Set digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int64Set non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int64Set digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "Int64Set non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "RangeSet digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "RangeSet non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "RangeSet digit separator integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, - { - "oracle": "LiteralCorpusProbeTests", - "row": "RangeSet non-decimal integer ", - "family": "integer input refuses 0x, 0o, 0b and digit separators that PostgreSQL 16 reads", - "issue": "#127", - "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" - }, { "oracle": "LiteralCorpusProbeTests", "row": "DateSet dimension decoration ", @@ -404,5 +264,19 @@ "family": "(,max] needs max + 1 to canonicalize; PostgreSQL raises 22003", "issue": "#132", "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" + }, + { + "oracle": "LiteralCorpusProbeTests", + "row": "Int32Range non-decimal extreme (,0x7FFFFFFF]:", + "family": "(,max] needs max + 1 to canonicalize; PostgreSQL raises 22003", + "issue": "#132", + "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" + }, + { + "oracle": "LiteralCorpusProbeTests", + "row": "Int64Range non-decimal extreme (,0x7FFFFFFFFFFFFFFF]:", + "family": "(,max] needs max + 1 to canonicalize; PostgreSQL raises 22003", + "issue": "#132", + "method": "EveryEntryPoint_ReadsTheLiteralAsPostgresReadsIt" } ] diff --git a/test/oracles/claims.json b/test/oracles/claims.json index aeeda881..3cc77ff3 100644 --- a/test/oracles/claims.json +++ b/test/oracles/claims.json @@ -1491,6 +1491,24 @@ "RangeParseFormatTests" ] }, + { + "id": "J39-integer-grammar", + "source": "docs/serialization.md#parse-a-literal", + "quote": "Integer bounds and elements follow PostgreSQL 16's integer grammar. You can write them in decimal, or in hexadecimal, octal or binary after `0x`, `0o` or `0b`, with single underscores between digits: `[0x1F,1_000)` is `[31,1000)`.", + "claim": "Every integer range, multirange and value set reads 0x/0o/0b prefixes and single digit separators as PostgreSQL 16 and 18 read them, accepts and refuses the same text, and overflows past the type's extremes.", + "oracle": "differential", + "surfaces": [ + "Int32Range.ParseValue", + "Int64Range.ParseValue", + "Int16Set.ParseValue", + "Int32Set.ParseValue", + "Int64Set.ParseValue" + ], + "heldBy": [ + "IntegerLiteralGrammarTests", + "LiteralCorpusProbeTests" + ] + }, { "id": "M01-rangeset-invariant-on-construction", "source": "docs/ranges.md#rangeset--multirange-support",