xref: /freebsd/contrib/bmake/unit-tests/varmod-range.mk (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1# $NetBSD: varmod-range.mk,v 1.13 2024/07/05 19:47:22 rillig Exp $
2#
3# Tests for the :range variable modifier, which generates sequences
4# of integers from the given range.
5#
6# See also:
7#	modword.mk
8
9# The :range modifier generates a sequence of integers, one number per
10# word of the expression's value.
11.if ${a b c:L:range} != "1 2 3"
12.  error
13.endif
14
15# To preserve spaces in a word, they can be enclosed in quotes, just like
16# everywhere else.
17.if ${:U first "the second word" third 4 :range} != "1 2 3 4"
18.  error
19.endif
20
21# The :range modifier takes the number of words from the value of the
22# expression.  If that expression is undefined, the range is
23# undefined as well.  This should not come as a surprise.
24.if "${:range}" != ""
25.  error
26.endif
27
28# An empty expression results in a sequence of a single number, even though
29# the expression contains 0 words.
30.if ${:U:range} != "1"
31.  error
32.endif
33
34# The :range modifier can be given a parameter, which makes the generated
35# range independent from the value or the name of the expression.
36.if "${:range=5}" != ""
37.  error
38.endif
39# XXX: As of 2023-12-17, the ':range=n' modifier does not turn the undefined
40# expression into a defined one, even though it does not depend on the value
41# of the expression.  This looks like an oversight.
42# expect+1: Malformed conditional (${:range=5} != "")
43.if ${:range=5} != ""
44.  error
45.else
46.  error
47.endif
48
49# Negative ranges don't make sense.
50# As of 2020-11-01, they are accepted though, using up all available memory.
51#.if "${:range=-1}"
52#.  error
53#.else
54#.  error
55#.endif
56
57# The :range modifier requires a number as parameter.
58#
59# Until 2020-11-01, the parser tried to read the 'x' as a number, failed and
60# stopped there.  It then tried to parse the next modifier at that point,
61# which failed with the message "Unknown modifier".
62#
63# Since 2020-11-01, the parser issues a more precise "Invalid number" error
64# instead.
65# expect+2: while evaluating "${:U:range=x}Rest" != "Rest"" with value "": Invalid number "x}Rest" != "Rest"" for ':range' modifier
66# expect+1: Malformed conditional ("${:U:range=x}Rest" != "Rest")
67.if "${:U:range=x}Rest" != "Rest"
68.  error
69.else
70.  error
71.endif
72
73# The upper limit of the range must always be given in decimal.
74# This parse error stops at the 'x', trying to parse it as a variable
75# modifier.
76# expect+2: while evaluating "${:U:range=0x0}Rest" != "Rest"" with value "1": Unknown modifier "x0"
77# expect+1: Malformed conditional ("${:U:range=0x0}Rest" != "Rest")
78.if "${:U:range=0x0}Rest" != "Rest"
79.  error
80.else
81.  error
82.endif
83
84# As of 2020-11-01, numeric overflow is not detected.
85# Since strtoul returns ULONG_MAX in such a case, it is interpreted as a
86# very large number, consuming all available memory.
87#.if "${:U:range=18446744073709551619}Rest" != "Rest"
88#.  error
89#.else
90#.  error
91#.endif
92
93# modifier name too short
94# expect+2: while evaluating variable "a b c" with value "a b c": Unknown modifier "rang"
95# expect+1: Malformed conditional ("${a b c:L:rang}Rest" != "Rest")
96.if "${a b c:L:rang}Rest" != "Rest"
97.  error
98.else
99.  error
100.endif
101
102# misspelled modifier name
103# expect+2: while evaluating variable "a b c" with value "a b c": Unknown modifier "rango"
104# expect+1: Malformed conditional ("${a b c:L:rango}Rest" != "Rest")
105.if "${a b c:L:rango}Rest" != "Rest"
106.  error
107.else
108.  error
109.endif
110
111# modifier name too long
112# expect+2: while evaluating variable "a b c" with value "a b c": Unknown modifier "ranger"
113# expect+1: Malformed conditional ("${a b c:L:ranger}Rest" != "Rest")
114.if "${a b c:L:ranger}Rest" != "Rest"
115.  error
116.else
117.  error
118.endif
119
120all:
121