1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // ato.cfa -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Thu Feb 4 08:10:57 2016 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Fri Mar 15 17:58:35 2024 |
---|
13 | // Update Count : 145 |
---|
14 | // |
---|
15 | |
---|
16 | #include <fstream.hfa> |
---|
17 | #include <stdlib.hfa> // ato, strto |
---|
18 | |
---|
19 | int main( void ) { |
---|
20 | // ato |
---|
21 | |
---|
22 | const char * sptr = "-123"; |
---|
23 | int i = ato( sptr ); |
---|
24 | sout | i | sptr; |
---|
25 | sptr = "123"; |
---|
26 | unsigned int ui = ato( sptr ); |
---|
27 | sout | ui | sptr; |
---|
28 | |
---|
29 | sptr = "-123"; |
---|
30 | long int li = ato( sptr ); |
---|
31 | sout | li | sptr; |
---|
32 | sptr = "123"; |
---|
33 | unsigned long int uli = ato( sptr ); |
---|
34 | sout | uli | sptr; |
---|
35 | |
---|
36 | sptr = " -123 "; // spaces allowed |
---|
37 | long long int lli = ato( sptr ); |
---|
38 | sout | lli | sptr; |
---|
39 | sptr = " 123 "; // spaces allowed |
---|
40 | unsigned long long int ulli = ato( sptr ); |
---|
41 | sout | ulli | sptr; |
---|
42 | |
---|
43 | sptr = "-123.456"; |
---|
44 | float f = ato( sptr ); |
---|
45 | sout | f | sptr; |
---|
46 | sptr = "-123.4567890123456"; |
---|
47 | double d = ato( sptr ); |
---|
48 | sout | d | sptr; |
---|
49 | sptr = " -123.45678901234567890123456789 "; // spaces allowed |
---|
50 | long double ld = ato( sptr ); |
---|
51 | sout | ld | sptr; |
---|
52 | |
---|
53 | sptr = "-123.456-123.456i"; |
---|
54 | float _Complex fc = ato( sptr ); |
---|
55 | sout | fc | sptr; |
---|
56 | sptr = "-123.4567890123456+123.4567890123456i"; |
---|
57 | double _Complex dc = ato( sptr ); |
---|
58 | sout | dc | sptr; |
---|
59 | sptr = "123.45678901234567890123456789-123.45678901234567890123456789i"; |
---|
60 | long double _Complex ldc = ato( sptr ); |
---|
61 | sout | ldc | sptr; |
---|
62 | sptr = " 123.45678901234 -123.4567890i "; // spaces allowed |
---|
63 | long double _Complex ldc2 = ato( sptr ); |
---|
64 | sout | ldc2 | sptr; |
---|
65 | |
---|
66 | // strto |
---|
67 | |
---|
68 | sptr = "-123"; |
---|
69 | i = strto( sptr, 0p, 10 ); |
---|
70 | sout | i | sptr; |
---|
71 | sptr = "123"; |
---|
72 | ui = strto( sptr, 0p, 10 ); |
---|
73 | sout | ui | sptr; |
---|
74 | |
---|
75 | sptr = "-123"; |
---|
76 | li = strto( sptr, 0p, 10 ); |
---|
77 | sout | li | sptr; |
---|
78 | sptr = "123"; |
---|
79 | uli = strto( sptr, 0p, 10 ); |
---|
80 | sout | uli | sptr; |
---|
81 | |
---|
82 | sptr = " -123 "; // spaces allowed |
---|
83 | lli = strto( sptr, 0p, 10 ); |
---|
84 | sout | lli | sptr; |
---|
85 | sptr = " 123 "; // spaces allowed |
---|
86 | ulli = strto( sptr, 0p, 10 ); |
---|
87 | sout | ulli | sptr; |
---|
88 | |
---|
89 | sptr = "-123.456"; |
---|
90 | f = strto( sptr, 0p ); |
---|
91 | sout | f | sptr; |
---|
92 | sptr = "-123.4567890123456"; |
---|
93 | d = strto( sptr, 0p ); |
---|
94 | sout | d | sptr; |
---|
95 | sptr = " -123.45678901234567890123456789 "; // spaces allowed |
---|
96 | ld = strto( sptr, 0p ); |
---|
97 | sout | ld | sptr; |
---|
98 | |
---|
99 | sptr = "-123.456-123.456i"; |
---|
100 | fc = strto( sptr, 0p ); |
---|
101 | sout | fc | sptr; |
---|
102 | sptr = "-123.4567890123456+123.4567890123456i"; |
---|
103 | dc = strto( sptr, 0p ); |
---|
104 | sout | dc | sptr; |
---|
105 | sptr = "123.45678901234567890123456789-123.45678901234567890123456789i"; |
---|
106 | ldc = strto( sptr, 0p ); |
---|
107 | sout | ldc | sptr; |
---|
108 | sptr = " 123.45678901234 -123.4567890i "; // spaces allowed |
---|
109 | ldc2 = strto( sptr, 0p ); |
---|
110 | sout | ldc2 | sptr; |
---|
111 | |
---|
112 | sptr = "2.0fred"; |
---|
113 | char * eptr = 0p; |
---|
114 | errno = 0; // reset |
---|
115 | f = strto( sptr, &eptr ); |
---|
116 | if ( errno == ERANGE ) sout | "out of range"; |
---|
117 | if ( eptr == sptr || // conversion failed, no characters generated |
---|
118 | *eptr != '\0' ) sout | "invalid argument" | sptr; // not at end of str ? |
---|
119 | else assert( false ); |
---|
120 | |
---|
121 | sptr = "2 3x"; |
---|
122 | eptr = 0p; |
---|
123 | errno = 0; // reset |
---|
124 | fc = strto( sptr, &eptr ); |
---|
125 | if ( errno == ERANGE ) sout | "out of range"; |
---|
126 | if ( eptr == sptr || // conversion failed, no characters generated |
---|
127 | *eptr != '\0' ) sout | "invalid argument" | sptr; // not at end of str ? |
---|
128 | else assert( false ); |
---|
129 | |
---|
130 | // convert |
---|
131 | |
---|
132 | sptr = "-123"; |
---|
133 | i = convert( sptr ); |
---|
134 | sout | i | sptr; |
---|
135 | sptr = "123"; |
---|
136 | ui = convert( sptr ); |
---|
137 | sout | ui | sptr; |
---|
138 | |
---|
139 | sptr = "-123"; |
---|
140 | li = convert( sptr ); |
---|
141 | sout | li | sptr; |
---|
142 | sptr = "123"; |
---|
143 | uli = convert( sptr ); |
---|
144 | sout | uli | sptr; |
---|
145 | |
---|
146 | sptr = " -123 "; // spaces allowed |
---|
147 | lli = convert( sptr ); |
---|
148 | sout | lli | sptr; |
---|
149 | sptr = " 123 "; // spaces allowed |
---|
150 | ulli = convert( sptr ); |
---|
151 | sout | ulli | sptr; |
---|
152 | |
---|
153 | sptr = "-123.456"; |
---|
154 | f = convert( sptr ); |
---|
155 | sout | f | sptr; |
---|
156 | sptr = "-123.4567890123456"; |
---|
157 | d = convert( sptr ); |
---|
158 | sout | d | sptr; |
---|
159 | sptr = " -123.45678901234567890123456789 "; // spaces allowed |
---|
160 | ld = convert( sptr ); |
---|
161 | sout | ld | sptr; |
---|
162 | |
---|
163 | sptr = "-123.456-123.456i"; |
---|
164 | fc = convert( sptr ); |
---|
165 | sout | fc | sptr; |
---|
166 | sptr = "-123.4567890123456+123.4567890123456i"; |
---|
167 | dc = convert( sptr ); |
---|
168 | sout | dc | sptr; |
---|
169 | sptr = "123.45678901234567890123456789-123.45678901234567890123456789i"; |
---|
170 | ldc = convert( sptr ); |
---|
171 | sout | ldc | sptr; |
---|
172 | sptr = "123.45678901234-123.4567890i"; |
---|
173 | ldc2 = convert( sptr ); |
---|
174 | sout | ldc2 | sptr; |
---|
175 | |
---|
176 | sptr = "2.0fred"; |
---|
177 | try { |
---|
178 | f = convert( sptr ); |
---|
179 | assert( false ); |
---|
180 | } catch( invalid_argument * ) { |
---|
181 | sout | "invalid argument" | sptr; |
---|
182 | } // try |
---|
183 | |
---|
184 | sptr = "2 3x"; |
---|
185 | try { |
---|
186 | fc = convert( sptr ); |
---|
187 | assert( false ); |
---|
188 | } catch( invalid_argument * ) { |
---|
189 | sout | "invalid argument" | sptr; |
---|
190 | } // try |
---|
191 | } // main |
---|
192 | |
---|
193 | // Local Variables: // |
---|
194 | // tab-width: 4 // |
---|
195 | // compile-command: "cfa ato.cfa" // |
---|
196 | // End: // |
---|