------------------------->  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 MAP_TEST is

   include TEST;

   main is
      map;
      vmap;
   end;
   
   
   map is
      class_name("MAP readonly");
      map0 ::= #MAP{CHAR,INT};
      map1 ::= map0.insert('a',1).insert('b',2).insert('c',3);
      map2 ::= map0.insert('a',1)              .insert('c',3).insert('d',4);
      test_map_readonly(map0,map1,map2);
      finish;
      class_name("MAP modifying");
      test_map(map0,map1,map2);
      finish;
   end;
   
   vmap is
      class_name("VMAP");
      map0 ::= #MAP{CHAR,INT};
      map1 ::= map0.insert('a',1).insert('b',2).insert('c',3);
      map2 ::= map0.insert('a',1)           .insert('c',3).insert('d',4);
      test_map_readonly(map0,map1,map2);
      finish;
   end;
   
   test_map_readonly(map0,map1,map2:$RO_MAP{CHAR,INT}) is
      -- Test readonly functions of maps
      -- map0 is empty
      -- map1 is (a,1),(b,2),(c,3)
      -- map2 is (a,1)       (c,3),(d,4)
      test("map0.size",map0.size,0);
      test("map1.size",map1.size,3);
      test("map1.aget(a)",map1.aget('a'),1);
      test("map1.insert(d,4) equals map2.insert(b,2)",
	   eq(map1.insert('d',4),map2.insert(#TUP{CHAR,INT}('b',2))),
	   true);
      test("map1 = map2",map1.equals(map2),false);
      test("map1.delete(b,2) equals map2.delete(d,4)",
	   eq(map1.delete('b',2),map2.delete(#TUP{CHAR,INT}('d',4))),
	   true);
      test("map1.has_ind(a)",map1.has_ind('a'),true);
      test("map1.delete_ind(a).has_ind(a)",
	   map1.delete_ind('a').has_ind('a'),false);
      test("map1.n_targets(a)",map1.n_targets('a'),1);
      test("map1.delete(a,1).n_targets(a)",
	   map1.delete('a',1).n_targets('a'),0);

      sind:ARRAY{CHAR} := #(map1.size);
      loop sind.set!(map1.ind!); end;
      test("map1.ind!",sind.sort.str,#ARRAY{CHAR}(|'a','b','c'|).str);
      
      target:INT;
      loop target := map1.target!('a') end;
      test("map1.target!",target,1);
      
      test("map1.delete(b,2).is_subset_of(map2)",
	   map1.delete('b',2).is_subset_of(map2), 
	   true);
      test("map1.is_subset_of(map2)",map1.is_subset_of(map2),  false);
      test("map1.add(d,4).add(d,4) equals map2.add(b,2).add(d,4)",
	   eq(map1.add('d',4).add('d',4),  map2.add('b',2).add('d',4)),
	   true);
      test("map1.union(map2)=map2.insert(b,2)",
	   eq(map1.union(map2), map2.insert('b',2)), 
	   true);
      test("map1.intersection(map2)=map1.delete(b,2)",
	   eq(map1.intersection(map2), map1.delete('b',2)), 
	   true);
      test("map1.diff(map2)=map1.delete(a,1).delete(c,3)",
	   eq(map1.diff(map2),map1.delete('a',1).delete('c',3)),
	   true);
      test("map1.sym_diff(map2)=map0.insert(b,2).insert(c,3)",
	   eq(map1.sym_diff(map2), map0.insert('b',2).insert('d',4)),
	   true);
      
   end;

   test_map(map0,map1,map2:$MAP{CHAR,INT}) is
      -- Test modifying functions of maps
      -- map0 is empty
      -- map1 is (a,1),(b,2),(c,3)
      -- map2 is (a,1)       (c,3),(d,4)

      -- map3 is (a,2),(b,2)(c,3),(d,4)
      map3 ::= map0.copy;
      map3['b'] := 2;
      test("map3.insert(b,2)",eq(map3,map0.insert('b',2)),true);
      -- map3 = (b,2)
      map3.to_union(map2);
      -- map3 = (a,1) (b,2) (c,3),(d,4)
      test("map3.to_union(map2)",eq(map3,map2.insert('b',2)),true);
      map3.to_sym_diff(map1);
      -- map3 = (d,4)
      test("map3.to_sym_diff(map1)",eq(map3,map0.insert('d',4)),true);
      map3.to_intersection(map2);
      test("map3.to_instersection(map2)",eq(map3,map0.insert('d',4)),true);
      map3.to_diff(map2);
      -- map3 = empty
      test("map3.to_diff(map2)",eq(map3,map0), true);
   end;
   
   eq(m1,m2:$RO_MULTIMAP{CHAR,INT}):BOOL is
      #OUT+"First="+m1.str+"\nSecond="+m2.str+"\n";
      return m1.equals(m2);
   end;      
end;
-------------------------------------------------------------------
