Skip to content

Commit ca953b7

Browse files
authored
Avoid test-setup antipatterns no longer needed (#2777)
2 parents e06326c + 21092c5 commit ca953b7

6 files changed

Lines changed: 61 additions & 115 deletions

File tree

test-suite/inflation.cpp

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ BOOST_AUTO_TEST_CASE(testZeroTermStructure) {
343343
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
344344
207.3};
345345

346-
RelinkableHandle<ZeroInflationTermStructure> hz;
347-
auto ii = ext::make_shared<UKRPI>(hz);
346+
auto ii = ext::make_shared<UKRPI>();
348347
for (Size i=0; i<std::size(fixData); i++) {
349348
ii->addFixing(rpiSchedule[i], fixData[i]);
350349
}
@@ -390,7 +389,6 @@ BOOST_AUTO_TEST_CASE(testZeroTermStructure) {
390389
ext::shared_ptr<PiecewiseZeroInflationCurve<Linear> > pZITS =
391390
ext::make_shared<PiecewiseZeroInflationCurve<Linear>>(
392391
evaluationDate, baseDate, frequency, dc, helpers);
393-
hz.linkTo(pZITS);
394392

395393
//===========================================================================================
396394
// first check that the quoted swaps are repriced correctly
@@ -399,6 +397,9 @@ BOOST_AUTO_TEST_CASE(testZeroTermStructure) {
399397
const Spread basisPoint = 1.0e-4;
400398
auto engine = ext::make_shared<DiscountingSwapEngine>(nominalTS);
401399

400+
Handle<ZeroInflationTermStructure> hz(pZITS);
401+
ii = ext::make_shared<UKRPI>(hz);
402+
402403
for (const auto& datum: zcData) {
403404
ZeroCouponInflationSwap nzcis(Swap::Payer,
404405
1000000.0,
@@ -505,9 +506,6 @@ BOOST_AUTO_TEST_CASE(testZeroTermStructure) {
505506
<< "\n maturity: " << nzcis.maturityDate()
506507
<< "\n rate: " << datum.rate);
507508
}
508-
509-
// remove circular refernce
510-
hz.reset();
511509
}
512510

513511
BOOST_AUTO_TEST_CASE(testZeroTermStructureLazyBaseDate) {
@@ -618,8 +616,7 @@ BOOST_AUTO_TEST_CASE(testSeasonalityCorrection) {
618616
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
619617
207.3};
620618

621-
RelinkableHandle<ZeroInflationTermStructure> hz;
622-
auto ii = ext::make_shared<UKRPI>(hz);
619+
auto ii = ext::make_shared<UKRPI>();
623620
for (Size i=0; i<std::size(fixData); i++) {
624621
ii->addFixing(rpiSchedule[i], fixData[i]);
625622
}
@@ -667,9 +664,10 @@ BOOST_AUTO_TEST_CASE(testSeasonalityCorrection) {
667664

668665
auto zeroCurve = ext::make_shared<InterpolatedZeroInflationCurve<Linear>>(
669666
evaluationDate, nodes, rates, frequency, dc);
670-
hz.linkTo(zeroCurve);
671667

672-
// Perform checks on the seasonality for this non-interpolated index
668+
Handle<ZeroInflationTermStructure> hz(zeroCurve);
669+
ii = ext::make_shared<UKRPI>(hz);
670+
673671
checkSeasonality(hz, ii);
674672
}
675673

@@ -961,9 +959,8 @@ BOOST_AUTO_TEST_CASE(testYYTermStructure) {
961959
207.3
962960
};
963961

964-
RelinkableHandle<YoYInflationTermStructure> hy;
965962
auto rpi = ext::make_shared<UKRPI>();
966-
auto iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
963+
auto iir = ext::make_shared<YoYInflationIndex>(rpi);
967964
for (Size i=0; i<std::size(fixData); i++) {
968965
rpi->addFixing(rpiSchedule[i], fixData[i]);
969966
}
@@ -1022,7 +1019,8 @@ BOOST_AUTO_TEST_CASE(testYYTermStructure) {
10221019
ext::shared_ptr<PricingEngine> sppe(new DiscountingSwapEngine(hTS));
10231020

10241021
// make sure that the index has the latest yoy term structure
1025-
hy.linkTo(pYYTS);
1022+
Handle<YoYInflationTermStructure> hy(pYYTS);
1023+
iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
10261024

10271025
for (Size j = 1; j < yyData.size(); j++) {
10281026

@@ -1093,8 +1091,6 @@ BOOST_AUTO_TEST_CASE(testYYTermStructure) {
10931091
<<", legs "<< yyS3.legNPV(0) << " and " << yyS3.legNPV(1)
10941092
);
10951093
}
1096-
// remove circular refernce
1097-
hy.reset();
10981094
}
10991095

11001096
BOOST_AUTO_TEST_CASE(testZeroBpsYoYInflationSwapFairRateAndSpread) {
@@ -1120,9 +1116,8 @@ BOOST_AUTO_TEST_CASE(testZeroBpsYoYInflationSwapFairRateAndSpread) {
11201116
199.2, 200.1, 200.4, 201.1, 202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
11211117
207.3};
11221118

1123-
RelinkableHandle<YoYInflationTermStructure> hy;
11241119
auto rpi = ext::make_shared<UKRPI>();
1125-
auto iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
1120+
auto iir = ext::make_shared<YoYInflationIndex>(rpi);
11261121
for (Size i = 0; i < std::size(fixData); i++) {
11271122
rpi->addFixing(rpiSchedule[i], fixData[i]);
11281123
}
@@ -1140,7 +1135,9 @@ BOOST_AUTO_TEST_CASE(testZeroBpsYoYInflationSwapFairRateAndSpread) {
11401135
auto yoyTs = ext::make_shared<InterpolatedYoYInflationCurve<Linear>>(
11411136
evaluationDate, yoyDates, yoyRates, iir->frequency(), dc);
11421137
yoyTs->enableExtrapolation();
1143-
hy.linkTo(yoyTs);
1138+
1139+
Handle<YoYInflationTermStructure> hy(yoyTs);
1140+
iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
11441141

11451142
Schedule yoySchedule =
11461143
MakeSchedule().from(nominalTS->referenceDate())
@@ -1166,8 +1163,6 @@ BOOST_AUTO_TEST_CASE(testZeroBpsYoYInflationSwapFairRateAndSpread) {
11661163
BOOST_CHECK_EXCEPTION(
11671164
swap.fairSpread(), Error,
11681165
ExpectedErrorMessage("result not available"));
1169-
1170-
hy.reset();
11711166
}
11721167

11731168
BOOST_AUTO_TEST_CASE(testExpiredYoYInflationSwapFairRateAndSpread) {
@@ -1193,9 +1188,8 @@ BOOST_AUTO_TEST_CASE(testExpiredYoYInflationSwapFairRateAndSpread) {
11931188
199.2, 200.1, 200.4, 201.1, 202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
11941189
207.3};
11951190

1196-
RelinkableHandle<YoYInflationTermStructure> hy;
11971191
auto rpi = ext::make_shared<UKRPI>();
1198-
auto iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
1192+
auto iir = ext::make_shared<YoYInflationIndex>(rpi);
11991193
for (Size i = 0; i < std::size(fixData); i++) {
12001194
rpi->addFixing(rpiSchedule[i], fixData[i]);
12011195
}
@@ -1214,7 +1208,9 @@ BOOST_AUTO_TEST_CASE(testExpiredYoYInflationSwapFairRateAndSpread) {
12141208
auto yoyTs = ext::make_shared<InterpolatedYoYInflationCurve<Linear>>(
12151209
evaluationDate, yoyDates, yoyRates, iir->frequency(), dc);
12161210
yoyTs->enableExtrapolation();
1217-
hy.linkTo(yoyTs);
1211+
1212+
Handle<YoYInflationTermStructure> hy(yoyTs);
1213+
iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
12181214

12191215
Schedule yoySchedule =
12201216
MakeSchedule().from(nominalTS->referenceDate())
@@ -1239,8 +1235,6 @@ BOOST_AUTO_TEST_CASE(testExpiredYoYInflationSwapFairRateAndSpread) {
12391235
BOOST_CHECK_EXCEPTION(
12401236
swap.fairSpread(), Error,
12411237
ExpectedErrorMessage("result not available"));
1242-
1243-
hy.reset();
12441238
}
12451239

12461240
BOOST_AUTO_TEST_CASE(testPeriod) {
@@ -1748,8 +1742,7 @@ BOOST_AUTO_TEST_CASE(testUsCpiLinearBootstrapAtMonthStart) {
17481742

17491743
Settings::instance().evaluationDate() = evalDate;
17501744

1751-
RelinkableHandle<ZeroInflationTermStructure> hz;
1752-
auto index = ext::make_shared<USCPI>(hz);
1745+
auto index = ext::make_shared<USCPI>();
17531746

17541747
for (auto& [d, v] : fixings)
17551748
index->addFixing(d, v);
@@ -1767,7 +1760,6 @@ BOOST_AUTO_TEST_CASE(testUsCpiLinearBootstrapAtMonthStart) {
17671760
try {
17681761
auto curve = ext::make_shared<PiecewiseZeroInflationCurve<Linear>>(
17691762
evalDate, baseDate, Monthly, dc, helpers);
1770-
hz.linkTo(curve);
17711763
curve->zeroRate(evalDate + 1*Years);
17721764
} catch (const std::exception&) {
17731765
failureCount++;
@@ -1826,8 +1818,7 @@ BOOST_AUTO_TEST_CASE(testEuHicpFlatBootstrapAtMonthStart) {
18261818

18271819
Settings::instance().evaluationDate() = evalDate;
18281820

1829-
RelinkableHandle<ZeroInflationTermStructure> hz;
1830-
auto index = ext::make_shared<EUHICPXT>(hz);
1821+
auto index = ext::make_shared<EUHICPXT>();
18311822

18321823
for (auto& [d, v] : fixings)
18331824
index->addFixing(d, v);
@@ -1846,20 +1837,16 @@ BOOST_AUTO_TEST_CASE(testEuHicpFlatBootstrapAtMonthStart) {
18461837
try {
18471838
auto curve = ext::make_shared<PiecewiseZeroInflationCurve<Linear>>(
18481839
evalDate, baseDate, Monthly, dc, helpers);
1849-
hz.linkTo(curve);
18501840
curve->zeroRate(evalDate + 1*Years);
18511841
} catch (const std::exception&) {
18521842
failureCount++;
18531843
}
18541844

1855-
hz.linkTo(ext::shared_ptr<ZeroInflationTermStructure>());
1856-
18571845
// GlobalBootstrap
18581846
try {
18591847
auto curve = ext::make_shared<
18601848
PiecewiseZeroInflationCurve<Linear, GlobalBootstrap>>(
18611849
evalDate, baseDate, Monthly, dc, helpers);
1862-
hz.linkTo(curve);
18631850
curve->zeroRate(evalDate + 1*Years);
18641851
} catch (const std::exception&) {
18651852
globalFailureCount++;
@@ -1919,8 +1906,7 @@ BOOST_AUTO_TEST_CASE(testUkRpiFlatBootstrapAtMonthStart) {
19191906

19201907
Settings::instance().evaluationDate() = evalDate;
19211908

1922-
RelinkableHandle<ZeroInflationTermStructure> hz;
1923-
auto index = ext::make_shared<UKRPI>(hz);
1909+
auto index = ext::make_shared<UKRPI>();
19241910

19251911
for (auto& [d, v] : fixings)
19261912
index->addFixing(d, v);
@@ -1940,20 +1926,16 @@ BOOST_AUTO_TEST_CASE(testUkRpiFlatBootstrapAtMonthStart) {
19401926
try {
19411927
auto curve = ext::make_shared<PiecewiseZeroInflationCurve<Linear>>(
19421928
evalDate, baseDate, Monthly, dc, helpers);
1943-
hz.linkTo(curve);
19441929
curve->zeroRate(evalDate + 1*Years);
19451930
} catch (const std::exception&) {
19461931
failureCount++;
19471932
}
19481933

1949-
hz.linkTo(ext::shared_ptr<ZeroInflationTermStructure>());
1950-
19511934
// GlobalBootstrap
19521935
try {
19531936
auto curve = ext::make_shared<
19541937
PiecewiseZeroInflationCurve<Linear, GlobalBootstrap>>(
19551938
evalDate, baseDate, Monthly, dc, helpers);
1956-
hz.linkTo(curve);
19571939
curve->zeroRate(evalDate + 1*Years);
19581940
} catch (const std::exception&) {
19591941
globalFailureCount++;
@@ -2012,8 +1994,7 @@ BOOST_AUTO_TEST_CASE(testUsCpiLinearGlobalBootstrapAtMonthStart) {
20121994

20131995
Settings::instance().evaluationDate() = evalDate;
20141996

2015-
RelinkableHandle<ZeroInflationTermStructure> hz;
2016-
auto index = ext::make_shared<USCPI>(hz);
1997+
auto index = ext::make_shared<USCPI>();
20171998

20181999
for (auto& [d, v] : fixings)
20192000
index->addFixing(d, v);
@@ -2032,7 +2013,6 @@ BOOST_AUTO_TEST_CASE(testUsCpiLinearGlobalBootstrapAtMonthStart) {
20322013
auto curve = ext::make_shared<
20332014
PiecewiseZeroInflationCurve<Linear, GlobalBootstrap>>(
20342015
evalDate, baseDate, Monthly, dc, helpers);
2035-
hz.linkTo(curve);
20362016
curve->zeroRate(evalDate + 1*Years);
20372017
} catch (const std::exception&) {
20382018
failureCount++;
@@ -2103,8 +2083,7 @@ BOOST_AUTO_TEST_CASE(testPillarCollisionWithDifferentMonthLengths) {
21032083

21042084
Settings::instance().evaluationDate() = evalDate;
21052085

2106-
RelinkableHandle<ZeroInflationTermStructure> hz;
2107-
auto index = ext::make_shared<USCPI>(hz);
2086+
auto index = ext::make_shared<USCPI>();
21082087

21092088
for (auto& [d, v] : fixings)
21102089
index->addFixing(d, v);
@@ -2122,7 +2101,6 @@ BOOST_AUTO_TEST_CASE(testPillarCollisionWithDifferentMonthLengths) {
21222101
try {
21232102
auto curve = ext::make_shared<PiecewiseZeroInflationCurve<Linear>>(
21242103
evalDate, baseDate, Monthly, dc, helpers);
2125-
hz.linkTo(curve);
21262104
curve->zeroRate(evalDate + 1*Years);
21272105
} catch (const std::exception&) {
21282106
failureCount++;

test-suite/inflationcapfloor.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ struct CommonVars {
101101
DayCounter dc;
102102
ext::shared_ptr<YoYInflationIndex> iir;
103103

104-
RelinkableHandle<YieldTermStructure> nominalTS;
104+
Handle<YieldTermStructure> nominalTS;
105105
ext::shared_ptr<YoYInflationTermStructure> yoyTS;
106-
RelinkableHandle<YoYInflationTermStructure> hy;
106+
Handle<YoYInflationTermStructure> hy;
107107

108108
// setup
109109
CommonVars()
@@ -139,12 +139,11 @@ struct CommonVars {
139139
for (Size i=0; i<rpiSchedule.size();i++) {
140140
rpi->addFixing(rpiSchedule[i], fixData[i]);
141141
}
142-
// link from yoy index to yoy TS
143-
iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
142+
iir = ext::make_shared<YoYInflationIndex>(rpi);
144143

145144
ext::shared_ptr<YieldTermStructure> nominalFF(
146145
new FlatForward(evaluationDate, 0.05, ActualActual(ActualActual::ISDA)));
147-
nominalTS.linkTo(nominalFF);
146+
nominalTS = Handle<YieldTermStructure>(nominalFF);
148147

149148
// now build the YoY inflation curve
150149
Period observationLag = Period(2,Months);
@@ -173,7 +172,7 @@ struct CommonVars {
173172
CPI::Flat,
174173
observationLag,
175174
calendar, convention, dc,
176-
Handle<YieldTermStructure>(nominalTS));
175+
nominalTS);
177176

178177
Date baseDate = rpi->lastFixingDate();
179178
Rate baseYYRate = yyData[0].rate/100.0;
@@ -183,7 +182,8 @@ struct CommonVars {
183182
yoyTS = ext::dynamic_pointer_cast<YoYInflationTermStructure>(pYYTS);
184183

185184
// make sure that the index has the latest yoy term structure
186-
hy.linkTo(pYYTS);
185+
hy = Handle<YoYInflationTermStructure>(pYYTS);
186+
iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
187187
}
188188

189189
// utilities
@@ -372,8 +372,6 @@ BOOST_AUTO_TEST_CASE(testConsistency) {
372372
}
373373
}
374374
} // pricer loop
375-
// remove circular refernce
376-
vars.hy.reset();
377375
}
378376

379377

@@ -427,8 +425,7 @@ BOOST_AUTO_TEST_CASE(testParity) {
427425
0.0, // spread on index
428426
vars.dc, UnitedKingdom());
429427

430-
Handle<YieldTermStructure> hTS(vars.nominalTS);
431-
ext::shared_ptr<PricingEngine> sppe(new DiscountingSwapEngine(hTS));
428+
ext::shared_ptr<PricingEngine> sppe(new DiscountingSwapEngine(vars.nominalTS));
432429
swap.setPricingEngine(sppe);
433430

434431
// N.B. nominals are 10e6
@@ -445,8 +442,6 @@ BOOST_AUTO_TEST_CASE(testParity) {
445442
}
446443
}
447444
}
448-
// remove circular refernce
449-
vars.hy.reset();
450445
}
451446

452447
BOOST_AUTO_TEST_CASE(testVolatilityQuoteObservability) {
@@ -487,9 +482,6 @@ BOOST_AUTO_TEST_CASE(testVolatilityQuoteObservability) {
487482
BOOST_CHECK_MESSAGE(relativeError(moved, expected, expected) < 1.0e-10,
488483
"yoy cap priced off a quote of 0.02 gives " << moved
489484
<< ", off a value of 0.02 gives " << expected);
490-
491-
// remove circular refernce
492-
vars.hy.reset();
493485
}
494486

495487
BOOST_AUTO_TEST_CASE(testCachedValue) {
@@ -559,9 +551,6 @@ BOOST_AUTO_TEST_CASE(testCachedValue) {
559551
BOOST_CHECK_MESSAGE(fabs(floor->NPV()-cachedFloorNPVbac)<0.22,"yoy floor cached NPV wrong "
560552
<<floor->NPV()<<" should be "<<cachedFloorNPVbac<<" bac Black pricer"
561553
<<" diff was "<<(fabs(floor->NPV()-cachedFloorNPVbac)));
562-
563-
// remove circular refernce
564-
vars.hy.reset();
565554
}
566555

567556
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)