Skip to content

Commit c3e35de

Browse files
committed
addressing PR comments
1 parent 9110b7b commit c3e35de

File tree

7 files changed

+68
-19
lines changed

7 files changed

+68
-19
lines changed

alleycats-core/src/main/scala/alleycats/ReferentialEq.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import cats.Eq
66
* An `Eq[A]` that delegates to referential equality (`eq`).
77
* Note that it is not referentially transparent!
88
*/
9-
class ReferentialEq[A <: AnyRef] extends Eq[A] {
10-
def eqv(x: A, y: A) = x eq y
9+
trait ReferentialEq[A <: AnyRef] extends Eq[A] {
10+
def eqv(x: A, y: A): Boolean = x eq y
1111
}
1212

1313
object ReferentialEq {
14-
private[this] val referentialEq: Eq[AnyRef] = new ReferentialEq[AnyRef]
14+
private[this] val referentialEq: Eq[AnyRef] = new ReferentialEq[AnyRef] {}
1515

1616
def apply[A <: AnyRef]: Eq[A] = referentialEq.asInstanceOf[Eq[A]]
1717
}

alleycats-core/.jvm/src/main/scala/alleycats/SystemIdentityHash.scala renamed to alleycats-core/src/main/scala/alleycats/SystemIdentityHash.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package alleycats
22

33
import cats.Hash
44

5-
class SystemIdentityHash[A <: AnyRef] extends ReferentialEq[A] with Hash[A] {
5+
trait SystemIdentityHash[A <: AnyRef] extends ReferentialEq[A] with Hash[A] {
66
override def hash(a: A): Int = java.lang.System.identityHashCode(a)
77
}
88

99
object SystemIdentityHash {
10-
private[this] val identityHash: Hash[AnyRef] = new SystemIdentityHash[AnyRef]
10+
private[this] val identityHash: Hash[AnyRef] = new SystemIdentityHash[AnyRef] {}
1111

1212
def apply[A <: AnyRef]: Hash[A] = identityHash.asInstanceOf[Hash[A]]
13-
}
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package alleycats.laws.discipline
2+
3+
import cats.kernel.Eq
4+
import cats.kernel.laws.EqLaws
5+
import cats.kernel.laws.discipline._
6+
import org.scalacheck.Arbitrary
7+
import org.scalacheck.Prop.forAll
8+
import org.typelevel.discipline.Laws
9+
10+
trait ReferentialEqTests[A] extends Laws {
11+
def laws: EqLaws[A]
12+
13+
def eqv(implicit arbA: Arbitrary[A]): RuleSet = {
14+
implicit val eqA: Eq[A] = laws.E
15+
new DefaultRuleSet(
16+
"referentialEq",
17+
None,
18+
"reflexivity eq" -> forAll(laws.reflexivityEq _),
19+
"symmetry eq" -> forAll(laws.symmetryEq _),
20+
"transitivity eq" -> forAll(laws.transitivityEq _)
21+
)
22+
}
23+
}
24+
25+
object ReferentialEqTests {
26+
def apply[A: Eq]: ReferentialEqTests[A] = new ReferentialEqTests[A] {
27+
override def laws: EqLaws[A] = EqLaws[A]
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package alleycats.laws.discipline
2+
3+
import cats.kernel.{Eq, Hash}
4+
import cats.kernel.laws.HashLaws
5+
import cats.kernel.laws.discipline._
6+
import org.scalacheck.Arbitrary
7+
import org.scalacheck.Prop.forAll
8+
9+
import scala.util.hashing.Hashing
10+
11+
trait SystemIdentityHashTests[A] extends ReferentialEqTests[A] {
12+
def laws: HashLaws[A]
13+
14+
def hash(implicit arbA: Arbitrary[A], eqA: Eq[A], hashA: Hashing[A]): RuleSet =
15+
new DefaultRuleSet(
16+
"systemIdentityHash",
17+
Some(eqv),
18+
"hash compatibility" -> forAll(laws.hashCompatibility _),
19+
"same as universal hash" -> forAll(laws.sameAsUniversalHash _),
20+
"same as scala hashing" -> forAll((x: A, y: A) => laws.sameAsScalaHashing(x, y, hashA))
21+
)
22+
}
23+
24+
object SystemIdentityHashTests {
25+
def apply[A: Hash]: SystemIdentityHashTests[A] = new SystemIdentityHashTests[A] {
26+
override def laws: HashLaws[A] = HashLaws[A]
27+
}
28+
}

alleycats-tests/shared/src/test/scala/alleycats/tests/AlleycatsSuite.scala

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import alleycats.std.MapInstances
44
import cats._
55
import cats.instances.all._
66
import org.scalacheck.{Arbitrary, Gen}
7-
import org.scalacheck.Arbitrary.arbitrary
87
import org.scalacheck.Test.Parameters
98

109
/**
@@ -29,11 +28,4 @@ sealed trait TestInstances {
2928
Arbitrary.arbUnit.arbitrary.map(_ => new Object)
3029
)
3130
)
32-
33-
implicit val arbObjectF: Arbitrary[Object => Object] =
34-
Arbitrary {
35-
for {
36-
obj <- arbitrary[Object]
37-
} yield _ => obj
38-
}
3931
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package alleycats.tests
22

33
import alleycats.ReferentialEq
4+
import alleycats.laws.discipline.ReferentialEqTests
45
import cats.kernel.Eq
5-
import cats.kernel.laws.discipline._
66

77
class ReferentialEqSuite extends AlleycatsSuite {
88
implicit val eqObject: Eq[Object] = ReferentialEq[Object]
99

10-
checkAll("ReferentialEq[Object]", EqTests[Object].eqv)
10+
checkAll("ReferentialEq[Object]", ReferentialEqTests[Object].eqv)
1111
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package alleycats.tests
22

33
import alleycats.SystemIdentityHash
4+
import alleycats.laws.discipline.SystemIdentityHashTests
45
import cats.kernel.Hash
5-
import cats.kernel.laws.discipline._
66

77
class SystemIdentityHashSuite extends AlleycatsSuite {
88
implicit val hashObject: Hash[Object] = SystemIdentityHash[Object]
99

10-
checkAll("SystemIdentityHash[Object]", HashTests[Object].hash)
11-
}
10+
checkAll("SystemIdentityHash[Object]", SystemIdentityHashTests[Object].hash)
11+
}

0 commit comments

Comments
 (0)