Home | History | Annotate | Download | only in gallery
      1 package com.android.camera.gallery;
      2 
      3 import android.test.AndroidTestCase;
      4 import android.util.Log;
      5 
      6 public class LruCacheUnitTests extends AndroidTestCase {
      7 
      8     public void testPut() {
      9         LruCache<Integer, Integer> cache = new LruCache<Integer, Integer>(2);
     10         Integer key = Integer.valueOf(1);
     11         Integer value = Integer.valueOf(3);
     12         cache.put(key, value);
     13         assertEquals(value, cache.get(key));
     14     }
     15 
     16     public void testTracingInUsedObject() {
     17         LruCache<Integer, Integer> cache = new LruCache<Integer, Integer>(2);
     18         Integer key = Integer.valueOf(1);
     19         Integer value = new Integer(3);
     20         cache.put(key, value);
     21         for (int i = 0; i < 3; ++i) {
     22             cache.put(i + 10, i * i);
     23         }
     24         System.gc();
     25         assertEquals(value, cache.get(key));
     26     }
     27 
     28     public void testLruAlgorithm() {
     29         LruCache<Integer, Integer> cache = new LruCache<Integer, Integer>(2);
     30         cache.put(0, new Integer(0));
     31         for (int i = 0; i < 3; ++i) {
     32             cache.put(i + 1, i * i);
     33             cache.get(0);
     34         }
     35         System.gc();
     36         assertEquals(Integer.valueOf(0), cache.get(0));
     37     }
     38 
     39     private static final int TEST_COUNT = 10000;
     40 
     41     static class Accessor extends Thread {
     42         private final LruCache<Integer,Integer> mMap;
     43 
     44         public Accessor(LruCache<Integer, Integer> map) {
     45             mMap = map;
     46         }
     47 
     48         @Override
     49         public void run() {
     50             Log.v("TAG", "start get " + this);
     51             for (int i = 0; i < TEST_COUNT; ++i) {
     52                 mMap.get(i % 2);
     53             }
     54             Log.v("TAG", "finish get " + this);
     55         }
     56     }
     57 
     58     @SuppressWarnings("unchecked")
     59     public void testConcurrentAccess() throws Exception {
     60         LruCache<Integer, Integer> cache = new LruCache<Integer, Integer>(4);
     61         cache.put(0, 0);
     62         cache.put(1, 1);
     63         Accessor accessor[] = new Accessor[4];
     64         for (int i = 0; i < accessor.length; ++i) {
     65             accessor[i] = new Accessor(cache);
     66         }
     67         for (int i = 0; i < accessor.length; ++i) {
     68             accessor[i].start();
     69         }
     70         for (int i = 0; i < accessor.length; ++i) {
     71             accessor[i].join();
     72         }
     73     }
     74 }
     75