11namespace FSharp.Collections.Immutable.Tests
22
3+ open System
34open System.Buffers
45open FSharp.Collections .Immutable
56open Microsoft.VisualStudio .TestTools .UnitTesting
67
8+ /// <summary >
9+ /// A pool that records the arrays that it lends and what each of them holds when it comes back.
10+ /// </summary >
11+ /// <remarks >
12+ /// The shared pool does not tell which array it gets back, and the next array that it lends need not be that one, so a test
13+ /// of the return needs a pool of its own. The arrays are longer than asked and full of a marker, as an array of the shared
14+ /// pool can be longer and can hold what its last user left.
15+ /// </remarks >
16+ type RecordingPool < 'T > ( marker : 'T ) =
17+ inherit ArrayPool< 'T> ()
18+
19+ let lent = ResizeArray< 'T array>()
20+ let returned = ResizeArray< struct ( 'T array * 'T array)>()
21+
22+ /// The arrays that the pool lent, in the order of the calls.
23+ member _.Lent = lent
24+
25+ /// The arrays that came back, each with a copy of what it held when it came back.
26+ member _.Returned = returned
27+
28+ override _.Rent ( minimumLength : int ) =
29+ let array = Array.create ( minimumLength + 3 ) marker
30+ lent.Add array
31+ array
32+
33+ override _.Return ( array : 'T array , _clearArray : bool ) = returned.Add ( struct ( array, Array.copy array))
34+
735/// The buffer in which ` choose ` collects the elements: an array of its own below ` Buffers.PooledFrom ` elements, one rented
836/// from the pool above, and a list for a result that does not fit the rented array.
937[<TestClass; IndexedListTestCategory>]
1038type ChosenBufferTests () =
1139
40+ /// The marker of the arrays of the recording pool of strings.
41+ static let stale = " stale"
42+
43+ /// Asserts that a pool of strings lent one array and got it back with the written elements cleared and the rest as it was.
44+ static let assertReturnedClean ( pool : RecordingPool < string >) ( written : int ) =
45+ let lent = Assert.ContainsSingle ( pool.Lent, " The buffer must rent one array" )
46+ let struct ( array , contents ) =
47+ Assert.ContainsSingle ( pool.Returned, " The buffer must give one array back" )
48+ Assert.AreSame< string array>( lent, array, " The buffer must give back the array that it rented" )
49+
50+ Assert.IsEmpty (
51+ contents
52+ |> Seq.take written
53+ |> Seq.filter ( fun item -> not ( obj.ReferenceEquals ( item, null ))),
54+ " The array must come back without the elements that the buffer wrote"
55+ )
56+
57+ Assert.IsEmpty (
58+ contents
59+ |> Seq.skip written
60+ |> Seq.filter ( fun item -> not ( obj.ReferenceEquals ( item, stale))),
61+ " The buffer must leave the part of the array that it did not write as it was"
62+ )
63+
1264 [<TestMethod>]
1365 [<DataRow( 0 , 0 ) >]
1466 [<DataRow( 1 , 1 ) >]
@@ -19,7 +71,7 @@ type ChosenBufferTests () =
1971 [<DataRow( 5000 , 17 ) >]
2072 [<DataRow( 5000 , 5000 ) >]
2173 member _. ``the buffer builds the list of the elements that were added`` ( bound : int , count : int ) =
22- let buffer = Buffers.ChosenBuffer< int> bound
74+ use buffer = new Buffers.ChosenBuffer< int> ( bound)
2375
2476 for value in 0 .. count - 1 do
2577 buffer.Add value
@@ -33,11 +85,15 @@ type ChosenBufferTests () =
3385 [<TestMethod>]
3486 member _. ``a result longer than the rented array moves into a list`` () =
3587 let count = Buffers.PooledUpTo + 10
36- let buffer = Buffers.ChosenBuffer< int> count
88+ let pool = RecordingPool< int> - 1
89+ use buffer = new Buffers.ChosenBuffer< int> ( count, pool)
3790
3891 for value in 0 .. count - 1 do
3992 buffer.Add value
4093
94+ Assert.ContainsSingle ( pool.Returned, " The buffer must give the rented array back when its elements move into a list" )
95+ |> ignore
96+
4197 let list = buffer.ToImmutable ()
4298 Assert.HasCount ( count, list, " The buffer must keep all elements of a result longer than the rented array" )
4399 Assert.AreEqual< int>(
@@ -51,21 +107,73 @@ type ChosenBufferTests () =
51107 " The first element after the rented array must follow it"
52108 )
53109 Assert.AreEqual< int>( count - 1 , list[ count - 1 ], " The last element must stay" )
110+ Assert.ContainsSingle ( pool.Returned, " The buffer must not give an array back a second time" )
111+ |> ignore
54112
55- [<TestMethod; DataRow 1024 ; DataRow 5000 >]
56- member _. ``the array that a buffer gives back holds no references`` ( count : int ) =
57- let buffer = Buffers.ChosenBuffer< string> count
113+ [<TestMethod; DataRow 0 ; DataRow 1 ; DataRow( Buffers.PooledFrom - 1 ) >]
114+ member _. ``a short buffer rents no array`` ( bound : int ) =
115+ let pool = RecordingPool< int> - 1
116+ use buffer = new Buffers.ChosenBuffer< int> ( bound, pool)
58117
59- for _ in 1 .. count do
60- buffer.Add " x "
118+ for value in 0 .. bound - 1 do
119+ buffer.Add value
61120
62121 buffer.ToImmutable () |> ignore
63- let rented = ArrayPool< string>. Shared.Rent count
64-
65- try
66- let leftovers =
67- rented
68- |> Array.filter ( fun item -> not ( obj.ReferenceEquals ( item, null )))
69- Assert.IsEmpty ( leftovers, " The pool must not keep the elements that a buffer held" )
70- finally
71- ArrayPool< string>. Shared.Return rented
122+ Assert.IsEmpty ( pool.Lent, " A buffer below the least pooled length must take an array of its own" )
123+
124+ [<TestMethod; DataRow( Buffers.PooledFrom, Buffers.PooledFrom); DataRow( 5000 , 17 ); DataRow( 5000 , 0 ); DataRow( 5000 , 5000 ) >]
125+ member _. ``a buffer that builds its list gives its array back without the elements`` ( bound : int , count : int ) =
126+ let pool = RecordingPool< string> stale
127+
128+ do
129+ use buffer = new Buffers.ChosenBuffer< string> ( bound, pool)
130+
131+ for _ in 1 .. count do
132+ buffer.Add " x"
133+
134+ buffer.ToImmutable () |> ignore
135+
136+ // The buffer is disposed after it built the list, which must not return the array again
137+ assertReturnedClean pool count
138+
139+ [<TestMethod; DataRow( Buffers.PooledFrom, Buffers.PooledFrom); DataRow( 5000 , 17 ); DataRow( 5000 , 0 ) >]
140+ member _. ``a buffer that is disposed before it builds a list gives its array back without the elements``
141+ ( bound : int , count : int )
142+ =
143+ let pool = RecordingPool< string> stale
144+
145+ // As in choose, when the chooser throws: the buffer is disposed and ToImmutable does not run
146+ Assert.ThrowsExactly< InvalidOperationException>(
147+ Action ( fun () ->
148+ use buffer = new Buffers.ChosenBuffer< string> ( bound, pool)
149+
150+ for _ in 1 .. count do
151+ buffer.Add " x"
152+
153+ invalidOp " the chooser throws"
154+ ),
155+ " The exception of the chooser must pass through"
156+ )
157+ |> ignore
158+
159+ assertReturnedClean pool count
160+
161+ [<TestMethod>]
162+ member _. ``an array of values comes back as it was written`` () =
163+ let pool = RecordingPool< int> - 1
164+
165+ do
166+ use buffer = new Buffers.ChosenBuffer< int> ( 5000 , pool)
167+
168+ for value in 0 .. 16 do
169+ buffer.Add value
170+
171+ buffer.ToImmutable () |> ignore
172+
173+ let struct ( _ , contents ) = Assert.ContainsSingle ( pool.Returned, " The buffer must give one array back" )
174+
175+ CollectionAssert.AreEqual (
176+ Array.init 17 id,
177+ Array.take 17 contents,
178+ " An array of values holds no references, so the buffer does not clear it"
179+ )
0 commit comments