Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lasse Magnussen committed Feb 14, 2013
1 parent e4adbac commit 95f956b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
Expand Up @@ -42,22 +42,10 @@ public class LruBitmapCache implements CacheManager {
public LruBitmapCache(Context context, int percentageOfMemoryForCache) {
int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();

if(memClass == 0) {
memClass = DEFAULT_MEMORY_CAPACITY_FOR_DEVICES_OLDER_THAN_API_LEVEL_4;
}
if(percentageOfMemoryForCache < 0) {
percentageOfMemoryForCache = 0;
}
if(percentageOfMemoryForCache > 81) {
percentageOfMemoryForCache = 80;
}
this.capacity = (int) ((memClass * percentageOfMemoryForCache * 1024L * 1024L) / 100L);
if(this.capacity <= 0) {
this.capacity = 1024*1024*4;
}
this.capacity = calculateCacheSize(memClass, percentageOfMemoryForCache);
reset();
}

/**
* Setting the default memory size to 25% percent of the total memory
* available of the application.
Expand All @@ -67,6 +55,12 @@ public LruBitmapCache(Context context) {
this(context, DEFAULT_MEMORY_CACHE_PERCENTAGE);
}

/**
* Empty constructor for testing purposes
*/
protected LruBitmapCache() {
}

private void reset() {
if (cache != null) {
cache.evictAll();
Expand Down Expand Up @@ -94,4 +88,21 @@ public void clean() {
reset();
}

public int calculateCacheSize(int memClass, int percentageOfMemoryForCache) {
if(memClass == 0) {
memClass = DEFAULT_MEMORY_CAPACITY_FOR_DEVICES_OLDER_THAN_API_LEVEL_4;
}
if(percentageOfMemoryForCache < 0) {
percentageOfMemoryForCache = 0;
}
if(percentageOfMemoryForCache > 81) {
percentageOfMemoryForCache = 80;
}
int capacity = (int) ((memClass * percentageOfMemoryForCache * 1024L * 1024L) / 100L);
if(capacity <= 0) {
capacity = 1024*1024*4;
}

return capacity;
}
}
@@ -0,0 +1,27 @@
package com.novoda.imageloader.core.cache;

import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;

public class LruBitmapCacheTest {

@Test
public void verifyCalculateCacheSizeDoesNotIntOverflow() {
LruBitmapCache cache = new LruBitmapCacheTestOverride();

long undividedResult = 200L * 25L * 1024L * 1024L;
assertTrue(undividedResult > Integer.MAX_VALUE);

long dividedResult = undividedResult / 100L;
assertTrue(dividedResult < Integer.MAX_VALUE);

long calculatedResult = cache.calculateCacheSize(200, 25);
assertEquals(dividedResult, calculatedResult);
}

private class LruBitmapCacheTestOverride extends LruBitmapCache {

}
}

0 comments on commit 95f956b

Please sign in to comment.