Skip to content

Commit 5d34d76

Browse files
xperiandriclaude
andcommitted
fixup! test: cover EquatableArray in F#
Regression tests for the two fixes: a cycle through a mutable record and a tree of 10 000 levels show "..." without a stack overflow, a tree of 50 levels shows its leaf, a later display starts at depth 0 again, and slices with extreme reversed bounds are empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a18ee1e commit 5d34d76

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎tests/FSharp.Collections.Immutable.Tests/EquatableArray/EquatableArrayFormattingTests.fs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ type EquatableArrayFormattingTests () =
2727
let nested = ea [ ea [ 1; 2 ]; EquatableArray.empty ]
2828
Assert.AreEqual ("[|[|1; 2|]; [||]|]", $"%A{nested}", "a nested array must show its elements, not '...'")
2929

30+
[<TestMethod>]
31+
member _.``a cycle through a mutable record stops with ... instead of overflowing the stack`` () =
32+
let node = { Children = EquatableArray.empty }
33+
node.Children <- EquatableArray.singleton node
34+
// Object.ToString is annotated as nullable; this override never returns null
35+
Assert.Contains ("...", nonNull (node.Children.ToString ()), "ToString must stop the cycle with '...'")
36+
Assert.Contains ("...", $"%A{node}", "%A must stop the cycle with '...'")
37+
38+
[<TestMethod>]
39+
member _.``a tree deeper than the depth limit ends in ... and a shallower tree is shown completely`` () =
40+
let deepen (depth : int) =
41+
let mutable tree = Leaf 0
42+
43+
for _ in 1..depth do
44+
tree <- Branch (EquatableArray.singleton tree)
45+
46+
tree
47+
48+
let deep = $"%A{deepen 10_000}"
49+
Assert.Contains ("...", deep, "a tree deeper than the limit must end in '...'")
50+
Assert.DoesNotContain ("Leaf 0", deep, "the leaf below the limit must not be shown")
51+
Assert.Contains ("Leaf 0", $"%A{deepen 50}", "a tree inside the limit must be shown down to its leaf")
52+
Assert.AreEqual ("[|[|1|]|]", (ea [ ea [ 1 ] ]).ToString(), "a display after a deep one must start at depth 0 again")
53+
3054
[<TestMethod>]
3155
member _.``a record field shows the elements`` () =
3256
let document = { Name = "x"; Tags = ea [ "a"; "b" ] }

‎tests/FSharp.Collections.Immutable.Tests/EquatableArray/EquatableArrayInteropTests.fs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ type EquatableArrayInteropTests () =
8787
check source[5..9] array[5..9] "a range after the end must be empty"
8888
check source[2..1] array[2..1] "a reversed range must be empty"
8989

90+
[<TestMethod>]
91+
member _.``F# slices with extreme reversed bounds are empty`` () =
92+
// The length of such a range overflows when it is computed before the bounds are compared
93+
let array = ea [ 1; 2; 3; 4 ]
94+
Assert.IsTrue (array[2 .. Int32.MinValue].IsEmpty, "a finish of Int32.MinValue must give the empty array")
95+
Assert.IsTrue (array[Int32.MaxValue .. Int32.MinValue].IsEmpty, "the most reversed range must give the empty array")
96+
Assert.IsTrue (array[Int32.MaxValue ..].IsEmpty, "a start of Int32.MaxValue must give the empty array")
97+
9098
[<TestMethod>]
9199
member _.``an F# slice of all elements shares the array and other slices copy`` () =
92100
let array = ea [ 1; 2; 3; 4 ]

‎tests/FSharp.Collections.Immutable.Tests/EquatableArray/EquatableArrayTestTypes.fs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type SyntaxNode =
1818
| Leaf of value : int
1919
| Branch of children : EquatableArray<SyntaxNode>
2020

21+
/// A record that can hold itself through a mutable field, to show that the display of a cycle stops.
22+
type CyclicNode = { mutable Children : EquatableArray<CyclicNode> }
23+
2124
/// An F# struct record as an element type.
2225
[<Struct>]
2326
type Point = { X : int; Y : int }

0 commit comments

Comments
 (0)