Skip to content

Commit 5577ec9

Browse files
authored
Merge pull request #23894 from bendjones/bendjones/TestJSONEncoder_fixes_pt2
[TestJSONEncoder] - testEncodingDate still has an unhandled edge case
2 parents 7f73f47 + 5f13843 commit 5577ec9

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

test/stdlib/TestJSONEncoder.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ class TestJSONEncoder : TestJSONEncoderSuper {
256256
/// our precision requests in every case. This bug effects Darwin, FreeBSD, and Linux currently
257257
/// causing this test (which uses the current time) to fail occasionally.
258258
let evalEdgeCase: (Date, Date) -> () = { decodedDate, expectedDate in
259-
if formattedLength(of: decodedDate.timeIntervalSinceReferenceDate) > DBL_DECIMAL_DIG + 2 {
259+
if formattedLength(of: decodedDate.timeIntervalSinceReferenceDate) > DBL_DECIMAL_DIG {
260260
let adjustedTimeIntervalSinceReferenceDate: (Date) -> Double = {
261261
let adjustment = pow(10, Double(DBL_DECIMAL_DIG))
262262
return Double(floor(adjustment * $0.timeIntervalSinceReferenceDate) / adjustment)
@@ -286,9 +286,15 @@ class TestJSONEncoder : TestJSONEncoderSuper {
286286
}
287287
}
288288

289-
// Test the above `snprintf` edge case evaluation with a known triggering case
290-
let knownBadDate = Date(timeIntervalSinceReferenceDate: 0.0021413276231263384)
291-
localTestRoundTrip(of: TopLevelWrapper(knownBadDate))
289+
// Test the above `snprintf` edge case evaluation with known triggering cases
290+
291+
// Tests the two precision digits larger case
292+
let knownBadDateTwoExtraDigits = Date(timeIntervalSinceReferenceDate: 0.0021413276231263384)
293+
localTestRoundTrip(of: TopLevelWrapper(knownBadDateTwoExtraDigits))
294+
295+
// Tests the one precision digit larger case
296+
let knownBadDateOneExtraDigit = Date(timeIntervalSinceReferenceDate: 576487829.7193049)
297+
localTestRoundTrip(of: TopLevelWrapper(knownBadDateOneExtraDigit))
292298

293299
// We can't encode a top-level Date, so it'll be wrapped in a dictionary.
294300
localTestRoundTrip(of: TopLevelWrapper(Date()))

0 commit comments

Comments
 (0)