-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathTransientHashMapTest.fs
More file actions
233 lines (181 loc) · 9.26 KB
/
TransientHashMapTest.fs
File metadata and controls
233 lines (181 loc) · 9.26 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
namespace FSharpx.Collections.Tests
open System
open FSharpx.Collections
open Expecto
open Expecto.Flip
module TransientHashMapTests =
[<CustomComparison; CustomEquality>]
type AlwaysSameHash =
{ Name: string }
interface System.IComparable<AlwaysSameHash> with
member this.CompareTo{ Name = name } =
compare this.Name name
interface IComparable with
member this.CompareTo obj =
match obj with
| :? AlwaysSameHash as other -> (this :> IComparable<_>).CompareTo other
| _ -> invalidArg "obj" "not a AlwaysSameHash"
interface IEquatable<AlwaysSameHash> with
member this.Equals{ Name = name } =
this.Name = name
override this.Equals obj =
match obj with
| :? AlwaysSameHash as other -> (this :> IEquatable<_>).Equals other
| _ -> invalidArg "obj" "not a AlwaysSameHash"
override __.GetHashCode() = 42
[<Tests>]
let testTransientHashMap =
testList
"TransientHashMap"
[
test "two AlwaysSameHash have same hash" {
Expect.equal "AlwaysSameHash" (hash { Name = "Test" }) (hash { Name = "Test" })
Expect.equal "AlwaysSameHash" { Name = "Test" } { Name = "Test" }
Expect.equal "AlwaysSameHash" (hash { Name = "Test1" }) (hash { Name = "Test" })
Expect.notEqual "AlwaysSameHash" { Name = "Test1" } { Name = "Test" }
}
test "empty map should be empty" {
let x = TransientHashMap<int, int>.Empty()
Expect.equal "empty" 0 (x.persistent() |> PersistentHashMap.length)
}
test "empty map should not contain key 0" {
let x = TransientHashMap<int, int>.Empty()
Expect.isFalse "empty" (x.persistent() |> PersistentHashMap.containsKey 1)
}
test "can add null entry to empty map" {
let x = TransientHashMap<string, string>.Empty()
Expect.isFalse "PersistentHashMap.containsKey" (x.persistent() |> PersistentHashMap.containsKey "value")
Expect.isFalse "PersistentHashMap.containsKey" (x.persistent() |> PersistentHashMap.containsKey null)
Expect.isTrue
"PersistentHashMap.containsKey"
(x.Add(null, "Hello").persistent()
|> PersistentHashMap.containsKey null)
}
//https://github.com/fsprojects/FSharpx.Collections/issues/85
test "can add None value to empty map" {
let x = TransientHashMap<string, string option>.Empty()
Expect.isTrue
"PersistentHashMap.containsKey"
(x.Add("Hello", None).persistent()
|> PersistentHashMap.containsKey "Hello")
}
test "can add empty string as key to empty map" {
let x = TransientHashMap<string, string>.Empty()
Expect.isFalse "" (x.persistent() |> PersistentHashMap.containsKey "")
Expect.isFalse "" (x.Add("", "Hello").persistent() |> PersistentHashMap.containsKey null)
Expect.isTrue "" (x.Add("", "Hello").persistent() |> PersistentHashMap.containsKey "")
}
test "can add some integers to empty map" {
let x =
TransientHashMap<int, string>.Empty().Add(1, "h").Add(2, "a").Add(3, "l").Add(4, "l").Add(5, "o").persistent()
Expect.isTrue "PersistentHashMap.containsKey" (x |> PersistentHashMap.containsKey 1)
Expect.isTrue "PersistentHashMap.containsKey" (x |> PersistentHashMap.containsKey 5)
Expect.isFalse "PersistentHashMap.containsKey" (x |> PersistentHashMap.containsKey 6)
}
test "can remove some integers from a map" {
let x =
TransientHashMap<int, string>
.Empty()
.Add(1, "h")
.Add(2, "a")
.Add(3, "l")
.Add(4, "l")
.Add(5, "o")
.Remove(1)
.Remove(4)
.persistent()
Expect.isFalse "PersistentHashMap.containsKey"
<| PersistentHashMap.containsKey 1 x
Expect.isFalse "PersistentHashMap.containsKey"
<| PersistentHashMap.containsKey 4 x
Expect.isTrue "PersistentHashMap.containsKey"
<| PersistentHashMap.containsKey 5 x
Expect.isFalse "PersistentHashMap.containsKey"
<| PersistentHashMap.containsKey 6 x
}
test "can find integers in a map" {
let x =
TransientHashMap<int, string>.Empty().Add(1, "h").Add(2, "a").Add(3, "l").Add(4, "l").Add(5, "o").persistent()
Expect.equal "find" "h" (x |> PersistentHashMap.find 1)
Expect.equal "find" "l" (x |> PersistentHashMap.find 4)
Expect.equal "find" "o" (x |> PersistentHashMap.find 5)
}
test "can lookup integers from a map" {
let x =
TransientHashMap<int, string>.Empty().Add(1, "h").Add(2, "a").Add(3, "l").Add(4, "l").Add(5, "o")
Expect.equal "lookup" "h" x.[1]
Expect.equal "lookup" "l" x.[4]
Expect.equal "lookup" "o" x.[5]
}
test "can add the same key multiple to a map" {
let x =
TransientHashMap<int, string>
.Empty()
.Add(1, "h")
.Add(2, "a")
.Add(3, "l")
.Add(4, "l")
.Add(5, "o")
.Add(4, "a")
.Add(5, "o")
.persistent()
Expect.equal "" "h" <| PersistentHashMap.find 1 x
Expect.equal "" "a" <| PersistentHashMap.find 4 x
Expect.equal "" "o" <| PersistentHashMap.find 5 x
}
test "can add tons of integers to empty map" {
let x = ref(TransientHashMap<int, int>.Empty())
let counter = 1000
for i in 0..counter do
x := (!x).Add(i, i)
let x = (!x).persistent()
for i in 0..counter do
x |> PersistentHashMap.containsKey i |> Expect.isTrue "lookup"
}
test "can lookup tons of integers from a map" {
let x = ref(TransientHashMap<int, int>.Empty())
let counter = 1000
for i in 0..counter do
x := (!x).Add(i, i)
for i in 0..counter do
(!x).[i] |> Expect.equal "lookup" i
}
test "can find tons of integers in a map" {
let x = ref(TransientHashMap<int, int>.Empty())
let counter = 1000
for i in 0..counter do
x := (!x).Add(i, i)
for i in 0..counter do
(!x).[i] |> Expect.equal "lookup" i
}
test "can add keys with colliding hashes to empty map" {
let x = { Name = "Test" }
let y = { Name = "Test1" }
let map =
TransientHashMap<AlwaysSameHash, string>.Empty().Add(x, x.Name).Add(y, y.Name).persistent()
Expect.isTrue "PersistentHashMap.containsKey"
<| PersistentHashMap.containsKey x map
Expect.isTrue "PersistentHashMap.containsKey"
<| PersistentHashMap.containsKey y map
Expect.isFalse "PersistentHashMap.containsKey"
<| PersistentHashMap.containsKey y PersistentHashMap.empty
}
test "can lookup keys with colliding hashes from map" {
let x = { Name = "Test" }
let y = { Name = "Test1" }
let map =
TransientHashMap<AlwaysSameHash, AlwaysSameHash>.Empty().Add(x, x).Add(y, y).persistent()
Expect.equal "find" { Name = "Test" } (map |> PersistentHashMap.find x)
Expect.equal "find" { Name = "Test1" } (map |> PersistentHashMap.find y)
}
test "can add lots of keys with colliding hashes to empty map" {
let x = ref(TransientHashMap<AlwaysSameHash, int>.Empty())
let counter = 1000
for i in 0..counter do
x := (!x).Add({ Name = i.ToString() }, i)
let x = (!x).persistent()
for i in 0..counter do
x
|> PersistentHashMap.containsKey { Name = i.ToString() }
|> Expect.isTrue "PersistentHashMap.containsKey"
} ]