Cotton code
164 lines · 7.9 KB · KotlinRaw
package com.cottondb.musicimport com.cottondb.music.faces.Cardimport com.cottondb.music.faces.Measureimport com.cottondb.music.faces.PartyKnobsimport com.cottondb.music.faces.PartyLabelimport com.cottondb.music.faces.TrackPlayimport com.cottondb.music.faces.TrayDeckimport com.cottondb.music.faces.TraySpotimport com.cottondb.music.faces.TurntableDeckimport com.cottondb.music.faces.TurntableKnobsimport com.cottondb.music.faces.TurntableLookimport com.cottondb.music.faces.partyTitleimport com.cottondb.music.faces.turntableCapitalsimport com.cottondb.music.faces.turntableTitleimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport kotlin.math.hypot/** The turntable's and the party's rules (SongTurntableTests, SongPartyTests), on invented cards. */class TurntableSettingTest {    private val k = TurntableKnobs    /** Every letter half its size wide. */    private val m = Measure { said, _, size -> said.length * size * 0.5f }    private fun card(slots: String) = Card(Json.parseToJsonElement("""{"id":"c","face":"song.turntable","slots":$slots}""").jsonObject)    private val harbour = card("""{"title":"Harbour Lights","artist":"The Low Tide","album":"Night Ferry","duration":242}""")    @Test fun itPlaysByThePlayersWord() {        assertFalse(TurntableDeck.playing(TrackPlay.still))        assertTrue(TurntableDeck.playing(TrackPlay(held = true, playing = true)))        assertTrue(TurntableDeck.playing(TrackPlay(held = true, loading = true)))        assertFalse(TurntableDeck.playing(TrackPlay(held = true)))    }    @Test fun theArmIsTheClock() {        assertEquals(0.0, TurntableDeck.arm(0.0, 242.0), 1e-9)        assertEquals(20.86014283177707, TurntableDeck.arm(242.0, 242.0), 1e-3)        assertEquals(20.86014283177707, TurntableDeck.sweep, 1e-3)        assertEquals(9.717391291810642, TurntableDeck.arm(134.0, 242.0), 1e-3)        assertEquals(TurntableDeck.sweep, TurntableDeck.arm(500.0, 242.0), 1e-9)        assertEquals(0.0, TurntableDeck.arm(10.0, 0.0), 0.0)        var last = -1.0        for (second in 0..242 step 11) {            val angle = TurntableDeck.arm(second.toDouble(), 242.0)            assertTrue(angle > last)            last = angle        }    }    @Test fun theNeedleSitsOnTheGroove() {        val outer = TurntableDeck.needle(0.0)        assertEquals(k.stylus.x, outer.x, 1e-9)        assertEquals(k.stylus.y, outer.y, 1e-9)        for (share in listOf(0.0, 0.25, 0.5537, 1.0)) {            val at = TurntableDeck.needle(TurntableDeck.arm(share * 242, 242.0))            assertEquals(k.outer + (k.inner - k.outer) * share, hypot(at.x - k.centre.x, at.y - k.centre.y), 1e-6)            assertEquals(share, TurntableDeck.shareAtArm(TurntableDeck.arm(share * 242, 242.0)), 1e-9)        }        assertTrue(k.inner > k.label.w / 2)    }    @Test fun aDragOfTheArmSeeks() {        assertEquals(134.0, TurntableDeck.turned(134.0, 0.0, 242.0), 1e-6)        assertEquals(242.0, TurntableDeck.turned(0.0, TurntableDeck.sweep, 242.0), 1e-6)        assertEquals(0.0, TurntableDeck.turned(100.0, -90.0, 242.0), 0.0)        assertEquals(242.0, TurntableDeck.turned(100.0, 90.0, 242.0), 1e-6)        assertTrue(TurntableDeck.turned(100.0, 2.0, 242.0) > 100)        assertEquals(0.0, TurntableDeck.turned(100.0, 2.0, 0.0), 0.0)    }    @Test fun pausedTheArmLifts() {        assertEquals(TurntableDeck.arm(134.0, 242.0), TurntableDeck.arm(134.0, 242.0, false), 0.0)        assertEquals(TurntableDeck.arm(134.0, 242.0) - k.lift, TurntableDeck.arm(134.0, 242.0, true), 1e-12)        assertTrue(k.shadowLifted.y > k.shadowDown.y)    }    @Test fun theRecordTurns() {        assertEquals(360.0, TrayDeck.turnOf(1.8, true, 0.0), 1e-9)        assertEquals(26_800.0, TrayDeck.turnOf(134.0, true, 0.0), 1e-9)        val rest = TrayDeck.turnOf(134.0, false, 0.0)        assertEquals(0.0, rest % 360, 0.0)        assertTrue(rest >= 26_800 && rest < 26_800 + 360)        val phase = TrayDeck.resumed(134.0, 0.0)        assertEquals(rest, TrayDeck.turnOf(134.0, true, phase), 1e-9)        assertEquals(0.0, TrayDeck.turnOf(134.0, true, 77.0, reduceMotion = true), 0.0)    }    @Test fun theLabelIsTheCards() {        assertEquals(listOf("Harbour Lights", "THE LOW TIDE", "NIGHT FERRY"), TurntableDeck.label(harbour))        assertEquals(emptyList<String>(), TurntableDeck.label(card("{}")))        assertEquals(listOf("2:14", "−1:48", "4:02"), TurntableDeck.times(134.0, 242.0))        assertEquals(listOf("2:14", "−1:48", "4:02"), TurntableDeck.times(134.9, 242.0))        assertEquals(listOf("0:05"), TurntableDeck.times(5.0, 0.0))    }    @Test fun theArmIsFoundAlongItsLineAndTheRecordRound() {        // The needle's end and the pivot's, as painted and turned with the arm.        assertTrue(TurntableDeck.onArm(TraySpot(k.arm.x + 37.0, k.arm.y + 1126.0), 0.0))        assertTrue(TurntableDeck.onArm(TurntableDeck.needle(15.0), 15.0))        assertFalse(TurntableDeck.onArm(TraySpot(k.centre.x, k.centre.y), 0.0))        assertTrue(TurntableDeck.onRecord(TraySpot(k.centre.x, k.centre.y)))        assertFalse(TurntableDeck.onRecord(TraySpot(5.0, 160.0)))    }    @Test fun besideTheRecordReachesTheWholeDrawing() {        val whole = TurntableLook.DarkRoom.whole        assertEquals(whole.x, k.previous(whole).x, 0f)        assertEquals(k.centre.x.toFloat(), k.previous(whole).right, 0f)        assertEquals(whole.right, k.next(whole).right, 0f)        assertEquals(k.slipmat.h, k.next(whole).h, 0f)    }    @Test fun theNameTakesOneLineAtItsSizeElseSmallerElseTwo() {        assertEquals(128f, turntableTitle("Lights", m).size, 0f)        assertEquals(107.52f, turntableTitle("Slow Train", m).size, 1e-3f)        val long = turntableTitle("The Slow Train Home Through Sixteen Lanterns", m)        assertTrue(long.lines.size <= 2)        long.lines.forEach { assertTrue(m.wide(it, Measure.Face.Caslon, long.size) <= k.titleBox.w) }    }    @Test fun capitalsAreSpacedWhileTheyFit() {        val spaced = turntableCapitals("The Low Tide", k.artistSize, k.artistBox.w, m)        assertEquals("THE LOW TIDE", spaced.words)        assertEquals(k.capitalsTracking, spaced.tracking, 0f)        val close = turntableCapitals("Sixteen Lanterns and the Blue Hour Sessions", k.albumSize, k.albumBox.w, m)        assertEquals(0f, close.tracking, 0f)        assertTrue(close.size < k.albumSize)    }    @Test fun thePartysLabelIsTheCardsInCapitals() {        assertEquals("HARBOUR LIGHTS", PartyLabel.title(harbour))        assertEquals("THE LOW TIDE", PartyLabel.artist(harbour))        assertEquals("NIGHT FERRY", PartyLabel.album(harbour))        assertNull(PartyLabel.title(card("{}")))        val long = partyTitle("THE SLOW TRAIN HOME THROUGH SIXTEEN LANTERNS", m)        assertTrue(long.lines.size <= PartyKnobs.titleLines)        assertEquals(PartyKnobs.titleSize, partyTitle("SLOW TRAIN", m).size, 0f)        assertEquals(TurntableLook.Party, TurntableLook.of("song.party"))        assertEquals(TurntableLook.DarkRoom, TurntableLook.of("song.turntable"))    }    @Test fun thePartysLabelLiesInsideTheRunOut() {        val p = PartyKnobs        assertEquals(k.centre.x.toFloat(), p.label.midX, 1e-3f)        assertEquals(k.centre.y.toFloat(), p.label.midY, 1e-3f)        assertTrue(p.round * p.label.w / p.painted < k.inner)        val centre = p.painted / 2        assertTrue(p.titleBox.bottom < centre - p.hole)        listOf(p.artistBox, p.albumBox, p.timesBox).forEach { assertTrue(it.y > centre + p.hole) }    }    @Test fun theLightsDriftHalfAPointASecondAndWrap() {        assertEquals(0f, PartyKnobs.lights(0.0), 0f)        assertEquals(5f, PartyKnobs.lights(10.0), 1e-4f)        assertEquals(5f, PartyKnobs.lights(PartyKnobs.lap + 10), 1e-3f)        assertEquals(0f, PartyKnobs.lights(10.0, still = true), 0f)    }}