import Testing
@testable import PureXML
/// A `double`/`float` value matches by IDENTITY in every value-equality context, under
/// which `NaN` is identical to `NaN` even though IEEE `==` reports `NaN != NaN`. The prior
/// comparison used IEEE equality and so dropped `NaN`, a false positive the libxml2 facet
/// differential caught. The identity reading is what the reference (libxml2) and XSD 1.1
/// do across the board, verified against `xmllint`: an `enumeration` listing `NaN` admits
/// `NaN`; two `NaN`-valued `unique` fields collide as duplicates; and `fixed="NaN"` matches
/// `NaN`. (A strict XSD-1.0 `·equal·` reading would instead leave `NaN` unequal to itself
/// for `fixed`/`key`; an adversarial critic flagged that path, but libxml2 rejects two
/// `NaN` keys and accepts `fixed="NaN"`, so a single identity `==` matches it.) Ordering
/// (`<`) keeps `NaN` incomparable, so the range facets are unaffected.
@Suite("Double NaN value-space identity")
struct SchemaDoubleEnumerationTests {
private func schema(_ base: String, _ facets: String) throws -> PureXML.Schema.Document {
try PureXML.Schema.Document("""
\(facets)
""")
}
@Test("NaN is admitted by an enumeration that lists NaN")
func test_nanInEnumeration() throws {
let doc = try schema("xs:double", #""#)
#expect(try doc.validate("NaN").isEmpty) // NaN is identical to NaN in the value space
#expect(try doc.validate("3.14").isEmpty)
#expect(try !doc.validate("2.0").isEmpty) // a value not enumerated is still rejected
}
@Test("NaN is rejected by an enumeration that does not list it")
func test_nanNotInEnumeration() throws {
let doc = try schema("xs:double", #""#)
#expect(try !doc.validate("NaN").isEmpty)
}
@Test("INF and -INF match their enumerations")
func test_infinityEnumeration() throws {
let doc = try schema("xs:double", #""#)
#expect(try doc.validate("INF").isEmpty)
#expect(try doc.validate("-INF").isEmpty)
#expect(try !doc.validate("5.0").isEmpty)
}
@Test("an xs:float NaN enumeration matches too")
func test_floatNaN() throws {
let doc = try schema("xs:float", #""#)
#expect(try doc.validate("NaN").isEmpty)
}
@Test("two NaN unique keys collide as duplicates (identity, matching libxml2)")
func test_nanUniqueKeysCollide() throws {
let doc = try PureXML.Schema.Document("""
""")
// libxml2 reports "Duplicate key-sequence ['nan']"; NaN is identical to NaN here.
#expect(try !doc.validate(" ").isEmpty)
}
@Test("a NaN fixed value matches a NaN instance (identity, matching libxml2)")
func test_nanFixedMatches() throws {
let doc = try PureXML.Schema.Document("""
""")
#expect(try doc.validate("NaN").isEmpty) // fixed="NaN" matches NaN
#expect(try !doc.validate("3.0").isEmpty) // a non-NaN value does not
}
}