|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2015 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package net.imagej.table; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertEquals; |
| 35 | +import static org.junit.Assert.assertTrue; |
| 36 | + |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Collection; |
| 39 | + |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +/** |
| 43 | + * Tests {@link DefaultGenericTable}. |
| 44 | + * |
| 45 | + * @author Alison Walter |
| 46 | + */ |
| 47 | +public class DefaultGenericTableTest { |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testTypes() { |
| 51 | + final GenericTable table = makeTable(); |
| 52 | + table.appendColumn(); |
| 53 | + assertEquals(table.get(0).getType(), Byte.class); |
| 54 | + assertEquals(table.get(1).getType(), Character.class); |
| 55 | + assertEquals(table.get(2).getType(), Float.class); |
| 56 | + assertEquals(table.get(3).getType(), String.class); |
| 57 | + assertEquals(table.get(4).getType(), Object.class); |
| 58 | + assertTrue(GenericColumn.class.isInstance(table.get(4))); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testValues() { |
| 63 | + final GenericTable table = makeTable(); |
| 64 | + |
| 65 | + assertTrue(Byte.class.isInstance(table.get(0, 0))); |
| 66 | + assertEquals(table.get(0, 3), (byte) 0); |
| 67 | + assertEquals(table.get(0).getHeader(), "ByteHeader"); |
| 68 | + |
| 69 | + assertTrue(Character.class.isInstance(table.get(1, 0))); |
| 70 | + assertEquals(table.get(1, 0), 'A'); |
| 71 | + assertEquals(table.get(1).getHeader(), "CharHeader"); |
| 72 | + |
| 73 | + assertTrue(Float.class.isInstance(table.get(2, 0))); |
| 74 | + assertEquals(table.get(2, 2), -100.25f); |
| 75 | + assertEquals(table.get(2).getHeader(), "FloatHeader"); |
| 76 | + |
| 77 | + assertTrue(String.class.isInstance(table.get(3, 0))); |
| 78 | + assertEquals(table.get(3, 1), "hello"); |
| 79 | + assertEquals(table.get(3).getHeader(), "StringHeader"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testAddOps() { |
| 84 | + final GenericTable table = makeTable(); |
| 85 | + final BoolColumn bcol = new BoolColumn(); |
| 86 | + final boolean[] bval = { true, true, true, false }; |
| 87 | + bcol.fill(bval); |
| 88 | + |
| 89 | + table.add(1, bcol); |
| 90 | + assertTrue(BoolColumn.class.isInstance(table.get(1))); |
| 91 | + |
| 92 | + final Collection<Column<? extends Object>> c = new ArrayList<>(); |
| 93 | + final ShortColumn scol = new ShortColumn(); |
| 94 | + final short[] sval = { 1, 11234, -13124, -18 }; |
| 95 | + scol.fill(sval); |
| 96 | + c.add(scol); |
| 97 | + c.add(bcol); |
| 98 | + |
| 99 | + table.addAll(c); |
| 100 | + assertTrue(ShortColumn.class.isInstance(table.get(5))); |
| 101 | + assertTrue(BoolColumn.class.isInstance(table.get(6))); |
| 102 | + assertEquals(table.getRowCount(), 4); |
| 103 | + assertEquals(table.getColumnCount(), 7); |
| 104 | + |
| 105 | + final IntColumn icol = new IntColumn(); |
| 106 | + final int[] ival = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 107 | + icol.fill(ival); |
| 108 | + c.add(icol); |
| 109 | + |
| 110 | + table.addAll(0, c); |
| 111 | + assertTrue(ShortColumn.class.isInstance(table.get(0))); |
| 112 | + assertTrue(BoolColumn.class.isInstance(table.get(1))); |
| 113 | + assertTrue(IntColumn.class.isInstance(table.get(2))); |
| 114 | + assertEquals(table.getRowCount(), 8); |
| 115 | + assertEquals(table.getColumnCount(), 10); |
| 116 | + assertEquals(table.get(3, 6), (byte) 0); |
| 117 | + } |
| 118 | + |
| 119 | + // TODO add more tests |
| 120 | + |
| 121 | + // -- Helper methods -- |
| 122 | + |
| 123 | + private GenericTable makeTable() { |
| 124 | + // create table and columns |
| 125 | + final GenericTable table = new DefaultGenericTable(); |
| 126 | + final ByteColumn col = new ByteColumn("ByteHeader"); |
| 127 | + final CharColumn col1 = new CharColumn("CharHeader"); |
| 128 | + final FloatColumn col2 = new FloatColumn("FloatHeader"); |
| 129 | + final DefaultColumn<String> col3 = |
| 130 | + new DefaultColumn<>(String.class, "StringHeader"); |
| 131 | + |
| 132 | + // fill columns with values |
| 133 | + final byte[] bval = { 3, 127, -128, 0 }; |
| 134 | + final char[] cval = { 'A', '&', '\t', '4' }; |
| 135 | + final float[] fval = { 12.125f, 100.5f, -100.25f, -0.03125f }; |
| 136 | + final String[] sval = { "hi", "hello", "good day", "greetings!" }; |
| 137 | + col.fill(bval); |
| 138 | + col1.fill(cval); |
| 139 | + col2.fill(fval); |
| 140 | + col3.setArray(sval.clone()); |
| 141 | + col3.setSize(sval.length); |
| 142 | + |
| 143 | + // add columns to table |
| 144 | + table.add(col); |
| 145 | + table.add(col1); |
| 146 | + table.add(col2); |
| 147 | + table.add(col3); |
| 148 | + return table; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments