    1   package test.net.sourceforge.pmd.rules.optimization;
    2
    3   import net.sourceforge.pmd.PMD;
    4   import net.sourceforge.pmd.Rule;
    5   import net.sourceforge.pmd.RuleSetNotFoundException;
    6   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    7   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    8
    9   public class AvoidArrayLoopsTest extends SimpleAggregatorTst {
    10
    11      private Rule rule;
    12
    13      public void setUp() throws RuleSetNotFoundException {
    14          rule = findRule("optimizations", "AvoidArrayLoops");
    15      }
    16
    17      public void testAll() {
    18          runTests(new TestDescriptor[]{
    19              new TestDescriptor(TEST1, "copy index into array", 0, rule),
    20              new TestDescriptor(TEST2, "copy one array to another", 1, rule),
    21              new TestDescriptor(TEST3, "copy via while loop", 1, rule),
    22              new TestDescriptor(TEST4, "copy involving multiple arrays is ok", 0, rule),
    23              new TestDescriptor(TEST5, "copy involving method invocation on array element is ok", 0, rule),
    24              new TestDescriptor(TEST6, "using an offset, still bad", 1, rule),
    25              new TestDescriptor(TEST7, "nested arrays on LHS, ok", 0, rule),
    26          });
    27      }
    28
    29      private static final String TEST1 =
    30              "public class Foo {" + PMD.EOL +
    31              " public void bar() {" + PMD.EOL +
    32              "   for (int i=0;i<10;i++) {" + PMD.EOL +
    33              "       a[i] = i;" + PMD.EOL +
    34              "   }" + PMD.EOL +
    35              " }" + PMD.EOL +
    36              "}";
    37
    38      private static final String TEST2 =
    39              "public class Foo {" + PMD.EOL +
    40              " public void bar() {" + PMD.EOL +
    41              "   for (int i=0;i<10;i++) {" + PMD.EOL +
    42              "       a[i] = b[i];" + PMD.EOL +
    43              "   }" + PMD.EOL +
    44              " }" + PMD.EOL +
    45              "}";
    46
    47      private static final String TEST3 =
    48              "public class Foo {" + PMD.EOL +
    49              " public void bar() {" + PMD.EOL +
    50              "  int i = 0;" + PMD.EOL +
    51              "  while (i < 10) {" + PMD.EOL +
    52              "   a[i] = b[i];" + PMD.EOL +
    53              "   i++;" + PMD.EOL +
    54              "  }" + PMD.EOL +
    55              " }" + PMD.EOL +
    56              "}";
    57
    58      private static final String TEST4 =
    59              "public class Foo {" + PMD.EOL +
    60              " public void bar() {" + PMD.EOL +
    61              "  for (int i=0;i<10;i++) {" + PMD.EOL +
    62              "   x[i] = b[i] + 1;" + PMD.EOL +
    63              "   y[i] = a[i] + 2;" + PMD.EOL +
    64              "  }" + PMD.EOL +
    65              " }" + PMD.EOL +
    66              "}";
    67
    68      private static final String TEST5 =
    69              "public class Foo {" + PMD.EOL +
    70              " public void bar() {" + PMD.EOL +
    71              "  for (int i=0;i<10;i++) {" + PMD.EOL +
    72              "   a[i] = b[i].size();" + PMD.EOL +
    73              "  }" + PMD.EOL +
    74              " }" + PMD.EOL +
    75              "}";
    76
    77      private static final String TEST6 =
    78              "public class Foo {" + PMD.EOL +
    79              " public void bar() {" + PMD.EOL +
    80              "  for (int i=0;i<10;i++) {" + PMD.EOL +
    81              "   b[i]=a[i+6];" + PMD.EOL +
    82              "  }" + PMD.EOL +
    83              " }" + PMD.EOL +
    84              "}";
    85
    86      private static final String TEST7 =
    87              "public class Foo {" + PMD.EOL +
    88              " public void bar() {" + PMD.EOL +
    89              "  for (int i=0;i<10;i++) {" + PMD.EOL +
    90              "   b[c[i]] = a[i];" + PMD.EOL +
    91              "  }" + PMD.EOL +
    92              " }" + PMD.EOL +
    93              "}";
    94  }