Skip to content

Commit ee3e7a5

Browse files
committed
[benchmark] Add versions of the set tests with class element types
1 parent 191b3c8 commit ee3e7a5

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

benchmark/single-source/SetTests.swift

+111
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,114 @@ public func run_SetIntersect(N: Int) {
103103
}
104104
sink(&and)
105105
}
106+
107+
class Box<T : Hashable where T : Equatable> : Hashable {
108+
var value: T
109+
110+
init(_ v: T) {
111+
value = v
112+
}
113+
114+
var hashValue : Int {
115+
return value.hashValue
116+
}
117+
}
118+
119+
extension Box : Equatable {
120+
}
121+
122+
func ==<T: Equatable>(lhs: Box<T>, rhs: Box<T>) -> Bool {
123+
return lhs.value == rhs.value
124+
}
125+
126+
@inline(never)
127+
public func run_SetIsSubsetOf_OfObjects(N: Int) {
128+
let size = 200
129+
130+
SRand()
131+
132+
var set = Set<Box<Int>>(minimumCapacity: size)
133+
var otherSet = Set<Box<Int>>(minimumCapacity: size)
134+
135+
for _ in 0 ..< size {
136+
set.insert(Box(Int(truncatingBitPattern: Random())))
137+
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
138+
}
139+
140+
var isSubset = false;
141+
for _ in 0 ..< N * 5000 {
142+
isSubset = set.isSubsetOf(otherSet)
143+
if isSubset {
144+
break
145+
}
146+
}
147+
148+
CheckResults(!isSubset, "Incorrect results in SetIsSubsetOf")
149+
}
150+
151+
@inline(never)
152+
func sink(s: inout Set<Box<Int>>) {
153+
}
154+
155+
@inline(never)
156+
public func run_SetExclusiveOr_OfObjects(N: Int) {
157+
let size = 400
158+
159+
SRand()
160+
161+
var set = Set<Box<Int>>(minimumCapacity: size)
162+
var otherSet = Set<Box<Int>>(minimumCapacity: size)
163+
164+
for _ in 0 ..< size {
165+
set.insert(Box(Int(truncatingBitPattern: Random())))
166+
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
167+
}
168+
169+
var xor = Set<Box<Int>>()
170+
for _ in 0 ..< N * 100 {
171+
xor = set.exclusiveOr(otherSet)
172+
}
173+
sink(&xor)
174+
}
175+
176+
@inline(never)
177+
public func run_SetUnion_OfObjects(N: Int) {
178+
let size = 400
179+
180+
SRand()
181+
182+
var set = Set<Box<Int>>(minimumCapacity: size)
183+
var otherSet = Set<Box<Int>>(minimumCapacity: size)
184+
185+
for _ in 0 ..< size {
186+
set.insert(Box(Int(truncatingBitPattern: Random())))
187+
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
188+
}
189+
190+
var or = Set<Box<Int>>()
191+
for _ in 0 ..< N * 100 {
192+
or = set.union(otherSet)
193+
}
194+
sink(&or)
195+
}
196+
197+
@inline(never)
198+
public func run_SetIntersect_OfObjects(N: Int) {
199+
let size = 400
200+
201+
SRand()
202+
203+
var set = Set<Box<Int>>(minimumCapacity: size)
204+
var otherSet = Set<Box<Int>>(minimumCapacity: size)
205+
206+
for _ in 0 ..< size {
207+
set.insert(Box(Int(truncatingBitPattern: Random())))
208+
otherSet.insert(Box(Int(truncatingBitPattern: Random())))
209+
}
210+
211+
var and = Set<Box<Int>>()
212+
for _ in 0 ..< N * 100 {
213+
and = set.intersect(otherSet)
214+
}
215+
sink(&and)
216+
}

benchmark/utils/main.swift

+4
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ precommitTests = [
165165
"SetIntersect": run_SetIntersect,
166166
"SetIsSubsetOf": run_SetIsSubsetOf,
167167
"SetUnion": run_SetUnion,
168+
"SetExclusiveOr_OfObjects": run_SetExclusiveOr_OfObjects,
169+
"SetIntersect_OfObjects": run_SetIntersect_OfObjects,
170+
"SetIsSubsetOf_OfObjects": run_SetIsSubsetOf_OfObjects,
171+
"SetUnion_OfObjects": run_SetUnion_OfObjects,
168172
"SevenBoom": run_SevenBoom,
169173
"Sim2DArray": run_Sim2DArray,
170174
"SortLettersInPlace": run_SortLettersInPlace,

0 commit comments

Comments
 (0)