DisjConditions.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. # Copyright (C) 2001-2017 Free Software Foundation, Inc.
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2, or (at your option)
  6. # any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use Automake::Condition qw/TRUE FALSE/;
  16. use Automake::DisjConditions;
  17. sub test_basics ()
  18. {
  19. my $true = new Automake::DisjConditions TRUE;
  20. my $false = new Automake::DisjConditions FALSE;
  21. my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
  22. my $other = new Automake::Condition "COND3_FALSE";
  23. my $another = new Automake::Condition "COND3_TRUE", "COND4_FALSE";
  24. my $set1 = new Automake::DisjConditions $cond, $other;
  25. my $set2 = new Automake::DisjConditions $other, $cond;
  26. my $set3 = new Automake::DisjConditions FALSE, $another;
  27. return 1 unless $set1 == $set2;
  28. return 1 if $set1->false;
  29. return 1 if $set1->true;
  30. return 1 unless (new Automake::DisjConditions)->false;
  31. return 1 if (new Automake::DisjConditions)->true;
  32. return 1 unless $true->human eq 'TRUE';
  33. return 1 unless $false->human eq 'FALSE';
  34. return 1 unless $set1->human eq "(COND1 and !COND2) or (!COND3)";
  35. return 1 unless $set2->human eq "(COND1 and !COND2) or (!COND3)";
  36. my $one_cond_human = $set1->one_cond->human;
  37. return 1 unless $one_cond_human eq "!COND3"
  38. || $one_cond_human eq "COND1 and !COND2";
  39. return 1 unless $set1->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
  40. my $merged1 = $set1->merge ($set2);
  41. my $merged2 = $set1->merge ($cond);
  42. my $mult1 = $set1->multiply ($set3);
  43. my $mult2 = $set1->multiply ($another);
  44. return 1 unless $merged1->simplify->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
  45. return 1 unless $merged2->simplify->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
  46. return 1 unless $mult1->string eq "COND1_TRUE COND2_FALSE COND3_TRUE COND4_FALSE";
  47. return 1 unless $mult1 == $mult2;
  48. return 0;
  49. }
  50. sub build_set (@)
  51. {
  52. my @conds = @_;
  53. my @set = ();
  54. for my $cond (@conds)
  55. {
  56. push @set, new Automake::Condition @$cond;
  57. }
  58. return new Automake::DisjConditions @set;
  59. }
  60. sub test_invert ()
  61. {
  62. my @tests = ([[["FALSE"]],
  63. [["TRUE"]]],
  64. [[["TRUE"]],
  65. [["FALSE"]]],
  66. [[["COND1_TRUE", "COND2_TRUE"],
  67. ["COND3_FALSE", "COND2_TRUE"]],
  68. [["COND2_FALSE"],
  69. ["COND1_FALSE", "COND3_TRUE"]]],
  70. [[["COND1_TRUE", "COND2_TRUE"],
  71. ["TRUE"]],
  72. [["FALSE"]]],
  73. [[["COND1_TRUE", "COND2_TRUE"],
  74. ["FALSE"]],
  75. [["COND1_FALSE"],
  76. ["COND2_FALSE"]]],
  77. [[["COND1_TRUE"],
  78. ["COND2_FALSE"]],
  79. [["COND1_FALSE", "COND2_TRUE"]]]
  80. );
  81. for my $t (@tests)
  82. {
  83. my $set = build_set @{$t->[0]};
  84. my $res = build_set @{$t->[1]};
  85. my $inv = $set->invert;
  86. if ($inv != $res)
  87. {
  88. print " (I) " . $set->string . "\n\t"
  89. . $inv->string . ' != ' . $res->string . "\n";
  90. return 1;
  91. }
  92. }
  93. return 0;
  94. }
  95. sub test_simplify ()
  96. {
  97. my @tests = ([[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  98. ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"]],
  99. [["FOO_TRUE", "BAR_FALSE"]]],
  100. [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  101. ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
  102. ["FOO_TRUE", "BAR_TRUE"]],
  103. [["FOO_TRUE"]]],
  104. [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  105. ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
  106. ["FOO_TRUE", "BAR_TRUE"],
  107. ["FOO_FALSE"]],
  108. [["TRUE"]]],
  109. [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  110. ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
  111. ["BAR_TRUE", "BAZ_TRUE"],
  112. ["BAR_FALSE", "BAZ_TRUE"]],
  113. [["BAZ_TRUE"], ["FOO_TRUE", "BAR_FALSE"]]],
  114. [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  115. ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
  116. ["BAR_TRUE", "BAZ_TRUE"],
  117. ["BAR_FALSE", "BAZ_TRUE"],
  118. ["FOO_FALSE"]],
  119. [["FOO_FALSE"], ["BAZ_TRUE"], ["BAR_FALSE"]]],
  120. [[["B_TRUE"],
  121. ["A_FALSE", "B_TRUE"]],
  122. [["B_TRUE"]]],
  123. [[["B_TRUE"],
  124. ["A_FALSE", "B_FALSE", "C_TRUE"],
  125. ["A_FALSE", "B_FALSE", "C_FALSE"]],
  126. [["A_FALSE"], ["B_TRUE"]]],
  127. [[["B_TRUE"],
  128. ["A_FALSE", "B_FALSE", "C_TRUE"],
  129. ["A_FALSE", "B_FALSE", "C_FALSE"],
  130. ["A_TRUE", "B_FALSE"]],
  131. [["TRUE"]]],
  132. [[["A_TRUE", "B_TRUE"],
  133. ["A_TRUE", "B_FALSE"],
  134. ["A_TRUE", "C_FALSE", "D_FALSE"]],
  135. [["A_TRUE"]]],
  136. [[["A_FALSE", "B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"],
  137. ["A_FALSE", "B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
  138. ["A_FALSE", "B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
  139. ["A_FALSE", "B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"],
  140. ["A_TRUE", "B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"],
  141. ["A_TRUE", "B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
  142. ["A_TRUE", "B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
  143. ["A_TRUE", "B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"]],
  144. [ ["B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"],
  145. ["B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
  146. ["B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
  147. ["B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"]]],
  148. [[["A_FALSE", "B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"],
  149. ["A_FALSE", "B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
  150. ["A_FALSE", "B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
  151. ["A_FALSE", "B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"],
  152. ["A_TRUE", "B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"],
  153. ["A_TRUE", "B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
  154. ["A_TRUE", "B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
  155. ["A_TRUE", "B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"],
  156. ["A_FALSE", "B_FALSE", "C_FALSE", "D_FALSE", "E_FALSE"],
  157. ["A_FALSE", "B_FALSE", "C_TRUE", "D_FALSE", "E_TRUE"],
  158. ["A_FALSE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE"],
  159. ["A_FALSE", "B_TRUE", "C_FALSE", "D_TRUE", "E_FALSE"],
  160. ["A_TRUE", "B_TRUE", "C_FALSE", "D_TRUE", "E_FALSE"],
  161. ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE"],
  162. ["A_TRUE", "B_FALSE", "C_TRUE", "D_FALSE", "E_TRUE"],
  163. ["A_TRUE", "B_FALSE", "C_FALSE", "D_FALSE", "E_FALSE"]],
  164. [["C_FALSE", "E_FALSE"],
  165. ["C_TRUE", "E_TRUE"]]],
  166. [[["A_FALSE"],
  167. ["A_TRUE", "B_FALSE"],
  168. ["A_TRUE", "B_TRUE", "C_FALSE"],
  169. ["A_TRUE", "B_TRUE", "C_TRUE", "D_FALSE"],
  170. ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_FALSE"],
  171. ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE", "F_FALSE"],
  172. ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE"]],
  173. [["TRUE"]]],
  174. # Simplify should work with up to 31 variables.
  175. [[["V01_TRUE", "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
  176. "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
  177. "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
  178. "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
  179. "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
  180. "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
  181. "V31_TRUE"],
  182. ["V01_TRUE", "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
  183. "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
  184. "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
  185. "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
  186. "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
  187. "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
  188. "V31_FALSE"],
  189. ["V01_FALSE","V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
  190. "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
  191. "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
  192. "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
  193. "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
  194. "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
  195. "V31_TRUE"],
  196. ["V01_FALSE","V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
  197. "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
  198. "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
  199. "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
  200. "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
  201. "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
  202. "V31_FALSE"]],
  203. [[ "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
  204. "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
  205. "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
  206. "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
  207. "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
  208. "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE"
  209. ]]]);
  210. for my $t (@tests)
  211. {
  212. my $set = build_set @{$t->[0]};
  213. my $res = build_set @{$t->[1]};
  214. # Make sure simplify() yields the expected result.
  215. my $sim = $set->simplify;
  216. if ($sim != $res)
  217. {
  218. print " (S1) " . $set->string . "\n\t"
  219. . $sim->string . ' != ' . $res->string . "\n";
  220. return 1;
  221. }
  222. # Make sure simplify() is idempotent.
  223. my $sim2 = $sim->simplify;
  224. if ($sim2 != $sim)
  225. {
  226. print " (S2) " . $sim->string . "\n\t"
  227. . $sim2->string . ' != ' . $sim->string . "\n";
  228. return 1;
  229. }
  230. # Also exercise invert() while we are at it.
  231. my $inv1 = $set->invert->simplify;
  232. my $inv2 = $sim->invert->simplify;
  233. if ($inv1 != $inv2)
  234. {
  235. print " (S3) " . $set->string . ", " . $sim->string . "\n\t"
  236. . $inv1->string . ' != ' . $inv2->string . "\n";
  237. return 1;
  238. }
  239. }
  240. return 0;
  241. }
  242. sub test_sub_conditions ()
  243. {
  244. my @tests = ([[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  245. ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
  246. ["FOO_FALSE"]],
  247. ["FOO_TRUE"],
  248. [["BAR_FALSE", "BAZ_FALSE"],
  249. ["BAR_FALSE", "BAZ_TRUE"]]],
  250. [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  251. ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
  252. ["FOO_FALSE"]],
  253. ["FOO_TRUE", "BAR_FALSE"],
  254. [["BAZ_FALSE"],
  255. ["BAZ_TRUE"]]],
  256. [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  257. ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
  258. ["FOO_FALSE"]],
  259. ["FOO_TRUE", "BAR_TRUE"],
  260. [["FALSE"]]],
  261. [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
  262. ["FOO_TRUE", "BAZ_TRUE"],
  263. ["FOO_FALSE"]],
  264. ["FOO_TRUE", "BAR_TRUE"],
  265. [["BAZ_TRUE"]]],
  266. [[["FOO_TRUE", "BAR_FALSE"],
  267. ["FOO_TRUE", "BAR_TRUE"]],
  268. ["FOO_TRUE", "BAR_TRUE"],
  269. [["TRUE"]]],
  270. [[["TRUE"]],
  271. ["TRUE"],
  272. [["TRUE"]]],
  273. [[["FALSE"]],
  274. ["TRUE"],
  275. [["FALSE"]]],
  276. [[["FALSE"]],
  277. ["FALSE"],
  278. [["FALSE"]]]);
  279. for my $t (@tests)
  280. {
  281. my $t1 = build_set @{$t->[0]};
  282. my $t2 = new Automake::Condition @{$t->[1]};
  283. my $t3 = build_set @{$t->[2]};
  284. # Make sure sub_conditions() yields the expected result.
  285. my $s = $t1->sub_conditions ($t2);
  286. if ($s != $t3)
  287. {
  288. print " (SC) " . $t1->string . "\n\t"
  289. . $s->string . ' != ' . $t3->string . "\n";
  290. return 1;
  291. }
  292. }
  293. }
  294. sub test_ambig ()
  295. {
  296. my @tests = ([[["TRUE"]],
  297. ["TRUE"],
  298. "multiply defined"],
  299. [[["C1_TRUE"]],
  300. ["C1_TRUE"],
  301. "multiply defined"],
  302. [[["TRUE"]],
  303. ["C1_FALSE"],
  304. "which includes"],
  305. [[["C1_TRUE"]],
  306. ["C1_TRUE", "C2_TRUE"],
  307. "which includes"],
  308. [[["C1_TRUE", "C2_TRUE"]],
  309. ["C2_TRUE"],
  310. "which is included in"],
  311. [[["C1_TRUE"]],
  312. ["C2_TRUE"],
  313. ''],
  314. [[["C1_TRUE"],
  315. ["C2_FALSE"]],
  316. ["C1_FALSE", "C2_TRUE"],
  317. '']);
  318. for my $t (@tests)
  319. {
  320. my $t1 = build_set @{$t->[0]};
  321. my $t2 = new Automake::Condition @{$t->[1]};
  322. my $t3 = $t->[2];
  323. my ($ans, $cond) = $t1->ambiguous_p ("FOO", $t2);
  324. if ($t3 && $ans !~ /FOO.*$t3/)
  325. {
  326. print " (A1) " . $t1->string . " vs. " . $t2->string . "\n\t"
  327. . "Error message '$ans' does not match '$t3'\n";
  328. return 1;
  329. }
  330. if (!$t3 && $ans ne '')
  331. {
  332. print " (A2) " . $t1->string . " vs. " . $t2->string . "\n\t"
  333. . "Unexpected error message: $ans\n";
  334. return 1;
  335. }
  336. }
  337. return 0;
  338. }
  339. exit (test_basics
  340. || test_invert
  341. || test_simplify
  342. || test_sub_conditions
  343. || test_ambig);
  344. ### Setup "GNU" style for perl-mode and cperl-mode.
  345. ## Local Variables:
  346. ## perl-indent-level: 2
  347. ## perl-continued-statement-offset: 2
  348. ## perl-continued-brace-offset: 0
  349. ## perl-brace-offset: 0
  350. ## perl-brace-imaginary-offset: 0
  351. ## perl-label-offset: -2
  352. ## cperl-indent-level: 2
  353. ## cperl-brace-offset: 0
  354. ## cperl-continued-brace-offset: 0
  355. ## cperl-label-offset: -2
  356. ## cperl-extra-newline-before-brace: t
  357. ## cperl-merge-trailing-else: nil
  358. ## cperl-continued-statement-offset: 2
  359. ## End: