Praat_tests.cpp 24 KB

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