Initial.
This commit is contained in:
50
kotlin-textfiles-skeleton/build.gradle
Normal file
50
kotlin-textfiles-skeleton/build.gradle
Normal 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 Text Files"""
|
||||
|
||||
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"
|
||||
}
|
||||
}
|
||||
BIN
kotlin-textfiles-skeleton/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
kotlin-textfiles-skeleton/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
7
kotlin-textfiles-skeleton/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
7
kotlin-textfiles-skeleton/gradle/wrapper/gradle-wrapper.properties
vendored
Normal 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-textfiles-skeleton/gradlew
vendored
Normal file
248
kotlin-textfiles-skeleton/gradlew
vendored
Normal 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-textfiles-skeleton/gradlew.bat
vendored
Normal file
92
kotlin-textfiles-skeleton/gradlew.bat
vendored
Normal 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
|
||||
@@ -0,0 +1,4 @@
|
||||
package textfiles;
|
||||
|
||||
class LazyTextFile {
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package textfiles
|
||||
|
||||
class FileIndexOutOfBoundsException : IndexOutOfBoundsException()
|
||||
@@ -0,0 +1,86 @@
|
||||
package textfiles
|
||||
|
||||
private const val BLOCK_SIZE: Int = 8
|
||||
|
||||
class MultiStringTextFile(initialContents: String) : TextFile {
|
||||
|
||||
private var blocks: MutableList<StringBuilder> = mutableListOf(StringBuilder(initialContents))
|
||||
|
||||
/**
|
||||
* Yields the number of blocks that are currently used to represent the file.
|
||||
*
|
||||
* @return The number of blocks used to represent this file.
|
||||
*/
|
||||
val numBlocks: Int
|
||||
get() = blocks.size
|
||||
|
||||
override val length: Int
|
||||
get() = TODO("To be implemented.")
|
||||
|
||||
init {
|
||||
rebalance()
|
||||
}
|
||||
|
||||
/**
|
||||
* Reorganises the internal representation of the text file so that the content is partitioned
|
||||
* into blocks of equal size, except that the final block may be shorter.
|
||||
*/
|
||||
fun rebalance() {
|
||||
TODO("To be implemented.")
|
||||
}
|
||||
|
||||
override fun insertText(offset: Int, toInsert: String) {
|
||||
TODO("To be implemented.")
|
||||
}
|
||||
|
||||
override fun deleteText(offset: Int, size: Int) {
|
||||
if (offset < 0 || size < 0) {
|
||||
throw FileIndexOutOfBoundsException()
|
||||
}
|
||||
val newBlocks: MutableList<StringBuilder> = mutableListOf()
|
||||
var index = 0
|
||||
var block = 0
|
||||
while (block < blocks.size) {
|
||||
if (index < offset + size && offset < index + blocks[block].length) {
|
||||
if (size == 0) {
|
||||
return
|
||||
}
|
||||
// Add the prefix
|
||||
val prefixSize: Int = offset - index
|
||||
if (prefixSize > 0) {
|
||||
newBlocks.add(StringBuilder(blocks[block].substring(0, prefixSize)))
|
||||
}
|
||||
index += prefixSize
|
||||
// If the suffix is in the same block, add it. Otherwise, skip over blocks.
|
||||
if (prefixSize + size < blocks[block].length) {
|
||||
newBlocks.add(StringBuilder(blocks[block].substring(prefixSize + size)))
|
||||
} else {
|
||||
var deleted: Int = blocks[block].length - prefixSize
|
||||
block++
|
||||
while (block < blocks.size && size - deleted >= blocks[block].length) {
|
||||
deleted += blocks[block].length
|
||||
block++
|
||||
}
|
||||
if (deleted < size) {
|
||||
if (block >= blocks.size) {
|
||||
throw FileIndexOutOfBoundsException()
|
||||
}
|
||||
assert(blocks[block].length > size - deleted)
|
||||
newBlocks.add(StringBuilder(blocks[block].substring(size - deleted)))
|
||||
}
|
||||
}
|
||||
index += size
|
||||
} else {
|
||||
newBlocks.add(blocks[block])
|
||||
index += blocks[block].length
|
||||
}
|
||||
block++
|
||||
}
|
||||
if (offset >= index) {
|
||||
if (offset > index || size > 0) {
|
||||
throw FileIndexOutOfBoundsException()
|
||||
}
|
||||
}
|
||||
blocks = newBlocks
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package textfiles
|
||||
|
||||
interface TextFile {
|
||||
/**
|
||||
* Provides the length of the text file.
|
||||
*
|
||||
* @return the number of characters in the text file
|
||||
*/
|
||||
val length: Int
|
||||
|
||||
/**
|
||||
* Inserts text into the file.
|
||||
*
|
||||
* @param offset The offset at which the text should be inserted
|
||||
* @param toInsert The text to be inserted
|
||||
* @throws FileIndexOutOfBoundsException if offset is negative or larger
|
||||
* than the length of the file
|
||||
*/
|
||||
fun insertText(offset: Int, toInsert: String)
|
||||
|
||||
/**
|
||||
* Deletes text from the file.
|
||||
*
|
||||
* @param offset The offset at which the deletion should occur
|
||||
* @param size The number of characters to be deleted
|
||||
* @throws FileIndexOutOfBoundsException if offset or size is negative, or
|
||||
* if offset + size is larger than the length of the file
|
||||
*/
|
||||
fun deleteText(offset: Int, size: Int)
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package textfiles
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.fail
|
||||
*/
|
||||
|
||||
class Question1Tests {
|
||||
/*
|
||||
@Test
|
||||
fun testLength1() {
|
||||
val textFile: TextFile = SingleStringTextFile("")
|
||||
assertEquals(0, textFile.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLength2() {
|
||||
val textFile: TextFile = SingleStringTextFile("Hello\nWorld")
|
||||
assertEquals(11, textFile.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert1() {
|
||||
val textFile: TextFile = SingleStringTextFile("")
|
||||
textFile.insertText(0, "First")
|
||||
textFile.insertText(0, "Second")
|
||||
textFile.insertText(0, "Third")
|
||||
assertEquals("ThirdSecondFirst", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert2() {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.insertText(11, "MoreContent")
|
||||
textFile.insertText(22, "EvenMoreContent")
|
||||
assertEquals("SomeContentMoreContentEvenMoreContent", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert3() {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.insertText(0, "!")
|
||||
textFile.insertText(2, "!")
|
||||
textFile.insertText(4, "!")
|
||||
textFile.insertText(6, "!")
|
||||
textFile.insertText(8, "!")
|
||||
textFile.insertText(10, "!")
|
||||
textFile.insertText(12, "!")
|
||||
textFile.insertText(14, "!")
|
||||
textFile.insertText(16, "!")
|
||||
textFile.insertText(18, "!")
|
||||
textFile.insertText(20, "!")
|
||||
textFile.insertText(22, "!")
|
||||
assertEquals("!S!o!m!e!C!o!n!t!e!n!t!", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException1() {
|
||||
try {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.insertText(-1, "DoNotAddMe")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException2() {
|
||||
try {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.insertText(12, "DoNotAddMe")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException3() {
|
||||
try {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.insertText(12, "")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete1() {
|
||||
val textFile: TextFile = SingleStringTextFile("")
|
||||
textFile.deleteText(0, 0)
|
||||
assertEquals("", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete2() {
|
||||
val textFile: TextFile = SingleStringTextFile("Hello")
|
||||
textFile.deleteText(3, 0)
|
||||
assertEquals("Hello", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete3() {
|
||||
val textFile: TextFile = SingleStringTextFile("Example text file")
|
||||
textFile.deleteText(0, 17)
|
||||
assertEquals("", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete4() {
|
||||
val textFile: TextFile = SingleStringTextFile("Example text file")
|
||||
textFile.deleteText(8, 5)
|
||||
assertEquals("Example file", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete5() {
|
||||
val textFile: TextFile = SingleStringTextFile("T")
|
||||
textFile.deleteText(1, 0)
|
||||
assertEquals("T", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException1() {
|
||||
try {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.deleteText(-1, 3)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException2() {
|
||||
try {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.deleteText(0, 12)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException3() {
|
||||
try {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.deleteText(10, 2)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException4() {
|
||||
try {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.deleteText(100, 0)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException5() {
|
||||
try {
|
||||
val textFile: TextFile = SingleStringTextFile("SomeContent")
|
||||
textFile.deleteText(0, -1)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
package textfiles
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.fail
|
||||
*/
|
||||
|
||||
class Question2Tests {
|
||||
/*
|
||||
@Test
|
||||
fun testLength1() {
|
||||
val textFile: TextFile = MultiStringTextFile("")
|
||||
assertEquals(0, textFile.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLength2() {
|
||||
val textFile: TextFile = MultiStringTextFile("Hello\nWorld")
|
||||
assertEquals(11, textFile.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLength3() {
|
||||
val textFile: TextFile = MultiStringTextFile(
|
||||
"The quick brown fox jumped over the lazy dog ten times today",
|
||||
)
|
||||
assertEquals(60, textFile.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert1() {
|
||||
val textFile: TextFile = MultiStringTextFile("")
|
||||
textFile.insertText(0, "First")
|
||||
textFile.insertText(0, "Second")
|
||||
textFile.insertText(0, "Third")
|
||||
assertEquals("ThirdSecondFirst", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert2() {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.insertText(11, "MoreContent")
|
||||
textFile.insertText(22, "EvenMoreContent")
|
||||
assertEquals("SomeContentMoreContentEvenMoreContent", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert3() {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.insertText(0, "!")
|
||||
textFile.insertText(2, "!")
|
||||
textFile.insertText(4, "!")
|
||||
textFile.insertText(6, "!")
|
||||
textFile.insertText(8, "!")
|
||||
textFile.insertText(10, "!")
|
||||
textFile.insertText(12, "!")
|
||||
textFile.insertText(14, "!")
|
||||
textFile.insertText(16, "!")
|
||||
textFile.insertText(18, "!")
|
||||
textFile.insertText(20, "!")
|
||||
textFile.insertText(22, "!")
|
||||
assertEquals("!S!o!m!e!C!o!n!t!e!n!t!", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException1() {
|
||||
try {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.insertText(-1, "DoNotAddMe")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException2() {
|
||||
try {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.insertText(12, "DoNotAddMe")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException3() {
|
||||
try {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.insertText(12, "")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete1() {
|
||||
val textFile: TextFile = MultiStringTextFile("")
|
||||
textFile.deleteText(0, 0)
|
||||
assertEquals("", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete2() {
|
||||
val textFile: TextFile = MultiStringTextFile("Hello")
|
||||
textFile.deleteText(3, 0)
|
||||
assertEquals("Hello", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete3() {
|
||||
val textFile: TextFile = MultiStringTextFile("Example text file")
|
||||
textFile.deleteText(0, 17)
|
||||
assertEquals("", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete4() {
|
||||
val textFile: TextFile = MultiStringTextFile("Example text file")
|
||||
textFile.deleteText(8, 5)
|
||||
assertEquals("Example file", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete5() {
|
||||
val textFile: TextFile = MultiStringTextFile("T")
|
||||
textFile.deleteText(1, 0)
|
||||
assertEquals("T", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete6() {
|
||||
val textFile: TextFile = MultiStringTextFile(
|
||||
"The quick brown fox jumped over the lazy dog ten times today",
|
||||
)
|
||||
textFile.deleteText(3, 43)
|
||||
assertEquals("Theen times today", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException1() {
|
||||
try {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.deleteText(-1, 3)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException2() {
|
||||
try {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.deleteText(0, 12)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException3() {
|
||||
try {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.deleteText(10, 2)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException4() {
|
||||
try {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.deleteText(100, 0)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException5() {
|
||||
try {
|
||||
val textFile: TextFile = MultiStringTextFile("SomeContent")
|
||||
textFile.deleteText(0, -1)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetNumBlocksAndRebalance1() {
|
||||
val textFile = MultiStringTextFile("")
|
||||
assertEquals(0, textFile.numBlocks)
|
||||
textFile.rebalance()
|
||||
assertEquals(0, textFile.numBlocks)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetNumBlocksAndRebalance2() {
|
||||
val textFile = MultiStringTextFile(
|
||||
"AKAI Professional MPK249 midi controller does not act as a stand-alone synthesizer, unfortunately.",
|
||||
)
|
||||
assertEquals(13, textFile.numBlocks)
|
||||
textFile.rebalance()
|
||||
assertEquals(13, textFile.numBlocks)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetNumBlocksAndRebalance3() {
|
||||
val textFile = MultiStringTextFile("Harmonious")
|
||||
assertEquals(2, textFile.numBlocks)
|
||||
textFile.rebalance()
|
||||
assertEquals(2, textFile.numBlocks)
|
||||
textFile.insertText(2, "A")
|
||||
assertEquals(2, textFile.numBlocks)
|
||||
textFile.insertText(11, "B")
|
||||
assertEquals(3, textFile.numBlocks)
|
||||
textFile.insertText(12, "C")
|
||||
assertEquals(4, textFile.numBlocks)
|
||||
textFile.rebalance()
|
||||
assertEquals(2, textFile.numBlocks)
|
||||
assertEquals("HaArmoniousBC", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetNumBlocksAndRebalance4() {
|
||||
val textFile = MultiStringTextFile("Harmonious")
|
||||
assertEquals(2, textFile.numBlocks)
|
||||
textFile.rebalance()
|
||||
assertEquals(2, textFile.numBlocks)
|
||||
textFile.deleteText(2, 1)
|
||||
assertEquals(3, textFile.numBlocks)
|
||||
textFile.deleteText(7, 2)
|
||||
assertEquals(2, textFile.numBlocks)
|
||||
assertEquals("Hamonio", textFile.toString())
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,295 @@
|
||||
package textfiles
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.fail
|
||||
*/
|
||||
|
||||
class Question3Tests {
|
||||
|
||||
/*
|
||||
@Test
|
||||
fun testLength1() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile(""))
|
||||
assertEquals(0, textFile.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLength2() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("Hello\nWorld"))
|
||||
assertEquals(11, textFile.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLength3() {
|
||||
val textFile: TextFile = LazyTextFile(
|
||||
SingleStringTextFile(
|
||||
"The quick brown fox jumped over the lazy dog ten times today",
|
||||
),
|
||||
)
|
||||
assertEquals(60, textFile.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert1() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile(""))
|
||||
textFile.insertText(0, "First")
|
||||
textFile.insertText(0, "Second")
|
||||
textFile.insertText(0, "Third")
|
||||
assertEquals("ThirdSecondFirst", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert2() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.insertText(11, "MoreContent")
|
||||
textFile.insertText(22, "EvenMoreContent")
|
||||
assertEquals("SomeContentMoreContentEvenMoreContent", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsert3() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.insertText(0, "!")
|
||||
textFile.insertText(2, "!")
|
||||
textFile.insertText(4, "!")
|
||||
textFile.insertText(6, "!")
|
||||
textFile.insertText(8, "!")
|
||||
textFile.insertText(10, "!")
|
||||
textFile.insertText(12, "!")
|
||||
textFile.insertText(14, "!")
|
||||
textFile.insertText(16, "!")
|
||||
textFile.insertText(18, "!")
|
||||
textFile.insertText(20, "!")
|
||||
textFile.insertText(22, "!")
|
||||
assertEquals("!S!o!m!e!C!o!n!t!e!n!t!", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException1() {
|
||||
try {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.insertText(-1, "DoNotAddMe")
|
||||
textFile.insertText(0, "a")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException2() {
|
||||
try {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.insertText(12, "DoNotAddMe")
|
||||
textFile.insertText(0, "a")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertException3() {
|
||||
try {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.insertText(12, "")
|
||||
textFile.insertText(0, "a")
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete1() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile(""))
|
||||
textFile.deleteText(0, 0)
|
||||
assertEquals("", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete2() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("Hello"))
|
||||
textFile.deleteText(3, 0)
|
||||
assertEquals("Hello", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete3() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("Example text file"))
|
||||
textFile.deleteText(0, 17)
|
||||
assertEquals("", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete4() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("Example text file"))
|
||||
textFile.deleteText(8, 5)
|
||||
assertEquals("Example file", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete5() {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("T"))
|
||||
textFile.deleteText(1, 0)
|
||||
assertEquals("T", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete6() {
|
||||
val textFile: TextFile = LazyTextFile(
|
||||
SingleStringTextFile(
|
||||
"The quick brown fox jumped over the lazy dog ten times today",
|
||||
),
|
||||
)
|
||||
textFile.deleteText(3, 43)
|
||||
assertEquals("Theen times today", textFile.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException1() {
|
||||
try {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.deleteText(-1, 3)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException2() {
|
||||
try {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.deleteText(0, 12)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException3() {
|
||||
try {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.deleteText(10, 2)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException4() {
|
||||
try {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.deleteText(100, 0)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteException5() {
|
||||
try {
|
||||
val textFile: TextFile = LazyTextFile(SingleStringTextFile("SomeContent"))
|
||||
textFile.deleteText(0, -1)
|
||||
} catch (exception: FileIndexOutOfBoundsException) {
|
||||
// Good: an exception should have been thrown.
|
||||
return
|
||||
}
|
||||
fail()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLaziness() {
|
||||
val operationCountingTextFile = OperationCountingTextFile()
|
||||
val lazyTextFile: TextFile = LazyTextFile(operationCountingTextFile)
|
||||
assertEquals(0, operationCountingTextFile.numInsertions)
|
||||
assertEquals(0, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(0, "hello")
|
||||
assertEquals(0, operationCountingTextFile.numInsertions)
|
||||
assertEquals(0, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(0, "hello")
|
||||
assertEquals(0, operationCountingTextFile.numInsertions)
|
||||
assertEquals(0, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(0, "hello")
|
||||
assertEquals(0, operationCountingTextFile.numInsertions)
|
||||
assertEquals(0, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.length
|
||||
assertEquals(1, operationCountingTextFile.numInsertions)
|
||||
assertEquals(0, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(0, "hello")
|
||||
assertEquals(1, operationCountingTextFile.numInsertions)
|
||||
assertEquals(0, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.deleteText(5, 2)
|
||||
assertEquals(2, operationCountingTextFile.numInsertions)
|
||||
assertEquals(1, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(0, "hello")
|
||||
assertEquals(2, operationCountingTextFile.numInsertions)
|
||||
assertEquals(1, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(0, "hello")
|
||||
assertEquals(2, operationCountingTextFile.numInsertions)
|
||||
assertEquals(1, operationCountingTextFile.numDeletions)
|
||||
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
lazyTextFile.toString()
|
||||
assertEquals(3, operationCountingTextFile.numInsertions)
|
||||
assertEquals(1, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(0, "hello")
|
||||
assertEquals(3, operationCountingTextFile.numInsertions)
|
||||
assertEquals(1, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(1, "hello")
|
||||
assertEquals(4, operationCountingTextFile.numInsertions)
|
||||
assertEquals(1, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.insertText(1, "hello")
|
||||
assertEquals(4, operationCountingTextFile.numInsertions)
|
||||
assertEquals(1, operationCountingTextFile.numDeletions)
|
||||
|
||||
lazyTextFile.deleteText(1, 6)
|
||||
assertEquals(5, operationCountingTextFile.numInsertions)
|
||||
assertEquals(2, operationCountingTextFile.numDeletions)
|
||||
}
|
||||
|
||||
class OperationCountingTextFile : TextFile {
|
||||
|
||||
var numInsertions = 0
|
||||
private set
|
||||
|
||||
var numDeletions = 0
|
||||
private set
|
||||
|
||||
override val length: Int
|
||||
get() = 1000
|
||||
|
||||
override fun insertText(offset: Int, toInsert: String) {
|
||||
numInsertions++
|
||||
}
|
||||
|
||||
override fun deleteText(offset: Int, size: Int) {
|
||||
numDeletions++
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package textfiles
|
||||
|
||||
/*
|
||||
import java.util.Random
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
*/
|
||||
|
||||
class Question4Tests {
|
||||
/*
|
||||
@Test
|
||||
fun concurrencyTest() {
|
||||
for (repeat in 1..20) {
|
||||
println("Repeat run $repeat")
|
||||
val initialText = "initialtext"
|
||||
val authorStrings: List<List<String>> = (0..<8).map {
|
||||
(0..<1000).map {
|
||||
(0..it % 10).map { number -> number.toString() }.joinToString(separator = "")
|
||||
}
|
||||
}
|
||||
val expectedOutput: String =
|
||||
(initialText + authorStrings.flatten().joinToString(separator = ""))
|
||||
.toCharArray().sortedArray().joinToString(separator = "")
|
||||
|
||||
val singleStringTextFile = SingleStringTextFile(initialText)
|
||||
|
||||
// TODO - complete this test
|
||||
|
||||
assertEquals(
|
||||
expectedOutput,
|
||||
threadSafeTextFile.toString().toCharArray().sortedArray().joinToString(separator = ""),
|
||||
)
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package textfiles
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
*/
|
||||
|
||||
class Question5Tests {
|
||||
/*
|
||||
@Test
|
||||
fun testCompareTo1() {
|
||||
val t1: TextFile = SingleStringTextFile("Hello")
|
||||
val t2: TextFile = SingleStringTextFile("Hello")
|
||||
assertEquals(0, t1.compareTo(t2))
|
||||
assertEquals(0, t2.compareTo(t1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompareTo2() {
|
||||
val t1: TextFile = SingleStringTextFile("Hell")
|
||||
val t2: TextFile = SingleStringTextFile("Hello")
|
||||
assertTrue(t1 < t2)
|
||||
assertTrue(t2 > t1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompareTo3() {
|
||||
val t1: TextFile = MultiStringTextFile("Hello")
|
||||
val t2: TextFile = SingleStringTextFile("Hello")
|
||||
assertEquals(0, t1.compareTo(t2))
|
||||
assertEquals(0, t2.compareTo(t1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompareTo4() {
|
||||
val t1: TextFile = MultiStringTextFile("Aello")
|
||||
val t2: TextFile = SingleStringTextFile("Hello")
|
||||
assertTrue(t1 < t2)
|
||||
assertTrue(t2 > t1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompareTo5() {
|
||||
val t1: TextFile = MultiStringTextFile("Hello")
|
||||
val t2: TextFile = MultiStringTextFile("Hello")
|
||||
assertEquals(0, t1.compareTo(t2))
|
||||
assertEquals(0, t2.compareTo(t1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompareTo6() {
|
||||
val t1: TextFile = MultiStringTextFile("Hella")
|
||||
val t2: TextFile = MultiStringTextFile("Hello")
|
||||
assertTrue(t1 < t2)
|
||||
assertTrue(t2 > t1)
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
package textfiles
|
||||
|
||||
/*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertSame
|
||||
*/
|
||||
|
||||
class Question6Tests {
|
||||
/*
|
||||
private val file1: TextFile = SingleStringTextFile("File1Contents")
|
||||
private val file2: TextFile = SingleStringTextFile("File2Contents")
|
||||
private val file3: TextFile = SingleStringTextFile("File3Contents")
|
||||
private val file4: TextFile = SingleStringTextFile("File4Contents")
|
||||
private val file5: TextFile = SingleStringTextFile("File5Contents")
|
||||
|
||||
@Test
|
||||
fun testGetSetAndSize() {
|
||||
val fileMap = FileMap()
|
||||
assertEquals(0, fileMap.size)
|
||||
assertNull(fileMap.get("a"))
|
||||
assertNull(fileMap.get("b"))
|
||||
assertNull(fileMap.get("c"))
|
||||
assertNull(fileMap.get("d"))
|
||||
assertNull(fileMap.get("e"))
|
||||
|
||||
fileMap.set("a", file1)
|
||||
assertEquals(1, fileMap.size)
|
||||
assertSame(file1, fileMap.get("a"))
|
||||
assertNull(fileMap.get("b"))
|
||||
assertNull(fileMap.get("c"))
|
||||
assertNull(fileMap.get("d"))
|
||||
assertNull(fileMap.get("e"))
|
||||
|
||||
fileMap.set("b", file2)
|
||||
assertEquals(2, fileMap.size)
|
||||
assertSame(file1, fileMap.get("a"))
|
||||
assertSame(file2, fileMap.get("b"))
|
||||
assertNull(fileMap.get("c"))
|
||||
assertNull(fileMap.get("d"))
|
||||
assertNull(fileMap.get("e"))
|
||||
|
||||
fileMap.set("c", file3)
|
||||
assertEquals(3, fileMap.size)
|
||||
assertSame(file1, fileMap.get("a"))
|
||||
assertSame(file2, fileMap.get("b"))
|
||||
assertSame(file3, fileMap.get("c"))
|
||||
assertNull(fileMap.get("d"))
|
||||
assertNull(fileMap.get("e"))
|
||||
|
||||
fileMap.set("d", file4)
|
||||
assertEquals(4, fileMap.size)
|
||||
assertSame(file1, fileMap.get("a"))
|
||||
assertSame(file2, fileMap.get("b"))
|
||||
assertSame(file3, fileMap.get("c"))
|
||||
assertSame(file4, fileMap.get("d"))
|
||||
assertNull(fileMap.get("e"))
|
||||
|
||||
fileMap.set("e", file5)
|
||||
assertEquals(5, fileMap.size)
|
||||
assertSame(file1, fileMap.get("a"))
|
||||
assertSame(file2, fileMap.get("b"))
|
||||
assertSame(file3, fileMap.get("c"))
|
||||
assertSame(file4, fileMap.get("d"))
|
||||
assertSame(file5, fileMap.get("e"))
|
||||
|
||||
fileMap.set("a", file2)
|
||||
fileMap.set("b", file3)
|
||||
fileMap.set("c", file4)
|
||||
fileMap.set("d", file5)
|
||||
fileMap.set("e", file1)
|
||||
|
||||
assertSame(file2, fileMap.get("a"))
|
||||
assertSame(file3, fileMap.get("b"))
|
||||
assertSame(file4, fileMap.get("c"))
|
||||
assertSame(file5, fileMap.get("d"))
|
||||
assertSame(file1, fileMap.get("e"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetSetAndSizeViaOperator() {
|
||||
val fileMap = FileMap()
|
||||
assertEquals(0, fileMap.size)
|
||||
assertNull(fileMap["a"])
|
||||
assertNull(fileMap["b"])
|
||||
assertNull(fileMap["c"])
|
||||
assertNull(fileMap["d"])
|
||||
assertNull(fileMap["e"])
|
||||
|
||||
fileMap["a"] = file1
|
||||
assertEquals(1, fileMap.size)
|
||||
assertSame(file1, fileMap["a"])
|
||||
assertNull(fileMap["b"])
|
||||
assertNull(fileMap["c"])
|
||||
assertNull(fileMap["d"])
|
||||
assertNull(fileMap["e"])
|
||||
|
||||
fileMap["b"] = file2
|
||||
assertEquals(2, fileMap.size)
|
||||
assertSame(file1, fileMap["a"])
|
||||
assertSame(file2, fileMap["b"])
|
||||
assertNull(fileMap["c"])
|
||||
assertNull(fileMap["d"])
|
||||
assertNull(fileMap["e"])
|
||||
|
||||
fileMap["c"] = file3
|
||||
assertEquals(3, fileMap.size)
|
||||
assertSame(file1, fileMap["a"])
|
||||
assertSame(file2, fileMap["b"])
|
||||
assertSame(file3, fileMap["c"])
|
||||
assertNull(fileMap["d"])
|
||||
assertNull(fileMap["e"])
|
||||
|
||||
fileMap["d"] = file4
|
||||
assertEquals(4, fileMap.size)
|
||||
assertSame(file1, fileMap["a"])
|
||||
assertSame(file2, fileMap["b"])
|
||||
assertSame(file3, fileMap["c"])
|
||||
assertSame(file4, fileMap["d"])
|
||||
assertNull(fileMap["e"])
|
||||
|
||||
fileMap["e"] = file5
|
||||
assertEquals(5, fileMap.size)
|
||||
assertSame(file1, fileMap["a"])
|
||||
assertSame(file2, fileMap["b"])
|
||||
assertSame(file3, fileMap["c"])
|
||||
assertSame(file4, fileMap["d"])
|
||||
assertSame(file5, fileMap["e"])
|
||||
|
||||
fileMap["a"] = file2
|
||||
fileMap["b"] = file3
|
||||
fileMap["c"] = file4
|
||||
fileMap["d"] = file5
|
||||
fileMap["e"] = file1
|
||||
|
||||
assertSame(file2, fileMap["a"])
|
||||
assertSame(file3, fileMap["b"])
|
||||
assertSame(file4, fileMap["c"])
|
||||
assertSame(file5, fileMap["d"])
|
||||
assertSame(file1, fileMap["e"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIterator() {
|
||||
val fileMap = FileMap()
|
||||
val files: Array<TextFile> = Array(100) { SingleStringTextFile(it.toString()) }
|
||||
for (i in 0..<100) {
|
||||
fileMap.set("$i".padStart(2, '0'), files[i])
|
||||
}
|
||||
val iterator = fileMap.iterator()
|
||||
val retrievedContents: MutableList<Pair<String, TextFile>> = mutableListOf()
|
||||
while (iterator.hasNext()) {
|
||||
retrievedContents.add(iterator.next())
|
||||
}
|
||||
assertEquals(100, retrievedContents.size)
|
||||
val retrievedContentsSorted = retrievedContents.sortedBy { it.first }
|
||||
for (i in 0..<100) {
|
||||
assertEquals("$i".padStart(2, '0'), retrievedContentsSorted[i].first)
|
||||
assertSame(files[i], retrievedContentsSorted[i].second)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIteratorViaOperator() {
|
||||
val fileMap = FileMap()
|
||||
val files: Array<TextFile> = Array(100) { SingleStringTextFile(it.toString()) }
|
||||
for (i in 0..<100) {
|
||||
fileMap.set("$i".padStart(2, '0'), files[i])
|
||||
}
|
||||
val retrievedContents: MutableList<Pair<String, TextFile>> = mutableListOf()
|
||||
for (entry in fileMap) {
|
||||
retrievedContents.add(entry)
|
||||
}
|
||||
assertEquals(100, retrievedContents.size)
|
||||
val retrievedContentsSorted = retrievedContents.sortedBy { it.first }
|
||||
for (i in 0..<100) {
|
||||
assertEquals("$i".padStart(2, '0'), retrievedContentsSorted[i].first)
|
||||
assertSame(files[i], retrievedContentsSorted[i].second)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun performanceTest() {
|
||||
val numElements = 1000000
|
||||
val fileMap: FileMap = FileMap()
|
||||
val files: Array<TextFile> = Array(numElements) { SingleStringTextFile(it.toString()) }
|
||||
(0..<numElements).forEach {
|
||||
assertEquals(it, fileMap.size)
|
||||
fileMap.set("f$it", files[it])
|
||||
}
|
||||
assertEquals(numElements, fileMap.size)
|
||||
for (i in 0..<numElements) {
|
||||
assertSame(files[i], fileMap.get("f$i"))
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user