<commit_before>//===--- Interval.swift ---------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
//
// XFAIL: interpret

import StdlibUnittest

// Also import modules which are used by StdlibUnittest internally. This
// workaround is needed to link all required libraries in case we compile
// StdlibUnittest with -sil-serialize-all.
import SwiftPrivate
#if _runtime(_ObjC)
import ObjectiveC
#endif

// Check that the generic parameter is called 'Bound'.
protocol TestProtocol1 {}

extension HalfOpenInterval where Bound : TestProtocol1 {
  var _elementIsTestProtocol1: Bool {
    fatalError("not implemented")
  }
}

extension ClosedInterval where Bound : TestProtocol1 {
  var _elementIsTestProtocol1: Bool {
    fatalError("not implemented")
  }
}

var IntervalTestSuite = TestSuite("Interval")

IntervalTestSuite.test("Ambiguity") {
  // Ensure type deduction still works as expected; these will fail to
  // compile if it's broken
  var pieToPie = -3.1415927..<3.1415927
  expectType(HalfOpenInterval<Double>.self, &pieToPie)

  var pieThruPie = -3.1415927...3.1415927
  expectType(ClosedInterval<Double>.self, &pieThruPie)

  var zeroToOne = 0..<1
  expectType(Range<Int>.self, &zeroToOne)

  var zeroThruOne = 0...1
  // If/when we get a separate ClosedRange representation, this test
  // will have to change.
  expectType(Range<Int>.self, &zeroThruOne)
}

IntervalTestSuite.test("PatternMatching") {

  let pie = 3.1415927

  let expectations : [(Double, halfOpen: Bool, closed: Bool)] = [
    (-2 * pie, false, false),
    (-pie, true, true),
    (0, true, true),
    (pie, false, true),
    (2 * pie, false, false)
  ]

  for (x, halfOpenExpected, closedExpected) in expectations {
    var halfOpen: Bool
    switch x {
    case -3.1415927..<3.1415927:
      halfOpen = true
    default:
      halfOpen = false
    }

    var closed: Bool
    switch x {
    case -3.1415927...3.1415927:
      closed = true
    default:
      closed = false
    }
    
    expectEqual(halfOpenExpected, halfOpen)
    expectEqual(closedExpected, closed)
  }
}

IntervalTestSuite.test("Overlaps") {
  
  func expectOverlaps<
    I0: Interval, I1: Interval where I0.Bound == I1.Bound
  >(expectation: Bool, _ lhs: I0, _ rhs: I1) {
    if expectation {
      expectTrue(lhs.overlaps(rhs))
      expectTrue(rhs.overlaps(lhs))
    }
    else {
      expectFalse(lhs.overlaps(rhs))
      expectFalse(rhs.overlaps(lhs))
    }
  }
  
  // 0-4, 5-10
  expectOverlaps(false, 0..<4, 5..<10)
  expectOverlaps(false, 0..<4, 5...10)
  expectOverlaps(false, 0...4, 5..<10)
  expectOverlaps(false, 0...4, 5...10)

  // 0-5, 5-10
  expectOverlaps(false, 0..<5, 5..<10)
  expectOverlaps(false, 0..<5, 5...10)
  expectOverlaps(true, 0...5, 5..<10)
  expectOverlaps(true, 0...5, 5...10)

  // 0-6, 5-10
  expectOverlaps(true, 0..<6, 5..<10)
  expectOverlaps(true, 0..<6, 5...10)
  expectOverlaps(true, 0...6, 5..<10)
  expectOverlaps(true, 0...6, 5...10)

  // 0-20, 5-10
  expectOverlaps(true, 0..<20, 5..<10)
  expectOverlaps(true, 0..<20, 5...10)
  expectOverlaps(true, 0...20, 5..<10)
  expectOverlaps(true, 0...20, 5...10)

  // 0-0, 0-5
  expectOverlaps(false, 0..<0, 0..<5)
  expectOverlaps(false, 0..<0, 0...5)
  
}

IntervalTestSuite.test("Emptiness") {
  expectTrue((0.0..<0.0).isEmpty)
  expectFalse((0.0...0.0).isEmpty)
  expectFalse((0.0..<0.1).isEmpty)
  expectFalse((0.0..<0.1).isEmpty)
}

IntervalTestSuite.test("start/end") {
  expectEqual(0.0, (0.0..<0.1).lowerBound)
  expectEqual(0.0, (0.0...0.1).lowerBound)
  expectEqual(0.1, (0.0..<0.1).end)
  expectEqual(0.1, (0.0...0.1).end)
}

// Something to test with that distinguishes debugDescription from description
struct X<T : Comparable>
  : Comparable, CustomStringConvertible, CustomDebugStringConvertible {
  init(_ a: T) {
    self.a = a
  }

  var description: String {
    return String(a)
  }

  var debugDescription: String {
    return "X(\(String(reflecting: a)))"
  }
  
  var a: T
}

func < <T : Comparable>(lhs: X<T>, rhs: X<T>) -> Bool {
  return lhs.a < rhs.a
}

func == <T : Comparable>(lhs: X<T>, rhs: X<T>) -> Bool {
  return lhs.a == rhs.a
}

