add performance test
This commit is contained in:
parent
a15a0809ec
commit
6557cf2564
|
@ -1,52 +1,70 @@
|
||||||
import org.apache.commons.lang3.RandomStringUtils
|
import org.apache.commons.lang3.RandomStringUtils
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.jupiter.api.BeforeAll
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import java.security.SecureRandom
|
||||||
import java.util.concurrent.ThreadLocalRandom
|
import java.util.concurrent.ThreadLocalRandom
|
||||||
|
import kotlin.experimental.and
|
||||||
import kotlin.streams.asSequence
|
import kotlin.streams.asSequence
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
const val STRING_LENGTH = 10;
|
const val STRING_LENGTH = 10
|
||||||
const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+";
|
const val ALPHANUMERIC_REGEX = "[a-zA-Z0-9]+"
|
||||||
|
|
||||||
class RandomStringTest {
|
class RandomStringTest {
|
||||||
|
|
||||||
@Test
|
val charPool = ArrayList<Char>()
|
||||||
fun generateRandomString_useJava_returnString() {
|
|
||||||
val charPool = ArrayList<Char>();
|
@BeforeEach
|
||||||
|
fun charPool() {
|
||||||
charPool.addAll('a'..'z');
|
charPool.addAll('a'..'z');
|
||||||
charPool.addAll('A'..'Z');
|
charPool.addAll('A'..'Z');
|
||||||
charPool.addAll('0'..'9');
|
charPool.addAll('0'..'9');
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenAStringLength_whenUsingJava_thenReturnAlphanumericString() {
|
||||||
var randomString = ThreadLocalRandom.current()
|
var randomString = ThreadLocalRandom.current()
|
||||||
.ints(STRING_LENGTH.toLong(), 0, charPool.size)
|
.ints(STRING_LENGTH.toLong(), 0, charPool.size)
|
||||||
.asSequence()
|
.asSequence()
|
||||||
.map(charPool::get)
|
.map(charPool::get)
|
||||||
.joinToString("")
|
.joinToString("")
|
||||||
|
|
||||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)));
|
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||||
assertEquals(STRING_LENGTH, randomString.length);
|
assertEquals(STRING_LENGTH, randomString.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun generateRandomString_useKotlin_returnString() {
|
fun givenAStringLength_whenUsingKotlin_thenReturnAlphanumericString() {
|
||||||
val charPool = ArrayList<Char>();
|
|
||||||
charPool.addAll('a'..'z');
|
|
||||||
charPool.addAll('A'..'Z');
|
|
||||||
charPool.addAll('0'..'9');
|
|
||||||
|
|
||||||
var randomString = (1..STRING_LENGTH).map { i -> kotlin.random.Random.nextInt(0, charPool.size) }
|
var randomString = (1..STRING_LENGTH).map { i -> kotlin.random.Random.nextInt(0, charPool.size) }
|
||||||
.map(charPool::get)
|
.map(charPool::get)
|
||||||
.joinToString("");
|
.joinToString("")
|
||||||
|
|
||||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)));
|
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||||
assertEquals(STRING_LENGTH, randomString.length);
|
assertEquals(STRING_LENGTH, randomString.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun generateRandomString_useApacheCommon_returnString() {
|
fun givenAStringLength_whenUsingApacheCommon_thenReturnAlphanumericString() {
|
||||||
var randomString = RandomStringUtils.randomAlphanumeric(STRING_LENGTH);
|
var randomString = RandomStringUtils.randomAlphanumeric(STRING_LENGTH)
|
||||||
|
|
||||||
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)));
|
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||||
assertEquals(STRING_LENGTH, randomString.length);
|
assertEquals(STRING_LENGTH, randomString.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenAStringLength_whenUsingRandomForBytes_thenReturnAlphanumericString() {
|
||||||
|
val random = SecureRandom()
|
||||||
|
val bytes = ByteArray(STRING_LENGTH)
|
||||||
|
random.nextBytes(bytes)
|
||||||
|
|
||||||
|
var randomString = (0..bytes.size - 1).map { i ->
|
||||||
|
charPool.get((bytes[i] and 0xFF.toByte() and charPool.size.toByte()).toInt())
|
||||||
|
}.joinToString("")
|
||||||
|
|
||||||
|
assert(randomString.matches(Regex(ALPHANUMERIC_REGEX)))
|
||||||
|
assertEquals(STRING_LENGTH, randomString.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue