xref: /freebsd/contrib/bmake/unit-tests/varname.mk (revision 9d54812421274e490dc5f0fe4722ab8d35d9b258)
1# $NetBSD: varname.mk,v 1.10 2022/02/09 21:09:24 rillig Exp $
2#
3# Tests for special variables, such as .MAKE or .PARSEDIR.
4# And for variable names in general.
5
6.MAKEFLAGS: -dv
7
8# In variable names, braces are allowed, but they must be balanced.
9# Parentheses and braces may be mixed.
10VAR{{{}}}=	3 braces
11.if "${VAR{{{}}}}" != "3 braces"
12.  error
13.endif
14
15# In variable expressions, the parser works differently.  It doesn't treat
16# braces and parentheses equally, therefore the first closing brace already
17# marks the end of the variable name.
18VARNAME=	VAR(((
19${VARNAME}=	3 open parentheses
20.if "${VAR(((}}}}" != "3 open parentheses}}}"
21.  error
22.endif
23
24# In the above test, the variable name is constructed indirectly.  Neither
25# of the following expressions produces the intended effect.
26#
27# This is not a variable assignment since the parentheses and braces are not
28# balanced.  At the end of the line, there are still 3 levels open, which
29# means the variable name is not finished.
30${:UVAR(((}=	try1
31# On the left-hand side of a variable assignments, the backslash is not parsed
32# as an escape character, therefore the parentheses still count to the nesting
33# level, which at the end of the line is still 3.  Therefore this is not a
34# variable assignment as well.
35${:UVAR\(\(\(}=	try2
36# To assign to a variable with an arbitrary name, the variable name has to
37# come from an external source, not the text that is parsed in the assignment
38# itself.  This is exactly the reason why further above, the indirect
39# ${VARNAME} works, while all other attempts fail.
40${VARNAME}=	try3
41
42.MAKEFLAGS: -d0
43
44# All variable names of a scope are stored in the same hash table, using a
45# simple hash function.  Ensure that HashTable_Find handles collisions
46# correctly and that the correct variable is looked up.  The strings "0x" and
47# "1Y" have the same hash code, as 31 * '0' + 'x' == 31 * '1' + 'Y'.
48V.0x=	0x
49V.1Y=	1Y
50.if ${V.0x} != "0x" || ${V.1Y} != "1Y"
51.  error
52.endif
53
54# The string "ASDZguv", when used as a prefix of a variable name, keeps the
55# hash code unchanged, its own hash code is 0.
56ASDZguvV.0x=	0x
57ASDZguvV.1Y=	1Y
58.if ${ASDZguvV.0x} != "0x"
59.  error
60.elif ${ASDZguvV.1Y} != "1Y"
61.  error
62.endif
63
64# Ensure that variables with the same hash code whose name is a prefix of the
65# other can be accessed.  In this case, the shorter variable name is defined
66# first to make it appear later in the bucket of the hash table.
67ASDZguv=	once
68ASDZguvASDZguv=	twice
69.if ${ASDZguv} != "once"
70.  error
71.elif ${ASDZguvASDZguv} != "twice"
72.  error
73.endif
74
75# Ensure that variables with the same hash code whose name is a prefix of the
76# other can be accessed.  In this case, the longer variable name is defined
77# first to make it appear later in the bucket of the hash table.
78ASDZguvASDZguv.param=	twice
79ASDZguv.param=		once
80.if ${ASDZguv.param} != "once"
81.  error
82.elif ${ASDZguvASDZguv.param} != "twice"
83.  error
84.endif
85
86all:
87