Unit TestLib;
{Input: TName.dat, }
{Output:TName.grd }
Interface
  {operations used by checker programs:}
  Function DoTest:Boolean;
  Procedure Done;
  Procedure WriteMSG(st:Integer; M:String);
  Procedure Grade(st, v:Integer);
  Function IntToStr(I : Longint) : String;
{Variables:
TCase:Integer; the nuber of test cases
TInF:Text;     input file of the test case, opened for reading if appropriate
ToutF:text;    output file of the test case, opened for reading if appropriate
CoutF:Text;
}
Var
  TCase:Integer;  	{the nuber of test cases}
  TInF:Text;  		{input file of the test case}
  ToutF:text; 		{output file of the test case}
  CoutF:Text; 		{contestant's output file}

Implementation
Const
  MaxNTc=20;		{max. # test cases}
  MaxNst=4;             {max # subtaks}
  TeX:Array[1..MaxNTc] of Char=('1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k');
Type
  TaskRec=Record
    Name:String;                                      {task name}
    Tlim:Real;                                        {time limit}
    Nst:Integer;                                      {# subtasks}
    NTc:Integer;                                      {# test case}
    BasePoint:Array[1..MaxNst,1..MaxNTc] of Integer;            {base point/testcase}
    GradeValue:Array[1..MaxNst,1..MaxNTc] of Integer; {Filled by checker}
    Point:Array[1..MaxNst,1..MaxNTc] of Integer;      {point/test/subtask}
    MSG:Array[1..MaxNst,1..MaxNTc] of String[80];     {Filled by checker}
    RTime:Array[1..MaxNTc] of Longint;                {Filled by checker}
  End;
Var
  AtC:Integer;	        {actual test case #}
  TcS,                 	{testcase number string}
  TName,            	{task name=ParamStr(2)}
  TInFName,         	{test input file name}
  ToutFName,       	{test output file name}
  CoutFName,  	    	{cont. out file name}
  TimeFName,            {time file name}
  TestDir      :String; {test dir. name=ParamStr(1)}

  Task:TaskRec;         {current task}
  Code:Integer;		{exec code}
  TimeF:File of Longint;

Function IntToStr(I : Longint) : String;
 Var S : String;
 begin
  Str(I,S);
  IntToStr:=S;
 end;

Procedure WriteGrd;
Var
  GFName:String;
  GFile:Text;
  i,j:Integer;
Begin
  GFName:=TName+'.grd';
  Assign(GFile, GFName); Rewrite(GFile);

  For i:=1 To Task.Nst Do Begin
    For j:=1 To TCase Do
      Write(GFile, Task.GradeValue[i,j]:3);
    WriteLn(GFile);
  End{for i};

  For j:=1 To TCase Do Begin
    For i:=1 To Task.Nst Do Begin
      WriteLn(GFile, j:2,'.',i:1,' ',Task.MSG[i,j]);
    End{for i};
  End{for j};

  For j:=1 To TCase Do Begin
    Write(GFile, Task.RTime[j],' ');
  End{for j};
  WriteLn(GFile);

  Close(GFile);

End{WriteGrd};

Procedure CloseAll;
Begin{CloseAll}
  {close all files}
  {$I-}
    Close(TimeF);
  {$I+}
  If IOResult<>0 Then Begin End;

  {$I-}
    Close(CoutF);
  {$I+}
  If IOResult<>0 Then Begin End;

  {$I-}
    Close(TinF);
  {$I+}
  If IOResult<>0 Then Begin End;

  {$I-}
    Close(ToutF);
  {$I+}
  If IOResult<>0 Then Begin End;

End{CloseAll};

Procedure Done;
Begin
  If AtC=TCase Then Begin
    CloseAll;
  End;
  WriteGrd;             {write TName.grd}
  Halt;
End{Done};

Function OpenAll:Boolean;
Var
  CRTime,E:Longint;  {running time in clock ticks}
Begin
  TcS:=TeX[AtC];
{set file names}
  TInFName:=TestDir+'\'+TName+'.be'+TcS;  {test input file name}
  TOutFName:=TestDir+'\'+TName+'.ki'+TcS; {test out file name}
  CoutFName:=TName+'.ki'+TcS;             {cont. out file name}
  TimeFName:=TName+'.st'+TcS;             {time file}

  OpenAll:=False;

{open all files}

   Assign(TimeF, TimeFName);
  {&I-}
    Reset(TimeF);
  {&I+}
  If IOResult<>0 Then Begin
    Writeln('Testlib error: Time file not found');
    Task.MSG[1, AtC]:='TEST ERROR: Time file not found';
    Exit;
  End;
  Read(TimeF, CRTime,E);
  If E<0 Then
    Task.RTime[AtC]:=E
  Else
    Task.RTime[AtC]:=CRtime;		{record running time }
(*
  RunOK:=CRTime<=Task.Tlim;
  If Not RunOK Then Begin
    Task.MSG[1,TestId]:='HIBA: Időlimit túllépés';
    Exit;
  End;
*)
  Assign(CoutF, CoutFName);
  {$I-}
    Reset(CoutF);
  {$I+}
  If IOResult<>0 Then Begin
    Task.MSG[1, AtC]:='HIBA: Nincs output állomány';
    Exit;
  End;

  Assign(TinF, TinFName);
  {$I-}
    Reset(TinF);
  {$I+}
  If IOResult<>0 Then begin
    writeln('Testlib error:Test input not found');
    Task.MSG[1, AtC]:='TEST ERROR: Test input not found';
    Exit;
  End;

  Assign(ToutF, ToutFName);
  {$I-}
    Reset(ToutF);
  {$I+}
  If IOResult<>0 Then begin
    writeln('Testlib error:Test outpout not found');
    Task.MSG[1, AtC]:='TEST ERROR: Test output not found';
    Exit
  End;

  OpenAll:=True;
End{OpenAll};

Procedure WriteMSG(st:Integer; M:String);
Begin
  Task.MSG[st, AtC]:=M;
End{WriteMSG};

Procedure Grade(st, v:Integer);
Begin
  Task.GradeValue[st, AtC]:=v;
End{Grade};

Procedure Init;
Var
  ExeF:File;
  TaskDat:Text;
  i,j,k:Integer;
  ExeFName,XName:String;
  NT:Integer; {# tasks}
  Dummy:TaskRec;
Begin{Init}
  TestDir:=ParamStr(1);
  TName:=ParamStr(2);

  Assign(TaskDat, TestDir+'\verseny.dat');
  {$I-}
  Reset(TaskDat);
  {$I+}
  If IOResult<>0 Then Begin
    Writeln('verseny.dat file does not exist');
    Halt(1);
  End;
  ReadLn(TaskDat, NT);
  For k:=1 To NT Do Begin
    ReadLn(TaskDat, XName);
    If XName=TName Then Begin
      ReadLn(TaskDat, Task.Tlim); {}
      ReadLn(TaskDat, Task.NTc);
      ReadLn(TaskDat, Task.Nst);
      TCase:=Task.NTc;
      For i:=1 To Task.Nst Do Begin
        For j:=1 To Task.NTc Do Begin
          Read(TaskDat, Task.BasePoint[i,j]);
          Task.GradeValue[i,j]:=0;
          Task.MSG[i,j]:='';
          Task.RTime[j]:=-1;
        End{for j};
        ReadLn(TaskDat);
      End{for i}
    End Else Begin
      ReadLn(TaskDat, Dummy.Tlim); {}
      ReadLn(TaskDat, Dummy.NTc);
      ReadLn(TaskDat, Dummy.Nst);

      For i:=1 To Dummy.Nst Do Begin
        For j:=1 To Dummy.NTc Do Begin
          Read(TaskDat, Dummy.BasePoint[i,j]);
          Dummy.GradeValue[i,j]:=0;
          Dummy.MSG[i,j]:='';
        End{for j};
        ReadLn(TaskDat);
      End{for i}
    End{XName<>TName};
  End{for k};

  Close(TaskDat);

  ExeFName:=TName+'.exe';
  Assign(ExeF, ExeFName);
  {$i-}
    Reset(ExeF);
  {$i+}
  If IOResult<>0 Then Begin
{    Writeln('FIGYELEM: nincs EXE  llom ny!');}
    Task.MSG[1,1]:='HIBA: nincs EXE állomány!';
    Done;
  End;
  Close(ExeF);
  AtC:=0;
End{Init};

Function DoTest:Boolean;
Begin{DoTest}
  If AtC>0 Then CloseAll;
  Inc(AtC);
  DoTest:=OpenAll;
End{DoTest};

Begin{Init}
  Init;
End.
