Prefer HTTPS to FTP and HTTP
[autoconf.git] / tests / m4sugar.at
blob57bd293858c50f387cd50dde0fb284cc385fb60f
1 #                                                       -*- Autotest -*-
3 AT_BANNER([M4sugar.])
5 # Copyright (C) 2000-2002, 2005-2017 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
21 # AT_CHECK_M4SUGAR_TEXT(CODE, STDOUT, STDERR)
22 # -------------------------------------------
23 # Check that m4sugar CODE expands to STDOUT and emits STDERR.
24 m4_define([AT_CHECK_M4SUGAR_TEXT],
26 AT_DATA_M4SUGAR([script.4s],
27 [[m4_init
28 m4_divert_push([])[]dnl
29 ]$1[[]dnl
30 m4_divert_pop([])
31 ]])
33 AT_CHECK_M4SUGAR([-o-],, [$2], [$3])
34 ])# AT_CHECK_M4SUGAR_TEXT
37 ## ------------------ ##
38 ## m4_stack_foreach.  ##
39 ## ------------------ ##
41 AT_SETUP([m4@&t@_stack])
43 AT_KEYWORDS([m4@&t@_stack_foreach m4@&t@_stack_foreach_lifo])
44 AT_KEYWORDS([m4@&t@_stack_foreach_sep m4@&t@_stack_foreach_sep_lifo])
45 AT_KEYWORDS([m4@&t@_copy m4@&t@_n])
47 # Test the semantics of macros to walk stacked macro definitions.
48 AT_CHECK_M4SUGAR_TEXT([[dnl
49 m4_pushdef([abc], [def])dnl
50 m4_pushdef([abc], [ghi])dnl
51 m4_pushdef([abc], [jkl])dnl
52 m4_stack_foreach([abc], [m4_n])
53 abc
54 m4_stack_foreach_lifo([abc], [m4_n])
55 m4_stack_foreach([abc], [m4_n])
56 m4_copy([abc], [foo])dnl
57 m4_stack_foreach([foo], [m4_n])
58 m4_stack_foreach_lifo([foo], [m4_n])
59 m4_stack_foreach_sep([abc], [ m4_index([abcdefghijkl],], [)])
60 m4_define([colon], [:])m4_define([lt], [<])m4_define([gt], [>])dnl
61 m4_stack_foreach_sep_lifo([abc], [lt], [gt], [colon])
62 m4_pushdef([xyz], [123])dnl
63 m4_pushdef([xyz], [456])dnl
64 m4_define([doit], [[$1](m4_stack_foreach_sep([xyz], [m4_dquote(], [)], [,]))
65 ])dnl
66 m4_stack_foreach([abc], [doit])]],
67 [[def
68 ghi
69 jkl
71 jkl
72 jkl
73 ghi
74 def
76 def
77 ghi
78 jkl
80 def
81 ghi
82 jkl
84 jkl
85 ghi
86 def
88  3 6 9
89 <jkl>:<ghi>:<def>
90 def([123],[456])
91 ghi([123],[456])
92 jkl([123],[456])
93 ]])
95 AT_CLEANUP
98 ## --------- ##
99 ## m4_defn.  ##
100 ## --------- ##
102 AT_SETUP([m4@&t@_defn])
104 AT_KEYWORDS([m4@&t@_popdef m4@&t@_undefine m4@&t@_copy m4@&t@_rename
105 m4@&t@_copy_force m4@&t@_rename_force])
107 # Ensure that m4sugar dies when dereferencing undefined macros, whether
108 # this is provided by m4 natively or faked by wrappers in m4sugar.
110 AT_DATA_M4SUGAR([script.4s],
111 [[m4_define([good])
112 m4_defn([good], [oops])
115 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
116 AT_CHECK([grep good stderr], [1])
117 AT_CHECK([grep 'm4@&t@_defn: undefined.*oops' stderr], [0], [ignore])
119 AT_DATA_M4SUGAR([script.4s],
120 [[m4_define([good])
121 m4_popdef([good], [oops])
124 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
125 AT_CHECK([grep good stderr], [1])
126 AT_CHECK([grep 'm4@&t@_popdef: undefined.*oops' stderr], [0], [ignore])
128 AT_DATA_M4SUGAR([script.4s],
129 [[m4_define([good])
130 m4_undefine([good], [oops])
133 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
134 AT_CHECK([grep good stderr], [1])
135 AT_CHECK([grep 'm4@&t@_undefine: undefined.*oops' stderr], [0], [ignore])
137 # Cannot rename an undefined macro.
138 AT_DATA_M4SUGAR([script.4s],
139 [[m4_rename([oops], [good])
142 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
143 AT_CHECK([grep 'm4@&t@_undefine: undefined.*oops' stderr], [0], [ignore])
145 # Check that pushdef stacks can be renamed.
146 AT_CHECK_M4SUGAR_TEXT([[m4_pushdef([a], [1])dnl
147 m4_pushdef([a], [2])dnl
148 m4_pushdef([a], m4_defn([m4_divnum]))dnl
149 a b c
150 m4_rename([a], [b])dnl
151 a b c
152 m4_copy([b], [c])dnl
153 a b c
154 m4_popdef([b], [c])dnl
155 a b c
156 m4_popdef([b], [c])dnl
157 a b c
158 m4_popdef([b], [c])dnl
159 a b c
160 dnl m4_copy is intentionally a no-op on undefined source
161 m4_copy([oops], [dummy])m4_ifdef([dummy], [[oops]])dnl
162 dnl allow forceful overwrites
163 m4_define([d], [4])m4_define([e], [5])m4_define([f], [6])dnl
164 m4_copy_force([d], [e])dnl
165 m4_rename_force([d], [f])dnl
166 d e f
167 m4_popdef([e], [f])dnl
168 d e f
169 ]], [[0 b c
170 a 0 c
171 a 0 0
172 a 2 2
173 a 1 1
174 a b c
175 d 4 4
176 d e f
179 AT_CLEANUP
182 ## ------------ ##
183 ## m4_dumpdef.  ##
184 ## ------------ ##
186 AT_SETUP([m4@&t@_dumpdef])
188 AT_KEYWORDS([m4@&t@_dumpdefs])
190 # Ensure that m4sugar dies when dereferencing undefined macros.
192 AT_DATA_M4SUGAR([script.4s],
193 [[m4_define([good], [yep])
194 m4_dumpdef([good], [oops])
197 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
198 AT_CHECK([grep '^good:  \[[yep\]]$' stderr], [0], [ignore])
199 AT_CHECK([grep 'm4@&t@_dumpdef: undefined.*oops' stderr], [0], [ignore])
201 # Check that pushdef stacks can be dumped.
202 AT_CHECK_M4SUGAR_TEXT([[m4_divert_push([KILL])
203 m4_pushdef([a], [1])
204 m4_pushdef([a], [2])
205 m4_dumpdef([a])
206 m4_dumpdefs([oops], [a])
207 m4_divert_pop([KILL])dnl
208 ]], [],
209 [[a:    [2]
210 a:      [2]
211 a:      [1]
214 # Check behavior when dumping builtins.  Unfortunately, when using M4 1.4.x
215 # (or more precisely, when __m4_version__ is undefined), builtins get
216 # flattened to an empty string.  It takes M4 1.6 to work around this.
217 AT_DATA_M4SUGAR([script.4s],
218 [[m4_ifdef([__m4_version__], [_m4_undefine([__m4_version__])])
219 m4_init
220 m4_dumpdef([m4_define])
223 AT_CHECK_M4SUGAR([-o-], [0], [],
224 [[m4_define:    []
227 AT_DATA_M4SUGAR([script.4s],
228 [[m4_init
229 m4_ifdef([__m4_version__],
230 [m4_dumpdef([m4_define])],
231 [m4_errprintn([m4_define:       <define>])])
234 AT_CHECK_M4SUGAR([-o-], [0], [],
235 [[m4_define:    <define>
238 AT_CLEANUP
241 ## --------- ##
242 ## m4_warn.  ##
243 ## --------- ##
245 AT_SETUP([m4@&t@_warn])
247 AT_DATA_M4SUGAR([script.4s],
248 [[m4_init
249 m4_defun([cross_warning], [m4_warn([cross], [cross])])
251 m4_divert([0])dnl
252 m4_warn([obsolete], [obsolete])dnl
253 cross_warning[]dnl
254 m4_warn([syntax], [syntax])dnl
255 cross_warning[]dnl
256 m4_warn([syntax], [syntax])dnl
259 AT_CHECK_M4SUGAR([-o-], 0, [],
260 [script.4s:4: warning: prefer named diversions
261 script.4s:7: warning: syntax
262 script.4s:9: warning: syntax
265 AT_CHECK_M4SUGAR([-o- -Wall], 0, [],
266 [script.4s:4: warning: prefer named diversions
267 script.4s:5: warning: obsolete
268 script.4s:6: warning: cross
269 script.4s:2: cross_warning is expanded from...
270 script.4s:6: the top level
271 script.4s:7: warning: syntax
272 script.4s:8: warning: cross
273 script.4s:2: cross_warning is expanded from...
274 script.4s:8: the top level
275 script.4s:9: warning: syntax
278 AT_CHECK_M4SUGAR([-o- -Wnone,cross], 0, [],
279 [script.4s:6: warning: cross
280 script.4s:2: cross_warning is expanded from...
281 script.4s:6: the top level
282 script.4s:8: warning: cross
283 script.4s:2: cross_warning is expanded from...
284 script.4s:8: the top level
287 AT_CHECK_M4SUGAR([-o- -Wnone,cross,error], 1, [],
288 [[script.4s:6: warning: cross
289 script.4s:2: cross_warning is expanded from...
290 script.4s:6: the top level
291 script.4s:8: warning: cross
292 script.4s:2: cross_warning is expanded from...
293 script.4s:8: the top level
296 AT_CLEANUP
299 ## ----------------- ##
300 ## m4_divert_stack.  ##
301 ## ----------------- ##
303 AT_SETUP([m4@&t@_divert_stack])
304 AT_KEYWORDS([m4@&t@_divert m4@&t@_divert_push m4@&t@_divert_pop
305 m4@&t@_undivert m4@&t@_cleardivert m4@&t@_divert_text])
307 dnl This test names some diversions to avoid a warning.
308 AT_CHECK_M4SUGAR_TEXT([[m4_define([_m4_divert(ten)], [10])dnl
309 m4_define([_m4_divert(twenty)], [20])dnl
310 m4_define([_m4_divert(thirty)], [30])dnl
311 1.m4_divert_stack
312 m4_divert_push([ten])2.m4_divert_stack
313 m4_divert_text([twenty], [3.m4_divert_stack])dnl
314 m4_divert([thirty])4.m4_divert_stack
315 m4_divert_pop([thirty])dnl
316 5.m4_undivert([twenty], [thirty])
317 m4_pattern_allow([^m4_divert])dnl
318 ]], [[1.script.4s:2: m4@&t@_divert_push:
319 script.4s:1: m4@&t@_divert: KILL
320 5.3.script.4s:8: m4@&t@_divert_push: twenty
321 script.4s:7: m4@&t@_divert_push: ten
322 script.4s:2: m4@&t@_divert_push:
323 script.4s:1: m4@&t@_divert: KILL
324 4.script.4s:9: m4@&t@_divert: thirty
325 script.4s:2: m4@&t@_divert_push:
326 script.4s:1: m4@&t@_divert: KILL
328 2.script.4s:7: m4@&t@_divert_push: ten
329 script.4s:2: m4@&t@_divert_push:
330 script.4s:1: m4@&t@_divert: KILL
333 AT_CHECK_M4SUGAR_TEXT([[dnl
334 m4_divert_text([3], [three])dnl
335 m4_divert_text([4], [four])dnl
336 m4_divert_text([1], [one])dnl
337 m4_divert_text([2], [two])dnl
338 m4_cleardivert([2], [3])dnl
340 [[one
341 four
343 [[script.4s:4: warning: prefer named diversions
344 script.4s:5: warning: prefer named diversions
345 script.4s:6: warning: prefer named diversions
346 script.4s:7: warning: prefer named diversions
347 script.4s:8: warning: prefer named diversions
350 AT_DATA_M4SUGAR([script.4s],
351 [[m4_divert_pop
353 AT_CHECK_M4SUGAR([-o-], [1], [],
354 [[script.4s:1: error: too many m4@&t@_divert_pop
355 script.4s:1: the top level
356 autom4te: m4 failed with exit status: 1
359 AT_DATA_M4SUGAR([script.4s],
360 [[m4_init
361 m4_divert_push([1])
362 m4_divert_pop([2])
364 AT_CHECK_M4SUGAR([-o-], [1], [],
365 [[script.4s:3: error: m4@&t@_divert_pop(2): diversion mismatch:
366 script.4s:2: m4@&t@_divert_push: 1
367 script.4s:1: m4@&t@_divert: KILL
368 script.4s:3: the top level
369 autom4te: m4 failed with exit status: 1
372 AT_DATA_M4SUGAR([script.4s],
373 [[m4_divert([1])
374 m4_init
375 m4_divert_push([2])
377 AT_CHECK_M4SUGAR([-o-], [1], [],
378 [[script.4s:2: error: m4@&t@_init: unbalanced m4@&t@_divert_push:
379 script.4s:3: m4@&t@_divert_push: 2
380 script.4s:2: m4@&t@_divert: KILL
381 script.4s:2: the top level
382 autom4te: m4 failed with exit status: 1
385 AT_CLEANUP
388 ## -------------------- ##
389 ## m4_expansion_stack.  ##
390 ## -------------------- ##
392 AT_SETUP([m4@&t@_expansion_stack])
394 AT_CHECK_M4SUGAR_TEXT([[1.m4_expansion_stack
395 m4_defun([a], [b])dnl
396 m4_define([c], [d])dnl
397 m4_defun([d], [2.m4_expansion_stack])dnl
398 m4_defun([b], [c])dnl
400 3.m4_ifdef([_m4_expansion_stack], [m4_expansion_stack])
401 ]], [[1.script.4s:3: the top level
402 2.script.4s:6: d is expanded from...
403 script.4s:7: b is expanded from...
404 script.4s:4: a is expanded from...
405 script.4s:8: the top level
409 AT_CLEANUP
412 ## --------------------------- ##
413 ## m4_require: error message.  ##
414 ## --------------------------- ##
416 AT_SETUP([m4@&t@_require: error message])
417 AT_KEYWORDS([m4@&t@_require])
419 AT_DATA_M4SUGAR([script.4s],
420 [[m4_defun([foo], [FOO])
421 m4_require([foo])
424 AT_CHECK_M4SUGAR([], 1, [],
425 [[script.4s:2: error: m4@&t@_require(foo): cannot be used outside of an m4_defun'd macro
426 script.4s:2: the top level
427 autom4te: m4 failed with exit status: 1
429 AT_CLEANUP
432 ## ----------------------------- ##
433 ## m4_require: warning message.  ##
434 ## ----------------------------- ##
436 AT_SETUP([m4@&t@_require: warning message])
437 AT_KEYWORDS([m4@&t@_require m4@&t@_require_silent_probe])
439 # Mirror the job of aclocal on a typical scenario: the user invokes a
440 # single macro that comes from one included file, which in turn requires
441 # another macro from a second file.  When using the incomplete set of
442 # files, we want a warning, unless we are merely learning which additional
443 # macros are needed in order to regenerate the list of files to include.
444 AT_DATA_M4SUGAR([script.4s],
445 [[m4_init
446 m4_include([script1.4s])
450 AT_DATA_M4SUGAR([script1.4s],
451 [[m4_defun([foo], [m4_require([bar])])
454 AT_DATA_M4SUGAR([script2.4s],
455 [[m4_defun([bar], [BAR])
458 AT_CHECK_M4SUGAR([], [0], [],
459 [[script.4s:3: warning: bar is m4@&t@_require'd but not m4@&t@_defun'd
460 script1.4s:1: foo is expanded from...
461 script.4s:3: the top level
464 # Inline expansion of AT_CHECK_M4SUGAR, mirroring how aclocal will
465 # inject a definition of our witness macro for a silent run.
466 echo 'm4@&t@_define([m4@&t@_require_silent_probe])' |
467   AT_CHECK_AUTOM4TE([--language=m4sugar - script.4s -o script],
468 [0], [], [])
470 # Now that we have recomputed the set of include files, things should work.
471 AT_DATA_M4SUGAR([script.4s],
472 [[m4_init
473 m4_include([script1.4s])
474 m4_include([script2.4s])
478 AT_CHECK_M4SUGAR([], [0], [], [])
480 AT_CLEANUP
483 ## ----------------------------------- ##
484 ## m4_require: circular dependencies.  ##
485 ## ----------------------------------- ##
487 AT_SETUP([m4@&t@_require: circular dependencies])
488 AT_KEYWORDS([m4@&t@_require])
490 AT_DATA_M4SUGAR([script.4s],
491 [[m4_defun([foo], [m4_require([bar])])
493 m4_defun([bar], [m4_require([foo])])
495 m4_defun([baz], [m4_require([foo])])
497 m4_init
498 m4_divert([0])dnl
502 AT_CHECK_M4SUGAR([], 1, [],
503 [[script.4s:9: error: m4@&t@_require: circular dependency of foo
504 script.4s:3: bar is expanded from...
505 script.4s:1: foo is expanded from...
506 script.4s:5: baz is expanded from...
507 script.4s:9: the top level
508 autom4te: m4 failed with exit status: 1
510 AT_CLEANUP
513 ## ---------------------- ##
514 ## m4_require: one-shot.  ##
515 ## ---------------------- ##
517 AT_SETUP([m4@&t@_require: one-shot initialization])
518 AT_KEYWORDS([m4@&t@_require])
519 AT_KEYWORDS([m4@&t@_defun_init m4@&t@_copy m4@&t@_defun_once])
521 dnl check out m4_defun_init, m4_copy, and odd macro names
522 AT_CHECK_M4SUGAR_TEXT([[
523 m4_define([t], [text])dnl
524 m4_defun_init([a], [[init a
525 ]], [[common a] t])dnl
526 m4_defun([b], [[b]m4_require([a])])dnl
527 m4_defun([c], [[c]m4_require([a])])dnl
530 a()dnl
532 m4_defun_init([-], [hello, ], [m4_if([$#], [0], [world], [[$1]])])dnl
533 m4_copy([-], [.])dnl
534 m4_indir([.])
535 m4_indir([.], [goodbye])
536 m4_indir([-], [again])
537 ]], [[
538 init a
539 common a text
542 common a text
543 hello, world
544 goodbye
545 hello, again
548 dnl Check m4_defun_once behavior
549 AT_CHECK_M4SUGAR_TEXT([[
550 m4_defun_once([a], [[a]])dnl
551 m4_defun([b], [[b]m4_require([a])])dnl
552 m4_defun([c], [[c]a[]m4_require([b])])dnl
555 m4_defun_once([d], [[d]m4_require([a])])dnl
557 m4_defun_once([e], [[e]])dnl
558 m4_defun([f], [[f]m4_require([e])e])dnl
560 ]], [[
571 AT_CLEANUP
574 ## -------------------- ##
575 ## m4_require: nested.  ##
576 ## -------------------- ##
578 AT_SETUP([m4@&t@_require: nested])
579 AT_KEYWORDS([m4@&t@_require m4@&t@_defun])
581 dnl From the m4sugar.m4 discourse: Require chains, top level
582 AT_CHECK_M4SUGAR_TEXT([[dnl
583 m4_defun([a], [[a]])dnl aka TEST2a
584 m4_defun([b], [[b]m4_require([a])])dnl aka TEST3
585 m4_defun([c], [[c]m4_require([b])])dnl aka TEST2b
586 m4_defun([d], [[d]m4_require([a])m4_require([c])])dnl aka TEST1
590 post
592 [[pre
598 post
601 dnl From the m4sugar.m4 discourse: Require chains, nested
602 AT_CHECK_M4SUGAR_TEXT([[dnl
603 m4_defun([a], [[a]])dnl aka TEST2a
604 m4_defun([b], [[b]m4_require([a])])dnl aka TEST3
605 m4_defun([c], [[c]m4_require([b])])dnl aka TEST2b
606 m4_defun([d], [[d]m4_require([a])m4_require([c])])dnl aka TEST1
607 m4_defun([wrap],
608 [pre
611 post])dnl
612 wrap
620 post
623 dnl Direct invocation, nested requires, top level
624 AT_CHECK_M4SUGAR_TEXT([[dnl
625 m4_defun([a], [[a]])dnl
626 m4_defun([b], [[b]m4_require([a])])dnl
627 m4_defun([c], [[c]m4_require([b])])dnl
633 post
635 [[pre
641 post
644 dnl Direct invocation, nested requires, nested defun.  This is an example
645 dnl of expansion before requirement, such that b occurs before its
646 dnl prerequisite a.  This indicates a bug in the macros (but not in
647 dnl autoconf), so we should be emitting a warning.
648 AT_CHECK_M4SUGAR_TEXT([[dnl
649 m4_defun([a], [[a]])dnl
650 m4_defun([b], [[b]m4_require([a])])dnl
651 m4_defun([c], [[c]m4_require([b])])dnl
652 dnl the extra macro layer works around line number differences in older m4
653 m4_define([foo], [m4_defun([outer],
654 [pre
659 post])])foo[]dnl
660 outer
669 post
671 [[script.4s:15: warning: m4@&t@_require: `a' was expanded before it was required
672 script.4s:15: https://www.gnu.org/software/autoconf/manual/autoconf.html#Expanded-Before-Required
673 script.4s:5: b is expanded from...
674 script.4s:6: c is expanded from...
675 script.4s:14: outer is expanded from...
676 script.4s:15: the top level
679 dnl Direct invocation, expand-before-require but no nested require.  As this
680 dnl is common in real life, but does not result in out-of-order expansion,
681 dnl we silently permit this.
682 AT_CHECK_M4SUGAR_TEXT([[dnl
683 m4_defun([a], [[a]])dnl
684 m4_defun([b], [[b]m4_require([a])])dnl
685 m4_defun([c], [[c]])dnl
686 m4_defun([d], [[d]m4_require([c])])dnl
687 pre1
692 post1
693 m4_defun([outer],
694 [pre2
699 post2])dnl
700 outer
701 m4_defun([e], [[e]])dnl
702 m4_defun([f], [[f]m4_require([e])])dnl
703 m4_defun([g], [[g]
705 f])dnl
706 m4_defun([h], [[h]m4_require([g])])dnl
708 m4_defun([i], [[i]])dnl
709 m4_defun([j], [[j]
710 i])dnl
711 m4_defun([k], [[k]m4_require([i])])dnl
712 m4_defun([l], [[l]m4_require([k])])dnl
713 m4_defun([m], [[m]m4_require([j])m4_require([l])])dnl
716 [[pre1
721 post1
722 pre2
727 post2
739 AT_CLEANUP
742 ## ------------------------------------------------- ##
743 ## m4_ifval, m4_ifblank, m4_ifset, m4_default, etc.  ##
744 ## ------------------------------------------------- ##
746 AT_SETUP([m4sugar shorthand conditionals])
747 AT_KEYWORDS([m4@&t@_ifval m4@&t@_ifblank m4@&t@_ifnblank m4@&t@_ifset
748 m4@&t@_default m4@&t@_default_quoted m4@&t@_default_nblank
749 m4@&t@_default_nblank_quoted])
751 AT_CHECK_M4SUGAR_TEXT([[m4_define([active], [ACTIVE])m4_define([empty])
752 m4_ifval([active], [yes], [no])
753 m4_ifval([empty], [yes], [no])
754 m4_ifval([ ], [yes], [no])
755 m4_ifval([], [yes], [no])
756 m4_ifblank([active], [yes], [no])
757 m4_ifblank([empty], [yes], [no])
758 m4_ifblank([ ], [yes], [no])
759 m4_ifblank([], [yes], [no])
760 m4_ifnblank([active], [yes], [no])
761 m4_ifnblank([empty], [yes], [no])
762 m4_ifnblank([ ], [yes], [no])
763 m4_ifnblank([], [yes], [no])
764 m4_ifset([active], [yes], [no])
765 m4_ifset([empty], [yes], [no])
766 m4_ifset([ ], [yes], [no])
767 m4_ifset([], [yes], [no])
769 m4_define([demo1], [m4_default([$1], [$2])])dnl
770 m4_define([demo2], [m4_default_quoted([$1], [$2])])dnl
771 m4_define([demo3], [m4_default_nblank([$1], [$2])])dnl
772 m4_define([demo4], [m4_default_nblank_quoted([$1], [$2])])dnl
773 demo1([active], [default])
774 demo1([], [active])
775 demo1([empty], [text])
776 -demo1([ ], [active])-
777 demo2([active], [default])
778 demo2([], [active])
779 demo2([empty], [text])
780 -demo2([ ], [active])-
781 demo3([active], [default])
782 demo3([], [active])
783 demo3([empty], [text])
784 -demo3([ ], [active])-
785 demo4([active], [default])
786 demo4([], [active])
787 demo4([empty], [text])
788 -demo4([ ], [active])-
789 ]], [[
807 ACTIVE
808 ACTIVE
810 - -
811 active
812 active
813 empty
814 - -
815 ACTIVE
816 ACTIVE
818 -ACTIVE-
819 active
820 active
821 empty
822 -active-
825 AT_CLEANUP
827 ## --------- ##
828 ## m4_cond.  ##
829 ## --------- ##
831 AT_SETUP([m4@&t@_cond])
833 AT_CHECK_M4SUGAR_TEXT([[m4_define([side], [m4_errprintn([$1])$1])
834 m4_cond([side(1)], [1], [a],
835         [side(1)], [1], [b],
836         [side(1)], [2], [c])
837 m4_cond([side(2)], [1], [a],
838         [side(2)], [1], [b],
839         [side(2)], [2], [c],
840         [side(2)])
841 m4_cond([side(3)], [1], [a],
842         [side(3)], [1], [b],
843         [side(3)], [2], [c],
844         [side(3)])
845 m4_cond([a,a], [a,a], [yes], [no])
846 m4_cond([[a,a]], [a,a], [yes])
847 m4_cond([a,a], [a,b], [yes], [no])
848 m4_cond([a,a], [a,b], [yes])
849 m4_cond([m4_eval([0xa])])
850 m4_define([ab], [AB])dnl
851 m4_cond([a])b
852 m4_cond([1], [1], [a])b
853 m4_cond([1], [2], [3], [a])b
854 ]], [[
866 ]], [[1
876 AT_CLEANUP
879 ## ---------- ##
880 ## m4 lists.  ##
881 ## ---------- ##
883 AT_SETUP([m4 lists])
885 AT_KEYWORDS([m4@&t@_car m4@&t@_cdr m4@&t@_argn _m4@&t_cdr])
887 AT_CHECK_M4SUGAR_TEXT([[dnl
888 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])
889 m4_argn([1], [a], [b], [c])
890 m4_argn([2], [a], [b], [c])
891 m4_argn([3], [a], [b], [c])
892 m4_argn([4], [a], [b], [c])
893 m4_car([a], [b], [c])
894 m4_cdr([a], [b], [c])
895 m4_cdr([a], [b])
896 m4_cdr([a])
897 _m4_cdr([a], [b], [c])
898 _m4_cdr([a], [b])
899 _m4_cdr([a])
900 m4_if(m4_cdr([], []), [[]], [good], [bad])
901 m4_if(m4_cdr([]), [], [good], [bad])
902 ]], [[
908 [b],[c]
911 , [b],[c]
912 , [b]
914 good
915 good
918 AT_DATA_M4SUGAR([script.4s],
919 [[m4_init
920 m4_argn([0], [a], [b], [c])
922 AT_CHECK_M4SUGAR([-o-], [1], [],
923 [[script.4s:2: error: assert failed: 0 < 0
924 script.4s:2: the top level
925 autom4te: m4 failed with exit status: 1
928 AT_CLEANUP
931 ## ---------- ##
932 ## m4_split.  ##
933 ## ---------- ##
935 AT_SETUP([m4@&t@_split])
937 AT_CHECK_M4SUGAR_TEXT(
938 [[m4_define([active], [ACT, IVE])m4_define([bd], [oops])
939 m4_split
940 m4_split([[]])
941 m4_split([ ])
942 m4_split([active])
943 m4_split([ active       active ])end
944 m4_split([ ], [ ])
945 m4_split([active], [ ])
946 m4_split([ active       active ], [ ])end
947 m4_split([abcde], [bd])
948 m4_split([abcde], [[bd]])
949 m4_split([foo=`` bar=''])
950 m4_split([foo='' bar=``])
951 dnl these next two are from the manual; keep this in sync if the internal
952 dnl quoting strings in m4_split are changed
953 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])dnl
954 m4_split([a )}>=- b -=<{( c])
955 m4_split([a )}@&t@>=- b -=<@&t@{( c])
959 [[]]
960 [], []
961 [active]
962 [], [active], [active], []end
963 [], []
964 [active]
965 [], [active     active], []end
966 [abcde]
967 [a], [c], [e]
968 [foo=``], [bar='']
969 [foo=''], [bar=``]
970 [a], [], [B], [], [c]
971 [a], [)}>=@&t@-], [b], [-@&t@=<{(], [c]
974 AT_CLEANUP
977 ## ------- ##
978 ## m4_do.  ##
979 ## ------- ##
981 AT_SETUP([m4@&t@_do])
983 AT_CHECK_M4SUGAR_TEXT(
984 [[m4_define([ab], [1])m4_define([bc], [2])m4_define([abc], [3])dnl
985 m4_define([AB], [4])m4_define([BC], [5])m4_define([ABC], [6])dnl
986 m4_do
987 m4_do([a])
988 m4_do([a], [b])c
989 m4_unquote(m4_join([], [a], [b]))c
990 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])dnl
991 m4_do([a], [b])c
992 m4_unquote(m4_join([], [a], [b]))c
1002 AT_CLEANUP
1005 ## ----------- ##
1006 ## m4_append.  ##
1007 ## ----------- ##
1009 AT_SETUP([m4@&t@_append])
1010 AT_KEYWORDS([m4@&t@_append_uniq m4@&t@_append_uniq_w])
1012 AT_CHECK_M4SUGAR_TEXT(
1013 [[m4_define([active], [ACTIVE])dnl
1014 m4_append([sentence], [This is an])dnl
1015 m4_append([sentence], [ active ])dnl
1016 m4_append([sentence], [symbol.])dnl
1017 sentence
1018 m4_undefine([active])dnl
1019 sentence
1020 m4_define([active], [ACTIVE])dnl
1021 m4_append([hooks], [m4_define([act1], [act2])])dnl
1022 m4_append([hooks], [m4_define([act2], [active])])dnl
1023 m4_undefine([active])dnl
1024 act1
1025 hooks
1026 act1
1027 dnl Test for bug fixed in 2.62 when separator is active.
1028 m4_define([a], [A])dnl
1029 m4_append_uniq([foo], [-], [a])dnl
1030 m4_append_uniq([foo], [-], [a])dnl
1031 m4_append_uniq([bar], [-], [a])dnl
1032 m4_append_uniq([bar], [~], [a])dnl
1033 m4_append_uniq([bar], [-], [a])dnl
1034 m4_defn([foo])
1035 m4_defn([bar])
1038 m4_append_uniq([blah], [one], [, ], [new], [existing])
1039 m4_append_uniq([blah], [two], [, ], [new], [existing])
1040 m4_append_uniq([blah], [two], [, ], [new], [existing])
1041 m4_append_uniq([blah], [three], [, ], [new], [existing])
1042 m4_append([blah], [two], [, ])dnl
1043 blah
1044 m4_dquote(blah)
1045 m4_append([list], [one], [[, ]])dnl
1046 m4_append([list], [two], [[, ]])dnl
1047 m4_append([list], [three], [[, ]])dnl
1048 list
1049 m4_dquote(list)
1050 m4_append_uniq_w([numbers], [1 1 2])dnl
1051 m4_append_uniq_w([numbers], [ 2 3 ])dnl
1052 numbers
1054 [[This is an ACTIVE symbol.
1055 This is an active symbol.
1056 act1
1058 active
1065 existing
1067 one, two, three, two
1068 [one],[two],[three],[two]
1069 one, two, three
1070 [one, two, three]
1071 1 2 3
1074 AT_DATA_M4SUGAR([script.4s],
1075 [[m4_init[]dnl
1076 m4_append_uniq([str], [a], [ ])
1077 m4_append_uniq([str], [a b], [ ])
1078 m4_divert([])dnl
1082 AT_CHECK_M4SUGAR([-o-], 0, [[a a b
1083 ]], [[script.4s:3: warning: m4@&t@_append_uniq: `a b' contains ` '
1086 AT_CLEANUP
1089 ## --------- ##
1090 ## m4_join.  ##
1091 ## --------- ##
1093 AT_SETUP([m4@&t@_join])
1095 AT_KEYWORDS([m4@&t@_joinall])
1097 AT_CHECK_M4SUGAR_TEXT(
1098 [[m4_define([active], [ACTIVE])
1099 m4_join
1100 m4_join([|])
1101 m4_join([, ], [one], [two])
1102 m4_dquote(m4_join([, ], [one], [two]))
1103 m4_join([|], [active], [active])
1104 m4_join([|], ,,,[one])
1105 m4_join([|], [one],,,)
1106 m4_join([], ,,,[two])
1107 m4_join([], [two],,,)
1108 m4_join([ active ], [one], , [two])
1109 m4_join([], [one], [two])
1110 m4_joinall([-], [one], [], [two])
1111 m4_joinall([-], [], [], [three], [], [])
1112 m4_joinall([], [one], [], [two])
1113 m4_joinall
1114 m4_joinall([-])
1115 m4_joinall([-], [one])
1120 one, two
1121 [one, two]
1122 active|active
1127 one active two
1128 onetwo
1129 one--two
1130 --three--
1131 onetwo
1137 AT_CLEANUP
1140 ## ----------- ##
1141 ## m4_expand.  ##
1142 ## ----------- ##
1144 AT_SETUP([m4@&t@_expand])
1146 AT_CHECK_M4SUGAR_TEXT(
1147 [[m4_define([active], [ACTIVE])dnl
1148 m4_expand([#active
1149 active])
1150 m4_expand([[active]])
1151 dnl properly quoted case statements
1152 m4_expand([case a in @%:@(
1153   *) echo active, ;;
1154 esac
1155 case b in
1156   *[)] echo active, ;;
1157 esac])
1158 dnl unbalanced underquoted `)', but we manage anyway (gasp!)
1159 m4_expand([case c in #(
1160   *) echo active, ;;
1161 esac
1162 case d in
1163   *) echo active, ;;
1164 esac])
1165 dnl unterminated comment/dnl
1166 m4_expand([active # active])
1167 m4_expand([a
1168 dnl])
1169 m4_expand([a
1170 -dnl])
1172 [[#active
1173 ACTIVE
1174 active
1175 case a in #(
1176   *) echo ACTIVE, ;;
1177 esac
1178 case b in
1179   *) echo ACTIVE, ;;
1180 esac
1181 case c in #(
1182   *) echo ACTIVE, ;;
1183 esac
1184 case d in
1185   *) echo ACTIVE, ;;
1186 esac
1187 ACTIVE # active
1193 AT_CLEANUP
1196 ## ------------- ##
1197 ## m4_text_box.  ##
1198 ## ------------- ##
1200 AT_SETUP([m4@&t@_text_box])
1202 AT_CHECK_M4SUGAR_TEXT([[
1203 m4_text_box([a $1 @&t@b])
1204 m4_text_box([a $1 @&t@b], [$])
1205 m4_text_box([a $1 @&t@b], [,])
1206 ]], [[
1207 ## ------ ##
1208 ## a $1 b ##
1209 ## ------ ##
1210 ## $$$$$$ ##
1211 ## a $1 b ##
1212 ## $$$$$$ ##
1213 ## ,,,,,, ##
1214 ## a $1 b ##
1215 ## ,,,,,, ##
1218 AT_CLEANUP
1220 ## -------------- ##
1221 ## m4_text_wrap.  ##
1222 ## -------------- ##
1224 AT_SETUP([m4@&t@_text_wrap])
1225 AT_KEYWORDS([m4@&t@_escape])
1227 # m4_text_wrap is used to display the help strings.  Also, check that
1228 # commas and $ are not swallowed.  This can easily happen because of
1229 # m4-listification.
1231 AT_DATA_M4SUGAR([script.4s],
1232 [[m4_init[]m4_divert([])dnl
1233 m4_define([a], [OOPS])dnl
1234 m4_escape([a[b $c#]d])
1235 m4_if(m4_escape([a[b $c#]d]), [a[b $c#]d], [oops],
1236       m4_escape([a[b $c#]d]), [a@<:@b @S|@c@%:@@:>@d], [pass], [oops])
1238 m4_text_wrap([Short string */], [   ], [/* ], 20)
1240 m4_text_wrap([Much longer string */], [   ], [/* ], 20)
1242 m4_text_wrap([Short doc.], [          ], [  --short ], 30)
1244 m4_text_wrap([Short doc.], [          ], [  --too-wide], 30)
1246 m4_text_wrap([Super long documentation.], [          ], [  --too-wide], 30)
1248 m4_text_wrap([First, second  , third, [,quoted  space]])
1249 m4_define([xfff], [oops])
1250 m4_text_wrap([Some $1 $2 $3 $4 embedded dollars.], [ $* ], [ $@ ], [0xfff & 20])
1253 AT_DATA([expout],
1254 [[a[b $c#]d
1255 pass
1257 /* Short string */
1259 /* Much longer
1260    string */
1262   --short Short doc.
1264   --too-wide
1265           Short doc.
1267   --too-wide
1268           Super long
1269           documentation.
1271 First, second , third, [,quoted space]
1273  $@ Some $1 $2 $3
1274  $* $4 embedded
1275  $* dollars.
1278 AT_CHECK_M4SUGAR([-o-], 0, [expout])
1280 AT_CLEANUP
1282 ## -------------------- ##
1283 ## m4_version_compare.  ##
1284 ## -------------------- ##
1286 AT_SETUP([m4@&t@_version_compare])
1288 AT_KEYWORDS([m4@&t@_list_cmp])
1290 AT_CHECK_M4SUGAR_TEXT(
1291 [[m4_version_compare([1.1], [2.0])
1292 m4_version_compare([2.0b], [2.0a])
1293 m4_version_compare([2.0z], [2.0y])
1294 m4_version_compare([1.1.1], [1.1.1a])
1295 m4_version_compare([1.2], [1.1.1a])
1296 m4_version_compare([1.0], [1])
1297 m4_version_compare([1.0a], [1.0a])
1298 m4_version_compare([1.1a], [1.1a.1])
1299 m4_version_compare([1.10], [1.1a])
1300 m4_version_compare([1-1a], [1,1A])
1301 m4_define([a], [oops])dnl
1302 m4_version_compare([1.1a], [1.1A])
1303 m4_version_compare([1z], [1aa])
1304 m4_version_compare([2.61a], [2.61a-248-dc51])
1305 m4_version_compare([2.61b], [2.61a-248-dc51])
1306 m4_version_compare([08], [09])
1307 m4_version_compare([010], [8])
1308 dnl Test that side effects to m4_list_cmp occur exactly once
1309 m4_list_cmp([[0], [0], [0]m4_errprintn([hi])],
1310             [[0], [0], [0]m4_errprintn([hi])])
1311 m4_list_cmp([[0], [0], [0]m4_errprintn([hi])],
1312             [[0], [0], [0]m4_errprintn([bye])])
1314 [[-1
1332 ]], [[hi
1338 AT_CLEANUP
1340 ## ------------------------------ ##
1341 ## Standard regular expressions.  ##
1342 ## ------------------------------ ##
1344 AT_SETUP([Standard regular expressions])
1346 # AT_CHECK_M4RE(RE-NAME, TEXT, INTENT = `ok' | `')
1347 # ------------------------------------------------
1348 # Check whether RE-NAME (a macro whose definition is a regular expression)
1349 # matches TEXT.  INTENT = `ok' if the match should succeed or else empty.
1350 m4_define([AT_CHECK_M4RE],
1351 [AT_CHECK_M4SUGAR_TEXT(
1352 [[m4_bregexp([$2], ^m4_defn([$1])$, [ok])
1353 ]], [$3
1354 ])])
1356 AT_CHECK_M4RE([m4_re_word], [ab9_c], [ok])
1357 AT_CHECK_M4RE([m4_re_word], [_9abc], [ok])
1358 AT_CHECK_M4RE([m4_re_word], [9ab_c])
1360 AT_CHECK_M4RE([m4_re_string], [ab9_c], [ok])
1361 AT_CHECK_M4RE([m4_re_string], [_9abc], [ok])
1362 AT_CHECK_M4RE([m4_re_string], [9ab_c], [ok])
1363 AT_CHECK_M4RE([m4_re_string], [9a@_c])
1365 AT_CLEANUP
1367 ## ----------- ##
1368 ## m4_bmatch.  ##
1369 ## ----------- ##
1371 AT_SETUP([m4@&t@_bmatch])
1373 AT_CHECK_M4SUGAR_TEXT(
1374 [[m4_bmatch([abc], [default\])
1375 m4_bmatch([abc], [^a], [yes])
1376 m4_bmatch([abc], [^a], [yes], [no])
1377 m4_bmatch([abc], [^.a], [yes])
1378 m4_bmatch([abc], [^.a], [yes], [no\])
1379 m4_bmatch([abc], [a], [1], [b], [2])
1380 m4_bmatch([abc], [A], [1], [b], [2])
1381 m4_define([ab], [AB])dnl
1382 m4_bmatch([$*], [a])b
1383 m4_bmatch([$*], [\*], [a])b
1384 m4_bmatch([$*], [1], [2], [a])b
1385 ]], [[default\
1397 AT_CLEANUP
1399 ## ------------------------ ##
1400 ## m4_toupper, m4_tolower.  ##
1401 ## ------------------------ ##
1403 AT_SETUP([m4@&t@_toupper and m4@&t@_tolower])
1405 AT_CHECK_M4SUGAR_TEXT(
1406 [[m4_define([abc], [hI])m4_define([ABC], [Hi])
1407 m4_toupper(abc aBc ABC)
1408 m4_tolower(abc aBc ABC)
1409 m4_toupper([abc aBc ABC])
1410 m4_tolower([abc aBc ABC])
1411 m4_echo(m4_toupper(abc aBc ABC))
1412 m4_echo(m4_tolower(abc aBc ABC))
1413 m4_echo(m4_toupper([abc aBc ABC]))
1414 m4_echo(m4_tolower([abc aBc ABC]))
1415 m4_do(m4_toupper(abc aBc ABC))
1416 m4_do(m4_tolower(abc aBc ABC))
1417 m4_do(m4_toupper([abc aBc ABC]))
1418 m4_do(m4_tolower([abc aBc ABC]))
1419 ]], [[
1420 HI ABC HI
1421 hi abc hi
1422 ABC ABC ABC
1423 abc abc abc
1424 HI ABC HI
1425 hi abc hi
1426 ABC ABC ABC
1427 abc abc abc
1428 HI Hi HI
1429 hi hI hi
1430 Hi Hi Hi
1431 hI hI hI
1434 AT_CLEANUP
1436 ## --------------- ##
1437 ## m4_bpatsubsts.  ##
1438 ## --------------- ##
1440 AT_SETUP([m4@&t@_bpatsubsts])
1442 AT_CHECK_M4SUGAR_TEXT(
1443 [[m4_bpatsubsts([11], [^..$])
1444 m4_bpatsubsts([11], [\(.\)1], [\12])
1445 m4_bpatsubsts([11], [^..$], [], [1], [2])
1446 m4_bpatsubsts([11], [\(.\)1], [\12], [1], [3])
1447 m4_define([a], [oops])m4_define([c], [oops])dnl
1448 m4_define([AB], [good])m4_define([bc], [good])dnl
1449 m4_bpatsubsts([abc], [a], [A], [b], [B], [c])
1450 m4_bpatsubsts([ab], [a])c
1451 m4_bpatsubsts([ab], [c], [C], [a])c
1452 m4_bpatsubsts([$1$*$@], [\$\*], [$#])
1453 ]], [[11
1457 good
1458 good
1459 good
1460 $1$#$@
1463 AT_CLEANUP
1465 ## -------------- ##
1466 ## m4_esyscmd_s.  ##
1467 ## -------------- ##
1469 AT_SETUP([m4@&t@_esyscmd_s])
1470 AT_KEYWORDS([m4@&t@_chomp m4@&t@_chomp_all])
1472 AT_CHECK_M4SUGAR_TEXT(
1473 [[m4_define([world], [WORLD])dnl
1474 m4_chomp([abc])
1475 m4_chomp([world
1478 m4_esyscmd_s([echo hello world])
1479 m4_esyscmd_s([echo '[goodbye,
1480 cruel world
1482 ]'])
1483 ]], [[abc
1484 world
1486 hello WORLD
1487 goodbye,
1488 cruel world
1491 AT_CLEANUP
1493 ## ---------- ##
1494 ## M4 Loops.  ##
1495 ## ---------- ##
1497 AT_SETUP([M4 loops])
1499 AT_KEYWORDS([m4@&t@_for m4@&t@_foreach m4@&t@_foreach_w m4@&t@_map_args_w])
1501 AT_CHECK_M4SUGAR_TEXT([[dnl
1502 m4_define([myvar], [outer value])dnl
1503 m4_for([myvar], 1, 3, 1, [ myvar])
1504 m4_for([myvar], 1, 3,  , [ myvar])
1505 m4_for([myvar], 3, 1,-1, [ myvar])
1506 m4_for([myvar], 3, 1,  , [ myvar])
1507 m4_for([myvar], 1, 3, 2, [ myvar])
1508 m4_for([myvar], 3, 1,-2, [ myvar])
1509 m4_for([myvar],-1,-3,-2, [ myvar])
1510 m4_for([myvar],-3,-1, 2, [ myvar])
1511 dnl Make sure we recalculate the bounds correctly:
1512 m4_for([myvar], 1, 3, 3, [ myvar])
1513 m4_for([myvar], 1, 6, 3, [ myvar])
1514 m4_for([myvar],22,-7,-5, [ myvar])
1515 m4_for([myvar],-2,-7,-4, [ myvar])
1516 m4_for([myvar],-7,-2, 4, [ myvar])
1517 dnl Make sure we are not exposed to division truncation:
1518 m4_for([myvar], 2, 5, 2, [ myvar])
1519 m4_for([myvar],-5,-2, 2, [ myvar])
1520 m4_for([myvar], 5, 2,-2, [ myvar])
1521 m4_for([myvar],-2,-5,-2, [ myvar])
1522 dnl Make sure we do not divide by zero:
1523 m4_for([myvar], 1, 1,  , [ myvar])
1524 m4_for([myvar], 1, 1,+2, [ myvar])
1525 m4_for([myvar], 1, 1,-2, [ myvar])
1526 dnl Make sure we do not loop endlessly
1527 m4_for([myval], 1, 1, 0, [ myval])
1528 dnl Make sure to properly parenthesize
1529 m4_for([myvar], 3-5, -2+8, , [ myvar])
1530 m4_for([myvar], -2+8, 3-5, , [ myvar])
1531 m4_for([myvar], 8, 16, 3 * 2, [ myvar])
1532 m4_for([myvar], 8, 16, -3 * -2, [ myvar])
1533 m4_for([myvar], [2<<2], [2<<3], [-3 * (-2)], [ myvar])
1534 dnl Modifying var does not affect the number of iterations
1535 m4_for([myvar], 1, 5, , [ myvar[]m4_define([myvar], 5)])
1536 dnl Make sure we can do nameless iteration
1537 m4_for(, 1, 10, , -)
1538 dnl foreach tests
1539 m4_foreach([myvar], [[a], [b, c], [d], [e
1540 ],[f]], [ myvar|])
1541 m4_foreach_w([myvar], [a  b c, d,e f
1542 g], [ myvar|])
1543 myvar
1544 m4_map_args_w([a  b c, d,e f
1545 g], [ ], [|])
1546 m4_map_args_w([a b], [\1], [/])
1547 m4_define([dashes], [--])dnl
1548 m4_map_args_w([a b c], [/], [\1], [dashes])
1549 dnl only one side effect expansion, prior to visiting list elements
1550 m4_foreach([i], [[1], [2], [3]m4_errprintn([hi])], [m4_errprintn(i)])dnl
1551 dnl shifting forms an important part of loops
1552 m4_shift3:m4_shift3(1,2,3):m4_shift3(1,2,3,4)
1553 m4_shiftn(3,1,2,3):m4_shiftn(3,1,2,3,4)
1555 [[ 1 2 3
1556  1 2 3
1557  3 2 1
1558  3 2 1
1559  1 3
1560  3 1
1561  -1 -3
1562  -3 -1
1564  1 4
1565  22 17 12 7 2 -3
1566  -2 -6
1567  -7 -3
1568  2 4
1569  -5 -3
1570  5 3
1571  -2 -4
1576  -2 -1 0 1 2 3 4 5 6
1577  6 5 4 3 2 1 0 -1 -2
1578  8 14
1579  8 14
1580  8 14
1581  1 2 3 4 5
1582 ----------
1583  a| b, c| d| e
1584 | f|
1585  a| b| c,| d,e| f| g|
1586 outer value
1587  a| b| c,| d,e| f| g|
1588 \1a/\1b/
1589 /a\1--/b\1--/c\1
1592 ]], [[hi
1598 dnl bounds checking in m4_for
1599 AT_DATA_M4SUGAR([script.4s],
1600 [[m4_init
1601 m4_divert([0])dnl
1602 m4_for([myvar], 1, 3,-1, [ myvar])
1604 AT_CHECK_M4SUGAR([], 1, [],
1605 [[script.4s:3: error: assert failed: -1 > 0
1606 script.4s:3: the top level
1607 autom4te: m4 failed with exit status: 1
1610 AT_DATA_M4SUGAR([script.4s],
1611 [[m4_init
1612 m4_divert([0])dnl
1613 m4_for([myvar], 1, 2, 0, [ myvar])
1615 AT_CHECK_M4SUGAR([], 1, [],
1616 [[script.4s:3: error: assert failed: 0 > 0
1617 script.4s:3: the top level
1618 autom4te: m4 failed with exit status: 1
1621 AT_DATA_M4SUGAR([script.4s],
1622 [[m4_init
1623 m4_divert([0])dnl
1624 m4_for([myvar], 2, 1, 0, [ myvar])
1626 AT_CHECK_M4SUGAR([], 1, [],
1627 [[script.4s:3: error: assert failed: 0 < 0
1628 script.4s:3: the top level
1629 autom4te: m4 failed with exit status: 1
1632 dnl m4_shiftn also does bounds checking
1633 AT_DATA_M4SUGAR([script.4s],
1634 [[m4_init
1635 m4_divert([0])dnl
1636 m4_shiftn(3,1,2)
1638 AT_CHECK_M4SUGAR([], 1, [],
1639 [[script.4s:3: error: assert failed: 0 < 3 && 3 < 3
1640 script.4s:3: the top level
1641 autom4te: m4 failed with exit status: 1
1644 AT_CLEANUP
1647 ## --------------------- ##
1648 ## m4_map{,all}{,_sep}.  ##
1649 ## --------------------- ##
1651 AT_SETUP([m4@&t@_map])
1652 AT_KEYWORDS([m4@&t@_apply m4@&t@_map_sep m4@&t@_mapall m4@&t@_mapall_sep])
1653 AT_KEYWORDS([m4@&t@_count])
1655 AT_CHECK_M4SUGAR_TEXT([[dnl
1656 m4_map([m4_count], [])
1657 m4_map([ m4_count], [[],
1658                      [[1]],
1659                      [[1], [2]]])
1660 m4_mapall([ m4_count], [[],
1661                         [[1]],
1662                         [[1], [2]]])
1663 m4_map_sep([m4_eval], [,], [[[1+2]],
1664                             [[10], [16]]])
1665 m4_count(m4_map_sep([m4_echo], [,], [[], [[1]], [[2]]]))
1666 m4_count(m4_mapall_sep([m4_echo], [,], [[], [[1]], [[2]]]))
1667 m4_map_sep([m4_eval], [[,]], [[[1+2]],
1668                               [[10], [16]]])
1669 m4_count(m4_map_sep([m4_echo], [[,]], [[], [[1]], [[2]]]))
1670 m4_count(m4_mapall_sep([m4_echo], [[,]], [[], [[1]], [[2]]]))
1671 m4_map([-], [[]])
1672 m4_mapall([-], [[]])
1673 m4_map_sep([-], [:], [[]])
1674 m4_mapall_sep([-], [:], [[]])
1675 m4_define([a], [m4_if([$#], [0], [oops], [$1], [a], [pass], [oops])])dnl
1676 m4_define([a1], [oops])dnl
1677 m4_define([pass1], [oops])dnl
1678 m4_map([a], [[[a]]])1
1679 m4_map([m4_unquote([a])], [m4_dquote([a])])
1680 dnl only one side effect expansion, prior to visiting list elements
1681 m4_map([m4_errprintn], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1682 m4_map_sep([m4_errprintn], [], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1683 m4_mapall([m4_errprintn], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1684 m4_mapall_sep([m4_errprintn], [], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1687  1 2
1688  0 1 2
1699 pass1
1700 pass
1701 ]], [[hi
1719 AT_CLEANUP
1722 ## --------------------------------------- ##
1723 ## m4_map_args{,_sep,_pair} and m4_curry.  ##
1724 ## --------------------------------------- ##
1726 AT_SETUP([m4@&t@_map_args and m4@&t@_curry])
1727 AT_KEYWORDS([m4@&t@_map_args_sep m4@&t@_map_args_pair m4@&t@_reverse
1728 m4@&t@_map])
1730 dnl First, make sure we can curry in isolation.
1731 AT_CHECK_M4SUGAR_TEXT(
1732 [[m4_curry([m4_echo])([1])
1733 m4_curry([m4_curry], [m4_reverse], [1])([2])([3])
1734 m4_define([add], [m4_eval(([$1]) + ([$2]))])dnl
1735 m4_define([add_one], [m4_curry([add], [1])])dnl
1736 add_one()([4])
1739 3, 2, 1
1743 dnl Now, check that we can map a list of arguments.
1744 AT_CHECK_M4SUGAR_TEXT([[m4_define([active], [ACTIVE])dnl
1745 m4_map_args([ m4_echo])
1746 m4_map_args([ m4_echo], [plain], [active])
1747 m4_map_args([m4_unquote], [plain], [active])
1748 m4_map_args_pair([, m4_reverse], [])
1749 m4_map_args_pair([, m4_reverse], [], [1])
1750 m4_map_args_pair([, m4_reverse], [], [1], [2])
1751 m4_map_args_pair([, m4_reverse], [], [1], [2], [3])
1752 m4_map_args_pair([, m4_reverse], [], [1], [2], [3], [4])
1753 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1])
1754 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2])
1755 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3])
1756 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3], [4])
1757 m4_map_args_sep([<], [>], [:], [1], [2], [3])
1758 m4_map_args_sep([m4_echo(], [)], [ ], [plain], [active])
1761  plain active
1762 plainACTIVE
1764 , 1
1765 , 2, 1
1766 , 2, 1, 3
1767 , 2, 1, 4, 3
1768 , [1]
1769 , 2, 1
1770 , 2, 1, [3]
1771 , 2, 1, 4, 3
1772 <1>:<2>:<3>
1773 plain active
1776 dnl Finally, put the two concepts together, to show the real power of the API.
1777 AT_CHECK_M4SUGAR_TEXT(
1778 [[m4_define([add], [m4_eval(([$1]) + ([$2]))])dnl
1779 m4_define([list], [[-1], [0], [1]])dnl
1780 dnl list_add_n(value, arg...)
1781 dnl add VALUE to each ARG and output the resulting list
1782 m4_define([list_add_n],
1783   [m4_shift(m4_map_args([,m4_curry([add], [$1])], m4_shift($@)))])
1784 list_add_n([1], list)
1785 list_add_n([2], list)
1786 ]], [[
1787 0,1,2
1788 1,2,3
1791 AT_CLEANUP
1794 ## ------------ ##
1795 ## m4_combine.  ##
1796 ## ------------ ##
1798 AT_SETUP([m4@&t@_combine])
1800 AT_CHECK_M4SUGAR_TEXT([[m4_define([a], [oops])dnl
1801 m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
1802 m4_combine([, ], [[a], [b]], [-])
1803 m4_combine([, ], [[a], [b]], [-], [])
1804 m4_combine([, ], [], [-], [a], [b])
1805 m4_combine([, ], [[]], [-], [a], [b])
1806 m4_combine([ a ], [[-], [+]], [a], [-], [+])
1807 m4_combine([$* ], [[$1], [$2]], [$#], [$@])
1809 [[a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
1811 a-, b-
1813 -a, -b
1814 -a- a -a+ a +a- a +a+
1815 $1$#$@$* $2$#$@
1816 ]], [])
1818 AT_CLEANUP
1821 ## -------------- ##
1822 ## m4_{max,min}.  ##
1823 ## -------------- ##
1825 AT_SETUP([m4@&t@_max and m4@&t@_min])
1827 AT_DATA_M4SUGAR([script.4s],
1828 [[m4_max
1831 AT_CHECK_M4SUGAR([], 1, [],
1832 [[script.4s:1: error: too few arguments to m4@&t@_max
1833 script.4s:1: the top level
1834 autom4te: m4 failed with exit status: 1
1837 AT_DATA_M4SUGAR([script.4s],
1838 [[m4_min
1841 AT_CHECK_M4SUGAR([], 1, [],
1842 [[script.4s:1: error: too few arguments to m4@&t@_min
1843 script.4s:1: the top level
1844 autom4te: m4 failed with exit status: 1
1847 AT_CHECK_M4SUGAR_TEXT([[dnl
1848 m4_min(0)
1849 m4_min(0xa)
1850 m4_min(0, 0)
1851 m4_min(0, 1)
1852 m4_min(1, 0)
1853 m4_min(0+1, 1+1)
1854 m4_min(0+1, 1+0)
1855 m4_min(0, 1, 2)
1856 m4_min(2, 1, 0)
1857 m4_min(1m4_for([i], 2, 100, , [,i]))
1858 m4_min(m4_for([i], 100, 2, , [i,])1)
1859 ----
1860 m4_max(0)
1861 m4_max(0xa)
1862 m4_max(0, 0)
1863 m4_max(0, 1)
1864 m4_max(1, 0)
1865 m4_max(1+0, 1+1)
1866 m4_max(1+0, 1+0)
1867 m4_max(0, 1, 2)
1868 m4_max(2, 1, 0)
1869 m4_max(1m4_for([i], 2, 100, , [,i]))
1870 m4_max(m4_for([i], 100, 2, , [i,])1)
1883 ----
1895 ]], [])
1897 AT_CLEANUP
1900 ## ----------- ##
1901 ## Recursion.  ##
1902 ## ----------- ##
1904 AT_SETUP([recursion])
1906 AT_KEYWORDS([m4@&t@_foreach m4@&t@_foreach_w m4@&t@_case m4@&t@_cond
1907 m4@&t@_bpatsubsts m4@&t@_shiftn m4@&t@_do m4@&t@_dquote_elt m4@&t@_reverse
1908 m4@&t@_map m4@&t@_join m4@&t@_joinall m4@&t@_list_cmp m4@&t@_max m4@&t@_min
1909 m4@&t@_bmatch m4@&t@_map_args m4@&t@_map_args_pair])
1911 dnl This test completes in a reasonable time if m4_foreach is linear,
1912 dnl but thrashes if it is quadratic.  If we are testing with m4 1.4.x,
1913 dnl only the slower foreach.m4 implementation will work.  But if we
1914 dnl are testing with m4 1.6, we can rerun the test with __m4_version__
1915 dnl undefined to exercise the alternate code path.
1916 AT_DATA_M4SUGAR([script.4s],
1917 [[m4_init
1918 m4_divert_push([])[]dnl
1919 m4_len(m4_foreach_w([j], m4_do(m4_for([i], [1], [10000], [], [,i ])), [j ]))
1920 m4_shiftn(9998m4_for([i], [1], [10000], [], [,i]))
1921 m4_len(m4_join([--],, m4_dquote_elt(m4_for([i], [1], [10000], [], [,i])),))
1922 m4_len(m4_joinall([--], m4_map([, m4_echo],
1923   m4_dquote([1]m4_for([i], [2], [10000], [], [,i])))))
1924 m4_max(m4_min([1]m4_for([i], [2], [10000], [],
1925   [,i]))m4_for([i], [2], [10000], [], [,i]))
1926 m4_case([10000]m4_for([i], [1], [10000], [], [,i]),[end])
1927 m4_list_cmp(m4_dquote(1m4_for([i], [2], [10000], [], [,i])),
1928   m4_dquote(m4_reverse(10000m4_for([i], [9999], [1], [], [,i])), [0]))
1929 m4_list_cmp([0], [0m4_for([i], [1], [10000], [], [,0])])
1930 m4_list_cmp([0m4_for([i], [1], [10000], [], [,0])], [0])
1931 m4_for([i], [1], [10000], [], [m4_define(i)])dnl
1932 m4_undefine(1m4_for([i], [2], [10000], [], [,i]))dnl
1933 m4_bpatsubsts([a1]m4_for([i], [1], [10000], [], [,i]), [a2], [A])
1934 m4_bmatch([9997]m4_for([i], [1], [10000], [], [,^i$]))
1935 m4_define([up], [m4_define([$1], m4_incr($1))$1])m4_define([j], 0)dnl
1936 m4_cond(m4_for([i], [1], [10000], [], [[up([j])], [9990], i,]) [oops]) j
1937 m4_count(m4_map_args_pair([,m4_quote], []m4_map_args([,m4_echo]m4_for([i],
1938   [1], [10000], [], [,i]))))
1939 m4_divert_pop([])
1942 AT_CHECK_M4SUGAR([-o-], [0], [[48894
1943 9999,10000
1944 78896
1945 58894
1946 10000
1952 ^9998$
1953 9990 9990
1954 5001
1957 AT_DATA_M4SUGAR([script.4s],
1958 [[m4_ifdef([__m4_version__],
1959 [m4_undefine([__m4_version__])],
1960 [m4_divert_push([])48894
1961 9999,10000
1962 78896
1963 58894
1964 10000
1970 ^9998$
1971 9990 9990
1972 5001
1973 m4_exit([0])])
1974 m4_init
1975 m4_divert_push([])[]dnl
1976 m4_len(m4_foreach_w([j], m4_do(m4_for([i], [1], [10000], [], [,i ])), [j ]))
1977 m4_shiftn(9998m4_for([i], [1], [10000], [], [,i]))
1978 m4_len(m4_join([--],, m4_dquote_elt(m4_for([i], [1], [10000], [], [,i])),))
1979 m4_len(m4_joinall([--], m4_map([, m4_echo],
1980   m4_dquote([1]m4_for([i], [2], [10000], [], [,i])))))
1981 m4_max(m4_min([1]m4_for([i], [2], [10000], [],
1982   [,i]))m4_for([i], [2], [10000], [], [,i]))
1983 m4_case([10000]m4_for([i], [1], [10000], [], [,i]),[end])
1984 m4_list_cmp(m4_dquote(1m4_for([i], [2], [10000], [], [,i])),
1985   m4_dquote(m4_reverse(10000m4_for([i], [9999], [1], [], [,i])), [0]))
1986 m4_list_cmp([0], [0m4_for([i], [1], [10000], [], [,0])])
1987 m4_list_cmp([0m4_for([i], [1], [10000], [], [,0])], [0])
1988 m4_for([i], [1], [10000], [], [m4_define(i)])dnl
1989 m4_undefine(1m4_for([i], [2], [10000], [], [,i]))dnl
1990 m4_bpatsubsts([a1]m4_for([i], [1], [10000], [], [,i]), [a2], [A])
1991 m4_bmatch([9997]m4_for([i], [1], [10000], [], [,^i$]))
1992 m4_define([up], [m4_define([$1], m4_incr($1))$1])m4_define([j], 0)dnl
1993 m4_cond(m4_for([i], [1], [10000], [], [[up([j])], [9990], i,]) [oops]) j
1994 m4_count(m4_map_args_pair([,m4_quote], []m4_map_args([,m4_echo]m4_for([i],
1995   [1], [10000], [], [,i]))))
1996 m4_divert_pop([])
1999 AT_CHECK_M4SUGAR([-o-], [0], [[48894
2000 9999,10000
2001 78896
2002 58894
2003 10000
2009 ^9998$
2010 9990 9990
2011 5001
2014 AT_CLEANUP
2017 ## ---------- ##
2018 ## m4_set_*.  ##
2019 ## ---------- ##
2021 AT_SETUP([m4@&t@_set])
2023 AT_KEYWORDS([m4@&t@_set_add m4@&t@_set_add_all m4@&t@_set_contains
2024 m4@&t@_set_contents m4@&t@_set_delete m4@&t@_set_difference m4@&t@_set_dump
2025 m4@&t@_set_empty m4@&t@_set_foreach m4@&t@_set_intersection m4@&t@_set_list
2026 m4@&t@_set_listc m4@&t@_set_map m4@&t@_set_remove m4@&t@_set_size
2027 m4@&t@_set_union])
2029 # Simple tests
2030 AT_CHECK_M4SUGAR_TEXT([[m4_set_contains([a], [1], [yes], [no])
2031 m4_set_add([a], [1], [added], [dup])
2032 m4_set_contains([a], [1], [yes], [no])
2033 m4_set_add([a], [1], [added], [dup])
2034 m4_set_contents([a])
2035 m4_set_remove([a], [1], [removed], [missing])
2036 m4_set_contains([a], [1], [yes], [no])
2037 m4_set_remove([a], [1], [removed], [missing])
2038 m4_set_add([a], [2], [added], [dup])
2039 m4_set_empty([a], [yes], [no])
2040 m4_set_delete([a])
2041 m4_set_empty([a], [yes], [no])
2042 m4_set_add_all([c], [1], [2], [3])
2043 m4_set_add_all([a]m4_set_listc([c]))
2044 m4_set_contents([c], [-])
2045 m4_set_dump([a], [-])
2046 m4_set_contents([a])
2047 m4_set_add_all([a], [1], [2], [3])m4_set_add_all([b], [3], [], [4])
2048 m4_set_difference([a], [b])
2049 m4_set_difference([b], [a])
2050 m4_set_intersection([a], [b])
2051 m4_set_union([a], [b])
2052 m4_define([printodd], [m4_if(m4_eval([$1 & 1]), [1], [:$1])])dnl
2053 m4_set_map([a], [printodd])
2054 m4_set_foreach([a], [i], [m4_if(m4_eval(i & 1), [1], [m4_set_remove([a], i)])])
2055 m4_set_list([a])
2056 m4_set_add([a], [])
2057 m4_set_list([a])
2058 m4_set_remove([a], [2])
2059 m4_dquote(m4_set_list([a]))
2060 m4_set_listc([a])
2061 m4_set_size([a])
2062 m4_set_delete([a])
2063 m4_dquote(m4_set_list([a]))
2064 m4_indir([m4_dquote]m4_set_listc([a]))
2065 m4_set_listc([a])
2066 m4_set_size([a])
2067 ]], [[no
2068 added
2072 removed
2074 missing
2075 added
2081 1-2-3
2082 3-2-1
2085 ,1,2
2088 ,1,2,3,,4
2089 :1:3
2105 # Stress tests - check for unusual names/values
2106 AT_CHECK_M4SUGAR_TEXT([[m4_define([a], [oops])dnl
2107 m4_set_add([a], [a])dnl
2108 m4_set_remove([a], [oops], [yes], [no])
2109 m4_set_add([a,b], [c])dnl
2110 m4_set_add([a,b], [$*[]])dnl
2111 m4_set_add_all([a], [b,c])dnl
2112 m4_set_size([a])
2113 m4_count(m4_set_contents([a], [,]))
2114 m4_count(m4_set_list([a], [,]))
2115 m4_set_dump([a], [,])
2116 m4_set_contents([a,b], [,])
2117 m4_set_list([a,b])
2118 m4_set_foreach([$*[]], [$*[]], [oops])
2119 m4_set_add([$*[]], [])dnl
2120 m4_set_remove([$*[]], [a], [yes], [no])
2121 m4_set_add([$*[]], [a])dnl
2122 m4_set_foreach([$*[]], [$*[]], [-m4_defn([$*[]])m4_indir([$*[]])-])
2123 m4_set_remove([$*[]], [], [yes], [no])
2124 m4_set_add([c], [,])dnl
2125 m4_set_foreach([a,b], [set], [:m4_set_listc(_m4_defn([set])):])
2126 ]],[[no
2130 b,c,a
2131 c,$*[]
2132 c,$*[]
2135 ---aoops-
2137 :,,::,a:
2140 # Stress tests - check for linear scaling (won't necessarily fail if
2141 # quadratic, but hopefully users will complain if it appears to hang)
2142 AT_CHECK_M4SUGAR_TEXT([[dnl
2143 m4_for([i], [1], [10000], [], [m4_set_add([a], i)])dnl
2144 m4_set_add_all([b]m4_for([i], [1], [10000], [], [,i]))dnl
2145 m4_set_remove([a], [1])dnl
2146 m4_set_remove([b], [10000])dnl
2147 m4_set_add_all([a]m4_for([i], [1], [10000], [], [,i]))dnl
2148 m4_for([i], [1], [10000], [], [m4_set_add([b], i)])dnl
2149 m4_len(m4_set_contents([a]))
2150 m4_len(m4_set_foreach([b], [b], [m4_if(m4_eval(b & 1), [1],
2151   [m4_set_remove([b], b, [-])])]))
2152 m4_set_size([b])
2153 m4_define([prune3x], [m4_if(m4_eval([$1 % 3]), [0],
2154   [m4_set_remove([a], [$1], [-])])])dnl
2155 m4_len(m4_set_map([a], [prune3x]))
2156 m4_count(m4_shift(m4_set_intersection([a], [b])))
2157 ]], [[38894
2158 5000
2159 5000
2160 3333
2161 3334
2164 AT_CLEANUP
2167 ## ---------------------- ##
2168 ## __file__ and __line__. ##
2169 ## ---------------------- ##
2171 AT_SETUP([[__file__ and __line__]])
2173 # Check that __file__ and __line__ work.
2174 # Check that m4__file__ and m4__line__ are not defined
2175 # (and get them to pass by the undefined-macro check).
2176 # Try to not assume too much about AT_CHECK_M4SUGAR_TEXT.
2177 AT_CHECK_M4SUGAR_TEXT([[dnl
2178 m4_pattern_allow([m4__file__])dnl
2179 m4_pattern_allow([m4__line__])dnl
2180 m4__file__
2181 m4__line__
2182 __file__
2183 m4_define([first], __line__)dnl
2184 m4_define([second], __line__)dnl
2185 m4_assert(first + 1 == second)dnl
2186 ]], [[m4@&t@__@&t@file__
2187 m4@&t@__@&t@line__
2188 script.4s
2191 AT_CLEANUP