rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/CompareByteArrayTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Nov 2014 19:32:00 +0100
changeset 1723 3a1f262311cf
parent 1034 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareByteArrayTest.java@28dc692f3b11
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Separating compact profile tests into own project, so launcher.http can depend on compact profile JAR
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@807
    71
    
jaroslav@807
    72
    @Compare public int multiArrayLength() {
jaroslav@807
    73
         int[][] arr = new int[1][0];
jaroslav@807
    74
         return arr[0].length;
jaroslav@807
    75
    }
jaroslav@807
    76
jaroslav@807
    77
    @Compare public int multiObjectArrayLength() {
jaroslav@807
    78
         Object[][] arr = new Object[1][0];
jaroslav@807
    79
         return arr[0].length;
jaroslav@807
    80
    }
jaroslav@459
    81
jaroslav@458
    82
    private static final int[] arr = { 0, 1, 2 };
jaroslav@458
    83
    public static String atIndex(int at) {
jaroslav@458
    84
        return "at@" + arr[at];
jaroslav@458
    85
    }
jaroslav@459
    86
    public static String toIndex(int at) {
jaroslav@459
    87
        arr[at] = 10;
jaroslav@459
    88
        return "ok";
jaroslav@459
    89
    }
jaroslav@458
    90
    
jaroslav@458
    91
    
jaroslav@390
    92
    @Factory
jaroslav@390
    93
    public static Object[] create() {
jaroslav@390
    94
        return VMTest.create(CompareByteArrayTest.class);
jaroslav@390
    95
    }
jaroslav@390
    96
jaroslav@390
    97
    private byte[] createArray() {
jaroslav@390
    98
        byte[] arr = new byte[10];
jaroslav@390
    99
        arr[5] = 3;
jaroslav@390
   100
        arr[7] = 8;
jaroslav@390
   101
        return arr;
jaroslav@390
   102
    }
jaroslav@390
   103
}