Guest User

Untitled

a guest
Jul 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. require 'rspec'
  3.  
  4. class LRUCache
  5. attr_reader :order, :capacity
  6.  
  7. def initialize(capacity)
  8. @capacity = capacity
  9. @order = []
  10. @hash = {}
  11. end
  12. def put key, value
  13. @hash[key] = value
  14. touch key
  15. truncate!
  16. end
  17. def get key
  18. @hash[key]
  19. end
  20. def get! key
  21. touch key
  22. get key
  23. end
  24. def touch key
  25. @order.delete key
  26. @order.unshift key
  27. end
  28. def truncate!
  29. while @order.size > @capacity
  30. @hash.delete(@order.pop)
  31. end
  32. end
  33. def resize! size
  34. @capacity = size
  35. truncate!
  36. end
  37. end
  38.  
  39. describe LRUCache do
  40. describe 'with capacty 3' do
  41. before do
  42. @cache = LRUCache.new(3)
  43. end
  44. context 'resizing capacity' do
  45. context 'resizing to smaller' do
  46. it 'should decrease capacity' do
  47. @cache.resize! 2
  48. @cache.capacity.should == 2
  49. end
  50. it 'should truncate old elements of cache' do
  51. @cache.put('key1', 'value1')
  52. @cache.put('key2', 'value2')
  53. @cache.put('key3', 'value3')
  54. @cache.resize! 2
  55. @cache.get('key1').should be_nil
  56. @cache.get('key2').should == 'value2'
  57. @cache.get('key3').should == 'value3'
  58. end
  59. end
  60. context 'resizing to 0' do
  61. it 'should be empty' do
  62. @cache.put('key1', 'value1')
  63. @cache.put('key2', 'value2')
  64. @cache.put('key3', 'value3')
  65. @cache.resize! 0
  66. @cache.get('key1').should be_nil
  67. @cache.get('key2').should be_nil
  68. @cache.get('key3').should be_nil
  69. end
  70. end
  71. end
  72. context 'same key inserted twice' do
  73. before do
  74. @cache.put('key1', 'value1')
  75. @cache.put('key1', 'value1-2')
  76. end
  77. it "updates key1's value" do
  78. @cache.get('key1').should == 'value1-2'
  79. end
  80. end
  81. context 'put a key, and put another key many times' do
  82. before do
  83. @cache.put('key1', 'value1')
  84. 3.times {
  85. @cache.put('key2', 'value2')
  86. }
  87. end
  88. it "should keeps key1" do
  89. @cache.get('key1').should == 'value1'
  90. end
  91. end
  92. context 'two pairs inserted' do
  93. before do
  94. @cache.put('key1', 'value1')
  95. @cache.put('key2', 'value2')
  96. end
  97. it 'contains two pairs' do
  98. @cache.get('key1').should == 'value1'
  99. @cache.get('key2').should == 'value2'
  100. end
  101. it 'preserves the order inserted' do
  102. @cache.order.should == ['key2', 'key1']
  103. end
  104. end
  105. end
  106. describe 'with capcity 2' do
  107. before do
  108. @cache = LRUCache.new(2)
  109. end
  110. context 'doing get! while putting values' do
  111. before do
  112. @cache.put('key1', 'value1')
  113. @cache.put('key2', 'value2')
  114. @cache.get!('key1')
  115. @cache.put('key3', 'value3')
  116. end
  117. it 'should change key order' do
  118. @cache.get('key1').should == 'value1'
  119. @cache.get('key2').should be_nil
  120. @cache.get('key3').should == 'value3'
  121. end
  122. end
  123. end
  124.  
  125. describe 'with capcity 1' do
  126. before do
  127. @cache = LRUCache.new(1)
  128. end
  129. context 'same key inserted twice' do
  130. before do
  131. @cache.put('key1', 'value1')
  132. @cache.put('key1', 'value1-2')
  133. end
  134. it "updates key1's value" do
  135. @cache.get('key1').should == 'value1-2'
  136. end
  137. end
  138. it '要素を 2 つ追加して,1つめが取れないこと' do
  139. @cache.put('key1', 'value1')
  140. @cache.put('key2', 'value2')
  141. @cache.get('key1').should be_nil
  142. @cache.get('key2').should == 'value2'
  143. end
  144. it '要素を 3 つ追加して,3つめ以外が取れないこと' do
  145. @cache.put('key1', 'value1')
  146. @cache.put('key2', 'value2')
  147. @cache.put('key3', 'value3')
  148. @cache.get('key1').should be_nil
  149. @cache.get('key2').should be_nil
  150. @cache.get('key3').should == 'value3'
  151. end
  152. end
  153.  
  154. it "with capacity 0"
  155. it "with capacity -1"
  156. it "with capacity given by not integer"
  157. end
Add Comment
Please, Sign In to add comment