------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 1995 by International Computer Science Institute            --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute  and/or modify it under the terms of the GNU Library General --
-- Public  License (LGPL)  as published  by the  Free  Software  Foundation; --
-- either version 2 of the license, or (at your option) any later version.   --
-- This  library  is distributed  in the  hope that it will  be  useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details.       --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------

-- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>

class FSET_TEST is
   include TEST;
   
   main is
      class_name("FSET");
      a ::= #FSET{INT}(4);
      b ::= #FSET{INT}(4);
      c ::= #FSET{INT}(4);
      test("is_empty",a.is_empty,true);
      a := a.insert(1).insert(5).insert(7).insert(9);
      test("test 1",a.has(5),true);
      test("test 2",a.has(11),false);
      test("is_empty",a.is_empty,false);
      a := a.delete(5);
      test("delete",a.has(5),false);
      test("delete",a.has(1),true);
      b := b.insert(7).insert(9).insert(11);
      test("instersect",a.intersects(b),true);
      test("stersect",a.intersects(b),true);
      f ::= #FSET{INT}(10);
      f := f.insert(1).insert(2).insert(3).insert(4);
      g ::= #FSET{INT}(5);
      g := g.insert(1).insert(2).insert(3).insert(4);
      test("union",g.union(f).str,g.str);
      test("union",g.union(f).str,f.str);
      f := f.insert(11);
      g := g.to_union(f);
      test("to_union",g.str,f.str);
      d ::= #FSET{INT}(15);
      arr: ARRAY{INT} := #(100);
      loop arr.set!(RND::int(0,500)); end;
      loop  d := d.insert(arr.elt!); end;
      loop i ::= 10.times!;
	 test("contains",d.has(arr.elt!),true);
      end;
      loop i ::= 20.times!; 
	 loop j ::= 20.times!;   test_size(i,j); end;
      end;
      finish;
   end;

   test_size(sz1,sz2: INT) is
      -- Test against the behaviour of FSET which is assumed to be correct.
      a ::= #FSET{INT};
      b ::= #FSET{INT};
      ra ::= #ARRAY{INT}(sz1);
      rb ::= #ARRAY{INT}(sz2);
      loop ra.set!(RND::int(0,sz1)); end;
      loop rb.set!(RND::int(0,sz2)); end;
      loop a := a.insert(ra.elt!) end;
      loop b := b.insert(rb.elt!) end;
      fa ::= #FSET{INT};
      fb ::= #FSET{INT};
      loop fa := fa.insert(ra.elt!) end;
      loop fb := fb.insert(rb.elt!) end;
      aau_arr: ARRAY{INT} := sorted_arr(a.union(a));
      a_arr: ARRAY{INT} := sorted_arr(a);
      test("union",str(aau_arr),str(a_arr));
      abu_arr: ARRAY{INT} := sorted_arr(a.union(b));
      fabu_arr: ARRAY{INT} := sorted_arr(fa.union(fb));
      test("union",str(abu_arr),str(fabu_arr));
      abi_arr: ARRAY{INT} := sorted_arr(a.intersect(b));
      fabi_arr: ARRAY{INT} := sorted_arr(fa.intersect(fb));
      test("intersection",str(abi_arr),str(fabi_arr));
   end;
   
   sorted_arr(u: FSET{INT}): ARRAY{INT} is
      res ::= #ARRAY{INT}(u.size);
      loop res.set!(u.elt!) end;
      res.sort;
      return res;
   end;

   str(a: ARRAY{INT}): STR is
      res ::= "{";
      loop res := res + ",".separate!(a.elt!.str) end;
      res := res +"}";
      return res;
   end;
end;
-------------------------------------------------------------------   
