Swift: overlaps() assumes that `start` is contained in an interval
| Originator: | natecook | ||
| Number: | rdar://22307600 | Date Originated: | 17-Aug-2015 08:34 AM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode-beta (7A176x) |
| Classification: | Other Bug | Reproducible: | Always |
Summary:
For an implementation of OpenInterval, an interval type that includes neither the starting nor ending bound, the overlaps function returns an incorrect result when called with an interval that includes the start of the OpenInterval. The IntervalType protocol mentions no requirement that start be contained in an interval. To the contrary, since the descriptions for start and end are essentially the same, the existence of HalfOpenInterval and ClosedInterval in the standard library indicates that there should be no such requirement.
Steps to Reproduce:
(See attached Playground.)
1) In a playground, create a new OpenInterval type:
/// A fully open `IntervalType`, which contains neither its `start` nor its
/// `end`. Can represent an empty interval.
///
/// - parameter Bound: The type of the endpoints.
struct OpenInterval<Bound: Comparable where Bound: ForwardIndexType> : IntervalType {
let start: Bound
let end: Bound
init(_ start: Bound, _ end: Bound) {
self.start = start
self.end = end
}
var isEmpty: Bool {
return start.successor() >= end
}
func contains(value: Bound) -> Bool {
return value > start && value < end
}
func clamp(intervalToClamp: OpenInterval) -> OpenInterval {
let newStart = max(start, intervalToClamp.start)
let newEnd = min(end, intervalToClamp.end)
return OpenInterval(newStart, max(newEnd, newStart))
}
}
2) Create a closed interval from 0 to 3 and an open interval from 3 to 6, and test with overlaps(). These should be non-overlapping, since the highest value in the closed interval (3) is not contained in the open interval:
let closed = ClosedInterval(0, 3)
let open = OpenInterval(3, 6)
assert(overlaps(closed, open) == false, "Intervals do not overlap")
Expected Results:
overlaps() should return false, so the assertion should pass.
Actual Results:
The assertion fails.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!