Cotton code
97 lines · 5.1 KB · KotlinRaw
package com.cottondb.albumsimport android.app.Applicationimport com.cottondb.albums.analysis.AnalysisPlanimport com.cottondb.albums.analysis.AnalysisStoreimport com.cottondb.albums.analysis.Exposureimport com.cottondb.albums.analysis.FaceLookimport com.cottondb.albums.analysis.ItemLookimport com.cottondb.albums.analysis.Paceimport com.cottondb.albums.analysis.Stageimport com.cottondb.albums.library.LibraryItemimport org.junit.Assert.assertArrayEqualsimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.RuntimeEnvironmentimport org.robolectric.annotation.Config/** The pass resumes where it stopped: what is written stays written, what is not is looked at next, newest first. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], application = Application::class)class AnalysisResumeTest {    private fun item(id: Long, taken: Long = id * 1000, pending: Boolean = false, video: Boolean = false) =        LibraryItem(id, video, "external_primary", 1, "Camera", "DCIM/Camera/", "IMG_$id.jpg", taken, id, id, 1_000, 4000, 3000, "image/jpeg", null, pending, null, id, false)    private fun look(i: LibraryItem, faces: List<FaceLook> = emptyList()) = ItemLook(        i, 512, 384, 0x0123456789abcdefL, ByteArray(256) { it.toByte() }, 321f, Exposure(0.4f, 0.01f, 0.1f, 0.4f, 0.9f), 0.1f,        null, false, false, 0f, 0, faces, DoubleArray(Stage.entries.size) { (it + 1).toDouble() },    )    @Test    fun the_plan_is_what_is_not_done_newest_first_never_a_pending_one() {        val items = listOf(item(1, 10), item(2, 30), item(3, 20), item(4, 40, pending = true), item(5, 5))        assertEquals(listOf(2L, 3L, 1L, 5L), AnalysisPlan.todo(items, emptySet()).map { it.id })        assertEquals(listOf(3L, 5L), AnalysisPlan.todo(items, setOf(1L, 2L)).map { it.id })        assertEquals(emptyList<Long>(), AnalysisPlan.todo(items, setOf(1L, 2L, 3L, 5L)).map { it.id })    }    @Test    fun a_pass_killed_after_a_batch_resumes_from_what_was_written() {        val app = RuntimeEnvironment.getApplication()        val name = "resume-test.db"        val library = (1L..7L).map { item(it) }        val first = AnalysisStore(app, name)        // The first batch is written; the second was in hand when the process died (never written).        val batch = AnalysisPlan.todo(library, first.done()).take(3)        assertEquals(listOf(7L, 6L, 5L), batch.map { it.id })        val vector = FloatArray(128) { it / 128f }        first.put(batch.map { look(it, if (it.id == 6L) listOf(FaceLook(1f, 2f, 60f, 70f, 3f, 4f, 5f, .9f, .8f, .1f, "yunet", .95f, DoubleArray(10) { it.toDouble() }, vector, com.cottondb.albums.analysis.HyperFace.NAME)) else emptyList()) })        first.close()        val again = AnalysisStore(app, name)        assertEquals(setOf(7L, 6L, 5L), again.done())        assertEquals(3, again.count())        assertEquals(listOf(4L, 3L, 2L, 1L), AnalysisPlan.todo(library, again.done()).map { it.id })        // What was written reads back: the vector to the bit, the stage times per row.        again.readableDatabase.rawQuery("SELECT vector, model, marks FROM face WHERE item = 6", null).use { c ->            assertTrue(c.moveToFirst())            assertArrayEquals(vector, AnalysisStore.floats(c.getBlob(0)), 0f)            assertEquals(com.cottondb.albums.analysis.HyperFace.NAME, c.getString(1))            assertEquals("yunet", c.getString(2))        }        again.readableDatabase.rawQuery("SELECT dhash, ms_thumb, ms_embed, failed FROM item WHERE id = 7", null).use { c ->            assertTrue(c.moveToFirst())            assertEquals(0x0123456789abcdefL, c.getLong(0))            assertEquals(1.0, c.getDouble(1), 0.0)            assertEquals(Stage.entries.size.toDouble(), c.getDouble(2), 0.0)            assertNull(c.getString(3))        }        // Writing the same picture again replaces it (a kill after the write but before the progress is harmless).        again.put(listOf(look(library[6])))        assertEquals(3, again.count())        val t = again.timings()        assertEquals(3, t.getValue("ms_thumb").size)        assertEquals((1..Stage.entries.size).sum().toDouble(), t.getValue("total")[0], 1e-9)        again.forget()        assertEquals(0, again.count())        again.close()    }    @Test    fun the_pace_is_measured_over_the_last_ones_finished() {        val pace = Pace(window = 500)        assertNull(pace.perMinute())        (0..10).forEach { pace.add(it * 1000L) }        assertEquals(60.0, pace.perMinute()!!, 1e-9) // 10 intervals in 10 s        assertEquals(120L, pace.etaSeconds(120))        val short = Pace(window = 3)        listOf(0L, 1_000L, 2_000L, 10_000L, 20_000L).forEach { short.add(it) }        assertEquals(2 * 60_000.0 / 18_000, short.perMinute()!!, 1e-9) // the last three: 2, 10, 20 s        assertEquals(2.0, Pace.percentile(doubleArrayOf(3.0, 1.0, 2.0), 0.5), 0.0)        assertEquals(2.0, Pace.percentile(doubleArrayOf(3.0, 1.0, 2.0, 0.5), 0.95), 0.0) // rank floor(0.95·3) = 2    }}