1 // Copyright (C) 2009-2020 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.5  Increment and decrement operators (decimal32).
21 // ISO/IEC TR 24733  3.2.3.5  Increment and decrement operators (decimal64).
22 // ISO/IEC TR 24733  3.2.4.5  Increment and decrement operators (decimal128).
23 
24 #include <decimal/decimal>
25 #include <testsuite_hooks.h>
26 
27 // Use extension to replace implicit long long conversion with function call.
28 #define LONGLONG(X) decimal_to_long_long(X)
29 
30 using namespace std::decimal;
31 
32 void
incdec32(void)33 incdec32 (void)
34 {
35   int ival;
36   decimal32 a(11), b, c;
37 
38   // Verify that we get the expected value of b after assignment.
39   b = a;
40   ival = LONGLONG (b); VERIFY (ival == 11);
41 
42   // Check that the increment and decrement operators change the value
43   // of the original class.
44   b = a;
45   ++b;
46   ival = LONGLONG (b); VERIFY (ival == 12);
47 
48   b = a;
49   b++;
50   ival = LONGLONG (b); VERIFY (ival == 12);
51 
52   b = a;
53   --b;
54   ival = LONGLONG (b); VERIFY (ival == 10);
55 
56   b = a;
57   b--;
58   ival = LONGLONG (b); VERIFY (ival == 10);
59 
60   // Check that the increment and decrement operators return the
61   // correct value.
62   b = a;
63   c = ++b;
64   ival = LONGLONG (c); VERIFY (ival == 12);
65 
66   b = a;
67   c = b++;
68   ival = LONGLONG (c); VERIFY (ival == 11);
69 
70   b = a;
71   c = --b;
72   ival = LONGLONG (c); VERIFY (ival == 10);
73 
74   b = a;
75   c = b--;
76   ival = LONGLONG (c); VERIFY (ival == 11);
77 }
78 
79 void
incdec64(void)80 incdec64 (void)
81 {
82   int ival;
83   decimal64 a(11), b, c;
84 
85   // Verify that we get the expected value of b after assignment.
86   b = a;
87   ival = LONGLONG (b); VERIFY (ival == 11);
88 
89   // Check that the increment and decrement operators change the value
90   // of the original class.
91   b = a;
92   ++b;
93   ival = LONGLONG (b); VERIFY (ival == 12);
94 
95   b = a;
96   b++;
97   ival = LONGLONG (b); VERIFY (ival == 12);
98 
99   b = a;
100   --b;
101   ival = LONGLONG (b); VERIFY (ival == 10);
102 
103   b = a;
104   b--;
105   ival = LONGLONG (b); VERIFY (ival == 10);
106 
107   // Check that the increment and decrement operators return the
108   // correct value.
109   b = a;
110   c = ++b;
111   ival = LONGLONG (c); VERIFY (ival == 12);
112 
113   b = a;
114   c = b++;
115   ival = LONGLONG (c); VERIFY (ival == 11);
116 
117   b = a;
118   c = --b;
119   ival = LONGLONG (c); VERIFY (ival == 10);
120 
121   b = a;
122   c = b--;
123   ival = LONGLONG (c); VERIFY (ival == 11);
124 }
125 
126 void
incdec128(void)127 incdec128 (void)
128 {
129   int ival;
130   decimal128 a(11), b, c;
131 
132   // Verify that we get the expected value of b after assignment.
133   b = a;
134   ival = LONGLONG (b); VERIFY (ival == 11);
135 
136   // Check that the increment and decrement operators change the value
137   // of the original class.
138   b = a;
139   ++b;
140   ival = LONGLONG (b); VERIFY (ival == 12);
141 
142   b = a;
143   b++;
144   ival = LONGLONG (b); VERIFY (ival == 12);
145 
146   b = a;
147   --b;
148   ival = LONGLONG (b); VERIFY (ival == 10);
149 
150   b = a;
151   b--;
152   ival = LONGLONG (b); VERIFY (ival == 10);
153 
154   // Check that the increment and decrement operators return the
155   // correct value.
156   b = a;
157   c = ++b;
158   ival = LONGLONG (c); VERIFY (ival == 12);
159 
160   b = a;
161   c = b++;
162   ival = LONGLONG (c); VERIFY (ival == 11);
163 
164   b = a;
165   c = --b;
166   ival = LONGLONG (c); VERIFY (ival == 10);
167 
168   b = a;
169   c = b--;
170   ival = LONGLONG (c); VERIFY (ival == 11);
171 }
172 
173 int
main()174 main ()
175 {
176   incdec32 ();
177   incdec64 ();
178   incdec128 ();
179 }
180