BasicPermutationParity extends PermutationParity { @Override public int parity(long[] current) { int count = 0; for (int i = 0, n = indices.length; i < n; i++) { for (int j = i + 1; j < n; j++) { int cmp = compare(current[indices[i]], current[indices[j]]); if (cmp == 0) return 0; else if (cmp > 0) count++; } } return Integer.lowestOneBit(count) == 1 ? -1 : +1; } BasicPermutationParity(int[] indices); @Override int parity(long[] current);  }
@Test public void testParity_Even() throws Exception { assertEquals(1, permutationParity.parity(new long[]{4, 3, 2, 1})); }
@Test public void testParity_Odd() throws Exception { assertEquals(-1, permutationParity.parity(new long[]{4, 2, 3, 1})); }
@Test public void testParity_Even_Negative() throws Exception { assertEquals(1, permutationParity.parity(new long[]{4, 3, -1, -2})); }
@Test public void testParity_Odd_Negative() throws Exception { assertEquals(-1, permutationParity.parity(new long[]{4, -1, 3, -2})); }
@Test public void testParity_Duplicate() throws Exception { assertEquals(0, permutationParity.parity(new long[]{4, 3, -1, -1})); }
@Test public void testParity_All() throws Exception { assertEquals(1, permutationParity.parity(new long[]{1, 2, 3, 4})); assertEquals(-1, permutationParity.parity(new long[]{2, 1, 3, 4})); assertEquals(-1, permutationParity.parity(new long[]{1, 3, 2, 4})); assertEquals(1, permutationParity.parity(new long[]{3, 1, 2, 4})); assertEquals(1, permutationParity.parity(new long[]{2, 3, 1, 4})); assertEquals(-1, permutationParity.parity(new long[]{3, 2, 1, 4})); assertEquals(-1, permutationParity.parity(new long[]{1, 2, 4, 3})); assertEquals(1, permutationParity.parity(new long[]{2, 1, 4, 3})); assertEquals(1, permutationParity.parity(new long[]{1, 4, 2, 3})); assertEquals(-1, permutationParity.parity(new long[]{4, 1, 2, 3})); assertEquals(-1, permutationParity.parity(new long[]{2, 4, 1, 3})); assertEquals(1, permutationParity.parity(new long[]{4, 2, 1, 3})); assertEquals(1, permutationParity.parity(new long[]{1, 3, 4, 2})); assertEquals(-1, permutationParity.parity(new long[]{3, 1, 4, 2})); assertEquals(-1, permutationParity.parity(new long[]{1, 4, 3, 2})); assertEquals(1, permutationParity.parity(new long[]{4, 1, 3, 2})); assertEquals(1, permutationParity.parity(new long[]{3, 4, 1, 2})); assertEquals(-1, permutationParity.parity(new long[]{4, 3, 1, 2})); assertEquals(-1, permutationParity.parity(new long[]{2, 3, 4, 1})); assertEquals(1, permutationParity.parity(new long[]{3, 2, 4, 1})); assertEquals(1, permutationParity.parity(new long[]{2, 4, 3, 1})); assertEquals(-1, permutationParity.parity(new long[]{4, 2, 3, 1})); assertEquals(-1, permutationParity.parity(new long[]{3, 4, 2, 1})); assertEquals(1, permutationParity.parity(new long[]{4, 3, 2, 1})); }