diff --git a/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java b/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java new file mode 100644 index 00000000000..c77756f2335 --- /dev/null +++ b/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.cfg; + +import com.google.common.collect.Sets; +import kotlin.Function1; +import kotlin.Function3; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.cfg.pseudocode.Instruction; +import org.jetbrains.jet.lang.cfg.pseudocode.InstructionImpl; +import org.jetbrains.jet.lang.cfg.pseudocode.PseudocodeImpl; + +import java.util.*; + +public abstract class AbstractControlFlowTest extends AbstractPseudocodeTest { + + @Override + protected void dumpInstructions(PseudocodeImpl pseudocode, @NotNull StringBuilder out) { + final int nextInstructionsColumnWidth = countNextInstructionsColumnWidth(pseudocode.getAllInstructions()); + + dumpInstructions(pseudocode, out, new Function3() { + @Override + public String invoke(Instruction instruction, Instruction next, Instruction prev) { + StringBuilder result = new StringBuilder(); + Collection nextInstructions = instruction.getNextInstructions(); + + if (!sameContents(next, nextInstructions)) { + result.append(" NEXT:").append( + String.format("%1$-" + nextInstructionsColumnWidth + "s", formatInstructionList(nextInstructions))); + } + Collection previousInstructions = instruction.getPreviousInstructions(); + if (!sameContents(prev, previousInstructions)) { + result.append(" PREV:").append(formatInstructionList(previousInstructions)); + } + return result.toString(); + } + }); + } + + private static String formatInstructionList(Collection instructions) { + StringBuilder sb = new StringBuilder(); + sb.append('['); + for (Iterator iterator = instructions.iterator(); iterator.hasNext(); ) { + Instruction instruction = iterator.next(); + String instructionText = instruction.toString(); + String[] parts = instructionText.split("\n"); + if (parts.length > 1) { + StringBuilder instructionSb = new StringBuilder(); + for (String part : parts) { + instructionSb.append(part.trim()).append(' '); + } + if (instructionSb.toString().length() > 30) { + sb.append(instructionSb.substring(0, 28)).append("..)"); + } + else { + sb.append(instructionSb); + } + } + else { + sb.append(instruction); + } + if (iterator.hasNext()) { + sb.append(", "); + } + } + sb.append(']'); + return sb.toString(); + } + + + private static int countNextInstructionsColumnWidth(List instructions) { + int maxWidth = 0; + for (Instruction instruction : instructions) { + String instructionListText = formatInstructionList(instruction.getNextInstructions()); + if (instructionListText.length() > maxWidth) { + maxWidth = instructionListText.length(); + } + } + return maxWidth; + } + + private static boolean sameContents(@Nullable Instruction natural, Collection actual) { + if (natural == null) { + return actual.isEmpty(); + } + return Collections.singleton(natural).equals(new HashSet(actual)); + } +} diff --git a/compiler/tests/org/jetbrains/jet/cfg/AbstractPseudocodeTest.java b/compiler/tests/org/jetbrains/jet/cfg/AbstractPseudocodeTest.java index 81a7833682f..37177ef5307 100644 --- a/compiler/tests/org/jetbrains/jet/cfg/AbstractPseudocodeTest.java +++ b/compiler/tests/org/jetbrains/jet/cfg/AbstractPseudocodeTest.java @@ -20,7 +20,6 @@ import com.google.common.collect.Sets; import com.intellij.openapi.util.text.StringUtil; import kotlin.Function3; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.analyzer.AnalyzeExhaust; @@ -157,59 +156,9 @@ public abstract class AbstractPseudocodeTest extends KotlinTestWithEnvironment { return sb.toString(); } - private static String formatInstructionList(Collection instructions) { - StringBuilder sb = new StringBuilder(); - sb.append('['); - for (Iterator iterator = instructions.iterator(); iterator.hasNext(); ) { - Instruction instruction = iterator.next(); - String instructionText = instruction.toString(); - String[] parts = instructionText.split("\n"); - if (parts.length > 1) { - StringBuilder instructionSb = new StringBuilder(); - for (String part : parts) { - instructionSb.append(part.trim()).append(' '); - } - if (instructionSb.toString().length() > 30) { - sb.append(instructionSb.substring(0, 28)).append("..)"); - } - else { - sb.append(instructionSb); - } - } - else { - sb.append(instruction); - } - if (iterator.hasNext()) { - sb.append(", "); - } - } - sb.append(']'); - return sb.toString(); - } + protected abstract void dumpInstructions(PseudocodeImpl pseudocode, @NotNull StringBuilder out); - private void dumpInstructions(PseudocodeImpl pseudocode, @NotNull StringBuilder out) { - final int nextInstructionsColumnWidth = countNextInstructionsColumnWidth(pseudocode.getAllInstructions()); - - dumpInstructions(pseudocode, out, new Function3() { - @Override - public String invoke(Instruction instruction, Instruction next, Instruction prev) { - StringBuilder result = new StringBuilder(); - Collection nextInstructions = instruction.getNextInstructions(); - - if (!sameContents(next, nextInstructions)) { - result.append(" NEXT:").append( - String.format("%1$-" + nextInstructionsColumnWidth + "s", formatInstructionList(nextInstructions))); - } - Collection previousInstructions = instruction.getPreviousInstructions(); - if (!sameContents(prev, previousInstructions)) { - result.append(" PREV:").append(formatInstructionList(previousInstructions)); - } - return result.toString(); - } - }); - } - - private void dumpInstructions( + protected void dumpInstructions( @NotNull PseudocodeImpl pseudocode, @NotNull StringBuilder out, @NotNull Function3 getInstructionData @@ -263,22 +212,4 @@ public abstract class AbstractPseudocodeTest extends KotlinTestWithEnvironment { } return maxWidth; } - - private static int countNextInstructionsColumnWidth(List instructions) { - int maxWidth = 0; - for (Instruction instruction : instructions) { - String instructionListText = formatInstructionList(instruction.getNextInstructions()); - if (instructionListText.length() > maxWidth) { - maxWidth = instructionListText.length(); - } - } - return maxWidth; - } - - private static boolean sameContents(@Nullable Instruction natural, Collection actual) { - if (natural == null) { - return actual.isEmpty(); - } - return Collections.singleton(natural).equals(new HashSet(actual)); - } }