xref: /freebsd/contrib/bmake/unit-tests/varmod-range.mk (revision 759b177aecbfc49ebc900739954ac56b1aa5fc53)
1# $NetBSD: varmod-range.mk,v 1.18 2025/04/04 18:57:01 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: Variable "" is undefined
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+1: Invalid number "x}Rest" != "Rest"" for ':range' modifier
66.if "${:U:range=x}Rest" != "Rest"
67.  error
68.else
69.  error
70.endif
71
72# The upper limit of the range must always be given in decimal.
73# This parse error stops at the 'x', trying to parse it as a variable
74# modifier.
75# expect+1: Unknown modifier ":x0"
76.if "${:U:range=0x0}Rest" != "Rest"
77.  error
78.else
79.  error
80.endif
81
82# As of 2020-11-01, numeric overflow is not detected.
83# Since strtoul returns ULONG_MAX in such a case, it is interpreted as a
84# very large number, consuming all available memory.
85#.if "${:U:range=18446744073709551619}Rest" != "Rest"
86#.  error
87#.else
88#.  error
89#.endif
90
91# modifier name too short
92# expect+1: Unknown modifier ":rang"
93.if "${a b c:L:rang}Rest" != "Rest"
94.  error
95.else
96.  error
97.endif
98
99# misspelled modifier name
100# expect+1: Unknown modifier ":rango"
101.if "${a b c:L:rango}Rest" != "Rest"
102.  error
103.else
104.  error
105.endif
106
107# modifier name too long
108# expect+1: Unknown modifier ":ranger"
109.if "${a b c:L:ranger}Rest" != "Rest"
110.  error
111.else
112.  error
113.endif
114
115all:
116