@@ -171,7 +171,6 @@ final class CalendarTests : XCTestCase {
171
171
172
172
func validateOrdinality( _ expected: Array < Array < Int ? > > , calendar: Calendar , date: Date ) {
173
173
let units : [ Calendar . Component ] = [ . era, . year, . month, . day, . hour, . minute, . second, . weekday, . weekdayOrdinal, . quarter, . weekOfMonth, . weekOfYear, . yearForWeekOfYear, . nanosecond]
174
-
175
174
var smallerIndex = 0
176
175
for smaller in units {
177
176
var largerIndex = 0
@@ -274,25 +273,6 @@ final class CalendarTests : XCTestCase {
274
273
validateOrdinality ( expected, calendar: calendar, date: Date ( timeIntervalSinceReferenceDate: 668858528.712 ) )
275
274
}
276
275
#endif // arch(x86_64) || arch(arm64)
277
-
278
- @available ( FoundationPreview 0 . 4 , * )
279
- func test_dateSequence( ) {
280
- let cal = Calendar ( identifier: . gregorian)
281
- // August 22, 2022 at 3:02:38 PM PDT
282
- let date = Date ( timeIntervalSinceReferenceDate: 682898558.712307 )
283
- let next3Minutes = [
284
- Date ( timeIntervalSinceReferenceDate: 682898580.0 ) ,
285
- Date ( timeIntervalSinceReferenceDate: 682898640.0 ) ,
286
- Date ( timeIntervalSinceReferenceDate: 682898700.0 ) ,
287
- ]
288
-
289
- let dates = cal. dates ( startingAt: date, matching: DateComponents ( second: 0 ) , matchingPolicy: . nextTime)
290
-
291
- let result = zip ( next3Minutes, dates)
292
- for i in result {
293
- XCTAssertEqual ( i. 0 , i. 1 )
294
- }
295
- }
296
276
297
277
// This test requires 64-bit integers
298
278
#if (arch(x86_64) || arch(arm64)) && FOUNDATION_FRAMEWORK
@@ -655,7 +635,7 @@ final class CalendarTests : XCTestCase {
655
635
XCTAssertEqual ( [ " domingo " , " lunes " , " martes " , " mi \u{00e9} rcoles " , " jueves " , " viernes " , " s \u{00e1} bado " ] , c. standaloneWeekdaySymbols)
656
636
XCTAssertEqual ( [ " domingo " , " lunes " , " martes " , " mi \u{00e9} rcoles " , " jueves " , " viernes " , " s \u{00e1} bado " ] , c. weekdaySymbols)
657
637
}
658
-
638
+
659
639
func test_weekOfMonthLoop( ) {
660
640
// This test simply needs to not hang or crash
661
641
let date = Date ( timeIntervalSinceReferenceDate: 2.4499581972890255e+18 )
@@ -666,6 +646,105 @@ final class CalendarTests : XCTestCase {
666
646
_ = calendar. nextDate ( after: date, matching: components, matchingPolicy: . previousTimePreservingSmallerComponents)
667
647
}
668
648
649
+ @available ( FoundationPreview 0 . 4 , * )
650
+ func test_datesAdding_range( ) {
651
+ let startDate = Date ( timeIntervalSinceReferenceDate: 689292158.712307 ) // 2022-11-04 22:02:38 UTC
652
+ let endDate = startDate + ( 86400 * 3 ) + ( 3600 * 2 ) // 3 days + 2 hours later - cross a DST boundary which adds a day with an additional hour in it
653
+ var cal = Calendar ( identifier: . gregorian)
654
+ let tz = TimeZone ( name: " America/Los_Angeles " ) !
655
+ cal. timeZone = tz
656
+
657
+ // Purpose of this test is not to test the addition itself (we have others for that), but to smoke test the wrapping API
658
+ let numberOfDays = Array ( cal. dates ( startingAt: startDate, in: startDate..< endDate, byAdding: . day) ) . count
659
+ XCTAssertEqual ( numberOfDays, 3 )
660
+ }
661
+
662
+ @available ( FoundationPreview 0 . 4 , * )
663
+ func test_datesMatching_simpleExample( ) {
664
+ let cal = Calendar ( identifier: . gregorian)
665
+ // August 22, 2022 at 3:02:38 PM PDT
666
+ let date = Date ( timeIntervalSinceReferenceDate: 682898558.712307 )
667
+ let next3Minutes = [
668
+ Date ( timeIntervalSinceReferenceDate: 682898580.0 ) ,
669
+ Date ( timeIntervalSinceReferenceDate: 682898640.0 ) ,
670
+ Date ( timeIntervalSinceReferenceDate: 682898700.0 ) ,
671
+ ]
672
+
673
+ let dates = cal. dates ( startingAt: date, matching: DateComponents ( second: 0 ) , matchingPolicy: . nextTime)
674
+
675
+ let result = zip ( next3Minutes, dates)
676
+ for i in result {
677
+ XCTAssertEqual ( i. 0 , i. 1 )
678
+ }
679
+ }
680
+
681
+ @available ( FoundationPreview 0 . 4 , * )
682
+ func test_datesMatching( ) {
683
+ let startDate = Date ( timeIntervalSinceReferenceDate: 682898558.712307 ) // 2022-08-22 22:02:38 UTC
684
+ let endDate = startDate + ( 86400 * 3 )
685
+ var cal = Calendar ( identifier: . gregorian)
686
+ let tz = TimeZone . gmt
687
+ cal. timeZone = tz
688
+
689
+ var dc = DateComponents ( )
690
+ dc. hour = 22
691
+
692
+ // There should be 3 "hour 23"s in this range.
693
+ let numberOfMatchesForward = Array ( cal. dates ( startingAt: startDate, in: startDate..< endDate, matching: dc) ) . count
694
+ XCTAssertEqual ( numberOfMatchesForward, 3 )
695
+
696
+ let numberOfMatchesBackward = Array ( cal. dates ( startingAt: endDate, in: startDate..< endDate, matching: dc, direction: . backward) ) . count
697
+ XCTAssertEqual ( numberOfMatchesBackward, 3 )
698
+
699
+ let unboundedForward = Array ( cal. dates ( startingAt: startDate, matching: dc) . prefix ( 10 ) )
700
+ XCTAssertEqual ( unboundedForward. count, 10 )
701
+
702
+ // sanity check of results
703
+ XCTAssertTrue ( unboundedForward. first! < unboundedForward. last!)
704
+
705
+ let unboundedBackward = Array ( cal. dates ( startingAt: startDate, matching: dc, direction: . backward) . prefix ( 10 ) )
706
+ XCTAssertEqual ( unboundedForward. count, 10 )
707
+
708
+ // sanity check of results
709
+ XCTAssertTrue ( unboundedBackward. first! > unboundedBackward. last!)
710
+ }
711
+
712
+ @available ( FoundationPreview 0 . 4 , * )
713
+ func test_dayOfYear_bounds( ) {
714
+ let date = Date ( timeIntervalSinceReferenceDate: 682898558.712307 ) // 2022-08-22 22:02:38 UTC, day 234
715
+ var cal = Calendar ( identifier: . gregorian)
716
+ let tz = TimeZone . gmt
717
+ cal. timeZone = tz
718
+
719
+ // Test some invalid day of years
720
+ var dayOfYearComps = DateComponents ( )
721
+ dayOfYearComps. dayOfYear = 0
722
+ let zeroDay = cal. nextDate ( after: date, matching: dayOfYearComps, matchingPolicy: . previousTimePreservingSmallerComponents)
723
+ XCTAssertNil ( zeroDay)
724
+
725
+ dayOfYearComps. dayOfYear = 400
726
+ let futureDay = cal. nextDate ( after: date, matching: dayOfYearComps, matchingPolicy: . nextTime)
727
+ XCTAssertNil ( futureDay)
728
+
729
+ // Test subtraction over a year boundary
730
+ dayOfYearComps. dayOfYear = 1
731
+ let firstDay = cal. nextDate ( after: date, matching: dayOfYearComps, matchingPolicy: . nextTime, direction: . backward)
732
+ XCTAssertNotNil ( firstDay)
733
+ let firstDayComps = cal. dateComponents ( [ . year] , from: firstDay!)
734
+ let expectationComps = DateComponents ( year: 2022 )
735
+ XCTAssertEqual ( firstDayComps, expectationComps)
736
+
737
+ var subtractMe = DateComponents ( )
738
+ subtractMe. dayOfYear = - 1
739
+ let previousDay = cal. date ( byAdding: subtractMe, to: firstDay!)
740
+ XCTAssertNotNil ( previousDay)
741
+ let previousDayComps = cal. dateComponents ( [ . year, . dayOfYear] , from: previousDay!)
742
+ var previousDayExpectationComps = DateComponents ( )
743
+ previousDayExpectationComps. year = 2021
744
+ previousDayExpectationComps. dayOfYear = 365
745
+ XCTAssertEqual ( previousDayComps, previousDayExpectationComps)
746
+ }
747
+
669
748
@available ( FoundationPreview 0 . 4 , * )
670
749
func test_dayOfYear( ) {
671
750
// An arbitrary date, for which we know the answers
@@ -746,43 +825,14 @@ final class CalendarTests : XCTestCase {
746
825
XCTAssertEqual ( cal. compare ( date, to: beforeDate, toGranularity: . dayOfYear) , . orderedDescending)
747
826
XCTAssertEqual ( cal. compare ( date, to: afterDate, toGranularity: . dayOfYear) , . orderedAscending)
748
827
XCTAssertEqual ( cal. compare ( date + 10 , to: date, toGranularity: . dayOfYear) , . orderedSame)
749
- }
750
-
751
- @available ( FoundationPreview 0 . 4 , * )
752
- func test_dayOfYear_bounds( ) {
753
- let date = Date ( timeIntervalSinceReferenceDate: 682898558.712307 ) // 2022-08-22 22:02:38 UTC, day 234
754
- var cal = Calendar ( identifier: . gregorian)
755
- let tz = TimeZone . gmt
756
- cal. timeZone = tz
757
-
758
- // Test some invalid day of years
759
- var dayOfYearComps = DateComponents ( )
760
- dayOfYearComps. dayOfYear = 0
761
- let zeroDay = cal. nextDate ( after: date, matching: dayOfYearComps, matchingPolicy: . previousTimePreservingSmallerComponents)
762
- XCTAssertNil ( zeroDay)
763
-
764
- dayOfYearComps. dayOfYear = 400
765
- let futureDay = cal. nextDate ( after: date, matching: dayOfYearComps, matchingPolicy: . nextTime)
766
- XCTAssertNil ( futureDay)
767
-
768
- // Test subtraction over a year boundary
769
- dayOfYearComps. dayOfYear = 1
770
- let firstDay = cal. nextDate ( after: date, matching: dayOfYearComps, matchingPolicy: . nextTime, direction: . backward)
771
- XCTAssertNotNil ( firstDay)
772
- let firstDayComps = cal. dateComponents ( [ . year] , from: firstDay!)
773
- let expectationComps = DateComponents ( year: 2022 )
774
- XCTAssertEqual ( firstDayComps, expectationComps)
775
828
776
- var subtractMe = DateComponents ( )
777
- subtractMe. dayOfYear = - 1
778
- let previousDay = cal. date ( byAdding: subtractMe, to: firstDay!)
779
- XCTAssertNotNil ( previousDay)
780
- let previousDayComps = cal. dateComponents ( [ . year, . dayOfYear] , from: previousDay!)
781
- var previousDayExpectationComps = DateComponents ( )
782
- previousDayExpectationComps. year = 2021
783
- previousDayExpectationComps. dayOfYear = 365
784
- XCTAssertEqual ( previousDayComps, previousDayExpectationComps)
829
+ // Nonsense day-of-year
830
+ var nonsenseDayOfYear = DateComponents ( )
831
+ nonsenseDayOfYear. dayOfYear = 500
832
+ let shouldBeEmpty = Array ( cal. dates ( startingAt: beforeDate, matching: nonsenseDayOfYear) )
833
+ XCTAssertTrue ( shouldBeEmpty. isEmpty)
785
834
}
835
+
786
836
}
787
837
788
838
0 commit comments