Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OrderedDictionary] forward ordered dictionary values equality to values property #335

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Benchmarks/Benchmarks/OrderedDictionaryBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,5 +482,61 @@ extension Benchmark {
blackHole(d)
}
}

self.add(
title: "OrderedDictionary<Int, Int> equality different instance",
input: [Int].self
) { input in
let keysAndValues = input.map { ($0, 2 * $0) }
let left = OrderedDictionary(uniqueKeysWithValues: keysAndValues)
let right = OrderedDictionary(uniqueKeysWithValues: keysAndValues)
return { timer in
timer.measure {
precondition(left == right)
}
}
}

self.add(
title: "OrderedDictionary<Int, Int> equality same instance",
input: [Int].self
) { input in
let keysAndValues = input.map { ($0, 2 * $0) }
let left = OrderedDictionary(uniqueKeysWithValues: keysAndValues)
let right = left
return { timer in
timer.measure {
precondition(left == right)
}
}
}

self.add(
title: "OrderedDictionary<Int, Int>.Values equality different instance",
input: [Int].self
) { input in
let keysAndValues = input.map { ($0, 2 * $0) }
let left = OrderedDictionary(uniqueKeysWithValues: keysAndValues)
let right = OrderedDictionary(uniqueKeysWithValues: keysAndValues)
return { timer in
timer.measure {
precondition(left.values == right.values)
}
}
}

self.add(
title: "OrderedDictionary<Int, Int>.Values equality same instance",
input: [Int].self
) { input in
let keysAndValues = input.map { ($0, 2 * $0) }
let left = OrderedDictionary(uniqueKeysWithValues: keysAndValues)
let right = left
return { timer in
timer.measure {
precondition(left.values == right.values)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ extension OrderedDictionary.Values: MutableCollection {
extension OrderedDictionary.Values: Equatable where Value: Equatable {
@inlinable
public static func ==(left: Self, right: Self) -> Bool {
left.elementsEqual(right)
left._base._values == right._base._values
}
}

Expand Down
16 changes: 16 additions & 0 deletions Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ public func expectEqualElements<S1: Sequence, S2: Sequence>(
message, trapping: trapping, file: file, line: line)
}

public func expectNotEqualElements<S1: Sequence, S2: Sequence>(
_ left: S1,
_ right: S2,
_ message: @autoclosure () -> String = "",
trapping: Bool = false,
file: StaticString = #file,
line: UInt = #line
) where S1.Element == S2.Element, S1.Element: Equatable {
let left = Array(left)
let right = Array(right)
if !left.elementsEqual(right) { return }
_expectFailure(
"'\(left)' does have equal elements to '\(right)'",
message, trapping: trapping, file: file, line: line)
}

vanvoorden marked this conversation as resolved.
Show resolved Hide resolved
public func expectEquivalentElements<S1: Sequence, S2: Sequence>(
_ left: S1,
_ right: S2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,40 @@ import XCTest
import _CollectionsTestSupport

class OrderedDictionaryValueTests: CollectionTestCase {
func test_values_getter() {
func test_values_getter_equal() {
let left: OrderedDictionary = [
"one": 1,
"two": 2,
"three": 3,
"four": 4,
]
let right: OrderedDictionary = [
"one": 1,
"two": 2,
"three": 3,
"four": 4,
]
expectEqual(left.values, right.values)
vanvoorden marked this conversation as resolved.
Show resolved Hide resolved
}

func test_values_getter_not_equal() {
let left: OrderedDictionary = [
"one": 1,
"two": 2,
"three": 3,
"four": 4,
]
let right: OrderedDictionary = [
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
]
expectNotEqual(left.values, right.values)
}

func test_values_getter_equal_elements() {
let d: OrderedDictionary = [
"one": 1,
"two": 2,
Expand All @@ -24,6 +57,16 @@ class OrderedDictionaryValueTests: CollectionTestCase {
]
expectEqualElements(d.values, [1, 2, 3, 4])
}

func test_values_getter_not_equal_elements() {
let d: OrderedDictionary = [
"one": 1,
"two": 2,
"three": 3,
"four": 4,
]
expectNotEqualElements(d.values, [1, 2, 3, 4, 5])
}
vanvoorden marked this conversation as resolved.
Show resolved Hide resolved

func test_values_RandomAccessCollection() {
withEvery("count", in: 0 ..< 30) { count in
Expand Down