How to use ys method of Minitest Package

Best Minitest_ruby code snippet using Minitest.ys

benchmark.rb

Source:benchmark.rb Github

copy

Full Screen

...53 # Runs the given +work+, gathering the times of each run. Range54 # and times are then passed to a given +validation+ proc. Outputs55 # the benchmark name and times in tab-separated format, making it56 # easy to paste into a spreadsheet for graphing or further57 # analysis.58 #59 # Ranges are specified by ::bench_range.60 #61 # Eg:62 #63 # def bench_algorithm64 # validation = proc { |x, y| ... }65 # assert_performance validation do |n|66 # @obj.algorithm(n)67 # end68 # end69 def assert_performance validation, &work70 range = self.class.bench_range71 io.print "#{self.name}"72 times = []73 range.each do |x|74 GC.start75 t0 = Minitest.clock_time76 instance_exec(x, &work)77 t = Minitest.clock_time - t078 io.print "\t%9.6f" % t79 times << t80 end81 io.puts82 validation[range, times]83 end84 ##85 # Runs the given +work+ and asserts that the times gathered fit to86 # match a constant rate (eg, linear slope == 0) within a given87 # +threshold+. Note: because we're testing for a slope of 0, R^288 # is not a good determining factor for the fit, so the threshold89 # is applied against the slope itself. As such, you probably want90 # to tighten it from the default.91 #92 # See https://www.graphpad.com/guides/prism/8/curve-fitting/reg_intepretingnonlinr2.htm93 # for more details.94 #95 # Fit is calculated by #fit_linear.96 #97 # Ranges are specified by ::bench_range.98 #99 # Eg:100 #101 # def bench_algorithm102 # assert_performance_constant 0.9999 do |n|103 # @obj.algorithm(n)104 # end105 # end106 def assert_performance_constant threshold = 0.99, &work107 validation = proc do |range, times|108 a, b, rr = fit_linear range, times109 assert_in_delta 0, b, 1 - threshold110 [a, b, rr]111 end112 assert_performance validation, &work113 end114 ##115 # Runs the given +work+ and asserts that the times gathered fit to116 # match a exponential curve within a given error +threshold+.117 #118 # Fit is calculated by #fit_exponential.119 #120 # Ranges are specified by ::bench_range.121 #122 # Eg:123 #124 # def bench_algorithm125 # assert_performance_exponential 0.9999 do |n|126 # @obj.algorithm(n)127 # end128 # end129 def assert_performance_exponential threshold = 0.99, &work130 assert_performance validation_for_fit(:exponential, threshold), &work131 end132 ##133 # Runs the given +work+ and asserts that the times gathered fit to134 # match a logarithmic curve within a given error +threshold+.135 #136 # Fit is calculated by #fit_logarithmic.137 #138 # Ranges are specified by ::bench_range.139 #140 # Eg:141 #142 # def bench_algorithm143 # assert_performance_logarithmic 0.9999 do |n|144 # @obj.algorithm(n)145 # end146 # end147 def assert_performance_logarithmic threshold = 0.99, &work148 assert_performance validation_for_fit(:logarithmic, threshold), &work149 end150 ##151 # Runs the given +work+ and asserts that the times gathered fit to152 # match a straight line within a given error +threshold+.153 #154 # Fit is calculated by #fit_linear.155 #156 # Ranges are specified by ::bench_range.157 #158 # Eg:159 #160 # def bench_algorithm161 # assert_performance_linear 0.9999 do |n|162 # @obj.algorithm(n)163 # end164 # end165 def assert_performance_linear threshold = 0.99, &work166 assert_performance validation_for_fit(:linear, threshold), &work167 end168 ##169 # Runs the given +work+ and asserts that the times gathered curve170 # fit to match a power curve within a given error +threshold+.171 #172 # Fit is calculated by #fit_power.173 #174 # Ranges are specified by ::bench_range.175 #176 # Eg:177 #178 # def bench_algorithm179 # assert_performance_power 0.9999 do |x|180 # @obj.algorithm181 # end182 # end183 def assert_performance_power threshold = 0.99, &work184 assert_performance validation_for_fit(:power, threshold), &work185 end186 ##187 # Takes an array of x/y pairs and calculates the general R^2 value.188 #189 # See: http://en.wikipedia.org/wiki/Coefficient_of_determination190 def fit_error xys191 y_bar = sigma(xys) { |_, y| y } / xys.size.to_f192 ss_tot = sigma(xys) { |_, y| (y - y_bar) ** 2 }193 ss_err = sigma(xys) { |x, y| (yield(x) - y) ** 2 }194 1 - (ss_err / ss_tot)195 end196 ##197 # To fit a functional form: y = ae^(bx).198 #199 # Takes x and y values and returns [a, b, r^2].200 #201 # See: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html202 def fit_exponential xs, ys203 n = xs.size204 xys = xs.zip(ys)205 sxlny = sigma(xys) { |x, y| x * Math.log(y) }206 slny = sigma(xys) { |_, y| Math.log(y) }207 sx2 = sigma(xys) { |x, _| x * x }208 sx = sigma xs209 c = n * sx2 - sx ** 2210 a = (slny * sx2 - sx * sxlny) / c211 b = ( n * sxlny - sx * slny ) / c212 return Math.exp(a), b, fit_error(xys) { |x| Math.exp(a + b * x) }213 end214 ##215 # To fit a functional form: y = a + b*ln(x).216 #217 # Takes x and y values and returns [a, b, r^2].218 #219 # See: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html220 def fit_logarithmic xs, ys221 n = xs.size222 xys = xs.zip(ys)223 slnx2 = sigma(xys) { |x, _| Math.log(x) ** 2 }224 slnx = sigma(xys) { |x, _| Math.log(x) }225 sylnx = sigma(xys) { |x, y| y * Math.log(x) }226 sy = sigma(xys) { |_, y| y }227 c = n * slnx2 - slnx ** 2228 b = ( n * sylnx - sy * slnx ) / c229 a = (sy - b * slnx) / n230 return a, b, fit_error(xys) { |x| a + b * Math.log(x) }231 end232 ##233 # Fits the functional form: a + bx.234 #235 # Takes x and y values and returns [a, b, r^2].236 #237 # See: http://mathworld.wolfram.com/LeastSquaresFitting.html238 def fit_linear xs, ys239 n = xs.size240 xys = xs.zip(ys)241 sx = sigma xs242 sy = sigma ys243 sx2 = sigma(xs) { |x| x ** 2 }244 sxy = sigma(xys) { |x, y| x * y }245 c = n * sx2 - sx**2246 a = (sy * sx2 - sx * sxy) / c247 b = ( n * sxy - sx * sy ) / c248 return a, b, fit_error(xys) { |x| a + b * x }249 end250 ##251 # To fit a functional form: y = ax^b.252 #253 # Takes x and y values and returns [a, b, r^2].254 #255 # See: http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html256 def fit_power xs, ys257 n = xs.size258 xys = xs.zip(ys)259 slnxlny = sigma(xys) { |x, y| Math.log(x) * Math.log(y) }260 slnx = sigma(xs) { |x | Math.log(x) }261 slny = sigma(ys) { | y| Math.log(y) }262 slnx2 = sigma(xs) { |x | Math.log(x) ** 2 }263 b = (n * slnxlny - slnx * slny) / (n * slnx2 - slnx ** 2)264 a = (slny - b * slnx) / n265 return Math.exp(a), b, fit_error(xys) { |x| (Math.exp(a) * (x ** b)) }266 end267 ##268 # Enumerates over +enum+ mapping +block+ if given, returning the269 # sum of the result. Eg:270 #271 # sigma([1, 2, 3]) # => 1 + 2 + 3 => 6272 # sigma([1, 2, 3]) { |n| n ** 2 } # => 1 + 4 + 9 => 14273 def sigma enum, &block274 enum = enum.map(&block) if block275 enum.inject { |sum, n| sum + n }276 end277 ##278 # Returns a proc that calls the specified fit method and asserts279 # that the error is within a tolerable threshold....

