From 0b31e26007ddf377ca9d75fb4558411095a9dac2 Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Thu, 25 Jun 2020 22:14:52 +0200 Subject: [PATCH] use StableRNGs for testing --- Project.toml | 4 +++- test/imgcomp.jl | 19 +++++++++++++++++-- test/runtests.jl | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index fa18f967..bf2d24cd 100644 --- a/Project.toml +++ b/Project.toml @@ -49,6 +49,7 @@ RecipesPipeline = "0.1.7" Reexport = "0.2" Requires = "1" Showoff = "0.3.1" +StableRNGs = "0.1.1" StatsBase = "0.32, 0.33" julia = "1" @@ -63,6 +64,7 @@ LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925" RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b" +StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" @@ -70,4 +72,4 @@ UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228" VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92" [targets] -test = ["Distributions", "FileIO", "Gtk", "ImageMagick", "Images", "LibGit2", "OffsetArrays", "PGFPlotsX", "HDF5", "RDatasets", "StaticArrays", "StatsPlots", "Test", "UnicodePlots", "VisualRegressionTests"] +test = ["Distributions", "FileIO", "Gtk", "ImageMagick", "Images", "LibGit2", "OffsetArrays", "PGFPlotsX", "HDF5", "RDatasets", "StableRNGs", "StaticArrays", "StatsPlots", "Test", "UnicodePlots", "VisualRegressionTests"] diff --git a/test/imgcomp.jl b/test/imgcomp.jl index e7151573..4a06a0ef 100644 --- a/test/imgcomp.jl +++ b/test/imgcomp.jl @@ -1,5 +1,21 @@ import Plots._current_plots_version +# replace `f(args...)` with `f(rng, args...)` for `f ∈ (rand, randn)` +function replace_rand!(ex) end +function replace_rand!(ex::Expr) + for arg in ex.args + replace_rand!(arg) + end + if ex.head === :call && ex.args[1] ∈ (:rand, :randn) + pushfirst!(ex.args, ex.args[1]) + ex.args[2] = :rng + end +end +function fix_rand!(ex) + replace_rand!(ex) + pushfirst!(ex.args[1].args, :(rng = StableRNG(1234))) +end + function image_comparison_tests(pkg::Symbol, idx::Int; debug = false, popup = !is_ci(), sigma = [1,1], tol = 1e-2) Plots._debugMode.on = debug example = Plots._examples[idx] @@ -8,8 +24,6 @@ function image_comparison_tests(pkg::Symbol, idx::Int; debug = false, popup = !i backend(pkg) backend() default(size=(500,300)) - # ensure consistent results - Random.seed!(1234) fn = "ref$idx.png" reffn = reference_file(pkg, idx, _current_plots_version) @@ -19,6 +33,7 @@ function image_comparison_tests(pkg::Symbol, idx::Int; debug = false, popup = !i func = (fn, idx) -> begin expr = Expr(:block) append!(expr.args, example.exprs) + fix_rand!(expr) eval(expr) png(fn) end diff --git a/test/runtests.jl b/test/runtests.jl index 15591aee..dcf305ad 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,7 @@ import ImageMagick using VisualRegressionTests using Plots using Random +using StableRNGs using Test using FileIO using Gtk