|
| 1 | +@testitem "take_while" begin |
| 2 | + source = QueryOperators.query([1, 2, 3, 4, 1, 2]) |
| 3 | + |
| 4 | + res = QueryOperators.take_while(source, i -> i < 3, :(i -> i < 3)) |
| 5 | + |
| 6 | + # Stops at the first failure; the trailing 1 and 2 are not taken. |
| 7 | + @test collect(res) == [1, 2] |
| 8 | + @test eltype(res) == Int |
| 9 | +end |
| 10 | + |
| 11 | +@testitem "take_while taking everything or nothing" begin |
| 12 | + source = QueryOperators.query([1, 2, 3]) |
| 13 | + |
| 14 | + @test collect(QueryOperators.take_while(source, i -> true, :(i -> true))) == [1, 2, 3] |
| 15 | + @test collect(QueryOperators.take_while(source, i -> false, :(i -> false))) == Int[] |
| 16 | +end |
| 17 | + |
| 18 | +@testitem "take_while on an empty source" begin |
| 19 | + source = QueryOperators.query(Int[]) |
| 20 | + |
| 21 | + @test collect(QueryOperators.take_while(source, i -> true, :(i -> true))) == Int[] |
| 22 | +end |
| 23 | + |
| 24 | +@testitem "take_while stops walking the source at the first failure" begin |
| 25 | + import IteratorInterfaceExtensions |
| 26 | + |
| 27 | + mutable struct CountedTakeWhileSource |
| 28 | + data::Vector{Int} |
| 29 | + pulled::Int |
| 30 | + end |
| 31 | + Base.eltype(::Type{CountedTakeWhileSource}) = Int |
| 32 | + Base.IteratorSize(::Type{CountedTakeWhileSource}) = Base.HasLength() |
| 33 | + Base.length(c::CountedTakeWhileSource) = length(c.data) |
| 34 | + function Base.iterate(c::CountedTakeWhileSource, i=1) |
| 35 | + i > length(c.data) && return nothing |
| 36 | + c.pulled += 1 |
| 37 | + return c.data[i], i + 1 |
| 38 | + end |
| 39 | + IteratorInterfaceExtensions.isiterable(::CountedTakeWhileSource) = true |
| 40 | + IteratorInterfaceExtensions.getiterator(c::CountedTakeWhileSource) = c |
| 41 | + |
| 42 | + src = CountedTakeWhileSource(collect(1:100), 0) |
| 43 | + res = QueryOperators.take_while(QueryOperators.query(src), i -> i < 4, :(i -> i < 4)) |
| 44 | + |
| 45 | + @test collect(res) == [1, 2, 3] |
| 46 | + # Three taken plus the one that failed the predicate. |
| 47 | + @test src.pulled == 4 |
| 48 | +end |
| 49 | + |
| 50 | +@testitem "drop_while" begin |
| 51 | + source = QueryOperators.query([1, 2, 3, 4, 1, 2]) |
| 52 | + |
| 53 | + res = QueryOperators.drop_while(source, i -> i < 3, :(i -> i < 3)) |
| 54 | + |
| 55 | + # Only the leading run is dropped; the trailing 1 and 2 survive. |
| 56 | + @test collect(res) == [3, 4, 1, 2] |
| 57 | + @test eltype(res) == Int |
| 58 | +end |
| 59 | + |
| 60 | +@testitem "drop_while dropping everything or nothing" begin |
| 61 | + source = QueryOperators.query([1, 2, 3]) |
| 62 | + |
| 63 | + @test collect(QueryOperators.drop_while(source, i -> true, :(i -> true))) == Int[] |
| 64 | + @test collect(QueryOperators.drop_while(source, i -> false, :(i -> false))) == [1, 2, 3] |
| 65 | +end |
| 66 | + |
| 67 | +@testitem "drop_while on an empty source" begin |
| 68 | + source = QueryOperators.query(Int[]) |
| 69 | + |
| 70 | + @test collect(QueryOperators.drop_while(source, i -> true, :(i -> true))) == Int[] |
| 71 | +end |
| 72 | + |
| 73 | +@testitem "take_while and drop_while partition the source" begin |
| 74 | + source = QueryOperators.query([1, 2, 3, 4, 5]) |
| 75 | + p = i -> i < 3 |
| 76 | + |
| 77 | + taken = collect(QueryOperators.take_while(source, p, :(i -> i < 3))) |
| 78 | + dropped = collect(QueryOperators.drop_while(source, p, :(i -> i < 3))) |
| 79 | + |
| 80 | + @test vcat(taken, dropped) == [1, 2, 3, 4, 5] |
| 81 | +end |
| 82 | + |
| 83 | +@testitem "take_last" begin |
| 84 | + source = QueryOperators.query([1, 2, 3, 4, 5]) |
| 85 | + |
| 86 | + res = QueryOperators.take_last(source, 2) |
| 87 | + |
| 88 | + @test collect(res) == [4, 5] |
| 89 | + @test eltype(res) == Int |
| 90 | + @test Base.IteratorSize(typeof(res)) == Base.HasLength() |
| 91 | + @test length(res) == 2 |
| 92 | +end |
| 93 | + |
| 94 | +@testitem "take_last with a count at or beyond the source length" begin |
| 95 | + source = QueryOperators.query([1, 2, 3]) |
| 96 | + |
| 97 | + @test collect(QueryOperators.take_last(source, 3)) == [1, 2, 3] |
| 98 | + @test collect(QueryOperators.take_last(source, 10)) == [1, 2, 3] |
| 99 | + @test length(QueryOperators.take_last(source, 10)) == 3 |
| 100 | +end |
| 101 | + |
| 102 | +@testitem "take_last with a count of zero or less" begin |
| 103 | + source = QueryOperators.query([1, 2, 3]) |
| 104 | + |
| 105 | + @test collect(QueryOperators.take_last(source, 0)) == Int[] |
| 106 | + @test collect(QueryOperators.take_last(source, -1)) == Int[] |
| 107 | +end |
| 108 | + |
| 109 | +@testitem "take_last on an empty source" begin |
| 110 | + source = QueryOperators.query(Int[]) |
| 111 | + |
| 112 | + @test collect(QueryOperators.take_last(source, 2)) == Int[] |
| 113 | +end |
| 114 | + |
| 115 | +@testitem "drop_last" begin |
| 116 | + source = QueryOperators.query([1, 2, 3, 4, 5]) |
| 117 | + |
| 118 | + res = QueryOperators.drop_last(source, 2) |
| 119 | + |
| 120 | + @test collect(res) == [1, 2, 3] |
| 121 | + @test eltype(res) == Int |
| 122 | + @test Base.IteratorSize(typeof(res)) == Base.HasLength() |
| 123 | + @test length(res) == 3 |
| 124 | +end |
| 125 | + |
| 126 | +@testitem "drop_last with a count at or beyond the source length" begin |
| 127 | + source = QueryOperators.query([1, 2, 3]) |
| 128 | + |
| 129 | + @test collect(QueryOperators.drop_last(source, 3)) == Int[] |
| 130 | + @test collect(QueryOperators.drop_last(source, 10)) == Int[] |
| 131 | + @test length(QueryOperators.drop_last(source, 10)) == 0 |
| 132 | +end |
| 133 | + |
| 134 | +@testitem "drop_last with a count of zero or less is the identity" begin |
| 135 | + source = QueryOperators.query([1, 2, 3]) |
| 136 | + |
| 137 | + @test collect(QueryOperators.drop_last(source, 0)) == [1, 2, 3] |
| 138 | + @test collect(QueryOperators.drop_last(source, -1)) == [1, 2, 3] |
| 139 | +end |
| 140 | + |
| 141 | +@testitem "drop_last on an empty source" begin |
| 142 | + source = QueryOperators.query(Int[]) |
| 143 | + |
| 144 | + @test collect(QueryOperators.drop_last(source, 2)) == Int[] |
| 145 | +end |
| 146 | + |
| 147 | +@testitem "take_last and drop_last partition the source" begin |
| 148 | + source = QueryOperators.query([1, 2, 3, 4, 5]) |
| 149 | + |
| 150 | + dropped = collect(QueryOperators.drop_last(source, 2)) |
| 151 | + taken = collect(QueryOperators.take_last(source, 2)) |
| 152 | + |
| 153 | + @test vcat(dropped, taken) == [1, 2, 3, 4, 5] |
| 154 | +end |
| 155 | + |
| 156 | +@testitem "partitioning operators work downstream of groupby" begin |
| 157 | + source = QueryOperators.query([(k=1, v=1), (k=2, v=2), (k=3, v=3)]) |
| 158 | + grouped = QueryOperators.@groupby_simple(source, i -> i.k) |
| 159 | + |
| 160 | + last_two = collect(QueryOperators.take_last(grouped, 2)) |
| 161 | + @test [QueryOperators.key(g) for g in last_two] == [2, 3] |
| 162 | + |
| 163 | + all_but_last = collect(QueryOperators.drop_last(grouped, 1)) |
| 164 | + @test [QueryOperators.key(g) for g in all_but_last] == [1, 2] |
| 165 | +end |
0 commit comments