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