Class: TestMinitestBenchmark

Inherits:
Minitest::Test show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb

Overview

Constant Summary

Constants inherited from Minitest::Test

Minitest::Test::PASSTHROUGH_EXCEPTIONS, Minitest::Test::SETUP_METHODS, Minitest::Test::TEARDOWN_METHODS

Constants included from Minitest::Assertions

Minitest::Assertions::E, Minitest::Assertions::UNDEFINED

Constants inherited from Minitest::Runnable

Minitest::Runnable::SIGNALS

Instance Attribute Summary

Attributes inherited from Minitest::Runnable

#assertions, #failures, #time

Instance Method Summary collapse

Methods inherited from Minitest::Test

#capture_exceptions, #class_name, #clean, i_suck_and_my_tests_are_order_dependent!, make_my_diffs_pretty!, #neuter_exception, #new_exception, parallelize_me!, #run, runnable_methods, #sanitize_exception, test_order, #with_empty_backtrace_filter, #with_info_handler

Methods included from Minitest::Guard

#jruby?, #maglev?, #mri?, #osx?, #rubinius?, #windows?

Methods included from Minitest::Test::LifecycleHooks

#after_setup, #after_teardown, #before_setup, #before_teardown, #setup, #teardown

Methods included from Minitest::Reportable

#class_name, #error?, #location, #passed?, #result_code, #skipped?

Methods included from Minitest::Assertions

#_synchronize, #assert, #assert_empty, #assert_equal, #assert_in_delta, #assert_in_epsilon, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_mock, #assert_nil, #assert_operator, #assert_output, #assert_path_exists, #assert_predicate, #assert_raises, #assert_respond_to, #assert_same, #assert_send, #assert_silent, #assert_throws, #capture_io, #capture_subprocess_io, #diff, diff, diff=, #exception_details, #fail_after, #flunk, #message, #mu_pp, #mu_pp_for_diff, #pass, #refute, #refute_empty, #refute_equal, #refute_in_delta, #refute_in_epsilon, #refute_includes, #refute_instance_of, #refute_kind_of, #refute_match, #refute_nil, #refute_operator, #refute_path_exists, #refute_predicate, #refute_respond_to, #refute_same, #skip, #skip_until, #skipped?, #things_to_diff

Methods inherited from Minitest::Runnable

#failure, inherited, #initialize, #marshal_dump, #marshal_load, methods_matching, #name, #name=, on_signal, #passed?, reset, #result_code, run, #run, run_one_method, runnable_methods, runnables, #skipped?, #time_it, #whatever, with_info_handler

Constructor Details

This class inherits a constructor from Minitest::Runnable

Instance Method Details

#assert_fit(msg, x, y, fit, exp_a, exp_b) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 121

def assert_fit msg, x, y, fit, exp_a, exp_b
  bench = Minitest::Benchmark.new :blah

  a, b, rr = bench.send "fit_#{msg}", x, y

  assert_operator rr, :>=, fit if fit
  assert_in_delta exp_a, a
  assert_in_delta exp_b, b
end

#test_cls_bench_expObject



9
10
11
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 9

def test_cls_bench_exp
  assert_equal [2, 4, 8, 16, 32], Minitest::Benchmark.bench_exp(2, 32, 2)
end

#test_cls_bench_linearObject



13
14
15
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 13

def test_cls_bench_linear
  assert_equal [2, 4, 6, 8, 10], Minitest::Benchmark.bench_linear(2, 10, 2)
end

#test_cls_bench_rangeObject



28
29
30
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 28

def test_cls_bench_range
  assert_equal [1, 10, 100, 1_000, 10_000], Minitest::Benchmark.bench_range
end

#test_cls_runnable_methodsObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 17

def test_cls_runnable_methods
  assert_equal [], Minitest::Benchmark.runnable_methods

  c = Class.new(Minitest::Benchmark) do
    def bench_blah
    end
  end

  assert_equal ["bench_blah"], c.runnable_methods
end

#test_fit_constant_cleanObject



63
64
65
66
67
68
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 63

