# test readfile()

body common control
{
      inputs => { "../../default.sub.cf" };
      bundlesequence => { default("$(this.promise_filename)") };
      version => "1.0";
}

bundle agent init
{
}

bundle agent test
{
  vars:
      "sizes" ilist => { "0", "1", "4094", "4095", "4096", "4097", "99999999" };
      "read_$(sizes)" string => readfile("$(this.promise_filename).txt",
                                         $(sizes));
      "length_$(sizes)" int => string_length("$(read_$(sizes))");

      "read_no_size" string => readfile("$(this.promise_filename).txt");
      "length_no_size"  int => string_length("$(read_no_size)");
}

bundle agent check
{
  vars:
      "sizes" ilist => { @(test.sizes) };
      "expected[0]" int => "10240";
      "expected[1]" int => "1";
      "expected[4094]" int => "4094";
      "expected[4095]" int => "4095";
      "expected[4096]" int => "4096";
      "expected[4097]" int => "4097";
      "expected[99999999]" int => "10240";
      "expected_no_size"   int => "10240";

  classes:
      "ok_$(sizes)" expression => strcmp("$(test.length_$(sizes))",
                                         "$(expected[$(sizes)])");
      "ok_no_size"  expression => strcmp("$(test.length_no_size)",
                                         "$(expected_no_size)");

      "ok" and => {
                    "ok_0",
                    "ok_1",
                    "ok_4094",
                    "ok_4095",
                    "ok_4096",
                    "ok_4097",
                    "ok_99999999",
                    "ok_no_size",
      };

  reports:
    DEBUG::
      "Got expected size for read of $(sizes): $(test.length_$(sizes))"
      if => "ok_$(sizes)";
      "Did NOT get expected size for read of $(sizes): $(test.length_$(sizes)) != expected $(expected[$(sizes)])"
      if => "!ok_$(sizes)";

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