Debian Codegraph @

fon/Praat_tests.cpp

raw
1 /* Praat_tests.cpp
2 *
3 * Copyright (C) 2001-2007,2009,2011-2026 Paul Boersma, David Weenink 2025
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* December 10, 2006: MelderInfo */
20 /* November 5, 2007: wchar */
21 /* 21 March 2009: modern enums */
22 /* 24 May 2011: C++ */
23 /* 5 June 2015: char32 */
24
25 #include "FileInMemory.h"
26 #include "NUMselect.h"
27 #include "SlopeSelector.h"
28 #include "Praat_tests.h"
29
30 #include "Graphics.h"
31 #include "praat.h"
32 #include "NUM2.h"
33 #include "Sound.h"
34
35 #include "enums_getText.h"
36 #include "Praat_tests_enums.h"
37 #include "enums_getValue.h"
38 #include "Praat_tests_enums.h"
39 #include <string>
40
41 #include "Gui.h"
42
43 static void testAutoData (autoDaata data) {
44 fprintf (stderr, "testAutoData: %p %p\n", data.get(), data -> name.get());
45 }
46 static void testAutoDataRef (autoDaata& data) {
47 fprintf (stderr, "testAutoDataRef: %p %p\n", data.get(), data -> name.get());
48 }
49 static void testData (Daata data) {
50 fprintf (stderr, "testData: %p %s\n", data, Melder_peek32to8 (data -> name.get()));
51 }
52 static autoDaata newAutoData () {
53 autoDaata data (Thing_new (Daata));
54 return data;
55 }
56 static integer length (conststring32 s) {
57 const integer result = Melder_length (s);
58 Melder_free (s);
59 return result;
60 }
61
62 static autoMAT constantHH (integer nrow, integer ncol, double value) {
63 autoMAT result = raw_MAT (nrow, ncol);
64 result.all() <<= value;
65 return result;
66 }
67
68 int Praat_tests (kPraatTests itest, conststring32 arg1, conststring32 arg2, conststring32 arg3, conststring32 arg4) {
69 int64 n = Melder_atoi (arg1);
70 double t = 0.0;
71 (void) arg1;
72 (void) arg2;
73 (void) arg3;
74 (void) arg4;
75 Melder_clearInfo ();
76 Melder_stopwatch ();
77 switch (itest) {
78 case kPraatTests::UNDEFINED:
79 case kPraatTests::_:
80 case kPraatTests::CHECK_RANDOM_1009_2009: {
81 } break;
82 case kPraatTests::TIME_RANDOM_FRACTION: {
83 for (int64 i = 1; i <= n; i ++)
84 (void) NUMrandomFraction ();
85 t = Melder_stopwatch ();
86 } break;
87 case kPraatTests::TIME_RANDOM_GAUSS: {
88 for (int64 i = 1; i <= n; i ++)
89 (void) NUMrandomGauss (0.0, 1.0);
90 t = Melder_stopwatch ();
91 } break;
92 case kPraatTests::TIME_SORT: {
93 integer size = Melder_atoi (arg2);
94 autoVEC array = raw_VEC (size);
95 Melder_stopwatch ();
96 for (int64 iteration = 1; iteration <= n; iteration ++) {
97 for (int64 i = 1; i <= size; i ++)
98 array [i] = NUMrandomFraction ();
99 sort_e_VEC_inout (array.get());
100 }
101 t = Melder_stopwatch () / (size * log2 (size));
102 } break;
103 case kPraatTests::TIME_INTEGER: {
104 int64 sum = 0;
105 for (int64 i = 1; i <= n; i ++)
106 sum += i * (i - 1) * (i - 2);
107 t = Melder_stopwatch ();
108 MelderInfo_writeLine (sum);
109 } break;
110 case kPraatTests::TIME_FLOAT: {
111 double sum = 0.0, fn = n;
112 for (double fi = 1.0; fi <= fn; fi += 1.0)
113 sum += fi * (fi - 1.0) * (fi - 2.0);
114 t = Melder_stopwatch (); // 2.02 ns
115 MelderInfo_writeLine (sum);
116 } break;
117 case kPraatTests::TIME_FLOAT_TO_UNSIGNED_BUILTIN: {
118 uint64 sum = 0;
119 double fn = n;
120 for (double fi = 1.0; fi <= fn; fi += 1.0)
121 sum += (uint32) fi;
122 t = Melder_stopwatch (); // 1.45 ns
123 MelderInfo_writeLine (sum);
124 } break;
125 case kPraatTests::TIME_FLOAT_TO_UNSIGNED_EXTERN: {
126 uint64 sum = 0;
127 double fn = n;
128 for (double fi = 1.0; fi <= fn; fi += 1.0)
129 sum += (uint32) ((int32) (fi - 2147483648.0) + 2147483647L + 1);
130 t = Melder_stopwatch (); // 1.47 ns
131 MelderInfo_writeLine (sum);
132 } break;
133 case kPraatTests::TIME_UNSIGNED_TO_FLOAT_BUILTIN: {
134 double sum = 0.0;
135 uint32 nu = (uint32) n;
136 for (uint32 iu = 1; iu <= nu; iu ++)
137 sum += (double) iu;
138 t = Melder_stopwatch (); // 0.88 ns
139 MelderInfo_writeLine (sum);
140 } break;
141 case kPraatTests::TIME_UNSIGNED_TO_FLOAT_EXTERN: {
142 double sum = 0.0;
143 uint32 nu = (uint32) n;
144 for (uint32 iu = 1; iu <= nu; iu ++)
145 sum += (double) (int32) (iu - 2147483647L - 1) + 2147483648.0;
146 t = Melder_stopwatch (); // 0.87 ns
147 MelderInfo_writeLine (sum);
148 } break;
149 case kPraatTests::TIME_STRING_MELDER_32: {
150 autoMelderString string;
151 char32 word [] { U"abc" };
152 word [2] = char32 (NUMrandomInteger (U'a', U'z'));
153 for (int64 i = 1; i <= n; i ++) {
154 MelderString_copy (& string, word);
155 for (int j = 1; j <= 30; j ++)
156 MelderString_append (& string, word);
157 }
158 t = Melder_stopwatch ();
159 } break;
160 case kPraatTests::TIME_STRING_MELDER_32_ALLOC: {
161 char32 word [] { U"abc" };
162 word [2] = char32 (NUMrandomInteger (U'a', U'z'));
163 for (int64 i = 1; i <= n; i ++) {
164 autoMelderString string;
165 MelderString_copy (& string, word);
166 for (int j = 1; j <= 30; j ++)
167 MelderString_append (& string, word);
168 }
169 t = Melder_stopwatch ();
170 } break;
171 case kPraatTests::TIME_STRING_CPP_S: {
172 std::string s = "";
173 char word [] { "abc" };
174 word [2] = char (NUMrandomInteger ('a', 'z'));
175 for (int64 i = 1; i <= n; i ++) {
176 s = word;
177 for (int j = 1; j <= 30; j ++)
178 s += word;
179 }
180 t = Melder_stopwatch ();
181 } break;
182 case kPraatTests::TIME_STRING_CPP_C: {
183 std::basic_string<char> s = "";
184 char word [] { "abc" };
185 word [2] = char (NUMrandomInteger ('a', 'z'));
186 for (int64 i = 1; i <= n; i ++) {
187 s = word;
188 for (int j = 1; j <= 30; j ++)
189 s += word;
190 }
191 t = Melder_stopwatch ();
192 } break;
193 case kPraatTests::TIME_STRING_CPP_WS: {
194 std::wstring s = L"";
195 wchar_t word [] { L"abc" };
196 word [2] = wchar_t (NUMrandomInteger (L'a', L'z'));
197 for (int64 i = 1; i <= n; i ++) {
198 s = word;
199 for (int j = 1; j <= 30; j ++)
200 s += word;
201 }
202 t = Melder_stopwatch ();
203 } break;
204 case kPraatTests::TIME_STRING_CPP_WC: {
205 std::basic_string<wchar_t> s = L"";
206 wchar_t word [] { L"abc" };
207 word [2] = wchar_t (NUMrandomInteger (L'a', L'z'));
208 for (int64 i = 1; i <= n; i ++) {
209 s = word;
210 for (int j = 1; j <= 30; j ++)
211 s += word;
212 }
213 t = Melder_stopwatch ();
214 } break;
215 case kPraatTests::TIME_STRING_CPP_32: {
216 std::basic_string<char32_t> s = U"";
217 char32 word [] { U"abc" };
218 word [2] = char32 (NUMrandomInteger (U'a', U'z'));
219 for (int64 i = 1; i <= n; i ++) {
220 s = word;
221 for (int j = 1; j <= 30; j ++)
222 s += word;
223 }
224 t = Melder_stopwatch ();
225 } break;
226 case kPraatTests::TIME_STRING_CPP_U32STRING: {
227 std::u32string s = U"";
228 char32 word [] { U"abc" };
229 word [2] = char32 (NUMrandomInteger (U'a', U'z'));
230 for (int64 i = 1; i <= n; i ++) {
231 s = word;
232 for (int j = 1; j <= 30; j ++)
233 s += word;
234 }
235 t = Melder_stopwatch ();
236 } break;
237 case kPraatTests::TIME_STRCPY: {
238 char buffer [100];
239 char word [] { "abc" };
240 word [2] = (char) NUMrandomInteger ('a', 'z');
241 for (int64 i = 1; i <= n; i ++) {
242 strcpy (buffer, word);
243 for (int j = 1; j <= 30; j ++)
244 strcpy (buffer + strlen (buffer), word);
245 }
246 t = Melder_stopwatch ();
247 MelderInfo_writeLine (Melder_peek8to32_u (buffer));
248 } break;
249 case kPraatTests::TIME_WCSCPY: {
250 wchar_t buffer [100];
251 wchar_t word [] { L"abc" };
252 word [2] = wchar_t (NUMrandomInteger (L'a', L'z'));
253 for (int64 i = 1; i <= n; i ++) {
254 wcscpy (buffer, word);
255 for (int j = 1; j <= 30; j ++)
256 wcscpy (buffer + wcslen (buffer), word);
257 }
258 t = Melder_stopwatch ();
259 } break;
260 case kPraatTests::TIME_STR32CPY: {
261 char32 buffer [100];
262 char32 word [] { U"abc" };
263 word [2] = char32 (NUMrandomInteger (U'a', U'z'));
264 for (int64 i = 1; i <= n; i ++) {
265 str32cpy (buffer, word);
266 for (int j = 1; j <= 30; j ++)
267 str32cpy (buffer + Melder_length (buffer), word);
268 }
269 t = Melder_stopwatch ();
270 MelderInfo_writeLine (buffer);
271 } break;
272 case kPraatTests::TIME_GRAPHICS_TEXT_TOP: {
273 autoPraatPictureOpen picture;
274 for (int64 i = 1; i <= n; i ++) {
275 Graphics_textTop (GRAPHICS, false, U"hello world");
276 }
277 t = Melder_stopwatch ();
278 } break;
279 case kPraatTests::TIME_UNDEFINED_NUMUNDEFINED: {
280 bool isAllDefined = true;
281 double x = 0.0;
282 for (int64 i = 1; i <= n; i ++) {
283 x += (double) i;
284 isAllDefined &= ( x != undefined );
285 }
286 t = Melder_stopwatch (); // 0.86 ns
287 MelderInfo_writeLine (isAllDefined, U" ", x);
288 } break;
289 case kPraatTests::TIME_UNDEFINED_ISINF_OR_ISNAN: {
290 bool isAllDefined = true;
291 double x = 0.0;
292 for (int64 i = 1; i <= n; i ++) {
293 x += (double) i;
294 isAllDefined &= ! isinf (x) && ! isnan (x);
295 //isAllDefined &= ! isfinite (x); // same
296 }
297 t = Melder_stopwatch (); // 1.29 ns
298 MelderInfo_writeLine (isAllDefined, U" ", x);
299 } break;
300 case kPraatTests::TIME_UNDEFINED_0x7FF: {
301 bool isAllDefined = true;
302 double x = 0.0;
303 for (int64 i = 1; i <= n; i ++) {
304 x += (double) i;
305 isAllDefined &= ((* (uint64 *) & x) & 0x7FF0'0000'0000'0000) != 0x7FF0'0000'0000'0000;
306 }
307 t = Melder_stopwatch (); // 0.90 ns
308 MelderInfo_writeLine (isAllDefined, U" ", x);
309 } break;
310 case kPraatTests::TIME_INNER: {
311 integer size = Melder_atoi (arg2);
312 autoVEC x = randomGauss_VEC (size, 0.0, 1.0);
313 autoVEC y = randomGauss_VEC (size, 0.0, 1.0);
314 double z = 0.0;
315 for (int64 i = 1; i <= n; i ++)
316 z += NUMinner (x.get(), y.get());
317 t = Melder_stopwatch () / size; // 2.9 Gops = 5.8 Gflops (multiplication-addition pair)
318 MelderInfo_writeLine (z);
319 } break;
320 case kPraatTests::TIME_OUTER_NUMMAT: {
321 integer nrow = 100, ncol = 100;
322 autoVEC x = randomGauss_VEC (nrow, 0.0, 1.0);
323 autoVEC y = randomGauss_VEC (ncol, 0.0, 1.0);
324 for (int64 i = 1; i <= n; i ++)
325 const autoMAT mat = outer_MAT (x.get(), y.get());
326 t = Melder_stopwatch () / nrow / ncol; // 6.1 Gops, i.e. less than one clock cycle per cell
327 } break;
328 case kPraatTests::CHECK_INVFISHERQ: {
329 MelderInfo_writeLine (NUMinvFisherQ (0.003, 1, 100000));
330 } break;
331 case kPraatTests::TIME_AUTOSTRING: {
332 conststring32 strings [6] = { U"ghdg", U"jhd", U"hkfjjd", U"fhfj", U"jhksfd", U"hfjs" };
333 int64 sumOfLengths = 0;
334 for (int64 i = 1; i <= n; i ++) {
335 int istring = i % 6;
336 autostring32 s = Melder_dup (strings [istring]);
337 sumOfLengths += length (s.transfer());
338 }
339 t = Melder_stopwatch (); // 72 ns (but 152 bytes more)
340 MelderInfo_writeLine (sumOfLengths);
341 } break;
342 case kPraatTests::TIME_CHAR32: {
343 conststring32 strings [6] = { U"ghdg", U"jhd", U"hkfjjd", U"fhfj", U"jhksfd", U"hfjs" };
344 int64 sumOfLengths = 0;
345 for (int64 i = 1; i <= n; i ++) {
346 int istring = i % 6;
347 char32 *s = Melder_dup (strings [istring]).transfer();
348 sumOfLengths += length (s);
349 }
350 t = Melder_stopwatch (); // 72 ns
351 MelderInfo_writeLine (sumOfLengths);
352 } break;
353 case kPraatTests::TIME_SUM: {
354 integer size = Melder_atoi (arg2);
355 autoVEC x = randomGauss_VEC (size, 0.0, 1.0);
356 double z = 0.0;
357 for (int64 i = 1; i <= n; i ++) {
358 double sum = NUMsum (x.get());
359 z += sum;
360 }
361 t = Melder_stopwatch () / size; // for size == 100: 0.31 ns
362 MelderInfo_writeLine (z);
363 } break;
364 case kPraatTests::TIME_MEAN: {
365 integer size = Melder_atoi (arg2);
366 autoVEC x = randomGauss_VEC (size, 0.0, 1.0);
367 double z = 0.0;
368 for (int64 i = 1; i <= n; i ++) {
369 double sum = NUMmean (x.get());
370 z += sum;
371 }
372 t = Melder_stopwatch () / size; // for size == 100: 0.34 ns
373 MelderInfo_writeLine (z);
374 } break;
375 case kPraatTests::TIME_STDEV: {
376 integer size = 10000;
377 autoVEC x = randomGauss_VEC (size, 0.0, 1.0);
378 double z = 0.0;
379 for (int64 i = 1; i <= n; i ++) {
380 double stdev = NUMstdev (x.get());
381 z += stdev;
382 }
383 t = Melder_stopwatch () / size;
384 MelderInfo_writeLine (z);
385 } break;
386 case kPraatTests::TIME_ALLOC: {
387 integer size = Melder_atoi (arg2);
388 for (int64 iteration = 1; iteration <= n; iteration ++) {
389 autoVEC result = raw_VEC (size);
390 for (integer i = 1; i <= size; i ++)
391 result [i] = 0.0;
392 }
393 t = Melder_stopwatch () / size; // 10^0..7: 70/6.9/1.08 / 0.074/0.0074/0.0091 / 0.51/0.00026 ns
394 } break;
395 case kPraatTests::TIME_ALLOC0: {
396 integer size = Melder_atoi (arg2);
397 for (int64 iteration = 1; iteration <= n; iteration ++)
398 autoVEC result = zero_VEC (size);
399 t = Melder_stopwatch () / size; // 10^0..7: 76/7.7/1.23 / 0.165/0.24/0.25 / 1.30/1.63 ns
400 } break;
401 case kPraatTests::TIME_ZERO: {
402 integer size = Melder_atoi (arg2);
403 autoVEC result = raw_VEC (size);
404 double z = 0.0;
405 for (int64 iteration = 1; iteration <= n; iteration ++) {
406 for (integer i = 1; i <= size; i ++)
407 result [i] = (double) i;
408 z += result [size - 1];
409 }
410 t = Melder_stopwatch () / size;
411 MelderInfo_writeLine (z);
412 } break;
413 case kPraatTests::TIME_MALLOC: {
414 integer size = Melder_atoi (arg2);
415 double value = Melder_atof (arg3);
416 double z = 0.0;
417 for (int64 iteration = 1; iteration <= n; iteration ++) {
418 double *result = (double *) malloc (sizeof (double) * (size_t) size);
419 for (integer i = 0; i < size; i ++)
420 result [i] = value;
421 z += result [size / 2];
422 free (result);
423 }
424 t = Melder_stopwatch () / size;
425 MelderInfo_writeLine (z);
426 } break;
427 case kPraatTests::TIME_CALLOC: {
428 integer size = Melder_atoi (arg2);
429 double z = 0.0;
430 for (integer iteration = 1; iteration <= n; iteration ++) {
431 double *result = (double *) calloc (sizeof (double), (size_t) size);
432 z += result [size / 2];
433 free (result);
434 }
435 t = Melder_stopwatch () / size;
436 MelderInfo_writeLine (z);
437 } break;
438 case kPraatTests::TIME_ADD: {
439 integer size = Melder_atoi (arg2);
440 autoMAT result = randomGauss_MAT (size, size, 0.0, 1.0);
441 Melder_stopwatch ();
442 for (integer iteration = 1; iteration <= n; iteration ++)
443 result.all() <<= 5.0;
444 t = Melder_stopwatch () / size / size; // 10^0..4: 2.7/0.16/0.24 / 0.38/0.98
445 double sum = NUMsum (result.get());
446 MelderInfo_writeLine (sum);
447 } break;
448 case kPraatTests::TIME_SIN: {
449 integer size = Melder_atoi (arg2);
450 autoMAT result = randomGauss_MAT (size, size, 0.0, 1.0);
451 Melder_stopwatch ();
452 for (integer iteration = 1; iteration <= n; iteration ++)
453 sin_MAT_inout (result.get());
454 t = Melder_stopwatch () / size / size; // 10^0..4: 18/5.3/5.2 / 5.3/12
455 double sum = NUMsum (result.get());
456 MelderInfo_writeLine (sum);
457 } break;
458 case kPraatTests::TIME_VECADD: {
459 integer size = Melder_atoi (arg2);
460 autoVEC x = randomGauss_VEC (size, 0.0, 1.0);
461 autoVEC y = randomGauss_VEC (size, 0.0, 1.0);
462 autoVEC result = raw_VEC (size);
463 Melder_stopwatch ();
464 for (integer iteration = 1; iteration <= n; iteration ++)
465 //add_VEC_out (result.all(), x.all(), y.all());
466 result.all() <<= x.all() + y.all();
467 t = Melder_stopwatch () / size;
468 double sum = NUMsum (result.get());
469 MelderInfo_writeLine (sum);
470 } break;
471 case kPraatTests::TIME_MATMUL: {
472 const integer size1 = Melder_atoi (arg2);
473 integer size2 = Melder_atoi (arg3);
474 integer size3 = Melder_atoi (arg4);
475 if (size2 == 0 || size3 == 0) size3 = size2 = size1;
476 //autoMAT const x = randomGauss_MAT (size1, size2, 0.0, 1.0);
477 //autoMAT const y = randomGauss_MAT (size2, size3, 0.0, 1.0);
478 autoMAT x = constantHH (size1, size2, 10.0);
479 autoMAT y = constantHH (size2, size3, 3.0);
480 autoMAT const result = raw_MAT (size1, size3);
481 //MAT resultget = result.get();
482 //constMAT xget = x.get(), yget = y.get();
483 MATVU const result_all = result.all();
484 constMATVU const x_all = x.all();
485 constMATVU const y_all = y.all();
486 Melder_stopwatch ();
487 for (integer iteration = 1; iteration <= n; iteration ++)
488 //MATmul_forceMetal_ (result_all, x_all, y_all);
489 _mul_allowAllocation_MAT_out (result_all, x_all, y_all);
490 const integer numberOfComputations = size1 * size2 * size3 * 2;
491 t = Melder_stopwatch () / numberOfComputations;
492 const double sum = NUMsum (result.get());
493 const integer numberOfStores = size1 * size2 + size2 * size3 + size1 * size3 + 10000;
494 MelderInfo_writeLine (double (numberOfComputations) / double (numberOfStores), U" computations per store");
495 MelderInfo_writeLine (sum, U" should be ", size1 * size2 * size3 * 30.0);
496 //Melder_require (NUMequal (result.get(), constantHH (size, size, size * 30.0).get()), U"...");
497 } break;
498 case kPraatTests::THING_AUTO: {
499 integer numberOfThingsBefore = theTotalNumberOfThings;
500 {
501 Melder_casual (U"1\n");
502 autoDaata data = Thing_new (Daata);
503 Thing_setName (data.get(), U"hello");
504 Melder_casual (U"2\n");
505 testData (data.get());
506 testAutoData (data.move());
507 autoDaata data18 = Thing_new (Daata);
508 testAutoData (data18.move());
509 fprintf (stderr, "3\n");
510 autoDaata data2 = newAutoData ();
511 fprintf (stderr, "4\n");
512 autoDaata data3 = newAutoData ();
513 fprintf (stderr, "5\n");
514 //data2 = data; // disabled l-value copy assignment from same class
515 fprintf (stderr, "6\n");
516 autoOrdered ordered = Thing_new (Ordered);
517 fprintf (stderr, "7\n");
518 //data = ordered; // disabled l-value copy assignment from subclass
519 data = ordered.move();
520 //ordered = data; // disabled l-value copy assignment from superclass
521 //ordered = data.move(); // assignment from superclass to subclass is rightfully refused by compiler
522 fprintf (stderr, "8\n");
523 data2 = newAutoData ();
524 fprintf (stderr, "8a\n");
525 autoDaata data5 = newAutoData ();
526 fprintf (stderr, "8b\n");
527 data2 = data5.move();
528 fprintf (stderr, "9\n");
529 //ordered = data; // rightfully refused by compiler
530 fprintf (stderr, "10\n");
531 //autoOrdered ordered2 = Thing_new (Daata); // rightfully refused by compiler
532 fprintf (stderr, "11\n");
533 autoDaata data4 = Thing_new (Ordered); // constructor
534 fprintf (stderr, "12\n");
535 //autoDaata data6 = data4; // disabled l-value copy constructor from same class
536 fprintf (stderr, "13\n");
537 autoDaata data7 = data4.move();
538 fprintf (stderr, "14\n");
539 autoOrdered ordered3 = Thing_new (Ordered);
540 autoDaata data8 = ordered3.move();
541 fprintf (stderr, "15\n");
542 //autoDaata data9 = ordered; // disabled l-value copy constructor from subclass
543 fprintf (stderr, "16\n");
544 autoDaata data10 = data7.move();
545 fprintf (stderr, "17\n");
546 autoDaata data11 = Thing_new (Daata); // constructor, move assignment, null destructor
547 fprintf (stderr, "18\n");
548 data11 = Thing_new (Ordered);
549 fprintf (stderr, "19\n");
550 testAutoDataRef (data11);
551 fprintf (stderr, "20\n");
552 //data11 = nullptr; // disabled implicit assignment of pointer to autopointer
553 fprintf (stderr, "21\n");
554 }
555 integer numberOfThingsAfter = theTotalNumberOfThings;
556 fprintf (stderr, "Number of things: before %td, after %td\n", numberOfThingsBefore, numberOfThingsAfter);
557 #if 0
558 MelderCallback<void,structDaata>::FunctionType f;
559 typedef void (*DataFunc) (Daata);
560 typedef void (*OrderedFunc) (Ordered);
561 DataFunc dataFun;
562 OrderedFunc orderedFun;
563 MelderCallback<void,structDaata> dataFun2 (dataFun);
564 MelderCallback<void,structOrdered> orderedFun2 (orderedFun);
565 MelderCallback<void,structDaata> dataFun3 (orderedFun);
566 //MelderCallback<void,structOrdered> orderedFun3 (dataFun); // rightfully refused by compiler
567 autoDaata data = Thing_new (Daata);
568 dataFun3 (data.get());
569 #endif
570 {
571 #if 1
572 autoMelderAsynchronous x;
573 //autoMelderAsynchronous y = x; // deleted copy constructor
574 autoMelderAsynchronous y = x.move(); // defined move constructor
575 //x = y; // deleted copy assignment
576 x = y.move(); // defined move assignment
577 autoVEC a;
578 autoVEC b = a.move();
579 const autoVEC c;
580 const autoVEC d { };
581 #if 0
582 double *e;
583 const autoVEC f { e, 10 };
584 #endif
585 {
586 autoVEC g = zero_VEC (100);
587 g [1] = 3.0;
588 VEC gg = g.get();
589 gg [2] = 4.0;
590 constVEC ggg = g.get();
591 //ggg [3] = 5.0; // should be refused by the compiler
592 const VEC gggg = g.get();
593 //gggg [3] = 6.0; // should be refused by the compiler
594 //return f; // call to deleted constructor
595 //gggg. reset(); // should be refused by the compiler
596 //ggg. reset(); // should be refused by the compiler
597 //gg. reset();
598 }
599 {
600 double x [3], *px = & x [0];
601 const double *cpx = px;
602 VEC vx (px, 2);
603 constVEC cvx (px, 2);
604 const VEC c_vx (px, 2);
605 double a = c_vx [1];
606 const double b = c_vx [2];
607 const double y = 0.0, *py = & y;
608 //VEC vy (py, 0); // ruled out: "No matching constructor for initialization of VEC" (2021-04-03)
609 constVEC cvy { py, 2 };
610 //const VEC c_vy = VEC (py, 2); // ruled out: "No matching constructor for initialization of VEC" (2021-04-03)
611 const VEC c_vy = (const VEC) VEC (const_cast<double *> (py), 2);
612 double c = c_vy [1];
613 const double d = c_vy [2];
614 //VEC c_vy2 = VEC (py, 2); // ruled out: "No matching constructor for initialization of VEC" (2021-04-03)
615 }
616 {
617 structSampled sampled {};
618 structFunction function {};
619 structFunction *pFunction = & sampled;
620 structSampled *pSampled = & sampled;
621 //structFunction **ppFunction = MelderPointerToPointerCast<structSampled> (& pFunction); // not allowed
622 structFunction **ppSampled = MelderPointerToPointerCast<structFunction> (& pSampled); // allowed
623 }
624 {
625 const structSampled sampled {};
626 const structFunction function {};
627 const structFunction *const pFunction = & sampled;
628 const structSampled *const pSampled = & sampled;
629 const structFunction *const *const ppFunction = & pFunction;
630 //const structFunction *const *const ppSampled = & pSampled; // forbidden
631 }
632
633 VEC h;
634 autoVEC j;
635 //VEC jh = j; // ruled out: "No viable conversion from autoVEC to VEC" (2021-04-03)
636 //VEC zero = zero_VEC (10); // ruled out: "No viable conversion from autoVEC to VEC" (2021-04-03)
637 //constVEC zero = zero_VEC (10); // ruled out: "No viable conversion from autoVEC to constVEC" (2021-04-03)
638 //j = h; // ruled out: "No viable overloaded '='" (2021-04-03)
639 //h = j; // ruled out: "No viable overloaded '='" (2021-04-03)
640 //h = VEC (j); // ruled out: "No matching conversion for functional-style cast from autoVEC to VEC" (2021-04-03)
641 //VEC & jref = j; // ruled out: "Non-const lvalue reference to type VEC cannot bind to a value of unrelated type autoVEC" (2021-04-03)
642 VEC *ph = & h;
643 autoVEC *pj = & j;
644 //ph = pj; // correctly ruled out: Assigning to 'VEC' from incompatible type 'autoVEC' (2021-04-03)
645 //pj = ph; // correctly ruled out: "Assigning to 'autoVEC *' from incompatible type 'VEC *' (2021-04-03)
646 #endif
647 autoSound sound = Sound_create (1, 0.0, 1.0, 10000, 0.0001, 0.0);
648 sound = Sound_create (1, 0.0, 1.0, 10000, 0.0001, 0.00005);
649 Melder_casual (U"hello ", sound -> dx);
650 autoSTRVEC v;
651 mutablestring32 *pm = v.peek2();
652 const mutablestring32 *pcm = v.peek2();
653 //conststring32 *pc = v.peek2();
654 const conststring32 *pcc = v.peek2();
655 {
656 vector<double> aa, bb;
657 vector<const double> aac, bbc;
658 //aa = aac;
659 aa = bb;
660 aac = bbc;
661 //aac = aa;
662 bbc.cells = bb.cells;
663 }
664 }
665 } break;
666 case kPraatTests::FILEINMEMORY_IO: {
667 //Melder_throw (U"Cannot test FileInMemoryManager directly from Praat.");
668 test_FileInMemory_io ();
669 } break;
670 case kPraatTests::TIME_MEDIAN: {
671 timeMedian ();
672 // Gflops is --undefined--
673 } break;
674 case kPraatTests::TIME_SLOPE_SELECTION: {
675 timeSlopeSelection ();
676 // Gflops is --undefined--
677 } break;
678 case kPraatTests::TIME_NS_DATE: {
679 #ifdef macintosh
680 NSDate *till = [NSDate dateWithTimeIntervalSinceNow: 1.0];
681 integer count = 0;
682 while ([[NSDate date] compare: till] == NSOrderedAscending)
683 ++ count;
684 MelderInfo_writeLine (count, U" per second");
685 #endif
686 } break;
687 case kPraatTests::TIME_MELDER_CLOCK: {
688 const double till = Melder_clock() + 1.0;
689 integer count = 0;
690 while (Melder_clock() < till)
691 ++ count;
692 MelderInfo_writeLine (count, U" per second");
693 } break;
694 case kPraatTests::TIME_STOPWATCH: {
695 MelderStopwatch stopwatch;
696 for (int64 i = 1; i <= n; i ++)
697 Melder_stopwatch();
698 t = stopwatch ();
699 } break;
700 }
701 MelderInfo_writeLine (Melder_single (t * 1e9 / n), U" nanoseconds per iteration");
702 MelderInfo_close ();
703 return 1;
704 }
705
706 /* More compiler stuff */
707 #if 1
708 /*
709 Trying out inheritance without encapsulation...
710 Advantage: everything is a method; therefore, the Law of Demeter is satisfied idiomatically
711 Disadvantage: problematic encapsulation
712 */
713 Thing_declare (Matrix_);
714 Thing_declare (Sound_);
715 Thing_declare (Pitch_);
716
717 /*
718 The following two sets of files have to be included
719 in Pitch_to_Sound.cpp as well as in Sound_to_Pitch.cpp,
720 but can come in either order:
721 */
722
723 /*
724 Set 1: Pitch.h
725 */
726 struct structPitch_ : structThing {
727 double f0;
728 autoSound_ toSound (); // anti-encapsulation
729 };
730
731 /*
732 Set 2: Matrix.h followed by Sound.h
733 */
734 struct structMatrix_ : structThing {
735 private: double x, y;
736 public: double getX () { return x; }
737 void setX (double newX) { x = newX; }
738 };
739 struct structSound_ : public structMatrix_ { // the definition of structSound_ requires the prior definition of structMatrix_
740 autoPitch_ toPitch (); // anti-encapsulation
741 };
742
743 /*
744 The following two files are independent of each other:
745 */
746
747 /*
748 Pitch_to_Sound.cpp:
749 #include "Pitch.h"
750 #include "Sound.h"
751 */
752 autoSound_ structPitch_::toSound () { // this requires the prior definition of structPitch_ and the prior declaration of structSound_
753 autoSound_ result = autoSound_ ();
754 result -> setX (f0); // this requires the prior definition of structSound_ and structMatrix_
755 return result;
756 }
757
758 /*
759 Sound_to_Pitch.cpp:
760 #include "Sound.h"
761 #include "Pitch.h"
762 */
763 autoPitch_ structSound_::toPitch () { // this requires the prior definition of structSound_ and the prior declaration of structPitch_
764 double x = getX (); // this requires the prior definition of structSound_ and structMatrix_
765 autoPitch_ result = autoPitch_ ();
766 result -> f0 = x; // this requires the prior definition of structPitch_
767 return result;
768 }
769
770 #endif
771
772 /*
773 An attempt to not have VEC and constVEC, but VEC and const VEC instead.
774 */
775
776 class Vec {
777 public:
778 double *at;
779 integer size;
780 const double *const_propagate_at () const { return at; }
781 double *const_propagate_at () { return at; }
782 public:
783 Vec (double *initialAt, integer initialSize) : at (initialAt), size (initialSize) { }
784 double& operator[] (integer index) { return at [index]; } // selected for Vec (1)
785 const double& operator[] (integer index) const { return at [index]; } // selected for const Vec (2)
786 //Vec (Vec& other) : at (other.const_propagate_at()), size (other.size) { }; // can assign Vec to Vec (3)
787 //Vec (Vec&& other) : at (other.at), size (other.size) { }; // can assign Vec to Vec (3)
788 Vec (Vec& other) : at (other.at), size (other.size) { }; // can assign Vec to Vec (3)
789 Vec (const Vec& other) = delete; // cannot assign const Vec to Vec (4)
790 /* unfortunately, this also precludes initializing a *const* Vec from a const Vec */
791 //Vec (const Vec& other) const = default; // attempt to copy a const Vec to a const Vec, but constructors cannot be const
792 //const Vec (const Vec& other) = default; // attempt to copy a const Vec to a const Vec, but constructors cannot have a return type
793 };
794
795 /*static void tryVec () {
796 Vec x = Vec (nullptr, 0);
797 x [1] = 3.0;
798 double a = x [2];
799 const Vec cx = Vec (nullptr, 0);
800 //cx [1] = 3.0; // should be refused by compiler, because operator[] returns a const value that cannot be assigned to (2)
801 a = cx [2]; // should be allowed by compiler, because not an assignment (2)
802 const Vec cy = x; // should be allowed (3)
803 //Vec y = cx; // should be refused (4)
804 const Vec cz = copy (x);
805 //cx.at [1] = 3.0;
806 ////const Vec ca = cy; // should be allowed
807 }*/
808
809 /* End of file Praat_tests.cpp */