# The following test allows to assess what different types of named
# references refer to during their lifetime depending on where they
# were initialized and whether they were defined with or without the
# "-u" flag.
#
# The test defines the functions "e", "f", "g", "h", "i", "j", and
# "k". The test calls "e" and each function calls the next one. A
# string "s" and an array "a" are defined at the top-level and in each
# function before the call to the next function. The named references
# "rs", "ra", "rs1" and "ra1" are defined at the start of function "g"
# and eventually initialized with respectively "s", "a", "s[1]", and
# "a[1]". Each function "g", "h", "i", "j", and "k" prints the
# expansion of the named references before the local definition of "s"
# and "a", after that definition but before the call to the next
# function and after that call.
#
# Usage: nr-test.zsh (-u|"") <0-12>
#
# The first parameter determines whether the named references are
# defined with or without the flag "-u".
#
# The second parameter determines where the named references are
# initialized. In all cases the named references are defined at the
# start of function "g". With value "0" they are initialized at the
# same place (in the same statement). With the other values, the
# initialization is delayed until later, the greater the value and the
# later the initialization.

function e() {
  local s=$0 a=($0);
  f "$@";
}

function f() {
  local s=$0 a=($0);
  g "$@";
}

function g() {
  if (($2)); then local -n $1 rs ra rs1 ra1; else local -n $1 rs=s ra=a rs1="s[1]" ra1="a[1]"; fi;
  if (($2 == 1)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
  local s=$0 a=($0);
  if (($2 == 2)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:2: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
  h "$@";
  if (($2 == 12)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:3: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
}

function h() {
  if (($2 == 3)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
  local s=$0 a=($0);
  if (($2 == 4)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:2: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
  i "$@";
  if (($2 == 11)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:3: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
}

function i() {
  if (($2 == 5)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
  local s=$0 a=($0);
  if (($2 == 6)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:2: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
  j "$@";
  if (($2 == 10)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:3: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
}

function j() {
  if (($2 == 7)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
  local s=$0 a=($0);
  if (($2 == 8)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:2: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
  k "$@";
  if (($2 == 9)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
  echo "$0:3: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
}

function k() {
  echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
}

(($# == 2)) || { echo "Usage: $ZSH_SCRIPT (-u|\"\") <0-11>" 1>&2; exit 1; }
local s=T a=(T);
e "$@";
