-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_spec.rb
127 lines (120 loc) · 4.51 KB
/
function_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'spec_helper'
def drops(results_for_function)
# count how often the function takes more time for a smaller input than a larger one
xs = results_for_function.values.map {|a| a.reduce(0, :+) }
n = 0
(0..xs.size - 2).each do |i|
n = n + 1 if xs[i] > xs[i + 1]
end
n
end
describe Graph::Function do
it 'has a version number' do
expect(Graph::Function::VERSION).not_to be nil
end
describe Graph::Function::IntsComparison do
def one(array)
array.each {|e| e }
end
def two(array)
array.each {|e| e * 2 }
end
it 'plots two functions and returns the times from each' do
Graph::Function.configuration.trials = 3
results = Graph::Function::IntsComparison.of(method(:one), method(:two))
expect(drops(results[:one])).to be <= 2
expect(drops(results[:two])).to be <= 2
end
it 'can output to gif' do
Graph::Function.configuration.terminal = 'gif'
Graph::Function.configuration.output = File.expand_path('../two_func.gif', __FILE__)
Graph::Function::IntsComparison.of(method(:one), method(:two))
end
it 'can output to txt' do
Graph::Function.configuration.terminal = 'dumb'
Graph::Function.configuration.output = File.expand_path('../two_func.txt', __FILE__)
Graph::Function::IntsComparison.of(method(:one), method(:two))
end
end
describe Graph::Function::Comparison do
let(:rantly_generator) do
proc {|size|
Rantly { dict(size) { [string, integer] }}
}
end
let(:hash_first_proc) { proc {|hash| hash.values.first}}
def hash_last_value(hash)
hash.values.last
end
def hash_first_value(hash)
hash.values.first
end
it 'uses a Rantly generator for x data' do
Graph::Function.configuration.terminal = 'gif'
Graph::Function.configuration.output = File.expand_path('../comparison.gif', __FILE__)
comparison = Graph::Function::Comparison.new(rantly_generator)
comparison.of(method(:hash_last_value), method(:hash_first_value))
end
it 'can take a proc as input' do
Graph::Function.configuration.terminal = 'gif'
Graph::Function.configuration.output = File.expand_path('../proc_v_method.gif', __FILE__)
comparison = Graph::Function::Comparison.new(rantly_generator)
comparison.of(hash_first_proc, method(:hash_first_value))
end
it 'can perform multiple trials' do
Graph::Function.configuration.terminal = 'gif'
Graph::Function.configuration.output = File.expand_path('../multitrial.gif', __FILE__)
Graph::Function.configuration.trials = 3
comparison = Graph::Function::Comparison.new(rantly_generator)
comparison.of(method(:hash_last_value), method(:hash_first_value))
end
end
describe "Comparison of one function" do
let(:rantly_generator) do
proc {|size|
Rantly { array(size) {string} }
}
end
def single_func(as)
as.map(&:upcase)
end
let(:faker_generator) do
proc {|size|
Rantly(size) { call(Proc.new { Faker::Date.backward(14) }) }
}
end
def custom_types(faked)
if faked.is_a?(Array)
faked.map {|e| e.strftime("at %I:%M%p") }
else
faked.strftime("at %I:%M%p")
end
end
it 'uses a Rantly generator and acts on one function' do
Graph::Function.configuration.terminal = 'gif'
Graph::Function.configuration.output = File.expand_path('../rantly.gif', __FILE__)
graph = Graph::Function::Comparison.new(rantly_generator)
graph.of(method(:single_func))
end
it 'can use a Faker/Rantly generator and acts on one function' do
Graph::Function.configuration.terminal = 'gif'
Graph::Function.configuration.output = File.expand_path('../faker.gif', __FILE__)
graph = Graph::Function::Comparison.new(faker_generator)
graph.of(method(:custom_types))
end
it 'allows for basic memory profiling' do
Graph::Function.configuration.terminal = 'gif'
Graph::Function.configuration.memory = true
Graph::Function.configuration.output = File.expand_path('../memory.gif', __FILE__)
graph = Graph::Function::Comparison.new(faker_generator)
graph.of(method(:custom_types))
end
end
describe Graph::Function::ReformatString do
include Graph::Function::ReformatString
it 'extracts a filename and line number from Proc#to_s' do
proc_to_s = '#<proc:07ffb5380c220@/users/corajr/development/graph-function/spec/graph/function_spec.rb:36>'
expect(extract_filename(proc_to_s)).to eq('function_spec.rb:36')
end
end
end