3

I'd like to find a nice and concise way to test intarray

at first I tried

mFaces[0].mIndices shouldBe intArrayOf(0, 1, 2)

where mIndices is

var mIndices: IntArray = IntArray(0)

but fails. Intellij also suggests me to override equals() with Arrays

Then I wanted to try something like this

mFaces[0].mIndices.all { it. == index } shouldBe true

but it looks like there is no way to retrieve the index of it inside all{..} or is this

var p = 0
mFaces[0].mIndices.all { it == p++ } shouldBe true

the only possibility?

elect
  • 6,765
  • 10
  • 53
  • 119

1 Answers1

4

In Java (Kotlin) arrays are compared by reference, not by content. That means that intArrayOf(1, 2, 3) != intArrayOf(1, 2, 3).

To compare content of arrays you have 2 options:

  1. Use deep comparison:

    Arrays.deepequals(mFaces[0].mIndices, intArrayOf(0, 1, 2))

  2. Use lists:

    mFaces[0].mIndices.toList() == listOf(0, 1, 2)

Melquiades
  • 8,496
  • 1
  • 31
  • 46
voddan
  • 31,956
  • 8
  • 77
  • 87
  • Strictly speaking, the equals operator calls the method `equals()` (this is Kotlin so far) which in the case of arrays happens to be implemented as a reference comparison (this is Java's fault). – Michał Kosmulski Nov 19 '16 at 17:33
  • What about `mFaces[0].mIndices.toList().contentEquals(listOf(0, 1, 2))`? – gotube Nov 22 '22 at 21:06