diff --git a/src/main/java/org/fundacionjala/coding/abel/DataReverse.java b/src/main/java/org/fundacionjala/coding/abel/DataReverse.java new file mode 100644 index 0000000..64fdda4 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/abel/DataReverse.java @@ -0,0 +1,45 @@ +package org.fundacionjala.coding.abel; + +import java.util.stream.IntStream; + +/** + * CA stream of data is received and needs to be reversed. Each segment is 8 bits + * meaning the order of these segments need to be reversed: + * 11111111 00000000 00001111 10101010 + * (byte1) (byte2) (byte3) (byte4) + * 10101010 00001111 00000000 11111111 + * (byte4) (byte3) (byte2) (byte1) + * Total number of bits will always be a multiple of 8. The data is given in an array as such: + * [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0] + */ +public final class DataReverse { + + /** + * Private Constructor. + */ + private DataReverse() { + + } + + /** + * Reverse the bytes on the input. + * + * @param data int[]. + * @return int[]. + */ + public static int[] dataReverse(int[] data) { + int numberOfBytes = (data.length / 8) - 1; + int[] result = new int[data.length]; + IntStream.rangeClosed(0, numberOfBytes) + .forEach(i -> { + int dataPosition = 8 * i; + int resultPosition = (8 * numberOfBytes) - dataPosition; + IntStream.range(0, 8) + .forEach(n -> { + result[n + resultPosition] = data[n + dataPosition]; + }); + }); + + return result; + } +} diff --git a/src/test/java/org/fundacionjala/coding/abel/DataReverseTest.java b/src/test/java/org/fundacionjala/coding/abel/DataReverseTest.java new file mode 100644 index 0000000..b4cbde2 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/abel/DataReverseTest.java @@ -0,0 +1,55 @@ +package org.fundacionjala.coding.abel; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; + +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +/** + * Test for DataReverse Kata. + */ +public class DataReverseTest { + + /** + * Test for Private Constructors. + * + * @throws NoSuchMethodException Thrown when a particular method cannot be found. + * @throws IllegalAccessException Thrown when an application tries to reflectively create an instance. + * @throws InvocationTargetException thrown by an invoked method or constructor. + * @throws InstantiationException Thrown when an application tries to create an instance of a class + * using the {@code newInstance} method in class. + */ + @Test + public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { + Constructor constructor = DataReverse.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + + /** + * First tests. + */ + @Test + public void testOne() { + int[] data1 = {1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0}; + int[] data2 = {1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}; + assertArrayEquals(data2, DataReverse.dataReverse(data1)); + } + + /** + * Second tests. + */ + @Test + public void testTwo() { + int[] data1 = {0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1}; + int[] data2 = {0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0}; + assertArrayEquals(data2, DataReverse.dataReverse(data1)); + + } +}