f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/test/java/io/protobase/f7/models/GridTest.java
-rw-r--r--
8225
  1package io.protobase.f7.models;
  2
  3import com.google.common.collect.ImmutableList;
  4import com.google.common.collect.Lists;
  5import org.junit.Before;
  6import org.junit.Test;
  7
  8import java.util.ArrayList;
  9import java.util.List;
 10
 11import static com.google.common.truth.Truth.assertThat;
 12
 13public class GridTest {
 14  private Grid<String> grid;
 15
 16  @Before
 17  public void setup() {
 18    grid = new Grid<>(4, 6);
 19  }
 20
 21  @Test
 22  public void constructor() {
 23    assertThat(grid.getColumnSize()).isEqualTo(4);
 24    assertThat(grid.getRowSize()).isEqualTo(6);
 25  }
 26
 27  @Test
 28  public void constructor_negativeColumns() {
 29    grid = null;
 30    try {
 31      grid = new Grid<>(-1, 4);
 32      assertThat(grid).isNull();
 33    } catch (IndexOutOfBoundsException exception) {
 34      assertThat(exception).isNotNull();
 35    }
 36  }
 37
 38  @Test
 39  public void constructor_negativeRows() {
 40    grid = null;
 41    try {
 42      grid = new Grid<>(4, -1);
 43      assertThat(grid).isNull();
 44    } catch (IndexOutOfBoundsException exception) {
 45      assertThat(exception).isNotNull();
 46    }
 47  }
 48
 49  @Test
 50  public void set() {
 51    grid.set(0, 0, "0-0");
 52    assertThat(grid.get(0, 0)).isEqualTo("0-0");
 53    grid.set(0, 1, "0-1");
 54    assertThat(grid.get(0, 1)).isEqualTo("0-1");
 55    grid.set(1, 1, "1-1");
 56    assertThat(grid.get(1, 1)).isEqualTo("1-1");
 57    grid.set(1, 0, "1-0");
 58    assertThat(grid.get(1, 0)).isEqualTo("1-0");
 59  }
 60
 61  @Test
 62  public void set_bumpSize() {
 63    assertThat(grid.getColumnSize()).isEqualTo(4);
 64    assertThat(grid.getRowSize()).isEqualTo(6);
 65    grid.set(10, 21, "10-21");
 66    assertThat(grid.get(10, 21)).isEqualTo("10-21");
 67    assertThat(grid.getColumnSize()).isEqualTo(11);
 68    assertThat(grid.getRowSize()).isEqualTo(22);
 69  }
 70
 71  @Test
 72  public void get() {
 73    grid.set(0, 0, "0-0");
 74    assertThat(grid.get(0, 0)).isEqualTo("0-0");
 75    assertThat(grid.get(1, 3)).isNull();
 76  }
 77
 78  @Test
 79  public void iterator_default() {
 80    buildFullGrid();
 81    List<String> found = new ArrayList<>();
 82    for (String string : grid) {
 83      found.add(string);
 84    }
 85    /*
 86     *   | 0    1    2    3
 87     * --+-----------------
 88     * 0 | 0    1    2    3
 89     * 1 | 4    5    6    7
 90     * 2 | 8    9    10   11
 91     * 3 | 12   13   14   15
 92     * 4 | 16   17   18   19
 93     * 5 | 20   21   22   23
 94     */
 95    assertThat(found.size()).isEqualTo((grid.getColumnSize() * grid.getRowSize()));
 96    assertThat(found).isEqualTo(ImmutableList.of(
 97        "0-0",
 98        "1-0",
 99        "2-0",
100        "3-0",
101        "0-1",
102        "1-1",
103        "2-1",
104        "3-1",
105        "0-2",
106        "1-2",
107        "2-2",
108        "3-2",
109        "0-3",
110        "1-3",
111        "2-3",
112        "3-3",
113        "0-4",
114        "1-4",
115        "2-4",
116        "3-4",
117        "0-5",
118        "1-5",
119        "2-5",
120        "3-5"
121    ));
122  }
123
124  @Test
125  public void iterator_indexIterator() {
126    buildFullGrid();
127    grid.remove(0, 0);
128    grid.remove(1, 2);
129    grid.remove(3, 4);
130    grid.remove(2, 5);
131    List<ColumnRowKey> found = new ArrayList<>();
132    grid.indexIterator().forEachRemaining(found::add);
133    assertThat(found.size()).isEqualTo((grid.getColumnSize() * grid.getRowSize()));
134    assertThat(found).isEqualTo(ImmutableList.of(
135        new ColumnRowKey(0, 0),
136        new ColumnRowKey(1, 0),
137        new ColumnRowKey(2, 0),
138        new ColumnRowKey(3, 0),
139        new ColumnRowKey(0, 1),
140        new ColumnRowKey(1, 1),
141        new ColumnRowKey(2, 1),
142        new ColumnRowKey(3, 1),
143        new ColumnRowKey(0, 2),
144        new ColumnRowKey(1, 2),
145        new ColumnRowKey(2, 2),
146        new ColumnRowKey(3, 2),
147        new ColumnRowKey(0, 3),
148        new ColumnRowKey(1, 3),
149        new ColumnRowKey(2, 3),
150        new ColumnRowKey(3, 3),
151        new ColumnRowKey(0, 4),
152        new ColumnRowKey(1, 4),
153        new ColumnRowKey(2, 4),
154        new ColumnRowKey(3, 4),
155        new ColumnRowKey(0, 5),
156        new ColumnRowKey(1, 5),
157        new ColumnRowKey(2, 5),
158        new ColumnRowKey(3, 5)
159    ));
160  }
161
162  @Test
163  public void iterator_reverseIndexIterator() {
164    buildFullGrid();
165    grid.remove(0, 0);
166    grid.remove(1, 2);
167    grid.remove(3, 4);
168    grid.remove(2, 5);
169    List<ColumnRowKey> found = new ArrayList<>();
170    grid.reverseIndexIterator().forEachRemaining(found::add);
171    assertThat(found.size()).isEqualTo((grid.getColumnSize() * grid.getRowSize()));
172    assertThat(found).isEqualTo(ImmutableList.of(
173        new ColumnRowKey(3, 5),
174        new ColumnRowKey(2, 5),
175        new ColumnRowKey(1, 5),
176        new ColumnRowKey(0, 5),
177        new ColumnRowKey(3, 4),
178        new ColumnRowKey(2, 4),
179        new ColumnRowKey(1, 4),
180        new ColumnRowKey(0, 4),
181        new ColumnRowKey(3, 3),
182        new ColumnRowKey(2, 3),
183        new ColumnRowKey(1, 3),
184        new ColumnRowKey(0, 3),
185        new ColumnRowKey(3, 2),
186        new ColumnRowKey(2, 2),
187        new ColumnRowKey(1, 2),
188        new ColumnRowKey(0, 2),
189        new ColumnRowKey(3, 1),
190        new ColumnRowKey(2, 1),
191        new ColumnRowKey(1, 1),
192        new ColumnRowKey(0, 1),
193        new ColumnRowKey(3, 0),
194        new ColumnRowKey(2, 0),
195        new ColumnRowKey(1, 0),
196        new ColumnRowKey(0, 0)
197    ));
198  }
199
200  @Test
201  public void iterator_indexIteratorWithStartAndEndIndex() {
202    buildFullGrid();
203    /*
204     *   | 0    1    2    3
205     * --+----------------
206     * 0 | N    N    N    N
207     * 1 | N    N    N    N
208     * 2 | N    N    N    N
209     * 3 | N    5    4    N
210     * 4 | N    3    2    N
211     * 5 | N    1    0    N
212     */
213    assertThat(Lists.newArrayList(grid.reverseIndexIterator(2, 1, 5, 3))).isEqualTo(ImmutableList.of(
214        new ColumnRowKey(2, 5),
215        new ColumnRowKey(1, 5),
216        new ColumnRowKey(2, 4),
217        new ColumnRowKey(1, 4),
218        new ColumnRowKey(2, 3),
219        new ColumnRowKey(1, 3)
220    ));
221    /*
222     *   | 0    1    2    3
223     * --+----------------
224     * 0 | N    N    3    2
225     * 1 | N    N    1    0
226     * 2 | N    N    N    N
227     * 3 | N    N    N    N
228     * 4 | N    N    N    N
229     * 5 | N    N    N    N
230     */
231    assertThat(Lists.newArrayList(grid.reverseIndexIterator(3, 2, 1, 0))).isEqualTo(ImmutableList.of(
232        new ColumnRowKey(3, 1),
233        new ColumnRowKey(2, 1),
234        new ColumnRowKey(3, 0),
235        new ColumnRowKey(2, 0)
236    ));
237  }
238
239  @Test
240  public void addGridToBottom() {
241    Grid<String> first = new Grid<>(2, 2);
242    first.set(0, 0, "A");
243    first.set(0, 1, "B");
244    first.set(1, 1, "C");
245    first.set(1, 0, "D");
246    Grid<String> second = new Grid<>(2, 2);
247    second.set(0, 0, "E");
248    second.set(0, 1, "F");
249    second.set(1, 1, "G");
250    second.set(1, 0, "H");
251    first.addGridToBottom(second);
252    assertThat(first.getColumnSize()).isEqualTo(2);
253    assertThat(first.getRowSize()).isEqualTo(4);
254    assertThat(first.get(0, 0)).isEqualTo("A");
255    assertThat(first.get(0, 1)).isEqualTo("B");
256    assertThat(first.get(1, 1)).isEqualTo("C");
257    assertThat(first.get(1, 0)).isEqualTo("D");
258    assertThat(first.get(1, 2)).isEqualTo("H");
259    assertThat(first.get(1, 3)).isEqualTo("G");
260    assertThat(first.get(0, 2)).isEqualTo("E");
261    assertThat(first.get(0, 3)).isEqualTo("F");
262  }
263
264  @Test
265  public void addGridToRight() {
266    Grid<String> first = new Grid<>(2, 2);
267    first.set(0, 0, "A");
268    first.set(0, 1, "B");
269    first.set(1, 1, "C");
270    first.set(1, 0, "D");
271    Grid<String> second = new Grid<>(2, 2);
272    second.set(0, 0, "E");
273    second.set(0, 1, "F");
274    second.set(1, 1, "G");
275    second.set(1, 0, "H");
276    first.addGridToRight(second);
277    assertThat(first.getColumnSize()).isEqualTo(4);
278    assertThat(first.getRowSize()).isEqualTo(2);
279    assertThat(first.get(0, 0)).isEqualTo("A");
280    assertThat(first.get(0, 1)).isEqualTo("B");
281    assertThat(first.get(1, 1)).isEqualTo("C");
282    assertThat(first.get(1, 0)).isEqualTo("D");
283    assertThat(first.get(2, 0)).isEqualTo("E");
284    assertThat(first.get(3, 0)).isEqualTo("H");
285    assertThat(first.get(2, 1)).isEqualTo("F");
286    assertThat(first.get(3, 1)).isEqualTo("G");
287  }
288
289  private void buildFullGrid() {
290    for (int column = 0; column < grid.getColumnSize(); column++) {
291      for (int row = 0; row < grid.getRowSize(); row++) {
292        grid.set(column, row, column + "-" + row);
293      }
294    }
295  }
296}