|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +require "test_helper" |
| 16 | +require "gapic/lru_hash" |
| 17 | + |
| 18 | +## |
| 19 | +# Tests LRU hash class |
| 20 | +# |
| 21 | +class LruHashTest < Minitest::Test |
| 22 | + def test_hash_size_exceed |
| 23 | + lru_cache = Gapic::LruHash.new 3 |
| 24 | + lru_cache.put 1, "one" |
| 25 | + lru_cache.put 2, "two" |
| 26 | + lru_cache.put 3, "three" |
| 27 | + lru_cache.put 4, "four" |
| 28 | + assert lru_cache.get(1).nil? |
| 29 | + assert_equal "four", lru_cache.get(4) |
| 30 | + end |
| 31 | + |
| 32 | + def test_hash_size_value |
| 33 | + assert_raises ArgumentError do |
| 34 | + Gapic::LruHash.new 0 |
| 35 | + end |
| 36 | + assert_raises ArgumentError do |
| 37 | + Gapic::LruHash.new(-1) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def test_hash_removes_lru_value |
| 42 | + lru_cache = Gapic::LruHash.new 3 |
| 43 | + lru_cache.put 1, "one" |
| 44 | + lru_cache.put 2, "two" |
| 45 | + lru_cache.put 3, "three" |
| 46 | + lru_cache.get 3 |
| 47 | + lru_cache.get 2 |
| 48 | + lru_cache.get 1 |
| 49 | + |
| 50 | + lru_cache.put 4, "four" |
| 51 | + assert lru_cache.get(3).nil? |
| 52 | + assert_equal "four", lru_cache.get(4) |
| 53 | + end |
| 54 | +end |
0 commit comments