| 1 | using System; |
| 2 | using System.Linq; |
| 3 | |
| 4 | using Mono.Cecil; |
| 5 | using Mono.Cecil.Cil; |
| 6 | |
| 7 | using NUnit.Framework; |
| 8 | |
| 9 | namespace Mono.Cecil.Tests { |
| 10 | |
| 11 | [TestFixture] |
| 12 | public class VariableTests : BaseTestFixture { |
| 13 | |
| 14 | [Test] |
| 15 | public void AddVariableIndex () |
| 16 | { |
| 17 | var object_ref = new TypeReference ("System", "Object", null, null, false); |
| 18 | var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref); |
| 19 | var body = new MethodBody (method); |
| 20 | |
| 21 | var x = new VariableDefinition ("x", object_ref); |
| 22 | var y = new VariableDefinition ("y", object_ref); |
| 23 | |
| 24 | body.Variables.Add (x); |
| 25 | body.Variables.Add (y); |
| 26 | |
| 27 | Assert.AreEqual (0, x.Index); |
| 28 | Assert.AreEqual (1, y.Index); |
| 29 | } |
| 30 | |
| 31 | [Test] |
| 32 | public void RemoveAtVariableIndex () |
| 33 | { |
| 34 | var object_ref = new TypeReference ("System", "Object", null, null, false); |
| 35 | var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref); |
| 36 | var body = new MethodBody (method); |
| 37 | |
| 38 | var x = new VariableDefinition ("x", object_ref); |
| 39 | var y = new VariableDefinition ("y", object_ref); |
| 40 | var z = new VariableDefinition ("z", object_ref); |
| 41 | |
| 42 | body.Variables.Add (x); |
| 43 | body.Variables.Add (y); |
| 44 | body.Variables.Add (z); |
| 45 | |
| 46 | Assert.AreEqual (0, x.Index); |
| 47 | Assert.AreEqual (1, y.Index); |
| 48 | Assert.AreEqual (2, z.Index); |
| 49 | |
| 50 | body.Variables.RemoveAt (1); |
| 51 | |
| 52 | Assert.AreEqual (0, x.Index); |
| 53 | Assert.AreEqual (-1, y.Index); |
| 54 | Assert.AreEqual (1, z.Index); |
| 55 | } |
| 56 | |
| 57 | [Test] |
| 58 | public void RemoveVariableIndex () |
| 59 | { |
| 60 | var object_ref = new TypeReference ("System", "Object", null, null, false); |
| 61 | var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref); |
| 62 | var body = new MethodBody (method); |
| 63 | |
| 64 | var x = new VariableDefinition ("x", object_ref); |
| 65 | var y = new VariableDefinition ("y", object_ref); |
| 66 | var z = new VariableDefinition ("z", object_ref); |
| 67 | |
| 68 | body.Variables.Add (x); |
| 69 | body.Variables.Add (y); |
| 70 | body.Variables.Add (z); |
| 71 | |
| 72 | Assert.AreEqual (0, x.Index); |
| 73 | Assert.AreEqual (1, y.Index); |
| 74 | Assert.AreEqual (2, z.Index); |
| 75 | |
| 76 | body.Variables.Remove (y); |
| 77 | |
| 78 | Assert.AreEqual (0, x.Index); |
| 79 | Assert.AreEqual (-1, y.Index); |
| 80 | Assert.AreEqual (1, z.Index); |
| 81 | } |
| 82 | |
| 83 | [Test] |
| 84 | public void InsertVariableIndex () |
| 85 | { |
| 86 | var object_ref = new TypeReference ("System", "Object", null, null, false); |
| 87 | var method = new MethodDefinition ("foo", MethodAttributes.Static, object_ref); |
| 88 | var body = new MethodBody (method); |
| 89 | |
| 90 | var x = new VariableDefinition ("x", object_ref); |
| 91 | var y = new VariableDefinition ("y", object_ref); |
| 92 | var z = new VariableDefinition ("z", object_ref); |
| 93 | |
| 94 | body.Variables.Add (x); |
| 95 | body.Variables.Add (z); |
| 96 | |
| 97 | Assert.AreEqual (0, x.Index); |
| 98 | Assert.AreEqual (-1, y.Index); |
| 99 | Assert.AreEqual (1, z.Index); |
| 100 | |
| 101 | body.Variables.Insert (1, y); |
| 102 | |
| 103 | Assert.AreEqual (0, x.Index); |
| 104 | Assert.AreEqual (1, y.Index); |
| 105 | Assert.AreEqual (2, z.Index); |
| 106 | } |
| 107 | } |
| 108 | } |