summaryrefslogtreecommitdiff
path: root/libs/units/test/test_information_units.cpp
blob: 6bb1ab59607d3e95066f89639e5925034f3dbcd8 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Boost.Units - A C++ library for zero-overhead dimensional analysis and 
// unit/quantity manipulation and conversion
//
// Copyright (C) 2014 Erik Erlandson
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <iostream>
#include <sstream>

#include <boost/units/quantity.hpp>
#include <boost/units/conversion.hpp>
#include <boost/units/io.hpp>

#include <boost/units/systems/si/prefixes.hpp>
#include <boost/units/systems/si/time.hpp>

// All information systems definitions
#include <boost/units/systems/information.hpp>

using std::cout;
using std::cerr;
using std::endl;
using std::stringstream;

namespace bu = boost::units;
namespace si = boost::units::si;

using bu::quantity;

using bu::information::bit_base_unit;
using bu::information::byte_base_unit;
using bu::information::nat_base_unit;
using bu::information::hartley_base_unit;
using bu::information::shannon_base_unit;


#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>


#include <boost/multiprecision/cpp_int.hpp>

const double close_fraction = 0.0000001;

// checks that cf(u2,u1) == expected
// also checks invariant property that cf(u2,u1) * cf(u1,u2) == 1 
#define CHECK_DIRECT_CF(u1, u2, expected) \
    BOOST_CHECK_CLOSE_FRACTION(bu::conversion_factor((u2), (u1)), (expected), close_fraction); \
    BOOST_CHECK_CLOSE_FRACTION(bu::conversion_factor((u2), (u1)) * bu::conversion_factor((u1), (u2)), 1.0, close_fraction);

// check transitive conversion factors
// invariant:  cf(u1,u3) = cf(u1,u2)*cf(u2,u3) 
#define CHECK_TRANSITIVE_CF(u1, u2, u3) { \
    double cf12 = bu::conversion_factor((u2), (u1)) ; \
    double cf23 = bu::conversion_factor((u3), (u2)) ; \
    double cf13 = bu::conversion_factor((u3), (u1)) ; \
    BOOST_CHECK_CLOSE_FRACTION(cf13, cf12*cf23, close_fraction); \
    double cf32 = bu::conversion_factor((u2), (u3)) ; \
    double cf21 = bu::conversion_factor((u1), (u2)) ; \
    double cf31 = bu::conversion_factor((u1), (u3)) ; \
    BOOST_CHECK_CLOSE_FRACTION(cf31, cf32*cf21, close_fraction); \
}


BOOST_AUTO_TEST_CASE(test_cf_bit_byte) {
    CHECK_DIRECT_CF(bit_base_unit::unit_type(), byte_base_unit::unit_type(), 8.0);
}

BOOST_AUTO_TEST_CASE(test_cf_bit_nat) {
    CHECK_DIRECT_CF(bit_base_unit::unit_type(), nat_base_unit::unit_type(), 1.442695040888964);
}

BOOST_AUTO_TEST_CASE(test_cf_bit_hartley) {
    CHECK_DIRECT_CF(bit_base_unit::unit_type(), hartley_base_unit::unit_type(), 3.321928094887363);
}

BOOST_AUTO_TEST_CASE(test_cf_bit_shannon) {
    CHECK_DIRECT_CF(bit_base_unit::unit_type(), shannon_base_unit::unit_type(), 1.0);
}

/////////////////////////////////////////////////////////////////////////////////////
// spot-check that these are automatically transitive, thru central "hub unit" bit:
// basic pattern is to test invariant property:  cf(c,a) = cf(c,b)*cf(b,a)

BOOST_AUTO_TEST_CASE(test_transitive_byte_nat) {
    CHECK_TRANSITIVE_CF(byte_base_unit::unit_type(), bit_base_unit::unit_type(), nat_base_unit::unit_type());
}
BOOST_AUTO_TEST_CASE(test_transitive_nat_hartley) {
    CHECK_TRANSITIVE_CF(nat_base_unit::unit_type(), bit_base_unit::unit_type(), hartley_base_unit::unit_type());
}
BOOST_AUTO_TEST_CASE(test_transitive_hartley_shannon) {
    CHECK_TRANSITIVE_CF(hartley_base_unit::unit_type(), bit_base_unit::unit_type(), shannon_base_unit::unit_type());
}
BOOST_AUTO_TEST_CASE(test_transitive_shannon_byte) {
    CHECK_TRANSITIVE_CF(shannon_base_unit::unit_type(), bit_base_unit::unit_type(), byte_base_unit::unit_type());
}

// test transitive factors, none of which are bit, just for good measure
BOOST_AUTO_TEST_CASE(test_transitive_byte_nat_hartley) {
    CHECK_TRANSITIVE_CF(byte_base_unit::unit_type(), nat_base_unit::unit_type(), hartley_base_unit::unit_type());
}

BOOST_AUTO_TEST_CASE(test_byte_quantity_is_default) {
    using namespace bu::information;
    quantity<info, double> qd(2 * byte);
    BOOST_CHECK_EQUAL(qd.value(), double(2));
    quantity<info, long> ql(2 * byte);
    BOOST_CHECK_EQUAL(ql.value(), long(2));
}

BOOST_AUTO_TEST_CASE(test_byte_quantity_explicit) {
    using namespace bu::information;
    quantity<hu::byte::info, double> qd(2 * byte);
    BOOST_CHECK_EQUAL(qd.value(), double(2));
    quantity<hu::byte::info, long> ql(2 * byte);
    BOOST_CHECK_EQUAL(ql.value(), long(2));
}

BOOST_AUTO_TEST_CASE(test_bit_quantity) {
    using namespace bu::information;
    quantity<hu::bit::info, double> qd(2 * bit);
    BOOST_CHECK_EQUAL(qd.value(), double(2));
    quantity<hu::bit::info, long> ql(2 * bit);
    BOOST_CHECK_EQUAL(ql.value(), long(2));
}

BOOST_AUTO_TEST_CASE(test_nat_quantity) {
    using namespace bu::information;
    quantity<hu::nat::info, double> qd(2 * nat);
    BOOST_CHECK_EQUAL(qd.value(), double(2));
    quantity<hu::nat::info, long> ql(2 * nat);
    BOOST_CHECK_EQUAL(ql.value(), long(2));
}

BOOST_AUTO_TEST_CASE(test_hartley_quantity) {
    using namespace bu::information;
    quantity<hu::hartley::info, double> qd(2 * hartley);
    BOOST_CHECK_EQUAL(qd.value(), double(2));
    quantity<hu::hartley::info, long> ql(2 * hartley);
    BOOST_CHECK_EQUAL(ql.value(), long(2));
}

BOOST_AUTO_TEST_CASE(test_shannon_quantity) {
    using namespace bu::information;
    quantity<hu::shannon::info, double> qd(2 * shannon);
    BOOST_CHECK_EQUAL(qd.value(), double(2));
    quantity<hu::shannon::info, long> ql(2 * shannon);
    BOOST_CHECK_EQUAL(ql.value(), long(2));
}

BOOST_AUTO_TEST_CASE(test_mixed_hu) {
    using namespace bu::information;
    const double cf = 0.001;
    BOOST_CHECK_CLOSE_FRACTION((quantity<hu::bit::info>(1.0 * bits)).value(), 1.0, cf);
    BOOST_CHECK_CLOSE_FRACTION((quantity<hu::byte::info>(1.0 * bits)).value(), 1.0/8.0, cf);
    BOOST_CHECK_CLOSE_FRACTION((quantity<hu::nat::info>(1.0 * bits)).value(), 0.69315, cf);
    BOOST_CHECK_CLOSE_FRACTION((quantity<hu::hartley::info>(1.0 * bits)).value(), 0.30102, cf);
    BOOST_CHECK_CLOSE_FRACTION((quantity<hu::shannon::info>(1.0 * bits)).value(), 1.0, cf);
}

BOOST_AUTO_TEST_CASE(test_info_prefixes) {
    using namespace bu::information;
    quantity<info, long long> q10(1LL * kibi * byte);
    BOOST_CHECK_EQUAL(q10.value(), 1024LL);

    quantity<info, long long> q20(1LL * mebi * byte);
    BOOST_CHECK_EQUAL(q20.value(), 1048576LL);

    quantity<info, long long> q30(1LL * gibi * byte);
    BOOST_CHECK_EQUAL(q30.value(), 1073741824LL);

    quantity<info, long long> q40(1LL * tebi * byte);
    BOOST_CHECK_EQUAL(q40.value(), 1099511627776LL);

    quantity<info, long long> q50(1LL * pebi * byte);
    BOOST_CHECK_EQUAL(q50.value(), 1125899906842624LL);

    quantity<info, long long> q60(1LL * exbi * byte);
    BOOST_CHECK_EQUAL(q60.value(), 1152921504606846976LL);

    using boost::multiprecision::int128_t;

    quantity<info, int128_t> q70(1LL * zebi * byte);
    BOOST_CHECK_EQUAL(q70.value(), int128_t("1180591620717411303424"));

    quantity<info, int128_t> q80(1LL * yobi * byte);
    BOOST_CHECK_EQUAL(q80.value(), int128_t("1208925819614629174706176"));

    // sanity check: si prefixes should also operate
    quantity<info, long long> q1e3(1LL * si::kilo * byte);
    BOOST_CHECK_EQUAL(q1e3.value(), 1000LL);

    quantity<info, long long> q1e6(1LL * si::mega * byte);
    BOOST_CHECK_EQUAL(q1e6.value(), 1000000LL);
}

BOOST_AUTO_TEST_CASE(test_unit_constant_io) {
    using namespace bu::information;

    std::stringstream ss;
    ss << bu::symbol_format << bytes;
    BOOST_CHECK_EQUAL(ss.str(), "B");

    ss.str("");
    ss << bu::name_format << bytes;
    BOOST_CHECK_EQUAL(ss.str(), "byte");

    ss.str("");
    ss << bu::symbol_format << bits;
    BOOST_CHECK_EQUAL(ss.str(), "b");

    ss.str("");
    ss << bu::name_format << bits;
    BOOST_CHECK_EQUAL(ss.str(), "bit");

    ss.str("");
    ss << bu::symbol_format << nats;
    BOOST_CHECK_EQUAL(ss.str(), "nat");

    ss.str("");
    ss << bu::name_format << nats;
    BOOST_CHECK_EQUAL(ss.str(), "nat");

    ss.str("");
    ss << bu::symbol_format << hartleys;
    BOOST_CHECK_EQUAL(ss.str(), "Hart");

    ss.str("");
    ss << bu::name_format << hartleys;
    BOOST_CHECK_EQUAL(ss.str(), "hartley");

    ss.str("");
    ss << bu::symbol_format << shannons;
    BOOST_CHECK_EQUAL(ss.str(), "Sh");

    ss.str("");
    ss << bu::name_format << shannons;
    BOOST_CHECK_EQUAL(ss.str(), "shannon");
}