From 61fc27f1e6338622c9e3815fd9e003d9260685df Mon Sep 17 00:00:00 2001 From: Travis Brown Date: Wed, 26 Feb 2020 05:22:20 -0600 Subject: [PATCH 1/2] Add tests for commutativity for BigDecimal operations --- .../scala/cats/kernel/laws/LawTests.scala | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala index f4e8944c40..f4be22e54c 100644 --- a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala @@ -319,6 +319,27 @@ class Tests extends TestsConfig with AnyFunSuiteLike with FunSuiteDiscipline wit 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) checkAll("Band[(Int, Int)]", SerializableTests.serializable(Band[(Int, Int)])) From dc5d48ebe9ab902e1bb7eecb71aa777392bc644b Mon Sep 17 00:00:00 2001 From: Travis Brown Date: Wed, 26 Feb 2020 05:43:59 -0600 Subject: [PATCH 2/2] Remove now-unnecessary brackets --- .../shared/src/test/scala/cats/kernel/laws/LawTests.scala | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala index f4be22e54c..42a5c84ca9 100644 --- a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala @@ -313,11 +313,9 @@ 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