package com.ponzischeme89.memby.data

import org.junit.Assert.assertEquals
import org.junit.Test

class ImageCacheTest {

    @Test
    fun `an empty cache reads as zero megabytes rather than zero bytes`() {
        // "0 B" on a storage page reads as a measurement that failed. The unit the rest of
        // the page is counting in is what says the cache is simply empty.
        assertEquals("0 MB", formatCacheSize(0L))
        assertEquals("0 MB", formatCacheSize(-1L))
    }

    @Test
    fun `sizes carry one unit and at most one decimal`() {
        assertEquals("512 B", formatCacheSize(512L))
        assertEquals("2 KB", formatCacheSize(2048L))
        assertEquals("1 MB", formatCacheSize(1024L * 1024L))
        assertEquals("1.5 MB", formatCacheSize(1024L * 1024L * 3L / 2L))
        assertEquals("128 MB", formatCacheSize(128L * 1024L * 1024L))
        assertEquals("1.2 GB", formatCacheSize((1.23 * 1024 * 1024 * 1024).toLong()))
    }

    @Test
    fun `a whole number keeps no trailing decimal point`() {
        assertEquals("2 MB", formatCacheSize(2L * 1024L * 1024L))
        assertEquals("1 GB", formatCacheSize(1024L * 1024L * 1024L))
    }

    @Test
    fun `the total is both halves`() {
        val size = ImageCacheSize(diskBytes = 90L * 1024L * 1024L, memoryBytes = 38L * 1024L * 1024L)
        assertEquals(128L * 1024L * 1024L, size.totalBytes)
        assertEquals("128 MB", formatCacheSize(size.totalBytes))
    }
}
