Initial.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package tunes
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KVisibility
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
fun checkAllOtherMembersFinal(
|
||||
targetClass: KClass<*>,
|
||||
vararg expectedNonFinalMembers: String,
|
||||
) {
|
||||
val allowedNames: Set<String> = expectedNonFinalMembers.toSet()
|
||||
var ok = true
|
||||
for (member in targetClass.members) {
|
||||
if (!member.isFinal) {
|
||||
if (allowedNames.contains(member.name)) {
|
||||
continue
|
||||
}
|
||||
System.err.println("Non-final member: ${member.name} in class ${targetClass.simpleName}")
|
||||
ok = false
|
||||
}
|
||||
}
|
||||
assertTrue(ok)
|
||||
}
|
||||
|
||||
fun checkAllOtherMembersPrivate(
|
||||
targetClass: KClass<*>,
|
||||
vararg expectedPublicMembers: String,
|
||||
) {
|
||||
val allowedNames: Set<String> = expectedPublicMembers.toSet()
|
||||
var ok = true
|
||||
for (member in targetClass.members) {
|
||||
if (member.visibility!! == KVisibility.PRIVATE) {
|
||||
continue
|
||||
}
|
||||
if (allowedNames.contains(member.name)) {
|
||||
continue
|
||||
}
|
||||
System.err.println(
|
||||
"Unexpected non-private member ${member.name} in class ${targetClass.simpleName}",
|
||||
)
|
||||
ok = false
|
||||
}
|
||||
assertTrue(ok)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.reflect.KVisibility
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
*/
|
||||
|
||||
class GoodPracticesSongCollection {
|
||||
/*
|
||||
@Test
|
||||
fun testClassIsFinal() {
|
||||
assertTrue(SongCollection::class.isFinal)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllHelperMembersPrivate() {
|
||||
checkAllOtherMembersPrivate(
|
||||
SongCollection::class,
|
||||
"addSong",
|
||||
"getSongNames",
|
||||
"getTune",
|
||||
"equals",
|
||||
"hashCode",
|
||||
"toString",
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllNestedClassesPrivateAndFinal() {
|
||||
for (nestedClass in SongCollection::class.nestedClasses) {
|
||||
assert(nestedClass.isFinal)
|
||||
assert(nestedClass.visibility!! == KVisibility.PRIVATE)
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
*/
|
||||
|
||||
class GoodPracticesTestsNote {
|
||||
/*
|
||||
@Test
|
||||
fun testClassIsFinal() {
|
||||
assertTrue(Note::class.isFinal)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllMembersFinal() {
|
||||
checkAllOtherMembersFinal(
|
||||
Note::class,
|
||||
"equals",
|
||||
"hashCode",
|
||||
"toString",
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllHelperMembersPrivate() {
|
||||
checkAllOtherMembersPrivate(
|
||||
Note::class,
|
||||
"equals",
|
||||
"toString",
|
||||
"hashCode",
|
||||
"hasNoteAbove",
|
||||
"hasNoteBelow",
|
||||
"noteAbove",
|
||||
"noteBelow",
|
||||
"duration",
|
||||
"pitch",
|
||||
"copy",
|
||||
"component1",
|
||||
"component2",
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
*/
|
||||
|
||||
class GoodPracticesTestsStandardTune {
|
||||
/*
|
||||
@Test
|
||||
fun testClassIsFinal() {
|
||||
assertTrue(StandardTune::class.isFinal)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllHelperMembersPrivate() {
|
||||
checkAllOtherMembersPrivate(
|
||||
StandardTune::class,
|
||||
"addNote",
|
||||
"notes",
|
||||
"iterator",
|
||||
"totalDuration",
|
||||
"equals",
|
||||
"hashCode",
|
||||
"toString",
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
*/
|
||||
|
||||
class GoodPracticesTestsThreadSafeTune {
|
||||
/*
|
||||
@Test
|
||||
fun testClassIsFinal() {
|
||||
assertTrue(ThreadSafeTune::class.isFinal)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllHelperMembersPrivate() {
|
||||
checkAllOtherMembersPrivate(
|
||||
ThreadSafeTune::class,
|
||||
"addNote",
|
||||
"notes",
|
||||
"iterator",
|
||||
"totalDuration",
|
||||
"equals",
|
||||
"hashCode",
|
||||
"toString",
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
*/
|
||||
|
||||
class GoodPracticesTestsTransposedTune {
|
||||
/*
|
||||
@Test
|
||||
fun testClassIsFinal() {
|
||||
assertTrue(TransposedTune::class.isFinal)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllHelperMembersPrivate() {
|
||||
checkAllOtherMembersPrivate(
|
||||
TransposedTune::class,
|
||||
"addNote",
|
||||
"notes",
|
||||
"iterator",
|
||||
"totalDuration",
|
||||
"equals",
|
||||
"hashCode",
|
||||
"toString",
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
*/
|
||||
|
||||
class GoodPracticesTestsTune {
|
||||
/*
|
||||
@Test
|
||||
fun testAllHelperMembersPrivate() {
|
||||
checkAllOtherMembersPrivate(
|
||||
Tune::class,
|
||||
"addNote",
|
||||
"notes",
|
||||
"iterator",
|
||||
"totalDuration",
|
||||
"equals",
|
||||
"hashCode",
|
||||
"toString",
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
242
kotlin-tunes-skeleton/src/test/kotlin/tunes/Question1Tests.kt
Normal file
242
kotlin-tunes-skeleton/src/test/kotlin/tunes/Question1Tests.kt
Normal file
@@ -0,0 +1,242 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.fail
|
||||
*/
|
||||
|
||||
class Question1Tests {
|
||||
/*
|
||||
@Test
|
||||
fun testPitch() {
|
||||
assertEquals(5, Note(5, 10.0).pitch)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDuration() {
|
||||
assertEquals(10.0, Note(5, 10.0).duration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorNegativePitch() {
|
||||
try {
|
||||
Note(-1, 2.0)
|
||||
fail()
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
// Good - an exception should have been thrown.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorPitchTooHigh1() {
|
||||
try {
|
||||
Note(201, 8.0)
|
||||
fail()
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
// Good - an exception should have been thrown.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorPitchTooHigh2() {
|
||||
try {
|
||||
Note(5000, 8.0)
|
||||
fail()
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
// Good - an exception should have been thrown.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorZeroDuration() {
|
||||
try {
|
||||
Note(5, 0.0)
|
||||
fail()
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
// Good - an exception should have been thrown.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorNegativeDuration() {
|
||||
try {
|
||||
Note(8, -20.0)
|
||||
fail()
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
// Good - an exception should have been thrown.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorDurationTooHigh1() {
|
||||
try {
|
||||
Note(80, 65.0)
|
||||
fail()
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
// Good - an exception should have been thrown.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorDurationTooHigh2() {
|
||||
try {
|
||||
Note(80, 1000.0)
|
||||
fail()
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
// Good - an exception should have been thrown.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorBottomShortestNote() {
|
||||
Note(0, 1.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorTopFastestNote() {
|
||||
Note(200, 64.0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testToString() {
|
||||
assertEquals("C0(1.0)", Note(0, 1.0).toString())
|
||||
assertEquals("C#0(2.0)", Note(1, 2.0).toString())
|
||||
assertEquals("D0(3.0)", Note(2, 3.0).toString())
|
||||
assertEquals("D#0(4.0)", Note(3, 4.0).toString())
|
||||
assertEquals("E0(5.0)", Note(4, 5.0).toString())
|
||||
assertEquals("F0(6.0)", Note(5, 6.0).toString())
|
||||
assertEquals("F#0(7.0)", Note(6, 7.0).toString())
|
||||
assertEquals("G0(8.0)", Note(7, 8.0).toString())
|
||||
assertEquals("G#0(9.0)", Note(8, 9.0).toString())
|
||||
assertEquals("A0(10.0)", Note(9, 10.0).toString())
|
||||
assertEquals("A#0(11.0)", Note(10, 11.0).toString())
|
||||
assertEquals("B0(12.0)", Note(11, 12.0).toString())
|
||||
assertEquals("C1(13.0)", Note(12, 13.0).toString())
|
||||
assertEquals("C#1(14.0)", Note(13, 14.0).toString())
|
||||
assertEquals("D1(15.0)", Note(14, 15.0).toString())
|
||||
assertEquals("D#1(16.0)", Note(15, 16.0).toString())
|
||||
assertEquals("E1(17.0)", Note(16, 17.0).toString())
|
||||
assertEquals("F1(18.0)", Note(17, 18.0).toString())
|
||||
assertEquals("F#1(19.0)", Note(18, 19.0).toString())
|
||||
assertEquals("G1(20.0)", Note(19, 20.0).toString())
|
||||
assertEquals("G#1(21.0)", Note(20, 21.0).toString())
|
||||
assertEquals("A1(22.0)", Note(21, 22.0).toString())
|
||||
assertEquals("A#1(23.0)", Note(22, 23.0).toString())
|
||||
assertEquals("B1(24.0)", Note(23, 24.0).toString())
|
||||
assertEquals("C4(13.0)", Note(48, 13.0).toString())
|
||||
assertEquals("C#4(14.0)", Note(49, 14.0).toString())
|
||||
assertEquals("D4(15.0)", Note(50, 15.0).toString())
|
||||
assertEquals("D#4(16.0)", Note(51, 16.0).toString())
|
||||
assertEquals("E4(17.0)", Note(52, 17.0).toString())
|
||||
assertEquals("F4(18.0)", Note(53, 18.0).toString())
|
||||
assertEquals("F#4(19.0)", Note(54, 19.0).toString())
|
||||
assertEquals("G4(20.0)", Note(55, 20.0).toString())
|
||||
assertEquals("G#4(21.0)", Note(56, 21.0).toString())
|
||||
assertEquals("A4(22.0)", Note(57, 22.0).toString())
|
||||
assertEquals("A#4(23.0)", Note(58, 23.0).toString())
|
||||
assertEquals("B4(24.0)", Note(59, 24.0).toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEquals1() {
|
||||
val note = Note(1, 3.0)
|
||||
assertEquals(note, note)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEquals2() {
|
||||
val note1 = Note(1, 3.0)
|
||||
val note2 = Note(1, 3.0)
|
||||
assertEquals(note1, note2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEquals3() {
|
||||
val note1 = Note(1, 3.0)
|
||||
val note2 = Note(2, 3.0)
|
||||
assertNotEquals(note1, note2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEquals4() {
|
||||
val note1 = Note(1, 3.0)
|
||||
val note2 = Note(1, 4.0)
|
||||
assertNotEquals(note1, note2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEquals5() {
|
||||
val note = Note(1, 3.0)
|
||||
assertNotEquals<Note?>(note, null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEquals6() {
|
||||
val note = Note(1, 3.0)
|
||||
assertNotEquals<Any>(note, note.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEquals7() {
|
||||
val note1 = Note(1, 3.0)
|
||||
val note2 = Note(1, 3.0)
|
||||
val notes: MutableSet<Note> = HashSet()
|
||||
notes.add(note1)
|
||||
assertTrue(notes.contains(note2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasNoteAbove1() {
|
||||
assertTrue(Note(0, 1.0).hasNoteAbove())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasNoteAbove2() {
|
||||
assertTrue(Note(199, 1.0).hasNoteAbove())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasNoteAbove3() {
|
||||
assertFalse(Note(200, 1.0).hasNoteAbove())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasNoteBelow1() {
|
||||
assertTrue(Note(1, 1.0).hasNoteBelow())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasNoteBelow2() {
|
||||
assertTrue(Note(8, 1.0).hasNoteBelow())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHasNoteBelow3() {
|
||||
assertFalse(Note(0, 1.0).hasNoteBelow())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNoteAbove1() {
|
||||
assertEquals(Note(1, 1.0), Note(0, 1.0).noteAbove())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNoteAbove2() {
|
||||
assertEquals(Note(200, 1.0), Note(199, 1.0).noteAbove())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNoteBelow1() {
|
||||
assertEquals(Note(0, 1.0), Note(1, 1.0).noteBelow())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNoteBelow2() {
|
||||
assertEquals(Note(7, 1.0), Note(8, 1.0).noteBelow())
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
import kotlin.test.assertTrue
|
||||
*/
|
||||
|
||||
class Question2Tests {
|
||||
/*
|
||||
@Test
|
||||
fun testConstructor() {
|
||||
val tune: Tune = StandardTune()
|
||||
assertTrue(tune.notes.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddNote() {
|
||||
val tune: Tune = StandardTune()
|
||||
val note1 = Note(1, 4.0)
|
||||
val note2 = Note(2, 5.0)
|
||||
val note3 = Note(3, 6.0)
|
||||
val note4 = Note(4, 7.0)
|
||||
tune.addNote(note1)
|
||||
assertEquals(1, tune.notes.size)
|
||||
assertSame(note1, tune.notes[0])
|
||||
tune.addNote(note2)
|
||||
assertEquals(2, tune.notes.size)
|
||||
assertSame(note1, tune.notes[0])
|
||||
assertSame(note2, tune.notes[1])
|
||||
tune.addNote(note3)
|
||||
assertEquals(3, tune.notes.size)
|
||||
assertSame(note1, tune.notes[0])
|
||||
assertSame(note2, tune.notes[1])
|
||||
assertSame(note3, tune.notes[2])
|
||||
tune.addNote(note4)
|
||||
assertEquals(4, tune.notes.size)
|
||||
assertSame(note1, tune.notes[0])
|
||||
assertSame(note2, tune.notes[1])
|
||||
assertSame(note3, tune.notes[2])
|
||||
assertSame(note4, tune.notes[3])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetTotalDuration1() {
|
||||
assertEquals(0.0, StandardTune().totalDuration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetTotalDuration2() {
|
||||
val tune: Tune = StandardTune()
|
||||
tune.addNote(Note(10, 10.0))
|
||||
tune.addNote(Note(10, 2.0))
|
||||
tune.addNote(Note(10, 7.0))
|
||||
tune.addNote(Note(10, 15.0))
|
||||
assertEquals(34.0, tune.totalDuration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetTotalDuration3() {
|
||||
val tune: Tune = StandardTune()
|
||||
for (i in 1..29) {
|
||||
tune.addNote(Note(0, i.toDouble()))
|
||||
}
|
||||
assertEquals(435.0, tune.totalDuration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIteration() {
|
||||
val expectedNotes = listOf(
|
||||
Note(10, 10.0),
|
||||
Note(10, 2.0),
|
||||
Note(10, 7.0),
|
||||
Note(10, 15.0),
|
||||
)
|
||||
val actualNotes: MutableList<Note> = mutableListOf()
|
||||
val tune: Tune = StandardTune()
|
||||
tune.addNote(Note(10, 10.0))
|
||||
tune.addNote(Note(10, 2.0))
|
||||
tune.addNote(Note(10, 7.0))
|
||||
tune.addNote(Note(10, 15.0))
|
||||
for (note in tune) {
|
||||
actualNotes.add(note)
|
||||
}
|
||||
assertEquals(expectedNotes, actualNotes)
|
||||
assertEquals(expectedNotes, tune.notes)
|
||||
}
|
||||
*/
|
||||
}
|
||||
232
kotlin-tunes-skeleton/src/test/kotlin/tunes/Question3Tests.kt
Normal file
232
kotlin-tunes-skeleton/src/test/kotlin/tunes/Question3Tests.kt
Normal file
@@ -0,0 +1,232 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
*/
|
||||
|
||||
class Question3Tests {
|
||||
/*
|
||||
private fun simpleTuneNotes(): List<Note> {
|
||||
return listOf(
|
||||
Note(1, 2.0),
|
||||
Note(10, 4.0),
|
||||
Note(20, 8.0),
|
||||
Note(40, 12.0),
|
||||
)
|
||||
}
|
||||
|
||||
private fun longerTuneNotes(): List<Note> {
|
||||
val result: MutableList<Note> = mutableListOf(
|
||||
Note(1, 2.0),
|
||||
Note(10, 4.0),
|
||||
Note(20, 8.0),
|
||||
Note(40, 12.0),
|
||||
Note(50, 2.0),
|
||||
Note(60, 4.0),
|
||||
Note(70, 8.0),
|
||||
Note(80, 12.0),
|
||||
)
|
||||
return result
|
||||
}
|
||||
|
||||
private fun simpleTune(): Tune {
|
||||
val result: Tune = StandardTune()
|
||||
simpleTuneNotes().forEach({ result.addNote(it) })
|
||||
return result
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTransposeUp() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedUp: Tune = TransposedTune(originalTune, 2)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(3, 2.0),
|
||||
Note(12, 4.0),
|
||||
Note(22, 8.0),
|
||||
Note(42, 12.0),
|
||||
),
|
||||
transposedUp.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTransposeDown() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedDown: Tune = TransposedTune(originalTune, -1)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(0, 2.0),
|
||||
Note(9, 4.0),
|
||||
Note(19, 8.0),
|
||||
Note(39, 12.0),
|
||||
),
|
||||
transposedDown.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTransposeUpThenDown() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedUp: Tune = TransposedTune(originalTune, 1)
|
||||
val transposedDown: Tune = TransposedTune(transposedUp, -1)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(simpleTuneNotes(), transposedDown.notes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTransposeUpTooFar() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedUp: Tune = TransposedTune(originalTune, 190)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(191, 2.0),
|
||||
Note(200, 4.0),
|
||||
Note(200, 8.0),
|
||||
Note(200, 12.0),
|
||||
),
|
||||
transposedUp.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTransposeDownTooFar() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedDown: Tune = TransposedTune(originalTune, -20)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(0, 2.0),
|
||||
Note(0, 4.0),
|
||||
Note(0, 8.0),
|
||||
Note(20, 12.0),
|
||||
),
|
||||
transposedDown.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddNotesAllOk1() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedUp: Tune = TransposedTune(originalTune, 2)
|
||||
transposedUp.addNote(Note(52, 2.0))
|
||||
transposedUp.addNote(Note(62, 4.0))
|
||||
transposedUp.addNote(Note(72, 8.0))
|
||||
transposedUp.addNote(Note(82, 12.0))
|
||||
assertEquals(longerTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(3, 2.0),
|
||||
Note(12, 4.0),
|
||||
Note(22, 8.0),
|
||||
Note(42, 12.0),
|
||||
Note(52, 2.0),
|
||||
Note(62, 4.0),
|
||||
Note(72, 8.0),
|
||||
Note(82, 12.0),
|
||||
),
|
||||
transposedUp.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddNotesAllOk2() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedDown: Tune = TransposedTune(originalTune, -1)
|
||||
transposedDown.addNote(Note(49, 2.0))
|
||||
transposedDown.addNote(Note(59, 4.0))
|
||||
transposedDown.addNote(Note(69, 8.0))
|
||||
transposedDown.addNote(Note(79, 12.0))
|
||||
assertEquals(longerTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(0, 2.0),
|
||||
Note(9, 4.0),
|
||||
Note(19, 8.0),
|
||||
Note(39, 12.0),
|
||||
Note(49, 2.0),
|
||||
Note(59, 4.0),
|
||||
Note(69, 8.0),
|
||||
Note(79, 12.0),
|
||||
),
|
||||
transposedDown.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddNotesSomeTooHigh() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedDown: Tune = TransposedTune(originalTune, -10)
|
||||
transposedDown.addNote(Note(189, 2.0))
|
||||
transposedDown.addNote(Note(190, 4.0))
|
||||
transposedDown.addNote(Note(191, 8.0))
|
||||
transposedDown.addNote(Note(192, 12.0))
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 2.0),
|
||||
Note(10, 4.0),
|
||||
Note(20, 8.0),
|
||||
Note(40, 12.0),
|
||||
Note(199, 2.0),
|
||||
Note(200, 4.0),
|
||||
Note(200, 8.0),
|
||||
Note(200, 12.0),
|
||||
),
|
||||
originalTune.notes,
|
||||
)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(0, 2.0),
|
||||
Note(0, 4.0),
|
||||
Note(10, 8.0),
|
||||
Note(30, 12.0),
|
||||
Note(189, 2.0),
|
||||
Note(190, 4.0),
|
||||
Note(190, 8.0),
|
||||
Note(190, 12.0),
|
||||
),
|
||||
transposedDown.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddNotesSomeTooLow() {
|
||||
val originalTune = simpleTune()
|
||||
val transposedUp: Tune = TransposedTune(originalTune, 10)
|
||||
transposedUp.addNote(Note(11, 2.0))
|
||||
transposedUp.addNote(Note(10, 4.0))
|
||||
transposedUp.addNote(Note(9, 8.0))
|
||||
transposedUp.addNote(Note(8, 12.0))
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 2.0),
|
||||
Note(10, 4.0),
|
||||
Note(20, 8.0),
|
||||
Note(40, 12.0),
|
||||
Note(1, 2.0),
|
||||
Note(0, 4.0),
|
||||
Note(0, 8.0),
|
||||
Note(0, 12.0),
|
||||
),
|
||||
originalTune.notes,
|
||||
)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(11, 2.0),
|
||||
Note(20, 4.0),
|
||||
Note(30, 8.0),
|
||||
Note(50, 12.0),
|
||||
Note(11, 2.0),
|
||||
Note(10, 4.0),
|
||||
Note(10, 8.0),
|
||||
Note(10, 12.0),
|
||||
),
|
||||
transposedUp.notes,
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
204
kotlin-tunes-skeleton/src/test/kotlin/tunes/Question4Tests.kt
Normal file
204
kotlin-tunes-skeleton/src/test/kotlin/tunes/Question4Tests.kt
Normal file
@@ -0,0 +1,204 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
*/
|
||||
|
||||
class Question4Tests {
|
||||
/*
|
||||
private fun simpleTuneNotes(): List<Note> {
|
||||
return listOf(
|
||||
Note(1, 2.0),
|
||||
Note(10, 4.0),
|
||||
Note(20, 8.0),
|
||||
Note(40, 12.0),
|
||||
)
|
||||
}
|
||||
|
||||
private fun longerTuneNotes(): List<Note> {
|
||||
val result: MutableList<Note> = mutableListOf(
|
||||
Note(1, 2.0),
|
||||
Note(10, 4.0),
|
||||
Note(20, 8.0),
|
||||
Note(40, 12.0),
|
||||
Note(50, 2.0),
|
||||
Note(60, 4.0),
|
||||
Note(70, 8.0),
|
||||
Note(80, 12.0),
|
||||
)
|
||||
return result
|
||||
}
|
||||
|
||||
private fun simpleTune(): Tune {
|
||||
val result: Tune = StandardTune()
|
||||
simpleTuneNotes().forEach({ result.addNote(it) })
|
||||
return result
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStretchLonger() {
|
||||
val originalTune = simpleTune()
|
||||
val stretched: Tune = StretchedTune(originalTune, 2.0)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 4.0),
|
||||
Note(10, 8.0),
|
||||
Note(20, 16.0),
|
||||
Note(40, 24.0),
|
||||
),
|
||||
stretched.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStretchMuchLonger() {
|
||||
val originalTune = simpleTune()
|
||||
val stretched: Tune = StretchedTune(originalTune, 31.0)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 62.0),
|
||||
Note(10, 64.0),
|
||||
Note(20, 64.0),
|
||||
Note(40, 64.0),
|
||||
),
|
||||
stretched.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStretchShorter() {
|
||||
val originalTune = simpleTune()
|
||||
val stretched: Tune = StretchedTune(originalTune, 0.25)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 0.5),
|
||||
Note(10, 1.0),
|
||||
Note(20, 2.0),
|
||||
Note(40, 3.0),
|
||||
),
|
||||
stretched.notes,
|
||||
)
|
||||
assertEquals(6.5, stretched.totalDuration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStretchMuchShorter() {
|
||||
val originalTune = simpleTune()
|
||||
val stretched: Tune = StretchedTune(originalTune, 1.0 / 32.0)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 2.0 / 32.0),
|
||||
Note(10, 4.0 / 32.0),
|
||||
Note(20, 8.0 / 32.0),
|
||||
Note(40, 12.0 / 32.0),
|
||||
),
|
||||
stretched.notes,
|
||||
)
|
||||
assertEquals(26.0 / 32.0, stretched.totalDuration)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStretchAndShrink() {
|
||||
val originalTune = simpleTune()
|
||||
val stretched: Tune = StretchedTune(originalTune, 2.0)
|
||||
val shrunk: Tune = StretchedTune(stretched, 0.5)
|
||||
assertEquals(simpleTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
simpleTuneNotes(),
|
||||
shrunk.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddNotesAllOk1() {
|
||||
val originalTune = simpleTune()
|
||||
val stretched: Tune = StretchedTune(originalTune, 2.0)
|
||||
stretched.addNote(Note(50, 4.0))
|
||||
stretched.addNote(Note(60, 8.0))
|
||||
stretched.addNote(Note(70, 16.0))
|
||||
stretched.addNote(Note(80, 24.0))
|
||||
assertEquals(longerTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 4.0),
|
||||
Note(10, 8.0),
|
||||
Note(20, 16.0),
|
||||
Note(40, 24.0),
|
||||
Note(50, 4.0),
|
||||
Note(60, 8.0),
|
||||
Note(70, 16.0),
|
||||
Note(80, 24.0),
|
||||
),
|
||||
stretched.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddNotesAllOk2() {
|
||||
val originalTune = simpleTune()
|
||||
val stretched: Tune = StretchedTune(originalTune, 0.5)
|
||||
stretched.addNote(Note(50, 1.0))
|
||||
stretched.addNote(Note(60, 2.0))
|
||||
stretched.addNote(Note(70, 4.0))
|
||||
stretched.addNote(Note(80, 6.0))
|
||||
assertEquals(longerTuneNotes(), originalTune.notes)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 1.0),
|
||||
Note(10, 2.0),
|
||||
Note(20, 4.0),
|
||||
Note(40, 6.0),
|
||||
Note(50, 1.0),
|
||||
Note(60, 2.0),
|
||||
Note(70, 4.0),
|
||||
Note(80, 6.0),
|
||||
),
|
||||
stretched.notes,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddNotesSomeTooSlow() {
|
||||
val originalTune = simpleTune()
|
||||
val stretched: Tune = StretchedTune(originalTune, 0.25)
|
||||
stretched.addNote(Note(189, 2.0))
|
||||
stretched.addNote(Note(190, 8.0))
|
||||
stretched.addNote(Note(191, 16.0))
|
||||
stretched.addNote(Note(192, 32.0))
|
||||
stretched.addNote(Note(193, 64.0))
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 2.0),
|
||||
Note(10, 4.0),
|
||||
Note(20, 8.0),
|
||||
Note(40, 12.0),
|
||||
Note(189, 8.0),
|
||||
Note(190, 32.0),
|
||||
Note(191, 64.0),
|
||||
Note(192, 64.0),
|
||||
Note(193, 64.0),
|
||||
),
|
||||
originalTune.notes,
|
||||
)
|
||||
assertEquals(
|
||||
listOf(
|
||||
Note(1, 0.5),
|
||||
Note(10, 1.0),
|
||||
Note(20, 2.0),
|
||||
Note(40, 3.0),
|
||||
Note(189, 2.0),
|
||||
Note(190, 8.0),
|
||||
Note(191, 16.0),
|
||||
Note(192, 16.0),
|
||||
Note(193, 16.0),
|
||||
),
|
||||
stretched.notes,
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
*/
|
||||
|
||||
class Question5Tests {
|
||||
/*
|
||||
@Test
|
||||
fun concurrencyTest() {
|
||||
for (repeat in 1..20) {
|
||||
println("Repeat run $repeat")
|
||||
val composerNotes: List<MutableList<Note>> = (0..<8).map { mutableListOf() }
|
||||
val expectedNotes: MutableSet<Note> = mutableSetOf()
|
||||
for (i in 0..<20000) {
|
||||
(0..<8).forEach {
|
||||
val note = Note(i % 200, (i % 4 * it + 1).toDouble())
|
||||
composerNotes[it].add(note)
|
||||
expectedNotes.add(note)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: create a ThreadSafeTune
|
||||
|
||||
// TODO: create eight Composers using the composerNotes lists
|
||||
|
||||
// TODO: create eight Threads, one per Composer
|
||||
|
||||
// TODO: start the threads
|
||||
|
||||
// TODO: join the threads
|
||||
|
||||
assertEquals(expectedNotes, tune.notes.toSet())
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
126
kotlin-tunes-skeleton/src/test/kotlin/tunes/Question6Tests.kt
Normal file
126
kotlin-tunes-skeleton/src/test/kotlin/tunes/Question6Tests.kt
Normal file
@@ -0,0 +1,126 @@
|
||||
package tunes
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
import kotlin.test.fail
|
||||
*/
|
||||
|
||||
class Question6Tests {
|
||||
/*
|
||||
private val tune1: Tune = StandardTune()
|
||||
private val tune2: Tune = StandardTune()
|
||||
private val tune3: Tune = StandardTune()
|
||||
private val tune4: Tune = StandardTune()
|
||||
private val tune5: Tune = StandardTune()
|
||||
private val tune6: Tune = StandardTune()
|
||||
private val tune7: Tune = StandardTune()
|
||||
private val tune8: Tune = StandardTune()
|
||||
private val tune9: Tune = StandardTune()
|
||||
private val tune10: Tune = StandardTune()
|
||||
|
||||
init {
|
||||
tune1.addNote(Note(12, 4.0))
|
||||
tune2.addNote(Note(6, 8.0))
|
||||
tune3.addNote(Note(18, 4.0))
|
||||
tune3.addNote(Note(19, 4.0))
|
||||
tune4.addNote(Note(180, 0.25))
|
||||
tune4.addNote(Note(170, 0.25))
|
||||
tune4.addNote(Note(160, 0.25))
|
||||
tune5.addNote(Note(16, 0.25))
|
||||
tune5.addNote(Note(60, 0.25))
|
||||
tune5.addNote(Note(163, 0.5))
|
||||
tune7.addNote(Note(1, 1.0))
|
||||
tune7.addNote(Note(1, 1.0))
|
||||
tune7.addNote(Note(1, 1.0))
|
||||
tune7.addNote(Note(1, 1.0))
|
||||
tune7.addNote(Note(1, 1.0))
|
||||
tune8.addNote(Note(12, 10.0))
|
||||
tune8.addNote(Note(14, 1.0))
|
||||
tune9.addNote(Note(140, 1.0))
|
||||
tune9.addNote(Note(140, 1.0))
|
||||
tune9.addNote(Note(141, 1.0))
|
||||
tune9.addNote(Note(142, 1.0))
|
||||
tune10.addNote(Note(1, 1.0))
|
||||
tune10.addNote(Note(1, 1.0))
|
||||
tune10.addNote(Note(1, 1.0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEmptySongCollection() {
|
||||
val songCollection = SongCollection()
|
||||
assertEquals(emptyList(), songCollection.getSongNames())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSmallCollection() {
|
||||
val songCollection = SongCollection()
|
||||
songCollection.addSong("Carol", tune1)
|
||||
songCollection.addSong("Anthem", tune2)
|
||||
songCollection.addSong("Ballad", tune3)
|
||||
assertEquals(listOf("Anthem", "Ballad", "Carol"), songCollection.getSongNames())
|
||||
assertSame(tune1, songCollection.getTune("Carol"))
|
||||
assertSame(tune2, songCollection.getTune("Anthem"))
|
||||
assertSame(tune3, songCollection.getTune("Ballad"))
|
||||
try {
|
||||
songCollection.getTune("Rocker")
|
||||
fail("Expected NoSuchElementException")
|
||||
} catch (exception: NoSuchElementException) {
|
||||
// Good; an exception was expected.
|
||||
}
|
||||
try {
|
||||
songCollection.addSong("Carol", tune4)
|
||||
fail("Expected UnsupportedOperationException")
|
||||
} catch (exception: UnsupportedOperationException) {
|
||||
// Good; an exception was expected.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLargeCollection() {
|
||||
val songCollection = SongCollection()
|
||||
songCollection.addSong("Killer Queen", tune1)
|
||||
songCollection.addSong("Another One Bites The Dust", tune2)
|
||||
songCollection.addSong("Innuendo", tune3)
|
||||
songCollection.addSong("I Want It All", tune4)
|
||||
assertSame(tune1, songCollection.getTune("Killer Queen"))
|
||||
assertSame(tune2, songCollection.getTune("Another One Bites The Dust"))
|
||||
assertSame(tune3, songCollection.getTune("Innuendo"))
|
||||
assertSame(tune4, songCollection.getTune("I Want It All"))
|
||||
assertEquals(listOf("Another One Bites The Dust", "I Want It All", "Innuendo", "Killer Queen"), songCollection.getSongNames())
|
||||
|
||||
songCollection.addSong("Headlong", tune5)
|
||||
songCollection.addSong("Don't Try So Hard", tune6)
|
||||
songCollection.addSong("Tenement Funster", tune7)
|
||||
songCollection.addSong("It's A Kind Of Magic", tune8)
|
||||
songCollection.addSong("Breakthru", tune9)
|
||||
songCollection.addSong("Who Wants To Live Forever?", tune10)
|
||||
assertSame(tune1, songCollection.getTune("Killer Queen"))
|
||||
assertSame(tune2, songCollection.getTune("Another One Bites The Dust"))
|
||||
assertSame(tune3, songCollection.getTune("Innuendo"))
|
||||
assertSame(tune4, songCollection.getTune("I Want It All"))
|
||||
assertSame(tune5, songCollection.getTune("Headlong"))
|
||||
assertSame(tune6, songCollection.getTune("Don't Try So Hard"))
|
||||
assertSame(tune7, songCollection.getTune("Tenement Funster"))
|
||||
assertSame(tune8, songCollection.getTune("It's A Kind Of Magic"))
|
||||
assertSame(tune9, songCollection.getTune("Breakthru"))
|
||||
assertSame(tune10, songCollection.getTune("Who Wants To Live Forever?"))
|
||||
assertEquals(
|
||||
listOf(
|
||||
"Another One Bites The Dust",
|
||||
"Breakthru",
|
||||
"Don't Try So Hard",
|
||||
"Headlong",
|
||||
"I Want It All",
|
||||
"Innuendo",
|
||||
"It's A Kind Of Magic",
|
||||
"Killer Queen",
|
||||
"Tenement Funster",
|
||||
"Who Wants To Live Forever?",
|
||||
),
|
||||
songCollection.getSongNames(),
|
||||
)
|
||||
}
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user