// RUN: rm -rf %t ; mkdir -p %t
// RUN: %target-build-swift %s -o %t/a.out3 -swift-version 3 && %target-run %t/a.out3
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-run %t/a.out4

import StdlibUnittest

var tests = TestSuite("RangeReplaceableFilterCompatibility")

tests.test("String.filter return type") {
  var filtered = "Hello, World".filter { $0 < "A" }
#if swift(>=4)
  expectType(String.self, &filtered)
#else
  expectType([Character].self, &filtered)
#endif
}

tests.test("Array.filter return type") {
  var filtered = Array(0..<10).filter { $0 % 2 == 0 }
  expectType([Int].self, &filtered)
}

tests.test("ContiguousArray.filter return type") {
  var filtered = ContiguousArray(0..<10).filter { $0 % 2 == 0 }
  expectType([Int].self, &filtered)
}

tests.test("ArraySlice.filter return type") {
  var filtered = Array(0..<10)[0..<10].filter { $0 % 2 == 0 }
  expectType([Int].self, &filtered)
}

tests.test("String.filter can return [Character]") {
  let filtered = "Hello, World".filter { "A" <= $0 && $0 <= "Z"} as [Character]
  expectEqualSequence("HW", filtered)
}

tests.test("lazy.flatMap.filter ambiguity") {
  // this expression should compile without ambiguity
  _ = Array(0..<10).lazy.flatMap { .some($0) }.filter { _ in false }
}

runAllTests()

