* Tests the {@link RangeFormat#format(Object, StringBuffer, FieldPosition)} method with numbers.
*/
@Test
public void testFormatNumbers() {
format = new RangeFormat(Locale.CANADA);
minPos = new FieldPosition(RangeFormat.Field.MIN_VALUE);
maxPos = new FieldPosition(RangeFormat.Field.MAX_VALUE);
// Closed range
assertEquals("[-10 … 20]", format(NumberRange.create(-10, true, 20, true)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 4, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 7, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 9, maxPos.getEndIndex());
// Open range
assertEquals("(-3 … 4)", format(NumberRange.create(-3, false, 4, false)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 3, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 7, maxPos.getEndIndex());
// Half-open range
assertEquals("[2 … 8)", format(NumberRange.create(2, true, 8, false)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 2, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 5, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 6, maxPos.getEndIndex());
// Half-open range
assertEquals("(40 … 90]", format(NumberRange.create(40, false, 90, true)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 3, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 8, maxPos.getEndIndex());
// Single value
assertEquals("{300}", format(NumberRange.create(300, true, 300, true)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 4, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 1, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 4, maxPos.getEndIndex());
// Empty range
assertEquals("{}", format(NumberRange.create(300, true, 300, false)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 1, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 1, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 1, maxPos.getEndIndex());
// Negative infinity
assertEquals("(-∞ … 30]", format(NumberRange.create(Double.NEGATIVE_INFINITY, true, 30, true)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 3, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 8, maxPos.getEndIndex());
// Positive infinity
assertEquals("[50 … ∞)", format(NumberRange.create(50, true, Double.POSITIVE_INFINITY, true)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 3, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 7, maxPos.getEndIndex());
// Positive infinities
assertEquals("(-∞ … ∞)", format(NumberRange.create(Double.NEGATIVE_INFINITY, true, Double.POSITIVE_INFINITY, true)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 3, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 7, maxPos.getEndIndex());
// Positive infinity with integers
assertEquals("[50 … ∞)", format(new NumberRange<Integer>(Integer.class, 50, true, null, true)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 3, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 7, maxPos.getEndIndex());
// Negative infinity with integers
assertEquals("(-∞ … 40]", format(new NumberRange<Integer>(Integer.class, null, true, 40, true)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 3, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 8, maxPos.getEndIndex());
// Measurement
assertEquals("[-10 … 20] m", format(MeasurementRange.create(-10, true, 20, true, SI.METRE)));
assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
assertEquals("minPos.endIndex", 4, minPos.getEndIndex());
assertEquals("maxPos.beginIndex", 7, maxPos.getBeginIndex());
assertEquals("maxPos.endIndex", 9, maxPos.getEndIndex());
maxPos = new FieldPosition(RangeFormat.Field.UNIT);
assertEquals("[-1 … 2] km", format(MeasurementRange.create(-1, true, 2, true, SI.KILOMETRE)));
assertEquals("unitPos.beginIndex", 9, maxPos.getBeginIndex());
assertEquals("unitPos.endIndex", 11, maxPos.getEndIndex());
}