/src/quantlib/fuzz-test-suite/fuzz_exchangeratemanager.cpp
Line | Count | Source |
1 | | #include <ql/currencies/exchangeratemanager.hpp> |
2 | | #include <ql/currencies/europe.hpp> |
3 | | #include <ql/currencies/america.hpp> |
4 | | #include <ql/currencies/asia.hpp> |
5 | | #include <ql/currencies/oceania.hpp> |
6 | | #include <ql/settings.hpp> |
7 | | #include <fuzzer/FuzzedDataProvider.h> |
8 | | |
9 | | using namespace QuantLib; |
10 | | |
11 | 877 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
12 | 877 | FuzzedDataProvider fdp(data, size); |
13 | 877 | SavedSettings saved_settings; |
14 | | |
15 | 877 | try { |
16 | 877 | ExchangeRateManager& erm = ExchangeRateManager::instance(); |
17 | | |
18 | 877 | if (fdp.ConsumeBool()) { |
19 | 250 | erm.clear(); |
20 | 250 | } |
21 | | |
22 | 877 | const Currency currencies[] = { |
23 | 877 | EURCurrency(), USDCurrency(), GBPCurrency(), JPYCurrency(), |
24 | 877 | CHFCurrency(), CADCurrency(), AUDCurrency(), HKDCurrency() |
25 | 877 | }; |
26 | | |
27 | 877 | size_t numRates = fdp.ConsumeIntegralInRange<size_t>(0, 10); |
28 | 4.10k | for (size_t i = 0; i < numRates; ++i) { |
29 | 3.22k | Currency c1 = fdp.PickValueInArray(currencies); |
30 | 3.22k | Currency c2 = fdp.PickValueInArray(currencies); |
31 | 3.22k | if (c1 == c2) continue; |
32 | | |
33 | 2.00k | Rate rate = fdp.ConsumeFloatingPointInRange<Rate>(0.01, 2.0); |
34 | 2.00k | ExchangeRate er(c1, c2, rate); |
35 | | |
36 | 2.00k | Date d1(fdp.ConsumeIntegralInRange<int>(1, 31), |
37 | 2.00k | static_cast<Month>(fdp.ConsumeIntegralInRange<int>(1, 12)), |
38 | 2.00k | fdp.ConsumeIntegralInRange<int>(1901, 2199)); |
39 | 2.00k | Date d2 = d1 + fdp.ConsumeIntegralInRange<int>(1, 365); |
40 | | |
41 | 2.00k | erm.add(er, d1, d2); |
42 | 2.00k | } |
43 | | |
44 | 877 | Currency s = fdp.PickValueInArray(currencies); |
45 | 877 | Currency t = fdp.PickValueInArray(currencies); |
46 | 877 | Date d(fdp.ConsumeIntegralInRange<int>(1, 31), |
47 | 877 | static_cast<Month>(fdp.ConsumeIntegralInRange<int>(1, 12)), |
48 | 877 | fdp.ConsumeIntegralInRange<int>(1901, 2199)); |
49 | 877 | ExchangeRate::Type type = fdp.PickValueInArray({ExchangeRate::Direct, ExchangeRate::Derived}); |
50 | | |
51 | 877 | try { |
52 | 877 | (void)erm.lookup(s, t, d, type); |
53 | 877 | } catch (...) {} |
54 | | |
55 | 877 | } catch (...) {} |
56 | 877 | return 0; |
57 | 877 | } |