var ar = [1,2,3];

scope {
  assert 3 === ar.length();
  assert 2 === ar.indexOf(3);
  ar.length(2);
  assert 2=== ar.length();
  assert 2===ar[1];
  assert undefined ===ar[2];

  ar.prop = 1;
  ar.clear();
  assert 1===ar.prop;
  assert !ar.length();
  ar.clear(true);
  assert undefined===ar.prop;
}

scope {
  assert ar.isEmpty();
  ar.push(1,2,3);
  assert !ar.isEmpty();
  assert 3 === ar.length();
  var x = 0;
  var eachCallback = proc(v,i){
    //print('in func x=',x,i);
    x = i;
      //print(x, argv, v, i);
  } ;
  ar.eachIndex( eachCallback );
  assert 2 === x;
  eachCallback(0,1);
  assert 1 === x;
}

scope {
  assert 3 === ar.length();
  assert 3 === ar.2;
  assert 3 === ar.pop();
  assert 2 === ar.length();
  assert 1 === ar.shift();
  assert 1 === ar.length();
  assert 2 === ar.0;
  assert 0 === ar.indexOf(2);
  ar.reverse();
  assert 2 === ar.0;
}

scope {
  assert 1 === ar.length();
  assert 2 === ar.0;
  ar.unshift(1,-1);
  assert 3 === ar.length();
  assert 2 === ar.2;
  assert -1 === ar.1;
  assert 1 === ar[0];
  assert '1,-1,2' === ar.join(',');
  assert 3 === ar.setIndex(1,3);
  assert '1,2,3' === ar.sort().join(',');
  assert '3,2,1' === ar.sort(function(l,r){
    // reverse-order (l,h)...
    //l==r && return 0 || l < r && return 1 || return -1;
    // ^^^^ i think that "should" work, but doesn't.
    //l==r && (return 0) || l < r && (return 1) || return -1;
    // ^^^ it turns out keywords as non-RHS-most parts in
    // expressions need () to help them out.
    return l==r ? 0 : l < r ? 1 : -1;
    // The long way:
    l==r && return 0;
    l < r && return 1;
    return -1;
  }).join(',');
  assert '123' === ar.reverse().join('');
  assert ar === ar.unshift(-1);
  assert '-1123' === ar.join('');
}

scope {
  const a1 = [1,2,3];
  assert catch{a1.slice(0,-1)}.codeString() === 'CWAL_RC_RANGE';
  assert catch{a1.slice(-1,0)}.codeString() === 'CWAL_RC_RANGE';

  var a2 = a1.slice(0,1);
  assert 'array' === typename a2;
  assert 1 === a2.length();
  assert 1 === a2.0;

  const a3 = a2.slice(3);
  assert 0 === a3.length();
}

scope {
  const a1 = [1,2,3];
  var a2 = a1.slice(0, 1);
  //print('a2=',a2);
  assert 1=== a2.length();

  a2 = a1.slice(1,1);
  assert 1=== a2.length();
  assert 2 === a2.0;

  a2 = a1.slice(1, 6);
  assert 2=== a2.length();
  assert 3 === a2.1;
}


scope {
    const ar = [0,proc f(){assert f===this; return this},1];
    assert ar.1 === ar.1() /* 'this' is not the array */;
}
