Skip to content

Commit

Permalink
simplify murmurhash, fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Sep 20, 2023
1 parent cc30f88 commit efc50bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
17 changes: 5 additions & 12 deletions evaluation-core/src/commonMain/kotlin/Murmur3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,12 @@ internal object Murmur3 {
return hashResult
}

private fun Int.reverseBytes(): Int {
return (this and -0x1000000 ushr 24) or
(this and 0x00ff0000 ushr 8) or
(this and 0x0000ff00 shl 8) or
(this and 0x000000ff shl 24)
}

private fun ByteArray.readIntLe(index: Int = 0): Int {
return (
this[index].toInt() and 0xff shl 24
or (this[index + 1].toInt() and 0xff shl 16)
or (this[index + 2].toInt() and 0xff shl 8)
or (this[index + 3].toInt() and 0xff)
).reverseBytes()
this[index].toInt() and 0xff
or (this[index + 1].toInt() and 0xff shl 8)
or (this[index + 2].toInt() and 0xff shl 16)
or (this[index + 3].toInt() and 0xff shl 24)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ class EvaluationIntegrationTest {

@Test
fun `test device id bucketing`() {
val user = userContext(userId = "device_id")
val result = engine.evaluate(user, flags)["test-user-id-bucketing"]
val user = userContext(deviceId = "device_id")
val result = engine.evaluate(user, flags)["test-device-id-bucketing"]
DefaultAsserter.assertEquals(
"Unexpected evaluation result",
"on",
Expand Down
9 changes: 9 additions & 0 deletions evaluation-core/src/commonTest/kotlin/MurMurEnglishTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.amplitude.experiment.evaluation

import com.amplitude.experiment.evaluation.util.ENGLISH_WORDS
import com.amplitude.experiment.evaluation.util.HASH3_X86_32
import io.ktor.utils.io.core.toByteArray
import kotlin.test.Test
import kotlin.test.assertEquals

Expand All @@ -15,6 +16,14 @@ class Murmur3Test {
private val englishWords = ENGLISH_WORDS.trim()
private val hash3X8632 = HASH3_X86_32.trim()

@Test
fun testMurMur3HashSimple() {
val input = "brian".toByteArray()
val result = Murmur3.hash32x86(input, input.size, MURMUR_SEED).toLong()
val expected = (3948467465 and 0xffffffff)
assertEquals(3948467465, expected)
}

@Test
fun testEnglishWordsMurmur3_x86_32() {
testHashes(object : StringHashFunction {
Expand Down

0 comments on commit efc50bd

Please sign in to comment.