From 91ca0a3b9ed33b0abc39512a43eedb7d3cc17f9e Mon Sep 17 00:00:00 2001 From: Moldiy <36229419+AnderWoche@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:05:44 +0200 Subject: [PATCH] (#159) added new hashcode function in bitArray --- .../quillraven/fleks/collection/bitArray.kt | 6 +----- .../fleks/collection/BitArrayTest.kt | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/commonMain/kotlin/com/github/quillraven/fleks/collection/bitArray.kt b/src/commonMain/kotlin/com/github/quillraven/fleks/collection/bitArray.kt index b757881d..6afe3e80 100644 --- a/src/commonMain/kotlin/com/github/quillraven/fleks/collection/bitArray.kt +++ b/src/commonMain/kotlin/com/github/quillraven/fleks/collection/bitArray.kt @@ -154,13 +154,9 @@ class BitArray( } override fun hashCode(): Int { - if (bits.isEmpty()) { - return 0 - } - val word = length() ushr 6 var hash = 0 - for (i in 0..word) { + for(i in 0.. word) { hash = 127 * hash + (bits[i] xor (bits[i] ushr 32)).toInt() } return hash diff --git a/src/commonTest/kotlin/com/github/quillraven/fleks/collection/BitArrayTest.kt b/src/commonTest/kotlin/com/github/quillraven/fleks/collection/BitArrayTest.kt index 3f925901..c5dd9189 100644 --- a/src/commonTest/kotlin/com/github/quillraven/fleks/collection/BitArrayTest.kt +++ b/src/commonTest/kotlin/com/github/quillraven/fleks/collection/BitArrayTest.kt @@ -1,11 +1,22 @@ package com.github.quillraven.fleks.collection -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFalse -import kotlin.test.assertTrue +import kotlin.test.* internal class BitArrayTest { + + @Test + fun testBitAt63HashcodeFunction() { + val bits = BitArray(1) + bits.set(63) + try { + bits.hashCode() + } catch (e: Exception) { + fail("hashcode function is brocken.") + } + + } + + @Test fun createEmptyBitArray() { val bits = BitArray(0)