-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Implement tuple comparison operators #408
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Generate comparison functions for tuples up to some reasonable arity. | ||
|
||
% for arity in range(2,7): | ||
% typeParams = [chr(ord("A")+i) for i in range(arity)] | ||
% tupleT = "({})".format(",".join(typeParams)) | ||
|
||
% equatableTypeParams = ", ".join(["{} : Equatable".format(c) for c in typeParams]) | ||
|
||
/// Returns `true` iff each component of `lhs` is equal to the corresponding | ||
/// component of `rhs`. | ||
@warn_unused_result | ||
public func == <${equatableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool { | ||
% ops = ["lhs.{} == rhs.{}".format(i,i) for i in range(arity)] | ||
return ${" && ".join(ops)} | ||
} | ||
|
||
/// Returns `true` iff any component of `lhs` is not equal to the corresponding | ||
/// component of `rhs`. | ||
@warn_unused_result | ||
public func != <${equatableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool { | ||
% ops = ["lhs.{} != rhs.{}".format(i,i) for i in range(arity)] | ||
return ${" || ".join(ops)} | ||
} | ||
|
||
% comparableTypeParams = ", ".join(["{} : Comparable".format(c) for c in typeParams]) | ||
% for op in ["<", ">"]: | ||
% for opeq in ["", "="]: | ||
/// A [lexicographical order](https://en.wikipedia.org/wiki/Lexicographical_order) | ||
/// over tuples of `Comparable` elements. | ||
/// | ||
/// Given two tuples `(a1,a2,…,aN)` and `(b1,b2,…,bN)`, the first tuple is | ||
/// `${op}${opeq}` the second tuple iff `a1 ${op} b1` or (`a1 == b1` and | ||
/// `(a2,…,aN) ${op}${opeq} (b2,…,bN)`). | ||
@warn_unused_result | ||
public func ${op}${opeq} <${comparableTypeParams}>(lhs: ${tupleT}, rhs: ${tupleT}) -> Bool { | ||
% for i in range(arity-1): | ||
if lhs.${i} != rhs.${i} { return lhs.${i} ${op} rhs.${i} } | ||
% end | ||
return lhs.${arity-1} ${op}${opeq} rhs.${arity-1} | ||
} | ||
% end | ||
% end | ||
% end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// RUN: rm -f %t.swift %t.out | ||
|
||
// RUN: %S/../../utils/gyb %s -o %t.swift | ||
// RUN: %S/../../utils/line-directive %t.swift -- %target-build-swift %t.swift -o %t.out | ||
// RUN: %S/../../utils/line-directive %t.swift -- %target-run %t.out | ||
// REQUIRES: executable_test | ||
|
||
import StdlibUnittest | ||
|
||
var TupleTestSuite = TestSuite("Tuple") | ||
|
||
// Test tuple comparison operators | ||
// all the tuple types use the same basic implementation for the operators | ||
// so testing any arity tests the logic for them all. | ||
// Include at least one invocation for all arities as a sanity check. | ||
|
||
% maxArity = 6 # the highest arity the operators are defined for | ||
|
||
func testEquality<A : Equatable, B : Equatable, C : Equatable>( | ||
lhs: (A,B,C), equal: Bool, to rhs: (A,B,C), | ||
//===--- TRACE boilerplate ----------------------------------------------===// | ||
@autoclosure _ message: ()->String = "", | ||
showFrame: Bool = true, | ||
stackTrace: SourceLocStack = SourceLocStack(), | ||
file: String = __FILE__, line: UInt = __LINE__ | ||
) { | ||
let trace = stackTrace.pushIf(showFrame, file: file, line: line) | ||
expectEqual(equal, lhs == rhs, stackTrace: trace) | ||
expectEqual(equal, rhs == lhs, stackTrace: trace) | ||
expectEqual(!equal, lhs != rhs, stackTrace: trace) | ||
expectEqual(!equal, rhs != lhs, stackTrace: trace) | ||
} | ||
|
||
TupleTestSuite.test("Tuple/equality") { | ||
testEquality((1,2,3), equal: true, to: (1,2,3)) | ||
testEquality((1,2,3), equal: false, to: (1,2,4)) | ||
testEquality((1,2,3), equal: false, to: (1,3,3)) | ||
testEquality((1,2,3), equal: false, to: (2,2,3)) | ||
testEquality((1,"2",3), equal: true, to: (1,"2",3)) | ||
testEquality((1,"2",3), equal: false, to: (1,"3",3)) | ||
testEquality(("one", 2.2, 3..<5), equal: true, to: ("one", 2.2, 3..<5)) | ||
|
||
testEquality((1.0, 2.0, 3.0), equal: false, to: (1.0, 2.0, .NaN)) | ||
testEquality((1.0, 2.0, 3.0), equal: false, to: (1.0, .NaN, 3.0)) | ||
testEquality((1.0, 2.0, 3.0), equal: false, to: (.NaN, 2.0, 3.0)) | ||
testEquality((1.0, 2.0, 3.0), equal: false, to: (.NaN, .NaN, .NaN)) | ||
testEquality((1.0, 2.0, Float.NaN), equal: false, to: (1.0, 2.0, 3.0)) | ||
testEquality((1.0, 2.0, Float.NaN), equal: false, to: (1.0, 2.0, Float.NaN)) | ||
testEquality((Float.NaN, Float.NaN, Float.NaN), equal: false, to: (.NaN, .NaN, .NaN)) | ||
testEquality((Float.NaN, Float.NaN, Float.NaN), equal: false, to: (1.0, 2.0, 3.0)) | ||
|
||
expectTrue((1,2) == (1,2)) | ||
expectTrue((1,2) != (1,3)) | ||
expectTrue((1,2,3,4) == (1,2,3,4)) | ||
expectTrue((1,2,3,4) != (1,2,3,3)) | ||
expectTrue((1,2,3,4,5) == (1,2,3,4,5)) | ||
expectTrue((1,2,3,4,5) != (1,2,3,4,4)) | ||
expectTrue((1,2,3,4,5,6) == (1,2,3,4,5,6)) | ||
expectTrue((1,2,3,4,5,6) != (1,2,3,4,5,5)) | ||
} | ||
|
||
TupleTestSuite.test("Tuple/equality/sanity-check") { | ||
// sanity check all arities | ||
% for arity in range(2,maxArity+1): | ||
% a = str(tuple(range(1, arity+1))) | ||
% b = "({}, 0)".format(", ".join([str(i) for i in range(1,arity)])) | ||
% c = "(0, {})".format(", ".join([str(i) for i in range(2,arity+1)])) | ||
expectTrue(${a} == ${a}) | ||
expectTrue(${a} != ${b}) | ||
expectTrue(${b} != ${a}) | ||
expectTrue(${a} != ${c}) | ||
expectTrue(${c} != ${a}) | ||
% end | ||
} | ||
|
||
enum Ordering : Equatable { | ||
case LessThan | ||
case EqualTo | ||
case GreaterThan | ||
case UnorderedWith // Comparable defines strict total order, but Float disobeys that with NaN | ||
|
||
var isLT: Bool { | ||
return self == .LessThan | ||
} | ||
var isEQ: Bool { | ||
return self == .EqualTo | ||
} | ||
var isGT: Bool { | ||
return self == .GreaterThan | ||
} | ||
} | ||
|
||
func testOrdering<A : Comparable, B : Comparable, C : Comparable>( | ||
lhs: (A,B,C), _ ordering: Ordering, _ rhs: (A, B, C), | ||
//===--- TRACE boilerplate ----------------------------------------------===// | ||
@autoclosure _ message: ()->String = "", | ||
showFrame: Bool = true, | ||
stackTrace: SourceLocStack = SourceLocStack(), | ||
file: String = __FILE__, line: UInt = __LINE__ | ||
) { | ||
let trace = stackTrace.pushIf(showFrame, file: file, line: line) | ||
expectEqual(ordering.isLT, lhs < rhs, stackTrace: trace) | ||
expectEqual(ordering.isLT, rhs > lhs, stackTrace: trace) | ||
expectEqual(ordering.isLT || ordering.isEQ, lhs <= rhs, stackTrace: trace) | ||
expectEqual(ordering.isLT || ordering.isEQ, rhs >= lhs, stackTrace: trace) | ||
expectEqual(ordering.isGT, lhs > rhs, stackTrace: trace) | ||
expectEqual(ordering.isGT, rhs < lhs, stackTrace: trace) | ||
expectEqual(ordering.isGT || ordering.isEQ, lhs >= rhs, stackTrace: trace) | ||
expectEqual(ordering.isGT || ordering.isEQ, rhs <= lhs, stackTrace: trace) | ||
} | ||
|
||
TupleTestSuite.test("Tuple/comparison") { | ||
testOrdering((1,2,3), .EqualTo, (1,2,3)) | ||
testOrdering((1,2,3), .LessThan, (1,2,4)) | ||
testOrdering((1,2,3), .GreaterThan, (1,2,2)) | ||
testOrdering((1,3,2), .GreaterThan, (1,2,3)) | ||
testOrdering((0,2,3), .LessThan, (1,2,3)) | ||
testOrdering((3,2,1), .GreaterThan, (1,2,3)) | ||
|
||
testOrdering(("one", 2, 3.3), .EqualTo, ("one", 2, 3.3)) | ||
testOrdering(("one", 2, 3.3), .LessThan, ("one", 2, 3.4)) | ||
testOrdering(("on", 2, 3.3), .LessThan, ("one", 1, 3.2)) | ||
|
||
testOrdering((1, 2, Float.NaN), .UnorderedWith, (1, 2, .NaN)) | ||
testOrdering((1, Float.NaN, 3), .UnorderedWith, (1, 2, 3)) | ||
testOrdering((Double.NaN, 2, 3), .UnorderedWith, (.NaN, 2, 3)) | ||
testOrdering((Float.NaN, Float.NaN, Float.NaN), .UnorderedWith, (1, 2, 3)) | ||
testOrdering((1, 2, 3.0), .UnorderedWith, (1, 2, .NaN)) | ||
testOrdering((1, 2, 3.0), .LessThan, (1, 3, .NaN)) | ||
testOrdering((1, 2, 3.0), .GreaterThan, (1, 1, .NaN)) | ||
testOrdering((1, 2.0, 3), .LessThan, (2, .NaN, 3)) | ||
testOrdering((1, 2.0, 3), .GreaterThan, (0, .NaN, 3)) | ||
testOrdering((1, 2, Float.NaN), .LessThan, (1, 3, 3.0)) | ||
testOrdering((1, Float.NaN, 3), .GreaterThan, (0, 2.0, 3)) | ||
testOrdering(("one", "two", 3.0), .GreaterThan, ("a", "b", .NaN)) | ||
testOrdering(("one", "two", .NaN), .GreaterThan, ("a", "b", 3.0)) | ||
testOrdering((1.0, "two", "three"), .UnorderedWith, (.NaN, "two", "four")) | ||
testOrdering((.NaN, "two", "three"), .UnorderedWith, (1.0, "two", "four")) | ||
} | ||
|
||
TupleTestSuite.test("Tuple/comparison/sanity-check") { | ||
// sanity check all arities | ||
% for arity in range(2,maxArity+1): | ||
% a = str(tuple(range(1, arity+1))) | ||
% b = "({}, 0)".format(", ".join([str(i) for i in range(1,arity)])) | ||
% c = "(0, {})".format(", ".join([str(i) for i in range(2,arity+1)])) | ||
expectTrue(${b} < ${a}) | ||
expectTrue(${b} <= ${a}) | ||
expectTrue(${a} > ${c}) | ||
expectTrue(${a} >= ${c}) | ||
% end | ||
} | ||
|
||
runAllTests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a code size optimization, you could try defining one operators in terms of others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried defining
!=
in terms of=
and it actually increased code size. I didn't do any further investigation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's unfortunate. Thanks for checking!