rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareByteArrayTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 459 vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareByteArrayTest.java@a2871a3fd4c5
child 807 e93506e603ad
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@390
     1
/**
jaroslav@390
     2
 * Back 2 Browser Bytecode Translator
jaroslav@390
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@390
     4
 *
jaroslav@390
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@390
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@390
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@390
     8
 *
jaroslav@390
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@390
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@390
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@390
    12
 * GNU General Public License for more details.
jaroslav@390
    13
 *
jaroslav@390
    14
 * You should have received a copy of the GNU General Public License
jaroslav@390
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@390
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@390
    17
 */
jaroslav@390
    18
package org.apidesign.bck2brwsr.tck;
jaroslav@390
    19
jaroslav@390
    20
import org.apidesign.bck2brwsr.vmtest.Compare;
jaroslav@390
    21
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@390
    22
import org.testng.annotations.Factory;
jaroslav@390
    23
jaroslav@390
    24
/**
jaroslav@390
    25
 *
jaroslav@390
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@390
    27
 */
jaroslav@390
    28
public class CompareByteArrayTest {
jaroslav@390
    29
    @Compare public int byteArraySum() {
jaroslav@390
    30
        byte[] arr = createArray();
jaroslav@390
    31
        return sumByteArr(arr);
jaroslav@390
    32
    }
jaroslav@390
    33
    
jaroslav@390
    34
    @Compare public int countZeros() {
jaroslav@390
    35
        int zeros = 0;
jaroslav@390
    36
        for (Byte b : createArray()) {
jaroslav@390
    37
            if (b == 0) {
jaroslav@390
    38
                zeros++;
jaroslav@390
    39
            }
jaroslav@390
    40
        }
jaroslav@390
    41
        return zeros;
jaroslav@390
    42
    }
jaroslav@390
    43
    
jaroslav@390
    44
    private static int sumByteArr(byte[] arr) {
jaroslav@390
    45
        int sum = 0;
jaroslav@390
    46
        for (int i = 0; i < arr.length; i++) {
jaroslav@390
    47
            sum += arr[i];
jaroslav@390
    48
        }
jaroslav@390
    49
        return sum;
jaroslav@390
    50
    }
jaroslav@390
    51
    
jaroslav@458
    52
    @Compare public String noOutOfBounds() {
jaroslav@458
    53
        return atIndex(1);
jaroslav@458
    54
    }
jaroslav@458
    55
jaroslav@458
    56
    @Compare public String outOfBounds() {
jaroslav@458
    57
        return atIndex(5);
jaroslav@458
    58
    }
jaroslav@458
    59
jaroslav@458
    60
    @Compare public String outOfBoundsMinus() {
jaroslav@458
    61
        return atIndex(-1);
jaroslav@458
    62
    }
jaroslav@458
    63
jaroslav@459
    64
    @Compare public String toOfBounds() {
jaroslav@459
    65
        return toIndex(5);
jaroslav@459
    66
    }
jaroslav@459
    67
jaroslav@459
    68
    @Compare public String toOfBoundsMinus() {
jaroslav@459
    69
        return toIndex(-1);
jaroslav@459
    70
    }
jaroslav@459
    71
jaroslav@458
    72
    private static final int[] arr = { 0, 1, 2 };
jaroslav@458
    73
    public static String atIndex(int at) {
jaroslav@458
    74
        return "at@" + arr[at];
jaroslav@458
    75
    }
jaroslav@459
    76
    public static String toIndex(int at) {
jaroslav@459
    77
        arr[at] = 10;
jaroslav@459
    78
        return "ok";
jaroslav@459
    79
    }
jaroslav@458
    80
    
jaroslav@458
    81
    
jaroslav@390
    82
    @Factory
jaroslav@390
    83
    public static Object[] create() {
jaroslav@390
    84
        return VMTest.create(CompareByteArrayTest.class);
jaroslav@390
    85
    }
jaroslav@390
    86
jaroslav@390
    87
    private byte[] createArray() {
jaroslav@390
    88
        byte[] arr = new byte[10];
jaroslav@390
    89
        arr[5] = 3;
jaroslav@390
    90
        arr[7] = 8;
jaroslav@390
    91
        return arr;
jaroslav@390
    92
    }
jaroslav@390
    93
}