|
| 1 | +module test_FixedSizeFIFOQueue |
| 2 | + |
| 3 | +using Test |
| 4 | +using JETLS: FixedSizeFIFOQueue, capacity, isfull |
| 5 | + |
| 6 | +struct TestType |
| 7 | + x::Int |
| 8 | +end |
| 9 | + |
| 10 | +mutable struct MutableTestType |
| 11 | + x::Int |
| 12 | + data::Vector{Int} |
| 13 | +end |
| 14 | + |
| 15 | +@testset "FixedSizeFIFOQueue basic operations" begin |
| 16 | + let q = FixedSizeFIFOQueue{Int}(3) |
| 17 | + @test isempty(q) |
| 18 | + @test !isfull(q) |
| 19 | + @test length(q) == 0 |
| 20 | + @test capacity(q) == 3 |
| 21 | + |
| 22 | + push!(q, 1) |
| 23 | + @test !isempty(q) |
| 24 | + @test length(q) == 1 |
| 25 | + @test 1 in q |
| 26 | + @test !(2 in q) |
| 27 | + |
| 28 | + push!(q, 2) |
| 29 | + push!(q, 3) |
| 30 | + @test isfull(q) |
| 31 | + @test length(q) == 3 |
| 32 | + @test 1 in q && 2 in q && 3 in q |
| 33 | + end |
| 34 | + |
| 35 | + let q = FixedSizeFIFOQueue{Int}(3) |
| 36 | + push!(q, 1) |
| 37 | + push!(q, 2) |
| 38 | + push!(q, 3) |
| 39 | + push!(q, 4) # Should overwrite 1 |
| 40 | + @test isfull(q) |
| 41 | + @test length(q) == 3 |
| 42 | + @test !(1 in q) |
| 43 | + @test 2 in q && 3 in q && 4 in q |
| 44 | + |
| 45 | + push!(q, 5) # Should overwrite 2 |
| 46 | + @test !(2 in q) |
| 47 | + @test 3 in q && 4 in q && 5 in q |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +@testset "FixedSizeFIFOQueue type flexibility" begin |
| 52 | + let q = FixedSizeFIFOQueue(2) |
| 53 | + push!(q, "hello") |
| 54 | + push!(q, 42) |
| 55 | + @test "hello" in q |
| 56 | + @test 42 in q |
| 57 | + |
| 58 | + push!(q, :symbol) |
| 59 | + @test !("hello" in q) |
| 60 | + @test 42 in q |
| 61 | + @test :symbol in q |
| 62 | + end |
| 63 | + |
| 64 | + let q = FixedSizeFIFOQueue{TestType}(2) |
| 65 | + t1 = TestType(1) |
| 66 | + t2 = TestType(2) |
| 67 | + t3 = TestType(3) |
| 68 | + |
| 69 | + push!(q, t1) |
| 70 | + push!(q, t2) |
| 71 | + @test t1 in q |
| 72 | + @test t2 in q |
| 73 | + |
| 74 | + push!(q, t3) |
| 75 | + @test !(t1 in q) |
| 76 | + @test t2 in q |
| 77 | + @test t3 in q |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +@testset "FixedSizeFIFOQueue edge cases" begin |
| 82 | + let q = FixedSizeFIFOQueue{String}(1) |
| 83 | + push!(q, "a") |
| 84 | + @test "a" in q |
| 85 | + push!(q, "b") |
| 86 | + @test !("a" in q) |
| 87 | + @test "b" in q |
| 88 | + end |
| 89 | + |
| 90 | + @test_throws ArgumentError FixedSizeFIFOQueue{Int}(0) |
| 91 | + @test_throws ArgumentError FixedSizeFIFOQueue{Int}(-1) |
| 92 | +end |
| 93 | + |
| 94 | +@testset "FixedSizeFIFOQueue collect and display" begin |
| 95 | + let q = FixedSizeFIFOQueue{Int}(3) |
| 96 | + push!(q, 1) |
| 97 | + push!(q, 2) |
| 98 | + push!(q, 3) |
| 99 | + |
| 100 | + items = collect(q) |
| 101 | + @test items == [1, 2, 3] |
| 102 | + |
| 103 | + push!(q, 4) # Overwrites 1 |
| 104 | + items = collect(q) |
| 105 | + @test items == [2, 3, 4] |
| 106 | + |
| 107 | + str = string(q) |
| 108 | + @test occursin("FixedSizeFIFOQueue{Int", str) |
| 109 | + @test occursin("capacity=3", str) |
| 110 | + @test occursin("[2, 3, 4]", str) |
| 111 | + end |
| 112 | +end |
| 113 | + |
| 114 | +@testset "FixedSizeFIFOQueue large capacity" begin |
| 115 | + let q = FixedSizeFIFOQueue{Union{Int,String}}(1000) |
| 116 | + for i in 1:500 |
| 117 | + push!(q, i) |
| 118 | + end |
| 119 | + for i in 501:1000 |
| 120 | + push!(q, "id-$i") |
| 121 | + end |
| 122 | + |
| 123 | + @test isfull(q) |
| 124 | + @test 1 in q |
| 125 | + @test 500 in q |
| 126 | + @test "id-501" in q |
| 127 | + @test "id-1000" in q |
| 128 | + |
| 129 | + push!(q, "extra-1") |
| 130 | + @test !(1 in q) # First element should be evicted |
| 131 | + @test 2 in q # Second element should still be there |
| 132 | + @test "extra-1" in q |
| 133 | + |
| 134 | + for i in 1:100 |
| 135 | + push!(q, "new-$i") |
| 136 | + end |
| 137 | + |
| 138 | + @test !(2 in q) |
| 139 | + @test !(100 in q) |
| 140 | + @test "new-100" in q |
| 141 | + end |
| 142 | +end |
| 143 | + |
| 144 | +@testset "FixedSizeFIFOQueue garbage collection" begin |
| 145 | + let q = FixedSizeFIFOQueue{MutableTestType}(2) |
| 146 | + obj1 = MutableTestType(1, [1,2,3]) |
| 147 | + obj2 = MutableTestType(2, [4,5,6]) |
| 148 | + obj3 = MutableTestType(3, [7,8,9]) |
| 149 | + |
| 150 | + push!(q, obj1) |
| 151 | + push!(q, obj2) |
| 152 | + push!(q, obj3) |
| 153 | + |
| 154 | + @test obj2 in q |
| 155 | + @test obj3 in q |
| 156 | + @test !(obj1 in q.data) |
| 157 | + @test !(obj1 in q.items) |
| 158 | + end |
| 159 | +end |
| 160 | + |
| 161 | +end # module test_FixedSizeFIFOQueue |
0 commit comments