1
+ //===--- IndexPathTest.swift -------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2020 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
+ import Foundation
15
+
16
+ let size = 200
17
+ let tags : [ BenchmarkCategory ] = [ . validation, . api, . IndexPath]
18
+
19
+ public let IndexPathTest = [
20
+ BenchmarkInfo ( name: " IndexPathSubscriptMutation " ,
21
+ runFunction: { n in run_IndexPathSubscriptMutation ( n * 1000 , size) } ,
22
+ tags: tags) ,
23
+ BenchmarkInfo ( name: " IndexPathSubscriptRangeMutation " ,
24
+ runFunction: { n in run_IndexPathSubscriptRangeMutation ( n * 1000 , size) } ,
25
+ tags: tags) ,
26
+
27
+ BenchmarkInfo ( name: " IndexPathMaxBeginning " ,
28
+ runFunction: { n in run_IndexPathMaxBeginning ( n * 1000 ) } ,
29
+ tags: tags) ,
30
+ BenchmarkInfo ( name: " IndexPathMaxMiddle " ,
31
+ runFunction: { n in run_IndexPathMaxMiddle ( n * 1000 ) } ,
32
+ tags: tags) ,
33
+ BenchmarkInfo ( name: " IndexPathMaxEnd " ,
34
+ runFunction: { n in run_IndexPathMaxEnd ( n * 1000 ) } ,
35
+ tags: tags) ,
36
+
37
+ BenchmarkInfo ( name: " IndexPathMinBeginning " ,
38
+ runFunction: { n in run_IndexPathMinBeginning ( n * 1000 ) } ,
39
+ tags: tags) ,
40
+ BenchmarkInfo ( name: " IndexPathMinMiddle " ,
41
+ runFunction: { n in run_IndexPathMinMiddle ( n * 1000 ) } ,
42
+ tags: tags) ,
43
+ BenchmarkInfo ( name: " IndexPathMinEnd " ,
44
+ runFunction: { n in run_IndexPathMinEnd ( n * 1000 ) } ,
45
+ tags: tags) ,
46
+ ]
47
+
48
+ @inline ( __always)
49
+ func indexPath( _ size: Int ,
50
+ reversed: Bool = false ) -> IndexPath {
51
+ let indexes = Array ( 0 ..< size)
52
+ return IndexPath ( indexes: reversed ? indexes. reversed ( ) : indexes)
53
+ }
54
+
55
+ @inline ( __always)
56
+ func indexPath( _ size: Int ,
57
+ middle: Int ) -> IndexPath {
58
+ var indexes = Array ( 0 ..< size)
59
+ indexes. insert ( middle, at: ( indexes. count - 1 ) / 2 )
60
+ return IndexPath ( indexes: indexes)
61
+ }
62
+
63
+ // Subscript Mutations
64
+
65
+ @inline ( __always)
66
+ func subscriptMutation( n: Int ,
67
+ mutations: Int ,
68
+ mutate: ( inout IndexPath , Int ) -> ( ) ) {
69
+ for _ in 0 ..< n {
70
+ var ip = indexPath ( size)
71
+
72
+ for i in 0 ..< mutations {
73
+ mutate ( & ip, i)
74
+ }
75
+
76
+ blackHole ( ip)
77
+ }
78
+ }
79
+
80
+ @inline ( never)
81
+ public func run_IndexPathSubscriptMutation( _ n: Int , _ count: Int ) {
82
+ subscriptMutation ( n: n,
83
+ mutations: count,
84
+ mutate: { ip, i in
85
+ ip [ i % 4 ] += 1
86
+ } )
87
+ }
88
+
89
+ @inline ( never)
90
+ public func run_IndexPathSubscriptRangeMutation( _ n: Int , _ count: Int ) {
91
+ subscriptMutation ( n: count,
92
+ mutations: count,
93
+ mutate: { ip, i in
94
+ ip [ 0 ..< i] += [ i]
95
+ } )
96
+ }
97
+
98
+ // Max, Min
99
+
100
+ @inline ( __always)
101
+ func maxMin( n: Int ,
102
+ creator: ( ) -> IndexPath ,
103
+ maxMinFunc: ( inout IndexPath ) -> Int ? ) {
104
+ for _ in 0 ..< n {
105
+ var ip = creator ( )
106
+ let found = maxMinFunc ( & ip) != nil
107
+ blackHole ( found)
108
+ }
109
+ }
110
+
111
+ // Max
112
+
113
+ @inline ( never)
114
+ public func run_IndexPathMaxBeginning( _ n: Int ) {
115
+ maxMin ( n: n, creator: {
116
+ indexPath ( size, reversed: true )
117
+ } , maxMinFunc: { ip in
118
+ ip. max ( )
119
+ } )
120
+ }
121
+
122
+ @inline ( never)
123
+ public func run_IndexPathMaxMiddle( _ n: Int ) {
124
+ maxMin ( n: n, creator: {
125
+ indexPath ( size, middle: size + 1 )
126
+ } , maxMinFunc: { ip in
127
+ ip. max ( )
128
+ } )
129
+ }
130
+
131
+ @inline ( never)
132
+ public func run_IndexPathMaxEnd( _ n: Int ) {
133
+ maxMin ( n: n, creator: {
134
+ indexPath ( size)
135
+ } , maxMinFunc: { ip in
136
+ ip. max ( )
137
+ } )
138
+ }
139
+
140
+ // Min
141
+
142
+ @inline ( never)
143
+ public func run_IndexPathMinBeginning( _ n: Int ) {
144
+ maxMin ( n: n, creator: {
145
+ indexPath ( size)
146
+ } , maxMinFunc: { ip in
147
+ ip. min ( )
148
+ } )
149
+ }
150
+
151
+ @inline ( never)
152
+ public func run_IndexPathMinMiddle( _ n: Int ) {
153
+ maxMin ( n: n, creator: {
154
+ indexPath ( size, middle: - 1 )
155
+ } , maxMinFunc: { ip in
156
+ ip. min ( )
157
+ } )
158
+ }
159
+
160
+ @inline ( never)
161
+ public func run_IndexPathMinEnd( _ n: Int ) {
162
+ maxMin ( n: n, creator: {
163
+ indexPath ( size, reversed: true )
164
+ } , maxMinFunc: { ip in
165
+ ip. min ( )
166
+ } )
167
+ }
0 commit comments