001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.encoder;
015
016import static org.junit.Assert.*;
017
018import java.util.Random;
019
020import org.junit.Test;
021
022public class ByteArrayUtilTest {
023
024    int BA_SIZE = 16;
025    byte[] byteArray = new byte[BA_SIZE];
026
027    Random random = new Random(18532235);
028
029    @Test
030    public void smoke() {
031        verifyLoop(byteArray, 0, 0);
032        verifyLoop(byteArray, 0, 10);
033        verifyLoop(byteArray, 0, Integer.MAX_VALUE);
034        verifyLoop(byteArray, 0, Integer.MIN_VALUE);
035    }
036
037    @Test
038    public void random() {
039        for (int i = 0; i < 100000; i++) {
040            int rOffset = random.nextInt(BA_SIZE - 4);
041            int rInt = random.nextInt();
042            verifyLoop(byteArray, rOffset, rInt);
043        }
044    }
045
046    void verifyLoop(byte[] ba, int offset, int expected) {
047        ByteArrayUtil.writeInt(byteArray, offset, expected);
048        int back = ByteArrayUtil.readInt(byteArray, offset);
049        assertEquals(expected, back);
050
051    }
052
053}