From 10bb07861a8f2d1b78a12f5bdfa342b030592732 Mon Sep 17 00:00:00 2001 From: ChenCMD Date: Fri, 28 Jun 2024 14:59:11 +0900 Subject: [PATCH 1/2] Add hashCode() tests for ArrayRecord and Record classes --- .../github/tarao/record4s/ArrayRecordSpec.scala | 12 ++++++++++++ .../com/github/tarao/record4s/RecordSpec.scala | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/modules/core/src/test/scala/com/github/tarao/record4s/ArrayRecordSpec.scala b/modules/core/src/test/scala/com/github/tarao/record4s/ArrayRecordSpec.scala index 3722816..94c3c1e 100644 --- a/modules/core/src/test/scala/com/github/tarao/record4s/ArrayRecordSpec.scala +++ b/modules/core/src/test/scala/com/github/tarao/record4s/ArrayRecordSpec.scala @@ -913,6 +913,18 @@ class ArrayRecordSpec extends helper.UnitSpec { } } + describe(".hashCode()") { + it("should return the same hash code for equal records") { + val r1 = ArrayRecord(name = "tarao", age = 3) + val r2 = ArrayRecord(name = "tarao", age = 3) + val r3 = ArrayRecord(name = "ikura", age = 1) + + (r1.hashCode() == r1.hashCode()) shouldBe true + (r1.hashCode() == r2.hashCode()) shouldBe true + (r2.hashCode() == r1.hashCode()) shouldBe true + } + } + describe(".toString()") { it("can express empty Record") { ArrayRecord.empty.toString() shouldBe "ArrayRecord()" diff --git a/modules/core/src/test/scala/com/github/tarao/record4s/RecordSpec.scala b/modules/core/src/test/scala/com/github/tarao/record4s/RecordSpec.scala index 009ce0f..79f66c7 100644 --- a/modules/core/src/test/scala/com/github/tarao/record4s/RecordSpec.scala +++ b/modules/core/src/test/scala/com/github/tarao/record4s/RecordSpec.scala @@ -807,6 +807,22 @@ class RecordSpec extends helper.UnitSpec { } } + describe(".hashCode()") { + it("should return the same hash code for equal records") { + val r1 = %(name = "tarao", age = 3) + val r2 = %(name = "tarao", age = 3) + val r3 = %(name = "ikura", age = 1) + val r4 = %(name = "tarao", age = 3, email = "tarao@example.com") + val r5: % { val name: String; val age: Int } = r4 + + (r1.hashCode() == r1.hashCode()) shouldBe true + (r1.hashCode() == r2.hashCode()) shouldBe true + (r2.hashCode() == r1.hashCode()) shouldBe true + (r1.hashCode() == r5.as.hashCode()) shouldBe true + (r5.as.hashCode() == r1.hashCode()) shouldBe true + } + } + describe(".toString()") { it("can express empty Record") { Record.empty.toString() shouldBe "%()" From 2e59e6be62a1fcbf105a713f422d95229a7c37d9 Mon Sep 17 00:00:00 2001 From: ChenCMD Date: Fri, 28 Jun 2024 15:21:52 +0900 Subject: [PATCH 2/2] Update hashCode() implementation for ArrayRecord and Record classes --- .../src/main/scala/com/github/tarao/record4s/ArrayRecord.scala | 2 ++ .../core/src/main/scala/com/github/tarao/record4s/Record.scala | 2 ++ 2 files changed, 4 insertions(+) diff --git a/modules/core/src/main/scala/com/github/tarao/record4s/ArrayRecord.scala b/modules/core/src/main/scala/com/github/tarao/record4s/ArrayRecord.scala index db1ac5f..cf3fcb1 100644 --- a/modules/core/src/main/scala/com/github/tarao/record4s/ArrayRecord.scala +++ b/modules/core/src/main/scala/com/github/tarao/record4s/ArrayRecord.scala @@ -68,6 +68,8 @@ abstract class ProductRecord extends Record with Product { case _ => false } + + override def hashCode(): Int = __fields.hashCode() } object ProductRecord { diff --git a/modules/core/src/main/scala/com/github/tarao/record4s/Record.scala b/modules/core/src/main/scala/com/github/tarao/record4s/Record.scala index 188b41d..8685ec9 100644 --- a/modules/core/src/main/scala/com/github/tarao/record4s/Record.scala +++ b/modules/core/src/main/scala/com/github/tarao/record4s/Record.scala @@ -387,6 +387,8 @@ abstract class % extends Record with Selectable { case _ => false } + + override def hashCode(): Int = __iterable.hashCode() } val % = new Record.Extensible(Record.empty)