Sort methods.
This commit is contained in:
parent
61b1747580
commit
64e6ddbc6b
|
@ -33,15 +33,300 @@ import java.util.Map;
|
|||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
enum Enum64 {
|
||||
A00, A01, A02, A03, A04, A05, A06, A07, A08, A09, A10, A11, A12, A13, A14, A15,
|
||||
A16, A17, A18, A19, A20, A21, A22, A23, A24, A25, A26, A27, A28, A29, A30, A31,
|
||||
A32, A33, A34, A35, A36, A37, A38, A39, A40, A41, A42, A43, A44, A45, A46, A47,
|
||||
A48, A49, A50, A51, A52, A53, A54, A55, A56, A57, A58, A59, A60, A61, A62, A63
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class EnumUtilsTest {
|
||||
|
||||
private void assertArrayEquals(final long[] actual, final long... expected) {
|
||||
Assertions.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructable() {
|
||||
// enforce public constructor
|
||||
new EnumUtils();
|
||||
public void test_generateBitVector() {
|
||||
assertEquals(0L, EnumUtils.generateBitVector(Traffic.class, EnumSet.noneOf(Traffic.class)));
|
||||
assertEquals(1L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED)));
|
||||
assertEquals(2L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.AMBER)));
|
||||
assertEquals(4L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.GREEN)));
|
||||
assertEquals(3L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER)));
|
||||
assertEquals(5L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.GREEN)));
|
||||
assertEquals(6L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.AMBER, Traffic.GREEN)));
|
||||
assertEquals(7L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN)));
|
||||
|
||||
// 64 values Enum (to test whether no int<->long jdk conversion issue exists)
|
||||
assertEquals((1L << 31), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A31)));
|
||||
assertEquals((1L << 32), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A32)));
|
||||
assertEquals((1L << 63), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A63)));
|
||||
assertEquals(Long.MIN_VALUE, EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A63)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_longClass() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> EnumUtils.generateBitVector(TooMany.class, EnumSet.of(TooMany.A1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_longClassWithArray() {
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(TooMany.class, TooMany.A1));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test_generateBitVector_nonEnumClass() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
Class rawType = Object.class;
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
List rawList = new ArrayList();
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(rawType, rawList));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test_generateBitVector_nonEnumClassWithArray() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
Class rawType = Object.class;
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(rawType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullArray() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(Traffic.class, (Traffic[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullArrayElement() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> EnumUtils.generateBitVector(Traffic.class, Traffic.RED, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullClass() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(null, EnumSet.of(Traffic.RED)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullClassWithArray() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(null, Traffic.RED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullElement() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> EnumUtils.generateBitVector(Traffic.class, Arrays.asList(Traffic.RED, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullIterable() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> EnumUtils.generateBitVector(Traffic.class, (Iterable<Traffic>) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectorFromArray() {
|
||||
assertEquals(0L, EnumUtils.generateBitVector(Traffic.class));
|
||||
assertEquals(1L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED));
|
||||
assertEquals(2L, EnumUtils.generateBitVector(Traffic.class, Traffic.AMBER));
|
||||
assertEquals(4L, EnumUtils.generateBitVector(Traffic.class, Traffic.GREEN));
|
||||
assertEquals(3L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER));
|
||||
assertEquals(5L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.GREEN));
|
||||
assertEquals(6L, EnumUtils.generateBitVector(Traffic.class, Traffic.AMBER, Traffic.GREEN));
|
||||
assertEquals(7L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN));
|
||||
//gracefully handles duplicates:
|
||||
assertEquals(7L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN, Traffic.GREEN));
|
||||
|
||||
// 64 values Enum (to test whether no int<->long jdk conversion issue exists)
|
||||
assertEquals((1L << 31), EnumUtils.generateBitVector(Enum64.class, Enum64.A31));
|
||||
assertEquals((1L << 32), EnumUtils.generateBitVector(Enum64.class, Enum64.A32));
|
||||
assertEquals((1L << 63), EnumUtils.generateBitVector(Enum64.class, Enum64.A63));
|
||||
assertEquals(Long.MIN_VALUE, EnumUtils.generateBitVector(Enum64.class, Enum64.A63));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors() {
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.noneOf(Traffic.class)), 0L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED)), 1L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.AMBER)), 2L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.GREEN)), 4L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER)), 3L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.GREEN)), 5L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.AMBER, Traffic.GREEN)), 6L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN)), 7L);
|
||||
|
||||
// 64 values Enum (to test whether no int<->long jdk conversion issue exists)
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A31)), (1L << 31));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A32)), (1L << 32));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A63)), (1L << 63));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A63)), Long.MIN_VALUE);
|
||||
|
||||
// More than 64 values Enum
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, EnumSet.of(TooMany.M2)), 1L, 0L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, EnumSet.of(TooMany.L2, TooMany.M2)), 1L, (1L << 63));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test_generateBitVectors_nonEnumClass() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
Class rawType = Object.class;
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
List rawList = new ArrayList();
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(rawType, rawList));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test_generateBitVectors_nonEnumClassWithArray() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
Class rawType = Object.class;
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(rawType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullArray() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(Traffic.class, (Traffic[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullArrayElement() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullClass() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, EnumSet.of(Traffic.RED)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullClassWithArray() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, Traffic.RED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullElement() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> EnumUtils.generateBitVectors(Traffic.class, Arrays.asList(Traffic.RED, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullIterable() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, (Iterable<Traffic>) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectorsFromArray() {
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class), 0L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED), 1L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.AMBER), 2L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.GREEN), 4L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER), 3L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.GREEN), 5L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.AMBER, Traffic.GREEN), 6L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN), 7L);
|
||||
//gracefully handles duplicates:
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN, Traffic.GREEN), 7L);
|
||||
|
||||
// 64 values Enum (to test whether no int<->long jdk conversion issue exists)
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A31), (1L << 31));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A32), (1L << 32));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A63), (1L << 63));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A63), Long.MIN_VALUE);
|
||||
|
||||
// More than 64 values Enum
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, TooMany.M2), 1L, 0L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, TooMany.L2, TooMany.M2), 1L, (1L << 63));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnum() {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED"));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER"));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN"));
|
||||
assertNull(EnumUtils.getEnum(Traffic.class, "PURPLE"));
|
||||
assertNull(EnumUtils.getEnum(Traffic.class, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnum_defaultEnum() {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED", Traffic.AMBER));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER", Traffic.GREEN));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN", Traffic.RED));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.AMBER));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.GREEN));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.RED));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, null, Traffic.AMBER));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, null, Traffic.GREEN));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, null, Traffic.RED));
|
||||
assertNull(EnumUtils.getEnum(Traffic.class, "PURPLE", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnum_nonEnumClass() {
|
||||
final Class rawType = Object.class;
|
||||
assertNull(EnumUtils.getEnum(rawType, "rawType"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnum_nullClass() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.getEnum((Class<Traffic>) null, "PURPLE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumIgnoreCase() {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red"));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "Amber"));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "grEEn"));
|
||||
assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "purple"));
|
||||
assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumIgnoreCase_defaultEnum() {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red", Traffic.AMBER));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "Amber", Traffic.GREEN));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "grEEn", Traffic.RED));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", Traffic.AMBER));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "purple", Traffic.GREEN));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "pUrPlE", Traffic.RED));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.AMBER));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.GREEN));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.RED));
|
||||
assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumIgnoreCase_nonEnumClass() {
|
||||
final Class rawType = Object.class;
|
||||
assertNull(EnumUtils.getEnumIgnoreCase(rawType, "rawType"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumIgnoreCase_nullClass() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.getEnumIgnoreCase((Class<Traffic>) null, "PURPLE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumList() {
|
||||
final List<Traffic> test = EnumUtils.getEnumList(Traffic.class);
|
||||
assertEquals(3, test.size());
|
||||
assertEquals(Traffic.RED, test.get(0));
|
||||
assertEquals(Traffic.AMBER, test.get(1));
|
||||
assertEquals(Traffic.GREEN, test.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -58,15 +343,6 @@ public class EnumUtilsTest {
|
|||
assertFalse(test.containsKey("PURPLE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumList() {
|
||||
final List<Traffic> test = EnumUtils.getEnumList(Traffic.class);
|
||||
assertEquals(3, test.size());
|
||||
assertEquals(Traffic.RED, test.get(0));
|
||||
assertEquals(Traffic.AMBER, test.get(1));
|
||||
assertEquals(Traffic.GREEN, test.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_isValidEnum() {
|
||||
assertTrue(EnumUtils.isValidEnum(Traffic.class, "RED"));
|
||||
|
@ -95,293 +371,6 @@ public class EnumUtilsTest {
|
|||
assertThrows(NullPointerException.class, () -> EnumUtils.isValidEnumIgnoreCase(null, "PURPLE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnum() {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED"));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER"));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN"));
|
||||
assertNull(EnumUtils.getEnum(Traffic.class, "PURPLE"));
|
||||
assertNull(EnumUtils.getEnum(Traffic.class, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnum_nonEnumClass() {
|
||||
final Class rawType = Object.class;
|
||||
assertNull(EnumUtils.getEnum(rawType, "rawType"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnum_nullClass() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.getEnum((Class<Traffic>) null, "PURPLE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnum_defaultEnum() {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED", Traffic.AMBER));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER", Traffic.GREEN));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN", Traffic.RED));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.AMBER));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.GREEN));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.RED));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, null, Traffic.AMBER));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, null, Traffic.GREEN));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, null, Traffic.RED));
|
||||
assertNull(EnumUtils.getEnum(Traffic.class, "PURPLE", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumIgnoreCase() {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red"));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "Amber"));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "grEEn"));
|
||||
assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "purple"));
|
||||
assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumIgnoreCase_nonEnumClass() {
|
||||
final Class rawType = Object.class;
|
||||
assertNull(EnumUtils.getEnumIgnoreCase(rawType, "rawType"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumIgnoreCase_nullClass() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.getEnumIgnoreCase((Class<Traffic>) null, "PURPLE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumIgnoreCase_defaultEnum() {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red", Traffic.AMBER));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "Amber", Traffic.GREEN));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "grEEn", Traffic.RED));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", Traffic.AMBER));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "purple", Traffic.GREEN));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "pUrPlE", Traffic.RED));
|
||||
assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.AMBER));
|
||||
assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.GREEN));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.RED));
|
||||
assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullClass() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(null, EnumSet.of(Traffic.RED)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullClass() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, EnumSet.of(Traffic.RED)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullIterable() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> EnumUtils.generateBitVector(Traffic.class, (Iterable<Traffic>) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullIterable() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, (Iterable<Traffic>) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullElement() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> EnumUtils.generateBitVector(Traffic.class, Arrays.asList(Traffic.RED, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullElement() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> EnumUtils.generateBitVectors(Traffic.class, Arrays.asList(Traffic.RED, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullClassWithArray() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(null, Traffic.RED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullClassWithArray() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(null, Traffic.RED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullArray() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(Traffic.class, (Traffic[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullArray() {
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVectors(Traffic.class, (Traffic[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_nullArrayElement() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> EnumUtils.generateBitVector(Traffic.class, Traffic.RED, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors_nullArrayElement() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_longClass() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> EnumUtils.generateBitVector(TooMany.class, EnumSet.of(TooMany.A1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector_longClassWithArray() {
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(TooMany.class, TooMany.A1));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test_generateBitVector_nonEnumClass() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
Class rawType = Object.class;
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
List rawList = new ArrayList();
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(rawType, rawList));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test_generateBitVectors_nonEnumClass() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
Class rawType = Object.class;
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
List rawList = new ArrayList();
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(rawType, rawList));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test_generateBitVector_nonEnumClassWithArray() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
Class rawType = Object.class;
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVector(rawType));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void test_generateBitVectors_nonEnumClassWithArray() {
|
||||
@SuppressWarnings("rawtypes")
|
||||
final
|
||||
Class rawType = Object.class;
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.generateBitVectors(rawType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVector() {
|
||||
assertEquals(0L, EnumUtils.generateBitVector(Traffic.class, EnumSet.noneOf(Traffic.class)));
|
||||
assertEquals(1L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED)));
|
||||
assertEquals(2L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.AMBER)));
|
||||
assertEquals(4L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.GREEN)));
|
||||
assertEquals(3L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER)));
|
||||
assertEquals(5L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.GREEN)));
|
||||
assertEquals(6L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.AMBER, Traffic.GREEN)));
|
||||
assertEquals(7L, EnumUtils.generateBitVector(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN)));
|
||||
|
||||
// 64 values Enum (to test whether no int<->long jdk conversion issue exists)
|
||||
assertEquals((1L << 31), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A31)));
|
||||
assertEquals((1L << 32), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A32)));
|
||||
assertEquals((1L << 63), EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A63)));
|
||||
assertEquals(Long.MIN_VALUE, EnumUtils.generateBitVector(Enum64.class, EnumSet.of(Enum64.A63)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectors() {
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.noneOf(Traffic.class)), 0L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED)), 1L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.AMBER)), 2L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.GREEN)), 4L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER)), 3L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.GREEN)), 5L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.AMBER, Traffic.GREEN)), 6L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, EnumSet.of(Traffic.RED, Traffic.AMBER, Traffic.GREEN)), 7L);
|
||||
|
||||
// 64 values Enum (to test whether no int<->long jdk conversion issue exists)
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A31)), (1L << 31));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A32)), (1L << 32));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A63)), (1L << 63));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, EnumSet.of(Enum64.A63)), Long.MIN_VALUE);
|
||||
|
||||
// More than 64 values Enum
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, EnumSet.of(TooMany.M2)), 1L, 0L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, EnumSet.of(TooMany.L2, TooMany.M2)), 1L, (1L << 63));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectorFromArray() {
|
||||
assertEquals(0L, EnumUtils.generateBitVector(Traffic.class));
|
||||
assertEquals(1L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED));
|
||||
assertEquals(2L, EnumUtils.generateBitVector(Traffic.class, Traffic.AMBER));
|
||||
assertEquals(4L, EnumUtils.generateBitVector(Traffic.class, Traffic.GREEN));
|
||||
assertEquals(3L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER));
|
||||
assertEquals(5L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.GREEN));
|
||||
assertEquals(6L, EnumUtils.generateBitVector(Traffic.class, Traffic.AMBER, Traffic.GREEN));
|
||||
assertEquals(7L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN));
|
||||
//gracefully handles duplicates:
|
||||
assertEquals(7L, EnumUtils.generateBitVector(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN, Traffic.GREEN));
|
||||
|
||||
// 64 values Enum (to test whether no int<->long jdk conversion issue exists)
|
||||
assertEquals((1L << 31), EnumUtils.generateBitVector(Enum64.class, Enum64.A31));
|
||||
assertEquals((1L << 32), EnumUtils.generateBitVector(Enum64.class, Enum64.A32));
|
||||
assertEquals((1L << 63), EnumUtils.generateBitVector(Enum64.class, Enum64.A63));
|
||||
assertEquals(Long.MIN_VALUE, EnumUtils.generateBitVector(Enum64.class, Enum64.A63));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_generateBitVectorsFromArray() {
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class), 0L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED), 1L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.AMBER), 2L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.GREEN), 4L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER), 3L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.GREEN), 5L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.AMBER, Traffic.GREEN), 6L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN), 7L);
|
||||
//gracefully handles duplicates:
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Traffic.class, Traffic.RED, Traffic.AMBER, Traffic.GREEN, Traffic.GREEN), 7L);
|
||||
|
||||
// 64 values Enum (to test whether no int<->long jdk conversion issue exists)
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A31), (1L << 31));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A32), (1L << 32));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A63), (1L << 63));
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(Enum64.class, Enum64.A63), Long.MIN_VALUE);
|
||||
|
||||
// More than 64 values Enum
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, TooMany.M2), 1L, 0L);
|
||||
assertArrayEquals(EnumUtils.generateBitVectors(TooMany.class, TooMany.L2, TooMany.M2), 1L, (1L << 63));
|
||||
|
||||
}
|
||||
|
||||
private void assertArrayEquals(final long[] actual, final long... expected) {
|
||||
Assertions.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_processBitVector_nullClass() {
|
||||
final Class<Traffic> empty = null;
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.processBitVector(empty, 0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_processBitVectors_nullClass() {
|
||||
final Class<Traffic> empty = null;
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.processBitVectors(empty, 0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_processBitVector() {
|
||||
assertEquals(EnumSet.noneOf(Traffic.class), EnumUtils.processBitVector(Traffic.class, 0L));
|
||||
|
@ -400,6 +389,17 @@ public class EnumUtilsTest {
|
|||
assertEquals(EnumSet.of(Enum64.A63), EnumUtils.processBitVector(Enum64.class, Long.MIN_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_processBitVector_longClass() {
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.processBitVector(TooMany.class, 0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_processBitVector_nullClass() {
|
||||
final Class<Traffic> empty = null;
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.processBitVector(empty, 0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_processBitVectors() {
|
||||
assertEquals(EnumSet.noneOf(Traffic.class), EnumUtils.processBitVectors(Traffic.class, 0L));
|
||||
|
@ -437,11 +437,6 @@ public class EnumUtilsTest {
|
|||
assertEquals(EnumSet.of(Enum64.A63), EnumUtils.processBitVectors(Enum64.class, Long.MIN_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_processBitVector_longClass() {
|
||||
assertThrows(IllegalArgumentException.class, () -> EnumUtils.processBitVector(TooMany.class, 0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_processBitVectors_longClass() {
|
||||
assertEquals(EnumSet.noneOf(TooMany.class), EnumUtils.processBitVectors(TooMany.class, 0L));
|
||||
|
@ -485,17 +480,18 @@ public class EnumUtilsTest {
|
|||
assertEquals(EnumSet.of(TooMany.A, TooMany.B, TooMany.C, TooMany.M2), EnumUtils.processBitVectors(TooMany.class, 9L, 7L));
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void test_processBitVectors_nullClass() {
|
||||
final Class<Traffic> empty = null;
|
||||
assertThrows(NullPointerException.class, () -> EnumUtils.processBitVectors(empty, 0L));
|
||||
}
|
||||
|
||||
enum Traffic {
|
||||
RED, AMBER, GREEN
|
||||
}
|
||||
@Test
|
||||
public void testConstructable() {
|
||||
// enforce public constructor
|
||||
new EnumUtils();
|
||||
}
|
||||
|
||||
enum Enum64 {
|
||||
A00, A01, A02, A03, A04, A05, A06, A07, A08, A09, A10, A11, A12, A13, A14, A15,
|
||||
A16, A17, A18, A19, A20, A21, A22, A23, A24, A25, A26, A27, A28, A29, A30, A31,
|
||||
A32, A33, A34, A35, A36, A37, A38, A39, A40, A41, A42, A43, A44, A45, A46, A47,
|
||||
A48, A49, A50, A51, A52, A53, A54, A55, A56, A57, A58, A59, A60, A61, A62, A63
|
||||
}
|
||||
|
||||
enum TooMany {
|
||||
|
@ -503,3 +499,7 @@ enum TooMany {
|
|||
A1, B1, C1, D1, E1, F1, G1, H1, I1, J1, K1, L1, M1, N1, O1, P1, Q1, R1, S1, T1,
|
||||
U1, V1, W1, X1, Y1, Z1, A2, B2, C2, D2, E2, F2, G2, H2, I2, J2, K2, L2, M2
|
||||
}
|
||||
|
||||
enum Traffic {
|
||||
RED, AMBER, GREEN
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue