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