This commit is contained in:
2026-03-13 12:11:24 +00:00
commit fb57e2e831
763 changed files with 12954 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
ext.kotlin_version = "1.9.0"
ext.jvm_version = 17
ext.kotlinter_version = "3.15.0"
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'application'
id 'org.jetbrains.kotlin.jvm' version "$kotlin_version"
id 'org.jmailen.kotlinter' version "$kotlinter_version"
}
group = 'uk.ac.ic.doc'
version = '1.0.0'
description = """Kotlin Tunes"""
tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = "$jvm_version"
}
}
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(jvm_version))
}
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
test {
testLogging {
events "PASSED", "FAILED", "SKIPPED"
}
}

Binary file not shown.

View File

@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

248
kotlin-tunes-skeleton/gradlew vendored Normal file
View File

@@ -0,0 +1,248 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

92
kotlin-tunes-skeleton/gradlew.bat vendored Normal file
View File

@@ -0,0 +1,92 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@@ -0,0 +1,4 @@
package tunes;
class StretchedTune {
}

View File

@@ -0,0 +1,3 @@
package tunes
class Note

View File

@@ -0,0 +1,23 @@
package tunes
class SongCollection {
/*
private class Song(val name: String, val tune: Tune)
private class TreeNode(var song: Song, var left: TreeNode? = null, var right: TreeNode? = null)
private var root: TreeNode? = null
fun addSong(name: String, tune: Tune) {
TODO("Implement as part of Question 6")
}
fun getTune(name: String): Tune {
TODO("Implement as part of Question 6")
}
fun getSongNames(): List<String> {
TODO("Implement as part of Question 6")
}
*/
}

View File

@@ -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)
}

View File

@@ -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)
}
}
*/
}

View File

@@ -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",
)
}
*/
}

View File

@@ -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",
)
}
*/
}

View File

@@ -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",
)
}
*/
}

View File

@@ -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",
)
}
*/
}

View File

@@ -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",
)
}
*/
}

View 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())
}
*/
}

View File

@@ -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)
}
*/
}

View 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,
)
}
*/
}

View 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,
)
}
*/
}

View File

@@ -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())
}
}
*/
}

View 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(),
)
}
*/
}