Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specific commutativity and associativity tests for BigDecimal #3325

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,29 @@ class Tests extends TestsConfig with AnyFunSuiteLike with FunSuiteDiscipline wit
checkAll("Hash[SortedMap[Int, String]]", HashTests[SortedMap[Int, String]].hash)
checkAll("Hash[Queue[Int]", HashTests[Queue[Int]].hash)

{
checkAll("Order[BigDecimal]", OrderTests[BigDecimal].order)
checkAll("CommutativeGroup[BigDecimal]", CommutativeGroupTests[BigDecimal].commutativeGroup)
checkAll("CommutativeGroup[BigDecimal]", SerializableTests.serializable(CommutativeGroup[BigDecimal]))
checkAll("Order[BigDecimal]", OrderTests[BigDecimal].order)
checkAll("CommutativeGroup[BigDecimal]", CommutativeGroupTests[BigDecimal].commutativeGroup)
checkAll("CommutativeGroup[BigDecimal]", SerializableTests.serializable(CommutativeGroup[BigDecimal]))

test("CommutativeGroup[BigDecimal]'s combine should be associative for known problematic cases (#3303)") {
import java.math.MathContext

val one = BigDecimal("1", MathContext.DECIMAL32)
val small = BigDecimal("0.00001111111", MathContext.DECIMAL32)
val xs = one :: List.fill(10)(small)
val combineRight = xs.reduceRight(CommutativeGroup[BigDecimal].combine)
val combineLeft = xs.reduceLeft(CommutativeGroup[BigDecimal].combine)

assert(combineRight === combineLeft)
}

test("CommutativeGroup[BigDecimal]'s combine should be commutative for known problematic cases (#3303)") {
import java.math.MathContext

val one = BigDecimal("1")
val small = BigDecimal("1e-7", MathContext.DECIMAL32)

assert(CommutativeGroup[BigDecimal].combine(one, small) === CommutativeGroup[BigDecimal].combine(small, one))
}

checkAll("Band[(Int, Int)]", BandTests[(Int, Int)].band)
Expand Down