Nikola
Nikola

Reputation: 890

JUnit test not completing

I treid to run some test in JUnit but none of them got completed and I had to terminate the test as it did not exit on its own. I add all necessary classes below:

enter image description here

Test are like this:

import static org.junit.Assert.assertEquals;

import java.util.Arrays;

import org.junit.Test;



public class AnagramTest {

Anagram an;

@Test 
public void testSpecialCharacters() {
    an = new Anagram("@#$%^&!@");
    for (int i=0;i<an.generate().size();i++)
        assertEquals("Special Characters ", isAnagram("@#$%^&!@",String.valueOf(an.generate().get(i))), true);
}


@Test 
public void testDoubleLetter() {
    an = new Anagram("bc");
    String[] expected = {"bc","cb"};
    for (int i=0;i<expected.length;i++)
        assertEquals("Double letter", expected[i], an.generate().get(i));
}

public static boolean isAnagram(String str1, String str2){

    if(str1.length() != str2.length()) {return false;}

    char[] a, b;
    Arrays.sort(a = str1.toCharArray());
    Arrays.sort(b = str2.toCharArray());
    return Arrays.equals(a,b);
  }...

And Anagram class:

import java.util.ArrayList;
import java.util.List;

public class Anagram {

private char[] input;
private StringBuffer sb = new StringBuffer();
private List output;

public Anagram(String input){ 
   this.input = input.toCharArray();
}

public List generate(){
  output = new ArrayList();
  doAnagram(input.length);
  return output;
}

private void doAnagram(int rsize){
     if (rsize==1){ return; }
     for (int i=0; i < rsize ; i++){
     doAnagram(rsize-1);
     if (rsize==2){
         for (int j=0; j < input.length; j++){
         sb.append(input[j]);
         }
         output.add(sb.toString());
         sb.delete(0,input.length+1);
     }
     rotate(rsize);
      }
  }

 private void rotate(int rsize){
     int i;
     int pos = input.length - rsize;
     char tmp = input[pos];
     for (i=pos + 1; i < input.length ; i++){
     input[i-1] = input[i];
     }
     input[i-1] = tmp;
 }
 }

What am I doing wrong?

Upvotes: 0

Views: 2330

Answers (1)

SiKing
SiKing

Reputation: 10329

In your test, in the for loop, an.generate().size() is 40320 - that might take a while to loop over! This value is also rebuilt every iteration of the loop, which is bad design.

Upvotes: 1

Related Questions