#######################################################
#
# Test readstringarray(), change separator to 's'
#
# The 4xx tests are all related, and 400-419 are the readstringarray tests,
# 420-439 the same for readstringarrayidx, 440-459 parsestringarray, and
# 460-479 parsestringarrayidx
#
#######################################################

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
1:2:3
here is:a line: with spaces : in it
blank:fields:::in here::
:leading blank field
this:is:a:test
# A duplicate follows: this line is not always a comment
this:also
last:one";

}

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

#######################################################

bundle agent test
{
  vars:
      "cnt" int => readstringarray("ary", "$(G.testfile)","^#.*","s+",10,1000);
      "num" int => "7";
}

#######################################################

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

  classes:
      "ok" and => {   
		    strcmp("$(test.num)", "$(test.cnt)"),

		    strcmp("$(test.ary[0:1:2][0])", "0:1:2"),

		    strcmp("$(test.ary[1:2:3][0])", "1:2:3"),

		    strcmp("$(test.ary[here i][0])", "here i"),
		    strcmp("$(test.ary[here i][1])", ":a line: with space"),
		    strcmp("$(test.ary[here i][2])", " : in it"),

		    strcmp("$(test.ary[blank field][0])", "blank field"),
		    strcmp("$(test.ary[blank field][2])", "::: in here::"),

		    strcmp("$(test.ary[:leading blank field][0])", "leading blank field"),

		    strcmp("$(test.ary[thi][0])", "thi"),
		    strcmp("$(test.ary[thi][1])", ":also"),
		    strcmp("$(test.ary[thi][2])", "$(const.dollar)test.ary[this][2]"),
		    strcmp("$(test.ary[thi][3])", "$(const.dollar)test.ary[this][3]"),

		    strcmp("$(test.ary[la][0])", "la"),
		    strcmp("$(test.ary[la][1])", "t:one"),
      };

  reports:
    DEBUG::
      "expected $(test.num) entries, saw $(test.cnt)";

      "saw array indices '$(idx)'";

      "expected test.ary[0:1:2][0] = '0:1:2', saw '$(test.ary[0:1:2][0])'";

      "expected test.ary[1:2:3][0] = '1:2:3', saw '$(test.ary[1:2:3][0])'";

      "expected test.ary[here i][0] = 'here i', saw '$(test.ary[here i][0])'";
      "expected test.ary[here i][1] = ':a line: with space', saw '$(test.ary[here i][1])'";
      "expected test.ary[here i][2] = ' : in it', saw '$(test.ary[here i][2])'";

      "expected test.ary[blank field][0] = 'blank field', saw '$(test.ary[blank field][0])'";
      "expected test.ary[blank field][2] = '::: in here::', saw '$(test.ary[blank field][2])'";

      "expected test.ary[:leading blank field][0] = 'leading blank field', saw '$(test.ary[:leading blank field][0])'";

      "expected test.ary[thi][0] = 'thi', saw '$(test.ary[thi][0])'";
      "expected test.ary[thi][1]) = ':also', saw '$(test.ary[thi][1]))'";
      "expected test.ary[thi][2]) = '$(const.dollar)test.ary[this][2]', saw '$(test.ary[thi][2]))'";
      "expected test.ary[thi][3]) = '$(const.dollar)test.ary[this][3]', saw '$(test.ary[thi][3]))'";

      "expected test.ary[la][0]) = 'la', saw '$(test.ary[la][0]))'";
      "expected test.ary[la][1]) = 't:one', saw '$(test.ary[la][1]))'";

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

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