Skip to content

Commit

Permalink
Merge pull request #103 from ChenCMD/master
Browse files Browse the repository at this point in the history
Implement hashCode() in ArrayRecord and Record classes
  • Loading branch information
tarao authored Jun 28, 2024
2 parents c8348e3 + 2e59e6b commit fe8be58
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ abstract class ProductRecord extends Record with Product {
case _ =>
false
}

override def hashCode(): Int = __fields.hashCode()
}

object ProductRecord {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ abstract class % extends Record with Selectable {
case _ =>
false
}

override def hashCode(): Int = __iterable.hashCode()
}

val % = new Record.Extensible(Record.empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]")
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 "%()"
Expand Down

0 comments on commit fe8be58

Please sign in to comment.