1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
using NUnit.Framework;
using System;
using Microsoft.Boogie;
using System.Collections.Generic;
namespace CoreTests
{
[TestFixture()]
public class ExprImmutability : IErrorSink
{
public void Error(IToken tok, string msg)
{
Assert.Fail(msg);
}
// Cached hashcode checkers
[Test()]
public void CachedHashCodeLiteralExpr()
{
var literal = new LiteralExpr(Token.NoToken, true, /*immutable=*/true);
Assert.AreEqual(literal.ComputeHashCode(), literal.GetHashCode());
var literal2 = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(0), /*immutable=*/true);
Assert.AreEqual(literal2.ComputeHashCode(), literal2.GetHashCode());
var literal3 = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigDec.FromInt(0), /*immutable=*/true);
Assert.AreEqual(literal3.ComputeHashCode(), literal3.GetHashCode());
var literal4 = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(0), 8, /*immutable=*/true);
Assert.AreEqual(literal4.ComputeHashCode(), literal4.GetHashCode());
var literal5 = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigFloat.FromInt(0), /*immutable=*/true);
Assert.AreEqual(literal5.ComputeHashCode(), literal5.GetHashCode());
var literal6 = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.RoundingMode.RNE, /*immutable=*/true);
Assert.AreEqual(literal6.ComputeHashCode(), literal6.GetHashCode());
}
[Test()]
public void CachedHashCodeIdentifierExpr()
{
var id = new IdentifierExpr(Token.NoToken, "foo", BasicType.Bool, /*immutable=*/true);
Assert.AreEqual(id.ComputeHashCode(), id.GetHashCode());
var variable = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "foo2", BasicType.Int));
var id2 = new IdentifierExpr(Token.NoToken, variable, /*immutable=*/true);
Assert.AreEqual(id2.ComputeHashCode(), id2.GetHashCode());
}
[Test()]
public void CachedHashCodeNAryExpr()
{
var nary = new NAryExpr(Token.NoToken, new UnaryOperator(Token.NoToken, UnaryOperator.Opcode.Not), new List<Expr>() { Expr.True }, /*immutable=*/true);
Assert.AreEqual(nary.ComputeHashCode(), nary.GetHashCode());
}
[Test()]
public void CachedHashCodeBvExtractExpr()
{
var literalBv = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(0), 4, /*immutable=*/true);
var bvExtract = new BvExtractExpr(Token.NoToken, literalBv, 3, 0, /*immutable=*/true);
Assert.AreEqual(bvExtract.ComputeHashCode(), bvExtract.GetHashCode());
}
[Test()]
public void CachedHashCodeBvConcatExpr()
{
var literalBv = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(0), 4, /*immutable=*/true);
var bvConcat = new BvConcatExpr(Token.NoToken, literalBv, literalBv, /*immutable=*/true);
Assert.AreEqual(bvConcat.ComputeHashCode(), bvConcat.GetHashCode());
}
[Test()]
public void CachedHashCodeOldExpr()
{
var literalBv = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(0), 4, /*immutable=*/true);
var oldExpr = new OldExpr(Token.NoToken, literalBv, /*immutable=*/true);
Assert.AreEqual(oldExpr.ComputeHashCode(), oldExpr.GetHashCode());
}
[Test()]
public void CachedHashCodeForAllExpr()
{
var x = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var y = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var body = Expr.Gt (new IdentifierExpr (Token.NoToken, x, /*immutable=*/true),
new IdentifierExpr(Token.NoToken, y, /*immutable=*/true));
var forAll = new ForallExpr(Token.NoToken, new List<Variable> () {x, y }, body, /*immutable=*/ true);
Assert.AreEqual(forAll.ComputeHashCode(), forAll.GetHashCode());
}
[Test()]
public void CachedHashCodeExistsExpr()
{
var x = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var y = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var body = Expr.Gt (new IdentifierExpr (Token.NoToken, x, /*immutable=*/true),
new IdentifierExpr(Token.NoToken, y, /*immutable=*/true));
var exists = new ExistsExpr(Token.NoToken, new List<Variable> () {x, y }, body, /*immutable=*/ true);
Assert.AreEqual(exists.ComputeHashCode(), exists.GetHashCode());
}
[Test()]
public void CachedHashCodeLambdaExpr()
{
var x = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var y = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var body = Expr.Gt (new IdentifierExpr (Token.NoToken, x, /*immutable=*/true),
new IdentifierExpr(Token.NoToken, y, /*immutable=*/true));
var lambda = new LambdaExpr(Token.NoToken, new List<TypeVariable>(), new List<Variable>() { x, y},
null, body, /*immutable=*/true);
Assert.AreEqual(lambda.ComputeHashCode(), lambda.GetHashCode());
}
// Runtime immutability enforcement
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void IdentifierExprName()
{
var id = new IdentifierExpr(Token.NoToken, "foo", BasicType.Bool, /*immutable=*/true);
Assert.IsTrue(id.Immutable);
id.Name = "foo2";
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void IdentifierExprDecl()
{
var id = new IdentifierExpr(Token.NoToken, "foo", BasicType.Bool, /*immutable=*/true);
Assert.IsTrue(id.Immutable);
var typedIdent = new TypedIdent(Token.NoToken, "foo2", BasicType.Bool);
id.Decl = new GlobalVariable(Token.NoToken, typedIdent);
}
private NAryExpr GetUnTypedImmutableNAry()
{
var id = new IdentifierExpr(Token.NoToken, "foo", BasicType.Bool, /*immutable=*/true);
Assert.IsTrue(id.Immutable);
Assert.IsTrue(id.Type.IsBool);
var e = new NAryExpr(Token.NoToken, new BinaryOperator(Token.NoToken, BinaryOperator.Opcode.And), new List<Expr>() {
id,
id
}, /*immutable=*/true);
Assert.IsNull(e.Type);
Assert.IsTrue(e.Immutable);
return e;
}
[Test()]
public void ProtectedExprType()
{
var e = GetUnTypedImmutableNAry();
// Now Typecheck
// Even though it's immutable we allow the TypeCheck field to be set if the Expr has never been type checked
var TC = new TypecheckingContext(this);
e.Typecheck(TC);
Assert.IsNotNull(e.Type);
Assert.IsTrue(e.Type.IsBool);
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedExprChangeTypeFail()
{
var e = GetUnTypedImmutableNAry();
// Now Typecheck
// Even though it's immutable we allow the TypeCheck field to be set if the Expr has never been type checked
var TC = new TypecheckingContext(this);
e.Typecheck(TC);
Assert.IsNotNull(e.Type);
Assert.IsTrue(e.Type.IsBool);
// Trying to modify the Type to a different Type now should fail
e.Type = BasicType.Int;
}
[Test()]
public void ProtectedExprTypeChangeTypeSucceed()
{
var e = GetUnTypedImmutableNAry();
// Now Typecheck
// Even though it's immutable we allow the TypeCheck field to be set if the Expr has never been type checked
var TC = new TypecheckingContext(this);
e.Typecheck(TC);
Assert.IsNotNull(e.Type);
Assert.IsTrue(e.Type.IsBool);
// Trying to assign the same Type should succeed
e.Type = BasicType.Bool;
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedOldExpr()
{
var e = new OldExpr(Token.NoToken, Expr.True, /*immutable=*/ true);
e.Expr = Expr.False;
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedNAryFunc()
{
var e = GetUnTypedImmutableNAry();
e.Fun = new BinaryOperator(Token.NoToken, BinaryOperator.Opcode.Sub);
}
[Test(), ExpectedException(typeof(NotSupportedException))]
public void ProtectedNAryArgsList()
{
var e = GetUnTypedImmutableNAry();
e.Args.Add(null);
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedNAryArgs()
{
var e = GetUnTypedImmutableNAry();
e.Args = new List<Expr>();
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedForAllExprBody()
{
var x = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var y = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var xId = new IdentifierExpr(Token.NoToken, x, /*immutable=*/true);
var yId = new IdentifierExpr(Token.NoToken, y, /*immutable=*/true);
var body = Expr.Gt(xId, yId);
var forAll = new ForallExpr(Token.NoToken, new List<Variable> () { x, y }, body, /*immutable=*/true);
forAll.Body = Expr.Lt(xId, yId); // Changing the body of an immutable ForAllExpr should fail
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedExistsExprBody()
{
var x = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var y = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var xId = new IdentifierExpr(Token.NoToken, x, /*immutable=*/true);
var yId = new IdentifierExpr(Token.NoToken, y, /*immutable=*/true);
var body = Expr.Gt(xId, yId);
var exists = new ExistsExpr(Token.NoToken, new List<Variable> () { x, y }, body, /*immutable=*/true);
exists.Body = Expr.Lt(xId, yId); // Changing the body of an immutable ExistsExpr should fail
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedLambdaExprBody()
{
var x = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var y = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, "x", BasicType.Int));
var xId = new IdentifierExpr(Token.NoToken, x, /*immutable=*/true);
var yId = new IdentifierExpr(Token.NoToken, y, /*immutable=*/true);
var body = Expr.Gt(xId, yId);
var lambda = new LambdaExpr(Token.NoToken, new List<TypeVariable>(), new List<Variable>() { x, y},
null, body, /*immutable=*/true);
lambda.Body = Expr.Lt(xId, yId); // Changing the body of an immutable ExistsExpr should fail
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedBvConcatExprLhs()
{
var lhs = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(0), 32, /*immutable=*/true);
var rhs = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(1), 32, /*immutable=*/true);
var concat = new BvConcatExpr(Token.NoToken, lhs, rhs,/* immutable=*/true);
Assert.IsTrue(concat.Immutable);
concat.E0 = rhs; // Should throw
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedBvConcatExprRhs()
{
var lhs = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(0), 32, /*immutable=*/true);
var rhs = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(1), 32, /*immutable=*/true);
var concat = new BvConcatExpr(Token.NoToken, lhs, rhs,/* immutable=*/true);
Assert.IsTrue(concat.Immutable);
concat.E1 = lhs; // Should throw
}
[Test(), ExpectedException(typeof(InvalidOperationException))]
public void ProtectedBvExtract()
{
var bv = new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(0), 32, /*immutable=*/true);
var extract = new BvExtractExpr(Token.NoToken, bv, 32, 0, /*immutable=*/true);
Assert.IsTrue(extract.Immutable);
extract.Bitvector = bv; // Should throw
}
}
}
|