|
1 | 1 | {-# LANGUAGE ScopedTypeVariables #-} |
2 | 2 |
|
3 | 3 | module UT.UtilTest(daycountTests1,daycountTests2,daycountTests3,daycountTests4 |
4 | | - ,tsTest,ts2Test,ts3Test,dateVectorPatternTest,paddingTest,dateSliceTest |
| 4 | + ,tsTest,ts2Test,ts3Test,curveLookupLegacyTest,dateVectorPatternTest,paddingTest,dateSliceTest |
5 | 5 | ,capTest,roundingTest,sliceTest,splitTsTest,tableTest,lastOftest,paySeqTest |
6 | 6 | ,scaleListTest,mapUpdateTest,boundedParMapTest,resolveWorkerCapTest,forceMapValuesTest |
7 | 7 | ,workerNumTest,serverConfigLimitTest,badRequestJSONTest |
@@ -341,6 +341,121 @@ ts2Test = |
341 | 341 | getValByDate testThresholdCurve Inc (toDate "20220310") |
342 | 342 | ] |
343 | 343 |
|
| 344 | +-- | Legacy reference for the plural lookup. The fast path must match this |
| 345 | +-- exactly, including for descending/unsorted dates and unsorted curve points. |
| 346 | +legacyGetValByDates :: Ts -> CutoffType -> [Date] -> [Rational] |
| 347 | +legacyGetValByDates rc ct = map (getValByDate rc ct) |
| 348 | + |
| 349 | +assertCurveLookup :: String -> Ts -> CutoffType -> [[Date]] -> IO () |
| 350 | +assertCurveLookup label curve ct dss = |
| 351 | + mapM_ (\ds -> assertEqual (label ++ " / " ++ show ct ++ " / n=" ++ show (length ds)) |
| 352 | + (legacyGetValByDates curve ct ds) |
| 353 | + (getValByDates curve ct ds)) dss |
| 354 | + |
| 355 | +curveLookupLegacyTest :: TestTree |
| 356 | +curveLookupLegacyTest = |
| 357 | + let |
| 358 | + d0 = toDate "20220101" |
| 359 | + d1 = T.addDays 30 d0 |
| 360 | + d2 = T.addDays 60 d0 |
| 361 | + d3 = T.addDays 90 d0 |
| 362 | + dBefore = T.addDays (-10) d0 |
| 363 | + dAfter = T.addDays 200 d0 |
| 364 | + |
| 365 | + ratePts :: [TsPoint Rational] |
| 366 | + ratePts = [ TsPoint d0 (1 % 100) |
| 367 | + , TsPoint d1 (2 % 100) |
| 368 | + , TsPoint d1 (25 % 1000) |
| 369 | + , TsPoint d2 (3 % 100) |
| 370 | + , TsPoint d3 (4 % 100) ] |
| 371 | + |
| 372 | + iratePts :: [TsPoint IRate] |
| 373 | + iratePts = [ TsPoint d0 0.01 |
| 374 | + , TsPoint d1 0.02 |
| 375 | + , TsPoint d1 0.025 |
| 376 | + , TsPoint d2 0.03 |
| 377 | + , TsPoint d3 0.04 ] |
| 378 | + |
| 379 | + balPts :: [TsPoint Balance] |
| 380 | + balPts = [ TsPoint d0 100 |
| 381 | + , TsPoint d1 200 |
| 382 | + , TsPoint d2 300 |
| 383 | + , TsPoint d3 400 ] |
| 384 | + |
| 385 | + unsortedRatePts :: [TsPoint Rational] |
| 386 | + unsortedRatePts = [TsPoint d2 (3 % 100), TsPoint d0 (1 % 100), TsPoint d1 (2 % 100)] |
| 387 | + |
| 388 | + unsortedIratePts :: [TsPoint IRate] |
| 389 | + unsortedIratePts = [TsPoint d2 0.03, TsPoint d0 0.01, TsPoint d1 0.02] |
| 390 | + |
| 391 | + dateSets :: [[Date]] |
| 392 | + dateSets = [ [] |
| 393 | + , [d0] |
| 394 | + , [dBefore] |
| 395 | + , [dAfter] |
| 396 | + , [d0, d1, d2, d3] |
| 397 | + , [d0, d0, d1, d1, d2, d3, d3] |
| 398 | + , [d1] |
| 399 | + , [d1, d2] |
| 400 | + , [d3, d2, d1, d0] |
| 401 | + , [d1, d0, d2, d3] |
| 402 | + , [dBefore, d0, d1, dAfter] |
| 403 | + , [T.addDays (toInteger i) d0 | i <- [0..999]] |
| 404 | + ] |
| 405 | + |
| 406 | + simpleCurves :: [(String, Ts)] |
| 407 | + simpleCurves = |
| 408 | + [ ("IRateCurve", IRateCurve iratePts) |
| 409 | + , ("FloatCurve", FloatCurve ratePts) |
| 410 | + , ("RatioCurve", RatioCurve ratePts) |
| 411 | + , ("BalanceCurve", BalanceCurve balPts) |
| 412 | + , ("LeftBalanceCurve", LeftBalanceCurve balPts) |
| 413 | + ] |
| 414 | + |
| 415 | + emptyCurves :: [Ts] |
| 416 | + emptyCurves = |
| 417 | + [ IRateCurve [] |
| 418 | + , FloatCurve [] |
| 419 | + , RatioCurve [] |
| 420 | + , BalanceCurve [] |
| 421 | + , LeftBalanceCurve [] |
| 422 | + , ThresholdCurve [] |
| 423 | + ] |
| 424 | + in |
| 425 | + testGroup "getValByDates legacy compatibility" |
| 426 | + [ testCase "ascending, duplicate, descending and unsorted dates" $ |
| 427 | + mapM_ (\(name, c) -> do |
| 428 | + assertCurveLookup name c Inc dateSets |
| 429 | + assertCurveLookup name c Exc dateSets) simpleCurves |
| 430 | + , testCase "unsorted curve points fall back exactly" $ |
| 431 | + mapM_ (\(name, c) -> do |
| 432 | + assertCurveLookup name c Inc dateSets |
| 433 | + assertCurveLookup name c Exc dateSets) |
| 434 | + [ ("IRateCurve-unsorted", IRateCurve unsortedIratePts) |
| 435 | + , ("FloatCurve-unsorted", FloatCurve unsortedRatePts) |
| 436 | + ] |
| 437 | + , testCase "FactorCurveClosed matches the legacy lookup" $ do |
| 438 | + assertCurveLookup "factor" (FactorCurveClosed ratePts d2) Exc dateSets |
| 439 | + assertCurveLookup "factor-unsorted" (FactorCurveClosed unsortedRatePts d2) Exc dateSets |
| 440 | + , testCase "ThresholdCurve falls back exactly" $ do |
| 441 | + assertCurveLookup "threshold" (ThresholdCurve ratePts) Inc dateSets |
| 442 | + assertCurveLookup "threshold" (ThresholdCurve ratePts) Exc dateSets |
| 443 | + , testCase "PricingCurve falls back exactly" $ do |
| 444 | + assertCurveLookup "pricing" (PricingCurve ratePts) Inc dateSets |
| 445 | + assertCurveLookup "pricing" (PricingCurve ratePts) Exc dateSets |
| 446 | + , testCase "empty curves" $ do |
| 447 | + mapM_ (\c -> do |
| 448 | + assertCurveLookup "empty" c Inc dateSets |
| 449 | + assertCurveLookup "empty" c Exc dateSets) emptyCurves |
| 450 | + assertCurveLookup "empty-factor" (FactorCurveClosed [] d2) Exc dateSets |
| 451 | + assertCurveLookup "empty-pricing" (PricingCurve []) Inc dateSets |
| 452 | + , testCase "empty dates always return []" $ do |
| 453 | + mapM_ (\(name, c) -> do |
| 454 | + assertEqual (name ++ " Inc") [] (getValByDates c Inc []) |
| 455 | + assertEqual (name ++ " Exc") [] (getValByDates c Exc [])) simpleCurves |
| 456 | + assertEqual "factor" [] (getValByDates (FactorCurveClosed ratePts d2) Exc []) |
| 457 | + ] |
| 458 | + |
344 | 459 | -- ^ date pattern test |
345 | 460 | dateVectorPatternTest = |
346 | 461 | let |
|
0 commit comments