Cotton code
155 lines · 8.5 KB · KotlinRaw
package com.cottondb.musicimport com.cottondb.music.faces.Cardimport com.cottondb.music.faces.Measureimport com.cottondb.music.faces.TrackPlayimport com.cottondb.music.faces.TrayDeckimport com.cottondb.music.faces.TrayKindimport com.cottondb.music.faces.TrayKnobsimport com.cottondb.music.faces.TraySpotimport com.cottondb.music.faces.clockLeftimport com.cottondb.music.faces.trayLineimport com.cottondb.music.faces.trayStripTitleimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Testimport kotlin.math.cosimport kotlin.math.hypot/** The tray's setting rules (AlbumTrayFace.swift's TrayDeck, the turntable's turn), on invented cards. */class TraySettingTest {    /** Every letter half its size wide. */    private val m = Measure { words, _, size -> words.length * size * 0.5f }    private fun card(slots: String, face: String = "album.tray") =        Card(Json.parseToJsonElement("""{"id":"c","face":"$face","slots":$slots}""").jsonObject)    @Test fun theTiltStandsTheTrayAtItsSeenShare() {        assertEquals((cos(Math.toRadians(30.0)) / 1.2).toFloat(), TrayKnobs.tilted, 1e-5f)        // A tall room: the tray fills 0.92 of its width.        assertEquals(0.92f * 400f, TrayDeck.width(400f, 800f), 1e-3f)        // A wide room: of its height as the tilted tray stands.        assertEquals(0.92f * 300f * 1200f / (1600f * TrayKnobs.tilted), TrayDeck.width(1000f, 300f), 1e-2f)    }    @Test fun theArmIsPaintedOnTheTwelveInchOuterGroove() {        assertEquals(hypot(928.7123287671233 - 600, 920.2191780821918 - 590), TrayDeck.painted, 1e-9)        assertEquals(0.0, TrayDeck.arm(0.0, TrayKind.Album), 1e-9)        assertTrue(TrayDeck.arm(1.0, TrayKind.Album) > 0)        // The 7-inch's outer groove lies inside the 12-inch's: the arm starts further in.        assertTrue(TrayDeck.arm(0.0, TrayKind.Song) > 0)        assertTrue(TrayDeck.arm(1.0, TrayKind.Song) > TrayDeck.arm(0.0, TrayKind.Song))    }    @Test fun theArmRestsLiftsAndSweeps() {        assertEquals(TrayKnobs.rest, TrayDeck.arm(TrackPlay.still, kind = TrayKind.Album), 0.0)        val playing = TrackPlay(held = true, playing = true, elapsed = 60.0, duration = 240.0)        assertEquals(TrayDeck.arm(0.25, TrayKind.Song), TrayDeck.arm(playing, kind = TrayKind.Song), 1e-9)        val paused = playing.copy(playing = false)        assertEquals(TrayDeck.arm(0.25, TrayKind.Song) - TrayKnobs.lift, TrayDeck.arm(paused, kind = TrayKind.Song), 1e-9)        // Loading counts as playing: the jewel lit.        assertTrue(TrayDeck.playing(paused.copy(loading = true)))    }    @Test fun anAlbumsShareRunsOverItsSongs() {        val play = TrackPlay(held = true, playing = true, elapsed = 50.0, duration = 200.0, song = 1)        assertEquals(0.375, TrayDeck.share(play, listOf(100.0, 200.0, 100.0)), 1e-9)        // Past the song's length it holds at its end.        assertEquals(0.75, TrayDeck.share(play.copy(elapsed = 900.0), listOf(100.0, 200.0, 100.0)), 1e-9)        // No songs' lengths: over the song.        assertEquals(0.25, TrayDeck.share(play), 1e-9)        assertEquals(0.0, TrayDeck.share(TrackPlay.still, listOf(100.0)), 0.0)        val album = card("""{"title":"Night Ferry","songs":[{"title":"A","duration":"100"},{"title":"B","duration":200},{"title":"C"}]}""")        assertEquals(listOf(100.0, 200.0, 0.0), TrayDeck.songs(album))    }    @Test fun theArmsTurnSeeksAlongTheSong() {        for (share in listOf(0.0, 0.3, 0.7, 1.0)) {            assertEquals(share, TrayDeck.shareAtArm(TrayDeck.arm(share, TrayKind.Song), TrayKind.Song), 1e-6)        }        val sweep = TrayDeck.arm(1.0, TrayKind.Song) - TrayDeck.arm(0.0, TrayKind.Song)        assertEquals(240.0, TrayDeck.turned(0.0, sweep * 4, 240.0, TrayKind.Song), 1e-6)        assertEquals(0.0, TrayDeck.turned(100.0, -sweep * 4, 240.0, TrayKind.Song), 1e-6)        assertEquals(0.0, TrayDeck.turned(100.0, 1.0, 0.0, TrayKind.Song), 0.0)        assertEquals(10.0, TrayDeck.turn(350.0, 0.0), 1e-9)        assertEquals(-10.0, TrayDeck.turn(0.0, 350.0), 1e-9)        assertEquals(90.0, TrayDeck.angle(TraySpot(10.0, 0.0), TraySpot(0.0, 0.0)), 1e-9)        assertEquals(0.0, TrayDeck.angle(TraySpot(0.0, -10.0), TraySpot(0.0, 0.0)), 1e-9)    }    @Test fun aFingerFindsTheArmAndTheRecord() {        assertTrue(TrayDeck.onArm(TrayKnobs.pivot, 0.0))        assertTrue(TrayDeck.onArm(TrayDeck.needle(12.0), 12.0))        assertFalse(TrayDeck.onArm(TraySpot(100.0, 1400.0), 0.0))        assertTrue(TrayDeck.onRecord(TrayKnobs.centre, TrayKind.Album))        assertTrue(TrayDeck.onRecord(TraySpot(150.0, 590.0), TrayKind.Album))        assertFalse(TrayDeck.onRecord(TraySpot(150.0, 590.0), TrayKind.Song))    }    @Test fun theLabelTheStripAndTheTimesAreTheCards() {        val song = card("""{"title":"Harbour Lights","subtitle":"The Low Tide","album":"Night Ferry"}""", "song.tray")        assertEquals(TrayKind.Song, TrayKind.of(song.face))        assertEquals(listOf("Harbour Lights", "The Low Tide"), TrayDeck.label(song))        assertEquals(listOf("Harbour Lights", "The Low Tide"), TrayDeck.strip(song, player = false))        assertEquals(listOf("Night Ferry", "The Low Tide"), TrayDeck.strip(song, player = true))        val player = card("""{"title":"Harbour Lights","artist":"Mara Vance","subtitle":"The Low Tide","duration":242}""", "song.tray")        assertEquals(listOf("Harbour Lights", "Mara Vance"), TrayDeck.label(player))        assertEquals(242.0, TrayDeck.length(TrackPlay.still, player), 0.0)        assertEquals(300.0, TrayDeck.length(TrackPlay(held = true, duration = 300.0), player), 0.0)        assertEquals(emptyList<String>(), TrayDeck.times(TrackPlay.still))        assertEquals(emptyList<String>(), TrayDeck.times(TrackPlay(held = true, elapsed = 3.0)))        assertEquals(listOf("2:14", "−1:48"), TrayDeck.times(TrackPlay(held = true, playing = true, elapsed = 134.7, duration = 242.0)))        assertEquals("−0:00", clockLeft(300.0, 242.0))    }    @Test fun theRecordTurnsAtThirtyThreeAndAThird() {        assertEquals(200.0, TrayKnobs.degreesPerSecond, 1e-9)        assertEquals(600.0, TrayDeck.turnOf(3.0, playing = true, phase = 0.0), 1e-9)        // Paused it coasts on to the next whole turn, its label upright.        assertEquals(720.0, TrayDeck.turnOf(3.0, playing = false, phase = 0.0), 1e-9)        assertEquals(0.0, TrayDeck.turnOf(3.0, playing = true, phase = 0.0, reduceMotion = true), 0.0)        assertEquals(120.0, TrayDeck.resumed(3.0, 0.0), 1e-9)        // Playing on keeps the phase; a seek moves it so the record does not spin.        assertEquals(5.0, TrayDeck.phase(5.0, 10.0, 10.25), 0.0)        assertEquals(5.0 - 40 * 200.0, TrayDeck.phase(5.0, 10.0, 50.0), 1e-9)        assertEquals(5.0 + 4 * 200.0, TrayDeck.phase(5.0, 10.0, 6.0), 1e-9)    }    @Test fun theJewelsStoneStandsOnItsCentre() {        val on = TrayDeck.jewel(true)        val scale = 34f / 143f        assertEquals(1097f - (25f + 71.5f) * scale, on.x, 1e-3f)        assertEquals(193f * scale, on.w, 1e-3f)        val off = TrayDeck.jewel(false)        assertEquals(1142f - (10f + 73.5f) * (34f / 138f), off.y, 1e-3f)    }    @Test fun aLabelLineIsTakenDownThenCut() {        // "Harbour Lights": 14 letters at 35 is 245, over 236: taken down to fit.        val set = trayLine("Harbour Lights", m, Measure.Face.Caslon, 35f, 236f, 0.5f)        assertEquals("Harbour Lights", set.words)        assertEquals(35f * 236f / 245f, set.size, 1e-3f)        val long = trayLine("The Slow Train Home Through Sixteen Lanterns", m, Measure.Face.Caslon, 35f, 236f, 0.5f)        assertEquals(17.5f, long.size, 0f)        assertTrue(long.words.endsWith("…"))        assertTrue(m.wide(long.words, Measure.Face.Caslon, 17.5f) <= 236f)    }    @Test fun theStripsTitleKeepsToTwoLines() {        val (short, size) = trayStripTitle("Night Ferry", m, Measure.Face.Caslon)        assertEquals(listOf("Night Ferry"), short)        assertEquals(42f, size, 0f)        val (two, twoSize) = trayStripTitle("Sixteen Lanterns and the Blue Hour", m, Measure.Face.Caslon)        assertTrue(two.size <= 2)        assertTrue(twoSize < 42f && twoSize >= 42f * 0.6f)        assertTrue(two.all { m.wide(it, Measure.Face.Caslon, twoSize) <= 336f })        val (cut, least) = trayStripTitle("The Slow Train Home Through Sixteen Lanterns and the Blue Hour Sessions", m, Measure.Face.Caslon)        assertEquals(2, cut.size)        assertEquals(42f * 0.6f, least, 1e-4f)        assertTrue(cut.last().endsWith("…"))    }}