PROGRAM test_units (input, output); {-*-Mode: fundamental-mode}
uses
   base,
 { math_random,}
   gpc,
 { gpcutil,}
 { CRT,}
 { strings,}
 { math_random, }
   scanner,
   dictionary
   ;

(*
procedure test_math_random;
var i : integer;
begin
  writeln ('---------- test_math_random ---------');
  write ('random_boolean:  '); for i:= 1 to 30 do write ( ord (random_boolean)       :4);   writeln;
  write ('random_integer:  '); for i:= 1 to 30 do write ( random_integer   (1,     6):4);   writeln;
  write ('random_shortreal:'); for i:= 1 to 30 do write ( random_shortreal (0.0, 3.0):4:1); writeln;
  write ('random_double:   '); for i:= 1 to 30 do write ( random_double    (0.0, 3.0):4:1); writeln;
end;
*)

(*
procedure test_crt; 
var x, y: integer; ch: char;
begin
   TextBackground (LightGray);    TextColor (Black); ClrScr; Sound (440);
   WriteString ('1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0', 20,20);
   ch:= 'x'; x:= 1; y:=1;
   while ch <> ' ' do
    begin
      GotoXY (10, 10);
      ch:= ReadKey;
      WriteString (ch, x, y);
      y:= y+1
    end;
   while KeyPressed do begin end;
end;
*)

