SHARE
TWEET

Untitled

a guest Jun 19th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TestBlah < MiniTest::Unit::TestCase
  2.   def dumb_adder n
  3.     (1..n).inject { |sum, n| sum + n }
  4.   end
  5.  
  6.   def smart_adder n
  7.     (n * n + n) / 2
  8.   end
  9.  
  10.   def test_dumb_adder
  11.     assert_equal     55, dumb_adder(10)
  12.     assert_equal   5050, dumb_adder(100)
  13.     assert_equal 500500, dumb_adder(1000)
  14.   end
  15.  
  16.   def test_smart_adder
  17.     assert_equal     55, smart_adder(10)
  18.     assert_equal   5050, smart_adder(100)
  19.     assert_equal 500500, smart_adder(1000)
  20.   end
  21.  
  22.   bench_exp 10, 10_000
  23.  
  24.   def bench_dumb_adder n
  25.     n.times do
  26.       dumb_adder 100
  27.     end
  28.   end
  29.  
  30.   def bench_fast n
  31.     n.times do
  32.       smart_adder 100
  33.     end
  34.   end
  35. end
  36.  
  37. # Loaded suite blah
  38. # Started
  39. # ..
  40. # Finished in 0.001144 seconds.
  41. #
  42. # 2 tests, 6 assertions, 0 failures, 0 errors, 0 skips
  43. #
  44. # Test run options: --seed 40122
  45. #
  46. # # TestBlah benchmarks:
  47. # Benchmark                    10       100      1000     10000
  48. # bench_dumb_adder       0.000638  0.008971  0.077714  0.777714
  49. # bench_fast             0.000012  0.000079  0.000751  0.007552
RAW Paste Data
Top