From b9c7e1e328416d53d1c836a8335a8db9a3b9ce56 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 5 Mar 2014 14:10:54 +0400 Subject: [PATCH] refactoring: extracted methods computeInstructionColumnWidth, computeNextInstructionsColumnWidth --- .../jet/cfg/AbstractControlFlowTest.java | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java b/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java index 00b5e641aee..43981ccd99a 100644 --- a/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java +++ b/compiler/tests/org/jetbrains/jet/cfg/AbstractControlFlowTest.java @@ -208,30 +208,9 @@ public abstract class AbstractControlFlowTest extends KotlinTestWithEnvironment List instructions = pseudocode.getAllInstructions(); Set remainedAfterPostProcessInstructions = Sets.newHashSet(pseudocode.getInstructions()); List labels = pseudocode.getLabels(); - int maxLength = 0; - int maxNextLength = 0; - for (Instruction instruction : instructions) { - String instuctionText = instruction.toString(); - if (instuctionText.length() > maxLength) { - String[] parts = instuctionText.split("\n"); - if (parts.length > 1) { - for (String part : parts) { - if (part.length() > maxLength) { - maxLength = part.length(); - } - } - } - else { - if (instuctionText.length() > maxLength) { - maxLength = instuctionText.length(); - } - } - } - String instructionListText = formatInstructionList(instruction.getNextInstructions()); - if (instructionListText.length() > maxNextLength) { - maxNextLength = instructionListText.length(); - } - } + int instructionColumnWidth = countInstructionColumnWidth(instructions); + int nextInstructionsColumnWidth = countNextInstructionsColumnWidth(instructions); + for (int i = 0; i < instructions.size(); i++) { Instruction instruction = instructions.get(i); for (PseudocodeImpl.PseudocodeLabel label: labels) { @@ -242,13 +221,14 @@ public abstract class AbstractControlFlowTest extends KotlinTestWithEnvironment StringBuilder line = new StringBuilder(); - line.append(formatInstruction(instruction, maxLength, remainedAfterPostProcessInstructions)); + line.append(formatInstruction(instruction, instructionColumnWidth, remainedAfterPostProcessInstructions)); // Only print NEXT and PREV if the values are non-trivial Instruction next = i == instructions.size() - 1 ? null : instructions.get(i + 1); Collection nextInstructions = instruction.getNextInstructions(); if (!sameContents(next, nextInstructions)) { - line.append(" NEXT:").append(String.format("%1$-" + maxNextLength + "s", formatInstructionList(nextInstructions))); + line.append(" NEXT:").append( + String.format("%1$-" + nextInstructionsColumnWidth + "s", formatInstructionList(nextInstructions))); } Instruction prev = i == 0 ? null : instructions.get(i - 1); @@ -261,6 +241,40 @@ public abstract class AbstractControlFlowTest extends KotlinTestWithEnvironment } } + private static int countInstructionColumnWidth(List instructions) { + int maxWidth = 0; + for (Instruction instruction : instructions) { + String instuctionText = instruction.toString(); + if (instuctionText.length() > maxWidth) { + String[] parts = instuctionText.split("\n"); + if (parts.length > 1) { + for (String part : parts) { + if (part.length() > maxWidth) { + maxWidth = part.length(); + } + } + } + else { + if (instuctionText.length() > maxWidth) { + maxWidth = instuctionText.length(); + } + } + } + } + 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();