Skip to content

[tests] basic min/max test cases #566

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

Merged
merged 2 commits into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 4 additions & 16 deletions stdlib/public/core/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,15 @@ public func min<T : Comparable>(x: T, _ y: T, _ z: T, _ rest: T...) -> T {
/// Returns the greater of `x` and `y`.
@warn_unused_result
public func max<T : Comparable>(x: T, _ y: T) -> T {
var r = y
if y < x {
r = x
}
return r
return y >= x ? y : x
}

/// Returns the greatest argument passed.
@warn_unused_result
public func max<T : Comparable>(x: T, _ y: T, _ z: T, _ rest: T...) -> T {
var r = y
if y < x {
r = x
}
if r < z {
r = z
}
for t in rest {
if t >= r {
r = t
}
var r = max(max(x, y), z)
for t in rest where t >= r {
r = t
}
return r
}
Expand Down
60 changes: 52 additions & 8 deletions validation-test/stdlib/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,58 @@ public func == (

// FIXME(prext): move this struct to the point of use.
Algorithm.test("min,max") {
expectEqual(2, min(3, 2))
expectEqual(3, min(3, 7, 5))
expectEqual(3, max(3, 2))
expectEqual(7, max(3, 7, 5))

// FIXME: add tests that check that min/max return the
// first element of the sequence (by reference equality) that satisfy the
// condition.
// Identities are unique in this set.
let a1 = MinimalComparableValue(0, identity: 1)
let a2 = MinimalComparableValue(0, identity: 2)
let a3 = MinimalComparableValue(0, identity: 3)
let b1 = MinimalComparableValue(1, identity: 4)
let b2 = MinimalComparableValue(1, identity: 5)
let b3 = MinimalComparableValue(1, identity: 6)
let c1 = MinimalComparableValue(2, identity: 7)
let c2 = MinimalComparableValue(2, identity: 8)
let c3 = MinimalComparableValue(2, identity: 9)

// 2-arg min()
expectEqual(a1.identity, min(a1, b1).identity)
expectEqual(a1.identity, min(b1, a1).identity)
expectEqual(a1.identity, min(a1, a2).identity)

// 2-arg max()
expectEqual(c1.identity, max(c1, b1).identity)
expectEqual(c1.identity, max(b1, c1).identity)
expectEqual(c1.identity, max(c2, c1).identity)

// 3-arg min()
expectEqual(a1.identity, min(a1, b1, c1).identity)
expectEqual(a1.identity, min(b1, a1, c1).identity)
expectEqual(a1.identity, min(c1, b1, a1).identity)
expectEqual(a1.identity, min(c1, a1, b1).identity)
expectEqual(a1.identity, min(a1, a2, a3).identity)
expectEqual(a1.identity, min(a1, a2, b1).identity)
expectEqual(a1.identity, min(a1, b1, a2).identity)
expectEqual(a1.identity, min(b1, a1, a2).identity)

// 3-arg max()
expectEqual(c1.identity, max(c1, b1, a1).identity)
expectEqual(c1.identity, max(a1, c1, b1).identity)
expectEqual(c1.identity, max(b1, a1, c1).identity)
expectEqual(c1.identity, max(b1, c1, a1).identity)
expectEqual(c1.identity, max(c3, c2, c1).identity)
expectEqual(c1.identity, max(c2, c1, b1).identity)
expectEqual(c1.identity, max(c2, b1, c1).identity)
expectEqual(c1.identity, max(b1, c2, c1).identity)

// 4-arg min()
expectEqual(a1.identity, min(a1, b1, a2, b2).identity)
expectEqual(a1.identity, min(b1, a1, a2, b2).identity)
expectEqual(a1.identity, min(c1, b1, b2, a1).identity)
expectEqual(a1.identity, min(c1, b1, a1, a2).identity)

// 4-arg max()
expectEqual(c1.identity, max(c2, b1, c1, b2).identity)
expectEqual(c1.identity, max(b1, c2, c1, b2).identity)
expectEqual(c1.identity, max(a1, b1, b2, c1).identity)
expectEqual(c1.identity, max(a1, b1, c2, c1).identity)
}

Algorithm.test("sorted/strings")
Expand Down