-2

So, I was trying to code some test cases for a Java project I'm doing, and I decided that I wanted to move them into a function of their own, and choose between them using a parameter of the test function, rather than just using commenting and uncommenting of code, as I'm currently doing. However, I hit a snag: the code in question involves the initialisation of array variables, and if I try to initialise them inside an if-else statement, then the other pieces of code later on aren't capable of seeing them due to scope issues. Additionally, in Java, arrays are of a fixed size, and some of the test cases involve arrays of different sizes (including empty arrays), so simply creating the array before hand doesn't work.

Here's the code I've already tried (with arrayGen being a function that creates an integer array of n elements, with pseudorandom values that lie between the lower and upper bounds, inclusive). Note that in order to change which test case I'm running, I need to comment out the current test case and uncomment the test case I want to run; I'd like to replace this with a series of if-else if-else statements if possible.

    System.out.println("Unsorted:");
    int[] unsorted = arrayGen(n,lower,upper);
    //sorted array:
    //int[] unsorted = new int[n];
    //for (int i=0;i<n;i++)
    //{
    //    if (lower+i<upper)
    //    {
    //        unsorted[i]=lower+i;
    //    }
    //    else unsorted[i]=upper;
    //}
    //reverse sorted array:
    //int[] unsorted = new int[n];
    //for (int i=0;i<n;i++)
    //{
    //    if (upper-i>lower)
    //    {
    //        unsorted[i]=upper-i;
    //    }
    //    else unsorted[i]=lower;
    //}
    //array of static numbers:
    //int[] unsorted = arrayGen(n,upper,upper);
    //empty array:
    //int[] unsorted = arrayGen(0,lower,upper);

Is there any way to fix this, and have a neater version of my code, or am I going to have to be stuck with just commenting and uncommenting the test cases I want to use?

15
  • 1
    Learn to use either JUnit or TestNG. Commented Apr 25, 2019 at 6:10
  • @DawoodibnKareem If you'd like to explain how they would help me, feel free to post how they'd help in the answers. This is a legitimate question, and as far as I could tell it hadn't been asked before; I'm not sure why it's getting downvoted. Commented Apr 25, 2019 at 6:13
  • @nick012000 I don't see any code only comments... I did not understand what exactly is your problem as what you've described as a problem just requires a better technique to get over it... As of tests -> use any Test framework and then use google to find any, and again use google to find resources on learning any... Commented Apr 25, 2019 at 6:15
  • 1
    Testing frameworks like JUnit are specifically designed to help test code. Since you're asking about testing code, they seem relevant. Commented Apr 25, 2019 at 6:15
  • Well, I didn't downvote you (although I will downvote any answer that doesn't use a testing framework) so I can't comment on why you've been downvoted. But if you look for the web sites relating to JUnit or TestNG, you'll find tutorials you can follow. There's not a whole lot of point in my reproducing all that kind of material here. I've told you what to google. Commented Apr 25, 2019 at 6:20

1 Answer 1

1

Maybe this can give you an idea.

int[] unsorted = makeTestData("random");

private int[] makeTestData(String type, int n, int lower, int upper){
  switch(type){
    case "random":  return arrayGen(n,lower,upper);
    case "static":  return arrayGen(n, upper, upper);
    case "empty":   return new int[0];
    case "sorted":
       int[] toSort = arrayGen(n, lower, upper);
       Arrays.sort(toSort);
       return toSort;
    default: throw new IllegalArgumentException(type);
   }
}

If you run into scoping issues about re-defined variables, try moving things into their own self-contained functions.

But seriously, try using one of the established testing frameworks like JUnit. They give you a lot of the "boilerplate" stuff to run and report on tests for free.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.