def test_missing_stmt(self): sh = ShapeOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(1), DimensionObject(2)) r = repr(sh) self.assertIn("ShapeOperator('+', lambda x, y: x + y", r) a = sh.evaluate() self.assertEqual(a, 3) self.assertRaise( lambda: ShapeOperator("+", lambda x, y: x + y, "lambda x, y: x + y", 1, (2, )), TypeError) sh = ShapeOperator("+", lambda x, y: x + str(y), "lambda x, y: x + y", DimensionObject(1), DimensionObject(2)) self.assertRaise(lambda: sh.evaluate(), RuntimeError)
def test_addition(self): i1 = DimensionObject(1) i2 = DimensionObject(3) i3 = i1 + i2 self.assertEqual( "DimensionObject(ShapeOperatorAdd(DimensionObject(1), DimensionObject(3)))", repr(i3)) self.assertEqual(i3.to_string(), '4') v = i3.evaluate() self.assertEqual(v, 4) i1 = DimensionObject(1) i2 = DimensionObject("x") i3 = i1 + i2 self.assertEqual(i3.to_string(), '1+x') self.assertEqual( "DimensionObject(ShapeOperatorAdd(DimensionObject(1), DimensionObject('x')))", repr(i3)) v = i3.evaluate(x=1) self.assertEqual(v, 2) v = i3.evaluate() self.assertEqual(v, "(1)+(x)") self.assertRaise(lambda: DimensionObject((1, )) + 1, TypeError) self.assertRaise(lambda: DimensionObject(1) + DimensionObject((1, )), TypeError)
def test_multiplication(self): i1 = DimensionObject(2) i2 = DimensionObject(3) i3 = i1 * i2 self.assertEqual( "DimensionObject(ShapeOperatorMul(DimensionObject(2), DimensionObject(3)))", repr(i3)) self.assertEqual(i3.to_string(), '6') v = i3.evaluate() self.assertEqual(v, 6) i1 = DimensionObject(2) i2 = DimensionObject("x") i3 = i1 * i2 self.assertEqual(i3.to_string(), '2*x') self.assertEqual( "DimensionObject(ShapeOperatorMul(DimensionObject(2), DimensionObject('x')))", repr(i3)) v = i3.evaluate(x=2) self.assertEqual(v, 4) v = i3.evaluate() self.assertEqual(v, "(2)*(x)") self.assertRaise(lambda: DimensionObject((1, )) * 1, TypeError) self.assertRaise(lambda: DimensionObject(1) * DimensionObject((1, )), TypeError)
def test_greater(self): i1 = DimensionObject(2) i2 = DimensionObject(3) i3 = i1 > i2 self.assertEqual(i3, False) i1 = DimensionObject(2) i2 = DimensionObject("x") i3 = i1 > i2 self.assertEqual(i3.to_string(), '2>x') self.assertEqual( "DimensionObject(ShapeOperatorGreater(DimensionObject(2), DimensionObject('x')))", repr(i3)) v = i3.evaluate(x=2) self.assertEqual(v, False) v = i3.evaluate() self.assertEqual(v, "(2)>(x)") self.assertRaise(lambda: DimensionObject((1, )) * 1, TypeError) self.assertRaise(lambda: DimensionObject( 1) * DimensionObject((1, )), TypeError)
def fct2(): return ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject((1, 2)), DimensionObject(3))
def fct1(): return ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(1), DimensionObject((2, 3)))
def test_missing_stmt_binary(self): def fct1(): return ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(1), DimensionObject((2, 3))) def fct2(): return ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject((1, 2)), DimensionObject(3)) self.assertRaise(fct1, TypeError) self.assertRaise(fct2, TypeError) sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(1), DimensionObject(2)) st = sh.to_string() self.assertEqual(st, '3') sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject('1'), DimensionObject('2')) st = sh.to_string() self.assertEqual(st, '(1)+(2)') x, y = sh._args # pylint: disable=W0212 self.assertEqual(sh._to_string1(x, y), "12") # pylint: disable=W0212 self.assertEqual(sh._to_string2(x, y), "1+2") # pylint: disable=W0212 self.assertEqual( sh._to_string2b( # pylint: disable=W0212 x, y), "(1)+(2)") # pylint: disable=W0212 self.assertEqual(sh._to_string3(x), "1+x") # pylint: disable=W0212 sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject('X'), DimensionObject(2)) st = sh.to_string() self.assertEqual(st, 'X+2') sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(2), DimensionObject('X')) st = sh.to_string() self.assertEqual(st, '2+X') sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(2), DimensionObject(None)) st = sh.to_string() self.assertEqual(st, '2+x') d = DimensionObject(None) self.assertEqual(d.dim, None) d = DimensionObject(DimensionObject(2)) st = repr(d) self.assertEqual(st, "DimensionObject(2)")
def test_maximum(self): i1 = DimensionObject(1) i2 = DimensionObject(3) i3 = DimensionObject(ShapeOperatorMax(i1, i2)) self.assertEqual( "DimensionObject(ShapeOperatorMax(DimensionObject(1), DimensionObject(3)))", repr(i3)) self.assertEqual(i3.to_string(), '3') v = i3.evaluate() self.assertEqual(v, 3) i1 = DimensionObject(1) i2 = DimensionObject("x") i3 = DimensionObject(ShapeOperatorMax(i1, i2)) self.assertEqual(i3.to_string(), 'max(1,x)') self.assertEqual( "DimensionObject(ShapeOperatorMax(DimensionObject(1), DimensionObject('x')))", repr(i3)) v = i3.evaluate(x=1) self.assertEqual(v, 1) v = i3.evaluate() self.assertEqual(v, "max(1,x)") self.assertRaise(lambda: DimensionObject((1, )) + 1, TypeError) self.assertRaise(lambda: DimensionObject(1) + DimensionObject((1, )), TypeError)
def test_missing_stmt_binary(self): def fct1(): return ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(1), DimensionObject((2, 3))) def fct2(): return ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject((1, 2)), DimensionObject(3)) self.assertRaise(fct1, TypeError) self.assertRaise(fct2, TypeError) sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(1), DimensionObject(2)) st = sh.to_string() self.assertEqual(st, '3') sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject('1'), DimensionObject('2')) st = sh.to_string() self.assertEqual(st, '(1)+(2)') sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject('X'), DimensionObject(2)) st = sh.to_string() self.assertEqual(st, 'X+2') sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(2), DimensionObject('X')) st = sh.to_string() self.assertEqual(st, '2+X') sh = ShapeBinaryOperator("+", lambda x, y: x + y, "lambda x, y: x + y", DimensionObject(2), DimensionObject(None)) st = sh.to_string() self.assertEqual(st, '2+x') d = DimensionObject(None) self.assertEqual(d.dim, None) d = DimensionObject(DimensionObject(2)) st = repr(d) self.assertEqual(st, "DimensionObject(2)")