Reputation: 29
I did a sort method for sorting array in ascendent order:
public class Sorting {
public void sort(int[] array) {
if (Objects.isNull(array)) {
throw new UnsupportedOperationException();
} else {
Arrays.sort(array);
System.out.println(array);
}
}
}
I have a SortingTest class:
public class SortingTest {
Sorting sorting = new Sorting();
@Test
public void testNullCase() {
int [] expected = null;
int [] actual = null;
assertArrayEquals(expected, actual);
}
}
and I test and testEmptyCase()
, testSingleElementArrayCase()
, public void testSortedArraysCase()
and testOtherCases()
.
I test my program for sorting an array, but for below @test methods failure. How to do the test right? I want to understand what I need to do do what in the test is expected. Thanks for help.
@Test
public void testNullCarelessSorting() {
JUnitCore junit = new JUnitCore();
Result result = junit.run(NullCarelessSortingTestExtension.class);
assertEquals(1, result.getFailureCount());
assertEquals(5, result.getRunCount());
Failure nullCaseFailure = null;
}
@Test
public void testLazySorting() {
JUnitCore junit = new JUnitCore();
Result result = junit.run(LazySortingTestExtension.class);
assertEquals(2, result.getFailureCount());
assertEquals(5, result.getRunCount());
Failure nullCaseFailure = null;
Failure otherCasesFailure = null;
}
@Test
public void testTrickySorting() {
JUnitCore junit = new JUnitCore();
Result result = junit.run(TrickySortingTestExtension.class);
assertEquals(4, result.getFailureCount());
assertEquals(5, result.getRunCount());
Failure emptyCaseFailure = null;
Failure singleCaseFailure = null;
Failure sortedCaseFailure = null;
Failure otherCasesFailure = null;
}
Upvotes: 0
Views: 412
Reputation: 6466
Your for loop needs some correction as shown below:
public class Average
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final int ARRAY_SIZE = 100;
int[] n = new int[ARRAY_SIZE];
int sum = 0;
int count = 0;
System.out.println("Input: ");
for (int i = 0; i < ARRAY_SIZE; i++) {
int a = scanner.nextInt();
n[i] = a;
sum += n[i];
count++;
if (a == 0)
{
break;
}
}
System.out.println(count <= 1 ? "Output : " + (sum) : "Output : " + (sum / (count - 1)));
}
}
What was happening in your logic is that the first index (0) keeps repeating as there is another do-while loop inside your main for loop which keeps consuming your scanner inputs.
Upvotes: 3