1/*
2 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * utility class
26 */
27
28public class TestUtilities {
29    public static boolean equalsBlock(byte[] b1, byte[] b2, int len) {
30        for (int i = 0; i < len; i++) {
31            if (b1[i] != b2[i]) {
32                System.err.println("b1[" + i + "] : " + b1[i]
33                        + " b2[" + i + "] : " + b2[i]);
34                return false;
35            }
36        }
37        return true;
38    }
39
40    public static boolean equals(byte[] b1, byte[] b2) {
41        if (b2.length != b1.length) {
42            System.err.println("b1.length = " + b1.length
43                    + " b2.length = " + b2.length );
44            return false;
45        }
46        return equalsBlock(b1, b2, b1.length);
47    }
48
49    /**
50     * Verify b1's partial part is same as b2. compares b1 and b2 by chopping up
51     * b1 into blocks of b1BKSize and b2 into blocks of b2BKSize, and then
52     * compare the first b2BKSize bytes of each block, return true if they equal
53     * , otherwise return false.
54     * @param b1 byte array to be compared.
55     * @param b2 saved byte array.
56     * @param b1BKSize b1's block size.
57     * @param b2BKSize b2's block size.
58     * @return true is same. false otherwise.
59     */
60    public static boolean equalsBlockPartial(byte[] b1, byte[] b2, int b1BKSize,
61            int b2BKSize) {
62        int numOfBlock = b1.length / b1BKSize;
63        for (int b = 0; b < numOfBlock; b++) {
64            for (int i = 0; i < b2BKSize; i++) {
65                int j1 = b * b1BKSize + i;
66                int j2 = b * b2BKSize + i;
67                if (b1[j1] != b2[j2]) {
68                    System.err.println("Compare failed at b1[" + j1 + "]:" +
69                            b1[j1] + " b2[" + j2 + "]:" + b2[j2]);
70                    return false;
71                }
72            }
73        }
74        return true;
75    }
76
77    /**
78     * Generate a byte block by given length. The content of byte block
79     * is determined by the index.
80     * @param length length of byte array
81     * @return a byte array
82     */
83    public static byte[] generateBytes(int length) {
84        byte[] bytes = new byte[length];
85        for (int i = 0; i < length; i++) {
86            bytes[i] = (byte) (i & 0xff);
87        }
88        return bytes;
89    }
90}
91