or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array-assertions.mdcore-assertions.mdexception-handling.mdhamcrest-matchers.mdindex.mdtest-assumptions.mdtest-lifecycle.mdtest-runners.md

array-assertions.mddocs/

0

# Array Assertions

1

2

JUnit provides specialized assertion methods for comparing arrays element-by-element. These methods offer detailed failure messages showing exactly which elements differ and support different array types including primitive arrays and object arrays.

3

4

## Object Array Assertions

5

6

```scala { .api }

7

def assertArrayEquals(expecteds: Array[AnyRef], actuals: Array[AnyRef]): Unit

8

def assertArrayEquals(message: String, expecteds: Array[AnyRef], actuals: Array[AnyRef]): Unit

9

```

10

11

**Usage:**

12

```scala

13

val expected = Array("apple", "banana", "cherry")

14

val actual = fruitProcessor.getSortedFruits()

15

16

assertArrayEquals(expected, actual)

17

assertArrayEquals("Fruit sorting failed", expected, actual)

18

```

19

20

## Primitive Array Assertions

21

22

### Boolean Arrays

23

24

```scala { .api }

25

def assertArrayEquals(expecteds: Array[Boolean], actuals: Array[Boolean]): Unit

26

def assertArrayEquals(message: String, expecteds: Array[Boolean], actuals: Array[Boolean]): Unit

27

```

28

29

**Usage:**

30

```scala

31

val expected = Array(true, false, true, false)

32

val actual = validator.validateInputs(Array("valid", "invalid", "valid", "invalid"))

33

34

assertArrayEquals("Validation results mismatch", expected, actual)

35

```

36

37

### Numeric Arrays

38

39

```scala { .api }

40

// Byte arrays

41

def assertArrayEquals(expecteds: Array[Byte], actuals: Array[Byte]): Unit

42

def assertArrayEquals(message: String, expecteds: Array[Byte], actuals: Array[Byte]): Unit

43

44

// Character arrays

45

def assertArrayEquals(expecteds: Array[Char], actuals: Array[Char]): Unit

46

def assertArrayEquals(message: String, expecteds: Array[Char], actuals: Array[Char]): Unit

47

48

// Short arrays

49

def assertArrayEquals(expecteds: Array[Short], actuals: Array[Short]): Unit

50

def assertArrayEquals(message: String, expecteds: Array[Short], actuals: Array[Short]): Unit

51

52

// Integer arrays

53

def assertArrayEquals(expecteds: Array[Int], actuals: Array[Int]): Unit

54

def assertArrayEquals(message: String, expecteds: Array[Int], actuals: Array[Int]): Unit

55

56

// Long arrays

57

def assertArrayEquals(expecteds: Array[Long], actuals: Array[Long]): Unit

58

def assertArrayEquals(message: String, expecteds: Array[Long], actuals: Array[Long]): Unit

59

```

60

61

**Usage Examples:**

62

```scala

63

// Integer array comparison

64

val expectedScores = Array(95, 87, 92, 78)

65

val actualScores = grader.calculateScores(submissions)

66

assertArrayEquals("Score calculation failed", expectedScores, actualScores)

67

68

// Byte array comparison (useful for binary data)

69

val expectedBytes = Array[Byte](0x48, 0x65, 0x6C, 0x6C, 0x6F) // "Hello" in ASCII

70

val actualBytes = encoder.encode("Hello")

71

assertArrayEquals(expectedBytes, actualBytes)

72

73

// Character array comparison

74

val expectedChars = Array('H', 'e', 'l', 'l', 'o')

75

val actualChars = stringProcessor.toCharArray("Hello")

76

assertArrayEquals(expectedChars, actualChars)

77

```

78

79

## Floating-Point Array Assertions

80

81

### Double Arrays with Delta

82

83

```scala { .api }

84

def assertArrayEquals(expecteds: Array[Double], actuals: Array[Double], delta: Double): Unit

85

def assertArrayEquals(message: String, expecteds: Array[Double], actuals: Array[Double], delta: Double): Unit

86

```

87

88

**Usage:**

89

```scala

90

val expectedValues = Array(3.14159, 2.71828, 1.41421)

91

val actualValues = calculator.computeMathConstants()

92

93

assertArrayEquals("Math constants calculation", expectedValues, actualValues, 0.001)

94

```

95

96

### Float Arrays with Delta

97

98

```scala { .api }

99

def assertArrayEquals(expecteds: Array[Float], actuals: Array[Float], delta: Float): Unit

100

def assertArrayEquals(message: String, expecteds: Array[Float], actuals: Array[Float], delta: Float): Unit

101

```

102

103

**Usage:**

104

```scala

105

val expectedCoordinates = Array(1.5f, 2.3f, 4.7f)

106

val actualCoordinates = geometryEngine.calculateCoordinates(shape)

107

108

assertArrayEquals("Coordinate calculation failed", expectedCoordinates, actualCoordinates, 0.01f)

109

```

110

111

## Error Reporting

112

113

Array assertions provide detailed error messages showing:

114

115

1. **Array length differences**:

116

```scala

117

val expected = Array(1, 2, 3)

118

val actual = Array(1, 2)

119

assertArrayEquals(expected, actual)

120

// Error: arrays length differ expected:<3> but was:<2>

121

```

122

123

2. **Element-specific differences**:

124

```scala

125

val expected = Array("a", "b", "c")

126

val actual = Array("a", "x", "c")

127

assertArrayEquals(expected, actual)

128

// Error: arrays first differed at element [1]; expected:<b> but was:<x>

129

```

130

131

3. **Multi-dimensional array support**:

132

```scala

133

val expected = Array(Array(1, 2), Array(3, 4))

134

val actual = Array(Array(1, 2), Array(3, 5))

135

assertArrayEquals(expected, actual)

136

// Error: arrays first differed at element [1][1]; expected:<4> but was:<5>

137

```

138

139

## Complex Array Testing Examples

140

141

### Testing Array Transformations

142

143

```scala

144

class ArrayProcessorTest {

145

@Test

146

def shouldSortIntegerArray(): Unit = {

147

val input = Array(3, 1, 4, 1, 5, 9, 2, 6)

148

val expected = Array(1, 1, 2, 3, 4, 5, 6, 9)

149

val actual = ArrayProcessor.sort(input)

150

151

assertArrayEquals("Array should be sorted in ascending order", expected, actual)

152

}

153

154

@Test

155

def shouldFilterEvenNumbers(): Unit = {

156

val input = Array(1, 2, 3, 4, 5, 6, 7, 8)

157

val expected = Array(2, 4, 6, 8)

158

val actual = ArrayProcessor.filterEven(input)

159

160

assertArrayEquals(expected, actual)

161

}

162

163

@Test

164

def shouldComputeMovingAverage(): Unit = {

165

val input = Array(1.0, 2.0, 3.0, 4.0, 5.0)

166

val expected = Array(1.5, 2.5, 3.5, 4.5) // Window size 2

167

val actual = StatisticsEngine.movingAverage(input, windowSize = 2)

168

169

assertArrayEquals("Moving average calculation", expected, actual, 0.001)

170

}

171

}

172

```

173

174

### Testing Binary Data

175

176

```scala

177

class BinaryDataTest {

178

@Test

179

def shouldEncodeStringToBytes(): Unit = {

180

val input = "Hello, 世界"

181

val expected = Array[Byte](-2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 44, 0, 32, 78, 22, 117, -26)

182

val actual = BinaryEncoder.encodeUTF16(input)

183

184

assertArrayEquals("UTF-16 encoding failed", expected, actual)

185

}

186

187

@Test

188

def shouldCompressData(): Unit = {

189

val originalData = "Hello World! ".repeat(100).getBytes("UTF-8")

190

val compressed = CompressionEngine.compress(originalData)

191

val decompressed = CompressionEngine.decompress(compressed)

192

193

assertArrayEquals("Compression round-trip failed", originalData, decompressed)

194

}

195

}

196

```

197

198

### Testing Matrix Operations

199

200

```scala

201

class MatrixTest {

202

@Test

203

def shouldMultiplyMatrices(): Unit = {

204

val matrixA = Array(

205

Array(1, 2),

206

Array(3, 4)

207

)

208

val matrixB = Array(

209

Array(5, 6),

210

Array(7, 8)

211

)

212

val expected = Array(

213

Array(19, 22),

214

Array(43, 50)

215

)

216

217

val actual = MatrixOperations.multiply(matrixA, matrixB)

218

219

// Compare each row

220

for (i <- expected.indices) {

221

assertArrayEquals(s"Row $i multiplication failed", expected(i), actual(i))

222

}

223

}

224

}

225

```

226

227

## Internal Implementation Details

228

229

Array assertions use specialized comparison criteria:

230

231

```scala { .api }

232

// Internal comparison classes

233

abstract class ComparisonCriteria {

234

def arrayEquals(message: String, expecteds: AnyRef, actuals: AnyRef): Unit

235

protected def assertElementsEqual(expected: AnyRef, actual: AnyRef): Unit

236

}

237

238

class ExactComparisonCriteria extends ComparisonCriteria {

239

// Uses Objects.equals for exact comparison

240

}

241

242

class InexactComparisonCriteria(delta: Double) extends ComparisonCriteria {

243

// Uses delta tolerance for floating-point comparison

244

}

245

```

246

247

## Best Practices

248

249

1. **Use Appropriate Delta**: For floating-point arrays, choose delta values that account for precision limits:

250

```scala

251

// Too strict - may fail due to precision

252

assertArrayEquals(expected, actual, 0.0)

253

254

// Appropriate for most calculations

255

assertArrayEquals(expected, actual, 1e-10)

256

257

// Too loose - may miss real errors

258

assertArrayEquals(expected, actual, 1.0)

259

```

260

261

2. **Test Array Properties**: Consider testing length, ordering, and content separately:

262

```scala

263

// Test length first

264

assertEquals("Array length mismatch", expected.length, actual.length)

265

266

// Then test contents

267

assertArrayEquals("Array contents differ", expected, actual)

268

```

269

270

3. **Handle Null Arrays**: Be explicit about null array handling:

271

```scala

272

// Both null - passes

273

assertArrayEquals(null, null)

274

275

// One null - fails with clear message

276

assertArrayEquals("Expected array", expectedArray, null)

277

```

278

279

4. **Use Descriptive Messages**: Include context about what the array represents:

280

```scala

281

assertArrayEquals("User permission flags", expectedPermissions, actualPermissions)

282

assertArrayEquals("Pixel RGB values", expectedPixels, actualPixels)

283

```

284

285

5. **Consider Collection Assertions**: For more complex array testing, consider converting to collections:

286

```scala

287

import scala.collection.JavaConverters._

288

289

// More flexible collection assertions

290

assertEquals(expectedArray.toList, actualArray.toList)

291

assertThat(actualArray.toList, hasItems(expectedElements: _*))

292

```