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