1

Good afternoon. I want to test the LRU algorithm.

My implementation of LRU:

public class LRUCache {

    private int capacity;
    private LinkedHashMap<Integer, Element> cacheMap = new LinkedHashMap<Integer, Element>(0, 0.75f, true);

    public LRUCache(int capacity) {
        this.capacity = capacity;
    }

    private Element newEntity = new Element();

    public void put(int key, String value) {
        if (cacheMap.size() == capacity) {
            Map.Entry<Integer, Element> element = cacheMap.entrySet().iterator().next();
            int tempKey = element.getKey();
            cacheMap.remove(tempKey);
            addToMap(key, value);
        } else {
            addToMap(key, value);
        }
    }

    public void addToMap(int key, String value) {
        newEntity.setValue(value);
        cacheMap.put(key, newEntity);
    }

}

Test:

LRUCache actualList = new LRUCache<>(2);
LinkedHashMap<Integer, String> expectedList = new LinkedHashMap<>();

@Test
public void test(){

    actualList.put(1, "a");
    actualList.put(2, "b");
    actualList.put(3, "c");

    expectedList.put(2, "b");
    expectedList.put(3, "c");

    Assert.assertEquals(expectedList, actualList);

}

I've tried converting my actualList to map in LRUCache :

public LinkedHashMap converter() {
    return new LinkedHashMap(cacheMap);
}

But in all my attempts to transform my algorithm, a new Linkedhashmap object is created each time. I thought maybe you need to copy from one map to another, but then it will be larger than the specified size.

Due to the small Luggage of knowledge, I know that make somewhere a stupid mistake, please tell me how to do it properly or point to an example.

Kaan
  • 5,434
  • 3
  • 19
  • 41
Ilya Y
  • 744
  • 6
  • 24
  • 1
    Your addToMap() method doesn't make sense: you always add the same, unique Element object to the map. Read https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value. It should be private, too, otherwise anyone can add as many entries to the map. – JB Nizet Dec 24 '19 at 17:31
  • @jb-nizet ok, thanks. i will fix. but i have another question. How i can make compare – Ilya Y Dec 24 '19 at 17:33
  • An LRUCache can't possibly be equal to a LinkedHashMap, too. Those are not instances of the same class. You need to compare things that are comparable. – JB Nizet Dec 24 '19 at 17:34
  • @JBNizet Can I extend from Linkedhashmap? – Ilya Y Dec 24 '19 at 17:36
  • 2
    No, you really, really shouldn't do that. Your LRUCache is, at the moment, useless: you can add things to it, but not get anything out of it. Add the methods allowing to get what it contains (the set of keys, the value for each key), and then test that what it contains is what you expect it to contain. – JB Nizet Dec 24 '19 at 17:39
  • @JBNizet good. thanks, i will try – Ilya Y Dec 24 '19 at 17:41

1 Answers1

1

Your 'LRUCache' class doesn't implement hashCode or equals so the test line:

Assert.assertEquals(expectedList, actualList); is using the default Object implementation which will be returning false.

Perhaps you mean to make LRUCache a subclass of AbstractMap, or if you don't need that, just have your test compare the internal contents cacheMap with the expected contents.

karmakaze
  • 34,689
  • 1
  • 30
  • 32