fset_test.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
---------------------------> Sather 1.1 source file <--------------------------
-- test_ffset.sa:
-- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
-- Copyright (C) 1995, International Computer Science Institute
-- $Id: fset_test.sa,v 1.3 1996/04/09 10:05:02 borisv Exp $
--
-- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
-- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
-- LICENSE contained in the file: Sather/Doc/License of the
-- Sather distribution. The license is also available from ICSI,
-- 1947 Center St., Suite 600, Berkeley CA 94704, USA.
class TEST_FSET
class TEST_FSET 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;