Commit d158fbc4 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Add support for counting the number of cases in a hierarchical grid.

When dealing with a hierarchical grid, it is useful to know how many cases
there are.  Add a simple function to do so.  Also add some quick unit tests
to ensure it functions.
parent 01ee1732
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ package ca.mcscert.jtet.tablularexpression;

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

/**
 *
@@ -17,5 +18,29 @@ public class HierarchicalGrid implements SubHierarchyFetcher {
        return m_subHiearchy;
    }

    /** This counts the number of different cases this grid represents
     *
     * countTotalCases counts the total number of outputs this grid requires
     * to satisfy its total number of cases.
     *
     * @return The number of distinct cases.
     */
    public int countTotalCases() {
        int count = 0;

        Stack<HierarchicalCell> toCheck = new Stack<HierarchicalCell>();
        toCheck.addAll(m_subHiearchy);
        while (!toCheck.empty()) {
            HierarchicalCell next = toCheck.pop();
            List<HierarchicalCell> subGrid = next.getSubHiearchy();
            if (subGrid.isEmpty()) {
                count++;
            } else {
                toCheck.addAll(subGrid);
            }
        }
        return count;
    }
    
    private List<HierarchicalCell> m_subHiearchy = new ArrayList<HierarchicalCell>();
}
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 Matthew Dawson <matthew@mjdsystems.ca>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the distribution
 *     * Neither the name of the McMaster Centre for Software Certification nor the names
 *       of its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ca.mcscert.jtet.tablularexpression.test;

import ca.mcscert.jtet.tablularexpression.HierarchicalCell;
import ca.mcscert.jtet.tablularexpression.HierarchicalGrid;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.List;

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

@RunWith(JUnit4.class)
public class HierarchicalGridTest {

    @Test
    public void testCountTotalCases() {
        HierarchicalGrid grid = new HierarchicalGrid();

        // An empty grid has zero test cases
        assertThat(grid.countTotalCases(), is(0));

        List<HierarchicalCell> cells = grid.getSubHiearchy();

        cells.add(new HierarchicalCell());
        cells.add(new HierarchicalCell());
        assertThat(grid.countTotalCases(), is(2));

        cells = cells.get(0).getSubHiearchy();
        cells.add(new HierarchicalCell());
        assertThat(grid.countTotalCases(), is(2));

        cells.add(new HierarchicalCell());
        cells.add(new HierarchicalCell());
        assertThat(grid.countTotalCases(), is(4));

        // Insure further recursion then one layer is good.  Assume induction proves the rest of the possible cases.
        cells = cells.get(0).getSubHiearchy();
        cells.add(new HierarchicalCell());
        assertThat(grid.countTotalCases(), is(4));

        cells.add(new HierarchicalCell());
        cells.add(new HierarchicalCell());
        cells.add(new HierarchicalCell());
        assertThat(grid.countTotalCases(), is(7));
    }
}