aa
TestType.java
Go to the documentation of this file.
1 package com.cliffc.aa.type;
2 
3 import com.cliffc.aa.node.PrimNode;
4 import com.cliffc.aa.util.Ary;
5 import org.junit.Test;
6 
7 import java.util.HashMap;
8 
9 import static com.cliffc.aa.type.TypeMemPtr.NO_DISP;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 
13 
14 public class TestType {
15  // temp/junk holder for "instant" junits, when debugged moved into other tests
16  @Test public void testType() {
17 
18  TypeFld vi = TypeFld.make("v",TypeInt.INT64,2);
21  TypeStruct a2 = a1.approx(1,9);
22 
23  System.out.println(a1);
24  }
25 
26  @Test public void testBits0() {
27 
28  // This looks like a crossing-nil bug, but it is not quite.
29  // What is "nil join *[4]->str"?
30  // Inverting, what is "nil meet *[~4]->str"?
31  // The issue is that 'nil' says nothing about what to do with the '->str'
32  // part of the structural meet breakdown. See testLattice14 & 15.
33  //
34  // Tested as Lattice: [0]-> obj ==> NIL ==> [~0]->~obj
35  //
36  // If NIL is 'signed', and effectively either '[0]->obj' or '[~0]->~obj'
37  // then this test works. Either [0]->obj:
38  // If "[0]-> obj is [0,2]->rec" Then A^C isa B^C. Fails, !A->B
39  // If "[0]->~obj is [0,2]->rec" Then A^C isa B^C. Works:
40  // [0 ]->~obj ^ [4]->str => [~0+4]->~obj
41  // [0,2]-> rec ^ [4]->str => [~0+4]->~obj
42  //
43  // nil.isa(*rec?) so nil.join(*str) isa (*rec?).join(*str)
44  Type t0 = Type.XNIL; // [0 ] -> obj
45  Type t1 = TypeMemPtr.STRUCT0;// [0,2] -> rec
46  assertTrue(t0.isa(t1)); // [0,2] -> obj -- meet is not t1
47  Type t2 = TypeMemPtr.OOP0; // [0,2] -> obj
48  assertTrue(t0.isa(t2)); //
49 
50  }
51 
52  // Want Bits to support meet/join of fidxs for Unresolved:
53  // 4.join.5.join.6 == {+4+5+6}
54  // 4.meet.5.meet.6 == { 4&5&6}
55  // Need to support 'splitting' bits for inlining, so all these bits are
56  // *classes* not *constants. Hence there is no '4' bit, only '+4' and '&4'.
57  // Need the basic invariants: complete, distributed, commutative, associative.
58  @Test public void testBits() {
59  // 1 - obj
60  // 2 - tuples & structs
61  // 3 - arrays
62  // 4 - strings
63  // 5 - "abc"
64 
65  // Distributivity
66  // t0 = *[5] -> "abc"
67  // t1 = *[4] -> str
68  // t2 = *[2] -> ()
69  // Since t0.isa(t1) then t0.join(t2) .isa( t1.join(t2) )
73  assertTrue(t0.isa(t1));
74  Type t02,t12,mt;
75 
76  t02 = t0.join(t2); // join of unrelated bits2&5 []
77  t12 = t1.join(t2); // join of unrelated bits2&4 []
78  mt = t02.meet(t12);
79  assertEquals(t12,mt);
80  // also: (t0.meet(t1)).join(t2) == mt
81  // but since t0 isa t1,
82  // also: t1.join(t2) == mt
83  // [4] join [2] ==> [~2 + ~4]
84 
85  t1 = TypeMemPtr.STR0; // Same with nil
86  t02 = t0.join(t2); // join of unrelated bits2&5 []
87  t12 = t1.join(t2); // join of unrelated bits2&4 []
88  mt = t02.meet(t12);
89  assertEquals(t12,mt);
90  }
91 
92  @Test public void testNamesInts() {
93 
94  // Lattice around int8 and 0 is well formed; exactly 3 edges, 3 nodes
95  // Confirm lattice: {~i16 -> ~i8 -> 0 -> i8 -> i16 }
96  // Confirm lattice: { ~i8 -> 1 -> i8 }
97  Type i16= TypeInt.INT16;
98  Type i8 = TypeInt.INT8;
99  Type xi8 = i8.dual();
100  Type xi16= i16.dual();
101  Type z = TypeInt.FALSE;
102  Type o = TypeInt.TRUE;
103  assertEquals(xi8,xi8.meet(xi16)); // ~i16-> ~i8
104  assertEquals( z ,z .meet(xi8 )); // ~i8 -> 0
105  assertEquals(i8 ,i8 .meet(z )); // 0 -> i8
106  assertEquals(i16,i16.meet(xi8 )); // i8 -> i16
107  assertEquals( o ,o .meet(xi8 )); // ~i8 -> 1
108  assertEquals(i8 ,i8 .meet(o )); // 1 -> i8
109 
110  // Lattice around n:int8 and n:0 is well formed; exactly 2 edges, 3 nodes
111  // Confirm lattice: {N:~i8 -> N:1 -> N:i8}
112  // Confirm lattice: {N:~i8 -> N:0 -> N:i8}
113  Type ni8 = TypeInt.INT8.set_name("__test_enum:");
114  Type xni8= ni8.dual(); // dual name:int8
115  Type no = o.set_name("__test_enum:");
116  Type nz = z.set_name("__test_enum:");
117  assertEquals(no ,no .meet(xni8)); // N:~i8 -> N: 1
118  assertEquals(ni8,ni8.meet(no )); // N: 1 -> N:i8
119  assertEquals(nz ,nz .meet(xni8)); // N:~i8 -> N:0
120  assertEquals(ni8,ni8.meet(nz )); // N:0 -> N:i8
121  assertEquals(no ,no .meet(xni8)); // n:1 & n:~i8 -> mixing 0 and 1
122 
123  // Crossing lattice between named and unnamed ints
124  // Confirm lattice: {~i8 -> N:~i8 -> 0 -> N:i8 -> i8; N:0 -> 0 }
125  // NOT: Confirm lattice: {N:~i8 -> ~i8; N:i8 -> i8 }
126  assertEquals(xni8,xni8.meet( xi8));// ~i8 -> N:~i8
127  assertEquals( z, z .meet(xni8));// N:~i8 -> {0,1}??? When falling off from a Named Int, must fall below ANY constant to keep a true lattice
128  assertEquals( i8, ni8.meet( z));// 0 -> N:i8
129  assertEquals( i8, i8.meet( ni8));// N: i8 -> i8
130  assertEquals( z, z.meet( nz));// N: 0 -> 0
131 
132  assertEquals(xni8,xi8.meet(xni8)); // N:~i8 <- ~i8
133  assertEquals(o, o .meet(xni8)); // 1 & N:~i8
134  }
135 
136  // Memory is on a different line than pointers.
137  // Memory canNOT be nil, but ptrs to memory can be nil.
138  // Memory contents can be any/all, vs ptrs being any/all.
139  // A any-ptr-alias#6 means *any* of the alias#6 choices; same for all.
140  // all -> mem -> { str,tup} -> { string constants, tuple constants} -> {~str,~tup} -> ~mem -> any
141  // all -> *mem? -> {*mem,nil} -> {*str,*tup,nil} -> {~*str,~*tup,nil} -> {~*mem,nil} -> ~*mem+? -> any
142  @Test public void testOOPsNulls() {
143  Type bot = Type .ALL;
144 
145  Type mem = TypeMem .MEM; // All memory
146  Type str = TypeStr .STR; // All Strings
147  Type tup = TypeStruct.ALLSTRUCT; // All Structs
148 
149  Type abc = TypeStr .ABC; // String constant
150  Type zer = TypeInt .FALSE;
151  Type tp0 = TypeStruct.make(zer); // tuple of a '0'
152 
153  Type tupX= tup.dual();
154  Type strX= str.dual();
155  Type memX= mem.dual();
156 
157  Type top = Type.ANY;
158 
159  assertTrue( top.isa(memX));
160 
161  //assertTrue(memX.isa(strX)); // mem is a CONTAINER for memory objects, e.g. Struct,Str
162  //assertTrue(memX.isa(tupX));
163 
164  assertTrue( strX.isa(abc));
165  assertTrue( tupX.isa(tp0));
166  assertTrue(!strX.isa(zer));
167  assertTrue(!tupX.isa(zer));
168 
169  assertTrue( abc .isa(str));
170  assertTrue( tp0 .isa(tup));
171  assertTrue(!zer .isa(str));
172  assertTrue(!zer .isa(tup));
173 
174  //assertTrue( str .isa(mem)); // mem is a CONTAINER for memory objects, e.g. Struct,Str
175  //assertTrue( tup .isa(mem));
176 
177  assertTrue( mem .isa(bot));
178 
179  // ---
180  Type pmem0= TypeMemPtr.OOP0; // *[ALL]?
181  Type pmem = TypeMemPtr.OOP ; // *[ALL]
182  Type pstr0= TypeMemPtr.STR0; // *[str]?
183  TypeMemPtr pstr = TypeMemPtr.STRPTR; // *[str]
184  Type ptup0= TypeMemPtr.STRUCT0; // *[tup]?
185  Type ptup = TypeMemPtr.STRUCT; // *[tup]
186 
187  Type pabc0= TypeMemPtr.ABC0; // *["abc"]?
188  TypeMemPtr pabc = TypeMemPtr.ABCPTR; // *["abc"]
190  TypeMemPtr pzer0= TypeMemPtr.make(pzer._aliases.meet_nil(),TypeStruct.ALLSTRUCT); // *[(0)]?
191  Type nil = Type.NIL, xnil=Type.XNIL;
192 
193  Type xtup = ptup .dual();
194  Type xtup0= ptup0.dual();
195  TypeMemPtr xstr = pstr.dual();
196  Type xstr0= pstr0.dual();
197  Type xmem = pmem .dual();
198  Type xmem0= pmem0.dual();
199 
200  assertTrue( top .isa(xmem0));
201  assertTrue(xmem0.isa(xmem ));
202 
203  assertTrue(xmem0.isa(xstr0));
204  assertTrue(xmem .isa(xstr ));
205  assertTrue(xmem0.isa(xtup0));
206  assertTrue(xmem .isa(xtup ));
207 
208  // "~str?" or "*[~0+4+]~str?" includes a nil, but nothing can fall to a nil
209  // (breaks lattice)... instead they fall to their appropriate nil-type.
210  assertEquals(Type.NIL,xstr0.meet( nil ));
211 
212  // This is a choice ptr-to-alias#1, vs a nil-able ptr-to-alias#2. Since
213  // they are from different alias classes, they are NEVER equal (unless both
214  // nil). We cannot tell what they point-to, so we do not know if the
215  // memory pointed-at is compatible or not.
216  assertTrue (xstr0.isa(pabc0)); // ~*[1]+0 vs ~*[2]?
217  assertTrue (xstr .isa(pabc ));
218  // We can instead assert that values loaded are compatible:
219  assertTrue (TypeMem.MEM.dual().ld(xstr).isa(TypeMem.MEM_ABC.ld(pabc)));
220 
221  // "~@{}?" or "*[~0+2+]~@{}?" includes a nil, but nothing can fall to a nil
222  // (breaks lattice)... instead they fall to their appropriate nil-type.
223  assertEquals(TypeMemPtr.NIL,xtup0.meet( nil ));
224  assertTrue (xtup0.isa(pzer0));
225  assertTrue (xtup .isa(pzer ));
226  //assertTrue(TypeMem.MEM_TUP.dual().ld(xstr).isa(TypeMem.MEM_ZER.ld(pabc)));
227 
228  assertTrue(xnil .isa(pabc0)); // nil expands as [0]->obj so !isa [2]->"abc"
229  assertTrue(xnil .isa(pstr0)); // nil expands as [0]->obj so !isa [4]->str
230  assertTrue(xnil .isa(ptup0)); // nil expands as [0]->obj so !isa [2]->()
231  assertTrue(xnil .isa(pzer0)); // nil expands as [0]->obj so !isa [2]->@{}
232 
233  assertTrue (pabc0.isa(pstr0));
234  assertTrue (pabc .isa(pstr ));
235  assertTrue (TypeMem.MEM_ABC.ld(pabc).isa(TypeMem.MEM.ld(pstr)));
236  assertTrue (pzer0.isa(ptup0));
237  assertTrue (pzer .isa(ptup ));
238  assertTrue (ptup0.isa(pmem0));
239  assertTrue (ptup .isa(pmem ));
240 
241  assertTrue (pmem .isa(pmem0));
242  assertTrue (pmem0.isa( bot ));
243 
244 
245  // Crossing ints and *[ALL]+null
246  Type i8 = TypeInt.INT8;
247  Type xi8 = i8.dual();
248  assertEquals( Type.NSCALR, xi8.meet(xmem0)); // ~OOP+0 & ~i8 -> 0
249  }
250 
251  @Test public void testStructTuple() {
252  Type nil = Type.NIL;
253  // Tuple is more general that Struct
254  Type tf = TypeStruct.TFLT64; // ( flt64); choice leading field name
255  Type tsx= TypeStruct. FLT64; // @{x:flt64}; fixed leading field name
256  Type tff = tsx.meet(tf); //
257  assertEquals(tf,tff); // tsx.isa(tf)
258  TypeStruct t0 = TypeStruct.make(Types.ts(nil)); // (nil)
259  TypeStruct ts0= TypeStruct.make(TypeFld.NO_DISP,TypeFld.make("x",nil,1)); // @{x:nil}
260  Type tss = ts0.meet(t0);
261  assertEquals(t0,tss); // t0.isa(ts0)
262 
263  // meet @{c:0}? and @{c:@{x:1}?,}
264  int alias0 = BitsAlias.type_alias(BitsAlias.REC);
265  int alias1 = BitsAlias.type_alias(alias0);
266  int alias2 = BitsAlias.type_alias(BitsAlias.REC);
267  int alias3 = BitsAlias.type_alias(alias0);
268  TypeObj a1 = TypeStruct.make(TypeFld.make("c",Type.NIL,0)); // @{c:nil}
269  TypeObj a3 = TypeStruct.make(TypeFld.make("x",TypeInt.TRUE,0)); // @{x: 1 }
270  TypeObj a2 = TypeStruct.make(TypeFld.make("c",TypeMemPtr.make_nil(alias3,a3),0)); // @{c:*{3#}?}
271  Ary<TypeObj> tos = new Ary<>(TypeObj.class);
273  tos.setX(alias1,a1);
274  tos.setX(alias2,a2);
275  tos.setX(alias3,a3);
276  TypeMem mem = TypeMem.make0(tos.asAry()); // [7:@{c==nil},8:{c=*[0,9]},9:@{x==1}]
277  // *[1]? join *[2] ==> *[1+2]?
278  // {~0+7+8} -> @{ c== [~0] -> @{x==1}} // Retain precision after nil
279  Type ptr12 = Type.NIL.join(TypeMemPtr.make(-alias1,(TypeObj)a1.dual())).join( TypeMemPtr.make(-alias2,(TypeObj)a2.dual()));
280  // mem.ld(*[1+2]?) ==> @{c:0}
281  // Currently retaining precision after nil: [~0] -> @{ x==1}
282  Type ld = mem.ld((TypeMemPtr)ptr12);
283  TypeObj ax = TypeStruct.make(TypeFld.make("c",Type.NIL,0)); // @{c:nil}
284  assertTrue(ld.isa(ax));
285  }
286 
287 
288  @Test public void testFunction() {
289  PrimNode[] ignore2 = PrimNode.PRIMS(); // Force node
290 
292  // New functions fall squarely between +/- GENERIC_FUNPTR.
293 
294  // TypeTuple structure demands the shortest Tuple wins the "length
295  // war" (determines the length of the result based on short's any/all flag).
296  TypeFunPtr f1i2i = TypeFunPtr.make_new_fidx(BitsFun.ALL,2,NO_DISP);
297  // To be a GF result, GF has to be shorter and high; the isa does a meet of
298  // TypeFunPtrs which does a *join* of args, which duals the GF args down
299  // low. GF is zero length and low, and wins the meet.
300  assertTrue(f1i2i.isa(gf)); // To be long result, short must be high
301  // To have GF.dual() be anything else and short, GF.dual must be high and
302  // thus the result is a copy of F1I2I.
303  assertTrue(gf.dual().isa(f1i2i)); // To be short result, short must be low
304 
305  assertTrue(f1i2i.isa(gf));
306  TypeFunPtr f1f2f = TypeFunPtr.make_new_fidx(BitsFun.ALL,2,NO_DISP);
307  assertTrue(f1f2f.isa(gf));
308  TypeFunPtr mt = (TypeFunPtr)f1i2i.meet(f1f2f);
309  int fidx0 = f1i2i.fidx();
310  int fidx1 = f1f2f.fidx();
311  BitsFun funs = BitsFun.make0(fidx0).meet(BitsFun.make0(fidx1));
312  TypeFunPtr f3i2r = TypeFunPtr.make(funs,2,NO_DISP);
313  assertEquals(f3i2r,mt);
314  assertTrue(f3i2r.isa(gf));
315  assertTrue(f1i2i.isa(f3i2r));
316  assertTrue(f1f2f.isa(f3i2r));
317 
318  TypeFunPtr f2 = TypeFunPtr.make(BitsFun.make0(fidx1),2,NO_DISP); // Some generic function (happens to be #23, '&')
319  assertTrue(f2.isa(gf));
320  }
321 
322  // Test limits on recursive type structures; recursively building nested
323  // structures caps out in the type system at some reasonable limit.
324  @Test public void testRecursive() {
325 
326  // Recursive types no longer cyclic in the concrete definition? Because
327  // TypeObj can contain TypeMemPtrs but not another nested TypeObj...
328  final int alias1 = BitsAlias.new_alias(BitsAlias.REC);
329  final TypeMemPtr ts0ptr = TypeMemPtr.make (alias1,TypeStruct.ALLSTRUCT);
330  final TypeMemPtr ts0ptr0= TypeMemPtr.make_nil(alias1,TypeStruct.ALLSTRUCT);
331 
332  // Anonymous recursive structs -
333  // - struct with pointer to self
334  TypeFld fldv = TypeFld.make("v",TypeInt.INT64,1);
335  TypeStruct ts0 = TypeStruct.malloc("",false,TypeFlds.ts(TypeFld.malloc("n",null,0),fldv),true);
336  ts0._hash = ts0.compute_hash();
337  ts0.fld(0).setX(ts0ptr); ts0._cyclic = true;
338  ts0 = ts0.install_cyclic(ts0.reachable());
339  TypeMem ts0mem = TypeMem.make(alias1,ts0); // {1:@{n:*[1],v:int} }
340 
341  // - struct with pointer to self or nil
342  TypeStruct ts1 = TypeStruct.malloc("",false,TypeFlds.ts(TypeFld.malloc("n",null,0),fldv),true);
343  ts1._hash = ts1.compute_hash();
344  ts1.fld(0).setX(ts0ptr0); ts1._cyclic = true;
345  ts1 = ts1.install_cyclic(ts1.reachable());
346  TypeMem ts1mem = TypeMem.make(alias1,ts1); // {1:@{n:*[0,1],v:int} }
347 
348  Type tsmt = ts0.meet(ts1);
349  assertEquals(ts1,tsmt);
350  Type tsmemmt = ts0mem.meet(ts1mem);
351  assertEquals(ts1mem,tsmemmt);
352 
353  // Cyclic named struct: Memory#2 :A:@{n:*[0,2],v:int}
354  // If we unrolled this (and used S for Struct and 0 for Nil) we'd get:
355  // AS0AS0AS0AS0AS0AS0...
356  final int alias2 = BitsAlias.new_alias(BitsAlias.REC);
357  TypeMemPtr tptr2= TypeMemPtr.make_nil(alias2,TypeObj.OBJ); // *[0,2]
358  TypeStruct ts2 = TypeStruct.make(TypeFld.make("n",tptr2,0),fldv); // @{n:*[0,2],v:int}
359  TypeStruct ta2 = ts2.set_name("A:");
360 
361  // Peel A once without the nil: Memory#3: A:@{n:*[2],v:int}
362  // ASAS0AS0AS0AS0AS0AS0...
363  final int alias3 = BitsAlias.new_alias(BitsAlias.REC);
364  TypeMemPtr tptr3= TypeMemPtr.make(alias3,TypeObj.OBJ); // *[3]
365  TypeStruct ts3 = TypeStruct.make(TypeFld.make("n",tptr2,0),fldv); // @{n:*[0,2],v:int}
366  TypeStruct ta3 = ts3.set_name("A:");
367 
368  // Peel A twice without the nil: Memory#4: A:@{n:*[3],v:int}
369  // ASASAS0AS0AS0AS0AS0AS0...
370  final int alias4 = BitsAlias.new_alias(BitsAlias.REC);
371  TypeStruct ts4 = TypeStruct.make(TypeFld.make("n",tptr3,0),fldv); // @{n:*[3],v:int}
372  TypeStruct ta4 = ts4.set_name("A:");
373 
374  // Then make a MemPtr{3,4}, and ld - should be a PeelOnce
375  // Starting with the Struct not the A we get:
376  // Once: SAS0AS0AS0AS0AS0AS0...
377  // Twice: SAS AS0AS0AS0AS0AS0...
378  // Meet: SAS0AS0AS0AS0AS0AS0...
379  // which is the Once yet again
380  TypeObj[] tos = new TypeObj[alias4+1];
381  tos[1]=TypeObj.XOBJ;
382  tos[alias2]=ta2;
383  tos[alias3]=ta3;
384  tos[alias4]=ta4;
385  TypeMem mem234 = TypeMem.make0(tos);
387 
388  // Since hacking ptrs about from mem values, no cycles so instead...
389  Type mta = mem234.ld(ptr34);
390  //assertEquals(ta3,mta);
392  TypeStruct xts = TypeStruct.make(TypeFld.make("n",ptr023,0),fldv);
393  Type xta = xts.set_name("A:");
394  assertEquals(xta,mta);
395 
396  // Mismatched Names in a cycle; force a new cyclic type to appear
397  final int alias5 = BitsAlias.new_alias(BitsAlias.REC);
399  TypeStruct tfb = tsnb.set_name("B:");
400  Type mtab = ta2.meet(tfb);
401 
402  // TODO: Needs a way to easily test simple recursive types
403  TypeStruct mtab0 = (TypeStruct)mtab;
404  assertEquals("n",mtab0.fld(0)._fld);
405  assertEquals("v",mtab0.fld(1)._fld);
406  TypeMemPtr mtab1 = (TypeMemPtr)mtab0.at(0);
407  assertTrue(mtab1._aliases.test(alias2)&& mtab1._aliases.test(alias5));
408  assertEquals(Type.REAL,mtab0.at(1));
409 
410 
411  // In the ptr/mem model, all Objs from the same NewNode are immediately
412  // approximated by a single Alias#. This stops any looping type growth.
413  // The only way to get precision back is to inline the NewNode and get new
414  // Alias#s.
415 
416  // Nest a linked-list style tuple 10 deep; verify actual depth is capped at
417  // less than 5. Any data loop must contain a Phi; if structures are
418  // nesting infinitely deep, then it must contain a NewNode also.
419  int alias = BitsAlias.new_alias(BitsAlias.REC);
421  TypeMemPtr phi = TypeMemPtr.make(alias,ts);
422  for( int i=1; i<20; i++ ) {
424  TypeStruct approx = newt.approx(2/*CUTOFF*/,alias);
425  phi = TypeMemPtr.make(alias,approx);
426  }
427  HashMap<Type,Integer> ds = phi.depth();
428  int d = TypeMemPtr.max(alias,ds);
429  assertTrue(0 <= d && d <10);
430  }
431 
432  // Test a cycle with two names on mismatched cycle boundaries
433  @Test public void testNameCycle() {
434  Object dummy0 = TypeStruct.TYPES;
435  Object dummy1 = TypeMemPtr.TYPES;
436  // Make a cycle: 0_A: -> 1_(n=*,v=i64) -> 2_TMP -> 3_B: -> 4_(n=*,v=f64) -> 5_TMP ->
437  // Dual; then meet ~4_() and ~0_A
438  final int alias = BitsAlias.REC;
439 
440  TypeFld fldvi = TypeFld.make("v",TypeInt.INT64,1);
441  TypeFld fldvf = TypeFld.make("v",TypeFlt.FLT64,1);
443  TypeFld[] flds1 = TypeFlds.ts(TypeFld.malloc("n",null,0), fldvi);
444  TypeFld[] flds4 = TypeFlds.ts(TypeFld.malloc("n",null,0), fldvf);
445  TypeStruct as1 = TypeStruct.malloc("",false,flds1,true).set_name("A:");
446  TypeStruct bs4 = TypeStruct.malloc("",false,flds4,true).set_name("B:");
447  as1._hash = as1.compute_hash(); as1._cyclic = true;
448  bs4._hash = bs4.compute_hash(); bs4._cyclic = true;
449  TypeMemPtr ap5 = TypeMemPtr.make(alias,as1);
450  TypeMemPtr bp2 = TypeMemPtr.make(alias,bs4);
451  as1.fld(0).setX(bp2 );
452  bs4.fld(0).setX(ap5 );
454  as1 = as1.install_cyclic(as1.reachable());
455  bp2 = (TypeMemPtr)as1.at(0);
456  bs4 = (TypeStruct)bp2._obj;
457  ap5 = (TypeMemPtr)bs4.at(0);
458 
459  Type das1 = as1.dual(); // ~A:@{b,int}
460  Type dbs4 = bs4.dual(); // ~B:@{a,flt}
461  // Since names mismatch, but both as1 and bs4 are high... must fall hard.
462  Type mt = das1.meet(dbs4); // ~ ~@{a join b, int join flt} ==> @{a join b, int32}
463  TypeStruct smt = (TypeStruct)mt;
464  assertEquals(TypeInt.INT32,smt.at(1));
465  TypeMemPtr smp = (TypeMemPtr)smt.at(0);
466  assertEquals(smt,smp._obj);
467  assertEquals(BitsAlias.RECORD_BITS,smp._aliases);
468 
469  Type mx = as1.dual().meet(dbs4);
470  assertEquals(smt,mx);
471  }
472 
473  @Test public void testLoad() {
474  Object dummy0 = TypeStruct.TYPES;
475  // All are ISA
476  TypeMemPtr[] tmps = new TypeMemPtr[]{
480  };
481  TypeMemPtr[] tmps1 = new TypeMemPtr[]{
484  TypeMemPtr.make(5,TypeObj. OBJ), // testing 5-obj & ISUSED [1....:use] vs MEM_ABC
485  TypeMemPtr.make(4,TypeObj. OBJ),
486  };
487  for( int i=0; i<tmps.length-1; i++ )
488  assertTrue(tmps[i].isa(tmps[i+1]));
489 
490  // All are ISA
491  TypeMem[] tmems = new TypeMem[]{
492  TypeMem.ANYMEM, // [1:~obj,3:~obj,5:~obj ]
493  TypeMem.MEM_ABC, // [1:~obj,3:~obj,5:"abc"]
494  TypeMem.MEM.dual(), // [1:~obj,3:~str,5:"abc"]
495  TypeMem.MEM, // [1: obj,3: str,5:"abc"]
496  TypeMem.MEM_ABC.dual(), // [1: obj,3: obj,5:"abc"]
497  TypeMem.ALLMEM, // [1: obj,3: obj,5: obj ]
498  };
499  for( int j=0; j<tmems.length-1; j++ )
500  assertTrue(tmems[j].isa(tmems[j+1]));
501 
502  TypeObj[][] rez = new TypeObj[tmps.length][tmems.length];
503  for( int i=0; i<tmps.length; i++ )
504  for( int j=0; j<tmems.length; j++ )
505  rez[i][j] = tmems[j].ld(tmps[i]);
506 
507  for( int i0=0; i0<tmps.length; i0++ )
508  for( int j0=0; j0<tmems.length; j0++ )
509  for( int i1=i0; i1<tmps.length; i1++ )
510  for( int j1=j0; j1<tmems.length; j1++ )
511  assertTrue( rez[i0][j0].isa(rez[i1][j1]) );
512  }
513 
514  // Validate crush is monotonic
515  @Test public void testCrush() {
516  TypeObj[] objs = new TypeObj[]{
517  TypeObj.ISUSED,
518  TypeObj.OBJ,
519  TypeStr.STR,
520  TypeStr.ABC,
526  TypeStruct.A,
527  TypeStruct.ARW,
528  };
529 
530  for( int i=0; i<objs.length; i++ ) {
531  TypeObj toi = objs[i];
532  for( int j=i; j<objs.length; j++ ) {
533  TypeObj toj = objs[j];
534  testCrush0( toi , toj );
535  testCrush0((TypeObj)toi.dual(), toj );
536  testCrush0( toi ,(TypeObj)toj.dual());
537  testCrush0((TypeObj)toi.dual(),(TypeObj)toj.dual());
538  }
539  }
540  }
541 
542  private void testCrush0( TypeObj ti, TypeObj tj ) {
543  if( ti.isa(tj) ) {
544  TypeObj tic = ti.crush();
545  TypeObj tjc = tj.crush();
546  assertTrue(tic.isa(tjc));
547  }
548  }
549 
550 
551  @Test public void testCommuteSymmetricAssociative() {
552  assertTrue(Type.check_startup());
553  }
554 
555 }
com.cliffc.aa.type.TypeMemPtr.ABC0
static final TypeMemPtr ABC0
Definition: TypeMemPtr.java:100
com.cliffc.aa.type.TypeMemPtr.TYPES
static final Type[] TYPES
Definition: TypeMemPtr.java:106
com.cliffc.aa.type.Type.NSCALR
static final Type NSCALR
Definition: Type.java:330
com.cliffc.aa.type.TypeMemPtr.NO_DISP
static final Type NO_DISP
Definition: TypeMemPtr.java:80
com.cliffc.aa.type.TypeInt.INT8
static final TypeInt INT8
Definition: TypeInt.java:42
com.cliffc.aa.type.TestType.testType
void testType()
Definition: TestType.java:16
com.cliffc.aa.type.TypeMem.make
static TypeMem make(TypeObj[] pubs)
Definition: TypeMem.java:163
com.cliffc.aa.type.TypeMem.ld
TypeObj ld(TypeMemPtr ptr)
Definition: TypeMem.java:325
com.cliffc.aa.type.TypeFunPtr
Definition: TypeFunPtr.java:23
com.cliffc.aa.type.TestType.testCrush
void testCrush()
Definition: TestType.java:515
com.cliffc.aa.node.PrimNode
Definition: PrimNode.java:20
com.cliffc.aa.type.TestType.testCrush0
void testCrush0(TypeObj ti, TypeObj tj)
Definition: TestType.java:542
com.cliffc.aa.type.TypeMemPtr.STRUCT0
static final TypeMemPtr STRUCT0
Definition: TypeMemPtr.java:102
com.cliffc.aa.type.BitsFun.make0
static BitsFun make0(int bit)
Definition: BitsFun.java:44
com.cliffc.aa.type.Type.isa
boolean isa(Type t)
Definition: Type.java:623
com.cliffc.aa.type.TypeMemPtr.ABCPTR
static final TypeMemPtr ABCPTR
Definition: TypeMemPtr.java:99
com.cliffc.aa.type.TypeMem
Memory type; the state of all of memory; memory edges order memory ops.
Definition: TypeMem.java:53
com.cliffc.aa.type.TypeStruct.compute_hash
int compute_hash()
Definition: TypeStruct.java:66
com.cliffc.aa.type.TestType.testStructTuple
void testStructTuple()
Definition: TestType.java:251
com.cliffc.aa.type.Type.join
Type join(Type t)
Definition: Type.java:619
com.cliffc.aa.type.TypeMemPtr.OOP
static final TypeMemPtr OOP
Definition: TypeMemPtr.java:94
com.cliffc
com.cliffc.aa.type.TypeObj.crush
TypeObj crush()
Definition: TypeObj.java:85
com.cliffc.aa.type.BitsAlias.type_alias
static int type_alias(int par)
Definition: BitsAlias.java:76
com.cliffc.aa.type.TypeFld
Definition: TypeFld.java:12
com.cliffc.aa.type.Type.XSCALAR
static final Type XSCALAR
Definition: Type.java:329
com.cliffc.aa.util
Definition: AbstractEntry.java:1
com.cliffc.aa.type.TypeInt
Definition: TypeInt.java:9
com.cliffc.aa.type.TypeFunPtr.GENERIC_FUNPTR
static final TypeFunPtr GENERIC_FUNPTR
Definition: TypeFunPtr.java:80
com.cliffc.aa.type.Type
an implementation of language AA
Definition: Type.java:94
com.cliffc.aa.type.TypeFlt
Definition: TypeFlt.java:9
com.cliffc.aa.type.TypeStruct.ALLSTRUCT
static final TypeStruct ALLSTRUCT
Definition: TypeStruct.java:228
com.cliffc.aa.util.Ary
Definition: Ary.java:11
com.cliffc.aa.type.BitsAlias
Definition: BitsAlias.java:8
com.cliffc.aa.type.TypeStruct.POINT
static final TypeStruct POINT
Definition: TypeStruct.java:231
com.cliffc.aa.type.TypeStruct.TFLT64
static final TypeStruct TFLT64
Definition: TypeStruct.java:233
com.cliffc.aa.type.TypeStruct.A
static final TypeStruct A
Definition: TypeStruct.java:234
com.cliffc.aa.type.TypeFld.NO_DISP
static final TypeFld NO_DISP
Definition: TypeFld.java:170
com.cliffc.aa.type.TypeInt.con
static TypeInt con(long con)
Definition: TypeInt.java:37
com.cliffc.aa.type.TypeMem.ALLMEM
static final TypeMem ALLMEM
Definition: TypeMem.java:228
com.cliffc.aa.type.TestType.testNameCycle
void testNameCycle()
Definition: TestType.java:433
com.cliffc.aa.type.TypeFunPtr.make_new_fidx
static TypeFunPtr make_new_fidx(int parent, int nargs, Type disp)
Definition: TypeFunPtr.java:74
com.cliffc.aa.type.TypeFlds
Definition: TypeFlds.java:8
com.cliffc.aa.type.Type.ANY
static final Type ANY
Definition: Type.java:325
com.cliffc.aa.type.TypeStruct.install_cyclic
TypeStruct install_cyclic(Ary< Type > reachs)
Definition: TypeStruct.java:422
com.cliffc.aa.type.BitsAlias.new_alias
static int new_alias(int par)
Definition: BitsAlias.java:75
com.cliffc.aa.type.BitsFun.ALL
static final int ALL
Definition: BitsFun.java:24
com.cliffc.aa.type.Type.meet
final Type meet(Type t)
Definition: Type.java:412
com.cliffc.aa.type.TestType
Definition: TestType.java:14
com.cliffc.aa.type.TypeStruct
A memory-based collection of optionally named fields.
Definition: TypeStruct.java:50
com.cliffc.aa.type.TypeFld.make
static TypeFld make(String fld, Type t, int order)
Definition: TypeFld.java:58
com.cliffc.aa.type.Bits.test
static boolean test(long[] bits, int i)
Definition: Bits.java:224
com.cliffc.aa.type.TypeMemPtr._obj
TypeObj _obj
Definition: TypeMemPtr.java:26
com.cliffc.aa.type.TypeMem.ANYMEM
static final TypeMem ANYMEM
Definition: TypeMem.java:228
com.cliffc.aa.type.Types
Definition: Types.java:9
com.cliffc.aa.type.TypeStruct.at
Type at(int idx)
Definition: TypeStruct.java:1013
com.cliffc.aa.type.Types.ts
static Type[] ts(Type t0)
Definition: Types.java:90
com.cliffc.aa.type.Type.ALL
static final Type ALL
Definition: Type.java:324
com.cliffc.aa.type.BitsAlias.REC
static final int REC
Definition: BitsAlias.java:25
com.cliffc.aa.util.Ary.asAry
E[] asAry()
Definition: Ary.java:172
com.cliffc.aa.type.TypeStruct.INT64_INT64
static final TypeStruct INT64_INT64
Definition: TypeStruct.java:239
com.cliffc.aa.type.TypeMem.make0
static TypeMem make0(TypeObj[] as)
Definition: TypeMem.java:170
com.cliffc.aa.type.TypeInt.INT64
static final TypeInt INT64
Definition: TypeInt.java:39
com.cliffc.aa.type.Type.set_name
final T set_name(String name)
Definition: Type.java:551
com.cliffc.aa.type.TypeObj.OBJ
static final TypeObj OBJ
Definition: TypeObj.java:44
com.cliffc.aa.type.TypeObj
Definition: TypeObj.java:15
com.cliffc.aa.type.TypeFld.fldBot
static final String fldBot
Definition: TypeFld.java:140
com.cliffc.aa.type.TypeFld.setX
TypeFld setX(Type t)
Definition: TypeFld.java:173
com.cliffc.aa.type.TypeMemPtr.STR0
static final TypeMemPtr STR0
Definition: TypeMemPtr.java:98
com.cliffc.aa.type.TestType.testCommuteSymmetricAssociative
void testCommuteSymmetricAssociative()
Definition: TestType.java:551
com.cliffc.aa.type.TypeFunPtr.fidx
int fidx()
Definition: TypeFunPtr.java:128
com.cliffc.aa.type.TestType.testBits0
void testBits0()
Definition: TestType.java:26
com.cliffc.aa.type.Type.RECURSIVE_MEET
static int RECURSIVE_MEET
Definition: Type.java:163
com.cliffc.aa.type.TypeMemPtr.DISPLAY
static final TypeStruct DISPLAY
Definition: TypeMemPtr.java:78
com.cliffc.aa.type.BitsAlias.RECORD_BITS
static BitsAlias RECORD_BITS
Definition: BitsAlias.java:26
com.cliffc.aa.type.TypeObj.XOBJ
static final TypeObj XOBJ
Definition: TypeObj.java:47
com.cliffc.aa.util.Ary.setX
E setX(int i, E e)
Definition: Ary.java:115
com.cliffc.aa.node.PrimNode.PRIMS
static PrimNode[] PRIMS
Definition: PrimNode.java:36
com.cliffc.aa.type.TypeMem.MEM_ABC
static final TypeMem MEM_ABC
Definition: TypeMem.java:229
com.cliffc.aa.type.TypeStr.ABC
static final TypeStr ABC
Definition: TypeStr.java:47
com.cliffc.aa.type.BitsAlias.ALL
static final int ALL
Definition: BitsAlias.java:25
com.cliffc.aa.type.TypeInt.INT32
static final TypeInt INT32
Definition: TypeInt.java:40
com.cliffc.aa.type.TypeObj.ISUSED
static final TypeObj ISUSED
Definition: TypeObj.java:45
com.cliffc.aa.type.TypeFld._fld
String _fld
Definition: TypeFld.java:15
com.cliffc.aa.type.TypeStruct.reachable
Ary< Type > reachable()
Definition: TypeStruct.java:779
com.cliffc.aa.type.Bits.meet_nil
B meet_nil()
Definition: Bits.java:212
com.cliffc.aa.type.TypeMemPtr.STRUCT
static final TypeMemPtr STRUCT
Definition: TypeMemPtr.java:101
com.cliffc.aa.type.TestType.testFunction
void testFunction()
Definition: TestType.java:288
com.cliffc.aa.type.TypeFunPtr.make
static TypeFunPtr make(BitsFun fidxs, int nargs, Type disp)
Definition: TypeFunPtr.java:67
com.cliffc.aa.type.TestType.testLoad
void testLoad()
Definition: TestType.java:473
com.cliffc.aa.type.TypeStr
Definition: TypeStr.java:14
com.cliffc.aa.type.TypeStruct.ARW
static final TypeStruct ARW
Definition: TypeStruct.java:237
com.cliffc.aa.type.TestType.testNamesInts
void testNamesInts()
Definition: TestType.java:92
com.cliffc.aa.type.BitsFun
Definition: BitsFun.java:7
com.cliffc.aa.type.Type.check_startup
static boolean check_startup()
Definition: Type.java:684
com.cliffc.aa.type.Type.NIL
static final Type NIL
Definition: Type.java:332
com.cliffc.aa.type.TypeFld.malloc
static TypeFld malloc(String fld, Type t, Access access, int order)
Definition: TypeFld.java:53
com.cliffc.aa.type.TypeStruct.approx
TypeStruct approx(int cutoff, int alias)
Definition: TypeStruct.java:477
com.cliffc.aa
Definition: AA.java:1
com.cliffc.aa.type.TestType.testBits
void testBits()
Definition: TestType.java:58
com.cliffc.aa.type.TypeStruct.fld
TypeFld fld(int idx)
Definition: TypeStruct.java:1012
com.cliffc.aa.type.TypeInt.INT16
static final TypeInt INT16
Definition: TypeInt.java:41
com.cliffc.aa.type.TypeStruct.TYPES
static final TypeStruct[] TYPES
Definition: TypeStruct.java:242
com.cliffc.aa.type.TypeMemPtr._aliases
BitsAlias _aliases
Definition: TypeMemPtr.java:16
com.cliffc.aa.type.TypeInt.FALSE
static final Type FALSE
Definition: TypeInt.java:45
com.cliffc.aa.type.TestType.testRecursive
void testRecursive()
Definition: TestType.java:324
com.cliffc.aa.type.Type.REAL
static final Type REAL
Definition: Type.java:334
com.cliffc.aa.type.Type.dual
final T dual()
Definition: Type.java:361
com.cliffc.aa.type.TypeStruct.make
static TypeStruct make(String fld_name, Type t)
Definition: TypeStruct.java:190
com.cliffc.aa.type.Type.XNIL
static final Type XNIL
Definition: Type.java:333
com.cliffc.aa.type.TypeMemPtr.depth
HashMap< Type, Integer > depth()
Definition: TypeMemPtr.java:217
com.cliffc.aa.type.TypeStruct.malloc
static TypeStruct malloc(String name, boolean any, TypeFld[] flds, boolean open)
Definition: TypeStruct.java:175
com.cliffc.aa.type.TypeStr.STR
static final TypeStr STR
Definition: TypeStr.java:45
com.cliffc.aa.type.TestType.testOOPsNulls
void testOOPsNulls()
Definition: TestType.java:142
com.cliffc.aa.type.TypeMemPtr.STRPTR
static final TypeMemPtr STRPTR
Definition: TypeMemPtr.java:97
com.cliffc.aa.type.TypeMemPtr.OOP0
static final TypeMemPtr OOP0
Definition: TypeMemPtr.java:93
com.cliffc.aa.type.Bits.meet
B meet(final B bs)
Definition: Bits.java:298
com.cliffc.aa.type.TypeMemPtr.max
static int max(int alias, HashMap< Type, Integer > ds)
Definition: TypeMemPtr.java:243
com.cliffc.aa.type.TypeInt.TRUE
static final TypeInt TRUE
Definition: TypeInt.java:44
com.cliffc.aa.type.TypeStruct.NAMEPT
static final TypeStruct NAMEPT
Definition: TypeStruct.java:232
com.cliffc.aa.type.TypeMemPtr.make_nil
static TypeMemPtr make_nil(int alias, TypeObj obj)
Definition: TypeMemPtr.java:72
com.cliffc.aa.type.TypeStruct._cyclic
boolean _cyclic
Definition: TypeStruct.java:54
com
com.cliffc.aa.type.TypeMem.MEM
static final TypeMem MEM
Definition: TypeMem.java:224
com.cliffc.aa.type.TypeFlds.ts
static TypeFld[] ts(TypeFld t0)
Definition: TypeFlds.java:82
com.cliffc.aa.type
Definition: Bits.java:1
com.cliffc.aa.type.TypeMemPtr
Definition: TypeMemPtr.java:14
com.cliffc.aa.node
Definition: AssertNode.java:1
com.cliffc.aa.type.TypeFlt.FLT64
static final TypeFlt FLT64
Definition: TypeFlt.java:38
com.cliffc.aa.type.TypeMemPtr.make
static TypeMemPtr make(BitsAlias aliases, TypeObj obj)
Definition: TypeMemPtr.java:66