]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bmake/unit-tests/cond-short.mk
Merge bmake-20201117
[FreeBSD/FreeBSD.git] / contrib / bmake / unit-tests / cond-short.mk
1 # $NetBSD: cond-short.mk,v 1.12 2020/11/15 14:58:14 rillig Exp $
2 #
3 # Demonstrates that in conditions, the right-hand side of an && or ||
4 # is only evaluated if it can actually influence the result.
5 # This is called 'short-circuit evaluation' and is the usual evaluation
6 # mode in most programming languages.  A notable exception is Ada, which
7 # distinguishes between the operators 'And', 'And Then', 'Or', 'Or Else'.
8 #
9 # Between 2015-10-11 and 2020-06-28, the right-hand side of an && or ||
10 # operator was always evaluated, which was wrong.
11 # TODO: Had the evaluation been correct at some time before 2015-11-12?
12
13 # The && operator.
14
15 .if 0 && ${echo "unexpected and" 1>&2 :L:sh}
16 .endif
17
18 .if 1 && ${echo "expected and" 1>&2 :L:sh}
19 .endif
20
21 .if 0 && exists(nonexistent${echo "unexpected and exists" 1>&2 :L:sh})
22 .endif
23
24 .if 1 && exists(nonexistent${echo "expected and exists" 1>&2 :L:sh})
25 .endif
26
27 .if 0 && empty(${echo "unexpected and empty" 1>&2 :L:sh})
28 .endif
29
30 .if 1 && empty(${echo "expected and empty" 1>&2 :L:sh})
31 .endif
32
33 # "VAR U11" is not evaluated; it was evaluated before 2020-07-02.
34 # The whole !empty condition is only parsed and then discarded.
35 VAR=    ${VAR${:U11${echo "unexpected VAR U11" 1>&2 :L:sh}}}
36 VAR13=  ${VAR${:U12${echo "unexpected VAR13" 1>&2 :L:sh}}}
37 .if 0 && !empty(VAR${:U13${echo "unexpected U13 condition" 1>&2 :L:sh}})
38 .endif
39
40 VAR=    ${VAR${:U21${echo "unexpected VAR U21" 1>&2 :L:sh}}}
41 VAR23=  ${VAR${:U22${echo   "expected VAR23" 1>&2 :L:sh}}}
42 .if 1 && !empty(VAR${:U23${echo   "expected U23 condition" 1>&2 :L:sh}})
43 .endif
44 VAR=    # empty again, for the following tests
45
46 # The :M modifier is only parsed, not evaluated.
47 # Before 2020-07-02, it was wrongly evaluated.
48 .if 0 && !empty(VAR:M${:U${echo "unexpected M pattern" 1>&2 :L:sh}})
49 .endif
50
51 .if 1 && !empty(VAR:M${:U${echo   "expected M pattern" 1>&2 :L:sh}})
52 .endif
53
54 .if 0 && !empty(VAR:S,from,${:U${echo "unexpected S modifier" 1>&2 :L:sh}},)
55 .endif
56
57 .if 0 && !empty(VAR:C,from,${:U${echo "unexpected C modifier" 1>&2 :L:sh}},)
58 .endif
59
60 .if 0 && !empty("" == "" :? ${:U${echo "unexpected ? modifier" 1>&2 :L:sh}} :)
61 .endif
62
63 .if 0 && !empty(VAR:old=${:U${echo "unexpected = modifier" 1>&2 :L:sh}})
64 .endif
65
66 .if 0 && !empty(1 2 3:L:@var@${:U${echo "unexpected @ modifier" 1>&2 :L:sh}}@)
67 .endif
68
69 .if 0 && !empty(:U${:!echo "unexpected exclam modifier" 1>&2 !})
70 .endif
71
72 # Irrelevant assignment modifiers are skipped as well.
73 .if 0 && ${1 2 3:L:@i@${FIRST::?=$i}@}
74 .endif
75 .if 0 && ${1 2 3:L:@i@${LAST::=$i}@}
76 .endif
77 .if 0 && ${1 2 3:L:@i@${APPENDED::+=$i}@}
78 .endif
79 .if 0 && ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@}
80 .endif
81 .if defined(FIRST) || defined(LAST) || defined(APPENDED) || defined(RAN)
82 .  warning first=${FIRST} last=${LAST} appended=${APPENDED} ran=${RAN}
83 .endif
84
85 # The || operator.
86
87 .if 1 || ${echo "unexpected or" 1>&2 :L:sh}
88 .endif
89
90 .if 0 || ${echo "expected or" 1>&2 :L:sh}
91 .endif
92
93 .if 1 || exists(nonexistent${echo "unexpected or exists" 1>&2 :L:sh})
94 .endif
95
96 .if 0 || exists(nonexistent${echo "expected or exists" 1>&2 :L:sh})
97 .endif
98
99 .if 1 || empty(${echo "unexpected or empty" 1>&2 :L:sh})
100 .endif
101
102 .if 0 || empty(${echo "expected or empty" 1>&2 :L:sh})
103 .endif
104
105 # Unreachable nested conditions are skipped completely as well.
106
107 .if 0
108 .  if ${echo "unexpected nested and" 1>&2 :L:sh}
109 .  endif
110 .endif
111
112 .if 1
113 .elif ${echo "unexpected nested or" 1>&2 :L:sh}
114 .endif
115
116 # make sure these do not cause complaint
117 #.MAKEFLAGS: -dc
118
119 # TODO: Rewrite this whole section and check all the conditions and variables.
120 # Several of the assumptions are probably wrong here.
121 # TODO: replace 'x=' with '.info' or '.error'.
122 V42=    42
123 iV1=    ${V42}
124 iV2=    ${V66}
125
126 .if defined(V42) && ${V42} > 0
127 x=      Ok
128 .else
129 x=      Fail
130 .endif
131 x!=     echo 'defined(V42) && ${V42} > 0: $x' >&2; echo
132
133 # this one throws both String comparison operator and
134 # Malformed conditional with cond.c 1.78
135 # indirect iV2 would expand to "" and treated as 0
136 .if defined(V66) && ( ${iV2} < ${V42} )
137 x=      Fail
138 .else
139 x=      Ok
140 .endif
141 x!=     echo 'defined(V66) && ( "${iV2}" < ${V42} ): $x' >&2; echo
142
143 # next two thow String comparison operator with cond.c 1.78
144 # indirect iV1 would expand to 42
145 .if 1 || ${iV1} < ${V42}
146 x=      Ok
147 .else
148 x=      Fail
149 .endif
150 x!=     echo '1 || ${iV1} < ${V42}: $x' >&2; echo
151
152 .if 1 || ${iV2:U2} < ${V42}
153 x=      Ok
154 .else
155 x=      Fail
156 .endif
157 x!=     echo '1 || ${iV2:U2} < ${V42}: $x' >&2; echo
158
159 # the same expressions are fine when the lhs is expanded
160 # ${iV1} expands to 42
161 .if 0 || ${iV1} <= ${V42}
162 x=      Ok
163 .else
164 x=      Fail
165 .endif
166 x!=     echo '0 || ${iV1} <= ${V42}: $x' >&2; echo
167
168 # ${iV2:U2} expands to 2
169 .if 0 || ${iV2:U2} < ${V42}
170 x=      Ok
171 .else
172 x=      Fail
173 .endif
174 x!=     echo '0 || ${iV2:U2} < ${V42}: $x' >&2; echo
175
176 # TODO: Has this always worked?  There may have been a time, maybe around
177 # 2000, when make would complain about the "Malformed conditional" because
178 # UNDEF is not defined.
179 .if defined(UNDEF) && ${UNDEF} != "undefined"
180 .  error
181 .endif
182
183 # TODO: Test each modifier to make sure it is skipped when it is irrelevant
184 # for the result.  Since this test is already quite long, do that in another
185 # test.
186
187 all:
188         @:;: