package edu.hawaii.ics.yucheng; import junit.framework.TestCase; /** * Tests the Matrix class. */ public class MatrixTest extends TestCase { /** * Tests the clone method. * * @throws Exception */ public void testClone() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); m.set(0, 0, "A"); m.set(0, 1, "B"); final Matrix<String> m2 = m.clone(); assertEquals(1, m2.rows()); assertEquals(2, m2.columns()); assertEquals("A", m2.get(0, 0)); assertEquals("B", m2.get(0, 1)); } /** * Tests the columns method. * * @throws Exception */ public void testColumns() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); assertEquals(2, m.columns()); } /** * Tests the constructor. * * @throws Exception */ public void testConstructor1a() throws Exception { try { new Matrix<String>(0, 1, null); fail(); } catch (final IllegalArgumentException e) { // This is expected. } } /** * Tests the constructor. * * @throws Exception */ public void testConstructor1b() throws Exception { try { new Matrix<String>(1, 0, null); fail(); } catch (final IllegalArgumentException e) { // This is expected. } } /** * Tests the constructor. * * @throws Exception */ public void testConstructor1c() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, "A"); assertEquals(1, m.rows()); assertEquals(2, m.columns()); assertEquals("A", m.get(0, 0)); assertEquals("A", m.get(0, 1)); } /** * Tests the get method. * * @throws Exception */ public void testGet1() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); try { m.get(1, 0); fail(); } catch (final IndexOutOfBoundsException e) { // This is expected. } } /** * Tests the get method. * * @throws Exception */ public void testGet2() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); try { m.get(0, 2); fail(); } catch (final IndexOutOfBoundsException e) { // This is expected. } } /** * Tests the get method. * * @throws Exception */ public void testGet3() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); m.set(0, 0, "A"); m.set(0, 1, "B"); assertEquals("A", m.get(0, 0)); assertEquals("B", m.get(0, 1)); } /** * Tests the rows method. * * @throws Exception */ public void testRows() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); assertEquals(1, m.rows()); } /** * Tests the set method. * * @throws Exception */ public void testSet1() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); try { m.set(1, 0, null); fail(); } catch (final IndexOutOfBoundsException e) { // This is expected. } } /** * Tests the set method. * * @throws Exception */ public void testSet2() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); try { m.set(0, 2, null); fail(); } catch (final IndexOutOfBoundsException e) { // This is expected. } } /** * Tests the set method. * * @throws Exception */ public void testSet3() throws Exception { final Matrix<String> m = new Matrix<String>(1, 2, null); m.set(0, 0, "A"); m.set(0, 1, "B"); assertEquals("A", m.get(0, 0)); assertEquals("B", m.get(0, 1)); } /** * Tests the toString method. * * @throws Exception */ public void testToString() throws Exception { final Matrix<String> m = new Matrix<String>(2, 2, null); m.set(0, 0, "A"); m.set(0, 1, "B"); m.set(1, 0, "C"); m.set(1, 1, "D"); assertEquals("{ { A B }\n { C D } }", m.toString()); } }