-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Base UIDs on BigInteger (Issue #38) and implement Comparable (Issue #80). Encoding UIDs now returns variable length byte arrays (1, 2, 4, 8 or 16 bytes).
- Loading branch information
Showing
3 changed files
with
189 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.dd.plist.test; | ||
|
||
import com.dd.plist.UID; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class UidTests { | ||
|
||
@Test | ||
public void constructoRejectsTooLargeValues() | ||
{ | ||
byte[] data = new byte[17]; | ||
data[0] = 0x01; | ||
assertThrows(IllegalArgumentException.class, () -> new UID(null, data)); | ||
} | ||
|
||
@Test | ||
public void getBytes() { | ||
assertEquals(1, new UID(null, new byte[]{0x01}).getBytes().length); | ||
assertEquals(2, new UID(null, new byte[]{0x01, 0x00}).getBytes().length); | ||
assertEquals(4, new UID(null, new byte[]{0x01, 0x00, 0x00}).getBytes().length); | ||
assertEquals(4, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(8, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(8, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(8, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(8, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(16, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(16, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(16, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(16, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(16, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(16, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(16, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
assertEquals(16, new UID(null, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}).getBytes().length); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideUIDs") | ||
public void compareToAndEquals(UID a, UID b, int expectation) { | ||
assertEquals(expectation, a.compareTo(b), "compareTo returned unexpected value"); | ||
assertEquals(expectation * -1, b.compareTo(a), "compareTo returned unexpected value"); | ||
assertEquals(expectation == 0, a.equals(b), "equals returned unexpected value"); | ||
} | ||
|
||
private static Stream<Arguments> provideUIDs() { | ||
return Stream.of( | ||
Arguments.of( | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | ||
0), | ||
Arguments.of( | ||
new UID(null, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | ||
new UID("", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | ||
1), | ||
Arguments.of( | ||
new UID("a", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | ||
new UID("b", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | ||
-1), | ||
Arguments.of( | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}), | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}), | ||
1), | ||
Arguments.of( | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}), | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}), | ||
1), | ||
Arguments.of( | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}), | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}), | ||
1), | ||
Arguments.of( | ||
new UID("x", new byte[]{0x12, 0x34, 0x56, 0x78, 0x09}), | ||
new UID("x", new byte[]{0x12, 0x34, 0x55, 0x78, 0x09}), | ||
1), | ||
Arguments.of( | ||
new UID("x", new byte[]{0x12, 0x34, 0x56, 0x78, 0x09}), | ||
new UID("x", new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}), | ||
1)); | ||
} | ||
} |