Last active
February 25, 2023 07:54
-
-
Save nasserkhosravi/649dfc11d20aeab85c542c89ec2ef111 to your computer and use it in GitHub Desktop.
Update aFloat function
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
import kotlin.random.Random | |
import kotlin.reflect.KMutableProperty | |
import kotlin.reflect.KProperty | |
fun <T> Array<T>.aElement(): T { | |
require(this.isNotEmpty()) | |
return this[Random.nextInt(0, size - 1)] | |
} | |
fun aBoolean(random: Random = Random): Boolean { | |
return when ((0..1).random(random)) { | |
1 -> true | |
else -> false | |
} | |
} | |
fun aString(length: Int = 5, maybeNumber: Boolean = true, random: Random = Random): String { | |
require(length > -1) { | |
"length must bigger than -1" | |
} | |
var allowedChars = ('A'..'Z') + ('a'..'z') | |
if (maybeNumber) { | |
allowedChars = allowedChars.plus('0'..'9') | |
} | |
return (1..length) | |
.map { allowedChars.random(random) } | |
.joinToString("") | |
} | |
fun aDouble(random: Random = Random) = random.nextDouble(Double.MIN_VALUE, Double.MAX_VALUE) | |
fun aFloat(random: Random = Random) = Float.MIN_VALUE + random.nextFloat() * (Float.MAX_VALUE - Float.MIN_VALUE) | |
fun aPositiveDouble(random: Random = Random) = random.nextDouble(0.0, Double.MAX_VALUE) | |
fun aHexColor(random: Random = Random): String { | |
val nextInt = random.nextInt(0xffffff + 1) | |
return String.format("#%06x", nextInt) | |
} | |
fun aInt(min: Int = Int.MIN_VALUE, max: Int = Int.MAX_VALUE, random: Random = Random) = | |
random.nextInt(min, max) | |
fun aLong(random: Random = Random) = random.nextLong(Long.MIN_VALUE, Long.MAX_VALUE) | |
fun aPositiveInt(random: Random = Random) = random.nextInt(0, Int.MAX_VALUE) | |
fun aPositiveLong(random: Random = Random) = random.nextLong(0, Long.MAX_VALUE) | |
fun anotherString(outValues: List<String>, random: Random = Random): String { | |
require(outValues.isNotEmpty()) | |
val string = aString(random = random) | |
if (outValues.contains(string)) { | |
anotherString(outValues, random) | |
} | |
return string | |
} | |
/** | |
* Generate a random int by [generator] that is not an element of [outValues] | |
*/ | |
fun anotherInt(outValues: List<Int>, random: Random = Random, generator: (random: Random) -> Int = ::aInt): Int { | |
require(outValues.isNotEmpty()) | |
val int = generator(random) | |
if (outValues.contains(int)) { | |
anotherInt(outValues, random, generator) | |
} | |
return int | |
} | |
/** | |
* Generate a random int by [generator] that is not equal to [outValue] | |
*/ | |
fun anotherInt(outValue: Int, random: Random = Random, generator: (random: Random) -> Int = ::aInt): Int { | |
val int = generator(random) | |
if (outValue == int) { | |
anotherInt(outValue, random, generator) | |
} | |
return int | |
} | |
/** | |
* Generate [number] different url with specified [domain] | |
*/ | |
fun multiUrl( | |
number: Int, | |
scheme: String = "http", | |
domain: String = "com", | |
random: Random = Random(number) | |
): List<String> { | |
require(number > 0) | |
return (0..number).map { | |
aUrl(random = random, scheme = scheme, domain = domain) | |
} | |
} | |
fun aUrl( | |
scheme: String = "http", | |
host: String? = null, | |
domain: String = "com", | |
path: String = "", | |
params: Map<String, String> = emptyMap(), | |
random: Random = Random, | |
): String { | |
val fHost = host ?: aString(random = random) | |
val baseUrl = StringBuilder() | |
.append(normalizeScheme(scheme)) | |
.append(fHost) | |
.appendIfTrue(domain.isNotEmpty(), ".$domain") | |
.append(path) | |
.toString() | |
return if (params.isNotEmpty()) { | |
baseUrl.appendParamToUrl(params) | |
} else baseUrl | |
} | |
fun aScheme(): String = aString(4).plus("://") | |
private fun String.appendParamToUrl( | |
params: Map<String, String> | |
): String { | |
val url = this | |
if (params.isEmpty()) { | |
return url | |
} | |
val fUrl = if (!url.endsWith("?")) { | |
url.plus("?") | |
} else url | |
val path = params.map { "${it.key}=${it.value}" }.toList().joinToString(separator = "&") | |
return fUrl.plus(path) | |
} | |
private fun normalizeScheme(scheme: String) = if (scheme.isNotEmpty() && !scheme.endsWith("://")) { | |
scheme.plus("://") | |
} else scheme | |
private fun StringBuilder.appendIfTrue(condition: Boolean, string: String): StringBuilder { | |
if (condition) { | |
return append(string) | |
} | |
return this | |
} | |
infix fun <R> IntRange.generate(generator: (index: Int) -> R): List<R> { | |
return map { generator(it) } | |
} | |
fun <R> generate(count: Int, generator: (index: Int) -> R): List<R> { | |
return (1..count).map { generator(it) } | |
} | |
fun aPairLatLng(random: Random = Random): Pair<Double, Double> = aLat(random) to aLng(random) | |
fun aLat(random: Random = Random): Double = random.nextDouble() * -180.0 + 90.0 | |
fun aLng(random: Random = Random): Double = random.nextDouble() * -360.0 + 180.0 | |
fun aThrowable(message: String = ""): Throwable = Throwable(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment