package com.punchthrough.bean.sdk.utility;

import android.test.AndroidTestCase;

import com.punchthrough.bean.sdk.internal.BeanMessageID;
import com.punchthrough.bean.sdk.internal.upload.sketch.BeanState;
import com.punchthrough.bean.sdk.internal.exception.NoEnumFoundException;

import java.nio.ByteOrder;

import static com.punchthrough.bean.sdk.internal.utility.Misc.enumWithRawValue;
import static com.punchthrough.bean.sdk.internal.utility.Misc.intArrayToByteArray;
import static com.punchthrough.bean.sdk.internal.utility.Misc.intToByte;
import static com.punchthrough.bean.sdk.internal.utility.Misc.intToUInt32;
import static com.punchthrough.bean.sdk.internal.utility.Misc.twoBytesToInt;
import static org.assertj.core.api.Assertions.assertThat;

public class MiscTest extends AndroidTestCase {

    public void testIntToByte() {
        assertThat(intToByte(0xFF)).isEqualTo((byte) 0xFF);
    }

    public void testEnumWithRawInt() throws NoEnumFoundException {
        assertThat(enumWithRawValue(BeanMessageID.class, 0x1001)).isEqualTo(BeanMessageID.BL_FW_BLOCK);
    }

    public void testEnumWithRawByte() throws NoEnumFoundException {
        byte state = 5;
        assertThat(enumWithRawValue(BeanState.class, state)).isEqualTo(BeanState.COMPLETE);
    }

    public void testEnumWithUnparsableValue() {
        try {
            enumWithRawValue(BeanMessageID.class, 0x9999);
            fail("Expected a NoEnumFound exception to be thrown when parsing an enum from an " +
                    "invalid value");
        } catch (NoEnumFoundException e) {
            assertThat(e).isNotNull();
        }
    }

    public void testUnsigningInts() {

        assertThat(intToUInt32(0, ByteOrder.BIG_ENDIAN)).isEqualTo(intArrayToByteArray(
                new int[]{0x00, 0x00, 0x00, 0x00}));

        assertThat(intToUInt32(6400, ByteOrder.BIG_ENDIAN)).isEqualTo(intArrayToByteArray(
                new int[]{0x00, 0x00, 0x19, 0x00}));

        assertThat(intToUInt32(65535, ByteOrder.BIG_ENDIAN)).isEqualTo(intArrayToByteArray(
                new int[]{0x00, 0x00, 0xFF, 0xFF}));

        assertThat(intToUInt32(2147483647, ByteOrder.BIG_ENDIAN)).isEqualTo(intArrayToByteArray(
                new int[]{0x7F, 0xFF, 0xFF, 0xFF}));

        assertThat(intToUInt32(6400, ByteOrder.LITTLE_ENDIAN)).isEqualTo(intArrayToByteArray(
                new int[]{0x00, 0x19, 0x00, 0x00}));

        assertThat(intToUInt32(65535, ByteOrder.LITTLE_ENDIAN)).isEqualTo(intArrayToByteArray(
                new int[]{0xFF, 0xFF, 0x00, 0x00}));

    }

    public void testParsingTwoBytesWithOrder() {

        assertThat(twoBytesToInt(
                intArrayToByteArray(new int[] {0x43, 0x0C}),
                ByteOrder.BIG_ENDIAN))
                .isEqualTo(17164);

        assertThat(twoBytesToInt(
                intArrayToByteArray(new int[] {0x0C, 0x43}),
                ByteOrder.LITTLE_ENDIAN))
                .isEqualTo(17164);

        assertThat(twoBytesToInt(
                intArrayToByteArray(new int[] {0x13, 0xB2}),
                ByteOrder.BIG_ENDIAN))
                .isEqualTo(5042);

        assertThat(twoBytesToInt(
                intArrayToByteArray(new int[] {0xB2, 0x13}),
                ByteOrder.LITTLE_ENDIAN))
                .isEqualTo(5042);

    }

}

@@ -534,24 +534,101 @@
 .intToByte;%0A
+import static com.punchthrough.bean.sdk.internal.utility.Misc.intToTwoBytes;%0A
 import stati
@@ -3368,10 +3368,608 @@
 %0A    %7D%0A%0A
+    public void testIntToTwoBytes() %7B%0A%0A        assertThat(intToTwoBytes(17164, ByteOrder.BIG_ENDIAN))%0A                .isEqualTo(intArrayToByteArray(new int%5B%5D %7B0x43, 0x0C%7D));%0A%0A        assertThat(intToTwoBytes(17164, ByteOrder.LITTLE_ENDIAN))%0A                .isEqualTo(intArrayToByteArray(new int%5B%5D %7B0x0C, 0x43%7D));%0A%0A        assertThat(intToTwoBytes(5042, ByteOrder.BIG_ENDIAN))%0A                .isEqualTo(intArrayToByteArray(new int%5B%5D %7B0x13, 0xB2%7D));%0A%0A        assertThat(intToTwoBytes(5042, ByteOrder.LITTLE_ENDIAN))%0A                .isEqualTo(intArrayToByteArray(new int%5B%5D %7B0xB2, 0x13%7D));%0A%0A    %7D%0A%0A
 %7D%0A

Add tests for intToTwoBytes