-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d5b84a
commit c7c4f49
Showing
6 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ on: | |
|
||
jobs: | ||
build: | ||
name: "Lint and Test" | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ on: | |
|
||
jobs: | ||
build: | ||
name: "Build and Test" | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ plugins { | |
} | ||
|
||
group = "xyz.lbres" | ||
version = "0.2.0" | ||
version = "0.2.1" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
17 changes: 17 additions & 0 deletions
17
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/general/utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package xyz.lbres.kotlinutils.general | ||
|
||
/** | ||
* Function to emulate use of ternary operator for a boolean check | ||
* | ||
* @param check [Boolean] | ||
* @param trueValue [T]: value to return if [check] is true | ||
* @param falseValue [T]: value to return if [check] is false | ||
* @return [trueValue] if the check is true, [falseValue] otherwise | ||
*/ | ||
fun <T> ternaryIf(check: Boolean, trueValue: T, falseValue: T): T { | ||
return if (check) { | ||
trueValue | ||
} else { | ||
falseValue | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
kotlin-utils/src/test/kotlin/xyz/lbres/kotlinutils/general/UtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package xyz.lbres.kotlinutils.general | ||
|
||
import xyz.lbres.kotlinutils.int.ext.isNegative | ||
import kotlin.math.sqrt | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class UtilsTest { | ||
@Test | ||
fun testTernaryIf() { | ||
var expected = true | ||
var result = ternaryIf(true, true, false) | ||
assertEquals(expected, result) | ||
|
||
expected = false | ||
result = ternaryIf(false, true, false) | ||
assertEquals(expected, result) | ||
|
||
expected = false | ||
result = ternaryIf(true, false, true) | ||
assertEquals(expected, result) | ||
|
||
var testInt = 12 | ||
var expectedInt = 1 | ||
var resultInt = ternaryIf(testInt.isNegative(), -1, 1) | ||
assertEquals(expectedInt, resultInt) | ||
|
||
testInt = -12 | ||
expectedInt = -1 | ||
resultInt = ternaryIf(testInt.isNegative(), -1, 1) | ||
assertEquals(expectedInt, resultInt) | ||
|
||
val isPerfectSquare: (Int) -> Boolean = { | ||
val float = it.toFloat() | ||
val sqrt = sqrt(float).toInt() | ||
sqrt * sqrt == it | ||
} | ||
|
||
testInt = 49 | ||
expectedInt = 49 | ||
resultInt = ternaryIf(isPerfectSquare(testInt), testInt, -1) | ||
assertEquals(expectedInt, resultInt) | ||
|
||
testInt = 50 | ||
expectedInt = -1 | ||
resultInt = ternaryIf(isPerfectSquare(testInt), testInt, -1) | ||
assertEquals(expectedInt, resultInt) | ||
} | ||
} |