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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
# frozen_string_literal: true
DDMemoize.enable_metrics
describe DDMemoize do
it 'has a version number' do
expect(DDMemoize::VERSION).not_to be nil
end
class MemoizationSpecSample1
DDMemoize.activate(self)
def initialize(value)
@value = value
end
def run(n)
@value * 10 + n
end
memoize :run
end
class MemoizationSpecSample2
DDMemoize.activate(self)
def initialize(value)
@value = value
end
def run(n)
@value * 100 + n
end
memoize :run
end
class MemoizationSpecUpcaser
DDMemoize.activate(self)
def run(value)
value.upcase
end
memoize :run
end
class MemoizationSpecEqual
DDMemoize.activate(self)
class EqualToEverythingValue
def equal?(*)
true
end
end
def run
EqualToEverythingValue.new
end
memoize :run
end
class MemoizationSpecUpcaserInlineSyntax
DDMemoize.activate(self)
memoized def run(value)
value.upcase
end
end
class MemoizationSpecInlineSyntaxReturn
DDMemoize.activate(self)
class << self
attr_reader :sym
end
def self.record(sym)
@sym = sym
end
record memoized def run; end
end
class MemoizationSpecWithMetrics
DDMemoize.activate(self)
def run(value)
value.upcase
end
memoize :run
end
example do
sample1a = MemoizationSpecSample1.new(10)
sample1b = MemoizationSpecSample1.new(15)
sample2a = MemoizationSpecSample2.new(20)
sample2b = MemoizationSpecSample2.new(25)
3.times do
expect(sample1a.run(5)).to eq(10 * 10 + 5)
expect(sample1b.run(7)).to eq(10 * 15 + 7)
expect(sample2a.run(5)).to eq(100 * 20 + 5)
expect(sample2b.run(7)).to eq(100 * 25 + 7)
end
end
it 'supports frozen objects' do
sample = MemoizationSpecSample1.new(10)
sample.freeze
sample.run(5)
end
it 'supports objects that bizarrely override #equal?' do
sample = MemoizationSpecEqual.new
sample.freeze
sample.run
sample.run
sample.run
counter = DDMemoize.metrics_counter
expect(counter.get(method: 'MemoizationSpecEqual#run', type: :miss)).to eq(1)
expect(counter.get(method: 'MemoizationSpecEqual#run', type: :hit)).to eq(2)
end
it 'supports memoized def … syntax' do
upcaser = MemoizationSpecUpcaserInlineSyntax.new
expect(upcaser.run('hi')).to eq('HI')
end
it 'does not crash on #inspect' do
upcaser = MemoizationSpecUpcaser.new
10_000.times do |i|
upcaser.run("hello world #{i}")
end
GC.start
GC.start
upcaser.inspect
end
it 'returns method name' do
expect(MemoizationSpecInlineSyntaxReturn.sym).to eq(:run)
end
it 'records metrics' do
sample = MemoizationSpecWithMetrics.new
sample.run('denis')
sample.run('denis')
sample.run('defreyne')
counter = DDMemoize.metrics_counter
expect(counter.get(method: 'MemoizationSpecWithMetrics#run', type: :miss)).to eq(2)
expect(counter.get(method: 'MemoizationSpecWithMetrics#run', type: :hit)).to eq(1)
end
it 'prints recorded metrics' do
sample = MemoizationSpecWithMetrics.new
sample.run('denis')
sample.run('denis')
sample.run('defreyne')
expect { DDMemoize.print_metrics }.to output(/memoization │ hit\s+miss\s+%/).to_stdout
end
end
|