Full Screen

Full Screen

ys

Using AI Code Generation

copy

Full Screen

1Minitest::Reporters::SpecReporter.new(color: true)2Minitest::Reporters::SpecReporter.new(color: true, slow_count: 5)3Minitest::Reporters::DefaultReporter.new(color: true)4Minitest::Reporters::DefaultReporter.new(color: true, slow_count: 5)5Minitest::Reporters::ProgressReporter.new(color: true)6Minitest::Reporters::ProgressReporter.new(color: true, slow_count: 5)7Minitest::Reporters::RubyMineReporter.new(color: true)8Minitest::Reporters::RubyMineReporter.new(color: true, slow_count: 5)9Minitest::Reporters::RubyMateReporter.new(color: true)10Minitest::Reporters::RubyMateReporter.new(color: true, slow_count: 5)

Full Screen

Full Screen

ys

Using AI Code Generation

copy

Full Screen

1 assert_equal(ys(0), 1)2 assert_equal(ys(1), 1)3 assert_equal(ys(2), 2)4 assert_equal(ys(3), 4)5 assert_equal(ys(4), 8)6 assert_equal(ys(5), 16)7 assert_equal(ys(6), 32)8 assert_equal(ys(7), 64)9 assert_equal(ys(8), 128)10 assert_equal(ys(9), 256)11 assert_equal(ys(10), 512)12def ys(n)13 n == 0 ? 1 : 2 * ys(n - 1)14 assert_equal(ys(0), 1)15 assert_equal(ys(1), 1)16 assert_equal(ys(2), 2)17 assert_equal(ys(3), 4)18 assert_equal(ys(4), 8)19 assert_equal(ys(5), 16)20 assert_equal(ys(6), 32)21 assert_equal(ys(7), 64)22 assert_equal(ys(8), 128)23 assert_equal(ys(9), 256)24 assert_equal(ys(10), 512)25def ys(n)26 n == 0 ? 1 : 2 * ys(n - 1)27 assert_equal(ys(0), 1)28 assert_equal(ys(1), 1)29 assert_equal(ys(2), 2)30 assert_equal(ys(3), 4)31 assert_equal(ys(4), 8)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Minitest_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful