| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2010 Manas Bhatt |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include "commodityunitofmeasure.hpp" |
| 21 | #include "utilities.hpp" |
| 22 | #include <ql/experimental/commodities/unitofmeasureconversionmanager.hpp> |
| 23 | #include <ql/experimental/commodities/petroleumunitsofmeasure.hpp> |
| 24 | |
| 25 | using namespace QuantLib; |
| 26 | using namespace boost::unit_test_framework; |
| 27 | |
| 28 | void CommodityUnitOfMeasureTest::testDirect() { |
| 29 | |
| 30 | BOOST_TEST_MESSAGE("Testing direct commodity unit of measure conversions..." ); |
| 31 | |
| 32 | UnitOfMeasureConversionManager& UOMManager = |
| 33 | UnitOfMeasureConversionManager::instance(); |
| 34 | |
| 35 | //MB to BBL |
| 36 | Quantity actual = |
| 37 | UnitOfMeasureConversion(NullCommodityType(), MBUnitOfMeasure(), |
| 38 | BarrelUnitOfMeasure(), 1000) |
| 39 | .convert(quantity: Quantity(NullCommodityType(), MBUnitOfMeasure(), 1000)); |
| 40 | Quantity calc = |
| 41 | UOMManager.lookup(commodityType: NullCommodityType(), BarrelUnitOfMeasure(), |
| 42 | MBUnitOfMeasure(), type: UnitOfMeasureConversion::Direct) |
| 43 | .convert(quantity: Quantity(NullCommodityType(), MBUnitOfMeasure(), 1000)); |
| 44 | |
| 45 | if (!close(calc,actual)) { |
| 46 | BOOST_FAIL("Wrong result for MB to BBL Conversion: \n" |
| 47 | << " actual: " << actual << "\n" |
| 48 | << " calculated: " << calc); |
| 49 | } |
| 50 | |
| 51 | //BBL to Gallon |
| 52 | actual = |
| 53 | UnitOfMeasureConversion(NullCommodityType(), BarrelUnitOfMeasure(), |
| 54 | GallonUnitOfMeasure(), 42) |
| 55 | .convert(quantity: Quantity(NullCommodityType(), GallonUnitOfMeasure(), 1000)); |
| 56 | calc = |
| 57 | UOMManager.lookup(commodityType: NullCommodityType(), BarrelUnitOfMeasure(), |
| 58 | GallonUnitOfMeasure(), |
| 59 | type: UnitOfMeasureConversion::Direct) |
| 60 | .convert(quantity: Quantity(NullCommodityType(), GallonUnitOfMeasure(), 1000)); |
| 61 | |
| 62 | if (!close(calc,actual)) { |
| 63 | BOOST_FAIL("Wrong result for BBL to Gallon Conversion: \n" |
| 64 | << " actual: " << actual << "\n" |
| 65 | << " calculated: " << calc); |
| 66 | } |
| 67 | |
| 68 | //BBL to Litre |
| 69 | actual = |
| 70 | UnitOfMeasureConversion(NullCommodityType(), BarrelUnitOfMeasure(), |
| 71 | LitreUnitOfMeasure(), 158.987) |
| 72 | .convert(quantity: Quantity(NullCommodityType(), LitreUnitOfMeasure(), 1000)); |
| 73 | calc = |
| 74 | UOMManager.lookup(commodityType: NullCommodityType(),BarrelUnitOfMeasure(), |
| 75 | LitreUnitOfMeasure(), |
| 76 | type: UnitOfMeasureConversion::Direct) |
| 77 | .convert(quantity: Quantity(NullCommodityType(), LitreUnitOfMeasure(), 1000)); |
| 78 | |
| 79 | if (!close(calc,actual)) { |
| 80 | BOOST_FAIL("Wrong result for BBL to Litre Conversion: \n" |
| 81 | << " actual: " << actual << "\n" |
| 82 | << " calculated: " << calc); |
| 83 | } |
| 84 | |
| 85 | //BBL to KL |
| 86 | actual = |
| 87 | UnitOfMeasureConversion(NullCommodityType(), KilolitreUnitOfMeasure(), |
| 88 | BarrelUnitOfMeasure(), 6.28981) |
| 89 | .convert(quantity: Quantity(NullCommodityType(),KilolitreUnitOfMeasure(),1000)); |
| 90 | calc = |
| 91 | UOMManager.lookup(commodityType: NullCommodityType(),BarrelUnitOfMeasure(), |
| 92 | KilolitreUnitOfMeasure(), |
| 93 | type: UnitOfMeasureConversion::Direct) |
| 94 | .convert(quantity: Quantity(NullCommodityType(),KilolitreUnitOfMeasure(),1000)); |
| 95 | |
| 96 | if (!close(calc,actual)) { |
| 97 | BOOST_FAIL("Wrong result for BBL to KiloLitre Conversion: \n" |
| 98 | << " actual: " << actual << "\n" |
| 99 | << " calculated: " << calc); |
| 100 | } |
| 101 | |
| 102 | //MB to Gallon |
| 103 | actual = |
| 104 | UnitOfMeasureConversion(NullCommodityType(), GallonUnitOfMeasure(), |
| 105 | MBUnitOfMeasure(), 42000) |
| 106 | .convert(quantity: Quantity(NullCommodityType(),MBUnitOfMeasure(),1000)); |
| 107 | calc = |
| 108 | UOMManager.lookup(commodityType: NullCommodityType(),GallonUnitOfMeasure(), |
| 109 | MBUnitOfMeasure(), type: UnitOfMeasureConversion::Direct) |
| 110 | .convert(quantity: Quantity(NullCommodityType(),MBUnitOfMeasure(),1000)); |
| 111 | |
| 112 | if (!close(calc,actual)) { |
| 113 | BOOST_FAIL("Wrong result for MB to Gallon Conversion: \n" |
| 114 | << " actual: " << actual << "\n" |
| 115 | << " calculated: " << calc); |
| 116 | } |
| 117 | |
| 118 | //Gallon to Litre |
| 119 | actual = |
| 120 | UnitOfMeasureConversion(NullCommodityType(), LitreUnitOfMeasure(), |
| 121 | GallonUnitOfMeasure(), 3.78541) |
| 122 | .convert(quantity: Quantity(NullCommodityType(),LitreUnitOfMeasure(),1000)); |
| 123 | calc = |
| 124 | UOMManager.lookup(commodityType: NullCommodityType(),GallonUnitOfMeasure(), |
| 125 | LitreUnitOfMeasure(), |
| 126 | type: UnitOfMeasureConversion::Direct) |
| 127 | .convert(quantity: Quantity(NullCommodityType(),LitreUnitOfMeasure(),1000)); |
| 128 | |
| 129 | if (!close(calc,actual)) { |
| 130 | BOOST_FAIL("Wrong result for Gallon to Litre Conversion: \n" |
| 131 | << " actual: " << actual << "\n" |
| 132 | << " calculated: " << calc); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | test_suite* CommodityUnitOfMeasureTest::suite() { |
| 137 | auto* suite = BOOST_TEST_SUITE("Commodity Unit Of Measure tests" ); |
| 138 | suite->add(QUANTLIB_TEST_CASE(&CommodityUnitOfMeasureTest::testDirect)); |
| 139 | return suite; |
| 140 | } |
| 141 | |
| 142 | |