diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index da8e0396..55283239 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,7 +4,7 @@ ## Checks - [ ] New and existing unit tests pass with my changes - [ ] Unit tests have been written for all new and changed functions -- [ ] Comments have been added to hard-to-understand code +- [ ] Comments have been added where needed - [ ] Docstrings have been updated with any modified function signatures - [ ] ktlint has run successfully - [ ] The version number has been updated if necessary diff --git a/.github/workflows/basic_checks.yml b/.github/workflows/basic_checks.yml index 2483f42e..ada5775e 100644 --- a/.github/workflows/basic_checks.yml +++ b/.github/workflows/basic_checks.yml @@ -1,14 +1,15 @@ -name: Unit Tests +name: Lint and Test on: workflow_dispatch: + workflow_call: push: branches-ignore: - "main" jobs: - build: - name: "Lint and Test" + lint: + name: "Linting" runs-on: ubuntu-latest steps: @@ -17,6 +18,14 @@ jobs: - name: Linting run: ./gradlew ktlintCheck - + + test: + name: "Unit Tests" + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Unit Tests run: ./gradlew test diff --git a/.github/workflows/main_checks.yml b/.github/workflows/main_checks.yml index ee78e183..83c6b629 100644 --- a/.github/workflows/main_checks.yml +++ b/.github/workflows/main_checks.yml @@ -7,19 +7,18 @@ on: - "main" jobs: + basic-checks: + name: "Basic Checks" + uses: ./.github/workflows/basic_checks.yml + build: - name: "Build and Test" + name: "Build" runs-on: ubuntu-latest + needs: "basic-checks" steps: - name: Checkout uses: actions/checkout@v3 - - name: Linting - run: ./gradlew ktlintCheck - - - name: Unit Tests - run: ./gradlew test - - name: Build run: ./gradlew build diff --git a/kotlin-utils/build.gradle.kts b/kotlin-utils/build.gradle.kts index 53490257..07b8558a 100644 --- a/kotlin-utils/build.gradle.kts +++ b/kotlin-utils/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "xyz.lbres" -version = "1.1.0" +version = "1.2.0" repositories { mavenCentral() diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/biginteger/BigIntegerUtils.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/biginteger/BigIntegerUtils.kt index af23a844..35146aa1 100644 --- a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/biginteger/BigIntegerUtils.kt +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/biginteger/BigIntegerUtils.kt @@ -12,18 +12,18 @@ import java.math.BigInteger * @return [BigInteger] the positive greatest common divisor of [val1] and [val2] */ fun getGCD(val1: BigInteger, val2: BigInteger): BigInteger { - val aval1 = val1.abs() - val aval2 = val2.abs() + val abs1 = val1.abs() + val abs2 = val2.abs() when { - aval1.isZero() && aval2.isZero() -> return BigInteger.ONE - aval1.isZero() -> return aval2 - aval2.isZero() -> return aval1 - aval1 == aval2 -> return aval1 + abs1.isZero() && abs2.isZero() -> return BigInteger.ONE + abs1.isZero() -> return abs2 + abs2.isZero() -> return abs1 + abs1 == abs2 -> return abs1 } - var sum = aval1.max(aval2) - var value = aval1.min(aval2) + var sum = abs1.max(abs2) + var value = abs1.min(abs2) var finished = false while (!finished) { diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/labelled/Labelled.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/labelled/Labelled.kt index e0ca20a0..ea7f56bf 100644 --- a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/labelled/Labelled.kt +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/labelled/Labelled.kt @@ -6,6 +6,7 @@ package xyz.lbres.kotlinutils.general.labelled * @param value [T] * @param label [String] */ +@Suppress("Unused") data class Labelled( val value: T, val label: String diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/utils.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/utils.kt index fbf717dd..2510de61 100644 --- a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/utils.kt +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/utils.kt @@ -1,5 +1,7 @@ package xyz.lbres.kotlinutils.general +import kotlin.reflect.KClass + /** * Function to perform a simple boolean check, and return a value based on the result. * Values are computed before check occurs. @@ -34,6 +36,42 @@ fun simpleIf(check: Boolean, trueMethod: () -> T, falseMethod: () -> T): T { } } +/** + * Try to execute a function, and return a default value if the function throws any exception + * + * @param defaultValue [T]: default value to return if any exception is thrown + * @param function () -> [T]: function to execute + */ +fun tryOrDefault(defaultValue: T, function: () -> T): T { + return try { + function() + } catch (_: Exception) { + defaultValue + } +} + +/** + * Try to execute a function, and return a default value if the function throws any of the given exceptions + * + * @param defaultValue [T]: default value to return if an exception from [exceptions] is thrown + * @param exceptions List>: list of exception classes for which [defaultValue] should be returned. + * All other exceptions will be thrown. + * @param function () -> [T]: function to execute + */ +fun tryOrDefault(defaultValue: T, exceptions: List>, function: () -> T): T { + return try { + function() + } catch (e: Exception) { + for (exceptionClass in exceptions) { + if (exceptionClass.isInstance(e)) { + return defaultValue + } + } + + throw e + } +} + /** * Function to perform a simple boolean check, and return a value based on the result * diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/set/multiset/impl/AbstractMultiSetImpl.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/set/multiset/impl/AbstractMultiSetImpl.kt index 744a437c..d868e545 100644 --- a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/set/multiset/impl/AbstractMultiSetImpl.kt +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/set/multiset/impl/AbstractMultiSetImpl.kt @@ -1,6 +1,7 @@ package xyz.lbres.kotlinutils.set.multiset.impl import xyz.lbres.kotlinutils.general.simpleIf +import xyz.lbres.kotlinutils.general.tryOrDefault import xyz.lbres.kotlinutils.iterable.ext.countElement import xyz.lbres.kotlinutils.set.multiset.MultiSet import kotlin.math.min @@ -67,12 +68,10 @@ internal abstract class AbstractMultiSetImpl : MultiSet { return false } - return try { + return tryOrDefault(false) { @Suppress("UNCHECKED_CAST") other as MultiSet getCounts() == getCounts(other) - } catch (_: Exception) { - false } } diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/string/ext/StringExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/string/ext/StringExt.kt index 19d7912c..0d9d3800 100644 --- a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/string/ext/StringExt.kt +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/string/ext/StringExt.kt @@ -1,5 +1,7 @@ package xyz.lbres.kotlinutils.string.ext +import xyz.lbres.kotlinutils.general.tryOrDefault + /** * Substring function which uses end index instead of start index * @@ -9,16 +11,22 @@ package xyz.lbres.kotlinutils.string.ext */ fun String.substringTo(index: Int): String = substring(0, index) +/** + * Get number of characters matching a specific value + * + * @param element [Char]: value to match + * @return [Int]: number of characters with the given value + */ +fun String.countElement(element: Char) = this.count { it == element } + /** * Determine if string can be parsed as Int * * @return [Boolean]: true if string can be parsed as Int, false otherwise */ fun String.isInt(): Boolean { - return try { + return tryOrDefault(false) { toInt() true - } catch (e: Exception) { - false } } diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/array/ext/ArrayExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/array/ext/ArrayExtTest.kt index 11879328..4ee124d4 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/array/ext/ArrayExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/array/ext/ArrayExtTest.kt @@ -65,7 +65,7 @@ class ArrayExtTest { @Test fun testCountElement() { - var intArray: Array = arrayOf() + var intArray: Array = emptyArray() assertEquals(0, intArray.countElement(0)) assertEquals(0, intArray.countElement(175)) @@ -85,11 +85,11 @@ class ArrayExtTest { val mutableList2 = mutableListOf(1, 2, 3) val mutablesArray: Array = arrayOf(mutableList1, mutableList2) assertEquals(2, mutablesArray.countElement(listOf(1, 2, 3))) - assertEquals(0, mutablesArray.countElement(listOf())) + assertEquals(0, mutablesArray.countElement(emptyList())) mutableList1.clear() assertEquals(1, mutablesArray.countElement(listOf(1, 2, 3))) - assertEquals(1, mutablesArray.countElement(listOf())) + assertEquals(1, mutablesArray.countElement(emptyList())) var nullableArray = arrayOf(1, 2, null, 3, null) assertEquals(2, nullableArray.countElement(null)) diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/closedrange/intrange/ext/IntRangeExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/closedrange/intrange/ext/IntRangeExtTest.kt index 6ecb2a1d..d643e38f 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/closedrange/intrange/ext/IntRangeExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/closedrange/intrange/ext/IntRangeExtTest.kt @@ -22,7 +22,7 @@ class IntRangeExtTest { expected = 90 assertEquals(expected, range.size()) - // postive + negative + // positive + negative range = -1000..1000 expected = 2001 assertEquals(expected, range.size()) diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/closedrange/longrange/ext/LongRangeExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/closedrange/longrange/ext/LongRangeExtTest.kt index bc2f6d56..cd08c972 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/closedrange/longrange/ext/LongRangeExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/closedrange/longrange/ext/LongRangeExtTest.kt @@ -22,7 +22,7 @@ class LongRangeExtTest { expected = 90 assertEquals(expected, range.size()) - // postive + negative + // positive + negative range = -1000L..1000L expected = 2001 assertEquals(expected, range.size()) diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/ext/CollectionExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/ext/CollectionExtTest.kt index f57a1789..5b980305 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/ext/CollectionExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/ext/CollectionExtTest.kt @@ -9,6 +9,7 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue +@Suppress("KotlinConstantConditions") class CollectionExtTest { @Test fun testToMultiSet() { diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/general/UtilsTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/general/UtilsTest.kt index 87365433..30bf230d 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/general/UtilsTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/general/UtilsTest.kt @@ -1,10 +1,15 @@ package xyz.lbres.kotlinutils.general import xyz.lbres.kotlinutils.int.ext.isNegative +import xyz.lbres.kotlinutils.int.ext.isZero +import xyz.lbres.kotlinutils.list.IntList import kotlin.math.sqrt +import kotlin.reflect.KClass import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +@Suppress("BooleanLiteralArgument", "KotlinConstantConditions") class UtilsTest { @Test fun testSimpleIf() { @@ -66,12 +71,12 @@ class UtilsTest { val numerator = 4 var denominator = 0 expectedInt = 0 - resultInt = simpleIf(denominator == 0, { 0 }, { numerator / denominator }) + resultInt = simpleIf(denominator.isZero(), { 0 }, { numerator / denominator }) assertEquals(expectedInt, resultInt) denominator = 2 expectedInt = 2 - resultInt = simpleIf(denominator == 0, { 0 }, { numerator / denominator }) + resultInt = simpleIf(denominator.isZero(), { 0 }, { numerator / denominator }) assertEquals(expectedInt, resultInt) // no return @@ -79,4 +84,62 @@ class UtilsTest { simpleIf(false, { count += 1 }, { count += 2 }) assertEquals(2, count) } + + @Test + fun testTryOrDefault() { + val divFn: (Int) -> Int = { 10 / it } + val listMaxFn: (IntList) -> Int = { it.maxOrNull()!! } + val charIndexFn: (Int) -> Char = { "1234"[it] } + + // without exceptions param + var resultInt = tryOrDefault(0) { divFn(0) } + assertEquals(0, resultInt) + + resultInt = tryOrDefault(0) { divFn(1) } + assertEquals(10, resultInt) + + resultInt = tryOrDefault(-1) { listMaxFn(emptyList()) } + assertEquals(-1, resultInt) + + resultInt = tryOrDefault(-1) { listMaxFn(listOf(1, 3, 5, 7)) } + assertEquals(7, resultInt) + + var resultChar = tryOrDefault('-') { charIndexFn(4) } + assertEquals('-', resultChar) + + resultChar = tryOrDefault('-') { charIndexFn(3) } + assertEquals('4', resultChar) + + // with exceptions param + var exceptions: List> = listOf(ArithmeticException::class) + resultInt = tryOrDefault(0, exceptions) { divFn(0) } + assertEquals(0, resultInt) + + exceptions = listOf(NullPointerException::class, NumberFormatException::class) + assertFailsWith { tryOrDefault(0, exceptions) { divFn(0) } } + assertFailsWith { tryOrDefault(0, emptyList()) { divFn(0) } } + + resultInt = tryOrDefault(0, exceptions) { divFn(1) } + assertEquals(10, resultInt) + + exceptions = listOf(NullPointerException::class, IndexOutOfBoundsException::class) + resultChar = tryOrDefault('-', exceptions) { charIndexFn(4) } + assertEquals('-', resultChar) + + exceptions = listOf(NullPointerException::class, ClassCastException::class) + assertFailsWith { tryOrDefault('-', exceptions) { charIndexFn(4) } } + + resultChar = tryOrDefault('-') { charIndexFn(3) } + assertEquals('4', resultChar) + + // custom exception + class CustomException : Exception() + exceptions = listOf(CustomException::class) + + resultChar = tryOrDefault('X') { throw CustomException() } + assertEquals('X', resultChar) + + exceptions = listOf(NullPointerException::class, ClassCastException::class) + assertFailsWith { tryOrDefault('X', exceptions) { throw CustomException() } } + } } diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/generic/ext/GenericExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/generic/ext/GenericExtTest.kt index 1240f851..57b06af0 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/generic/ext/GenericExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/generic/ext/GenericExtTest.kt @@ -5,12 +5,13 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue +@Suppress("KotlinConstantConditions") class GenericExtTest { @Test fun testIfNull() { var string: String? = "abc" var expectedStr = "abc" - var resultStr = string.ifNull { "abcde" } + var resultStr = string.ifNull { "xyz" } assertEquals(expectedStr, resultStr) string = "abc" @@ -20,12 +21,12 @@ class GenericExtTest { string = "" expectedStr = "" - resultStr = string.ifNull { "abcde" } + resultStr = string.ifNull { "xyz" } assertEquals(expectedStr, resultStr) string = null - expectedStr = "abcde" - resultStr = string.ifNull { "abcde" } + expectedStr = "xyz" + resultStr = string.ifNull { "xyz" } assertEquals(expectedStr, resultStr) val nonnull = "123" diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/int/ext/IntExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/int/ext/IntExtTest.kt index 7e391065..b3a7ffd0 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/int/ext/IntExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/int/ext/IntExtTest.kt @@ -5,6 +5,7 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue +@Suppress("KotlinConstantConditions") class IntExtTest { @Test fun testIfZero() { diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/list/ext/ListExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/list/ext/ListExtTest.kt index 96bf254d..89ce50c0 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/list/ext/ListExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/list/ext/ListExtTest.kt @@ -33,8 +33,8 @@ class ListExtTest { .copyWithReplacement(2, "farewell") .copyWithReplacement(0, "hey") .copyWithReplacement(3, "what's up") - .copyWithReplacement(1, "byeeee") - expectedString = listOf("hey", "byeeee", "farewell", "what's up") + .copyWithReplacement(1, "bye") + expectedString = listOf("hey", "bye", "farewell", "what's up") assertEquals(expectedString, listString) val listInt = listOf(0, 1, 1, 2, 3, 6, 8) diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/long/ext/LongExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/long/ext/LongExtTest.kt index 622223a1..71894a6c 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/long/ext/LongExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/long/ext/LongExtTest.kt @@ -5,6 +5,7 @@ import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue +@Suppress("KotlinConstantConditions") class LongExtTest { @Test fun testIfZero() { diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/map/mutablemap/ext/MutableMapExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/map/mutablemap/ext/MutableMapExtTest.kt index 9b72571b..00ce5dd9 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/map/mutablemap/ext/MutableMapExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/map/mutablemap/ext/MutableMapExtTest.kt @@ -8,7 +8,7 @@ class MutableMapExtTest { fun testSetAllValues() { // empty map var intIntMap: MutableMap = mutableMapOf() - var intIntExpected: Map = mapOf() + var intIntExpected: Map = emptyMap() intIntMap.setAllValues(1) assertEquals(intIntExpected, intIntMap) diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/impl/mutable/runMutableUnaryTests.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/impl/mutable/runMutableUnaryTests.kt index d590e03b..720d26ae 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/impl/mutable/runMutableUnaryTests.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/impl/mutable/runMutableUnaryTests.kt @@ -27,7 +27,7 @@ fun runMutableIsEmptyTests() { stringSet = mutableMultiSetOf("123", "abc") assertFalse(stringSet.isEmpty()) - stringSet = mutableMultiSetOf("abcdefg", "abcdefg") + stringSet = mutableMultiSetOf("hello world", "hello world") assertFalse(stringSet.isEmpty()) // remove elements diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/testimpl/TestMultiSet.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/testimpl/TestMultiSet.kt index b39ca36c..860c249d 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/testimpl/TestMultiSet.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/testimpl/TestMultiSet.kt @@ -5,7 +5,8 @@ import xyz.lbres.kotlinutils.set.multiset.MultiSet /** * Alternate MultiSet implementation to use in testing */ -internal class TestMultiSet(collection: Collection) : MultiSet { +@Suppress("EqualsOrHashCode") +class TestMultiSet(collection: Collection) : MultiSet { private val list: List = collection.toList() override val size: Int = collection.size override val distinctValues: Set = collection.toSet() diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/testimpl/TestMutableMultiSet.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/testimpl/TestMutableMultiSet.kt index 4a0ac78b..29bb6521 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/testimpl/TestMutableMultiSet.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/set/multiset/testimpl/TestMutableMultiSet.kt @@ -6,7 +6,8 @@ import xyz.lbres.kotlinutils.set.multiset.MutableMultiSet /** * Alternate MutableMultiSet implementation to use in testing */ -internal class TestMutableMultiSet(collection: Collection) : MutableMultiSet { +@Suppress("EqualsOrHashCode") +class TestMutableMultiSet(collection: Collection) : MutableMultiSet { private val list: MutableList = collection.toMutableList() override val size: Int = collection.size override val distinctValues: MutableSet diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/string/ext/StringExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/string/ext/StringExtTest.kt index d54ac481..68c2be85 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/string/ext/StringExtTest.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/string/ext/StringExtTest.kt @@ -33,6 +33,27 @@ class StringExtTest { assertEquals(expected, string.substringTo(int)) } + @Test + fun testCountElement() { + var string = "" + assertEquals(0, string.countElement('-')) + assertEquals(0, string.countElement('a')) + + string = "hello world" + assertEquals(0, string.countElement('a')) + assertEquals(1, string.countElement('e')) + assertEquals(3, string.countElement('l')) + + string = "%#\\% \n \\.$?'' \" 919" + assertEquals(6, string.countElement(' ')) + assertEquals(2, string.countElement('\\')) + assertEquals(2, string.countElement('\'')) + assertEquals(1, string.countElement('"')) + assertEquals(2, string.countElement('9')) + assertEquals(1, string.countElement('$')) + assertEquals(1, string.countElement('\n')) + } + @Test fun testIsInt() { // int