def test_fit_constant_clean
  x = (1..5).to_a
  y = [5.0, 5.0, 5.0, 5.0, 5.0]

  assert_fit :linear, x, y, nil, 5.0, 0
end

#test_fit_constant_noisyObject



70
71
72
73
74
75
76
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 70

def test_fit_constant_noisy
  x = (1..5).to_a
  y = [1.0, 1.2, 1.0, 0.8, 1.0]

  # verified in numbers and R
  assert_fit :linear, x, y, nil, 1.12, -0.04
end

#test_fit_exponential_cleanObject



32
33
34
35
36
37
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 32

def test_fit_exponential_clean
  x = [1.0, 2.0, 3.0, 4.0, 5.0]
  y = x.map { |n| 1.1 * Math.exp(2.1 * n) }

  assert_fit :exponential, x, y, 1.0, 1.1, 2.1
end

#test_fit_exponential_noisyObject



39
40
41
42
43
44
45
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 39

def test_fit_exponential_noisy
  x = [1.0, 1.9, 2.6, 3.4, 5.0]
  y = [12, 10, 8.2, 6.9, 5.9]

  # verified with Numbers and R
  assert_fit :exponential, x, y, 0.95, 13.81148, -0.1820
end

#test_fit_linear_cleanObject



78
79
80
81
82
83
84
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 78

def test_fit_linear_clean
  # y = m * x + b where m = 2.2, b = 3.1
  x = (1..5).to_a
  y = x.map { |n| 2.2 * n + 3.1 }

  assert_fit :linear, x, y, 1.0, 3.1, 2.2
end

#test_fit_linear_noisyObject



86
87
88
89
90
91
92
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 86

def test_fit_linear_noisy
  x = [ 60,  61,  62,  63,  65]
  y = [3.1, 3.6, 3.8, 4.0, 4.1]

  # verified in numbers and R
  assert_fit :linear, x, y, 0.8315, -7.9635, 0.1878
end

#test_fit_logarithmic_cleanObject



47
48
49
50
51
52
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 47

def test_fit_logarithmic_clean
  x = [1.0, 2.0, 3.0, 4.0, 5.0]
  y = x.map { |n| 1.1 + 2.1 * Math.log(n) }

  assert_fit :logarithmic, x, y, 1.0, 1.1, 2.1
end

#test_fit_logarithmic_noisyObject



54
55
56
57
58
59
60
61
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 54

def test_fit_logarithmic_noisy
  x = [1.0, 2.0, 3.0, 4.0, 5.0]
  # Generated with
  # y = x.map { |n| jitter = 0.999 + 0.002 * rand; (Math.log(n) ) * jitter }
  y = [0.0, 0.6935, 1.0995, 1.3873, 1.6097]

  assert_fit :logarithmic, x, y, 0.95, 0, 1
end

#test_fit_power_cleanObject



94
95
96
97
98
99
100
101
102
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 94

def test_fit_power_clean
  # y = A x ** B, where B = b and A = e ** a
  # if, A = 1, B = 2, then

  x = [1.0, 2.0, 3.0, 4.0, 5.0]
  y = [1.0, 4.0, 9.0, 16.0, 25.0]

  assert_fit :power, x, y, 1.0, 1.0, 2.0
end

#test_fit_power_noisyObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_benchmark.rb', line 104

def test_fit_power_noisy
  # from www.engr.uidaho.edu/thompson/courses/ME330/lecture/least_squares.html
  x = [10, 12, 15, 17, 20, 22, 25, 27, 30, 32, 35]
  y = [95, 105, 125, 141, 173, 200, 253, 298, 385, 459, 602]

  # verified in numbers
  assert_fit :power, x, y, 0.90, 2.6217, 1.4556

  # income to % of households below income amount
  # https://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
  x = [15_000, 25_000, 35_000, 50_000, 75_000, 100_000]
  y = [0.154, 0.283, 0.402, 0.55, 0.733, 0.843]

  # verified in numbers
  assert_fit :power, x, y, 0.96, 3.119e-5, 0.8959
end