Skip to content

Commit 6915ed5

Browse files
authored
Merge pull request #10279 from natecook1000/nc-grouping-test
2 parents aaa7cfa + 220614c commit 6915ed5

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ set(SWIFT_BENCH_MODULES
4040
single-source/DictTest2
4141
single-source/DictTest3
4242
single-source/DictionaryBridge
43+
single-source/DictionaryGroup
4344
single-source/DictionaryLiteral
4445
single-source/DictionaryRemove
4546
single-source/DictionarySwap
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- DictionaryGroup.swift --------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
@inline(never)
16+
public func run_DictionaryGroup(_ N: Int) {
17+
let count = 100 * N
18+
let result = count / 10
19+
let dict = Dictionary(grouping: 0..<count, by: { $0 % 10 })
20+
CheckResults(dict.count == 10)
21+
CheckResults(dict[0]!.count == result)
22+
}
23+
24+
class Box<T : Hashable> : Hashable {
25+
var value: T
26+
27+
init(_ v: T) {
28+
value = v
29+
}
30+
31+
var hashValue: Int {
32+
return value.hashValue
33+
}
34+
35+
static func ==(lhs: Box, rhs: Box) -> Bool {
36+
return lhs.value == rhs.value
37+
}
38+
}
39+
40+
@inline(never)
41+
public func run_DictionaryGroupOfObjects(_ N: Int) {
42+
let count = 20 * N
43+
let result = count / 10
44+
let dict = Dictionary(grouping: (0..<count).map { Box($0) }, by: { Box($0.value % 10) })
45+
CheckResults(dict.count == 10)
46+
CheckResults(dict[Box(0)]!.count == result)
47+
}

benchmark/utils/main.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import DictTest
4545
import DictTest2
4646
import DictTest3
4747
import DictionaryBridge
48+
import DictionaryGroup
4849
import DictionaryLiteral
4950
import DictionaryRemove
5051
import DictionarySwap
@@ -181,6 +182,8 @@ addTo(&precommitTests, "Dictionary2OfObjects", run_Dictionary2OfObjects)
181182
addTo(&precommitTests, "Dictionary3", run_Dictionary3)
182183
addTo(&precommitTests, "Dictionary3OfObjects", run_Dictionary3OfObjects)
183184
addTo(&precommitTests, "DictionaryBridge", run_DictionaryBridge)
185+
addTo(&precommitTests, "DictionaryGroup", run_DictionaryGroup)
186+
addTo(&precommitTests, "DictionaryGroupOfObjects", run_DictionaryGroupOfObjects)
184187
addTo(&precommitTests, "DictionaryLiteral", run_DictionaryLiteral)
185188
addTo(&precommitTests, "DictionaryOfObjects", run_DictionaryOfObjects)
186189
addTo(&precommitTests, "DictionaryRemove", run_DictionaryRemove)

0 commit comments

Comments
 (0)