1// Copyright (C) 2009-2015 Free Software Foundation, Inc.
2//
3// This file is part of the GNU ISO C++ Library.  This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3.  If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-require-effective-target dfp }
19
20// ISO/IEC TR 24733  3.2.2.2  Conversion from floating-point type (decimal32).
21// ISO/IEC TR 24733  3.2.3.2  Conversion from floating-point type (decimal64).
22// ISO/IEC TR 24733  3.2.4.2  Conversion from floating-point type (decimal128).
23
24#include <decimal/decimal>
25#include <testsuite_hooks.h>
26
27using namespace std::decimal;
28
29void
30conversion_from_float_32 ()
31{
32  bool test __attribute__((unused)) = true;
33  decimal32 d32(123);
34  decimal64 d64(234);
35  decimal128 d128(345);
36  float f = 456.F;
37  double d = 567.;
38  long double ld = 678.L;
39
40  d32 = (decimal32) d64;
41  VERIFY (d32 == make_decimal32 (234LL, 0));
42  d32 = (decimal32) d128;
43  VERIFY (d32 == make_decimal32 (345LL, 0));
44  d32 = (decimal32) f;
45  VERIFY (d32 == make_decimal32 (456LL, 0));
46  d32 = (decimal32) d;
47  VERIFY (d32 == make_decimal32 (567LL, 0));
48  d32 = (decimal32) ld;
49  VERIFY (d32 == make_decimal32 (678LL, 0));
50}
51
52void
53conversion_from_float_64 ()
54{
55  bool test __attribute__((unused)) = true;
56  decimal32 d32(123);
57  decimal64 d64(234);
58  decimal128 d128(345);
59  float f = 456.F;
60  double d = 567.;
61  long double ld = 678.L;
62
63  d64 = d32;
64  VERIFY (d64 == make_decimal64 (123LL, 0));
65  d64 = (decimal64) d128;
66  VERIFY (d64 == make_decimal64 (345LL, 0));
67  d64 = (decimal64) f;
68  VERIFY (d64 == make_decimal64 (456LL, 0));
69  d64 = (decimal64) d;
70  VERIFY (d64 == make_decimal64 (567LL, 0));
71  d64 = (decimal64) ld;
72  VERIFY (d64 == make_decimal64 (678LL, 0));
73}
74
75void
76conversion_from_float_128 ()
77{
78  bool test __attribute__((unused)) = true;
79  decimal32 d32(123);
80  decimal64 d64(234);
81  decimal128 d128(345);
82  float f = 456.F;
83  double d = 567.;
84  long double ld = 678.L;
85
86  d128 = d32;
87  VERIFY (d128 == make_decimal128 (123LL, 0));
88  d128 = d64;
89  VERIFY (d128 == make_decimal128 (234LL, 0));
90  d128 = (decimal128) f;
91  VERIFY (d128 == make_decimal128 (456LL, 0));
92  d128 = (decimal128) d;
93  VERIFY (d128 == make_decimal128 (567LL, 0));
94  d128 = (decimal128) ld;
95  VERIFY (d128 == make_decimal128 (678LL, 0));
96}
97
98int
99main ()
100{
101  conversion_from_float_32 ();
102  conversion_from_float_64 ();
103  conversion_from_float_128 ();
104}
105