Skip to content

Commit

Permalink
add setDefaults test
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Oct 9, 2023
1 parent ecef000 commit e0fdc8e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions DXFeedFramework/Native/Schedule/NativeSchedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class NativeSchedule {
return try createDay(thread, day)
}

public func getDayByYearMonthDay(yearMonthDay: Int32) throws -> ScheduleDay {
let thread = currentThread()
let day = try ErrorCheck.nativeCall(thread, dxfg_Schedule_getDayByYearMonthDay(thread, schedule, yearMonthDay))
return try createDay(thread, day)
}

private func createDay(_ thread: OpaquePointer?,
_ day: UnsafeMutablePointer<dxfg_day_t>) throws -> ScheduleDay {
let scheduleDay = ScheduleDay()
Expand Down
17 changes: 17 additions & 0 deletions DXFeedFramework/Schedule/DXSchedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ public class DXSchedule {
return day
}

/// Returns day for specified year, month and day numbers.
///
/// Year, month, and day numbers shall be decimally packed in the following way:
/// YearMonthDay = year * 10000 + month * 100 + day
/// For example, September 28, 1977 has value 19770928.
/// If specified day does not exist then this method returns day with
/// the lowest valid YearMonthDay that is greater than specified one.
/// This method will throw {@link IllegalArgumentException} if specified year, month and day numbers
/// fall outside of valid date range from 0001-01-02 to 9999-12-30.
/// - Parameters:
/// - yearMonthDay: year, month and day numbers to search for
/// - Throws: GraalException. Rethrows exception from Java.recore
public func getDayByYearMonthDay(yearMonthDay: Int32) throws -> ScheduleDay {
let day = try native.getDayByYearMonthDay(yearMonthDay: yearMonthDay)
return day
}

/// Returns session that contains specified time.
///
/// This method will throw exception
Expand Down
27 changes: 27 additions & 0 deletions DXFeedFrameworkTests/ScheduleTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,31 @@ final class ScheduleTest: XCTestCase {
}
}

func testSetDefaults() throws {
let goodHoliday = 20170111
let badHoliday = 20170118
let worstHoliday = 20170125
let def = "date=30000101-000000+0000\n\nhd.GOOD=\\\n" + "\(goodHoliday)" + ",\\\n\n"
// test that changing defaults works by adding new holidays list
try DXSchedule.setDefaults(def.data(using: .utf8)!)
let goodSchedule = try DXSchedule(scheduleDefinition: "(tz=GMT;0=;hd=GOOD)")
try checkHoliday(goodSchedule, goodHoliday)
// test that changing defaults works again by adding yet another holidays list
try DXSchedule.setDefaults((def + "hd.BAD=\\\n" + "\(badHoliday)" + ",\\").data(using: .utf8)!)
let badSchedule = try DXSchedule(scheduleDefinition: "(tz=GMT;0=;hd=BAD)")
try checkHoliday(goodSchedule, goodHoliday)
try checkHoliday(badSchedule, badHoliday)
// test that replacing holidays list with new value works and affects old schedule instance
try DXSchedule.setDefaults((def + "hd.BAD=\\\n" + "\(worstHoliday)" + ",\\").data(using: .utf8)!)
try checkHoliday(goodSchedule, goodHoliday)
XCTAssertFalse(try badSchedule.getDayByYearMonthDay(yearMonthDay: Int32(badHoliday)).holiday == 1)
try checkHoliday(badSchedule, worstHoliday)
}

private func checkHoliday(_ schedule: DXSchedule, _ holiday: Int) throws {
for index in holiday-1...holiday+1 {
let day = try schedule.getDayByYearMonthDay(yearMonthDay: Int32(index))
XCTAssert((index == holiday) == (day.holiday == 1))
}
}
}

0 comments on commit e0fdc8e

Please sign in to comment.