Class: Sorting::TestHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/sorting/test_helper.rb

Class Method Summary collapse

Class Method Details

.diff(a, b) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/sorting/test_helper.rb', line 71

def self.diff(a, b)
  diff = {}
  a.each_with_index do |x, i|
    y = b[i]
    diff[i] = [x, y] if x != y
  end
  diff
end

.get_sample_data(size = 10, shuffle = true) ⇒ Object



5
6
7
8
# File 'lib/sorting/test_helper.rb', line 5

def self.get_sample_data(size=10, shuffle=true)
  data = (1..size).to_a
  shuffle ? data.shuffle : data
end

.sort(clazz, data) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/sorting/test_helper.rb', line 10

def self.sort(clazz, data)
  if clazz.respond_to? :sort!
    clazz.sort! data
  else
    data = clazz.sort data
  end
  data
end

.test(filename, benchmark_data_size = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sorting/test_helper.rb', line 19

def self.test(filename, benchmark_data_size=nil)
  data = get_sample_data
  clazz = eval("Sorting::#{File.basename(filename).split("_")[0].capitalize}Sort")
  benchmark_data_size ||= clazz::TEST_DATA_SIZE
  return if benchmark_data_size.nil?
  is_ext = clazz.const_defined?("IS_EXT") && clazz::IS_EXT ? "(C ext)" : ""
  puts "#{clazz}#{is_ext} - before sort: #{data.inspect}"
  data = sort(clazz, data)
  puts "#{clazz}#{is_ext} - after  sort: #{data.inspect}"

  sample_data = get_sample_data benchmark_data_size, false
  benchmark_data = sample_data.dup
  result = Benchmark.measure do
    benchmark_data = sort(clazz, benchmark_data)
    #10.times {sort(clazz, benchmark_data.dup)} #notice, you should dup sample data for multiple test
  end
  diff = diff(benchmark_data, sample_data.sort)
  raise "sort wrong for #{sample_data.inspect}, diff: #{diff.inspect}" unless diff.empty?
  puts "#{sample_data.size} ordered number cost"
  puts result

  sample_data = get_sample_data benchmark_data_size
  benchmark_data = sample_data.dup
  result = Benchmark.measure do
    benchmark_data = sort(clazz, benchmark_data)
    #10.times {sort(clazz, benchmark_data.dup)} #notice, you should dup sample data for multiple test
  end
  diff = diff(benchmark_data, sample_data.sort)
  raise "sort wrong for #{sample_data.inspect}, diff: #{diff.inspect}" unless diff.empty?
  puts "#{sample_data.size} ordered number cost"
  puts result
end

.test_original_sort(benchmark_data_size = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sorting/test_helper.rb', line 52

def self.test_original_sort(benchmark_data_size=nil)
  benchmark_data_size ||= 100_000
  sample_data = get_sample_data benchmark_data_size, false
  benchmark_data = sample_data.dup
  result = Benchmark.measure do
    benchmark_data.sort!
  end
  puts "Array.sort: #{sample_data.size} ordered number cost"
  puts result

  sample_data = get_sample_data benchmark_data_size
  benchmark_data = sample_data.dup
  result = Benchmark.measure do
    benchmark_data.sort!
  end
  puts "Array.sort: #{sample_data.size} ordered number cost"
  puts result
end