diff --git a/kotlin-utils/build.gradle.kts b/kotlin-utils/build.gradle.kts index 6754fed4..0fa3b6e0 100644 --- a/kotlin-utils/build.gradle.kts +++ b/kotlin-utils/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "xyz.lbres" -version = "0.2.1" +version = "0.3.0" repositories { mavenCentral() diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/char/ext/CharExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/char/ext/CharExt.kt new file mode 100644 index 00000000..59ece31d --- /dev/null +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/char/ext/CharExt.kt @@ -0,0 +1,19 @@ +package xyz.lbres.kotlinutils.char.ext + +import xyz.lbres.kotlinutils.general.ternaryIf +import xyz.lbres.kotlinutils.int.ext.isZero + +/** + * Returns this number if not zero, or the result of calling [getDefaultValue] if it is. + * + * @param getDefaultValue () -> [Char] + * @return [Char] the current value, or the default + */ +fun Char.ifZero(getDefaultValue: () -> Char): Char = ternaryIf(isZero(), getDefaultValue(), this) + +/** + * Unary check to determine if value is zero + * + * @return [Boolean]: true if value is zero, false otherwise + */ +fun Char.isZero(): Boolean = code.isZero() diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/char/ext/CharCollectionExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/char/ext/CharCollectionExt.kt new file mode 100644 index 00000000..61e0ee89 --- /dev/null +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/char/ext/CharCollectionExt.kt @@ -0,0 +1,30 @@ +package xyz.lbres.kotlinutils.collection.char.ext + +import xyz.lbres.kotlinutils.char.ext.isZero + +/** + * Filter a char collection to contain only elements that do not equal zero. + * + * @return [Collection]<[Char]>: collection containing the same values as [this], except any elements with value 0. + */ +fun Collection.filterNotZero(): Collection = filterNot { it.isZero() } + +/** + * Add all values in collection. + * + * @return [Char]: sum of numbers in collection + */ +fun Collection.sum(): Char = fold(Char(0)) { acc, char -> acc + char.code } + +/** + * Multiply all values in collection + * + * @return [Char]: product of numbers in collection, or 0 if collection is empty + */ +fun Collection.product(): Char { + if (isEmpty()) { + return Char(0) + } + + return fold(1) { acc, char -> acc * char.code }.toChar() +} diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/ext/CollectionExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/ext/CollectionExt.kt index 1856fd16..0c1af2f8 100644 --- a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/ext/CollectionExt.kt +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/ext/CollectionExt.kt @@ -4,7 +4,8 @@ import xyz.lbres.kotlinutils.classes.multiset.MultiSet import xyz.lbres.kotlinutils.classes.multiset.MultiSetImpl import xyz.lbres.kotlinutils.classes.multiset.MutableMultiSet import xyz.lbres.kotlinutils.classes.multiset.MutableMultiSetImpl -import xyz.lbres.kotlinutils.int.ext.isZero +import xyz.lbres.kotlinutils.generic.ext.isNotNull +import xyz.lbres.kotlinutils.generic.ext.isNull /** * Create a MultiSet with the elements in the current collection. @@ -21,8 +22,23 @@ fun Collection.toMultiSet(): MultiSet = MultiSetImpl(this) fun Collection.toMutableMultiSet(): MutableMultiSet = MutableMultiSetImpl(this) /** - * Filter an integer collection to contain only elements that do not equal zero. + * Count number of elements in the collection that are null. * - * @return [Collection]: collection containing the same values as [this], except any elements with value 0. + * @return [Int] */ -fun Collection.filterNotZero() = filterNot { it.isZero() } +fun Collection.countNull(): Int = count { it.isNull() } + +/** + * Count number of elements in the collection that are not null. + * + * @return [Int] + */ +fun Collection.countNotNull(): Int = count { it.isNotNull() } + +/** + * Check if a collection is not null and is not empty. + * Combines separate calls for checking if the collection is null and if it is empty. + * + * @return [Boolean]: true if the collection is not null and has size > 0, false otherwise + */ +fun Collection?.isNotNullOrEmpty() = !this.isNullOrEmpty() diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/int/ext/IntCollectionExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/int/ext/IntCollectionExt.kt new file mode 100644 index 00000000..e9570964 --- /dev/null +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/int/ext/IntCollectionExt.kt @@ -0,0 +1,25 @@ +package xyz.lbres.kotlinutils.collection.int.ext + +import xyz.lbres.kotlinutils.general.ternaryIf +import xyz.lbres.kotlinutils.int.ext.isZero + +/** + * Filter an integer collection to contain only elements that do not equal zero. + * + * @return [Collection]<[Int]>: collection containing the same values as [this], except any elements with value 0. + */ +fun Collection.filterNotZero(): Collection = filterNot { it.isZero() } + +/** + * Add all values in collection. + * + * @return [Int]: sum of numbers in collection + */ +fun Collection.sum(): Int = fold(0, Int::plus) + +/** + * Multiply all values in collection + * + * @return [Int]: product of numbers in collection, or 0 if collection is empty + */ +fun Collection.product(): Int = ternaryIf(isEmpty(), 0, fold(1, Int::times)) diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/long/ext/LongCollectionExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/long/ext/LongCollectionExt.kt new file mode 100644 index 00000000..a68c5051 --- /dev/null +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/collection/long/ext/LongCollectionExt.kt @@ -0,0 +1,25 @@ +package xyz.lbres.kotlinutils.collection.long.ext + +import xyz.lbres.kotlinutils.general.ternaryIf +import xyz.lbres.kotlinutils.long.ext.isZero + +/** + * Filter a long collection to contain only elements that do not equal zero. + * + * @return [Collection]<[Long]>: collection containing the same values as [this], except any elements with value 0. + */ +fun Collection.filterNotZero(): Collection = filterNot { it.isZero() } + +/** + * Add all values in collection. + * + * @return [Long]: sum of numbers in collection + */ +fun Collection.sum(): Long = fold(0, Long::plus) + +/** + * Multiply all values in collection + * + * @return [Long]: product of numbers in collection, or 0 if collection is empty + */ +fun Collection.product(): Long = ternaryIf(isEmpty(), 0, fold(1, Long::times)) diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/generic/ext/GenericExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/generic/ext/GenericExt.kt index 733d4f11..f0b5b343 100644 --- a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/generic/ext/GenericExt.kt +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/generic/ext/GenericExt.kt @@ -7,3 +7,17 @@ package xyz.lbres.kotlinutils.generic.ext * @return [T] the current value, or the default */ fun T?.ifNull(getDefaultValue: () -> T): T = this ?: getDefaultValue() + +/** + * Returns true if a value is null, or false otherwise + * + * @return [Boolean] + */ +fun T?.isNull(): Boolean = this == null + +/** + * Returns true if a value is not null, or false otherwise + * + * @return [Boolean] + */ +fun T?.isNotNull(): Boolean = this != null diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/int/ext/IntExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/int/ext/IntExt.kt index 98b4deab..a88ecc42 100644 --- a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/int/ext/IntExt.kt +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/int/ext/IntExt.kt @@ -1,18 +1,14 @@ package xyz.lbres.kotlinutils.int.ext +import xyz.lbres.kotlinutils.general.ternaryIf + /** * Returns this number if not zero, or the result of calling [getDefaultValue] if it is. * - * @param getDefaultValue [() -> Int] + * @param getDefaultValue () -> [Int] * @return [Int] the current value, or the default */ -fun Int.ifZero(getDefaultValue: () -> Int): Int { - return if (equals(0)) { - getDefaultValue() - } else { - this - } -} +fun Int.ifZero(getDefaultValue: () -> Int): Int = ternaryIf(isZero(), getDefaultValue(), this) /** * Unary check to determine if value is zero diff --git a/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/long/ext/LongExt.kt b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/long/ext/LongExt.kt new file mode 100644 index 00000000..eb92410f --- /dev/null +++ b/kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/long/ext/LongExt.kt @@ -0,0 +1,25 @@ +package xyz.lbres.kotlinutils.long.ext + +import xyz.lbres.kotlinutils.general.ternaryIf + +/** + * Returns this number if not zero, or the result of calling [getDefaultValue] if it is. + * + * @param getDefaultValue () -> [Long] + * @return [Long] the current value, or the default + */ +fun Long.ifZero(getDefaultValue: () -> Long): Long = ternaryIf(isZero(), getDefaultValue(), this) + +/** + * Unary check to determine if value is zero + * + * @return [Boolean]: true if value is zero, false otherwise + */ +fun Long.isZero(): Boolean = equals(0L) + +/** + * Unary check to determine if value is negative + * + * @return [Boolean]: true if value is less than zero, false otherwise + */ +fun Long.isNegative(): Boolean = this < 0L diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/char/ext/CharExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/char/ext/CharExtTest.kt new file mode 100644 index 00000000..7c3ce374 --- /dev/null +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/char/ext/CharExtTest.kt @@ -0,0 +1,37 @@ +package xyz.lbres.kotlinutils.char.ext + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +internal class CharExtTest { + @Test + internal fun testIfZero() { + val getValue = { Char(2) } + + var char = Char(0) + var expected = Char(2) + assertEquals(expected, char.ifZero(getValue)) + + char = Char(2) + expected = Char(2) + assertEquals(expected, char.ifZero(getValue)) + + char = Char(15) + expected = Char(15) + assertEquals(expected, char.ifZero(getValue)) + } + + @Test + internal fun testIsZero() { + var char = Char(0) + assertTrue(char.isZero()) + + char = Char(1) + assertFalse(char.isZero()) + + char = Char(100) + assertFalse(char.isZero()) + } +} diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/char/ext/CharCollectionExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/char/ext/CharCollectionExtTest.kt new file mode 100644 index 00000000..245ecb3a --- /dev/null +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/char/ext/CharCollectionExtTest.kt @@ -0,0 +1,68 @@ +package xyz.lbres.kotlinutils.collection.char.ext + +import kotlin.test.Test +import kotlin.test.assertEquals + +internal class CharCollectionExtTest { + private val zero = Char(0) + private val one = Char(1) + private val four = Char(4) + private val five = Char(5) + + @Test + internal fun testFilterNotZero() { + var list: List = listOf() + var expected: List = listOf() + assertEquals(expected, list.filterNotZero()) + + list = listOf(zero, zero, zero) + expected = listOf() + assertEquals(expected, list.filterNotZero()) + + list = listOf(one, Char(2), zero, four, zero, zero, five) + expected = listOf(one, Char(2), four, five) + assertEquals(expected, list.filterNotZero()) + + list = listOf(one, four, Char(1000), Char(19), five) + expected = listOf(one, four, Char(1000), Char(19), five) + assertEquals(expected, list.filterNotZero()) + } + + @Test + internal fun testSum() { + var list: List = emptyList() + var expected = zero + assertEquals(expected, list.sum()) + + list = listOf(Char(33)) + expected = Char(33) + assertEquals(expected, list.sum()) + + list = listOf(Char(100), Char(45), Char(10), Char(67), Char(99)) + expected = Char(321) + assertEquals(expected, list.sum()) + } + + @Test + internal fun testProduct() { + var list: List = emptyList() + var expected = zero + assertEquals(expected, list.product()) + + list = listOf(zero) + expected = zero + assertEquals(expected, list.product()) + + list = listOf(one) + expected = one + assertEquals(expected, list.product()) + + list = listOf(four, four, zero) + expected = zero + assertEquals(expected, list.product()) + + list = listOf(Char(15), Char(23), Char(4), Char(4)) + expected = Char(5520) + assertEquals(expected, list.product()) + } +} 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 5f64505d..105c6d35 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 @@ -6,6 +6,8 @@ import xyz.lbres.kotlinutils.classes.multiset.multiSetOf import xyz.lbres.kotlinutils.classes.multiset.mutableMultiSetOf import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue internal class CollectionExtTest { @Test @@ -61,25 +63,66 @@ internal class CollectionExtTest { } @Test - internal fun testFilterNotZero() { - var list: List = listOf() - var expected: List = listOf() - assertEquals(expected, list.filterNotZero()) - - list = listOf(0, 0, 0) - expected = listOf() - assertEquals(expected, list.filterNotZero()) - - list = listOf(1, 2, 0, 4, 0, 0, 5) - expected = listOf(1, 2, 4, 5) - assertEquals(expected, list.filterNotZero()) - - list = listOf(-1, 1, 0) - expected = listOf(-1, 1) - assertEquals(expected, list.filterNotZero()) - - list = listOf(1, 4, 1000, 19, 5) - expected = listOf(1, 4, 1000, 19, 5) - assertEquals(expected, list.filterNotZero()) + internal fun testCountNull() { + var collection: Collection = listOf() + var expected = 0 + assertEquals(expected, collection.countNull()) + + collection = listOf(1, 2, 3, 4, 5) + expected = 0 + assertEquals(expected, collection.countNull()) + + collection = setOf(1, null, 2, 3, 4) + expected = 1 + assertEquals(expected, collection.countNull()) + + collection = listOf(null, null, null, 4, 5, 6, null) + expected = 4 + assertEquals(expected, collection.countNull()) + + collection = listOf(null, null) + expected = 2 + assertEquals(expected, collection.countNull()) + } + + @Test + internal fun testCountNotNull() { + var collection: Collection = listOf() + var expected = 0 + assertEquals(expected, collection.countNotNull()) + + collection = listOf(null, null) + expected = 0 + assertEquals(expected, collection.countNotNull()) + + collection = setOf(1, null, 2, 3, 4) + expected = 4 + assertEquals(expected, collection.countNotNull()) + + collection = listOf(null, null, null, 4, 5, 6, null) + expected = 3 + assertEquals(expected, collection.countNotNull()) + + collection = listOf(1, 2, 3, 4, 5) + expected = 5 + assertEquals(expected, collection.countNotNull()) + } + + @Test + internal fun testIsNotNullOrEmpty() { + var collection: Collection? = null + assertFalse(collection.isNotNullOrEmpty()) + + collection = setOf() + assertFalse(collection.isNotNullOrEmpty()) + + collection = listOf(1) + assertTrue(collection.isNotNullOrEmpty()) + + collection = listOf(null) + assertTrue(collection.isNotNullOrEmpty()) + + collection = listOf(1, 2, 3) + assertTrue(collection.isNotNullOrEmpty()) } } diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/int/ext/IntCollectionExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/int/ext/IntCollectionExtTest.kt new file mode 100644 index 00000000..c3b9a3d8 --- /dev/null +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/int/ext/IntCollectionExtTest.kt @@ -0,0 +1,88 @@ +package xyz.lbres.kotlinutils.collection.int.ext + +import xyz.lbres.kotlinutils.list.IntList +import kotlin.test.Test +import kotlin.test.assertEquals + +class IntCollectionExtTest { + @Test + internal fun testFilterNotZero() { + var list: List = listOf() + var expected: List = listOf() + assertEquals(expected, list.filterNotZero()) + + list = listOf(0, 0, 0) + expected = listOf() + assertEquals(expected, list.filterNotZero()) + + list = listOf(1, 2, 0, 4, 0, 0, 5) + expected = listOf(1, 2, 4, 5) + assertEquals(expected, list.filterNotZero()) + + list = listOf(-1, 1, 0) + expected = listOf(-1, 1) + assertEquals(expected, list.filterNotZero()) + + list = listOf(1, 4, 1000, 19, 5) + expected = listOf(1, 4, 1000, 19, 5) + assertEquals(expected, list.filterNotZero()) + } + + @Test + internal fun testSum() { + var list: IntList = emptyList() + var expected = 0 + assertEquals(expected, list.sum()) + + list = listOf(33) + expected = 33 + assertEquals(expected, list.sum()) + + list = listOf(-33) + expected = -33 + assertEquals(expected, list.sum()) + + list = listOf(5, -5) + expected = 0 + assertEquals(expected, list.sum()) + + list = listOf(100, 45, -10, 67, 99) + expected = 301 + assertEquals(expected, list.sum()) + + list = listOf(-100, 45, -10, -67, 99) + expected = -33 + assertEquals(expected, list.sum()) + } + + @Test + internal fun testProduct() { + var list: IntList = emptyList() + var expected = 0 + assertEquals(expected, list.product()) + + list = listOf(0) + expected = 0 + assertEquals(expected, list.product()) + + list = listOf(1) + expected = 1 + assertEquals(expected, list.product()) + + list = listOf(-1) + expected = -1 + assertEquals(expected, list.product()) + + list = listOf(5, 5, -2, 0) + expected = 0 + assertEquals(expected, list.product()) + + list = listOf(-15, 23, 17, 4, 4, -2, 3) + expected = 563040 + assertEquals(expected, list.product()) + + list = listOf(-15, 23, 17, 4, 4, -2, -3) + expected = -563040 + assertEquals(expected, list.product()) + } +} diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/long/ext/LongCollectionExtTest.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/long/ext/LongCollectionExtTest.kt new file mode 100644 index 00000000..730b00e5 --- /dev/null +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/collection/long/ext/LongCollectionExtTest.kt @@ -0,0 +1,87 @@ +package xyz.lbres.kotlinutils.collection.long.ext + +import kotlin.test.Test +import kotlin.test.assertEquals + +internal class LongCollectionExtTest { + @Test + internal fun testFilterNotZero() { + var list: List = listOf() + var expected: List = listOf() + assertEquals(expected, list.filterNotZero()) + + list = listOf(0, 0, 0) + expected = listOf() + assertEquals(expected, list.filterNotZero()) + + list = listOf(1, 2, 0, 4, 0, 0, 5) + expected = listOf(1, 2, 4, 5) + assertEquals(expected, list.filterNotZero()) + + list = listOf(-1, 1, 0) + expected = listOf(-1, 1) + assertEquals(expected, list.filterNotZero()) + + list = listOf(1, 4, 1000, 19, 5) + expected = listOf(1, 4, 1000, 19, 5) + assertEquals(expected, list.filterNotZero()) + } + + @Test + internal fun testSum() { + var list: List = emptyList() + var expected = 0L + assertEquals(expected, list.sum()) + + list = listOf(33) + expected = 33 + assertEquals(expected, list.sum()) + + list = listOf(-33) + expected = -33 + assertEquals(expected, list.sum()) + + list = listOf(5, -5) + expected = 0 + assertEquals(expected, list.sum()) + + list = listOf(100, 45, -10, 67, 99) + expected = 301 + assertEquals(expected, list.sum()) + + list = listOf(-100, 45, -10, -67, 99) + expected = -33 + assertEquals(expected, list.sum()) + } + + @Test + internal fun testProduct() { + var list: List = emptyList() + var expected = 0L + assertEquals(expected, list.product()) + + list = listOf(0) + expected = 0 + assertEquals(expected, list.product()) + + list = listOf(1) + expected = 1 + assertEquals(expected, list.product()) + + list = listOf(-1) + expected = -1 + assertEquals(expected, list.product()) + + list = listOf(5, 5, -2, 0) + expected = 0 + assertEquals(expected, list.product()) + + list = listOf(-15, 23, 17, 4, 4, -2, 3) + expected = 563040 + assertEquals(expected, list.product()) + + list = listOf(-15, 23, 17, 4, 4, -2, -3) + expected = -563040 + assertEquals(expected, list.product()) + } +} 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 ea4c46a2..020795d0 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 @@ -2,6 +2,8 @@ package xyz.lbres.kotlinutils.generic.ext import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue internal class GenericExtTest { @Test @@ -56,4 +58,58 @@ internal class GenericExtTest { val resultErr = error.ifNull { expectedErr } assertEquals(expectedErr, resultErr) } + + @Test + fun testIsNull() { + var string: String? = null + assertTrue(string.isNull()) + + string = "" + assertFalse(string.isNull()) + + string = "null" + assertFalse(string.isNull()) + + string = "abc 123" + assertFalse(string.isNull()) + + var int: Int? = null + assertTrue(int.isNull()) + + int = -100 + assertFalse(int.isNull()) + + var list: List? = null + assertTrue(list.isNull()) + + list = listOf(null, null) + assertFalse(list.isNull()) + } + + @Test + fun testIsNotNull() { + var string: String? = null + assertFalse(string.isNotNull()) + + string = "" + assertTrue(string.isNotNull()) + + string = "null" + assertTrue(string.isNotNull()) + + string = "abc 123" + assertTrue(string.isNotNull()) + + var int: Int? = null + assertFalse(int.isNotNull()) + + int = -100 + assertTrue(int.isNotNull()) + + var list: List? = null + assertFalse(list.isNotNull()) + + list = listOf(null, null) + assertTrue(list.isNotNull()) + } } 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 new file mode 100644 index 00000000..23980e01 --- /dev/null +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/long/ext/LongExtTest.kt @@ -0,0 +1,65 @@ +package xyz.lbres.kotlinutils.long.ext + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class LongExtTest { + @Test + internal fun testIfZero() { + val getValue = { 2L } + + var long = 0L + var expected = 2L + assertEquals(expected, long.ifZero(getValue)) + + long = 2 + expected = 2 + assertEquals(expected, long.ifZero(getValue)) + + long = 15 + expected = 15 + assertEquals(expected, long.ifZero(getValue)) + + long = -100 + expected = -100 + assertEquals(expected, long.ifZero(getValue)) + } + + @Test + internal fun testIsNegative() { + var long = 0L + assertFalse(long.isNegative()) + + long = 1L + assertFalse(long.isNegative()) + + long = 100 + assertFalse(long.isNegative()) + + long = -1 + assertTrue(long.isNegative()) + + long = -100 + assertTrue(long.isNegative()) + } + + @Test + internal fun testIsZero() { + var long = 0L + assertTrue(long.isZero()) + + long = 1 + assertFalse(long.isZero()) + + long = -1 + assertFalse(long.isZero()) + + long = 100 + assertFalse(long.isZero()) + + long = -100 + assertFalse(long.isZero()) + } +} diff --git a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/testHelpers.kt b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/testHelpers.kt index 0e0c66d2..246ef4fd 100644 --- a/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/testHelpers.kt +++ b/kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/testHelpers.kt @@ -34,7 +34,7 @@ internal fun runRandomTest(randomAction: () -> T, randomCheck: (T) -> Boolea */ internal fun runTestWithWeights(weightedItems: WeightedList, randomAction: () -> T) { val iterations = 1000 - val errorRange = 0.05f + val errorRange = 0.075f // run action and collect results val results = List(iterations) { randomAction() }