0

I'm trying to upgrade our code from scala 2.12 to scala 2.13. Although I've made it compile, it fails a very small number of test cases (30 tests out of ~ 10k tests) that the 2.12 code didn't fail.

We make heavy use of Set and Map which have a different implementation in 2.13 vs 2.12. I suspect that the problem is an ordering issue; it's possible that the code depends on the order those containers are accessed when it shouldn't (there are no guarantees for that order in scala and therefore no obligation to maintain it between versions) e.g this piece of code:

object SetExample extends App {
  // Create a Set of strings
  val fruitSet = Set("apple", "banana", "cherry", "date", "elderberry", "pear", "watermellon", "strawberry")

  // Print each string in the Set
  fruitSet.foreach(println)
}

will return this in 2.12.14:

banana
watermellon
date
cherry
apple
pear
elderberry
strawberry

and this in 2.13.10:

banana
watermellon
apple
pear
elderberry
strawberry
date
cherry

I want to prove (or disprove) the hypothesis that this is an ordering issue.

There are migration tools from 2.12 to 2.13 where I assume I could gradually apply migration rules (and that includes containers) until I hit the problem, see here:

https://github.com/scala/scala-collection-compat/blob/main/scalafix/rules/src/main/scala/scala/fix/collection/CanBuildFrom.scala

Also here: https://docs.scala-lang.org/overviews/core/collections-migration-213.html

Are there other ways I could approach this problem?

There is no way I'm aware of to use 2.12 container implementations in 2.13.

burnedWood
  • 93
  • 1
  • 9
  • 3
    You can't rely on the order of a Set by default - see https://stackoverflow.com/questions/5245713/scala-can-i-rely-on-the-order-of-items-in-a-set - so the point is largely moot (neither example shows the Set in the order you inserted the items anyway). As the linked answer suggests, if you need to preserve order you can use LinkedHashSet though. – James Whiteley Jul 13 '23 at 10:55
  • If it's purely a testing issue, you can compare the Sets without caring about order a couple of ways - see the answers on https://stackoverflow.com/questions/3622895/is-there-an-api-method-that-compares-contents-of-a-seq-irrespective-of-order – James Whiteley Jul 13 '23 at 10:58
  • Could you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) where a test works in `2.12` but not in `2.13`? [Set](https://docs.scala-lang.org/overviews/collections-2.13/sets.html) is an interface with different implementations. Not all of them sort the elements. Same for `Map` – Gastón Schabas Jul 13 '23 at 11:54
  • I can confirm that it _is_ indeed an ordering issue. I have had a similar problem when we upgraded to 2.13. The traversal order of sets and maps is different than it was in 2.12 – Dima Jul 13 '23 at 12:49
  • 1
    Also, it is not clear what you are asking exactly: `I want to prove (or disprove) the hypothesis that this is an ordering issue.` - it seems like your example (running the same code in 2.12 and 2.13 and producing the results in different order) _is_ proving that very hypothesis. What else do you want to "prove or disprove" here? – Dima Jul 13 '23 at 12:51
  • @Dima, the tests fail but I don't know why. They don't "produce results in different order", they simply fail. The hypothesis is it's because of the different order of access and that's what I'm trying to test. It could be anything else. – burnedWood Jul 13 '23 at 13:56
  • 3
    How about a crazy idea: look at (one of) the tests and figure out why they actually fail? :D – Dima Jul 13 '23 at 14:04
  • without a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) is hard to help you. edit your post and try to provide what is the code that is failing – Gastón Schabas Jul 13 '23 at 16:48
  • You haven't written _what_ failed in runtime. Without that information we can at best nod that you proven that order _might be_ the issue if tests look similar to example code, but it might be also anything else: bug in some cross-compiled library, runtime reflection access, etc. – Mateusz Kubuszok Jul 13 '23 at 17:02

1 Answers1

0

As @Dima suggests, look at one of the failing tests and see why it is failing. If you think that ordering is an issue then add a sort on the results and see if the problem goes away.

If none of the failing tests allow you to work out the root cause then you need to add some more fine-grained tests that will.

Tim
  • 26,753
  • 2
  • 16
  • 29