/*
 * Copyright 2014 Andreas Schildbach
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.bitcoinj.script;

import static org.bitcoinj.script.ScriptOpCodes.OP_0;
import static org.bitcoinj.script.ScriptOpCodes.OP_IF;
import static org.bitcoinj.script.ScriptOpCodes.OP_PUSHDATA1;
import static org.bitcoinj.script.ScriptOpCodes.OP_PUSHDATA2;
import static org.bitcoinj.script.ScriptOpCodes.OP_PUSHDATA4;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Random;

import org.junit.Test;
import org.spongycastle.util.Arrays;

public class ScriptChunkTest {

    private static final Random RANDOM = new Random(42);

    @Test
    public void testShortestPossibleDataPush() {
        assertTrue("empty push", new ScriptBuilder().data(new byte[0]).build().getChunks().get(0)
                .isShortestPossiblePushData());

        for (byte i = -1; i < 127; i++)
            assertTrue("push of single byte " + i, new ScriptBuilder().data(new byte[] { i }).build().getChunks()
                    .get(0).isShortestPossiblePushData());

        for (int len = 2; len < Script.MAX_SCRIPT_ELEMENT_SIZE; len++)
            assertTrue("push of " + len + " bytes", new ScriptBuilder().data(new byte[len]).build().getChunks().get(0)
                    .isShortestPossiblePushData());

        // non-standard chunks
        for (byte i = 1; i <= 16; i++)
            assertFalse("push of smallnum " + i, new ScriptChunk(1, new byte[] { i }).isShortestPossiblePushData());
        assertFalse("push of 75 bytes", new ScriptChunk(OP_PUSHDATA1, new byte[75]).isShortestPossiblePushData());
        assertFalse("push of 255 bytes", new ScriptChunk(OP_PUSHDATA2, new byte[255]).isShortestPossiblePushData());
        assertFalse("push of 65535 bytes", new ScriptChunk(OP_PUSHDATA4, new byte[65535]).isShortestPossiblePushData());
    }

    @Test
    public void testToByteArray_opcode() {
        byte[] expected = new byte[] { OP_IF };
        byte[] actual = new ScriptChunk(OP_IF, null).toByteArray();
        assertArrayEquals(expected, actual);
    }

    @Test
    public void testToByteArray_smallNum() {
        byte[] expected = new byte[] { OP_0 };
        byte[] actual = new ScriptChunk(OP_0, null).toByteArray();
        assertArrayEquals(expected, actual);
    }

    @Test
    public void testToByteArray_lt_OP_PUSHDATA1() {
        // < OP_PUSHDATA1
        for (byte len = 1; len < OP_PUSHDATA1; len++) {
            byte[] bytes = new byte[len];
            RANDOM.nextBytes(bytes);
            byte[] expected = Arrays.concatenate(new byte[] { len }, bytes);
            byte[] actual = new ScriptChunk(len, bytes).toByteArray();
            assertArrayEquals(expected, actual);
        }
    }

    @Test
    public void testToByteArray_OP_PUSHDATA1() {
        // OP_PUSHDATA1
        byte[] bytes = new byte[0xFF];
        RANDOM.nextBytes(bytes);
        byte[] expected = Arrays.concatenate(new byte[] { OP_PUSHDATA1, (byte) 0xFF }, bytes);
        byte[] actual = new ScriptChunk(OP_PUSHDATA1, bytes).toByteArray();
        assertArrayEquals(expected, actual);
    }

    @Test
    public void testToByteArray_OP_PUSHDATA2() {
        // OP_PUSHDATA2
        byte[] bytes = new byte[0x0102];
        RANDOM.nextBytes(bytes);
        byte[] expected = Arrays.concatenate(new byte[] { OP_PUSHDATA2, 0x02, 0x01 }, bytes);
        byte[] actual = new ScriptChunk(OP_PUSHDATA2, bytes).toByteArray();
        assertArrayEquals(expected, actual);
    }

    @Test
    public void testToByteArray_OP_PUSHDATA4() {
        // OP_PUSHDATA4
        byte[] bytes = new byte[0x0102];
        RANDOM.nextBytes(bytes);
        byte[] expected = Arrays.concatenate(new byte[] { OP_PUSHDATA4, 0x02, 0x01, 0x00, 0x00 }, bytes);
        byte[] actual = new ScriptChunk(OP_PUSHDATA4, bytes).toByteArray();
        assertArrayEquals(expected, actual);
    }
}

@@ -1111,42 +1111,49 @@
 st;%0A
+%0A
 import 
-org.spongycastle.util.Array
+com.google.common.primitives.Byte
 s;%0A%0A
@@ -3143,37 +3143,36 @@
  expected = 
-Array
+Byte
 s.concat
 enate(new by
@@ -3151,37 +3151,32 @@
 d = Bytes.concat
-enate
 (new byte%5B%5D %7B le
@@ -3502,37 +3502,36 @@
  expected = 
-Array
+Byte
 s.concat
 enate(new by
@@ -3510,37 +3510,32 @@
 d = Bytes.concat
-enate
 (new byte%5B%5D %7B OP
@@ -3876,37 +3876,36 @@
  expected = 
-Array
+Byte
 s.concat
 enate(new by
@@ -3884,37 +3884,32 @@
 d = Bytes.concat
-enate
 (new byte%5B%5D %7B OP
@@ -4257,21 +4257,20 @@
 d = 
-Array
+Byte
 s.concat
 enat
@@ -4269,13 +4269,8 @@
 ncat
-enate
 (new

Use Guava for concatenating byte arrays.