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