
* mln/world/kn/safe_cast.hh: Add conversions towards/from unsigned. * tests/world/kn/safe_cast.cc: Add more tests. --- milena/ChangeLog | 9 +++ milena/mln/world/kn/safe_cast.hh | 108 +++++++++++++++++++++++++++++++++-- milena/tests/world/kn/safe_cast.cc | 106 +++++++++++++++++++++++------------ 3 files changed, 181 insertions(+), 42 deletions(-) diff --git a/milena/ChangeLog b/milena/ChangeLog index 39b1c71..56b4097 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,3 +1,12 @@ +2012-10-30 Guillaume Lazzara <z@lrde.epita.fr> + + Add more conversions in safe_cast. + + * mln/world/kn/safe_cast.hh: Add conversions towards/from + unsigned. + + * tests/world/kn/safe_cast.cc: Add more tests. + 2012-10-29 Guillaume Lazzara <z@lrde.epita.fr> Improve hqueue used in compute_tree_of_shapes. diff --git a/milena/mln/world/kn/safe_cast.hh b/milena/mln/world/kn/safe_cast.hh index 39309cd..da60265 100644 --- a/milena/mln/world/kn/safe_cast.hh +++ b/milena/mln/world/kn/safe_cast.hh @@ -62,6 +62,8 @@ namespace mln namespace value { + // From int_u<n> + template <unsigned m, unsigned n> void safe_cast_(const int_u<m>& from, intsub<n>& to) { @@ -92,6 +94,14 @@ namespace mln to = from.to_interop(); } + template <unsigned m> + void safe_cast_(const int_u<m>& from, unsigned& to) + { + to = from.to_interop(); + } + + // From interval<int> + void safe_cast_(const interval<int>& from, int& to) { if (!from.is_degenerated()) @@ -99,6 +109,13 @@ namespace mln to = from.first(); } + void safe_cast_(const interval<int>& from, unsigned& to) + { + if (!from.is_degenerated() || from.first() < 0) + std::abort(); + to = from.first(); + } + template <unsigned m> void safe_cast_(const interval<int>& from, int_u<m>& to) { @@ -115,13 +132,7 @@ namespace mln to = from.first(); } - template <unsigned n, unsigned m> - void safe_cast_(const interval<intsub<n> >& from, int_u<m>& to) - { - if (!from.is_degenerated()) - std::abort(); - to = intsub<n>(from.first()); - } + // From interval< int_u<m> > template <unsigned m> void safe_cast_(const interval<int_u<m> >& from, int_u<m>& to) @@ -147,6 +158,24 @@ namespace mln to = from.first().to_interop(); } + template <unsigned m> + void safe_cast_(const interval<int_u<m> >& from, unsigned& to) + { + if (!from.is_degenerated()) + std::abort(); + to = from.first().to_interop(); + } + + // From interval<intsub<n> > + + template <unsigned n, unsigned m> + void safe_cast_(const interval<intsub<n> >& from, int_u<m>& to) + { + if (!from.is_degenerated()) + std::abort(); + to = intsub<n>(from.first()); + } + template <unsigned n> void safe_cast_(const interval<intsub<n> >& from, int& to) { @@ -156,6 +185,14 @@ namespace mln } template <unsigned n> + void safe_cast_(const interval<intsub<n> >& from, unsigned& to) + { + if (!from.is_degenerated() || from.first() < 0) + std::abort(); + to = from.first(); + } + + template <unsigned n> void safe_cast_(const interval<intsub<n> >& from, intsub<n>& to) { if (!from.is_degenerated()) @@ -163,6 +200,8 @@ namespace mln to = from.first(); } + // From int + template <unsigned n> void safe_cast_(const int& from, interval<intsub<n> >& to) { @@ -174,6 +213,13 @@ namespace mln to = interval<int>(from); } + void safe_cast_(const int& from, unsigned& to) + { + if (from < 0) + std::abort(); + to = from; + } + template <unsigned m> void safe_cast_(const int& from, int_u<m>& to) { @@ -186,6 +232,7 @@ namespace mln to = from; } + // From intsub<n> template <unsigned n> void safe_cast_(const intsub<n>& from, intsub<2*n>& to) @@ -211,6 +258,14 @@ namespace mln to = from; } + template <unsigned n> + void safe_cast_(const intsub<n>& from, unsigned& to) + { + if (from < 0) + std::abort(); + to = from; + } + template <unsigned n, unsigned m> void safe_cast_(const intsub<n>& from, interval<int_u<m> >& to) { @@ -229,6 +284,45 @@ namespace mln to = interval<intsub<n> >(from); } + // From unsigned + + template <unsigned n> + void safe_cast_(const unsigned& from, interval<intsub<n> >& to) + { + // FIXME: check if unsigned value fits in intsub. + to = interval<intsub<n> >((int)from); + } + + void safe_cast_(const unsigned& from, interval<int>& to) + { + if (from > static_cast<unsigned>(mln_max(int))) + std::abort(); + to = interval<int>(from); + } + + void safe_cast_(const unsigned& from, int& to) + { + if (from > static_cast<unsigned>(mln_max(int))) + std::abort(); + to = from; + } + + template <unsigned m> + void safe_cast_(const unsigned& from, int_u<m>& to) + { + if (from > mln_max(int_u<m>)) + std::abort(); + to = from; + } + + template <unsigned n> + void safe_cast_(const unsigned& from, intsub<n>& to) + { + // FIXME: check if unsigned value fits in intsub. + to = (int)from; + } + + } // end of namespace mln::value diff --git a/milena/tests/world/kn/safe_cast.cc b/milena/tests/world/kn/safe_cast.cc index 7c5be20..135fc1a 100644 --- a/milena/tests/world/kn/safe_cast.cc +++ b/milena/tests/world/kn/safe_cast.cc @@ -36,46 +36,53 @@ int main() using namespace mln::value; using namespace mln::world; - // int -> int_u<8> + // unsigned -> int_u<8> { - int i = 2; + unsigned i = 2; int_u<8> j = kn::safe_cast_to<int_u<8> >(i); mln_assertion(j == 2); } - // int -> intsub<2> + // unsigned -> intsub<2> { - int i = 2; + unsigned i = 2; intsub<2> j = kn::safe_cast_to<intsub<2> >(i); mln_assertion(j == 2); } + // unsigned -> interval<int> + { + unsigned i = 2; + interval<int> j = kn::safe_cast_to<interval<int> >(i); + mln_assertion(j.is_degenerated() && j.first() == 2); + } - // int_u<8> -> int + // unsigned -> interval<int_u<8> > { - int_u<8> i = 2; - int j = kn::safe_cast_to<int>(i); - mln_assertion(j == 2); + unsigned i = 2; + interval<int_u<8> > j = kn::safe_cast_to<interval<int_u<8> > >(i); + mln_assertion(j.is_degenerated() && j.first() == 2); } - // int_u<8> -> intsub<2> + // unsigned -> interval<intsub<2> > { - int_u<8> i = 2; - intsub<2> j = kn::safe_cast_to<intsub<2> >(i); - mln_assertion(j == 2); + unsigned i = 2; + interval<intsub<2> > j = kn::safe_cast_to<interval<intsub<2> > >(i); + mln_assertion(j.is_degenerated() && j.first() == 2); } - // intsub<2> -> int + + // int -> int_u<8> { - intsub<2> i = 2; - int j = kn::safe_cast_to<int>(i); + int i = 2; + int_u<8> j = kn::safe_cast_to<int_u<8> >(i); mln_assertion(j == 2); } - // intsub<2> -> int_u<8> + // int -> intsub<2> { - intsub<2> i = 2; - int_u<8> j = kn::safe_cast_to<int_u<8> >(i); + int i = 2; + intsub<2> j = kn::safe_cast_to<intsub<2> >(i); mln_assertion(j == 2); } @@ -86,25 +93,32 @@ int main() mln_assertion(j.is_degenerated() && j.first() == 2); } - // int_u -> interval<int> + // int -> interval<int_u<8> > { - int_u<8> i = 2; - interval<int> j = kn::safe_cast_to<interval<int> >(i); + int i = 2; + interval<int_u<8> > j = kn::safe_cast_to<interval<int_u<8> > >(i); mln_assertion(j.is_degenerated() && j.first() == 2); } - // int_u -> interval<int> + // int -> interval<intsub<2> > { - intsub<2> i = 2; - interval<int> j = kn::safe_cast_to<interval<int> >(i); + int i = 2; + interval<intsub<2> > j = kn::safe_cast_to<interval<intsub<2> > >(i); mln_assertion(j.is_degenerated() && j.first() == 2); } - // int -> interval<int_u<8> > + // int_u<8> -> int { - int i = 2; - interval<int_u<8> > j = kn::safe_cast_to<interval<int_u<8> > >(i); - mln_assertion(j.is_degenerated() && j.first() == 2); + int_u<8> i = 2; + int j = kn::safe_cast_to<int>(i); + mln_assertion(j == 2); + } + + // int_u<8> -> intsub<2> + { + int_u<8> i = 2; + intsub<2> j = kn::safe_cast_to<intsub<2> >(i); + mln_assertion(j == 2); } // int_u<8> -> interval<int_u<8> > @@ -114,18 +128,17 @@ int main() mln_assertion(j.is_degenerated() && j.first() == 2); } - // intsub<2> -> interval<int_u<8> > + // int_u<8> -> interval<int> { - intsub<2> i = 2; - interval<int_u<8> > j = kn::safe_cast_to<interval<int_u<8> > >(i); + int_u<8> i = 2; + interval<int> j = kn::safe_cast_to<interval<int> >(i); mln_assertion(j.is_degenerated() && j.first() == 2); } - - // int -> interval<intsub<2> > + // int_u<8> -> interval<int> { - int i = 2; - interval<intsub<2> > j = kn::safe_cast_to<interval<intsub<2> > >(i); + intsub<2> i = 2; + interval<int> j = kn::safe_cast_to<interval<int> >(i); mln_assertion(j.is_degenerated() && j.first() == 2); } @@ -136,6 +149,27 @@ int main() mln_assertion(j.is_degenerated() && j.first() == 2); } + // intsub<2> -> int + { + intsub<2> i = 2; + int j = kn::safe_cast_to<int>(i); + mln_assertion(j == 2); + } + + // intsub<2> -> int_u<8> + { + intsub<2> i = 2; + int_u<8> j = kn::safe_cast_to<int_u<8> >(i); + mln_assertion(j == 2); + } + + // intsub<2> -> interval<int_u<8> > + { + intsub<2> i = 2; + interval<int_u<8> > j = kn::safe_cast_to<interval<int_u<8> > >(i); + mln_assertion(j.is_degenerated() && j.first() == 2); + } + // intsub<2> -> interval<intsub<2> > { intsub<2> i = 2; @@ -206,4 +240,6 @@ int main() mln_assertion(j == 2); } + + } -- 1.7.2.5