]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/safe_numerics/test/test_rational.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / safe_numerics / test / test_rational.cpp
CommitLineData
92f5a8d4
TL
1//////////////////////////////////////////////////////////////////
2// example94.cpp
3//
4// Copyright (c) 2015 Robert Ramey
5//
6// Distributed under the Boost Software License, Version 1.0. (See
7// accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
10// illustrate usage of safe<int> as drop-in replacement for int in
11// a more complex library. Use an example from the boost.rational
12// library with modifications to use safe<int> rather than int
13
14// rational number example program ----------------------------------------//
15
16// (C) Copyright Paul Moore 1999. Permission to copy, use, modify, sell
17// and distribute this software is granted provided this copyright notice
18// appears in all copies. This software is provided "as is" without express or
19// implied warranty, and with no claim as to its suitability for any purpose.
20
21// boostinspect:nolicense (don't complain about the lack of a Boost license)
22// (Paul Moore hasn't been in contact for years, so there's no way to change the
23// license.)
24
25// Revision History
26// 14 Dec 99 Initial version
27
28#include <iostream>
29#include <cassert>
30#include <cstdlib>
31#include <boost/config.hpp>
32#include <limits>
33#include <exception>
34#include <boost/rational.hpp>
35
36#include <boost/safe_numerics/safe_integer.hpp>
37
38using std::cout;
39using std::endl;
40using boost::rational;
41using namespace boost::safe_numerics;
42
43using int_type = safe<int>;
44
20effc67 45int main (){
92f5a8d4
TL
46 rational<int_type> half(1,2);
47 rational<int_type> one(1);
48 rational<int_type> two(2);
49
50 // Some basic checks
51 assert(half.numerator() == 1);
52 assert(half.denominator() == 2);
53// assert(boost::rational_cast<double>(half) == 0.5);
54
55 static_assert(
56 ! boost::safe_numerics::is_safe<rational<int_type>>::value,
57 "rational<int_type> is safe"
58 );
59
60 // Arithmetic
61 assert(half + half == one);
62 assert(one - half == half);
63 assert(two * half == one);
64 assert(one / half == two);
65
66 // With conversions to integer
67 assert(half+half == 1);
68 assert(2 * half == one);
69 assert(2 * half == 1);
70 assert(one / half == 2);
71 assert(1 / half == 2);
72
73 // Sign handling
74 rational<int_type> minus_half(-1,2);
75 assert(-half == minus_half);
76 assert(abs(minus_half) == half);
77
78 // Do we avoid overflow?
79 int maxint = (std::numeric_limits<int>::max)();
80 rational<int_type> big(maxint, 2);
81 assert(2 * big == maxint);
82
83 // Print some of the above results
84 cout << half << "+" << half << "=" << one << endl;
85 cout << one << "-" << half << "=" << half << endl;
86 cout << two << "*" << half << "=" << one << endl;
87 cout << one << "/" << half << "=" << two << endl;
88 cout << "abs(" << minus_half << ")=" << half << endl;
89 cout << "2 * " << big << "=" << maxint
90 << " (rational: " << rational<int>(maxint) << ")" << endl;
91
92 // Some extras
93// rational<int_type> pi(22,7);
94// cout << "pi = " << boost::rational_cast<double>(pi) << " (nearly)" << endl;
95
96 // Exception handling
97 try {
98 rational<int_type> r; // Forgot to initialise - set to 0
99 r = 1/r; // Boom!
100 }
101 catch (const boost::bad_rational &e) {
102 cout << "Bad rational, as expected: " << e.what() << endl;
103 }
104 catch (...) {
105 cout << "Wrong exception raised!" << endl;
106 }
107 return 0;
108}