xref: /inferno-os/appl/spree/lib/testsets.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1implement Testsets;
2include "sys.m";
3	sys: Sys;
4include "draw.m";
5include "rand.m";
6include "sets.m";		# "sets.m" or "sets32.m"
7	sets: Sets;
8	Set, set, A, B: import sets;
9
10BPW: con 32;
11SHIFT: con 5;
12MASK: con 31;
13
14Testsets: module {
15	init: fn(nil: ref Draw->Context, nil: list of string);
16};
17
18∅: Set;
19
20Testbig: con 1;
21
22init(ctxt: ref Draw->Context, argv: list of string)
23{
24	sys = load Sys Sys->PATH;
25	sets = load Sets Sets->PATH;
26	if (sets == nil) {
27		sys->print("cannot load %s: %r\n", Sets->PATH);
28		exit;
29	}
30	rand := load Rand Rand->PATH;
31	sets->init();
32
33	∅ = set();
34	s := set().addlist(1::2::3::4::nil);
35	addit(s);
36	sys->print("s %s\n", s.str());
37	r := s.invert();
38	sys->print("r %s\n", r.str());
39	r = r.del(20);
40	addit(r);
41	sys->print("r del20: %s\n", r.str());
42	z := r.X(~A&~B, s);
43	addit(z);
44	sys->print("z: %s\n", z.str());
45
46	x := set();
47	for (i := 0; i < 31; i++)
48		if (rand->rand(2))
49			x = x.add(i);
50	addit(x);
51	for(i = 0; i < 31; i++)
52		addit(set().add(i));
53	if (Testbig) {
54		r = r.del(100);
55		addit(r);
56		sys->print("rz: %s\n", r.str());
57		r = r.add(100);
58		addit(r);
59		sys->print("rz2: %s\n", r.str());
60		x = set();
61		for (i = 0; i < 200; i++)
62			x = x.add(rand->rand(300));
63		addit(x);
64		for(i = 31; i < 70; i++)
65			addit(set().add(i));
66	}
67	sys->print("empty: %s\n", set().str());
68	addit(set());
69	sys->print("full: %s\n", set().invert().str());
70	test();
71	sys->print("done tests\n");
72}
73
74ds(d: array of byte): string
75{
76	s := "";
77	for(i := len d - 1; i >= 0; i--)
78		s += sys->sprint("%.2x", int d[i]);
79	return s;
80}
81
82testsets: list of Set;
83addit(s: Set)
84{
85	testsets = s :: testsets;
86}
87
88test()
89{
90	for (t := testsets; t != nil; t = tl t)
91		testsets = (hd t).invert() :: testsets;
92
93	for (t = testsets; t != nil; t = tl t)
94		testa(hd t);
95	for (t = testsets; t != nil; t = tl t) {
96		a := hd t;
97		for (s := testsets; s != nil; s = tl s) {
98			b := hd s;
99			testab(a, b);
100		}
101	}
102}
103
104testab(a, b: Set)
105{
106	{
107		check(!a.eq(b) == !b.eq(a), "equality");
108		if (superset(a, b) && !a.eq(b))
109			check(!superset(b, a), "superset");
110	} exception {
111	"test failed" =>
112		sys->print("%s, %s [%s, %s]\n", a.str(), b.str(), a.debugstr(), b.debugstr());
113	}
114}
115
116testa(a: Set)
117{
118	{
119		check(sets->str2set(a.str()).eq(a), "string conversion");
120		check(a.eq(a), "self equality");
121		check(a.eq(a.invert().invert()), "double inversion");
122		check(a.X(A&~B, a).eq(∅), "self not intersect");
123		check(a.limit() == a.invert().limit(), "invert limit");
124		check(a.X(A&~B, set().invert()).limit() == 0, "zero limit");
125		check(sets->bytes2set(a.bytes(0)).eq(a), "bytes conversion");
126		check(sets->bytes2set(a.bytes(3)).eq(a), "bytes conversion(2)");
127
128		if (a.limit() > 0) {
129			if (a.msb())
130				check(!a.holds(a.limit() - 1), "hold limit 1");
131			else
132				check(a.holds(a.limit() - 1), "hold limit 2");
133		}
134	} exception {
135	"test failed" =>
136		sys->print("%s [%s]\n", a.str(), a.debugstr());
137	}
138}
139
140check(ok: int, s: string)
141{
142	if (!ok) {
143		sys->print("test failed: %s; ", s);
144		raise "test failed";
145	}
146}
147
148# return true if a is a superset of b
149superset(a, b: Set): int
150{
151	return a.X(~A&B, b).eq(∅);
152}
153