Commit 7be0b274 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Add a 2d grid to the java side of the code.

Add a 2d grid to the java code.  It uses List<> to make the 2d grid itself,
but hides all implementation details, to allow for future modifications.
parent 6bfe26e7
Loading
Loading
Loading
Loading
+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 Matthew Dawson <matthew@mjdsystems.ca>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.mcmaster.cas.tabularexpressiontoolbox.tablularexpression;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 */
public final class TwoDimensionalGrid {
    // Creates a 1x1 grid.
    public TwoDimensionalGrid() {
        this(1, 1);
    }

    public TwoDimensionalGrid(int sizeX, int sizeY) {
        resize(sizeX, sizeY);

    }

    // Note that this class assumes the grid is never empty.
    // Whenever logically possible, cell contents are preserved at the correct location.
    public void resize(int sizeX, int sizeY) {
        if (sizeX <= 0 || sizeY <= 0) {
            throw new IllegalArgumentException("This grid must have positive sizes in both dimensions!  Given (" +
                    sizeX + ", " + sizeY + ").");
        }
        if (m_grid.size() > sizeX) {
            m_grid.subList(sizeX, m_grid.size()).clear();
        } else {
            while(m_grid.size() != sizeX) {
                m_grid.add(new ArrayList<Cell>());
            }
        }
        for(int i = 0; i < sizeX; ++i) {
            List<Cell> currentList = m_grid.get(i);
            if(currentList.size() > sizeY) {
                currentList.subList(sizeY, currentList.size()).clear();
            } else {
                while(currentList.size() != sizeY) {
                    currentList.add(new Cell());
                }
            }
        }
    }

    public int sizeX() {
        return m_grid.size();
    }

    public int sizeY() {
        return m_grid.get(0).size();
    }

    public Cell get(int x, int y) {
        return m_grid.get(x).get(y);
    }

    // Indexed as m_grid[x][y], for reasons.  All access should be hidden behind api, so that can be changed if
    // performance suffers.
    private List<List<Cell>> m_grid = new ArrayList<List<Cell>>();
}
+127 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 Matthew Dawson <matthew@mjdsystems.ca>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.mcmaster.cas.tabularexpressiontoolbox.tablularexpression.test;

import ca.mcmaster.cas.tabularexpressiontoolbox.tablularexpression.TwoDimensionalGrid;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

/**
 *
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 */
@RunWith(Parameterized.class)
public class TwoDimensionalGridResizeTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {
                        1, 1,
                        4, 5
                },
                {
                        3, 3,
                        5, 3
                },
                {
                        3, 3,
                        3, 5
                },
                {
                        3, 3,
                        5, 3
                },
                {
                        4, 4,
                        2, 2
                },
                {
                        4, 4,
                        2, 4
                },
                {
                        4, 4,
                        4, 2
                },
                {
                        4, 4,
                        2, 6
                },
                {
                        4, 4,
                        6, 2
                }
        });
    }

    @Parameter(value = 0)
    public int initialX;

    @Parameter(value = 1)
    public int initialY;

    @Parameter(value = 2)
    public int newX;

    @Parameter(value = 3)
    public int newY;

    @Test
    public void doTest()
    {
        TwoDimensionalGrid grid = new TwoDimensionalGrid(initialX, initialY);
        //Verify proper creation (should never fail ...)
        assertThat(grid.sizeX(), is(initialX));
        assertThat(grid.sizeY(), is(initialY));
        // Setup cell contents for the next test.
        int i = 0;
        for (int x = 0; x < initialX; ++x) {
            for (int y = 0; y < initialY; ++y) {
                grid.get(x, y).setContents("" + (i++));
            }
        }

        //And do the resize
        grid.resize(newX, newY);
        // Verify new sizes
        assertThat(grid.sizeX(), is(newX));
        assertThat(grid.sizeY(), is(newY));
        // And verify a cell exists at the maximum size.
        assertThat(grid.get(newX - 1, newY - 1), is(notNullValue()));
        // And that the original cells are still there.
        i = -1;
        for (int x = 0; x < initialX; ++x) {
            for (int y = 0; y < initialY; ++y) {
                i++;
                if (x >= newX || y >= newY) {
                    continue;
                }
                assertThat(grid.get(x, y).contents(), is("" + i));
            }
        }
    }
}
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 Matthew Dawson <matthew@mjdsystems.ca>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.mcmaster.cas.tabularexpressiontoolbox.tablularexpression.test;

import ca.mcmaster.cas.tabularexpressiontoolbox.tablularexpression.Cell;
import ca.mcmaster.cas.tabularexpressiontoolbox.tablularexpression.TwoDimensionalGrid;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

/**
 *
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 */
@RunWith(JUnit4.class)
public class TwoDimensionalGridTest {
    @Test
    public void testEmptyConstructor() {
        TwoDimensionalGrid grid = new TwoDimensionalGrid();

        // By default, we should have a 1x1 grid.  Test that.
        assertThat(grid.sizeX(), is(1));
        assertThat(grid.sizeY(), is(1));
        Cell cell = grid.get(0,0);
        assertThat(cell, is(notNullValue()));
        assertThat(grid.get(0,0), is(cell));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNonPositiveXSize() {
        new TwoDimensionalGrid(-1, 2);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNonPositiveYSize() {
        new TwoDimensionalGrid(2, -1);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNonPositiveBothSides() {
        new TwoDimensionalGrid(-1, -2);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testZeroSize() {
        new TwoDimensionalGrid(0, 2);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testZeroYSize() {
        new TwoDimensionalGrid(2, 0);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testZeroBothSides() {
        new TwoDimensionalGrid(0, 0);
    }
}