@@ -9,16 +9,229 @@ open FSharpPlus.Internals
99#endif
1010
1111module Collections =
12-
12+ open System
13+ open System.Collections
14+ open System.Collections .Generic
15+ open System.Collections .Concurrent
16+ open System.Collections .ObjectModel
17+
1318 [<Test>]
1419 let chunkBy () =
1520 #if TEST_ TRACE
1621 Traces.reset()
1722 #endif
23+
1824 let source = [ 1 ; 2 ; 3 ; 5 ; 7 ; 9 ]
1925 let expected = [( 1 , [ 1 ]); ( 0 , [ 2 ]); ( 1 , [ 3 ; 5 ; 7 ; 9 ])]
2026 let actual = chunkBy ( flip (%) 2 ) source
2127 CollectionAssert.AreEqual( expected, actual)
28+
2229 #if TEST_ TRACE
2330 CollectionAssert.AreEqual ([ " ChunkBy, list<'T>" ], Traces.get())
24- #endif
31+ #endif
32+
33+
34+ let testCollections =
35+ let bigSeq = seq { 1 .. 10000000 }
36+ let bigLst = [ 1 .. 10000000 ]
37+ let bigArr = [| 1 .. 10000000 |]
38+ let bigMut = ResizeArray( seq { 1 .. 10000000 })
39+
40+ let _ = head bigSeq
41+ let _ = head bigLst
42+ let _ = head bigArr
43+
44+ let _ = skip 1000 bigSeq
45+ let _ = skip 1000 bigLst
46+ let _ = skip 1000 bigArr
47+ let _ = skip 1000 bigMut
48+ let _ = " hello world" |> skip 6 |> toList
49+ let _ = ofList [ 'h' ; 'e' ; 'l' ; 'l' ; 'o' ; ' ' ] + " world"
50+ let _ = item 2 " hello"
51+
52+ ()
53+
54+
55+ let testSeqConversions =
56+ let sk : Generic.Stack < _ > = ofSeq { 1 .. 3 }
57+ let sg : string = ofSeq { '1' .. '3' } // but it will come back as seq<char>
58+ let sb : Text.StringBuilder = ofSeq { '1' .. '3' } // but it will come back as seq<char>
59+ let sq1 : _ seq = ofSeq { 1 .. 3 }
60+ let sq2 : _ seq = ofSeq ( seq [( 1 , " One" ); ( 2 , " Two" )])
61+ let sq3 : _ seq = ofSeq ( seq [( 1 , " One" , '1' ); ( 2 , " Two" , '2' )])
62+ let sq4 : _ seq = ofSeq ( seq [( 1 , " One" , '1' , 1 M); ( 2 , " Two" , '2' , 2 M)])
63+ let ls1 : _ list = ofSeq { '1' .. '3' }
64+ let ls2 : _ list = ofSeq ( seq [( 1 , " One" , '1' ); ( 2 , " Two" , '2' )])
65+ let st1 : _ Set = ofSeq { '1' .. '3' }
66+ let st2 : _ Set = ofSeq ( seq [( 1 , " One" , '1' ); ( 2 , " Two" , '2' )])
67+ let ss : Generic.SortedSet < _ > = ofSeq ( seq [ 3 .. 6 ])
68+ let ra : Generic.List < _ > = ofSeq ( seq [ 1 .. 3 ])
69+ let sl : Generic.SortedList < _ , _ > = ofSeq ( seq [( 1 , " One" ); ( 2 , " Two" )]) // but it will come back as ...
70+ let _sl : Generic.SortedList < _ , _ > = ofSeq ( seq [ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
71+ let dc : Generic.Dictionary < _ , _ > = ofSeq ( seq [( 1 , " One" ); ( 2 , " Two" )]) // but it will come back as KeyValuePair
72+ let mp : Map < _ , _ > = ofSeq ( seq [( 1 , " One" ); ( 2 , " Two" )]) // but it will come back as ...
73+ let _mp : Map < _ , _ > = ofSeq ( seq [ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
74+ let d : Generic.IDictionary < _ , _ > = ofSeq ( seq [( " One" , 1 )]) // but it will come back as ...
75+ let _d : Generic.IDictionary < _ , _ > = ofSeq ( seq [ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
76+ let r : IReadOnlyDictionary < _ , _ > = ofSeq ( seq [( " One" , 1 )]) // but it will come back as ...
77+ let _r : IReadOnlyDictionary < _ , _ > = ofSeq ( seq [ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
78+ let rc : IReadOnlyCollection < _ > = ofSeq ( seq [ 2 .. 7 ])
79+ let ut : Hashtable = ofSeq ( seq [ 1 , '1' ; 2 , '2' ; 3 , '3' ]) // but it will come back as seq<obj>
80+ let al : ArrayList = ofSeq ( seq [ " 1" ; " 2" ; " 3" ]) // but it will come back as seq<obj>
81+ let us : SortedList = ofSeq ( seq [ 4 , '2' ; 3 , '4' ]) // but it will come back as seq<obj>
82+ let cc : BlockingCollection < _ > = ofSeq { '1' .. '3' } // but it will come back as seq<obj>
83+ let cd : ConcurrentDictionary < _ , _ > = ofSeq ( seq [( 1 , " One" ); ( 2 , " Two" )]) // but it will come back as ...
84+ let _cd : ConcurrentDictionary < _ , _ > = ofSeq ( seq [ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
85+ let cb : ConcurrentBag < _ > = ofSeq { '1' .. '3' }
86+
87+ // now go back
88+ let _sk ' = toSeq sk
89+ let _sg ' = toSeq sg
90+ let _sb ' = toSeq sb
91+ let _sq1 ' = toSeq sq1
92+ let _sq2 ' = toSeq sq2
93+ let _sq3 ' = toSeq sq3
94+ let _sq4 ' = toSeq sq4
95+ let _ls1 ' = toSeq ls1
96+ let _ls2 ' = toSeq ls2
97+ let _st1 ' = toSeq st1
98+ let _st2 ' = toSeq st2
99+ let _ss ' = toSeq ss
100+ let _ra ' = toSeq ra
101+ let _sl ' = toSeq sl
102+ let _dc ' = toSeq dc
103+ let _mp ' = toSeq mp
104+ let _d ' = toSeq d
105+ let _r ' = toSeq r
106+ let _rc ' = toSeq rc
107+ let _ut ' = toSeq ut
108+ let _al ' = toSeq al
109+ let _us ' = toSeq us
110+ let _cc ' = toSeq cc
111+ let _cd ' = toSeq cd
112+ let _cb ' = toSeq cb
113+
114+ // there are some 'one-way' collections that can only be converted toSeq
115+ let columns =
116+ let d = new Data.DataTable ()
117+ [| new Data.DataColumn " id" ; new Data.DataColumn " column1" ; new Data.DataColumn " column2" |] |> d.Columns.AddRange
118+ d.Columns
119+ let _col1 = columns |> find ( fun x -> x.ColumnName = " column1" )
120+ let _cols = columns |> toList |> map ( fun x -> x.ColumnName)
121+
122+ // Defaults
123+ let _12 : WrappedListI < _ > = seq [ 1 ; 2 ] |> ofSeq
124+
125+ ()
126+
127+ let testListConversions =
128+
129+ // From sequence
130+ let sk : Generic.Stack < _ > = ofList [ 1 .. 3 ]
131+ let sg : string = ofList [ '1' .. '3' ] // but it will come back as seq<char>
132+ let sb : Text.StringBuilder = ofList [ '1' .. '3' ] // but it will come back as seq<char>
133+ let sq1 : _ seq = ofList [ 1 .. 3 ]
134+ let sq2 : _ seq = ofList ([( 1 , " One" ); ( 2 , " Two" )])
135+ let sq3 : _ seq = ofList ([( 1 , " One" , '1' ); ( 2 , " Two" , '2' )])
136+ let sq4 : _ seq = ofList ([( 1 , " One" , '1' , 1 M); ( 2 , " Two" , '2' , 2 M)])
137+ let ls1 : _ list = ofList [ '1' .. '3' ]
138+ let ls2 : _ list = ofList ([( 1 , " One" , '1' ); ( 2 , " Two" , '2' )])
139+ let st1 : _ Set = ofList [ '1' .. '3' ]
140+ let st2 : _ Set = ofList ([( 1 , " One" , '1' ); ( 2 , " Two" , '2' )])
141+ let ss : Generic.SortedSet < _ > = ofList ([ 3 .. 6 ])
142+ let ra : Generic.List < _ > = ofList ([ 1 .. 3 ])
143+ let sl : Generic.SortedList < _ , _ > = ofList ([( 1 , " One" ); ( 2 , " Two" )]) // but it will come back as ...
144+ let _sl : Generic.SortedList < _ , _ > = ofList ([ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
145+ let dc : Generic.Dictionary < _ , _ > = ofList ([( 1 , " One" ); ( 2 , " Two" )]) // but it will come back as KeyValuePair
146+ let mp : Map < _ , _ > = ofList ([( 1 , " One" ); ( 2 , " Two" )]) // but it will come back as ...
147+ let _mp : Map < _ , _ > = ofList ([ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
148+ let d : Generic.IDictionary < _ , _ > = ofList ([( " One" , 1 )]) // but it will come back as ...
149+ let _d : Generic.IDictionary < _ , _ > = ofList ([ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
150+ let r : IReadOnlyDictionary < _ , _ > = ofList ([( " One" , 1 )]) // but it will come back as ...
151+ let _r : IReadOnlyDictionary < _ , _ > = ofList ([ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
152+ let rc : IReadOnlyCollection < _ > = ofList ([ 2 .. 5 ])
153+ let ut : Hashtable = ofList ([ 1 , '1' ; 2 , '2' ; 3 , '3' ]) // but it will come back as seq<obj>
154+ let al : ArrayList = ofList ([ " 1" ; " 2" ; " 3" ]) // but it will come back as seq<obj>
155+ let us : SortedList = ofList ([ 4 , '2' ; 3 , '4' ]) // but it will come back as seq<obj>
156+ let cc : BlockingCollection < _ > = ofList [ '1' .. '3' ] // but it will come back as seq<obj>
157+ let cd : ConcurrentDictionary < _ , _ > = ofList ([( 1 , " One" ); ( 2 , " Two" )]) // but it will come back as ...
158+ let _cd : ConcurrentDictionary < _ , _ > = ofList ([ KeyValuePair( 1 , " One" ); KeyValuePair( 2 , " Two" )])
159+ let cb : ConcurrentBag < _ > = ofList [ '1' .. '3' ]
160+
161+ // now go back
162+ let _sk ' = toList sk
163+ let _sg ' = toList sg
164+ let _sb ' = toList sb
165+ let _sq1 ' = toList sq1
166+ let _sq2 ' = toList sq2
167+ let _sq3 ' = toList sq3
168+ let _sq4 ' = toList sq4
169+ let _ls1 ' = toList ls1
170+ let _ls2 ' = toList ls2
171+ let _st1 ' = toList st1
172+ let _st2 ' = toList st2
173+ let _ss ' = toList ss
174+ let _ra ' = toList ra
175+ let _sl ' = toList sl
176+ let _dc ' = toList dc
177+ let _mp ' = toList mp
178+ let _d ' = toList d
179+ let _r ' = toList r
180+ let _rc ' = toList rc
181+ let _ut ' = toList ut
182+ let _al ' = toList al
183+ let _us ' = toList us
184+ let _cc ' = toList cc
185+ let _cd ' = toList cd
186+ let _cb ' = toList cb
187+
188+ ()
189+
190+ let testSorts =
191+ let _r1 = [ 4 .. 1 ] |> sort
192+ let _r2 = [ 4 .. 1 ] |> sortBy string
193+ let _r3 = seq [ 4 .. 1 ] |> sort
194+ let _r4 = seq [ 4 .. 1 ] |> sortBy string
195+ let _r5 = ResizeArray [ 4 .. 1 ] |> sort
196+ let _r6 = ResizeArray [ 4 .. 1 ] |> sortBy string
197+ ()
198+
199+ let testGeneralizableValues () =
200+ let a : list < _ > = empty
201+ let _ = 0 :: a
202+ let _ = '0' :: a
203+
204+ let b : WrappedSeqA < _ > = empty
205+ let _ = WrappedSeqA [ 0 ] <|> b
206+ let _ = WrappedSeqA [ '0' ] <|> b
207+
208+ ()
209+
210+ [<Test>]
211+ let readOnlyNth () =
212+ let readOnlyCollection = ReadOnlyCollection [| 1 .. 10 |]
213+ let iReadOnlyList = readOnlyCollection :> IReadOnlyList<_>
214+ Assert.AreEqual ( 2 , nth 1 [ 1 .. 10 ])
215+ Assert.AreEqual ( 2 , nth 1 readOnlyCollection)
216+ Assert.AreEqual ( 2 , nth 1 iReadOnlyList)
217+
218+ [<Test>]
219+ let readOnlyNthIndex () =
220+ let l = ListOnlyIndex [ 1 .. 10 ]
221+ Assert.AreEqual ( 2 , nth 1 l)
222+ let rl = ReadOnlyListOnlyIndex [ 1 .. 10 ]
223+ Assert.AreEqual ( 2 , nth 1 rl)
224+
225+ [<Test>]
226+ let choose () =
227+ let d = choose Some (( ofSeq : seq<_*_> -> Dictionary<_,_>) ( seq [ " a" , 1 ; " b" , 2 ]))
228+ Assert.IsInstanceOf< Option< Dictionary< string, int>>> ( Some d)
229+
230+ let d ' = choose Some (( ofSeq : seq<_*_> -> IDictionary<_,_>) ( seq [ " a" , 1 ; " b" , 2 ]))
231+ Assert.IsInstanceOf< Option< IDictionary< string, int>>> ( Some d')
232+
233+ let rd = choose Some (( ofSeq : seq<_*_> -> IReadOnlyDictionary<_,_>) ( seq [ " a" , 1 ; " b" , 2 ]))
234+ Assert.IsInstanceOf< Option< IReadOnlyDictionary< string, int>>> ( Some rd)
235+
236+ let m = choose Some (( ofSeq : seq<_*_> -> Map<_,_>) ( seq [ " a" , 1 ; " b" , 2 ]))
237+ Assert.IsInstanceOf< Option< Map< string, int>>> ( Some m)
0 commit comments