1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
//===--- RemoveElements.swift - tests for lazy filtering-------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
import StdlibUnittest
let RemoveElements = TestSuite("RemoveElements")
extension RangeReplaceableCollection where Element: Equatable {
mutating func remove(equalTo: Element) {
removeAll(where: { $0 == equalTo })
}
}
RemoveElements.test("removing-array-with-predicate") {
var a = Array(0..<10)
a.removeAll(where: { $0 % 2 == 0 })
expectEqualSequence([1,3,5,7,9], a)
}
RemoveElements.test("removing-array-nothing") {
var a = Array(0..<5)
a.removeAll(where: { _ in false })
expectEqualSequence(0..<5, a)
}
RemoveElements.test("removing-array-everything") {
var a = Array(0..<5)
a.removeAll(where: { _ in true })
expectEqualSequence([], a)
}
RemoveElements.test("removing-array-from-empty") {
var a: [Int] = []
a.removeAll(where: { _ in true })
expectEqualSequence([], a)
}
RemoveElements.test("removing-array-with-equatable") {
var a = Array(0..<5)
a.remove(equalTo: 6)
expectEqualSequence([0,1,2,3,4], a)
a.remove(equalTo: 3)
expectEqualSequence([0,1,2,4], a)
a.remove(equalTo: 0)
expectEqualSequence([1,2,4], a)
a.remove(equalTo: 4)
expectEqualSequence([1,2], a)
a.remove(equalTo: 1)
expectEqualSequence([2], a)
a.remove(equalTo: 2)
expectEqualSequence([], a)
}
RemoveElements.test("removing-string-with-predicate") {
var s = "0123456789"
s.removeAll(where: { Int(String($0))! % 2 == 0 })
expectEqualSequence("13579", s)
}
RemoveElements.test("removing-string-nothing") {
var s = "01234"
s.removeAll(where: { _ in false })
expectEqualSequence("01234", s)
}
RemoveElements.test("removing-string-everything") {
var s = "01234"
s.removeAll(where: { _ in true })
expectEqualSequence("", s)
}
RemoveElements.test("removing-string-from-empty") {
var s = ""
s.removeAll(where: { _ in true })
expectEqualSequence("", s)
}
RemoveElements.test("removing-string-with-equatable") {
var s = "01234"
s.remove(equalTo: "6")
expectEqualSequence("01234", s)
s.remove(equalTo: "3")
expectEqualSequence("0124", s)
s.remove(equalTo: "0")
expectEqualSequence("124", s)
s.remove(equalTo: "4")
expectEqualSequence("12", s)
s.remove(equalTo: "1")
expectEqualSequence("2", s)
s.remove(equalTo: "2")
expectEqualSequence("", s)
}
runAllTests()
|