public void AddAssociativityAddNearbyConstants() { var builderPair = GetSimpleAndConstantFoldingBuilder(); var sb = builderPair.Item1; var cfb = builderPair.Item2; var x = GetVarAndIdExpr("x", BasicType.Int).Item2; Expr foldedResult = x; Expr unfoldedResult = foldedResult; for (int index = 1; index <= 3; ++index) { foldedResult = cfb.Add(cfb.ConstantInt(index), foldedResult); unfoldedResult = sb.Add(sb.ConstantInt(index), unfoldedResult); } Assert.AreEqual("6 + x", foldedResult.ToString()); Assert.AreEqual("3 + 2 + 1 + x", unfoldedResult.ToString()); Assert.IsFalse(foldedResult.Equals(unfoldedResult)); var topAsAdd = ExprUtil.AsAdd(foldedResult); Assert.IsNotNull(topAsAdd); var topLhsAsLit = ExprUtil.AsLiteral(topAsAdd.Args[0]); Assert.IsNotNull(topLhsAsLit); Assert.AreEqual(BigNum.FromInt(6), topLhsAsLit.asBigNum); }
public void AddAssociativityPropagateConstantUp() { var builderPair = GetSimpleAndConstantFoldingBuilder(); var sb = builderPair.Item1; var cfb = builderPair.Item2; Expr foldedResult = sb.ConstantInt(1); Expr unfoldedResult = foldedResult; for (int index = 0; index < 3; ++index) { var x = GetVarAndIdExpr("x" + index.ToString(), BasicType.Int).Item2; foldedResult = cfb.Add(x, foldedResult); unfoldedResult = sb.Add(x, unfoldedResult); CheckIsInt(foldedResult); CheckIsInt(unfoldedResult); } Assert.AreEqual("1 + x2 + x1 + x0", foldedResult.ToString()); Assert.AreEqual("x2 + x1 + x0 + 1", unfoldedResult.ToString()); Assert.IsFalse(foldedResult.Equals(unfoldedResult)); var topAsAdd = ExprUtil.AsAdd(foldedResult); Assert.IsNotNull(topAsAdd); var topLhsAsLit = ExprUtil.AsLiteral(topAsAdd.Args[0]); Assert.IsNotNull(topLhsAsLit); Assert.AreEqual(BigNum.FromInt(1), topLhsAsLit.asBigNum); }
public void MinusConstantRealToAdd() { var cfb = GetConstantFoldingBuilder(); var x = GetVarAndIdExpr("x", BasicType.Real).Item2; var constant = cfb.ConstantReal("2.0"); var result = cfb.Sub(x, constant); CheckIsReal(result); var asAdd = ExprUtil.AsAdd(result); Assert.IsNotNull(asAdd); var lhs = ExprUtil.AsLiteral(asAdd.Args[0]); Assert.IsNotNull(lhs); Assert.AreEqual("-2e0", lhs.asBigDec.ToString()); }
public void MinusConstantIntToAdd() { var cfb = GetConstantFoldingBuilder(); var x = GetVarAndIdExpr("x", BasicType.Int).Item2; var constant = cfb.ConstantInt(2); var result = cfb.Sub(x, constant); CheckIsInt(result); var asAdd = ExprUtil.AsAdd(result); Assert.IsNotNull(asAdd); var lhs = ExprUtil.AsLiteral(asAdd.Args[0]); Assert.IsNotNull(lhs); Assert.AreEqual(-2, lhs.asBigNum.ToInt); }