Created
October 4, 2017 05:40
-
-
Save dulimarta/296c38c2f42ac8fae75d442015d7e866 to your computer and use it in GitHub Desktop.
CIS163 Lab: 2D Arrays Tester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cs163.arrays2d; | |
import cs163.arrays2d.TwoDimensionalArrays; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
/** | |
* Created by Hans Dulimarta (Fall 2017). | |
*/ | |
public class TwoDimensionalArraysTest { | |
@Test | |
public void sumOfOddTest () { | |
assertEquals (9, TwoDimensionalArrays.sumOfOdd(new int[][] {{1,2,3},{4,5,6}})); | |
assertEquals (37, TwoDimensionalArrays.sumOfOdd(new int[][] { | |
{1,2,3,5,7,2,6,4},{4,5,6,1,5,6,7,3} | |
})); | |
assertEquals (0, TwoDimensionalArrays.sumOfOdd(new int[][] {{2,2,6},{4,10,6}})); | |
assertEquals (0, TwoDimensionalArrays.sumOfOdd(new int[][] {{},{}})); | |
} | |
@Test | |
public void concatTest() { | |
assertEquals("to-succeed-in-life-you-need-three-things-a-wishbone" + | |
"-a-backbone-and-a-funny-bone", TwoDimensionalArrays.concatAll(new String[][]{ | |
{"to", "succeed", "in", "life", "you", "need", "three", "things"}, | |
{"a", "wishbone", "a", "backbone", "and", "a", "funny", "bone"} | |
})); | |
assertEquals("to-succeed-in-life-you-need-three-things-a-wishbone" + | |
"-a-backbone-and-a-funny-bone", TwoDimensionalArrays.concatAll(new String[][]{ | |
{"to", "succeed", "in", "life", "you", "need", "three", "things", | |
"a", "wishbone", "a", "backbone", "and", "a", "funny", "bone"} | |
})); | |
assertEquals("to-succeed-in-life-you-need-three-things-a-wishbone" + | |
"-a-backbone-and-a-funny-bone", TwoDimensionalArrays.concatAll(new String[][]{ | |
{"to", "succeed", "in", "life"}, {"you", "need", "three", "things"}, | |
{"a", "wishbone", "a", "backbone"}, {"and", "a", "funny", "bone"} | |
})); | |
assertEquals("to-succeed-in-life-you-need-three-things-a-wishbone" + | |
"-a-backbone-and-a-funny-bone", TwoDimensionalArrays.concatAll(new String[][]{ | |
{"to", "succeed"}, {"in", "life"}, {"you", "need"}, {"three", "things"}, | |
{"a", "wishbone"}, {"a", "backbone"}, {"and", "a"}, {"funny", "bone"} | |
})); | |
assertEquals("to-succeed-in-life-you-need-three-things-a-wishbone" + | |
"-a-backbone-and-a-funny-bone", TwoDimensionalArrays.concatAll(new String[][]{ | |
{"to"}, {"succeed"}, {"in"}, {"life"}, {"you"}, {"need"}, {"three"}, {"things"}, | |
{"a"}, {"wishbone"}, {"a"}, {"backbone"}, {"and"}, {"a"}, {"funny"}, {"bone"} | |
})); | |
} | |
@Test | |
public void convertGradeTest() { | |
assertArrayEquals(new char[][] {{'A','B','A'}, {'B', 'D','C'}}, | |
TwoDimensionalArrays.convertGrade(new int[][] {{94, 88, 95}, {84, 61, 75}})); | |
assertArrayEquals(new char[][] {{'F'}, {'B'}, {'D'},{'C'}}, | |
TwoDimensionalArrays.convertGrade(new int[][] {{59}, {88}, {62}, {77}})); | |
} | |
@Test | |
public void columnTotalTest() { | |
assertArrayEquals(new int[] { 19, 6}, | |
TwoDimensionalArrays.columnTotal(new int[][] {{3, 2},{7, 1}, {9,3}})); | |
assertArrayEquals(new int[] { 16, 26, 19, 29}, | |
TwoDimensionalArrays.columnTotal(new int[][] { | |
{3, 2, 7, 1}, {9, 3, 4, 10}, {1, 1, 4, 1}, {5, 16, 3, 10}, | |
{-2, 4, 1, 7}})); | |
} | |
@Test | |
public void increasingRowTest() { | |
assertTrue(TwoDimensionalArrays.increasingRow(new int[][] { | |
{1}, {2}, {8}, {7} | |
})); | |
assertTrue(TwoDimensionalArrays.increasingRow(new int[][] { | |
{10, 19, 31, 44}, {2, 3, 4, 7}, {60, 70, 75, 88} | |
})); | |
assertFalse(TwoDimensionalArrays.increasingRow(new int[][] { | |
{10, 19, 31, 44, 31} | |
})); | |
assertFalse(TwoDimensionalArrays.increasingRow(new int[][] { | |
{10, 19, 31, 44, 44} | |
})); | |
assertFalse(TwoDimensionalArrays.increasingRow(new int[][] { | |
{10, 10, 31, 44, 44} | |
})); | |
assertFalse(TwoDimensionalArrays.increasingRow(new int[][] { | |
{10, 9, 22, 28, 31} | |
})); | |
assertFalse(TwoDimensionalArrays.increasingRow(new int[][] { | |
{10, 19, 22, 28, 31}, | |
{10, 19, 22, 28, 31}, | |
{10, 19, 22, 20, 11}, | |
})); | |
} | |
@Test | |
public void rowTotalLeftTest() { | |
int[][] one = new int[][] { | |
{1, 5, 7, 10}, | |
{2, 0, 1, 4} | |
}; | |
TwoDimensionalArrays.rowTotalLeft(one); | |
assertArrayEquals(new int[][] { | |
{23, 0, 0, 0}, {7, 0, 0, 0} | |
}, one); | |
int[][] two = new int[][] { | |
{100, 0, 0, 0, 0, 0, 0}, | |
{220, 0, 0, 0, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.rowTotalLeft(two); | |
assertArrayEquals(new int[][] { | |
{100, 0, 0, 0, 0, 0, 0}, {220, 0, 0, 0, 0, 0, 0} | |
}, two); | |
} | |
@Test | |
public void compareHalvesTester() { | |
assertEquals (-1, TwoDimensionalArrays.compareLeftRightHalf( | |
new int[][] { | |
{2, 2, 3, 3}, | |
{1, 9, 0, 0}, | |
{0, 1, 10, 25} | |
})); | |
assertEquals (0, TwoDimensionalArrays.compareLeftRightHalf( | |
new int[][] { | |
{15, 2, 3, 3}, | |
{10, 9, 0, 0}, | |
{3, 2, 10, 25} | |
})); | |
assertEquals (1, TwoDimensionalArrays.compareLeftRightHalf( | |
new int[][] { | |
{15, 2, 3, 3}, | |
{10, 9, 0, 0}, | |
{3, 20, 10, 25} | |
})); | |
assertEquals (-1, TwoDimensionalArrays.compareLeftRightHalf( | |
new int[][] { | |
{2, 2, 10, 3, 3}, | |
{1, 9, 22, 0, 0}, | |
{0, 1, -9, 10, 25} | |
})); | |
assertEquals (0, TwoDimensionalArrays.compareLeftRightHalf( | |
new int[][] { | |
{15, 2, 0, 0, 0, 3, 3}, | |
{10, 9, 0, 0, 0, 0, 0}, | |
{3, 2, 0, 0, 0, 10, 25} | |
})); | |
assertEquals (1, TwoDimensionalArrays.compareLeftRightHalf( | |
new int[][] { | |
{15, 2, 22, 3, 3}, | |
{10, 9, -99, 0, 0}, | |
{3, 20, 14, 10, 25} | |
})); | |
} | |
@Test | |
public void replicateLeftRightTest() { | |
int[][] one = new int[][] { | |
{0, 0, 0, 0}, | |
{0, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.replicateToLeftRightCells(one); | |
assertArrayEquals(new int[][] { | |
{0, 0, 0, 0}, | |
{0, 0, 0, 0} | |
}, one); | |
int[][] two = new int[][] { | |
{0, 3, 0, 0, 0, 6, 0}, | |
{0, 0, 1, 0, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.replicateToLeftRightCells(two); | |
assertArrayEquals(new int[][] { | |
{3, 3, 3, 0, 6, 6, 6}, | |
{0, 1, 1, 1, 0, 0, 0} | |
}, two); | |
int[][] three = new int[][] { | |
{3, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 1, 0, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.replicateToLeftRightCells(three); | |
assertArrayEquals(new int[][] { | |
{3, 3, 0, 0, 0, 0, 0}, | |
{0, 1, 1, 1, 0, 0, 0} | |
}, three); | |
int[][] four = new int[][] { | |
{0, 2, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 5} | |
}; | |
TwoDimensionalArrays.replicateToLeftRightCells(four); | |
assertArrayEquals(new int[][] { | |
{2, 2, 2, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 5, 5} | |
}, four); | |
} | |
@Test | |
public void replicateDiagTest() { | |
int[][] one = new int[][] { | |
{0, 0, 0, 0}, | |
{0, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.replicateDiagonals(one); | |
assertArrayEquals(new int[][] { | |
{0, 0, 0, 0}, | |
{0, 0, 0, 0} | |
}, one); | |
int[][] two = new int[][] { | |
{0, 0, 0, 0}, | |
{0, 5, 0, 0}, | |
{0, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.replicateDiagonals(two); | |
assertArrayEquals(new int[][] { | |
{5, 0, 5, 0}, | |
{0, 5, 0, 0}, | |
{5, 0, 5, 0}, | |
}, two); | |
int[][] three = new int[][] { | |
{6, 0, 0, 0}, | |
{0, 0, 0, 0}, | |
{0, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.replicateDiagonals(three); | |
assertArrayEquals(new int[][] { | |
{6, 0, 0, 0}, | |
{0, 6, 0, 0}, | |
{0, 0, 0, 0} | |
}, three); | |
int[][] four = new int[][] { | |
{0, 0, 0, 5}, | |
{0, 0, 0, 0}, | |
{0, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.replicateDiagonals(four); | |
assertArrayEquals(new int[][] { | |
{0, 0, 0, 5}, | |
{0, 0, 5, 0}, | |
{0, 0, 0, 0} | |
}, four); | |
int[][] five = new int[][] { | |
{0, 0, 0, 0}, | |
{0, 0, 0, 0}, | |
{0, 0, 0, 7} | |
}; | |
TwoDimensionalArrays.replicateDiagonals(five); | |
assertArrayEquals(new int[][] { | |
{0, 0, 0, 0}, | |
{0, 0, 7, 0}, | |
{0, 0, 0, 7} | |
}, five); | |
int[][] six = new int[][] { | |
{0, 0, 0, 0}, | |
{0, 0, 0, 0}, | |
{3, 0, 0, 0} | |
}; | |
TwoDimensionalArrays.replicateDiagonals(six); | |
assertArrayEquals(new int[][] { | |
{0, 0, 0, 0}, | |
{0, 3, 0, 0}, | |
{3, 0, 0, 0} | |
}, six); | |
int[][] seven = new int[][] { | |
{0, 0, 0, 2, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
}; | |
TwoDimensionalArrays.replicateDiagonals(seven); | |
assertArrayEquals(new int[][] { | |
{0, 0, 0, 2, 0, 0, 0}, | |
{0, 0, 2, 0, 2, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
}, seven); | |
seven = new int[][] { | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 1}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
}; | |
TwoDimensionalArrays.replicateDiagonals(seven); | |
assertArrayEquals(new int[][] { | |
{0, 0, 0, 0, 0, 1, 0}, | |
{0, 0, 0, 0, 0, 0, 1}, | |
{0, 0, 0, 0, 0, 1, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
}, seven); | |
seven = new int[][] { | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 2, 0, 0}, | |
}; | |
TwoDimensionalArrays.replicateDiagonals(seven); | |
assertArrayEquals(new int[][] { | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0, 0, 0}, | |
{0, 0, 0, 2, 0, 2, 0}, | |
{0, 0, 0, 0, 2, 0, 0}, | |
}, seven); | |
} | |
@Test | |
public void inspectAllNeighborsTest() { | |
int[][] arr; | |
arr = new int[][] { | |
{0,0,0,0}, | |
{0,0,5,0}, | |
{0,0,0,0}, | |
}; | |
int[][] res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{0,1,1,1}, | |
{0,1,0,1}, | |
{0,1,1,1}, | |
}, res); | |
arr = new int[][] { | |
{0,0,0,0}, | |
{0,0,0,5}, | |
{0,0,0,0}, | |
}; | |
res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{0,0,1,1}, | |
{0,0,1,0}, | |
{0,0,1,1}, | |
}, res); | |
arr = new int[][] { | |
{0,0,0,0}, | |
{0,0,0,0}, | |
{0,0,4,0}, | |
}; | |
res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{0,0,0,0}, | |
{0,1,1,1}, | |
{0,1,0,1}, | |
}, res); | |
arr = new int[][] { | |
{0,0,0,0}, | |
{0,0,0,0}, | |
{6,0,0,0}, | |
}; | |
res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{0,0,0,0}, | |
{1,1,0,0}, | |
{0,1,0,0}, | |
}, res); | |
arr = new int[][] { | |
{0,0,0,0}, | |
{9,0,0,0}, | |
{0,0,0,0}, | |
}; | |
res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{1,1,0,0}, | |
{0,1,0,0}, | |
{1,1,0,0}, | |
}, res); | |
arr = new int[][] { | |
{0,0,0,0}, | |
{0,0,0,0}, | |
{9,0,0,2}, | |
{0,0,0,0}, | |
}; | |
res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{0,0,0,0}, | |
{1,1,1,1}, | |
{0,1,1,0}, | |
{1,1,1,1}, | |
}, res); | |
arr = new int[][] { | |
{0,0,0,0}, | |
{0,0,0,0}, | |
{9,0,7,0}, | |
{0,0,0,0}, | |
}; | |
res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{0,0,0,0}, | |
{1,2,1,1}, | |
{0,2,0,1}, | |
{1,2,1,1}, | |
}, res); | |
arr = new int[][] { | |
{0,0,0,0}, | |
{0,0,5,0}, | |
{9,0,0,0}, | |
{0,0,0,0}, | |
}; | |
res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{0,1,1,1}, | |
{1,2,0,1}, | |
{0,2,1,1}, | |
{1,1,0,0}, | |
}, res); | |
arr = new int[][] { | |
{0,0,0,0}, | |
{0,0,5,0}, | |
{9,0,0,0}, | |
{0,0,8,0}, | |
}; | |
res = TwoDimensionalArrays.infectAllNeighbors(arr); | |
assertArrayEquals(new int[][] { | |
{0,1,1,1}, | |
{1,2,0,1}, | |
{0,3,2,2}, | |
{1,2,0,1}, | |
}, res); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment