| 1 | from rpython.jit.metainterp.history import * |
| 2 | from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 3 | from rpython.rlib.rfloat import NAN, INFINITY |
| 4 | from rpython.jit.metainterp.resoperation import InputArgInt |
| 5 | from rpython.jit.codewriter import longlong |
| 6 | from rpython.translator.c.test.test_standalone import StandaloneTests |
| 7 | |
| 8 | |
| 9 | def test_repr(): |
| 10 | S = lltype.GcStruct('S') |
| 11 | T = lltype.GcStruct('T', ('header', S)) |
| 12 | t = lltype.malloc(T) |
| 13 | s = lltype.cast_pointer(lltype.Ptr(S), t) |
| 14 | const = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, s)) |
| 15 | assert const._getrepr_() == "*T" |
| 16 | |
| 17 | def test_same_constant(): |
| 18 | c1a = ConstInt(0) |
| 19 | c1b = ConstInt(0) |
| 20 | c2a = ConstPtr(lltype.nullptr(llmemory.GCREF.TO)) |
| 21 | c2b = ConstPtr(lltype.nullptr(llmemory.GCREF.TO)) |
| 22 | c3a = Const._new(0.0) |
| 23 | c3b = Const._new(0.0) |
| 24 | assert c1a.same_constant(c1b) |
| 25 | assert not c1a.same_constant(c2b) |
| 26 | assert not c1a.same_constant(c3b) |
| 27 | assert not c2a.same_constant(c1b) |
| 28 | assert c2a.same_constant(c2b) |
| 29 | assert not c2a.same_constant(c3b) |
| 30 | assert not c3a.same_constant(c1b) |
| 31 | assert not c3a.same_constant(c2b) |
| 32 | assert c3a.same_constant(c3b) |
| 33 | |
| 34 | def test_same_constant_float(): |
| 35 | c1 = Const._new(12.34) |
| 36 | c2 = Const._new(12.34) |
| 37 | c3 = Const._new(NAN) |
| 38 | c4 = Const._new(NAN) |
| 39 | c5 = Const._new(INFINITY) |
| 40 | c6 = Const._new(INFINITY) |
| 41 | assert c1.same_constant(c2) |
| 42 | assert c3.same_constant(c4) |
| 43 | assert c5.same_constant(c6) |
| 44 | assert not c1.same_constant(c4) |
| 45 | assert not c1.same_constant(c6) |
| 46 | assert not c3.same_constant(c2) |
| 47 | assert not c3.same_constant(c6) |
| 48 | assert not c5.same_constant(c2) |
| 49 | assert not c5.same_constant(c4) |
| 50 | |
| 51 | def test_float_nonnull(): |
| 52 | c1 = Const._new(0.0) |
| 53 | c2 = Const._new(1.0) |
| 54 | c3 = Const._new(INFINITY) |
| 55 | c4 = Const._new(-INFINITY) |
| 56 | c5 = Const._new(NAN) |
| 57 | c6 = Const._new(-0.0) |
| 58 | assert not c1.nonnull() |
| 59 | assert c2.nonnull() |
| 60 | assert c3.nonnull() |
| 61 | assert c4.nonnull() |
| 62 | assert c5.nonnull() |
| 63 | assert c6.nonnull() |
| 64 | |
| 65 | def test_frontendop(): |
| 66 | f = FrontendOp(42) |
| 67 | assert f.get_position() == 42 |
| 68 | f = FrontendOp(-56) |
| 69 | assert f.get_position() == -56 |
| 70 | |
| 71 | def fresh_ref(): |
| 72 | S = lltype.GcStruct('S') |
| 73 | s = lltype.malloc(S) |
| 74 | return lltype.cast_opaque_ptr(llmemory.GCREF, s) |
| 75 | |
| 76 | def duplicate_ref(x): |
| 77 | s = x._obj.container._as_ptr() |
| 78 | return lltype.cast_opaque_ptr(llmemory.GCREF, s) |
| 79 | |
| 80 | def test_ref_dict(): |
| 81 | d = new_ref_dict() |
| 82 | ref1 = fresh_ref() |
| 83 | ref2 = fresh_ref() |
| 84 | ref3 = fresh_ref() |
| 85 | d[ref1] = 123 |
| 86 | d[ref2] = 456 |
| 87 | d[ref3] = 789 |
| 88 | ref1b = duplicate_ref(ref1) |
| 89 | ref2b = duplicate_ref(ref2) |
| 90 | ref3b = duplicate_ref(ref3) |
| 91 | assert d[ref1b] == 123 |
| 92 | assert d[ref2b] == 456 |
| 93 | assert d[ref3b] == 789 |
| 94 | |
| 95 | class TestZTranslated(StandaloneTests): |
| 96 | def test_ztranslated_same_constant_float(self): |
| 97 | def fn(args): |
| 98 | n = INFINITY |
| 99 | c1 = ConstFloat(longlong.getfloatstorage(n - INFINITY)) |
| 100 | c2 = ConstFloat(longlong.getfloatstorage(n - INFINITY)) |
| 101 | c3 = ConstFloat(longlong.getfloatstorage(12.34)) |
| 102 | if c1.same_constant(c2): |
| 103 | print "ok!" |
| 104 | return 0 |
| 105 | |
| 106 | t, cbuilder = self.compile(fn) |
| 107 | data = cbuilder.cmdexec('') |
| 108 | assert "ok!\n" in data |