|
| 1 | +# This file is a part of BAT.jl, licensed under the MIT License (MIT). |
| 2 | + |
| 3 | +using MultiThreadingTools |
| 4 | +using Compat.Test |
| 5 | +using Base.Threads |
| 6 | + |
| 7 | +@testset "threadlocal" begin |
| 8 | + |
| 9 | + @testset "ThreadLocal" begin |
| 10 | + tl = @inferred ThreadLocal{Float32}() |
| 11 | + @test typeof(tl) <: ThreadLocal{Float32} |
| 12 | + @test length(tl.value) == nthreads() |
| 13 | + |
| 14 | + @test_throws ArgumentError ThreadLocal{Float32}(Array{Float32}(nthreads()+1)) |
| 15 | + |
| 16 | + tl = @inferred ThreadLocal{Float64}(ones(Float64, nthreads())) |
| 17 | + @test tl[] ≈ one(Float64) |
| 18 | + |
| 19 | + tmp = 2.5 |
| 20 | + tl = @inferred ThreadLocal(tmp) |
| 21 | + tmp = 0.0 |
| 22 | + @test tl[] ≈ 2.5 |
| 23 | + |
| 24 | + tmpF = () -> 3.0 |
| 25 | + tl = @inferred ThreadLocal(tmpF) |
| 26 | + @test tl[] ≈ 3.0 |
| 27 | + |
| 28 | + tl[] = 1.0 |
| 29 | + @test tl[] ≈ 1.0 |
| 30 | + |
| 31 | + @test get(tl) == tl[] |
| 32 | + @test get(tl, tmpF) ≈ 1.0 |
| 33 | + @test get!(tmpF, tl) ≈ 1.0 |
| 34 | + @test get!(tl, 2.0) ≈ 1.0 |
| 35 | + |
| 36 | + tmpF = () -> "test" |
| 37 | + tl = @inferred ThreadLocal{String}() |
| 38 | + |
| 39 | + @test get(tmpF, tl) == "test" |
| 40 | + @test get(tl, "default") == "default" |
| 41 | + |
| 42 | + @test get!(tmpF, tl) == "test" |
| 43 | + @test get(tl) == "test" |
| 44 | + |
| 45 | + tl = @inferred ThreadLocal{String}() |
| 46 | + @test get!(tl, "default") == "default" |
| 47 | + @test get(tl) == "default" |
| 48 | + |
| 49 | + @test threadlocal(3) == 3 |
| 50 | + @test threadlocal(tl) == "default" |
| 51 | + |
| 52 | + @test all_thread_values(tl) == tl.value |
| 53 | + |
| 54 | + end |
| 55 | + |
| 56 | +end |
0 commit comments