• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Boost test/add.cpp
2  * test with symbolic operations if the addition algorithm is correct
3  *
4  * Copyright 2002-2003 Guillaume Melquiond
5  *
6  * Distributed under the Boost Software License, Version 1.0.
7  * (See accompanying file LICENSE_1_0.txt or
8  * copy at http://www.boost.org/LICENSE_1_0.txt)
9  */
10 
11 #include <boost/numeric/interval/interval.hpp>
12 #include <boost/numeric/interval/arith.hpp>
13 #include <boost/numeric/interval/rounding.hpp>
14 #include <boost/numeric/interval/rounded_arith.hpp>
15 #include <boost/numeric/interval/utility.hpp>
16 #include <boost/numeric/interval/policies.hpp>
17 #include <boost/test/minimal.hpp>
18 #include "bugs.hpp"
19 
20 typedef enum { EXPR_VAR, EXPR_NEG, EXPR_UP, EXPR_DOWN, EXPR_ADD, EXPR_SUB } e_type;
21 
22 struct expr;
23 struct pexpr {
24   expr *ptr;
operator ->pexpr25   expr* operator->() { return ptr; }
pexprpexpr26   pexpr(expr *p = NULL): ptr(p) { }
27 };
28 
29 struct expr {
30   e_type type;
31   int var;
32   pexpr e;
33   pexpr e1, e2;
34 };
35 
var(int v)36 pexpr var(int v) {
37   pexpr e = new expr;
38   e->type = EXPR_VAR;
39   e->var = v;
40   return e;
41 }
42 
43 pexpr operator+(pexpr, pexpr);
44 pexpr operator-(pexpr, pexpr);
45 pexpr operator-(pexpr);
46 
operator +(pexpr a,pexpr b)47 pexpr operator+(pexpr a, pexpr b) {
48   if (a->type == EXPR_NEG) return b - a->e;
49   if (b->type == EXPR_NEG) return a - b->e;
50   if (a->type == EXPR_VAR && b->type == EXPR_VAR && a->var > b->var) return b + a;
51   pexpr c = new expr;
52   c->type = EXPR_ADD;
53   c->e1 = a;
54   c->e2 = b;
55   return c;
56 }
57 
operator -(pexpr a,pexpr b)58 pexpr operator-(pexpr a, pexpr b) {
59   if (b->type == EXPR_NEG) return a + b->e;
60   pexpr c = new expr;
61   c->type = EXPR_SUB;
62   c->e1 = a;
63   c->e2 = b;
64   return c;
65 }
66 
down(pexpr a)67 pexpr down(pexpr a) {
68   pexpr e = new expr;
69   e->type = EXPR_DOWN;
70   e->e = a;
71   return e;
72 }
73 
up(pexpr a)74 pexpr up(pexpr a) {
75   pexpr e = new expr;
76   e->type = EXPR_UP;
77   e->e = a;
78   return e;
79 }
80 
operator -(pexpr a)81 pexpr operator-(pexpr a) {
82   if (a->type == EXPR_NEG) return a->e;
83   if (a->type == EXPR_UP) return down(-a->e);
84   if (a->type == EXPR_DOWN) return up(-a->e);
85   if (a->type == EXPR_SUB) return a->e2 - a->e1;
86   if (a->type == EXPR_ADD) return -a->e1 - a->e2;
87   pexpr e = new expr;
88   e->type = EXPR_NEG;
89   e->e = a;
90   return e;
91 }
92 
operator ==(pexpr a,pexpr b)93 bool operator==(pexpr a, pexpr b) {
94   if (a->type != b->type) return false;
95   if (a->type == EXPR_VAR) return a->var == b->var;
96   if (a->type == EXPR_DOWN || a->type == EXPR_UP || a->type == EXPR_NEG)
97     return a->e == b->e;
98   return a->e1 == b->e1 && a->e2 == b->e2;
99 }
100 
operator <=(pexpr,pexpr)101 bool operator<=(pexpr, pexpr) { return true; }
102 
103 namespace boost {
104 namespace numeric {
105 namespace interval_lib {
106 
107 template<>
108 struct rounding_control<pexpr> {
109   typedef enum { RND_U, RND_M, RND_D } rounding_mode;
110   static rounding_mode mode;
rounding_controlboost::numeric::interval_lib::rounding_control111   rounding_control() { mode = RND_M; }
get_rounding_modeboost::numeric::interval_lib::rounding_control112   void get_rounding_mode(rounding_mode& m) { m = mode; }
set_rounding_modeboost::numeric::interval_lib::rounding_control113   void set_rounding_mode(rounding_mode m)  { mode = m; }
upwardboost::numeric::interval_lib::rounding_control114   void upward()   { mode = RND_U; }
downwardboost::numeric::interval_lib::rounding_control115   void downward() { mode = RND_D; }
force_roundingboost::numeric::interval_lib::rounding_control116   pexpr force_rounding(pexpr a) {
117     switch (mode) {
118     case RND_U: return up(a);
119     case RND_D: return down(a);
120     default: throw "Unset rounding mode";
121     }
122   }
123 };
124 
125 rounding_control<pexpr>::rounding_mode rounding_control<pexpr>::mode = RND_M;
126 
127 } // namespace interval_lib
128 } // namespace numeric
129 } // namespace boost
130 
131 template<class I>
test_neg()132 bool test_neg() {
133   I a(var(0), var(1));
134   return equal(-a, I(-var(1), -var(0)));
135 }
136 
137 template<class I>
test_add()138 bool test_add() {
139   I a(var(0), var(1)), b(var(2), var(3));
140   return equal(a + b, I(down(var(0) + var(2)), up(var(1) + var(3))));
141 }
142 
143 template<class I>
test_add1()144 bool test_add1() {
145   I a(var(0), var(1));
146   return equal(a + var(2), I(down(var(0) + var(2)), up(var(1) + var(2))));
147 }
148 
149 template<class I>
test_add2()150 bool test_add2() {
151   I a(var(0), var(1));
152   return equal(var(2) + a, I(down(var(0) + var(2)), up(var(1) + var(2))));
153 }
154 
155 template<class I>
test_sub()156 bool test_sub() {
157   I a(var(0), var(1)), b(var(2), var(3));
158   return equal(a - b, I(down(var(0) - var(3)), up(var(1) - var(2))));
159 }
160 
161 template<class I>
test_sub1()162 bool test_sub1() {
163   I a(var(0), var(1));
164   return equal(a - var(2), I(down(var(0) - var(2)), up(var(1) - var(2))));
165 }
166 
167 template<class I>
test_sub2()168 bool test_sub2() {
169   I a(var(0), var(1));
170   return equal(var(2) - a, I(down(var(2) - var(1)), up(var(2) - var(0))));
171 }
172 
173 template<class I>
test_addeq()174 bool test_addeq() {
175   I a(var(0), var(1)), b(var(2), var(3));
176   return equal(a += b, I(down(var(0) + var(2)), up(var(1) + var(3))));
177 }
178 
179 template<class I>
test_addeq1()180 bool test_addeq1() {
181   I a(var(0), var(1));
182   return equal(a += var(2), I(down(var(0) + var(2)), up(var(1) + var(2))));
183 }
184 
185 template<class I>
test_subeq()186 bool test_subeq() {
187   I a(var(0), var(1)), b(var(2), var(3));
188   return equal(a -= b, I(down(var(0) - var(3)), up(var(1) - var(2))));
189 }
190 
191 template<class I>
test_subeq1()192 bool test_subeq1() {
193   I a(var(0), var(1));
194   return equal(a -= var(2), I(down(var(0) - var(2)), up(var(1) - var(2))));
195 }
196 
197 struct my_checking
198 {
pos_infmy_checking199   static pexpr pos_inf() { throw; }
neg_infmy_checking200   static pexpr neg_inf() { throw; }
nanmy_checking201   static pexpr nan() { throw; }
is_nanmy_checking202   static bool is_nan(const pexpr&) { return false; }
empty_lowermy_checking203   static pexpr empty_lower() { throw; }
empty_uppermy_checking204   static pexpr empty_upper() { throw; }
is_emptymy_checking205   static bool is_empty(const pexpr&, const pexpr&) { return false; }
206 };
207 
208 template<class Rounding>
209 struct my_interval {
210 private:
211   typedef boost::numeric::interval_lib::save_state<Rounding> my_rounding;
212   typedef boost::numeric::interval_lib::policies<my_rounding, my_checking> my_policies;
213 public:
214   typedef boost::numeric::interval<pexpr, my_policies> type;
215 };
216 
test_main(int,char * [])217 int test_main(int, char *[]) {
218   typedef my_interval<boost::numeric::interval_lib::rounded_arith_std<pexpr> >::type I1;
219   typedef my_interval<boost::numeric::interval_lib::rounded_arith_opp<pexpr> >::type I2;
220   BOOST_CHECK((test_neg<I1>()));
221   BOOST_CHECK((test_neg<I2>()));
222   BOOST_CHECK((test_add<I1>()));
223   BOOST_CHECK((test_add<I2>()));
224   BOOST_CHECK((test_add1<I1>()));
225   BOOST_CHECK((test_add1<I2>()));
226   BOOST_CHECK((test_add2<I1>()));
227   BOOST_CHECK((test_add2<I2>()));
228   BOOST_CHECK((test_sub<I1>()));
229   BOOST_CHECK((test_sub<I2>()));
230   BOOST_CHECK((test_sub1<I1>()));
231   BOOST_CHECK((test_sub1<I2>()));
232   BOOST_CHECK((test_sub2<I1>()));
233   BOOST_CHECK((test_sub2<I2>()));
234   BOOST_CHECK((test_addeq<I1>()));
235   BOOST_CHECK((test_addeq<I2>()));
236   BOOST_CHECK((test_addeq1<I1>()));
237   BOOST_CHECK((test_addeq1<I2>()));
238   BOOST_CHECK((test_subeq<I1>()));
239   BOOST_CHECK((test_subeq<I2>()));
240   BOOST_CHECK((test_subeq1<I1>()));
241   BOOST_CHECK((test_subeq1<I2>()));
242   return 0;
243 }
244