source: tests/collections/string-operator.cfa@ dab6e39

stuck-waitfor-destruct
Last change on this file since dab6e39 was d03a386, checked in by Michael Brooks <mlbrooks@…>, 11 months ago

Give a few string operator overloads a preference boost.

Intent is to approximate: When selecting +/* candidates, treat it ambiguous until finding a user-given arithmetic-vs-string constraint, such as assigning the result to a string. Once a string interpretation is imposed, prefer an alternative that converts to string as soon as possible.

This description is not directly achievable with the CFA type system. The approximation has the known flaw shown in the string-operator test, where a fairly built-up expression that should be ambiguous is actually defaulting to the string version.

This change is the last of the string-overload reorganizations that the string-operator test was originally meant to illustrate. In Mike's opinion, the resulting state is ideal, except for the just-mentioned flaw.

  • Property mode set to 100644
File size: 8.0 KB
Line 
1#include <fstream.hfa>
2#include <string.hfa>
3
4// "May Reject" (MR) handling
5// This test traces how several SUT revisions treat a common set of cases.
6// Sometimes the handling is a compiler rejection.
7// When it is, that case has to be skipped from the main run, and run individually to show the rejection.
8// Just because some SUT revision will reject / once rejected, doesn't mean it rejects currently.
9
10// These MR points do reject in the current revision, so they have satellite "-ERR" cases:
11// MR09
12// MR11
13// MR13
14// MR15
15// MR16
16
17// These MR points do not reject in the current revision, so they join the "happy run":
18#define TRY_MR01
19#define TRY_MR02
20#define TRY_MR03
21#define TRY_MR04
22#define TRY_MR05
23#define TRY_MR06
24#define TRY_MR07
25#define TRY_MR08
26#define TRY_MR10
27#define TRY_MR12
28#define TRY_MR14
29
30
31#ifdef TRY_MR01
32#define MR01(...) __VA_ARGS__
33#else
34#define MR01(...) sout | "(skip)";
35#endif
36
37#ifdef TRY_MR02
38#define MR02(...) __VA_ARGS__
39#else
40#define MR02(...) sout | "(skip)";
41#endif
42
43#ifdef TRY_MR03
44#define MR03(...) __VA_ARGS__
45#else
46#define MR03(...) sout | "(skip)";
47#endif
48
49#ifdef TRY_MR04
50#define MR04(...) __VA_ARGS__
51#else
52#define MR04(...) sout | "(skip)";
53#endif
54
55#ifdef TRY_MR05
56#define MR05(...) __VA_ARGS__
57#else
58#define MR05(...) sout | "(skip)";
59#endif
60
61#ifdef TRY_MR06
62#define MR06(...) __VA_ARGS__
63#else
64#define MR06(...) sout | "(skip)";
65#endif
66
67#ifdef TRY_MR07
68#define MR07(...) __VA_ARGS__
69#else
70#define MR07(...) sout | "(skip)";
71#endif
72
73#ifdef TRY_MR08
74#define MR08(...) __VA_ARGS__
75#else
76#define MR08(...) sout | "(skip)";
77#endif
78
79#ifdef TRY_MR09
80#define MR09(...) __VA_ARGS__
81#else
82#define MR09(...) sout | "(skip)";
83#endif
84
85#ifdef TRY_MR10
86#define MR10(...) __VA_ARGS__
87#else
88#define MR10(...) sout | "(skip)";
89#endif
90
91#ifdef TRY_MR11
92#define MR11(...) __VA_ARGS__
93#else
94#define MR11(...) sout | "(skip)";
95#endif
96
97#ifdef TRY_MR12
98#define MR12(...) __VA_ARGS__
99#else
100#define MR12(...) sout | "(skip)";
101#endif
102
103#ifdef TRY_MR13
104#define MR13(...) __VA_ARGS__
105#else
106#define MR13(...) sout | "(skip)";
107#endif
108
109#ifdef TRY_MR14
110#define MR14(...) __VA_ARGS__
111#else
112#define MR14(...) sout | "(skip)";
113#endif
114
115#ifdef TRY_MR15
116#define MR15(...) __VA_ARGS__
117#else
118#define MR15(...) sout | "(skip)";
119#endif
120
121#ifdef TRY_MR16
122#define MR16(...) __VA_ARGS__
123#else
124#define MR16(...) sout | "(skip)";
125#endif
126
127
128int main() {
129 sout | "------------- Explicit char arithmetic";
130 {
131 char ch;
132 ch = 'a' + 'b';
133 sout | ch; // Ã
134 sout | nl;
135 }
136
137
138 sout | "------------- Initialization";
139MR01( {string s = 'a' + 'b'; // ab
140 sout | s;} )
141 {string s = 'a' + "b"; // ab
142 sout | s;}
143 {string s = "a" + 'b'; // ab
144 sout | s;}
145 {string s = "a" + "b"; // ab
146 sout | s;}
147 sout | nl; //
148
149 string s0 = "x";
150
151MR02( {string s = 'a' + 'b' + s0; // abx
152 sout | s;} )
153 {string s = 'a' + "b" + s0; // abx
154 sout | s;}
155 {string s = "a" + 'b' + s0; // abx
156 sout | s;}
157 {string s = "a" + "b" + s0; // abx
158 sout | s;}
159 sout | nl; //
160
161 {string s = s0 + 'a' + 'b'; // xab
162 sout | s;}
163 {string s = s0 + 'a' + "b"; // xab
164 sout | s;}
165 {string s = s0 + "a" + 'b'; // xab
166 sout | s;}
167 {string s = s0 + "a" + "b"; // xab
168 sout | s;}
169 sout | nl; //
170
171 {string s = 'a' + s0 + 'b'; // axb
172 sout | s;}
173 {string s = 'a' + s0 + "b"; // axb
174 sout | s;}
175 {string s = "a" + s0 + 'b'; // axb
176 sout | s;}
177 {string s = "a" + s0 + "b"; // axb
178 sout | s;}
179 sout | nl; //
180
181MR03( {string s = 'a' * 3; // aaa
182 sout | s;} )
183 {string s = "b" * 3; // bbb
184 sout | s;}
185 {string s = ('a' + 'b') * 3; // ababab
186 sout | s;}
187 {string s = ('c' + "d") * 3; // cdcdcd
188 sout | s;}
189 sout | nl; //
190
191MR04( {string s = 3 * 'a'; // aaa
192 sout | s;} )
193 {string s = 3 * "b"; // bbb
194 sout | s;}
195 {string s = 3 * ('a' + 'b'); // ababab
196 sout | s;}
197 {string s = 3 * ('c' + "d"); // cdcdcd
198 sout | s;}
199 sout | nl; //
200
201
202 sout | "------------- Assignment";
203
204 string s;
205
206 s = "";
207MR05( s = 'a' + 'b'; // ab
208 sout | s; )
209 s = 'a' + "b"; // ab
210 sout | s;
211 s = "a" + 'b'; // ab
212 sout | s;
213 s = "a" + "b"; // ab
214 sout | s;
215 sout | nl; //
216
217 s = "";
218MR06( s = 'a' + 'b' + s; // ab
219 sout | s; )
220 s = 'a' + "b" + s; // abab
221 sout | s;
222 s = "a" + 'b' + s; // ababab
223 sout | s;
224 s = "a" + "b" + s; // abababab
225 sout | s;
226 sout | nl; //
227
228 s = "";
229 s = s + 'a' + 'b'; // ab
230 sout | s;
231 s = s + 'a' + "b"; // abab
232 sout | s;
233 s = s + "a" + 'b'; // ababab
234 sout | s;
235 s = s + "a" + "b"; // abababab
236 sout | s;
237 sout | nl; //
238
239 s = "";
240 s = 'a' + s + 'b'; // ab
241 sout | s;
242 s = 'a' + s + "b"; // aabb
243 sout | s;
244 s = "a" + s + 'b'; // aaabbb
245 sout | s;
246 s = "a" + s + "b"; // aaaabbbb
247 sout | s;
248 sout | nl; //
249
250 s = "";
251MR07( s = 'a' * 3; // aaa
252 sout | s; )
253 s = "b" * 3; // bbb
254 sout | s;
255 s = ('a' + 'b') * 3; // ababab
256 sout | s;
257 s = ('c' + "d") * 3; // cdcdcd
258 sout | s;
259 sout | nl; //
260
261 s = "";
262MR08( s = 3 * 'a'; // aaa
263 sout | s; )
264 s = 3 * "b"; // bbb
265 sout | s;
266 s = 3 * ('a' + 'b'); // ababab
267 sout | s;
268 s = 3 * ('c' + "d"); // cdcdcd
269 sout | s;
270 sout | nl; //
271
272
273 sout | "------------- Bare (sout-direct)";
274
275 s = "x";
276
277MR09( sout | 'a' + 'b'; ) // (ambiguous)
278 sout | 'a' + "b"; // ab
279 sout | "a" + 'b'; // ab
280 sout | "a" + "b"; // ab
281 sout | nl; //
282
283MR10( sout | 'a' + 'b' + s; ) // abx
284 sout | 'a' + "b" + s; // abx
285 sout | "a" + 'b' + s; // abx
286 sout | "a" + "b" + s; // abx
287 sout | nl; //
288
289 sout | s + 'a' + 'b'; // xab
290 sout | s + 'a' + "b"; // xab
291 sout | s + "a" + 'b'; // xab
292 sout | s + "a" + "b"; // xab
293 sout | nl; //
294
295 sout | 'a' + s + 'b'; // axb
296 sout | 'a' + s + "b"; // axb
297 sout | "a" + s + 'b'; // axb
298 sout | "a" + s + "b"; // axb
299 sout | nl; //
300
301MR11( sout | 'a' * 3; ) // (ambiguous)
302 sout | "b" * 3; // bbb
303MR12( sout | ('a' + 'b') * 3; ) // ababab (ideally ambiguous, known approximation flaw)
304 sout | ('c' + "d") * 3; // cdcdcd
305 sout | nl; //
306
307MR13( sout | 3 * 'a'; ) // (ambiguous)
308 sout | 3 * "b"; // bbb
309MR14( sout | 3 * ('a' + 'b'); ) // ababab (ideally ambiguous, known approximation flaw)
310 sout | 3 * ('c' + "d"); // cdcdcd
311 sout | nl; //
312
313 sout | "------------- Miscellany";
314
315 // random examples that have been discussed
316
317MR15( printf( "%c\n", 'a' + 'b'); ) // (ambiguous)
318MR16( printf( "%d\n", 'a' * 3 ); ) // (ambiguous)
319
320 { // (picks arithmetic; there is no interpretation of `_ + 3` that's string)
321 char ch = 42;
322 printf( "0x%x 0x%x\n", ch, ch + 3 ); // 0x3 0x6
323 }
324}
Note: See TracBrowser for help on using the repository browser.