From 4002758844cd4897010ebefb5222fad17633a28a Mon Sep 17 00:00:00 2001 From: Arsen Gasparyan Date: Tue, 15 Dec 2015 23:19:33 +0300 Subject: [PATCH 1/2] [tests] basic min/max test cases --- test/1_stdlib/Algorithm.swift | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/1_stdlib/Algorithm.swift diff --git a/test/1_stdlib/Algorithm.swift b/test/1_stdlib/Algorithm.swift new file mode 100644 index 0000000000000..068827817c2fa --- /dev/null +++ b/test/1_stdlib/Algorithm.swift @@ -0,0 +1,27 @@ +// RUN: %target-run-simple-swift +// REQUIRES: executable_test + +import StdlibUnittest +import Swift + +let AlgorithmTests = TestSuite("Algorithm") + +AlgorithmTests.test("Min") { + expectEqual(0, min(0, 1)) + expectEqual(0, min(1, 0)) + + expectEqual(0, min(0, 1, 1)) + expectEqual(0, min(1, 0, 1)) + expectEqual(0, min(1, 1, 0)) +} + +AlgorithmTests.test("Max") { + expectEqual(1, max(1, 0)) + expectEqual(1, max(0, 1)) + + expectEqual(1, max(1, 0, 0)) + expectEqual(1, max(0, 1, 0)) + expectEqual(1, max(0, 0, 1)) +} + +runAllTests() From 9f728c6dbd9dd9376647eab41b4677429beaea1d Mon Sep 17 00:00:00 2001 From: Arsen Gasparyan Date: Wed, 16 Dec 2015 14:21:33 +0300 Subject: [PATCH 2/2] [stdlib] Make comparison operator choices consistent --- stdlib/public/core/Algorithm.swift | 20 ++------- test/1_stdlib/Algorithm.swift | 27 ------------ validation-test/stdlib/Algorithm.swift | 60 ++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 51 deletions(-) delete mode 100644 test/1_stdlib/Algorithm.swift diff --git a/stdlib/public/core/Algorithm.swift b/stdlib/public/core/Algorithm.swift index f8d829c515e7f..15b2fd94f918a 100644 --- a/stdlib/public/core/Algorithm.swift +++ b/stdlib/public/core/Algorithm.swift @@ -74,27 +74,15 @@ public func min(x: T, _ y: T, _ z: T, _ rest: T...) -> T { /// Returns the greater of `x` and `y`. @warn_unused_result public func max(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(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 } diff --git a/test/1_stdlib/Algorithm.swift b/test/1_stdlib/Algorithm.swift deleted file mode 100644 index 068827817c2fa..0000000000000 --- a/test/1_stdlib/Algorithm.swift +++ /dev/null @@ -1,27 +0,0 @@ -// RUN: %target-run-simple-swift -// REQUIRES: executable_test - -import StdlibUnittest -import Swift - -let AlgorithmTests = TestSuite("Algorithm") - -AlgorithmTests.test("Min") { - expectEqual(0, min(0, 1)) - expectEqual(0, min(1, 0)) - - expectEqual(0, min(0, 1, 1)) - expectEqual(0, min(1, 0, 1)) - expectEqual(0, min(1, 1, 0)) -} - -AlgorithmTests.test("Max") { - expectEqual(1, max(1, 0)) - expectEqual(1, max(0, 1)) - - expectEqual(1, max(1, 0, 0)) - expectEqual(1, max(0, 1, 0)) - expectEqual(1, max(0, 0, 1)) -} - -runAllTests() diff --git a/validation-test/stdlib/Algorithm.swift b/validation-test/stdlib/Algorithm.swift index c8ef76fd8a3b7..b24ccab9192b1 100644 --- a/validation-test/stdlib/Algorithm.swift +++ b/validation-test/stdlib/Algorithm.swift @@ -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")