|
package fr.pinguet62.util; |
|
|
|
import java.util.Arrays; |
|
import java.util.Collection; |
|
import java.util.Collections; |
|
import java.util.List; |
|
import java.util.function.Consumer; |
|
|
|
import org.junit.Assert; |
|
import org.junit.Test; |
|
|
|
/** Test for {@link fr.pinguet62.util.Hexavigesimal}. */ |
|
public final class HexavigesimalTest { |
|
|
|
/** |
|
* Case who associates a {@code long} value and {@code String} representation. |
|
*/ |
|
private static class Case { |
|
/** The {@code String} representation. */ |
|
String representation; |
|
|
|
/** The {@code long} value. */ |
|
long value; |
|
|
|
/** |
|
* Constructor. |
|
* |
|
* @param value |
|
* The {@code long} value. |
|
* @param representation |
|
* The {@code String} representation. |
|
*/ |
|
Case(long value, String representation) { |
|
this.value = value; |
|
this.representation = representation; |
|
} |
|
} |
|
|
|
/** |
|
* The association of {@code long} value and {@code String} representation to test. |
|
*/ |
|
private static Collection<Case> CASES = Arrays.asList(new Case(0L, "A"), |
|
new Case(1L, "B"), new Case(24L, "Y"), new Case(25L, "Z"), |
|
new Case(26L, "AA"), new Case(27L, "AB"), new Case(51L, "AZ"), |
|
new Case(52L, "BA"), new Case(53L, "BB"), new Case(700L, "ZY"), |
|
new Case(701L, "ZZ"), new Case(702L, "AAA"), new Case(703L, "AAB"), |
|
new Case(1433L, "BCD")); |
|
|
|
/** |
|
* Check that the method throw a {@code IllegalArgumentException} with invalid {@code String} representation. |
|
* |
|
* @param consumer |
|
* The method to execute. |
|
* @throws AssertionError |
|
* If the {@code IllegalArgumentException} does not thrown. |
|
*/ |
|
private void checkBadRepresentation(Consumer<String> consumer) { |
|
for (String representation : Arrays.asList(null, "", "123", "abc", "A-Z")) |
|
try { |
|
consumer.accept(representation); |
|
Assert.fail(); |
|
} catch (IllegalArgumentException e) { |
|
} catch (Exception e) { |
|
Assert.fail(); |
|
} |
|
} |
|
|
|
/** |
|
* Check that the method throw a {@code IllegalArgumentException} with invalid {@code long} values. |
|
* |
|
* @param consumer |
|
* The method to execute. |
|
* @throws AssertionError |
|
* If the {@code IllegalArgumentException} does not thrown. |
|
*/ |
|
private void checkBadValue(Consumer<Long> consumer) { |
|
for (Long value : Arrays.asList(Long.MIN_VALUE, -99L, -69L, -1L)) |
|
try { |
|
consumer.accept(value); |
|
Assert.fail(); |
|
} catch (IllegalArgumentException e) { |
|
} catch (Exception e) { |
|
Assert.fail(); |
|
} |
|
} |
|
|
|
/** |
|
* Test for {@link fr.pinguet62.util.Hexavigesimal#compareTo(Hexavigesimal)}. <br /> |
|
* Validate the correct comparison. |
|
*/ |
|
@Test |
|
public void test_compareTo() { |
|
// -1 : value1 > value2 |
|
Assert.assertTrue(Hexavigesimal.valueOf(0).compareTo(Hexavigesimal.valueOf(1)) < 0); |
|
Assert.assertTrue(Hexavigesimal.valueOf(0).compareTo(Hexavigesimal.valueOf(2)) < 0); |
|
Assert.assertTrue(Hexavigesimal.valueOf(1).compareTo(Hexavigesimal.valueOf(5)) < 0); |
|
// 0 : value1 == value2 |
|
Assert.assertEquals(0, Hexavigesimal.valueOf(0).compareTo(Hexavigesimal.valueOf(0))); |
|
Assert.assertEquals(0, Hexavigesimal.valueOf(1).compareTo(Hexavigesimal.valueOf(1))); |
|
// +1 : value1 < value2 |
|
Assert.assertTrue(Hexavigesimal.valueOf(1).compareTo(Hexavigesimal.valueOf(0)) > 0); |
|
Assert.assertTrue(Hexavigesimal.valueOf(2).compareTo(Hexavigesimal.valueOf(0)) > 0); |
|
Assert.assertTrue(Hexavigesimal.valueOf(5).compareTo(Hexavigesimal.valueOf(1)) > 0); |
|
|
|
// Sort using natural sorting |
|
List<Hexavigesimal> values = Arrays.asList(Hexavigesimal.valueOf(1), Hexavigesimal.valueOf(2), Hexavigesimal.valueOf(0)); |
|
Collections.sort(values); |
|
Assert.assertEquals(0, values.get(0).longValue()); |
|
Assert.assertEquals(1, values.get(1).longValue()); |
|
Assert.assertEquals(2, values.get(2).longValue()); |
|
} |
|
|
|
/** |
|
* Test for {@link fr.pinguet62.util.Hexavigesimal#longValue()}. <br /> |
|
* Validate the correct conversion to {@code long} value. |
|
*/ |
|
@Test |
|
public void test_longValue() { |
|
for (Case entry : CASES) { |
|
Assert.assertEquals(entry.value, new Hexavigesimal(entry.value).longValue()); |
|
Assert.assertEquals(entry.value, Hexavigesimal.valueOf(entry.value) .longValue()); |
|
Assert.assertEquals(entry.value, new Hexavigesimal(entry.representation).longValue()); |
|
Assert.assertEquals(entry.value, Hexavigesimal.valueOf(entry.representation).longValue()); |
|
} |
|
} |
|
|
|
/** |
|
* Test for {@link fr.pinguet62.util.Hexavigesimal#Hexavigesimal()}, |
|
* {@link fr.pinguet62.util.Hexavigesimal#Hexavigesimal(long))} and |
|
* {@link fr.pinguet62.util.Hexavigesimal#Hexavigesimal(String))}. <br /> |
|
* Validate the correct instantiation. |
|
*/ |
|
@Test |
|
public void test_new() { |
|
// Default constructor |
|
Hexavigesimal valueDefault = new Hexavigesimal(); |
|
Assert.assertEquals(0, valueDefault.longValue()); |
|
Assert.assertEquals("A", valueDefault.toString()); |
|
|
|
// Long value |
|
for (Case entry : CASES) { |
|
Hexavigesimal value = new Hexavigesimal(entry.value); |
|
Assert.assertEquals(entry.value, value.longValue()); |
|
Assert.assertEquals(entry.representation, value.toString()); |
|
} |
|
|
|
// String representation |
|
for (Case entry : CASES) { |
|
Hexavigesimal value = new Hexavigesimal(entry.representation); |
|
Assert.assertEquals(entry.value, value.longValue()); |
|
Assert.assertEquals(entry.representation, value.toString()); |
|
} |
|
|
|
// IllegalArgumentException |
|
checkBadValue(Hexavigesimal::new); |
|
checkBadRepresentation(Hexavigesimal::new); |
|
} |
|
|
|
/** |
|
* Test for {@link fr.pinguet62.util.Hexavigesimal#parse(String)}. <br /> |
|
* Validate the correct conversion {@code String} to {@code long}. |
|
*/ |
|
@Test |
|
public void test_parse() { |
|
for (Case entry : CASES) |
|
Assert.assertEquals(entry.value, Hexavigesimal.parse(entry.representation)); |
|
|
|
// IllegalArgumentException |
|
checkBadRepresentation(Hexavigesimal::parse); |
|
} |
|
|
|
/** |
|
* Test for {@link fr.pinguet62.util.Hexavigesimal#toString()} and {@link fr.pinguet62.util.Hexavigesimal#toString(long)}. <br /> |
|
* Validate the correct format of {@code String} representation. |
|
*/ |
|
@Test |
|
public void test_toString() { |
|
// Static method |
|
for (Case entry : CASES) |
|
Assert.assertEquals(entry.representation, Hexavigesimal.toString(entry.value)); |
|
|
|
// IllegalArgumentException |
|
checkBadValue(Hexavigesimal::toString); |
|
|
|
// Instance method |
|
for (Case entry : CASES) |
|
Assert.assertEquals(entry.representation, new Hexavigesimal(entry.value).toString()); |
|
} |
|
|
|
/** |
|
* Test for {@link fr.pinguet62.util.Hexavigesimal#valueOf(long)} and |
|
* {@link fr.pinguet62.util.Hexavigesimal#valueOf(String)}. <br /> |
|
* Validate the correct instantiation of {@code Hexavigesimal} object. |
|
*/ |
|
@Test |
|
public void test_valueOf() { |
|
// Long value |
|
for (Case entry : CASES) { |
|
Hexavigesimal value = Hexavigesimal.valueOf(entry.value); |
|
Assert.assertEquals(entry.value, value.longValue()); |
|
Assert.assertEquals(entry.representation, value.toString()); |
|
} |
|
|
|
// String representation |
|
for (Case entry : CASES) { |
|
Hexavigesimal value = Hexavigesimal.valueOf(entry.representation); |
|
Assert.assertEquals(entry.value, value.longValue()); |
|
Assert.assertEquals(entry.representation, value.toString()); |
|
} |
|
} |
|
|
|
} |