IntervalTestSuite.test("CustomStringConvertible/CustomDebugStringConvertible") {<commit_msg>[stdlib] indexing model: Fix stray use of "end"<commit_after>//===--- Interval.swift ---------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
//
// XFAIL: interpret

import StdlibUnittest

// Also import modules which are used by StdlibUnittest internally. This
// workaround is needed to link all required libraries in case we compile
// StdlibUnittest with -sil-serialize-all.
import SwiftPrivate
#if _runtime(_ObjC)
import ObjectiveC
#endif

// Check that the generic parameter is called 'Bound'.
protocol TestProtocol1 {}

extension HalfOpenInterval where Bound : TestProtocol1 {
  var _elementIsTestProtocol1: Bool {
    fatalError("not implemented")
  }
}

extension ClosedInterval where Bound : TestProtocol1 {
  var _elementIsTestProtocol1: Bool {
    fatalError("not implemented")
  }
}

var IntervalTestSuite = TestSuite("Interval")

IntervalTestSuite.test("Ambiguity") {
  // Ensure type deduction still works as expected; these will fail to
  // compile if it's broken
  var pieToPie = -3.1415927..<3.1415927
  expectType(HalfOpenInterval<Double>.self, &pieToPie)

  var pieThruPie = -3.1415927...3.1415927
  expectType(ClosedInterval<Double>.self, &pieThruPie)

  var zeroToOne = 0..<1
  expectType(Range<Int>.self, &zeroToOne)

  var zeroThruOne = 0...1
  // If/when we get a separate ClosedRange representation, this test
  // will have to change.
  expectType(Range<Int>.self, &zeroThruOne)
}

IntervalTestSuite.test("PatternMatching") {

  let pie = 3.1415927

  let expectations : [(Double, halfOpen: Bool, closed: Bool)] = [
    (-2 * pie, false, false),
    (-pie, true, true),
    (0, true, true),
    (pie, false, true),
    (2 * pie, false, false)
  ]

  for (x, halfOpenExpected, closedExpected) in expectations {
    var halfOpen: Bool
    switch x {
    case -3.1415927..<3.1415927:
      halfOpen = true
    default:
      halfOpen = false
    }

    var closed: Bool
    switch x {
    case -3.1415927...3.1415927:
      closed = true
    default:
      closed = false
    }
    
    expectEqual(halfOpenExpected, halfOpen)
    expectEqual(closedExpected, closed)
  }
}

IntervalTestSuite.test("Overlaps") {
  
  func expectOverlaps<
    I0: Interval, I1: Interval where I0.Bound == I1.Bound
  >(expectation: Bool, _ lhs: I0, _ rhs: I1) {
    if expectation {
      expectTrue(lhs.overlaps(rhs))
      expectTrue(rhs.overlaps(lhs))
    }
    else {
      expectFalse(lhs.overlaps(rhs))
      expectFalse(rhs.overlaps(lhs))
    }
  }
  
  // 0-4, 5-10
  expectOverlaps(false, 0..<4, 5..<10)
  expectOverlaps(false, 0..<4, 5...10)
  expectOverlaps(false, 0...4, 5..<10)
  expectOverlaps(false, 0...4, 5...10)

  // 0-5, 5-10
  expectOverlaps(false, 0..<5, 5..<10)
  expectOverlaps(false, 0..<5, 5...10)
  expectOverlaps(true, 0...5, 5..<10)
  expectOverlaps(true, 0...5, 5...10)

  // 0-6, 5-10
  expectOverlaps(true, 0..<6, 5..<10)
  expectOverlaps(true, 0..<6, 5...10)
  expectOverlaps(true, 0...6, 5..<10)
  expectOverlaps(true, 0...6, 5...10)

  // 0-20, 5-10
  expectOverlaps(true, 0..<20, 5..<10)
  expectOverlaps(true, 0..<20, 5...10)
  expectOverlaps(true, 0...20, 5..<10)
  expectOverlaps(true, 0...20, 5...10)

  // 0-0, 0-5
  expectOverlaps(false, 0..<0, 0..<5)
  expectOverlaps(false, 0..<0, 0...5)
  
}

IntervalTestSuite.test("Emptiness") {
  expectTrue((0.0..<0.0).isEmpty)
  expectFalse((0.0...0.0).isEmpty)
  expectFalse((0.0..<0.1).isEmpty)
  expectFalse((0.0..<0.1).isEmpty)
}

IntervalTestSuite.test("start/end") {
  expectEqual(0.0, (0.0..<0.1).lowerBound)
  expectEqual(0.0, (0.0...0.1).lowerBound)
  expectEqual(0.1, (0.0..<0.1).upperBound)
  expectEqual(0.1, (0.0...0.1).upperBound)
}

// Something to test with that distinguishes debugDescription from description
struct X<T : Comparable>
  : Comparable, CustomStringConvertible, CustomDebugStringConvertible {
  init(_ a: T) {
    self.a = a
  }

  var description: String {
    return String(a)
  }

  var debugDescription: String {
    return "X(\(String(reflecting: a)))"
  }
  
  var a: T
}

func < <T : Comparable>(lhs: X<T>, rhs: X<T>) -> Bool {
  return lhs.a < rhs.a
}

func == <T : Comparable>(lhs: X<T>, rhs: X<T>) -> Bool {
  return lhs.a == rhs.a
}

IntervalTestSuite.test("CustomStringConvertible/CustomDebugStringConvertible") {
{'fraction_non_alphanumeric': 0.17963119966087326, 'fraction_numerical': 0.05447223399745655, 'mean_word_length': 2.8021756647864624, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5804556', 'n_tokens_mistral': 3676, 'n_tokens_neox': 3353, 'n_words': 781}