/* * Copyright (C) 2022 Objectos Software LTDA. * * 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 library; import br.com.objectos.testing.random.TestingRandom; import java.util.Arrays; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ByteArraysTest { @Test(description = "ByteArrays.mismatch: for array lengths <= 7") public void mismatch() { test(-1, arr(), arr()); test(-1, arr(1), arr(1)); test(-1, arr(1, 2), arr(1, 2)); test(-1, arr(1, 2, 3), arr(1, 2, 3)); test(0, arr(), arr(1)); test(0, arr(), arr(1, 2)); test(0, arr(1), arr()); test(0, arr(1, 2), arr()); test(0, arr(3), arr(5)); test(0, arr(3), arr(5, 6)); test(0, arr(3, 4), arr(5, 6)); test(0, arr(3, 4, 5), arr(5, 6)); test(1, arr(1, 2), arr(1)); test(1, arr(1, 2, 3), arr(1)); test(1, arr(9), arr(9, 8, 7)); test(2, arr(1, 2, 3), arr(1, 2)); test(2, arr(1, 2, 3, 4), arr(1, 2)); test(2, arr(1, 2, 3, 4), arr(1, 2, 9)); test(2, arr(9, 8), arr(9, 8, 7)); } @DataProvider public Object[][] mismatchParam() { Object[][] result; result = new Object[63][]; for (int i = 0; i < result.length; i++) { result[i] = new Object[] {i + 1}; } return result; } @Test( description = "ByteArrays.mismatch: array length 0 < len <= 64 ", dataProvider = "mismatchParam") public void mismatchParam(int len) { byte[] a; a = TestingRandom.nextBytes(len); byte[] b; b = Arrays.copyOf(a, len); test(-1, a, a); test(-1, a, b); test(-1, b, a); b = Arrays.copyOf(a, len + 1); test(len, a, b); test(len, b, a); b[0] = (byte) (a[0] + 1); test(0, a, b); test(0, b, a); b[0] = a[0]; b[len - 1] = (byte) (a[len - 1] + 1); test(len - 1, a, b); test(len - 1, b, a); } private byte[] arr(int... values) { byte[] arr; arr = new byte[values.length]; for (int i = 0; i < arr.length; i++) { arr[i] = (byte) values[i]; } return arr; } private void test(int expected, byte[] a, byte[] b) { int result; result = ByteArrays.mismatch(a, b); Assert.assertEquals(result, expected); } }