Tooster

julia test function

Jan 23rd, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.69 KB | None | 0 0
  1. using LinearAlgebra # do operacji macierzowych i testowania
  2. using RandomNumbers.MersenneTwisters # do MT19937()
  3. using Printf
  4.  
  5. # test na macierzach.
  6. # f: funkcja zwracającą wynik do przetestowania z testem
  7. # name: nazwa metody liczącej rząd macierzy
  8. # rng: generator liczb losowych. Można przekazać np. MT19937(<seed>) aby generować te same zestawy losowych danych
  9. # exact_f: funkcja do liczenia dokładnego wyniku, chyba że test zawiera pole `exact`, wtedy to ono jest użyte
  10. # equal_f: opcjonalne do porównywania wartości wyliczonej przez f() i exact_f() lub test.exact. Domyślnie: ==
  11. function test(f, f_name, exact_f; rng=nothing, equal_f=(x, y)->x==y)
  12.     local _rand = rng==nothing ? rand : (args...)->rand(rng, args...) # custom rand if generator is set
  13.    
  14.     @printf(">>> Random test ID: [\e[1;35m%.16f\e[m]\n", _rand())
  15.  
  16.     function _do_test(test)
  17.         local exact, computed = haskey(test, :exact) ? test.exact : exact_f(test.M), f(test.M)
  18.         local success = equal_f(exact, computed)
  19.         @printf("[%5s] [exact:\t%3d] [%s:\t%3d] <<< %s\n",
  20.                 success ? "\e[1;32mPASS\e[m" : "\e[1;31mFAIL\e[m", exact, f_name, computed, test.desc)
  21.     end
  22.  
  23.     # test data
  24.     local zero_100x100 = zeros(Float64, 100, 100)
  25.     local Id_100x100   = Matrix{Float64}(I, 100, 100)
  26.  
  27.     # tests consist of fieds M=matrix, desc=description of test and exact=wanted
  28.     local tests = [
  29.         (M=zero_100x100, exact=0, desc="Zero 100x100 matrix")
  30.         (M=Id_100x100,            desc="Identity 100x100 matrix")
  31.     ]
  32.  
  33.     @printf("> Hardcoded tests:\n")
  34.     for test in tests
  35.         _do_test(test)
  36.     end
  37.    
  38.     flush(stdout)
  39.     @printf(">>> TESTS END\n")
  40. end
Add Comment
Please, Sign In to add comment