def test_duallinker_straightforward(): x, y, z = inputs() e = add(mul(x, y), mul(y, z)) # add and mul are correct in C and in Python lnk = DualLinker(checker=_my_checker).accept(FunctionGraph([x, y, z], [e])) fn = lnk.make_function() res = fn(7.2, 1.5, 3.0) assert res == 15.3
def test_not(self): x, y, z = ints("xyz") fn = DualLinker().accept(FunctionGraph([x, y], [invert(x)])).make_function() for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)): assert fn(a, b) == ~a, (a,) x, y, z = ints("xyz") fn = DualLinker().accept(FunctionGraph([x, y], [~x])).make_function() for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)): assert fn(a, b) == ~a, (a,)
def test_and(self): x, y, z = ints("xyz") fn = DualLinker().accept(FunctionGraph([x, y], [and_(x, y)])).make_function() for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)): assert fn(a, b) == (a & b), (a, b) x, y, z = ints("xyz") fn = DualLinker().accept(FunctionGraph([x, y], [x & y])).make_function() for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)): assert fn(a, b) == (a & b), (a, b)
def test_straightforward(self): x, y, z = floats("xyz") e = mul(add(x, y), true_div(x, y)) C = Composite([x, y], [e]) c = C.make_node(x, y) # print c.c_code(['x', 'y'], ['z'], dict(id = 0)) g = FunctionGraph([x, y], [c.out]) fn = DualLinker().accept(g).make_function() assert fn(1.0, 2.0) == 1.5
def test_with_constants(self): x, y, z = floats("xyz") e = mul(add(70.0, y), true_div(x, y)) C = Composite([x, y], [e]) c = C.make_node(x, y) assert "70.0" in c.op.c_code(c, "dummy", ["x", "y"], ["z"], dict(id=0)) # print c.c_code(['x', 'y'], ['z'], dict(id = 0)) g = FunctionGraph([x, y], [c.out]) fn = DualLinker().accept(g).make_function() assert fn(1.0, 2.0) == 36.0
def test_many_outputs(self): x, y, z = floats("xyz") e0 = x + y + z e1 = x + y * z e2 = x / y C = Composite([x, y, z], [e0, e1, e2]) c = C.make_node(x, y, z) # print c.c_code(['x', 'y', 'z'], ['out0', 'out1', 'out2'], dict(id = 0)) g = FunctionGraph([x, y, z], c.outputs) fn = DualLinker().accept(g).make_function() assert fn(1.0, 2.0, 3.0) == [6.0, 7.0, 0.5]
def test_duallinker_mismatch(): x, y, z = inputs() # bad_sub is correct in C but erroneous in Python e = bad_sub(mul(x, y), mul(y, z)) g = FunctionGraph([x, y, z], [e]) lnk = DualLinker(checker=_my_checker).accept(g) fn = lnk.make_function() # good assert CLinker().accept(g).make_function()(1.0, 2.0, 3.0) == -4.0 # good assert OpWiseCLinker().accept(g).make_function()(1.0, 2.0, 3.0) == -4.0 # (purposely) wrong assert PerformLinker().accept(g).make_function()(1.0, 2.0, 3.0) == -10.0 with pytest.raises(MyExc): # this runs OpWiseCLinker and PerformLinker in parallel and feeds # variables of matching operations to _my_checker to verify that they # are the same. fn(1.0, 2.0, 3.0)
def test_composite_printing(self): x, y, z = floats("xyz") e0 = x + y + z e1 = x + y * z e2 = x / y e3 = x // 5 e4 = -x e5 = x - y e6 = x**y + (-z) e7 = x % 3 C = Composite([x, y, z], [e0, e1, e2, e3, e4, e5, e6, e7]) c = C.make_node(x, y, z) g = FunctionGraph([x, y, z], c.outputs) DualLinker().accept(g).make_function() assert str(g) == ("FunctionGraph(*1 -> Composite{((i0 + i1) + i2)," " (i0 + (i1 * i2)), (i0 / i1), " "(i0 // ScalarConstant{5}), " "(-i0), (i0 - i1), ((i0 ** i1) + (-i2))," " (i0 % ScalarConstant{3})}(x, y, z), " "*1::1, *1::2, *1::3, *1::4, *1::5, *1::6, *1::7)")
def test_mul_add_true(): x, y, z = floats("xyz") e = mul(add(x, y), true_div(x, y)) g = FunctionGraph([x, y], [e]) fn = DualLinker().accept(g).make_function() assert fn(1.0, 2.0) == 1.5
def test_xor(self): x, y, z = ints("xyz") fn = DualLinker().accept(FunctionGraph([x, y], [x ^ y])).make_function() for a, b in ((0, 1), (0, 0), (1, 0), (1, 1)): assert fn(a, b) == (a ^ b), (a, b)
def test_neq(self): x, y, z = floats("xyz") fn = DualLinker().accept(FunctionGraph([x, y], [neq(x, y)])).make_function() for a, b in ((3.0, 9), (3, 0.9), (3, 3)): assert fn(a, b) == (a != b)