2
0

Condition.pl 8.9 KB

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