Condition-t.pl 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. BEGIN {
  16. use Config;
  17. if (eval { require 5.007_002; } # for CLONE support
  18. && $Config{useithreads}
  19. && !$ENV{WANT_NO_THREADS})
  20. {
  21. require threads;
  22. import threads;
  23. }
  24. else
  25. {
  26. exit 77;
  27. }
  28. }
  29. use Automake::Condition qw/TRUE FALSE/;
  30. sub test_basics ()
  31. {
  32. my @tests = (# [[Conditions], is_true?, is_false?, string, subst-string, human]
  33. [[], 1, 0, 'TRUE', '', 'TRUE'],
  34. [['TRUE'], 1, 0, 'TRUE', '', 'TRUE'],
  35. [['FALSE'], 0, 1, 'FALSE', '#', 'FALSE'],
  36. [['A_TRUE'], 0, 0, 'A_TRUE', '@A_TRUE@', 'A'],
  37. [['A_TRUE', 'B_FALSE'],
  38. 0, 0, 'A_TRUE B_FALSE', '@A_TRUE@@B_FALSE@', 'A and !B'],
  39. [['B_TRUE', 'FALSE'], 0, 1, 'FALSE', '#', 'FALSE'],
  40. [['B_TRUE', 'B_FALSE'], 0, 1, 'FALSE', '#', 'FALSE']);
  41. for (@tests)
  42. {
  43. my $a = new Automake::Condition @{$_->[0]};
  44. return 1
  45. if threads->new(sub {
  46. return 1 if $_->[1] != $a->true;
  47. return 1 if $_->[1] != ($a == TRUE);
  48. return 1 if $_->[2] != $a->false;
  49. return 1 if $_->[2] != ($a == FALSE);
  50. return 1 if $_->[3] ne $a->string;
  51. return 1 if $_->[4] ne $a->subst_string;
  52. return 1 if $_->[5] ne $a->human;
  53. })->join;
  54. }
  55. return 0;
  56. }
  57. sub test_true_when ()
  58. {
  59. my $failed = 0;
  60. my @tests = (# [When,
  61. # [Implied-Conditions],
  62. # [Not-Implied-Conditions]]
  63. [['TRUE'],
  64. [['TRUE']],
  65. [['A_TRUE'], ['A_TRUE', 'B_FALSE'], ['FALSE']]],
  66. [['A_TRUE'],
  67. [['TRUE'], ['A_TRUE']],
  68. [['A_TRUE', 'B_FALSE'], ['FALSE']]],
  69. [['A_TRUE', 'B_FALSE'],
  70. [['TRUE'], ['A_TRUE'], ['B_FALSE'], ['A_TRUE', 'B_FALSE']],
  71. [['FALSE'], ['C_FALSE'], ['C_FALSE', 'A_TRUE']]]);
  72. for my $t (@tests)
  73. {
  74. my $a = new Automake::Condition @{$t->[0]};
  75. return 1
  76. if threads->new(sub {
  77. for my $u (@{$t->[1]})
  78. {
  79. my $b = new Automake::Condition @$u;
  80. return threads->new(sub {
  81. if (! $b->true_when ($a))
  82. {
  83. print "`" . $b->string .
  84. "' not implied by `" . $a->string . "'?\n";
  85. $failed = 1;
  86. }
  87. })->join;
  88. }
  89. for my $u (@{$t->[2]})
  90. {
  91. my $b = new Automake::Condition @$u;
  92. return threads->new(sub {
  93. if ($b->true_when ($a))
  94. {
  95. print "`" . $b->string .
  96. "' implied by `" . $a->string . "'?\n";
  97. $failed = 1;
  98. }
  99. return threads->new(sub {
  100. return 1 if $b->true_when ($a);
  101. })->join;
  102. })->join;
  103. }
  104. })->join;
  105. }
  106. return $failed;
  107. }
  108. sub test_reduce_and ()
  109. {
  110. my @tests = (# If no conditions are given, TRUE should be returned
  111. [[], ["TRUE"]],
  112. # An empty condition is TRUE
  113. [[""], ["TRUE"]],
  114. # A single condition should be passed through unchanged
  115. [["FOO"], ["FOO"]],
  116. [["FALSE"], ["FALSE"]],
  117. [["TRUE"], ["TRUE"]],
  118. # TRUE and false should be discarded and overwhelm
  119. # the result, respectively
  120. [["FOO", "TRUE"], ["FOO"]],
  121. [["FOO", "FALSE"], ["FALSE"]],
  122. # Repetitions should be removed
  123. [["FOO", "FOO"], ["FOO"]],
  124. [["TRUE", "FOO", "FOO"], ["FOO"]],
  125. [["FOO", "TRUE", "FOO"], ["FOO"]],
  126. [["FOO", "FOO", "TRUE"], ["FOO"]],
  127. # Two different conditions should be preserved,
  128. # but TRUEs should be removed
  129. [["FOO", "BAR"], ["BAR,FOO"]],
  130. [["TRUE", "FOO", "BAR"], ["BAR,FOO"]],
  131. [["FOO", "TRUE", "BAR"], ["BAR,FOO"]],
  132. [["FOO", "BAR", "TRUE"], ["BAR,FOO"]],
  133. # A condition implied by another condition should be removed.
  134. [["FOO BAR", "BAR"], ["FOO BAR"]],
  135. [["BAR", "FOO BAR"], ["FOO BAR"]],
  136. [["TRUE", "FOO BAR", "BAR"], ["FOO BAR"]],
  137. [["FOO BAR", "TRUE", "BAR"], ["FOO BAR"]],
  138. [["FOO BAR", "BAR", "TRUE"], ["FOO BAR"]],
  139. [["BAR FOO", "BAR"], ["BAR FOO"]],
  140. [["BAR", "BAR FOO"], ["BAR FOO"]],
  141. [["TRUE", "BAR FOO", "BAR"], ["BAR FOO"]],
  142. [["BAR FOO", "TRUE", "BAR"], ["BAR FOO"]],
  143. [["BAR FOO", "BAR", "TRUE"], ["BAR FOO"]],
  144. # Check that reduction happens even when there are
  145. # two conditions to remove.
  146. [["FOO", "FOO BAR", "BAR"], ["FOO BAR"]],
  147. [["FOO", "FOO BAR", "BAZ", "FOO BAZ"], ["FOO BAR", "FOO BAZ"]],
  148. [["FOO", "FOO BAR", "BAZ", "FOO BAZ", "FOO BAZ BAR"],
  149. ["FOO BAZ BAR"]],
  150. # Duplicated conditionals should be removed.
  151. [["FOO", "BAR", "BAR"], ["BAR,FOO"]],
  152. # Equivalent conditions in different forms should be
  153. # reduced: which one is left is unfortunately order
  154. # dependent.
  155. [["BAR FOO", "FOO BAR"], ["FOO BAR"]],
  156. [["FOO BAR", "BAR FOO"], ["BAR FOO"]]);
  157. my $failed = 0;
  158. foreach (@tests)
  159. {
  160. my ($inref, $outref) = @$_;
  161. my @inconds = map { new Automake::Condition $_ } @$inref;
  162. return 1
  163. if threads->new(sub {
  164. my @outconds = map { (new Automake::Condition $_)->string } @$outref;
  165. return threads->new(sub {
  166. my @res =
  167. map { $_->string } (Automake::Condition::reduce_and (@inconds));
  168. return threads->new(sub {
  169. my $result = join (",", sort @res);
  170. my $exresult = join (",", @outconds);
  171. if ($result ne $exresult)
  172. {
  173. print '"' . join(",", @$inref) . '" => "' .
  174. $result . '" expected "' .
  175. $exresult . '"' . "\n";
  176. $failed = 1;
  177. }
  178. return $failed;
  179. })->join;
  180. })->join;
  181. })->join;
  182. }
  183. return $failed;
  184. }
  185. sub test_reduce_or ()
  186. {
  187. my @tests = (# If no conditions are given, FALSE should be returned
  188. [[], ["FALSE"]],
  189. # An empty condition is TRUE
  190. [[""], ["TRUE"]],
  191. # A single condition should be passed through unchanged
  192. [["FOO"], ["FOO"]],
  193. [["FALSE"], ["FALSE"]],
  194. [["TRUE"], ["TRUE"]],
  195. # FALSE and TRUE should be discarded and overwhelm
  196. # the result, respectively
  197. [["FOO", "TRUE"], ["TRUE"]],
  198. [["FOO", "FALSE"], ["FOO"]],
  199. # Repetitions should be removed
  200. [["FOO", "FOO"], ["FOO"]],
  201. [["FALSE", "FOO", "FOO"], ["FOO"]],
  202. [["FOO", "FALSE", "FOO"], ["FOO"]],
  203. [["FOO", "FOO", "FALSE"], ["FOO"]],
  204. # Two different conditions should be preserved,
  205. # but FALSEs should be removed
  206. [["FOO", "BAR"], ["BAR,FOO"]],
  207. [["FALSE", "FOO", "BAR"], ["BAR,FOO"]],
  208. [["FOO", "FALSE", "BAR"], ["BAR,FOO"]],
  209. [["FOO", "BAR", "FALSE"], ["BAR,FOO"]],
  210. # A condition implying another condition should be removed.
  211. [["FOO BAR", "BAR"], ["BAR"]],
  212. [["BAR", "FOO BAR"], ["BAR"]],
  213. [["FALSE", "FOO BAR", "BAR"], ["BAR"]],
  214. [["FOO BAR", "FALSE", "BAR"], ["BAR"]],
  215. [["FOO BAR", "BAR", "FALSE"], ["BAR"]],
  216. [["BAR FOO", "BAR"], ["BAR"]],
  217. [["BAR", "BAR FOO"], ["BAR"]],
  218. [["FALSE", "BAR FOO", "BAR"], ["BAR"]],
  219. [["BAR FOO", "FALSE", "BAR"], ["BAR"]],
  220. [["BAR FOO", "BAR", "FALSE"], ["BAR"]],
  221. # Check that reduction happens even when there are
  222. # two conditions to remove.
  223. [["FOO", "FOO BAR", "BAR"], ["BAR,FOO"]],
  224. [["FOO", "FOO BAR", "BAZ", "FOO BAZ"], ["BAZ,FOO"]],
  225. [["FOO", "FOO BAR", "BAZ", "FOO BAZ", "FOO BAZ BAR"],
  226. ["BAZ,FOO"]],
  227. # Duplicated conditionals should be removed.
  228. [["FOO", "BAR", "BAR"], ["BAR,FOO"]],
  229. # Equivalent conditions in different forms should be
  230. # reduced: which one is left is unfortunately order
  231. # dependent.
  232. [["BAR FOO", "FOO BAR"], ["FOO BAR"]],
  233. [["FOO BAR", "BAR FOO"], ["BAR FOO"]]);
  234. my $failed = 0;
  235. foreach (@tests)
  236. {
  237. my ($inref, $outref) = @$_;
  238. my @inconds = map { new Automake::Condition $_ } @$inref;
  239. return 1
  240. if threads->new(sub {
  241. my @outconds = map { (new Automake::Condition $_)->string } @$outref;
  242. return threads->new(sub {
  243. my @res =
  244. map { $_->string } (Automake::Condition::reduce_or (@inconds));
  245. return threads->new(sub {
  246. my $result = join (",", sort @res);
  247. my $exresult = join (",", @outconds);
  248. if ($result ne $exresult)
  249. {
  250. print '"' . join(",", @$inref) . '" => "' .
  251. $result . '" expected "' .
  252. $exresult . '"' . "\n";
  253. $failed = 1;
  254. }
  255. return $failed;
  256. })->join;
  257. })->join;
  258. })->join;
  259. }
  260. return $failed;
  261. }
  262. sub test_merge ()
  263. {
  264. my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
  265. return threads->new(sub {
  266. my $other = new Automake::Condition "COND3_FALSE";
  267. return threads->new(sub {
  268. my $both = $cond->merge ($other);
  269. return threads->new(sub {
  270. my $both2 = $cond->merge_conds ("COND3_FALSE");
  271. return threads->new(sub {
  272. $cond = $both->strip ($other);
  273. my @conds = $cond->conds;
  274. return 1 if $both->string ne "COND1_TRUE COND2_FALSE COND3_FALSE";
  275. return 1 if $cond->string ne "COND1_TRUE COND2_FALSE";
  276. return 1 if $both != $both2;
  277. })->join;
  278. })->join;
  279. })->join;
  280. })->join;
  281. return 0;
  282. }
  283. exit (test_basics
  284. || test_true_when
  285. || test_reduce_and
  286. || test_reduce_or
  287. || test_merge);
  288. ### Setup "GNU" style for perl-mode and cperl-mode.
  289. ## Local Variables:
  290. ## perl-indent-level: 2
  291. ## perl-continued-statement-offset: 2
  292. ## perl-continued-brace-offset: 0
  293. ## perl-brace-offset: 0
  294. ## perl-brace-imaginary-offset: 0
  295. ## perl-label-offset: -2
  296. ## cperl-indent-level: 2
  297. ## cperl-brace-offset: 0
  298. ## cperl-continued-brace-offset: 0
  299. ## cperl-label-offset: -2
  300. ## cperl-extra-newline-before-brace: t
  301. ## cperl-merge-trailing-else: nil
  302. ## cperl-continued-statement-offset: 2
  303. ## End: