summaryrefslogtreecommitdiff
path: root/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-array.sh
blob: b8c292ffed5e9bdaaa9d44620824440f06d66389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# shellcheck shell=bash

# Asserts that two arrays are equal, printing out differences if they are not.
# Does not short circuit on the first difference.
assertEqualArray() {
  if (($# != 2)); then
    nixErrorLog "expected two arguments!"
    nixErrorLog "usage: assertEqualArray expectedArrayRef actualArrayRef"
    exit 1
  fi

  local -nr expectedArrayRef="$1"
  local -nr actualArrayRef="$2"

  if ! isDeclaredArray "${!expectedArrayRef}"; then
    nixErrorLog "first argument expectedArrayRef must be a reference to an indexed array"
    exit 1
  fi

  if ! isDeclaredArray "${!actualArrayRef}"; then
    nixErrorLog "second argument actualArrayRef must be a reference to an indexed array"
    exit 1
  fi

  local -ir expectedLength=${#expectedArrayRef[@]}
  local -ir actualLength=${#actualArrayRef[@]}

  local -i hasDiff=0

  if ((expectedLength != actualLength)); then
    nixErrorLog "arrays differ in length: expectedArray has length $expectedLength but actualArray has length $actualLength"
    hasDiff=1
  fi

  local -i idx=0
  local expectedValue
  local actualValue

  # We iterate so long as at least one array has indices we've not considered.
  # This means that `idx` is a valid index to *at least one* of the arrays.
  for ((idx = 0; idx < expectedLength || idx < actualLength; idx++)); do
    # Update values for variables which are still in range/valid.
    if ((idx < expectedLength)); then
      expectedValue="${expectedArrayRef[idx]}"
    fi

    if ((idx < actualLength)); then
      actualValue="${actualArrayRef[idx]}"
    fi

    # Handle comparisons.
    if ((idx >= expectedLength)); then
      nixErrorLog "arrays differ at index $idx: expectedArray has no such index but actualArray has value ${actualValue@Q}"
      hasDiff=1
    elif ((idx >= actualLength)); then
      nixErrorLog "arrays differ at index $idx: expectedArray has value ${expectedValue@Q} but actualArray has no such index"
      hasDiff=1
    elif [[ $expectedValue != "$actualValue" ]]; then
      nixErrorLog "arrays differ at index $idx: expectedArray has value ${expectedValue@Q} but actualArray has value ${actualValue@Q}"
      hasDiff=1
    fi
  done

  ((hasDiff)) && exit 1 || return 0
}