PageRenderTime 45ms CodeModel.GetById 33ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/test/java/com/google/test/metric/method/BlockTest.java

http://testability-explorer.googlecode.com/
Java | 139 lines | 100 code | 23 blank | 16 comment | 1 complexity | a667cb0efe73de3cec91c86307000b90 MD5 | raw file
  1/*
  2 * Copyright 2007 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.test.metric.method;
 17
 18import static java.util.Arrays.asList;
 19
 20import java.util.List;
 21
 22import junit.framework.TestCase;
 23
 24import com.google.test.metric.FieldInfo;
 25import com.google.test.metric.JavaType;
 26import com.google.test.metric.Type;
 27import com.google.test.metric.Variable;
 28import com.google.test.metric.method.op.stack.GetField;
 29import com.google.test.metric.method.op.stack.Invoke;
 30import com.google.test.metric.method.op.stack.Load;
 31import com.google.test.metric.method.op.stack.PutField;
 32import com.google.test.metric.method.op.turing.MethodInvocation;
 33import com.google.test.metric.method.op.turing.Operation;
 34
 35public class BlockTest extends TestCase {
 36
 37  private static final Type OBJECT = JavaType.fromClass(Object.class);
 38
 39  public void testBlockToString() throws Exception {
 40    Block block = new Block("1");
 41    assertEquals("Block[1]{\n}", block.toString());
 42
 43    block.addOp(new Load(-1, var(1)));
 44    assertEquals("Block[1]{\n  load 1{java.lang.Object}\n}", block.toString());
 45  }
 46
 47  public void testVariableStaticAssignment() throws Exception {
 48    Block block = new Block("1");
 49    block.addOp(new Load(-1, var(1)));
 50    block.addOp(new PutField(-1, new FieldInfo(null, "abc", OBJECT,
 51        false, true, false)));
 52
 53    List<Operation> operations = new Stack2Turing(block).translate();
 54    assertEquals("[null.abc{java.lang.Object} <- 1{java.lang.Object}]", operations.toString());
 55  }
 56
 57  public void testVariableAssignment() throws Exception {
 58    Block block = new Block("1");
 59    block.addOp(new Load(-1, var("this"))); // this
 60    block.addOp(new Load(-1, var(1)));
 61    block.addOp(new PutField(-1, new FieldInfo(null, "abc", OBJECT,
 62        false, false, false)));
 63
 64    List<Operation> operations = new Stack2Turing(block).translate();
 65    assertEquals("[null.abc{java.lang.Object} <- 1{java.lang.Object}]", operations.toString());
 66  }
 67
 68  public void testGetField() throws Exception {
 69    Block block = new Block("1");
 70    block.addOp(new GetField(-1, new FieldInfo(null, "src", OBJECT,
 71        false, true, false)));
 72    block.addOp(new PutField(-1, new FieldInfo(null, "dst", OBJECT,
 73        false, true, false)));
 74
 75    List<Operation> operations = new Stack2Turing(block).translate();
 76    assertEquals("[null.dst{java.lang.Object} <- null.src{java.lang.Object}]", operations
 77        .toString());
 78  }
 79
 80  public void testMethodInvocation() throws Exception {
 81    Block block = new Block("1");
 82    block.addOp(new Load(-1, var("methodThis"))); // this
 83    block.addOp(new GetField(-1, new FieldInfo(null, "p1", OBJECT,
 84        false, true, false)));
 85    block.addOp(new GetField(-1, new FieldInfo(null, "p2", OBJECT,
 86        false, true, false)));
 87    block.addOp(new Invoke(-1, null, "int methodA(int, int)", asList(JavaType.INT,
 88        JavaType.INT), false, OBJECT));
 89    block.addOp(new PutField(-1, new FieldInfo(null, "dst", OBJECT,
 90        false, true, false)));
 91
 92    List<Operation> operations = new Stack2Turing(block).translate();
 93    assertEquals("[null:int methodA(int, int), null.dst{java.lang.Object} <- ?{java.lang.Object}]",
 94        operations.toString());
 95  }
 96
 97  private Variable var(Object value) {
 98    return new Constant(value, OBJECT);
 99  }
100
101  public void testDiamondBlockArrangment() throws Exception {
102    Block root = new Block("root");
103    Block branchA = new Block("branchA");
104    Block branchB = new Block("branchB");
105    Block joined = new Block("joined");
106    root.addNextBlock(branchA);
107    root.addNextBlock(branchB);
108    branchA.addNextBlock(joined);
109    branchB.addNextBlock(joined);
110
111    root.addOp(new Load(-1, var("this")));
112    root.addOp(new Load(-1, var("root")));
113    branchA.addOp(new Load(-1, var("A")));
114    branchB.addOp(new Load(-1, var("B")));
115    joined.addOp(new Load(-1, var("joined")));
116    joined.addOp(new Invoke(-1, null, "void m(int, int, int)", asList(JavaType.INT,
117        JavaType.INT, JavaType.INT), false, JavaType.VOID));
118
119    List<Operation> operations = new Stack2Turing(root).translate();
120    assertEquals(2, operations.size());
121    MethodInvocation mB = (MethodInvocation) operations.get(0);
122    MethodInvocation mA = (MethodInvocation) operations.get(1);
123    // since we use hash order is non-deterministic
124    if (mB.getParameters().get(1).toString().startsWith("A")) {
125      MethodInvocation temp = mB;
126      mB = mA;
127      mA = temp;
128    }
129
130    assertEquals("[root{java.lang.Object}, A{java.lang.Object}, joined{java.lang.Object}]", mA
131        .getParameters().toString());
132    assertEquals("this{java.lang.Object}", mA.getMethodThis().toString());
133
134    assertEquals("[root{java.lang.Object}, B{java.lang.Object}, joined{java.lang.Object}]", mB
135        .getParameters().toString());
136    assertEquals("this{java.lang.Object}", mB.getMethodThis().toString());
137  }
138
139}