Skip to content

Instantly share code, notes, and snippets.

@sanemat
Forked from anonymous/gist:259993
Created December 19, 2009 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanemat/259994 to your computer and use it in GitHub Desktop.
Save sanemat/259994 to your computer and use it in GitHub Desktop.
#
require 'rubygems'
require 'spec'
require 'lru_cache'
describe LruCache, 'when Lru called' do
before(:all) do
puts 'Test Kaisi'
end
# before do
# @lrucache = LruCache.new
# end
it 'should be construct.' do
@lrucache = LruCache.new(2)
@lrucache.size.should == 2
end
it 'size 3 no construct.' do
@lrucache = LruCache.new(3)
@lrucache.size.should == 3
end
it 'if put' do
@lrucache = LruCache.new(3)
@lrucache.put("a", "dataA")
end
it 'if get' do
@lrucache = LruCache.new(3)
@lrucache.get("a")
end
it 'if put and get ' do
@lrucache = LruCache.new(3)
@lrucache.put("a", "dataA")
@lrucache.get("a").should == "dataA"
end
it 'if put and get ' do
@lrucache = LruCache.new(3)
@lrucache.put("b", "dataB")
@lrucache.get("b").should == "dataB"
end
it 'if put put and get get' do
@lrucache = LruCache.new(3)
@lrucache.put("a", "dataA")
@lrucache.put("b", "dataB")
@lrucache.get("a").should == "dataA"
@lrucache.get("b").should == "dataB"
end
it 'if put put put and get get get' do
@lrucache = LruCache.new(3)
@lrucache.put("a", "dataA")
@lrucache.put("b", "dataB")
@lrucache.put("c", "dataC")
@lrucache.get("a").should == "dataA"
@lrucache.get("b").should == "dataB"
@lrucache.get("c").should == "dataC"
end
it 'if put put put and which is oldest' do
@lrucache = LruCache.new(3)
@lrucache.put("a", "dataA")
@lrucache.put("b", "dataB")
@lrucache.put("c", "dataC")
@lrucache.elder().should == "a"
end
it 'if put put put and which is oldest' do
@lrucache = LruCache.new(3)
@lrucache.put("a", "dataA")
@lrucache.put("b", "dataB")
@lrucache.put("c", "dataC")
@lrucache.elder().should == "a"
end
it 'if put put put and which is oldest' do
@lrucache = LruCache.new(3)
@lrucache.put("b", "dataB")
@lrucache.put("c", "dataC")
@lrucache.put("a", "dataA")
@lrucache.elder().should == "b"
end
it 'if put put put and which is oldest' do
@lrucache = LruCache.new(2)
@lrucache.put("a", "dataA")
@lrucache.put("b", "dataB")
@lrucache.put("c", "dataC")
@lrucache.elder().should == "b"
end
it 'if put put put and which is oldest' do
@lrucache = LruCache.new(3)
@lrucache.put("a", "dataA")
@lrucache.put("b", "dataB")
@lrucache.put("c", "dataC")
@lrucache.put("d", "dataD")
@lrucache.elder().should == "b"
end
after(:all) do
puts 'Test Owari'
end
end
#
class LruCache
attr_accessor :size
def initialize(size)
@size = size
@cache = {}
@oldest = nil
@age = {}
end
def put(key, value)
@cache[key] = value
@age.each do |k, v|
@age[k] += 1
end
@age[key] = 0
if @oldest == nil
@oldest = key
end
end
def get(key)
@cache[key]
end
def elder
ret = nil
old = 0
@age.each do |k, v|
if old > v
old = v
ret = k
end
end
ret
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment