#######################################################
#
# Test parsestringarrayidx(), add some weird indices (including a duplicate)
#
# 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:
      "teststr" str => readfile("$(G.testfile)",1000);
      "cnt" int => parsestringarrayidx("ary", "$(teststr)","NoComment",":",10,1000);
      "num" int => "9";
}

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

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

  classes:
      "ok" 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[1][0])", "1"),
		    strcmp("$(test.ary[1][1])", "2"),
		    strcmp("$(test.ary[1][2])", "3"),

		    strcmp("$(test.ary[2][0])", "here is"),
		    strcmp("$(test.ary[2][1])", "a line"),
		    strcmp("$(test.ary[2][2])", " with spaces "),
		    strcmp("$(test.ary[2][3])", " in it"),

		    strcmp("$(test.ary[3][0])", "blank"),
		    strcmp("$(test.ary[3][1])", "fields"),
		    strcmp("$(test.ary[3][2])", ""),
		    strcmp("$(test.ary[3][3])", ""),
		    strcmp("$(test.ary[3][4])", "in here"),
		    strcmp("$(test.ary[3][5])", ""),
		    strcmp("$(test.ary[3][6])", ""),

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

		    strcmp("$(test.ary[5][0])", "this"),
		    strcmp("$(test.ary[5][1])", "is"),
		    strcmp("$(test.ary[5][2])", "a"),
		    strcmp("$(test.ary[5][3])", "test"),

		    strcmp("$(test.ary[6][0])", "# A duplicate follows"),
		    strcmp("$(test.ary[6][1])", " this line is not always a comment"),

		    strcmp("$(test.ary[7][0])", "this"),
		    strcmp("$(test.ary[7][1])", "also"),

		    strcmp("$(test.ary[8][0])", "last"),
		    strcmp("$(test.ary[8][1])", "one"),
      };

  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[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])'";

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

      "expected test.ary[3][0] = 'blank', saw '$(test.ary[3][0])'";
      "expected test.ary[3][1] = 'fields', saw '$(test.ary[3][1])'";
      "expected test.ary[3][2] = '', saw '$(test.ary[3][2])'";
      "expected test.ary[3][3] = '', saw '$(test.ary[3][3])'";
      "expected test.ary[3][4] = 'in here', saw '$(test.ary[3][4])'";
      "expected test.ary[3][5] = '', saw '$(test.ary[3][5])'";
      "expected test.ary[3][6] = '', saw '$(test.ary[3][6])'";

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

      "expected test.ary[5][0] = 'this', saw '$(test.ary[5][0])'";
      "expected test.ary[5][1] = 'is', saw '$(test.ary[5][1])'";
      "expected test.ary[5][2] = 'a', saw '$(test.ary[5][2])'";
      "expected test.ary[5][3] = 'test', saw '$(test.ary[5][3])'";

      "expected test.ary[6][0] = '# A duplicate follows', saw '$(test.ary[6][0])'";
      "expected test.ary[6][1] = ' this line is not always a comment', saw '$(test.ary[6][1])'";

      "expected test.ary[7][0] = 'this', saw '$(test.ary[7][0])'";
      "expected test.ary[7][1] = 'also', saw '$(test.ary[7][1])'";

      "expected test.ary[8][0] = 'last', saw '$(test.ary[8][0])'";
      "expected test.ary[8][1] = 'one', saw '$(test.ary[8][1])'";

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

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