Add some Manual UnitTest to ArrayUtil Test

This commit is contained in:
Siwat Sirichai 2020-10-29 14:03:11 +07:00
parent bf69b7fc3b
commit 0350952979
5 changed files with 46 additions and 10 deletions

View File

@ -14,9 +14,9 @@ public class ArrayUtility {
int length = 0; //Initialize length to store the column as we will pass this to the array constructor
for(int i = 0;i < d.length;i++)length+=d[i].length; //SUM of X(nY) = length
int[] mergedArray = new int[length]; // Create a new array with size equal to length
int currentIndex = 0; //Create a new variable to store the 2D Array Index
int currentIndex = 0; //Create a new variable to store the 1D Array Index
for(int i = 0;i < d.length;i++)for(int j = 0;j < d[i].length;j++) { //Loop through every Row and In each iteration of row, loop through every column
mergedArray[currentIndex] = d[i][j]; //Set the 2D array element CI to 3D's IJ
mergedArray[currentIndex] = d[i][j]; //Set the 1D array element CI to 2D's IJ
currentIndex++; //Move the cursor to the next element
}
return mergedArray;

View File

@ -1,16 +1,49 @@
public class ArrayUtilityTest {
public static void main(String[] args) {
testMerge();
testSplit();
// testMerge();
// testSplit();
myTestSplit();
myTestMerge();
}
public static void myTestSplit() {
int[] myArray = { 1, 8, 3, 1, 5, 3, 7, 5, 3 };
// add your code and some testcases the test split() similar to testSplit()
ArrayUtility.show(ArrayUtility.split(myArray, 1));
System.out.println("Expected:\n[1, 8, 3, 1, 5, 3, 7, 5, 3]\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 2));
System.out.println("Expected: Array size 9 cannot be splitted into 2 parts\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 3));
System.out.println("Expected:\n[1, 1, 7]\n[8, 5, 5]\n[3, 3, 3]\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 4));
System.out.println("Expected: Array size 9 cannot be splitted into 4 parts\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 5));
System.out.println("Expected: Array size 9 cannot be splitted into 5 parts\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 6));
System.out.println("Expected: Array size 9 cannot be splitted into 6 parts\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 7));
System.out.println("Expected: Array size 9 cannot be splitted into 7 parts\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 8));
System.out.println("Expected: Array size 9 cannot be splitted into 8 parts\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 10));
System.out.println("Expected: n cannot be greater than array's size, 9\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, 0));
System.out.println("Expected: n must be >= 0\n----------");
ArrayUtility.show(ArrayUtility.split(myArray, -1));
System.out.println("Expected: n must be >= 0\n----------");
}
public static void myTestMerge() {
// add your code and some testcases the test merge() similar to testMerge()
int[][] myArray = { { 6, 13, 56 }, { 92, 55 }, { 12, 32, 13, 92 }, { 10 } };
ArrayUtility.show(ArrayUtility.merge(myArray));
System.out.println("[6, 13, 56, 92, 55, 12, 32, 13, 92, 10]] - Expected");
int[][] myArray2 = {{145},{134,423,42},{33,44,13}};
ArrayUtility.show(ArrayUtility.merge(myArray2));
System.out.println("[145, 134, 423, 42, 33, 44, 13] - Expected");
}
// do no modify this method

View File

@ -1,3 +1,4 @@
//This Code is written bt Siwat Sirichai
import java.io.File;
import java.io.FileNotFoundException;
@ -16,15 +17,17 @@ public class CSVUtility {
*/
public static String[] readFile(String filename) throws FileNotFoundException {
// ENTER CODE HERE
Scanner sc = new Scanner(new File(filename));
int length = 0;
for (length = 0; sc.hasNext(); length++)
Scanner sc = new Scanner(new File(filename)); // Create a Scanner to take in the file at path filename
int length = 0; //Create a length variable and initialize it to zero
for (length = 0; sc.hasNext(); length++)
sc.nextLine();
sc.close(); //Close the Scanner to prevent resource leak
String[] filedata = new String[length];
sc = new Scanner(new File(filename));
sc = new Scanner(new File(filename)); //Reinitialize the closed scanner
for (int i = 0; i < length; i++)
filedata[i] = sc.nextLine();
return filedata;
sc.close(); //Close the Scanner to prevent resource leak
return filedata; //Return
}
/**