Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.2.0 #46

Merged
merged 5 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions .github/workflows/basic_checks.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
13 changes: 6 additions & 7 deletions .github/workflows/main_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion kotlin-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "xyz.lbres"
version = "1.1.0"
version = "1.2.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package xyz.lbres.kotlinutils.general.labelled
* @param value [T]
* @param label [String]
*/
@Suppress("Unused")
data class Labelled<T>(
val value: T,
val label: String
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -34,6 +36,42 @@ fun <T> 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 <T> 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<KClass<out Exception>>: list of exception classes for which [defaultValue] should be returned.
* All other exceptions will be thrown.
* @param function () -> [T]: function to execute
*/
fun <T> tryOrDefault(defaultValue: T, exceptions: List<KClass<out Exception>>, 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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -67,12 +68,10 @@ internal abstract class AbstractMultiSetImpl<E> : MultiSet<E> {
return false
}

return try {
return tryOrDefault(false) {
@Suppress("UNCHECKED_CAST")
other as MultiSet<E>
getCounts() == getCounts(other)
} catch (_: Exception) {
false
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ArrayExtTest {

@Test
fun testCountElement() {
var intArray: Array<Int> = arrayOf()
var intArray: Array<Int> = emptyArray()
assertEquals(0, intArray.countElement(0))
assertEquals(0, intArray.countElement(175))

Expand All @@ -85,11 +85,11 @@ class ArrayExtTest {
val mutableList2 = mutableListOf(1, 2, 3)
val mutablesArray: Array<IntList> = 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@Suppress("KotlinConstantConditions")
class CollectionExtTest {
@Test
fun testToMultiSet() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -66,17 +71,75 @@ 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
var count = 0
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<KClass<out Exception>> = listOf(ArithmeticException::class)
resultInt = tryOrDefault(0, exceptions) { divFn(0) }
assertEquals(0, resultInt)

exceptions = listOf(NullPointerException::class, NumberFormatException::class)
assertFailsWith<ArithmeticException> { tryOrDefault(0, exceptions) { divFn(0) } }
assertFailsWith<ArithmeticException> { 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<IndexOutOfBoundsException> { 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<CustomException> { tryOrDefault('X', exceptions) { throw CustomException() } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
Loading