Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Кирилл committed Aug 5, 2024
1 parent c194789 commit df675d6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ abstract class UpstreamCreator(

companion object {
fun getHash(nodeId: Int?, obj: Any, hashes: MutableMap<Short, Boolean>): Short =
nodeId?.toShort() ?: (obj.hashCode() % 65535).let {
nodeId?.toShort()?.also {
hashes[it] = true
} ?: (obj.hashCode() % 65535).let {
if (it == 0) 1 else it
}.let { nonZeroHash ->
listOf<Function<Int, Int>>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.emeraldpay.dshackle.startup.configure

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource

class UpstreamCreatorTest {

@ParameterizedTest
@MethodSource("data")
fun `test getHash`(
inputHash: Int?,
obj: Any,
answer: Short
) {
val hash = UpstreamCreator.getHash(inputHash, obj, hashes)

assertThat(hash).isEqualTo(answer)
}

companion object {
private val hashes: MutableMap<Short, Boolean> = HashMap()

@JvmStatic
fun data(): List<Arguments> =
listOf(
Arguments.of(49, 1, 49.toShort()),
Arguments.of(4000, 1, 4000.toShort()),
Arguments.of(24000, 1, 24000.toShort()),
Arguments.of(null, 49, (-49).toShort()),
Arguments.of(null, 49, (32718).toShort()),
Arguments.of(null, 49, (-32719).toShort()),
Arguments.of(null, 49, (-32768).toShort()),
Arguments.of(null, 49, (-32767).toShort())
)
}
}

0 comments on commit df675d6

Please sign in to comment.