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