|
| 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 Foundation |
| 14 | +import TestsUtils |
| 15 | + |
| 16 | +let size = 200 |
| 17 | +let increasingIndexPath = indexPath(size) |
| 18 | +let decreasingIndexPath = indexPath(size, reversed: true) |
| 19 | +let increasingMaxMiddleIndexPath = indexPath(size, middle: size + 1) |
| 20 | +let increasingMinMiddleIndexPath = indexPath(size, middle: -1) |
| 21 | +let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath] |
| 22 | + |
| 23 | +public let IndexPathTest = [ |
| 24 | + BenchmarkInfo( |
| 25 | + name: "IndexPath.Subscript.Mutation", |
| 26 | + runFunction: run_IndexPathSubscriptMutation, |
| 27 | + tags: tags, |
| 28 | + setUpFunction: { blackHole(increasingIndexPath) }), |
| 29 | + BenchmarkInfo( |
| 30 | + name: "IndexPath.Subscript.Range.Mutation", |
| 31 | + runFunction: run_IndexPathSubscriptRangeMutation, |
| 32 | + tags: tags, |
| 33 | + setUpFunction: { blackHole(increasingIndexPath) }), |
| 34 | + BenchmarkInfo( |
| 35 | + name: "IndexPath.Max", |
| 36 | + runFunction: run_IndexPathMax, |
| 37 | + tags: tags, |
| 38 | + setUpFunction: { |
| 39 | + blackHole(decreasingIndexPath) |
| 40 | + blackHole(increasingMaxMiddleIndexPath) |
| 41 | + blackHole(increasingIndexPath) |
| 42 | + }), |
| 43 | + BenchmarkInfo( |
| 44 | + name: "IndexPath.Min", |
| 45 | + runFunction: run_IndexPathMin, |
| 46 | + tags: tags, |
| 47 | + setUpFunction: { |
| 48 | + blackHole(increasingIndexPath) |
| 49 | + blackHole(increasingMinMiddleIndexPath) |
| 50 | + blackHole(decreasingIndexPath) |
| 51 | + }), |
| 52 | +] |
| 53 | + |
| 54 | +func indexPath(_ size: Int, reversed: Bool = false) -> IndexPath { |
| 55 | + let indexes = Array(0..<size) |
| 56 | + return IndexPath(indexes: reversed ? indexes.reversed() : indexes) |
| 57 | +} |
| 58 | + |
| 59 | +func indexPath(_ size: Int, middle: Int) -> IndexPath { |
| 60 | + var indexes = Array(0..<size) |
| 61 | + indexes.insert(middle, at: (indexes.count - 1) / 2) |
| 62 | + return IndexPath(indexes: indexes) |
| 63 | +} |
| 64 | + |
| 65 | +// Subscript Mutations |
| 66 | + |
| 67 | +@inline(__always) |
| 68 | +func subscriptMutation( |
| 69 | + n: Int, |
| 70 | + mutations: Int, |
| 71 | + indexPath: IndexPath, |
| 72 | + mutate: (inout IndexPath, Int) -> Void |
| 73 | +) { |
| 74 | + for _ in 0..<n { |
| 75 | + for i in 0..<mutations { |
| 76 | + var ip = indexPath |
| 77 | + mutate(&ip, i) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +@inline(never) |
| 83 | +public func run_IndexPathSubscriptMutation(_ n: Int) { |
| 84 | + subscriptMutation( |
| 85 | + n: n * 10, mutations: size, indexPath: increasingIndexPath, |
| 86 | + mutate: { ip, i in |
| 87 | + ip[i % 4] += 1 |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +@inline(never) |
| 92 | +public func run_IndexPathSubscriptRangeMutation(_ n: Int) { |
| 93 | + subscriptMutation( |
| 94 | + n: n, mutations: size, indexPath: increasingIndexPath, |
| 95 | + mutate: { ip, i in |
| 96 | + ip[0..<i] += [i] |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +// Max |
| 101 | + |
| 102 | +@inline(never) |
| 103 | +public func run_IndexPathMax(_ n: Int) { |
| 104 | + for _ in 0..<n * 10 { |
| 105 | + var val: Int? |
| 106 | + // Beginning max |
| 107 | + val = decreasingIndexPath.max() |
| 108 | + blackHole(val) |
| 109 | + // Middle max |
| 110 | + val = increasingMaxMiddleIndexPath.max() |
| 111 | + blackHole(val) |
| 112 | + // End max |
| 113 | + val = increasingIndexPath.max() |
| 114 | + blackHole(val) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Min |
| 119 | + |
| 120 | +@inline(never) |
| 121 | +public func run_IndexPathMin(_ n: Int) { |
| 122 | + for _ in 0..<n * 10 { |
| 123 | + var val: Int? |
| 124 | + // Beginning min |
| 125 | + val = increasingIndexPath.min() |
| 126 | + blackHole(val) |
| 127 | + // Middle min |
| 128 | + val = increasingMinMiddleIndexPath.min() |
| 129 | + blackHole(val) |
| 130 | + // End min |
| 131 | + val = decreasingIndexPath.min() |
| 132 | + blackHole(val) |
| 133 | + } |
| 134 | +} |
0 commit comments