View Javadoc
1   /*
2    * Copyright © 2016 Greg Chabala
3    *
4    * This file is part of brick-control-lab.
5    *
6    * brick-control-lab is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * brick-control-lab is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with brick-control-lab.  If not, see http://www.gnu.org/licenses/.
18   */
19  package org.chabala.brick.controllab;
20  
21  import org.junit.Test;
22  
23  import java.util.EnumSet;
24  
25  import static org.hamcrest.Matchers.arrayWithSize;
26  import static org.hamcrest.Matchers.is;
27  import static org.junit.Assert.*;
28  
29  /**
30   * Test the encoding and decoding methods of {@link OutputId}.
31   */
32  public class OutputIdTest {
33  
34      @Test
35      public void testAllOutputSetEncodesByteWithAllBitsSet() throws Exception {
36          assertThat(OutputId.encodeSetToByte(OutputId.ALL), is((byte) 0b11111111));
37      }
38  
39      @Test
40      public void testOutputAEncodesByteWithLowBitSet() throws Exception {
41          assertThat(OutputId.encodeSetToByte(EnumSet.of(OutputId.A)), is((byte) 0b00000001));
42      }
43  
44      @Test
45      public void testOutputHEncodesByteWithHighBitSet() throws Exception {
46          assertThat(OutputId.encodeSetToByte(EnumSet.of(OutputId.H)), is((byte) 0b10000000));
47      }
48  
49      @Test
50      public void testByteWithAllBitsSetDecodesToAllOutputs() throws Exception {
51          assertThat(OutputId.decodeByteToSet((byte) 0b11111111), is(OutputId.ALL));
52      }
53  
54      @Test
55      public void testByteWithLowBitSetDecodesToOutputA() throws Exception {
56          assertThat(OutputId.decodeByteToSet((byte) 0b00000001), is(EnumSet.of(OutputId.A)));
57      }
58  
59      @Test
60      public void testByteWithEvenBitsSetDecodesToOutputsBDFH() throws Exception {
61          assertThat(OutputId.decodeByteToSet((byte) 0b10101010), is(EnumSet.of(OutputId.B, OutputId.D, OutputId.F, OutputId.H)));
62      }
63  
64      @Test
65      public void testByteWithHighBitSetDecodesToOutputH() throws Exception {
66          assertThat(OutputId.decodeByteToSet((byte) 0b10000000), is(EnumSet.of(OutputId.H)));
67      }
68  
69      @Test
70      public void testThereAreEightOutputs() throws Exception {
71          assertThat(OutputId.values(), arrayWithSize(8));
72      }
73  }