Home | History | Annotate | Line # | Download | only in unit-tests
cond-short.mk revision 1.15
      1 # $NetBSD: cond-short.mk,v 1.15 2020/12/01 19:37:23 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 # Before 2020-06-28, the right-hand side of an && or || operator was always
     10 # evaluated, which was wrong.  In cond.c 1.69 and var.c 1.197 on 2015-10-11,
     11 # Var_Parse got a new parameter named 'wantit'.  Since then it would have been
     12 # possible to skip evaluation of irrelevant variable expressions and only
     13 # parse them.  They were still evaluated though, the only difference to
     14 # relevant variable expressions was that in the irrelevant variable
     15 # expressions, undefined variables were allowed.
     16 
     17 # The && operator.
     18 
     19 .if 0 && ${echo "unexpected and" 1>&2 :L:sh}
     20 .endif
     21 
     22 .if 1 && ${echo "expected and" 1>&2 :L:sh}
     23 .endif
     24 
     25 .if 0 && exists(nonexistent${echo "unexpected and exists" 1>&2 :L:sh})
     26 .endif
     27 
     28 .if 1 && exists(nonexistent${echo "expected and exists" 1>&2 :L:sh})
     29 .endif
     30 
     31 .if 0 && empty(${echo "unexpected and empty" 1>&2 :L:sh})
     32 .endif
     33 
     34 .if 1 && empty(${echo "expected and empty" 1>&2 :L:sh})
     35 .endif
     36 
     37 # "VAR U11" is not evaluated; it was evaluated before 2020-07-02.
     38 # The whole !empty condition is only parsed and then discarded.
     39 VAR=	${VAR${:U11${echo "unexpected VAR U11" 1>&2 :L:sh}}}
     40 VAR13=	${VAR${:U12${echo "unexpected VAR13" 1>&2 :L:sh}}}
     41 .if 0 && !empty(VAR${:U13${echo "unexpected U13 condition" 1>&2 :L:sh}})
     42 .endif
     43 
     44 VAR=	${VAR${:U21${echo "unexpected VAR U21" 1>&2 :L:sh}}}
     45 VAR23=	${VAR${:U22${echo   "expected VAR23" 1>&2 :L:sh}}}
     46 .if 1 && !empty(VAR${:U23${echo   "expected U23 condition" 1>&2 :L:sh}})
     47 .endif
     48 VAR=	# empty again, for the following tests
     49 
     50 # The :M modifier is only parsed, not evaluated.
     51 # Before 2020-07-02, it was wrongly evaluated.
     52 .if 0 && !empty(VAR:M${:U${echo "unexpected M pattern" 1>&2 :L:sh}})
     53 .endif
     54 
     55 .if 1 && !empty(VAR:M${:U${echo   "expected M pattern" 1>&2 :L:sh}})
     56 .endif
     57 
     58 .if 0 && !empty(VAR:S,from,${:U${echo "unexpected S modifier" 1>&2 :L:sh}},)
     59 .endif
     60 
     61 .if 0 && !empty(VAR:C,from,${:U${echo "unexpected C modifier" 1>&2 :L:sh}},)
     62 .endif
     63 
     64 .if 0 && !empty("" == "" :? ${:U${echo "unexpected ? modifier" 1>&2 :L:sh}} :)
     65 .endif
     66 
     67 .if 0 && !empty(VAR:old=${:U${echo "unexpected = modifier" 1>&2 :L:sh}})
     68 .endif
     69 
     70 .if 0 && !empty(1 2 3:L:@var@${:U${echo "unexpected @ modifier" 1>&2 :L:sh}}@)
     71 .endif
     72 
     73 .if 0 && !empty(:U${:!echo "unexpected exclam modifier" 1>&2 !})
     74 .endif
     75 
     76 # Irrelevant assignment modifiers are skipped as well.
     77 .if 0 && ${1 2 3:L:@i@${FIRST::?=$i}@}
     78 .endif
     79 .if 0 && ${1 2 3:L:@i@${LAST::=$i}@}
     80 .endif
     81 .if 0 && ${1 2 3:L:@i@${APPENDED::+=$i}@}
     82 .endif
     83 .if 0 && ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@}
     84 .endif
     85 .if defined(FIRST) || defined(LAST) || defined(APPENDED) || defined(RAN)
     86 .  warning first=${FIRST} last=${LAST} appended=${APPENDED} ran=${RAN}
     87 .endif
     88 
     89 # The || operator.
     90 
     91 .if 1 || ${echo "unexpected or" 1>&2 :L:sh}
     92 .endif
     93 
     94 .if 0 || ${echo "expected or" 1>&2 :L:sh}
     95 .endif
     96 
     97 .if 1 || exists(nonexistent${echo "unexpected or exists" 1>&2 :L:sh})
     98 .endif
     99 
    100 .if 0 || exists(nonexistent${echo "expected or exists" 1>&2 :L:sh})
    101 .endif
    102 
    103 .if 1 || empty(${echo "unexpected or empty" 1>&2 :L:sh})
    104 .endif
    105 
    106 .if 0 || empty(${echo "expected or empty" 1>&2 :L:sh})
    107 .endif
    108 
    109 # Unreachable nested conditions are skipped completely as well.
    110 
    111 .if 0
    112 .  if ${echo "unexpected nested and" 1>&2 :L:sh}
    113 .  endif
    114 .endif
    115 
    116 .if 1
    117 .elif ${echo "unexpected nested or" 1>&2 :L:sh}
    118 .endif
    119 
    120 # make sure these do not cause complaint
    121 #.MAKEFLAGS: -dc
    122 
    123 # TODO: Rewrite this whole section and check all the conditions and variables.
    124 # Several of the assumptions are probably wrong here.
    125 # TODO: replace 'x=' with '.info' or '.error'.
    126 V42=	42
    127 iV1=	${V42}
    128 iV2=	${V66}
    129 
    130 .if defined(V42) && ${V42} > 0
    131 x=	Ok
    132 .else
    133 x=	Fail
    134 .endif
    135 x!=	echo 'defined(V42) && $${V42} > 0: $x' >&2; echo
    136 
    137 # With cond.c 1.76 from 2020-07-03, the following condition triggered a
    138 # warning: "String comparison operator should be either == or !=".
    139 # This was because the variable expression ${iV2} was defined, but the
    140 # contained variable V66 was undefined.  The left-hand side of the comparison
    141 # therefore evaluated to the string "${V66}", which is obviously not a number.
    142 #
    143 # This was fixed in cond.c 1.79 from 2020-07-09 by not evaluating irrelevant
    144 # comparisons.  Instead, they are only parsed and then discarded.
    145 #
    146 # At that time, there was not enough debug logging to see the details in the
    147 # -dA log.  To actually see it, add debug logging at the beginning and end of
    148 # Var_Parse.
    149 .if defined(V66) && ( ${iV2} < ${V42} )
    150 x=	Fail
    151 .else
    152 x=	Ok
    153 .endif
    154 # XXX: This condition doesn't match the one above. The quotes are missing
    155 # above.  This is a crucial detail since without quotes, the variable
    156 # expression ${iV2} evaluates to "${V66}", and with quotes, it evaluates to ""
    157 # since undefined variables are allowed and expand to an empty string.
    158 x!=	echo 'defined(V66) && ( "$${iV2}" < $${V42} ): $x' >&2; echo
    159 
    160 .if 1 || ${iV1} < ${V42}
    161 x=	Ok
    162 .else
    163 x=	Fail
    164 .endif
    165 x!=	echo '1 || $${iV1} < $${V42}: $x' >&2; echo
    166 
    167 # With cond.c 1.76 from 2020-07-03, the following condition triggered a
    168 # warning: "String comparison operator should be either == or !=".
    169 # This was because the variable expression ${iV2} was defined, but the
    170 # contained variable V66 was undefined.  The left-hand side of the comparison
    171 # therefore evaluated to the string "${V66}", which is obviously not a number.
    172 #
    173 # This was fixed in cond.c 1.79 from 2020-07-09 by not evaluating irrelevant
    174 # comparisons.  Instead, they are only parsed and then discarded.
    175 #
    176 # At that time, there was not enough debug logging to see the details in the
    177 # -dA log.  To actually see it, add debug logging at the beginning and end of
    178 # Var_Parse.
    179 .if 1 || ${iV2:U2} < ${V42}
    180 x=	Ok
    181 .else
    182 x=	Fail
    183 .endif
    184 x!=	echo '1 || $${iV2:U2} < $${V42}: $x' >&2; echo
    185 
    186 # the same expressions are fine when the lhs is expanded
    187 # ${iV1} expands to 42
    188 .if 0 || ${iV1} <= ${V42}
    189 x=	Ok
    190 .else
    191 x=	Fail
    192 .endif
    193 x!=	echo '0 || $${iV1} <= $${V42}: $x' >&2; echo
    194 
    195 # ${iV2:U2} expands to 2
    196 .if 0 || ${iV2:U2} < ${V42}
    197 x=	Ok
    198 .else
    199 x=	Fail
    200 .endif
    201 x!=	echo '0 || $${iV2:U2} < $${V42}: $x' >&2; echo
    202 
    203 # The right-hand side of the '&&' is irrelevant since the left-hand side
    204 # already evaluates to false.  Before cond.c 1.79 from 2020-07-09, it was
    205 # expanded nevertheless, although with a small modification:  undefined
    206 # variables may be used in these expressions without generating an error.
    207 .if defined(UNDEF) && ${UNDEF} != "undefined"
    208 .  error
    209 .endif
    210 
    211 # TODO: Test each modifier to make sure it is skipped when it is irrelevant
    212 # for the result.  Since this test is already quite long, do that in another
    213 # test.
    214 
    215 all:
    216 	@:;:
    217