-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.swift
77 lines (73 loc) · 1.77 KB
/
main.swift
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
//
// File.swift
//
//
// Created by Henry Hathaway on 10/4/19.
//
import Foundation
import HAMT
import GameKit
// This test is too slow without optimization...
// Uses irreproducible random, therefore this test
// is not reproducible.
let c = 1_00_000
var h = HAMT<Int,Int>()
var d = [Int:Int]()
let ns = Array(0..<c).shuffled()
var ks = Set<Int>(0..<c)
for i in 0..<c {
preconditionEqual(h.count, i)
h[i] = i
d[i] = i
preconditionEqual(h[i], i)
if i % 1_000 == 0 {
print("\(i)/\(c) inserted.")
}
}
preconditionEqual(h.count, c)
let r = GKMersenneTwisterRandomSource(seed: 0)
var ec = c
var insertc = 0
var updatec = 0
var removec = 0
for n in 0..<c {
switch abs(r.nextInt()) % 3 {
case 0:
let k = ns[n]
if h[k] == nil {
h[k] = k
d[k] = k
ec += 1
preconditionEqual(h[k], k)
preconditionEqual(h.count, ec)
precondition(h == d)
}
ks.insert(k)
insertc += 1
case 1:
let i = ns[n] % ks.count
let k = ks[ks.index(ks.startIndex, offsetBy: i)]
let v = ns[n]
h[k] = v
d[k] = v
preconditionEqual(h[k], v)
preconditionEqual(h.count, ec)
precondition(h == d)
updatec += 1
case 2:
let i = ns[n] % ks.count
let k = ks[ks.index(ks.startIndex, offsetBy: i)]
ks.remove(k)
precondition(h[k] != nil)
h[k] = nil
d[k] = nil
ec -= 1
preconditionEqual(h[k], nil)
preconditionEqual(h.count, ec)
precondition(h == d)
removec += 1
default:
fatalError("Bug in test code!")
}
print("\(n)/\(c) operations done. (insert/update/remove/count: \(insertc)/\(updatec)/\(removec)/\(h.count))")
}