set_test.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
---------------------------> Sather 1.1 source file <--------------------------
-- test_set.sa: Test for the set class
-- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
-- Copyright (C) 1995, International Computer Science Institute
-- $Id: set_test.sa,v 1.8 1996/07/18 01:00:30 davids 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_SET

class TEST_SET is include TEST; main is e:BINOP_SET_VIEW{INT}; r: SAME := new; r.test_hset; test_abs_set; end; test_abs_set is class_name("AbstractSettests"); s1: H_SET{INT} := H_SET{INT}::create_from(|9,1,3,4|); s2: H_SET{INT} := H_SET{INT}::create_from(|5,1,3,7|); s3 ::= s1.union(s2); sres: SET{INT} := #(#ARRAY{INT}(|9,1,3,4,7,5|)); test("union",sres.equals(s3),true); g1: $SET{INT} := s1; g2: $SET{INT} := s2; g3: $SET{INT} := g1.union(g2); test("union2",sres.equals(g3),true); finish; end; readonly attr a,b,c: $SET{INT}; test_hset is class_name("HSET"); a := #H_SET{INT}; b := #H_SET{INT}; c := #H_SET{INT}; hash_test; local_b ::= b; typecase local_b when H_SET{INT} then test("get",local_b.get(1234).str,INT::nil.str); else raise("Something wierd happened to b in hset test") end; finish; end; print_all is #OUT+"\ta="+a.str+"\n\tb="+b.str+"\n\tc="+c.str+"\n"; end; hash_test is rnd: RND; count,i,limit: INT; s: STR; test( "is_empty 1",b.is_empty.str,true); loop b.insert(0.upto!(10)) end; -- b = 0,1,2,3,4,5,6,7,8,9,10 even_rout:ROUT{INT}:BOOL := bind(_.is_even); odd_rout:ROUT{INT}:BOOL := bind(_.is_odd); b_even ::= #FILTER_SET_VIEW{INT}(b,even_rout); b_odd ::= #FILTER_SET_VIEW{INT}(b,odd_rout); loop a.insert(b_even.elt!); end; -- a = 0,2,4,6,8,10 test("a.size",a.size,6); filter_res: SET{INT} := #SET{INT}(#ARRAY{INT}(|0,2,4,6,8,10|)); test("filter!"+a.str+" - "+filter_res.str, a.str,filter_res.str); -- filter through odd, result is empty fv ::= #FILTER_SET_VIEW{INT}(a,odd_rout); test("some 1", fv.is_empty, true); a_lt_4::= #FILTER_SET_VIEW{INT}(a,bind(_.is_lt(4))); -- Filter through > 4 result is 0,2, not empty test("some 2:"+a_lt_4.str, a_lt_4.is_empty,false); a_lt_ten ::= #FILTER_SET_VIEW{INT}(a,bind(_.is_lt(10))); --Elements of a < 10 a_lt_ten = 0,2,4,6,8 test("forall 1", a_lt_ten.size, 5); --Elements of a that are even = all elements a_is_even ::= #FILTER_SET_VIEW{INT}(a,even_rout); test("forall 2", a_is_even.size,a.size); b := a.copy; -- b = 0,2,4,6,8,10 test("copy", b.str, a.str); b.clear; loop b.insert(5.upto!(15)) end; -- b = 5,6,7,8,9,10,11,12,13,14,15 test("is_eq 1"+a.copy.str, a.equals(a.copy), true); test("is_eq 2", a.equals(b), false); resb: SET{INT} := #SET{INT}(#ARRAY{INT}(|5,6,7,8,9,10,11,12,13,14,15|)); test("b"+b.str,b.equals(resb),true); print_all; resunion: SET{INT} := #SET{INT}(#ARRAY{INT}(|0,2,4,5,6,7,8,9,10,11,12,13,14,15|)); test("a.union(b):"+a.union_view(b).str, a.union_view(b).equals(resunion), true); res_inter ::= #SET{INT}(#ARRAY{INT}(|6,8,10|)); a_in_b ::= a.intersection_view(b); test("a.intersection(b)"+a_in_b.str, a_in_b.equals(res_inter),true); res_diff ::= #SET{INT}(#ARRAY{INT}(|0,2,4|)); test("a.diff(b)"+a.diff_view(b).str, a.diff_view(b).equals(res_diff),true); print_all; test("b subset of a 1", b.is_subset_of(a),false ); print_all; test("subset 2", a_in_b.is_subset_of(b), true); test("is_eq 3", a_in_b.equals(b), false); print_all; d ::= a.sym_diff_view(b); res5: SET{INT} := #SET{INT}(#ARRAY{INT}(|0,2,4,5,7,9,11,12,13,14,15|)); test("a.sym_diff_view(b)"+d.str, d.equals(res5),true); print_all; -- a=0,2,4,6,8,10 -- b=5,6,7,8,9,10,11,12,13,14,15 -- d=0,2,4,5,7,9,11,12,13,14,15 test("disjoint 1", a.intersection_view(d).is_empty, false); -- a intersecting b = 6,8,10 test("disjoint 2", d.intersection_view(a.intersection_view(b)).is_empty, true); loop until!(b.size > 1026 ); b.insert( rnd.int(0,20000) ); end; count := 0; loop dummy ::= b.elt!; count := count + 1 end; test( "size/elt!", b.size.str, "1027" ); b.insert( 1234 ); test( "insert", b.has(1234), true ); b.delete( 1234 ); test( "delete", b.has(1234), false ); test( "is_empty 2",b.is_empty,false); end; end; -- TEST_SET