Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,28 @@ public static void validateValue(double v, SchemaType sType, ValidationContext c
double d;

if ((x = sType.getFacet(SchemaType.FACET_MIN_EXCLUSIVE)) != null) {
if (compare(v, d = ((XmlObjectBase) x).getDoubleValue()) <= 0) {
if (compare(v, d = ((XmlObjectBase) x).getDoubleValue()) <= 0 || incomparable(v, d)) {
context.invalid(XmlErrorCodes.DATATYPE_MIN_EXCLUSIVE_VALID,
new Object[]{"double", v, d, QNameHelper.readable(sType)});
}
}

if ((x = sType.getFacet(SchemaType.FACET_MIN_INCLUSIVE)) != null) {
if (compare(v, d = ((XmlObjectBase) x).getDoubleValue()) < 0) {
if (compare(v, d = ((XmlObjectBase) x).getDoubleValue()) < 0 || incomparable(v, d)) {
context.invalid(XmlErrorCodes.DATATYPE_MIN_INCLUSIVE_VALID,
new Object[]{"double", v, d, QNameHelper.readable(sType)});
}
}

if ((x = sType.getFacet(SchemaType.FACET_MAX_INCLUSIVE)) != null) {
if (compare(v, d = ((XmlObjectBase) x).getDoubleValue()) > 0) {
if (compare(v, d = ((XmlObjectBase) x).getDoubleValue()) > 0 || incomparable(v, d)) {
context.invalid(XmlErrorCodes.DATATYPE_MAX_INCLUSIVE_VALID,
new Object[]{"double", v, d, QNameHelper.readable(sType)});
}
}

if ((x = sType.getFacet(SchemaType.FACET_MAX_EXCLUSIVE)) != null) {
if (compare(v, d = ((XmlObjectBase) x).getDoubleValue()) >= 0) {
if (compare(v, d = ((XmlObjectBase) x).getDoubleValue()) >= 0 || incomparable(v, d)) {
context.invalid(XmlErrorCodes.DATATYPE_MAX_EXCLUSIVE_VALID,
new Object[]{"double", v, d, QNameHelper.readable(sType)});
}
Expand All @@ -102,6 +102,12 @@ public static void validateValue(double v, SchemaType sType, ValidationContext c
}
}

// NaN is equal to itself but neither less than nor greater than any other
// value, so it satisfies none of the bound facets of a non-NaN bound
private static boolean incomparable(double v, double bound) {
return Double.isNaN(v) != Double.isNaN(bound);
}

protected void validate_simpleval(String lexical, ValidationContext ctx) {
validateLexical(lexical, schemaType(), ctx);
validateValue(getDoubleValue(), schemaType(), ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,28 @@ public static void validateValue(float v, SchemaType sType, ValidationContext co
float f;

if ((x = sType.getFacet(SchemaType.FACET_MIN_EXCLUSIVE)) != null) {
if (compare(v, f = ((XmlObjectBase) x).getFloatValue()) <= 0) {
if (compare(v, f = ((XmlObjectBase) x).getFloatValue()) <= 0 || incomparable(v, f)) {
context.invalid(XmlErrorCodes.DATATYPE_MIN_EXCLUSIVE_VALID,
new Object[]{"float", v, f, QNameHelper.readable(sType)});
}
}

if ((x = sType.getFacet(SchemaType.FACET_MIN_INCLUSIVE)) != null) {
if (compare(v, f = ((XmlObjectBase) x).getFloatValue()) < 0) {
if (compare(v, f = ((XmlObjectBase) x).getFloatValue()) < 0 || incomparable(v, f)) {
context.invalid(XmlErrorCodes.DATATYPE_MIN_INCLUSIVE_VALID,
new Object[]{"float", v, f, QNameHelper.readable(sType)});
}
}

if ((x = sType.getFacet(SchemaType.FACET_MAX_INCLUSIVE)) != null) {
if (compare(v, f = ((XmlObjectBase) x).getFloatValue()) > 0) {
if (compare(v, f = ((XmlObjectBase) x).getFloatValue()) > 0 || incomparable(v, f)) {
context.invalid(XmlErrorCodes.DATATYPE_MAX_INCLUSIVE_VALID,
new Object[]{"float", v, f, QNameHelper.readable(sType)});
}
}

if ((x = sType.getFacet(SchemaType.FACET_MAX_EXCLUSIVE)) != null) {
if (compare(v, f = ((XmlObjectBase) x).getFloatValue()) >= 0) {
if (compare(v, f = ((XmlObjectBase) x).getFloatValue()) >= 0 || incomparable(v, f)) {
context.invalid(XmlErrorCodes.DATATYPE_MAX_EXCLUSIVE_VALID,
new Object[]{"float", v, f, QNameHelper.readable(sType)});
}
Expand All @@ -102,6 +102,12 @@ public static void validateValue(float v, SchemaType sType, ValidationContext co
}
}

// NaN is equal to itself but neither less than nor greater than any other
// value, so it satisfies none of the bound facets of a non-NaN bound
private static boolean incomparable(float v, float bound) {
return Float.isNaN(v) != Float.isNaN(bound);
}

protected void validate_simpleval(String lexical, ValidationContext ctx) {
validateLexical(lexical, schemaType(), ctx);
validateValue(getFloatValue(), schemaType(), ctx);
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/misc/checkin/IncomparableMinFacetValidateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class IncomparableMinFacetValidateTest {
private static final String DT_INCOMPARABLE = "2000-01-01T12:00:00";
private static final String DUR_BOUND = "P30D";
private static final String DUR_INCOMPARABLE = "P1M";
// NaN is equal to itself but incomparable with every other float or double
private static final String NUM_BOUND = "0";

private static final String[] FACETS = {
"minInclusive", "minExclusive", "maxInclusive", "maxExclusive"
Expand All @@ -64,6 +66,8 @@ private static String xsd() {
for (String facet : FACETS) {
sb.append(element("dt-" + facet, "dateTime", facet, DT_BOUND));
sb.append(element("dur-" + facet, "duration", facet, DUR_BOUND));
sb.append(element("flt-" + facet, "float", facet, NUM_BOUND));
sb.append(element("dbl-" + facet, "double", facet, NUM_BOUND));
}
return sb.append("</xs:schema>").toString();
}
Expand Down Expand Up @@ -110,6 +114,14 @@ void incomparableDurationFailsEveryBound() throws Exception {
}
}

@Test
void nanFailsEveryBound() throws Exception {
for (String facet : FACETS) {
assertInvalid("flt-" + facet, "NaN");
assertInvalid("dbl-" + facet, "NaN");
}
}

@Test
void comparableDateTimeStillChecked() throws Exception {
// equal to the bound
Expand All @@ -133,4 +145,16 @@ void comparableDurationStillChecked() throws Exception {
assertInvalid("dur-minInclusive", "P27D");
assertInvalid("dur-minExclusive", "P27D");
}

@Test
void comparableFloatAndDoubleStillChecked() throws Exception {
for (String type : new String[]{"flt-", "dbl-"}) {
assertValid(type + "minInclusive", "0");
assertInvalid(type + "minExclusive", "0");
assertValid(type + "minExclusive", "INF");
assertInvalid(type + "minInclusive", "-INF");
assertValid(type + "maxInclusive", "-INF");
assertInvalid(type + "maxExclusive", "INF");
}
}
}
Loading