Skip to content

Commit

Permalink
Resolve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanshumittal committed Aug 9, 2023
1 parent baa0bfe commit 368ee24
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
9 changes: 7 additions & 2 deletions gapic-common/lib/gapic/lru_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

module Gapic
##
# @private
#
# Linked list based hash maintaining the order of
# access/creation of the keys.
#
Expand Down Expand Up @@ -49,9 +51,9 @@ def put key, value
private

def move_to_top node
return if node == @start
return if node.equal? @start

if node == @end
if node.equal? @end
@end = node.prev
@end.next = nil
else
Expand Down Expand Up @@ -83,7 +85,10 @@ def insert_at_top node
end

##
# @private
#
# Node class for linked list.
#
class Node
attr_accessor :key
attr_accessor :value
Expand Down
30 changes: 16 additions & 14 deletions gapic-common/test/gapic/lru_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,40 @@
require "test_helper"
require "gapic/lru_hash"

##
# Tests LRU hash class
#
class LruHashTest < Minitest::Test

def test_hash_size_exceed
lru_cache = Gapic::LruHash.new(3)
lru_cache.put 1, 'one'
lru_cache.put 2, 'two'
lru_cache.put 3, 'three'
lru_cache.put(4, 'four')
lru_cache = Gapic::LruHash.new 3
lru_cache.put 1, "one"
lru_cache.put 2, "two"
lru_cache.put 3, "three"
lru_cache.put 4, "four"
assert lru_cache.get(1).nil?
assert_equal 'four', lru_cache.get(4)
assert_equal "four", lru_cache.get(4)
end

def test_hash_size_value
assert_raises ArgumentError do
Gapic::LruHash.new(0)
Gapic::LruHash.new 0
end
assert_raises ArgumentError do
Gapic::LruHash.new(-1)
end
end

def test_hash_removes_lru_value
lru_cache = Gapic::LruHash.new(3)
lru_cache.put 1, 'one'
lru_cache.put 2, 'two'
lru_cache.put 3, 'three'
lru_cache = Gapic::LruHash.new 3
lru_cache.put 1, "one"
lru_cache.put 2, "two"
lru_cache.put 3, "three"
lru_cache.get 3
lru_cache.get 2
lru_cache.get 1

lru_cache.put(4, 'four')
lru_cache.put 4, "four"
assert lru_cache.get(3).nil?
assert_equal 'four', lru_cache.get(4)
assert_equal "four", lru_cache.get(4)
end
end

0 comments on commit 368ee24

Please sign in to comment.