#######################################################
#
# Test readintarray(), simple
#
# The 5xx tests look at the same thing as their corresponding 4xx tests
# 500-519 are readintarray, 520-539 test the same things for parseintarray
#
#######################################################
body common control
{
  inputs => { "../../default.sub.cf" };
  bundlesequence => { default("$(this.promise_filename)") };
  version => "1.0";
}

bundle agent init
{
  files:
    "$(G.testfile)" delete => init_delete;

    "$(G.testfile)"
      create => "true",
      edit_line => init_fill_in;
}

bundle edit_line init_fill_in
{
  insert_lines:
    "0:1:2
999:888:777:666
1:2:3";
}

body delete init_delete
{
  dirlinks => "delete";
  rmdirs => "true";
}

#######################################################
bundle agent test
{
  vars:
    "cnt"
      int => readintarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000);

    "num" int => "3";
}

#######################################################
bundle agent check
{
  vars:
    "idx" slist => getindices("test.ary");

  classes:
    "good"
      and => {
        strcmp("$(test.num)", "$(test.cnt)"),
        strcmp("$(test.ary[0][0])", "0"),
        strcmp("$(test.ary[0][1])", "1"),
        strcmp("$(test.ary[0][2])", "2"),
        strcmp("$(test.ary[999][0])", "999"),
        strcmp("$(test.ary[999][1])", "888"),
        strcmp("$(test.ary[999][2])", "777"),
        strcmp("$(test.ary[999][3])", "666"),
        strcmp("$(test.ary[1][0])", "1"),
        strcmp("$(test.ary[1][1])", "2"),
        strcmp("$(test.ary[1][2])", "3"),
      };

    # Make sure there are no trailing elements
    "bad"
      or => {
        isvariable("test.ary[0][3]"),
        isvariable("test.ary[999][4]"),
        isvariable("test.ary[1][3]"),
      };

    "ok" expression => "good&!bad";

  reports:
    DEBUG::
      "expected $(test.num) entries, saw $(test.cnt)";
      "saw array indices '$(idx)'";
      "expected test.ary[0][0] = '0', saw '$(test.ary[0][0])'";
      "expected test.ary[0][1] = '1', saw '$(test.ary[0][1])'";
      "expected test.ary[0][2] = '2', saw '$(test.ary[0][2])'";
      "expected test.ary[999][0] = '999', saw '$(test.ary[999][0])'";
      "expected test.ary[999][1] = '888', saw '$(test.ary[999][1])'";
      "expected test.ary[999][2] = '777', saw '$(test.ary[999][2])'";
      "expected test.ary[999][3] = '666', saw '$(test.ary[999][3])'";
      "expected test.ary[1][0] = '1', saw '$(test.ary[1][0])'";
      "expected test.ary[1][1] = '2', saw '$(test.ary[1][1])'";
      "expected test.ary[1][2] = '3', saw '$(test.ary[1][2])'";

    ok::
      "$(this.promise_filename) Pass";

    !ok::
      "$(this.promise_filename) FAIL";
}
