cond-cmp-unary.mk revision 1.5 1 # $NetBSD: cond-cmp-unary.mk,v 1.5 2023/06/01 20:56:35 rillig Exp $
2 #
3 # Tests for unary comparisons in .if conditions, that is, comparisons with
4 # a single operand. If the operand is a number, it is compared to zero,
5 # if it is a string, it is tested for emptiness.
6
7 # The number 0 in all its various representations evaluates to false.
8 .if 0 || 0.0 || 0e0 || 0.0e0 || 0.0e10
9 . error
10 .endif
11
12 # Any other number evaluates to true.
13 .if !12345
14 . error
15 .endif
16
17 # The empty string evaluates to false.
18 .if ""
19 . error
20 .endif
21
22 # Any other string evaluates to true.
23 .if !"0"
24 . error
25 .endif
26
27 # The empty string may come from a variable expression.
28 #
29 # XXX: As of 2023-06-01, this empty string is interpreted "as a number" in
30 # EvalTruthy, which is plain wrong. The bug is in TryParseNumber.
31 .if ${:U}
32 . error
33 .endif
34
35 # A variable expression that is not surrounded by quotes is interpreted
36 # as a number if possible, otherwise as a string.
37 .if ${:U0}
38 . error
39 .endif
40
41 # A non-zero number from a variable expression evaluates to true.
42 .if !${:U12345}
43 . error
44 .endif
45
46 # A string of whitespace should evaluate to false.
47 #
48 # XXX: As of 2023-06-01, the implementation in EvalTruthy does not skip
49 # whitespace before testing for the end. This was probably an oversight in
50 # a commit from 1992-04-15 saying "A variable is empty when it just contains
51 # spaces".
52 .if ${:U }
53 # expect+1: This is only reached because of a bug in EvalTruthy.
54 . info This is only reached because of a bug in EvalTruthy.
55 .else
56 . error
57 .endif
58
59 # The condition '${VAR:M*}' is almost equivalent to '${VAR:M*} != ""'. The
60 # only case where they differ is for a single word whose numeric value is zero.
61 .if ${:U0:M*}
62 . error
63 .endif
64 .if ${:U0:M*} == ""
65 . error
66 .endif
67 # Multiple words cannot be parsed as a single number, thus evaluating to true.
68 .if !${:U0 0:M*}
69 . error
70 .endif
71 .if ${:U0 0:M*} == ""
72 . error
73 .endif
74
75 all: # nothing
76