(*
procedure test_strings;
var s1, s2, s3, b: string (80); r: real; ts: TimeStamp;
begin
  b:= ''; r:= 9.789;
  s1:='0123456789';
  s2:='abcdefghijklmnop';
  writeln ('s1: ', '''', s1, '''', pad(b, 30-length(s1)), length(s1):3);
  writeln ('s2: ', '''', s2, '''', pad(b, 30-length(s2)), length(s2):3);

  s3:=s1+s2+'xyz';
{ if s1 = s2 then writeln ('s1 = s2') else writeln ('s1 <> s2'); }
  move (s1, s2, 8);
  writeln ('s2: ', '''', s2, '''', pad(b, 30-length(s2)), length(s2):3);
  writeln ('s3: ', '''', s3, '''', pad(b, 30-length(s3)), length(s3):3);
  WriteStr (s1, 1.23:4:2, ' ':5, r:6:3, '-->', s2:-20, '<---');  writeln (s1);
  GetTimeStamp (ts); writeln (time (ts));
end;
*)

(*
procedure test_dictionary;
var lib, lib1: dic_handle_t; index: dic_indx; i, L: integer; s: dic_sym; f : text;
begin
 {writeln        ('begin dictionary');}
  dic_new        (lib, true);
{ if dic_sym2indx(lib, 'abc', index, create) then write (index:2) else write ('E ');
  writeln (length (lib^.root_blk^.blk):3, ' ', lib^.root_blk^.blk);

  if dic_sym2indx(lib, 'xyz', index, create) then write (index:2) else write ('E ');
  writeln (length (lib^.root_blk^.blk):3, ' ', lib^.root_blk^.blk);
}

{
  for i:= 1 to 10000 do
     begin
       s:='';
       for L:=1 to random_integer(1,20) do  s:= S+char (random_integer(65, 122));
       if dic_sym2indx(lib, s, index, create) then else;
     end;
}
  assign (f, 'dummy.parse.symbols');
  reset (f);
  while not eof (f) do
    begin
      readln (f, s);
      if dic_sym2indx(lib, s, index, create) then else;
    end;
{
  writeln;  dic_diagnostic (lib);
  if dic_sym2indx(lib, 'lastsymbol', index, create) then write (index:7) else write ('E      ');
  writeln (' ', dic_symbol (lib, index), '  CPU-Time ', getcputime (i):10, ' ', i:8);
}
  dic_write (lib, 'dummy');

  dic_new   (lib1, true);

  dic_read  (lib1, 'dummy');
  dic_write (lib1, 'dummy1');
{  for index:= 1 to dic_last_indx (lib1) do writeln (dic_symbol (lib, index)); }
  dic_dispose    (lib); 
  dic_dispose    (lib1); 

  dic_new   (lib, true);
  dic_new   (lib1, true);
  dic_write (lib, 'dummy2');
  dic_read  (lib1,'dummy2');
  dic_write (lib1,'dummy3');
 {writeln        ('end dictionary');}
end;
*)

procedure test_scanner;
var
  sh                : scan_handle_t;
  dic1, dic2, dic3  : dic_handle_t;
  s: dic_sym; result: dic_indx; token: token_t;
  f                 : text;
procedure wr_dic (dic : dic_handle_t);
var i: integer;
begin for i:=1 to dic_last_indx (dic) do write (' ', i, '=', dic_symbol(dic, i)); writeln end;

begin
  {writeln ('Scanner');}
  dic_new        (dic1, true);
  dic_new        (dic2, true);
  dic_new        (dic3, true);

  open_scan_new  (sh);
  def_scan_dic   (sh, dic1, [text1_const]);
  def_scan_dic   (sh, dic2, [symbol_const]);
  def_scan_dic   (sh, dic3, [integer_const]);

  def_alphabet   (sh, ' ',
                      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
                      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789',
                      '0123456789',
                      '');

  def_quoting    (sh, '(*', '*)', comment_const);

  def_quoting    (sh, '"',   '"', text1_const);
  def_keyword    (sh, 'begin end and or << >> ; . - +');

{ readln (s);  def_string (sh, s); } {testing one line }
  assign  (f, '/tmp/scanner.test'); rewrite (f);
  writeln (f, '1 2');
  writeln (f, '');  writeln (f, ''); writeln (f, ''); writeln (f, '');
  writeln (f, '>> blabla  end');

  close   (f);

  def_file (sh, '/tmp/scanner.test');
  repeat
    token:= next_token (sh, result);
    case token of
      finish : writeln ('Finish:  ', result:4);
      symbol : writeln ('Symbol:  ', result:4);
      value  : writeln ('Value:   ', result:4, ord (value_type (sh)):3);
      keyword: writeln ('Keyword: ', result:4);
      otherwise abort ('Fatal returned token: '); writeln (ord (token)) end;
    if token=keyword then write_diagn (sh);
    until token=finish;
  write ('dic1 '); wr_dic (dic1);
  write ('dic2 '); wr_dic (dic2);
  write ('dic3 '); wr_dic (dic3);

  scan_close     (sh);
  dic_dispose    (dic1);
  dic_dispose    (dic2);
  dic_dispose    (dic3);
end;

(*
procedure test_diverses;
type color_t=(red, green, blue);
var i, n, ms: integer; a, b, c: longestreal;
    color: color_t;
begin
   ms:= 0;
 { for n:=1 to 10 do begin writeln (GetUnixTime (ms), ms:10); sleep (1) end; }
 { i:= system ('ls');}
 { writeln ('CPU time ', GetCPUTime (ms), ms:10, ' ', GetCurrentDirectory);}
 { writeln ('TMP file name: ', GetTempFileName);}
 { writeln (FileExists      ('test_units')); }
 { writeln (DirectoryExists ('pascal')); }
 { writeln ('Pi:  ', pi:20:18);}
 { a:= 1.0; b:= 0.0000000000000000001; c:= a+b;}
 { writeln (a:25:20);}
 { writeln (b:25:20);}
 { writeln (c:25:20);}
 { color:= blue; writeln (color); } {dosn't work}
 { writeln (Arctan2 ( 1.0,  1.0)*180.0/pi:20:15, pi:20:15); }
 { writeln (Arctan2 ( 0.0, -1.0)*180.0/pi:20:15); }
 { sleep (5); }
end;
*)

BEGIN
  { writeln ('Begin of test_units ');}
  { test_math_random; }
  { test_crt; }
  { test_strings; }
  { test_dictionary; }
    test_scanner;
  { test_diverses; }
  { writeln ('End of test_units'); }
END.
