-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathTestResizeArray.fs
More file actions
359 lines (314 loc) · 10.7 KB
/
Copy pathTestResizeArray.fs
File metadata and controls
359 lines (314 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
module Fable.Tests.ResizeArrays
open Util.Testing
open System.Text
type Animal = Duck of int | Dog of int
[<Fact>]
let ``test ResizeArray zero creation works`` () =
let li = ResizeArray<float>()
equal 0 li.Count
[<Fact>]
let ``test ResizeArray zero creation with size works`` () =
let li = ResizeArray<string>(5)
equal 0 li.Count
[<Fact>]
let ``test ResizeArray creation with seq works`` () =
let li = ResizeArray<_>(seq{1..5})
Seq.sum li
|> equal 15
[<Fact>]
let ``test ResizeArray creation with literal array works`` () =
let li = ResizeArray<_> [|1;2;3;4;5|]
Seq.sum li
|> equal 15
[<Fact>]
let ``test ResizeArray creation with literal list works`` () =
let li = ResizeArray<_> [1;2;3;4;5]
Seq.sum li
|> equal 15
[<Fact>]
let ``test ResizeArray casting to seq works`` () =
let xs = ResizeArray<_>(seq{1..5}) :> seq<_>
Seq.sum xs
|> equal 15
[<Fact>]
let ``test ResizeArray iteration works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
let acc = ref 0.
for i in li do
acc.Value <- acc.Value + i
equal 15. acc.Value
[<Fact>]
let ``test ResizeArray iteration with index works`` () =
let li = ResizeArray<_>()
for i = 1 to 4 do
li.Add(i)
let mutable x = 0
for i = 0 to li.Count - 1 do
x <- x + li.[i]
equal 10 x
[<Fact>]
let ``test ResizeArray folding works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
li |> Seq.fold (fun acc item -> acc + item) 0.
|> equal 15.
[<Fact>]
let ``test ResizeArray.Count works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
equal 5 li.Count
[<Fact>]
let ``test ResizeArray.Find works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
System.Predicate<_> (fun x -> x = 1.) |> li.Find |> equal 1.
System.Predicate<_> (fun x -> x = -1.) |> li.Find |> equal 0.
[<Fact>]
let ``test ResizeArray.Find with option works`` () =
let li = ResizeArray<_>()
li.Add(Some 1); li.Add(None);
System.Predicate<_> (fun _ -> true) |> li.Find |> equal (Some 1)
System.Predicate<_> (fun _ -> false) |> li.Find |> equal None
System.Predicate<_> Option.isNone |> li.Find |> equal None
System.Predicate<_> Option.isSome |> li.Find |> equal (Some 1)
[<Fact>]
let ``test ResizeArray.FindAll works`` () =
let xs = ResizeArray<_> [1.; 2.; 3.; 4.]
System.Predicate<_> (fun x -> x <= 3.) |> xs.FindAll |> (fun l -> l.Count) |> equal 3
System.Predicate<_> (fun x -> x = 5.) |> xs.FindAll |> (fun l -> l.Count) |> equal 0
[<Fact>]
let ``test ResizeArray.FindLast works`` () =
let li = ResizeArray<_>()
li.Add(1.,0.); li.Add(2.,0.); li.Add(3.,0.); li.Add(4.,0.); li.Add(5.,0.); li.Add(1.,1.)
System.Predicate<_> (fun (x, _) -> x = 1.) |> li.FindLast |> snd |> equal 1.
[<Fact>]
let ``test ResizeArray.FindLast with option works`` () =
let li = ResizeArray<_>()
li.Add(Some 1); li.Add(None);
System.Predicate<_> (fun _ -> true) |> li.FindLast |> equal None
System.Predicate<_> (fun _ -> false) |> li.FindLast |> equal None
System.Predicate<_> Option.isSome |> li.FindLast |> equal (Some 1)
[<Fact>]
let ``test ResizeArray.FindIndex works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(2.); li.Add(5.)
System.Predicate<_> (fun x -> x = 2.) |> li.FindIndex |> equal 1
System.Predicate<_> (fun x -> x = 0.) |> li.FindIndex |> equal -1
[<Fact>]
let ``test ResizeArray.FindLastIndex works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(2.); li.Add(5.)
System.Predicate<_> (fun x -> x = 2.) |> li.FindLastIndex |> equal 3
System.Predicate<_> (fun x -> x = 0.) |> li.FindLastIndex |> equal -1
[<Fact>]
let ``test ResizeArray.ForEach works`` () =
let li = ResizeArray<_>()
let mutable sum = 0
li.Add(1); li.Add(2); li.Add(3); li.Add(4); li.Add(5)
System.Action<_> (fun x -> sum <- sum + x) |> li.ForEach
sum |> equal 15
[<Fact>]
let ``test ResizeArray indexer getter works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
equal 2. li.[1]
[<Fact>]
let ``test ResizeArray indexer setter works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
li.[3] <- 10.
equal 10. li.[3]
[<Fact>]
let ``test ResizeArray.Clear works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
li.Clear()
equal 0 li.Count
[<Fact>]
let ``test ResizeArray.Add works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
li.Add(6.)
equal 6 li.Count
[<Fact>]
let ``test ResizeArray.AddRange works`` () =
let li = ResizeArray<_>()
li.AddRange [1;2;3]
equal 3 li.Count
[<Fact>]
let ``test ResizeArray.InsertRange works`` () =
let li = ResizeArray<_>()
li.Add(1); li.Add(2); li.Add(5)
li.InsertRange(2, [3;4])
Seq.toList li |> equal [1;2;3;4;5]
[<Fact>]
let ``test ResizeArray.GetRange works`` () =
let li = ResizeArray<_>()
li.AddRange [1;2;3]
let sub = li.GetRange(1, 2)
sub.Count |> equal 2
sub.Contains(1) |> equal false
[<Fact>]
let ``test ResizeArray.Contains works`` () =
let li = ResizeArray<_>()
li.Add("ab")
li.Add("ch")
li.Contains("ab") |> equal true
li.Contains("cd") |> equal false
[<Fact>]
let ``test ResizeArray.IndexOf works`` () =
let li = ResizeArray<_>()
li.Add("ch")
li.Add("ab")
li.IndexOf("ab") |> equal 1
li.IndexOf("cd") |> equal -1
[<Fact>]
let ``test ResizeArray.Remove works`` () =
let li = ResizeArray<_>()
li.Add("ab")
li.Add("ch")
li.Remove("ab") |> equal true
li.Remove("cd") |> equal false
[<Fact>]
let ``test ResizeArray.RemoveAll works`` () =
let li = ResizeArray<_>()
li.Add("ab")
li.Add("ch")
li.Add("ab")
System.Predicate<_> (fun x -> x = "ab") |> li.RemoveAll |> equal 2
System.Predicate<_> (fun x -> x = "ab") |> li.RemoveAll |> equal 0
li.[0] |> equal "ch"
[<Fact>]
let ``test ResizeArray.RemoveRange works`` () =
let xs = ResizeArray<int>()
for x in [1 .. 5] do xs.Add(x)
xs.RemoveRange(1, 2) // [1;2;3;4;5] -> [1;4;5]
equal 1 xs[0]
equal 4 xs[1]
equal 5 xs[2]
[<Fact>]
let ``test ResizeArray.Exists works`` () =
let xs = ResizeArray<int>()
for x in [1 .. 5] do xs.Add(x)
xs.Exists (fun a -> a > 5) |> equal false
xs.Exists (fun a -> a = 5) |> equal true
xs.Exists (fun a -> a > 1) |> equal true
xs.Exists (fun a -> a = 1) |> equal true
xs.Exists (fun a -> a < 1) |> equal false
xs.Exists (fun a -> a = 3) |> equal true
[<Fact>]
let ``test ResizeArray.RemoveAt works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
li.RemoveAt(2)
equal 4. li.[2]
[<Fact>]
let ``test ResizeArray.Insert works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
li.Insert(2, 8.)
equal 8. li.[2]
[<Fact>]
let ``test ResizeArray.ReverseInPlace works`` () =
let li = ResizeArray<_>()
li.Add(1.); li.Add(2.); li.Add(3.); li.Add(4.); li.Add(5.)
li.Reverse()
equal 2. li.[3]
[<Fact>]
let ``test ResizeArray.SortInPlace works`` () =
let li = ResizeArray<_>()
li.Add("Ana"); li.Add("Pedro"); li.Add("Lucía"); li.Add("Paco")
li.Sort()
equal "Paco" li.[2]
let li2 = ResizeArray [1;3;10;2]
li2.Sort()
equal 2 li2.[1]
(*
[<Fact>]
let ``test ResizeArray.SortInPlaceWith works`` () =
let li = ResizeArray<_>()
li.Add(3.); li.Add(6.); li.Add(5.); li.Add(4.); li.Add(8.)
li.Sort(fun x y -> if x > y then -1 elif x < y then 1 else 0)
equal 4. li.[3]
[<Fact>]
let ``test ResizeArray.SortInPlaceWith works with custom comparison function`` () =
let ns = ResizeArray<int> [1;3;2]
ns.Sort(fun x y -> if x < y then 1 else -1)
Seq.toList ns |> equal [3; 2; 1]
ns.Sort(compare)
Seq.toList ns |> equal [1;2;3]
[<Fact>]
let ``test ResizeArray.SortInPlaceWith works with custom comparer`` () =
let ns = ResizeArray<int> [1;3;2]
let comparer = System.Collections.Generic.Comparer<int>.Default
ns.Sort(comparer)
Seq.toList ns |> equal [1;2;3]
*)
[<Fact>]
let ``test ResizeArray.ToArray works`` () =
let li = ResizeArray<_>()
li.Add(3.); li.Add(6.); li.Add(5.); li.Add(4.); li.Add(8.)
equal 5 li.Count
let ar = li.ToArray()
Array.length ar |> equal li.Count
ar[0] <- 2.
equal 3. li[0]
equal 2. ar[0]
[<Fact>]
let ``test ResizeArray<byte>.ToArray works`` () =
let li = ResizeArray<byte>()
li.Add(0x66uy); li.Add(0x61uy); li.Add(0x62uy); li.Add(0x6cuy); li.Add(0x65uy)
let ar = li.ToArray()
let text = ar |> Encoding.UTF8.GetString
equal text "fable"
[<Fact>]
let ``test ResizeArray.Item is undefined when index is out of range`` () =
let xs = ResizeArray [0]
try (xs.Item 1) |> ignore; false with _ -> true
|> equal true
[<Fact>]
let ``test ResizeArray.Remove works with non-primitive types`` () =
let myResizeArray = ResizeArray<Animal>()
myResizeArray.Add (Duck 5)
myResizeArray.Remove (Duck 3) |> ignore
myResizeArray.Count |> equal 1
myResizeArray.Remove (Dog 5) |> ignore
myResizeArray.Count |> equal 1
myResizeArray.Remove (Duck 5) |> ignore
myResizeArray.Count |> equal 0
[<Fact>]
let ``test ResizeArray.Contains works with non-primitive types`` () =
let myResizeArray = ResizeArray<Animal>()
myResizeArray.Add (Duck 5)
myResizeArray.Contains (Duck 3) |> equal false
myResizeArray.Contains (Dog 5) |> equal false
myResizeArray.Contains (Duck 5) |> equal true
[<Fact>]
let ``test ResizeArray.IndexOf works with non-primitive types`` () =
let myResizeArray = ResizeArray<Animal>()
myResizeArray.Add (Duck 5)
myResizeArray.IndexOf (Duck 3) |> equal -1
myResizeArray.IndexOf (Dog 5) |> equal -1
myResizeArray.IndexOf (Duck 5) |> equal 0
myResizeArray.Add(Dog 3)
myResizeArray.IndexOf(Dog 3) |> equal 1
myResizeArray.IndexOf(Dog 3, 0, 1) |> equal -1
myResizeArray.IndexOf(Duck 5, 1) |> equal -1
[<Fact>]
let ``test ResizeArray works with Seq.item`` () =
let li = ResizeArray([1; 2; 3])
let firstItem = Seq.item 0 li
equal 1 firstItem
let secondItem = Seq.item 1 li
equal 2 secondItem
let thirdItem = Seq.item 2 li
equal 3 thirdItem
[<Fact>]
let ``test ResizeArray uses reference equality not structural equality`` () = // See #3718
let xs = ResizeArray [0; 1; 2]
let ys = ResizeArray [0; 1; 2]
equal false (xs = ys)
equal true (xs = xs)
equal true (xs <> ys)
equal false (xs <> xs)