Add m4_argn.
[autoconf.git] / tests / m4sugar.at
blobbf56c4dbacd2f42c440c7f5524c6c12e45ee698c
1 #                                                       -*- Autotest -*-
3 AT_BANNER([M4sugar.])
5 # Copyright (C) 2000, 2001, 2002, 2005, 2006, 2007, 2008, 2009 Free
6 # Software Foundation, Inc.
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # 02110-1301, USA.
24 # AT_CHECK_M4SUGAR_TEXT(CODE, STDOUT, STDERR)
25 # -------------------------------------------
26 # Check that m4sugar CODE expands to STDOUT and emits STDERR.
27 m4_define([AT_CHECK_M4SUGAR_TEXT],
29 AT_DATA_M4SUGAR([script.4s],
30 [[m4_init
31 m4_divert_push(0)[]dnl
32 ]$1[[]dnl
33 m4_divert_pop(0)
34 ]])
36 AT_CHECK_M4SUGAR([-o-],, [$2], [$3])
37 ])# AT_CHECK_M4SUGAR_TEXT
40 ## ------------------ ##
41 ## m4_stack_foreach.  ##
42 ## ------------------ ##
44 AT_SETUP([m4@&t@_stack])
46 AT_KEYWORDS([m4@&t@_stack_foreach m4@&t@_stack_foreach_lifo])
47 AT_KEYWORDS([m4@&t@_stack_foreach_sep m4@&t@_stack_foreach_sep_lifo])
48 AT_KEYWORDS([m4@&t@_copy m4@&t@_n])
50 # Test the semantics of macros to walk stacked macro definitions.
51 AT_CHECK_M4SUGAR_TEXT([[dnl
52 m4_pushdef([abc], [def])dnl
53 m4_pushdef([abc], [ghi])dnl
54 m4_pushdef([abc], [jkl])dnl
55 m4_stack_foreach([abc], [m4_n])
56 abc
57 m4_stack_foreach_lifo([abc], [m4_n])
58 m4_stack_foreach([abc], [m4_n])
59 m4_copy([abc], [foo])dnl
60 m4_stack_foreach([foo], [m4_n])
61 m4_stack_foreach_lifo([foo], [m4_n])
62 m4_stack_foreach_sep([abc], [ m4_index([abcdefghijkl],], [)])
63 m4_define([colon], [:])m4_define([lt], [<])m4_define([gt], [>])dnl
64 m4_stack_foreach_sep_lifo([abc], [lt], [gt], [colon])
65 m4_pushdef([xyz], [123])dnl
66 m4_pushdef([xyz], [456])dnl
67 m4_define([doit], [[$1](m4_stack_foreach_sep([xyz], [m4_dquote(], [)], [,]))
68 ])dnl
69 m4_stack_foreach([abc], [doit])]],
70 [[def
71 ghi
72 jkl
74 jkl
75 jkl
76 ghi
77 def
79 def
80 ghi
81 jkl
83 def
84 ghi
85 jkl
87 jkl
88 ghi
89 def
91  3 6 9
92 <jkl>:<ghi>:<def>
93 def([123],[456])
94 ghi([123],[456])
95 jkl([123],[456])
96 ]])
98 AT_CLEANUP
101 ## --------- ##
102 ## m4_defn.  ##
103 ## --------- ##
105 AT_SETUP([m4@&t@_defn])
107 AT_KEYWORDS([m4@&t@_popdef m4@&t@_undefine m4@&t@_copy m4@&t@_rename])
109 # Ensure that m4sugar dies when dereferencing undefined macros, whether
110 # this is provided by m4 natively or faked by wrappers in m4sugar.
112 AT_DATA_M4SUGAR([script.4s],
113 [[m4_define([good])
114 m4_defn([good], [oops])
117 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
118 AT_CHECK([grep good stderr], [1])
119 AT_CHECK([grep 'm4@&t@_defn: undefined.*oops' stderr], [0], [ignore])
121 AT_DATA_M4SUGAR([script.4s],
122 [[m4_define([good])
123 m4_popdef([good], [oops])
126 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
127 AT_CHECK([grep good stderr], [1])
128 AT_CHECK([grep 'm4@&t@_popdef: undefined.*oops' stderr], [0], [ignore])
130 AT_DATA_M4SUGAR([script.4s],
131 [[m4_define([good])
132 m4_undefine([good], [oops])
135 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
136 AT_CHECK([grep good stderr], [1])
137 AT_CHECK([grep 'm4@&t@_undefine: undefined.*oops' stderr], [0], [ignore])
139 # Check that pushdef stacks can be renamed.
140 AT_CHECK_M4SUGAR_TEXT([[m4_pushdef([a], [1])dnl
141 m4_pushdef([a], [2])dnl
142 m4_pushdef([a], m4_defn([m4_divnum]))dnl
143 a b c
144 m4_rename([a], [b])dnl
145 a b c
146 m4_copy([b], [c])dnl
147 a b c
148 m4_popdef([b], [c])dnl
149 a b c
150 m4_popdef([b], [c])dnl
151 a b c
152 m4_popdef([b], [c])dnl
153 a b c
154 ]], [[0 b c
155 a 0 c
156 a 0 0
157 a 2 2
158 a 1 1
159 a b c
162 AT_CLEANUP
165 ## ------------ ##
166 ## m4_dumpdef.  ##
167 ## ------------ ##
169 AT_SETUP([m4@&t@_dumpdef])
171 AT_KEYWORDS([m4@&t@_dumpdefs])
173 # Ensure that m4sugar dies when dereferencing undefined macros.
175 AT_DATA_M4SUGAR([script.4s],
176 [[m4_define([good], [yep])
177 m4_dumpdef([good], [oops])
180 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
181 AT_CHECK([grep '^good:  \[[yep\]]$' stderr], [0], [ignore])
182 AT_CHECK([grep 'm4@&t@_dumpdef: undefined.*oops' stderr], [0], [ignore])
184 # Check that pushdef stacks can be dumped.
185 AT_CHECK_M4SUGAR_TEXT([[m4_divert_push([KILL])
186 m4_pushdef([a], [1])
187 m4_pushdef([a], [2])
188 m4_dumpdef([a])
189 m4_dumpdefs([oops], [a])
190 m4_divert_pop([KILL])dnl
191 ]], [],
192 [[a:    [2]
193 a:      [2]
194 a:      [1]
197 # Check behavior when dumping builtins.  Unfortunately, when using M4 1.4.x
198 # (or more precisely, when __m4_version__ is undefined), builtins get
199 # flattened to an empty string.  It takes M4 1.6 to work around this.
200 AT_DATA_M4SUGAR([script.4s],
201 [[m4_ifdef([__m4_version__], [_m4_undefine([__m4_version__])])
202 m4_init
203 m4_dumpdef([m4_define])
206 AT_CHECK_M4SUGAR([-o-], [0], [],
207 [[m4_define:    []
210 AT_DATA_M4SUGAR([script.4s],
211 [[m4_init
212 m4_ifdef([__m4_version__],
213 [m4_dumpdef([m4_define])],
214 [m4_errprintn([m4_define:       <define>])])
217 AT_CHECK_M4SUGAR([-o-], [0], [],
218 [[m4_define:    <define>
221 AT_CLEANUP
224 ## --------- ##
225 ## m4_warn.  ##
226 ## --------- ##
228 AT_SETUP([m4@&t@_warn])
230 # m4_text_wrap is used to display the help strings.  Also, check that
231 # commas are not swallowed.  This can easily happen because of
232 # m4-listification.
234 # FIXME: For the time being we use -f to make sure we do issue the
235 # warnings.  But maybe autom4te should handle that by itself?
237 AT_DATA_M4SUGAR([script.4s],
238 [[m4_init
239 m4_defun([cross_warning], [m4_warn([cross],  [cross])])
241 m4_divert([0])dnl
242 m4_warn([obsolete],  [obsolete])dnl
243 cross_warning[]dnl
244 m4_warn([syntax], [syntax])dnl
247 AT_CHECK_M4SUGAR([-o-], 0, [],
248 [script.4s:7: warning: syntax
251 AT_CHECK_M4SUGAR([-o- -Wall -f], 0, [],
252 [script.4s:5: warning: obsolete
253 script.4s:6: warning: cross
254 script.4s:2: cross_warning is expanded from...
255 script.4s:6: the top level
256 script.4s:7: warning: syntax
259 AT_CHECK_M4SUGAR([-o- -Wnone,cross -f], 0, [],
260 [script.4s:6: warning: cross
261 script.4s:2: cross_warning is expanded from...
262 script.4s:6: the top level
265 AT_CHECK_M4SUGAR([-o- -Wnone,cross,error -f], 1, [],
266 [[script.4s:6: warning: cross
267 script.4s:2: cross_warning is expanded from...
268 script.4s:6: the top level
271 AT_CLEANUP
274 ## ----------------- ##
275 ## m4_divert_stack.  ##
276 ## ----------------- ##
278 AT_SETUP([m4@&t@_divert_stack])
279 AT_KEYWORDS([m4@&t@_divert m4@&t@_divert_push m4@&t@_divert_pop
280 m4@&t@_undivert m4@&t@_cleardivert m4@&t@_divert_text])
282 AT_CHECK_M4SUGAR_TEXT([[1.m4_divert_stack
283 m4_divert_push([10])2.m4_divert_stack
284 m4_divert_text([20], [3.m4_divert_stack])dnl
285 m4_divert([30])4.m4_divert_stack
286 m4_divert_pop([30])dnl
287 5.m4_undivert([20], [30])
288 m4_pattern_allow([^m4_divert])dnl
289 ]], [[1.script.4s:2: m4@&t@_divert_push: 0
290 script.4s:1: m4@&t@_divert: KILL
291 5.3.script.4s:5: m4@&t@_divert_push: 20
292 script.4s:4: m4@&t@_divert_push: 10
293 script.4s:2: m4@&t@_divert_push: 0
294 script.4s:1: m4@&t@_divert: KILL
295 4.script.4s:6: m4@&t@_divert: 30
296 script.4s:2: m4@&t@_divert_push: 0
297 script.4s:1: m4@&t@_divert: KILL
299 2.script.4s:4: m4@&t@_divert_push: 10
300 script.4s:2: m4@&t@_divert_push: 0
301 script.4s:1: m4@&t@_divert: KILL
304 AT_CHECK_M4SUGAR_TEXT([[dnl
305 m4_divert_text([3], [three])dnl
306 m4_divert_text([4], [four])dnl
307 m4_divert_text([1], [one])dnl
308 m4_divert_text([2], [two])dnl
309 m4_cleardivert([2], [3])dnl
311 [[one
312 four
315 AT_DATA_M4SUGAR([script.4s],
316 [[m4_divert_pop
318 AT_CHECK_M4SUGAR([-o-], [1], [],
319 [[script.4s:1: error: too many m4@&t@_divert_pop
320 script.4s:1: the top level
321 autom4te: m4 failed with exit status: 1
324 AT_DATA_M4SUGAR([script.4s],
325 [[m4_init
326 m4_divert_push([1])
327 m4_divert_pop([2])
329 AT_CHECK_M4SUGAR([-o-], [1], [],
330 [[script.4s:3: error: m4@&t@_divert_pop(2): diversion mismatch:
331 script.4s:2: m4@&t@_divert_push: 1
332 script.4s:1: m4@&t@_divert: KILL
333 script.4s:3: the top level
334 autom4te: m4 failed with exit status: 1
337 AT_DATA_M4SUGAR([script.4s],
338 [[m4_divert([1])
339 m4_init
340 m4_divert_push([2])
342 AT_CHECK_M4SUGAR([-o-], [1], [],
343 [[script.4s:2: error: m4@&t@_init: unbalanced m4@&t@_divert_push:
344 script.4s:3: m4@&t@_divert_push: 2
345 script.4s:2: m4@&t@_divert: KILL
346 script.4s:2: the top level
347 autom4te: m4 failed with exit status: 1
350 AT_CLEANUP
353 ## -------------------- ##
354 ## m4_expansion_stack.  ##
355 ## -------------------- ##
357 AT_SETUP([m4@&t@_expansion_stack])
359 AT_CHECK_M4SUGAR_TEXT([[1.m4_expansion_stack
360 m4_defun([a], [b])dnl
361 m4_define([c], [d])dnl
362 m4_defun([d], [2.m4_expansion_stack])dnl
363 m4_defun([b], [c])dnl
365 3.m4_ifdef([_m4_expansion_stack], [m4_expansion_stack])
366 ]], [[1.script.4s:3: the top level
367 2.script.4s:6: d is expanded from...
368 script.4s:7: b is expanded from...
369 script.4s:4: a is expanded from...
370 script.4s:8: the top level
374 AT_CLEANUP
377 ## --------------------------- ##
378 ## m4_require: error message.  ##
379 ## --------------------------- ##
381 AT_SETUP([m4@&t@_require: error message])
382 AT_KEYWORDS([m4@&t@_require])
384 AT_DATA_M4SUGAR([script.4s],
385 [[m4_defun([foo], [FOO])
386 m4_require([foo])
389 AT_CHECK_M4SUGAR([], 1, [],
390 [[script.4s:2: error: m4@&t@_require(foo): cannot be used outside of an m4_defun'd macro
391 script.4s:2: the top level
392 autom4te: m4 failed with exit status: 1
394 AT_CLEANUP
397 ## ----------------------------------- ##
398 ## m4_require: circular dependencies.  ##
399 ## ----------------------------------- ##
401 AT_SETUP([m4@&t@_require: circular dependencies])
402 AT_KEYWORDS([m4@&t@_require])
404 AT_DATA_M4SUGAR([script.4s],
405 [[m4_defun([foo], [m4_require([bar])])
407 m4_defun([bar], [m4_require([foo])])
409 m4_defun([baz], [m4_require([foo])])
411 m4_init
412 m4_divert([0])dnl
416 AT_CHECK_M4SUGAR([], 1, [],
417 [[script.4s:9: error: m4@&t@_require: circular dependency of foo
418 script.4s:3: bar is expanded from...
419 script.4s:1: foo is expanded from...
420 script.4s:5: baz is expanded from...
421 script.4s:9: the top level
422 autom4te: m4 failed with exit status: 1
424 AT_CLEANUP
427 ## ---------------------- ##
428 ## m4_require: one-shot.  ##
429 ## ---------------------- ##
431 AT_SETUP([m4@&t@_require: one-shot initialization])
432 AT_KEYWORDS([m4@&t@_require])
433 AT_KEYWORDS([m4@&t@_defun_init m4@&t@_copy m4@&t@_defun_once])
435 dnl check out m4_defun_init, m4_copy, and odd macro names
436 AT_CHECK_M4SUGAR_TEXT([[
437 m4_defun_init([a], [[init a
438 ]], [[common a]])dnl
439 m4_defun([b], [[b]m4_require([a])])dnl
440 m4_defun([c], [[c]m4_require([a])])dnl
444 m4_defun_init([-], [hello, ], [m4_if([$#], [0], [world], [[$1]])])dnl
445 m4_copy([-], [.])dnl
446 m4_indir([.])
447 m4_indir([.], [goodbye])
448 m4_indir([-], [again])
449 ]], [[
450 init a
451 common a
454 common a
455 hello, world
456 goodbye
457 hello, again
460 dnl Check m4_defun_once behavior
461 AT_CHECK_M4SUGAR_TEXT([[
462 m4_defun_once([a], [[a]])dnl
463 m4_defun([b], [[b]m4_require([a])])dnl
464 m4_defun([c], [[c]a[]m4_require([b])])dnl
467 m4_defun_once([d], [[d]m4_require([a])])dnl
469 m4_defun_once([e], [[e]])dnl
470 m4_defun([f], [[f]m4_require([e])e])dnl
472 ]], [[
483 AT_CLEANUP
486 ## -------------------- ##
487 ## m4_require: nested.  ##
488 ## -------------------- ##
490 AT_SETUP([m4@&t@_require: nested])
491 AT_KEYWORDS([m4@&t@_require m4@&t@_defun])
493 dnl From the m4sugar.m4 discourse: Require chains, top level
494 AT_CHECK_M4SUGAR_TEXT([[dnl
495 m4_defun([a], [[a]])dnl aka TEST2a
496 m4_defun([b], [[b]m4_require([a])])dnl aka TEST3
497 m4_defun([c], [[c]m4_require([b])])dnl aka TEST2b
498 m4_defun([d], [[d]m4_require([a])m4_require([c])])dnl aka TEST1
502 post
504 [[pre
510 post
513 dnl From the m4sugar.m4 discourse: Require chains, nested
514 AT_CHECK_M4SUGAR_TEXT([[dnl
515 m4_defun([a], [[a]])dnl aka TEST2a
516 m4_defun([b], [[b]m4_require([a])])dnl aka TEST3
517 m4_defun([c], [[c]m4_require([b])])dnl aka TEST2b
518 m4_defun([d], [[d]m4_require([a])m4_require([c])])dnl aka TEST1
519 m4_defun([wrap],
520 [pre
523 post])dnl
524 wrap
532 post
535 dnl Direct invocation, nested requires, top level
536 AT_CHECK_M4SUGAR_TEXT([[dnl
537 m4_defun([a], [[a]])dnl
538 m4_defun([b], [[b]m4_require([a])])dnl
539 m4_defun([c], [[c]m4_require([b])])dnl
545 post
547 [[pre
553 post
556 dnl Direct invocation, nested requires, nested defun.  This is an example
557 dnl of expansion before requirement, such that b occurs before its
558 dnl prerequisite a.  This indicates a bug in the macros (but not in
559 dnl autoconf), so we should be emitting a warning.
560 AT_CHECK_M4SUGAR_TEXT([[dnl
561 m4_defun([a], [[a]])dnl
562 m4_defun([b], [[b]m4_require([a])])dnl
563 m4_defun([c], [[c]m4_require([b])])dnl
564 m4_defun([outer],
565 [pre
570 post])dnl
571 outer
580 post
582 [[script.4s:14: warning: m4@&t@_require: `a' was expanded before it was required
583 script.4s:5: b is expanded from...
584 script.4s:6: c is expanded from...
585 script.4s:7: outer is expanded from...
586 script.4s:14: the top level
589 dnl Direct invocation, expand-before-require but no nested require.  As this
590 dnl is common in real life, but does not result in out-of-order expansion,
591 dnl we silently permit this.
592 AT_CHECK_M4SUGAR_TEXT([[dnl
593 m4_defun([a], [[a]])dnl
594 m4_defun([b], [[b]m4_require([a])])dnl
595 m4_defun([c], [[c]])dnl
596 m4_defun([d], [[d]m4_require([c])])dnl
597 pre1
602 post1
603 m4_defun([outer],
604 [pre2
609 post2])dnl
610 outer
611 m4_defun([e], [[e]])dnl
612 m4_defun([f], [[f]m4_require([e])])dnl
613 m4_defun([g], [[g]
615 f])dnl
616 m4_defun([h], [[h]m4_require([g])])dnl
618 m4_defun([i], [[i]])dnl
619 m4_defun([j], [[j]
620 i])dnl
621 m4_defun([k], [[k]m4_require([i])])dnl
622 m4_defun([l], [[l]m4_require([k])])dnl
623 m4_defun([m], [[m]m4_require([j])m4_require([l])])dnl
626 [[pre1
631 post1
632 pre2
637 post2
649 AT_CLEANUP
652 ## ------------------------------------------------- ##
653 ## m4_ifval, m4_ifblank, m4_ifset, m4_default, etc.  ##
654 ## ------------------------------------------------- ##
656 AT_SETUP([m4sugar shorthand conditionals])
657 AT_KEYWORDS([m4@&t@_ifval m4@&t@_ifblank m4@&t@_ifnblank m4@&t@_ifset
658 m4@&t@_default m4@&t@_default_quoted m4@&t@_default_nblank
659 m4@&t@_default_nblank_quoted])
661 AT_CHECK_M4SUGAR_TEXT([[m4_define([active], [ACTIVE])m4_define([empty])
662 m4_ifval([active], [yes], [no])
663 m4_ifval([empty], [yes], [no])
664 m4_ifval([ ], [yes], [no])
665 m4_ifval([], [yes], [no])
666 m4_ifblank([active], [yes], [no])
667 m4_ifblank([empty], [yes], [no])
668 m4_ifblank([ ], [yes], [no])
669 m4_ifblank([], [yes], [no])
670 m4_ifnblank([active], [yes], [no])
671 m4_ifnblank([empty], [yes], [no])
672 m4_ifnblank([ ], [yes], [no])
673 m4_ifnblank([], [yes], [no])
674 m4_ifset([active], [yes], [no])
675 m4_ifset([empty], [yes], [no])
676 m4_ifset([ ], [yes], [no])
677 m4_ifset([], [yes], [no])
679 m4_define([demo1], [m4_default([$1], [$2])])dnl
680 m4_define([demo2], [m4_default_quoted([$1], [$2])])dnl
681 m4_define([demo3], [m4_default_nblank([$1], [$2])])dnl
682 m4_define([demo4], [m4_default_nblank_quoted([$1], [$2])])dnl
683 demo1([active], [default])
684 demo1([], [active])
685 demo1([empty], [text])
686 -demo1([ ], [active])-
687 demo2([active], [default])
688 demo2([], [active])
689 demo2([empty], [text])
690 -demo2([ ], [active])-
691 demo3([active], [default])
692 demo3([], [active])
693 demo3([empty], [text])
694 -demo3([ ], [active])-
695 demo4([active], [default])
696 demo4([], [active])
697 demo4([empty], [text])
698 -demo4([ ], [active])-
699 ]], [[
717 ACTIVE
718 ACTIVE
720 - -
721 active
722 active
723 empty
724 - -
725 ACTIVE
726 ACTIVE
728 -ACTIVE-
729 active
730 active
731 empty
732 -active-
735 AT_CLEANUP
737 ## --------- ##
738 ## m4_cond.  ##
739 ## --------- ##
741 AT_SETUP([m4@&t@_cond])
743 AT_CHECK_M4SUGAR_TEXT([[m4_define([side], [m4_errprintn([$1])$1])
744 m4_cond([side(1)], [1], [a],
745         [side(1)], [1], [b],
746         [side(1)], [2], [c])
747 m4_cond([side(2)], [1], [a],
748         [side(2)], [1], [b],
749         [side(2)], [2], [c],
750         [side(2)])
751 m4_cond([side(3)], [1], [a],
752         [side(3)], [1], [b],
753         [side(3)], [2], [c],
754         [side(3)])
755 m4_cond([a,a], [a,a], [yes], [no])
756 m4_cond([[a,a]], [a,a], [yes])
757 m4_cond([a,a], [a,b], [yes], [no])
758 m4_cond([a,a], [a,b], [yes])
759 m4_cond([m4_eval([0xa])])
760 m4_define([ab], [AB])dnl
761 m4_cond([a])b
762 m4_cond([1], [1], [a])b
763 m4_cond([1], [2], [3], [a])b
764 ]], [[
776 ]], [[1
786 AT_CLEANUP
789 ## ---------- ##
790 ## m4 lists.  ##
791 ## ---------- ##
793 AT_SETUP([m4 lists])
795 AT_KEYWORDS([m4@&t@_car m4@&t@_cdr m4@&t@_argn _m4@&t_cdr])
797 AT_CHECK_M4SUGAR_TEXT([[dnl
798 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])
799 m4_argn([1], [a], [b], [c])
800 m4_argn([2], [a], [b], [c])
801 m4_argn([3], [a], [b], [c])
802 m4_argn([4], [a], [b], [c])
803 m4_car([a], [b], [c])
804 m4_cdr([a], [b], [c])
805 m4_cdr([a], [b])
806 m4_cdr([a])
807 _m4_cdr([a], [b], [c])
808 _m4_cdr([a], [b])
809 _m4_cdr([a])
810 m4_if(m4_cdr([], []), [[]], [good], [bad])
811 m4_if(m4_cdr([]), [], [good], [bad])
812 ]], [[
818 [b],[c]
821 , [b],[c]
822 , [b]
824 good
825 good
828 AT_DATA_M4SUGAR([script.4s],
829 [[m4_init
830 m4_argn([0], [a], [b], [c])
832 AT_CHECK_M4SUGAR([-o-], [1], [],
833 [[script.4s:2: error: assert failed: 0 < 0
834 script.4s:2: the top level
835 autom4te: m4 failed with exit status: 1
838 AT_CLEANUP
841 ## ---------- ##
842 ## m4_split.  ##
843 ## ---------- ##
845 AT_SETUP([m4@&t@_split])
847 AT_CHECK_M4SUGAR_TEXT(
848 [[m4_define([active], [ACT, IVE])m4_define([bd], [oops])
849 m4_split
850 m4_split([[]])
851 m4_split([ ])
852 m4_split([active])
853 m4_split([ active       active ])end
854 m4_split([ ], [ ])
855 m4_split([active], [ ])
856 m4_split([ active       active ], [ ])end
857 m4_split([abcde], [bd])
858 m4_split([abcde], [[bd]])
859 m4_split([foo=`` bar=''])
860 m4_split([foo='' bar=``])
861 dnl these next two are from the manual; keep this in sync if the internal
862 dnl quoting strings in m4_split are changed
863 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])dnl
864 m4_split([a )}>=- b -=<{( c])
865 m4_split([a )}@&t@>=- b -=<@&t@{( c])
869 [[]]
870 [], []
871 [active]
872 [], [active], [active], []end
873 [], []
874 [active]
875 [], [active     active], []end
876 [abcde]
877 [a], [c], [e]
878 [foo=``], [bar='']
879 [foo=''], [bar=``]
880 [a], [], [B], [], [c]
881 [a], [)}>=@&t@-], [b], [-@&t@=<{(], [c]
884 AT_CLEANUP
887 ## ------- ##
888 ## m4_do.  ##
889 ## ------- ##
891 AT_SETUP([m4@&t@_do])
893 AT_CHECK_M4SUGAR_TEXT(
894 [[m4_define([ab], [1])m4_define([bc], [2])m4_define([abc], [3])dnl
895 m4_define([AB], [4])m4_define([BC], [5])m4_define([ABC], [6])dnl
896 m4_do
897 m4_do([a])
898 m4_do([a], [b])c
899 m4_unquote(m4_join([], [a], [b]))c
900 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])dnl
901 m4_do([a], [b])c
902 m4_unquote(m4_join([], [a], [b]))c
912 AT_CLEANUP
915 ## ----------- ##
916 ## m4_append.  ##
917 ## ----------- ##
919 AT_SETUP([m4@&t@_append])
920 AT_KEYWORDS([m4@&t@_append_uniq m4@&t@_append_uniq_w])
922 AT_CHECK_M4SUGAR_TEXT(
923 [[m4_define([active], [ACTIVE])dnl
924 m4_append([sentence], [This is an])dnl
925 m4_append([sentence], [ active ])dnl
926 m4_append([sentence], [symbol.])dnl
927 sentence
928 m4_undefine([active])dnl
929 sentence
930 m4_define([active], [ACTIVE])dnl
931 m4_append([hooks], [m4_define([act1], [act2])])dnl
932 m4_append([hooks], [m4_define([act2], [active])])dnl
933 m4_undefine([active])dnl
934 act1
935 hooks
936 act1
937 dnl Test for bug fixed in 2.62 when separator is active.
938 m4_define([a], [A])dnl
939 m4_append_uniq([foo], [-], [a])dnl
940 m4_append_uniq([foo], [-], [a])dnl
941 m4_append_uniq([bar], [-], [a])dnl
942 m4_append_uniq([bar], [~], [a])dnl
943 m4_append_uniq([bar], [-], [a])dnl
944 m4_defn([foo])
945 m4_defn([bar])
948 m4_append_uniq([blah], [one], [, ], [new], [existing])
949 m4_append_uniq([blah], [two], [, ], [new], [existing])
950 m4_append_uniq([blah], [two], [, ], [new], [existing])
951 m4_append_uniq([blah], [three], [, ], [new], [existing])
952 m4_append([blah], [two], [, ])dnl
953 blah
954 m4_dquote(blah)
955 m4_append([list], [one], [[, ]])dnl
956 m4_append([list], [two], [[, ]])dnl
957 m4_append([list], [three], [[, ]])dnl
958 list
959 m4_dquote(list)
960 m4_append_uniq_w([numbers], [1 1 2])dnl
961 m4_append_uniq_w([numbers], [ 2 3 ])dnl
962 numbers
964 [[This is an ACTIVE symbol.
965 This is an active symbol.
966 act1
968 active
975 existing
977 one, two, three, two
978 [one],[two],[three],[two]
979 one, two, three
980 [one, two, three]
981 1 2 3
984 AT_DATA_M4SUGAR([script.4s],
985 [[m4_init[]dnl
986 m4_append_uniq([str], [a], [ ])
987 m4_append_uniq([str], [a b], [ ])
988 m4_divert([0])dnl
992 AT_CHECK_M4SUGAR([-o-], 0, [[a a b
993 ]], [[script.4s:3: warning: m4@&t@_append_uniq: `a b' contains ` '
996 AT_CLEANUP
999 ## --------- ##
1000 ## m4_join.  ##
1001 ## --------- ##
1003 AT_SETUP([m4@&t@_join])
1005 AT_KEYWORDS([m4@&t@_joinall])
1007 AT_CHECK_M4SUGAR_TEXT(
1008 [[m4_define([active], [ACTIVE])
1009 m4_join
1010 m4_join([|])
1011 m4_join([, ], [one], [two])
1012 m4_dquote(m4_join([, ], [one], [two]))
1013 m4_join([|], [active], [active])
1014 m4_join([|], ,,,[one])
1015 m4_join([|], [one],,,)
1016 m4_join([], ,,,[two])
1017 m4_join([], [two],,,)
1018 m4_join([ active ], [one], , [two])
1019 m4_join([], [one], [two])
1020 m4_joinall([-], [one], [], [two])
1021 m4_joinall([-], [], [], [three], [], [])
1022 m4_joinall([], [one], [], [two])
1023 m4_joinall
1024 m4_joinall([-])
1025 m4_joinall([-], [one])
1030 one, two
1031 [one, two]
1032 active|active
1037 one active two
1038 onetwo
1039 one--two
1040 --three--
1041 onetwo
1047 AT_CLEANUP
1050 ## ----------- ##
1051 ## m4_expand.  ##
1052 ## ----------- ##
1054 AT_SETUP([m4@&t@_expand])
1056 AT_CHECK_M4SUGAR_TEXT(
1057 [[m4_define([active], [ACTIVE])dnl
1058 m4_expand([#active
1059 active])
1060 m4_expand([[active]])
1061 dnl properly quoted case statements
1062 m4_expand([case a in @%:@(
1063   *) echo active, ;;
1064 esac
1065 case b in
1066   *[)] echo active, ;;
1067 esac])
1068 dnl unbalanced underquoted `)', but we manage anyway (gasp!)
1069 m4_expand([case c in #(
1070   *) echo active, ;;
1071 esac
1072 case d in
1073   *) echo active, ;;
1074 esac])
1075 dnl unterminated comment/dnl
1076 m4_expand([active # active])
1077 m4_expand([a
1078 dnl])
1079 m4_expand([a
1080 -dnl])
1082 [[#active
1083 ACTIVE
1084 active
1085 case a in #(
1086   *) echo ACTIVE, ;;
1087 esac
1088 case b in
1089   *) echo ACTIVE, ;;
1090 esac
1091 case c in #(
1092   *) echo ACTIVE, ;;
1093 esac
1094 case d in
1095   *) echo ACTIVE, ;;
1096 esac
1097 ACTIVE # active
1103 AT_CLEANUP
1106 ## -------------- ##
1107 ## m4_text_wrap.  ##
1108 ## -------------- ##
1110 AT_SETUP([m4@&t@_text_wrap])
1112 # m4_text_wrap is used to display the help strings.  Also, check that
1113 # commas and $ are not swallowed.  This can easily happen because of
1114 # m4-listification.
1116 AT_DATA_M4SUGAR([script.4s],
1117 [[m4_init[]m4_divert([0])dnl
1118 m4_text_wrap([Short string */], [   ], [/* ], 20)
1120 m4_text_wrap([Much longer string */], [   ], [/* ], 20)
1122 m4_text_wrap([Short doc.], [          ], [  --short ], 30)
1124 m4_text_wrap([Short doc.], [          ], [  --too-wide], 30)
1126 m4_text_wrap([Super long documentation.], [          ], [  --too-wide], 30)
1128 m4_text_wrap([First, second  , third, [,quoted]])
1129 m4_define([xfff], [oops])
1130 m4_text_wrap([Some $1 $2 $3 $4 embedded dollars.], [ $* ], [ $@ ], [0xfff & 20])
1133 AT_DATA([expout],
1134 [[/* Short string */
1136 /* Much longer
1137    string */
1139   --short Short doc.
1141   --too-wide
1142           Short doc.
1144   --too-wide
1145           Super long
1146           documentation.
1148 First, second , third, [,quoted]
1150  $@ Some $1 $2 $3
1151  $* $4 embedded
1152  $* dollars.
1155 AT_CHECK_M4SUGAR([-o-], 0, [expout])
1157 AT_CLEANUP
1159 ## -------------------- ##
1160 ## m4_version_compare.  ##
1161 ## -------------------- ##
1163 AT_SETUP([m4@&t@_version_compare])
1165 AT_KEYWORDS([m4@&t@_list_cmp])
1167 AT_CHECK_M4SUGAR_TEXT(
1168 [[m4_version_compare([1.1], [2.0])
1169 m4_version_compare([2.0b], [2.0a])
1170 m4_version_compare([2.0z], [2.0y])
1171 m4_version_compare([1.1.1], [1.1.1a])
1172 m4_version_compare([1.2], [1.1.1a])
1173 m4_version_compare([1.0], [1])
1174 m4_version_compare([1.0a], [1.0a])
1175 m4_version_compare([1.1a], [1.1a.1])
1176 m4_version_compare([1.10], [1.1a])
1177 m4_version_compare([1-1a], [1,1A])
1178 m4_define([a], [oops])dnl
1179 m4_version_compare([1.1a], [1.1A])
1180 m4_version_compare([1z], [1aa])
1181 m4_version_compare([2.61a], [2.61a-248-dc51])
1182 m4_version_compare([2.61b], [2.61a-248-dc51])
1183 m4_version_compare([08], [09])
1184 m4_version_compare([010], [8])
1185 dnl Test that side effects to m4_list_cmp occur exactly once
1186 m4_list_cmp([[0], [0], [0]m4_errprintn([hi])],
1187             [[0], [0], [0]m4_errprintn([hi])])
1188 m4_list_cmp([[0], [0], [0]m4_errprintn([hi])],
1189             [[0], [0], [0]m4_errprintn([bye])])
1191 [[-1
1209 ]], [[hi
1215 AT_CLEANUP
1217 ## ------------------------------ ##
1218 ## Standard regular expressions.  ##
1219 ## ------------------------------ ##
1221 AT_SETUP([Standard regular expressions])
1223 # AT_CHECK_M4RE(RE-NAME, TEXT, INTENT = `ok' | `')
1224 # ------------------------------------------------
1225 # Check whether RE-NAME (a macro whose definition is a regular expression)
1226 # matches TEXT.  INTENT = `ok' if the match should succeed or else empty.
1227 m4_define([AT_CHECK_M4RE],
1228 [AT_CHECK_M4SUGAR_TEXT(
1229 [[m4_bregexp([$2], ^m4_defn([$1])$, [ok])
1230 ]], [$3
1231 ])])
1233 AT_CHECK_M4RE([m4_re_word], [ab9_c], [ok])
1234 AT_CHECK_M4RE([m4_re_word], [_9abc], [ok])
1235 AT_CHECK_M4RE([m4_re_word], [9ab_c])
1237 AT_CHECK_M4RE([m4_re_string], [ab9_c], [ok])
1238 AT_CHECK_M4RE([m4_re_string], [_9abc], [ok])
1239 AT_CHECK_M4RE([m4_re_string], [9ab_c], [ok])
1240 AT_CHECK_M4RE([m4_re_string], [9a@_c])
1242 AT_CLEANUP
1244 ## ----------- ##
1245 ## m4_bmatch.  ##
1246 ## ----------- ##
1248 AT_SETUP([m4@&t@_bmatch])
1250 AT_CHECK_M4SUGAR_TEXT(
1251 [[m4_bmatch([abc], [default\])
1252 m4_bmatch([abc], [^a], [yes])
1253 m4_bmatch([abc], [^a], [yes], [no])
1254 m4_bmatch([abc], [^.a], [yes])
1255 m4_bmatch([abc], [^.a], [yes], [no\])
1256 m4_bmatch([abc], [a], [1], [b], [2])
1257 m4_bmatch([abc], [A], [1], [b], [2])
1258 m4_define([ab], [AB])dnl
1259 m4_bmatch([$*], [a])b
1260 m4_bmatch([$*], [\*], [a])b
1261 m4_bmatch([$*], [1], [2], [a])b
1262 ]], [[default\
1274 AT_CLEANUP
1276 ## --------------- ##
1277 ## m4_bpatsubsts.  ##
1278 ## --------------- ##
1280 AT_SETUP([m4@&t@_bpatsubsts])
1282 AT_CHECK_M4SUGAR_TEXT(
1283 [[m4_bpatsubsts([11], [^..$])
1284 m4_bpatsubsts([11], [\(.\)1], [\12])
1285 m4_bpatsubsts([11], [^..$], [], [1], [2])
1286 m4_bpatsubsts([11], [\(.\)1], [\12], [1], [3])
1287 m4_define([a], [oops])m4_define([c], [oops])dnl
1288 m4_define([AB], [good])m4_define([bc], [good])dnl
1289 m4_bpatsubsts([abc], [a], [A], [b], [B], [c])
1290 m4_bpatsubsts([ab], [a])c
1291 m4_bpatsubsts([ab], [c], [C], [a])c
1292 m4_bpatsubsts([$1$*$@], [\$\*], [$#])
1293 ]], [[11
1297 good
1298 good
1299 good
1300 $1$#$@
1303 AT_CLEANUP
1305 ## -------------- ##
1306 ## m4_esyscmd_s.  ##
1307 ## -------------- ##
1309 AT_SETUP([m4@&t@_esyscmd_s])
1310 AT_KEYWORDS([m4@&t@_chomp m4@&t@_chomp_all])
1312 AT_CHECK_M4SUGAR_TEXT(
1313 [[m4_define([world], [WORLD])dnl
1314 m4_chomp([abc])
1315 m4_chomp([world
1318 m4_esyscmd_s([echo hello world])
1319 m4_esyscmd_s([echo '[goodbye,
1320 cruel world
1322 ]'])
1323 ]], [[abc
1324 world
1326 hello WORLD
1327 goodbye,
1328 cruel world
1331 AT_CLEANUP
1333 ## ---------- ##
1334 ## M4 Loops.  ##
1335 ## ---------- ##
1337 AT_SETUP([M4 loops])
1339 AT_KEYWORDS([m4@&t@_for m4@&t@_foreach m4@&t@_foreach_w m4@&t@_map_args_w])
1341 AT_CHECK_M4SUGAR_TEXT([[dnl
1342 m4_define([myvar], [outer value])dnl
1343 m4_for([myvar], 1, 3, 1, [ myvar])
1344 m4_for([myvar], 1, 3,  , [ myvar])
1345 m4_for([myvar], 3, 1,-1, [ myvar])
1346 m4_for([myvar], 3, 1,  , [ myvar])
1347 m4_for([myvar], 1, 3, 2, [ myvar])
1348 m4_for([myvar], 3, 1,-2, [ myvar])
1349 m4_for([myvar],-1,-3,-2, [ myvar])
1350 m4_for([myvar],-3,-1, 2, [ myvar])
1351 dnl Make sure we recalculate the bounds correctly:
1352 m4_for([myvar], 1, 3, 3, [ myvar])
1353 m4_for([myvar], 1, 6, 3, [ myvar])
1354 m4_for([myvar],22,-7,-5, [ myvar])
1355 m4_for([myvar],-2,-7,-4, [ myvar])
1356 m4_for([myvar],-7,-2, 4, [ myvar])
1357 dnl Make sure we are not exposed to division truncation:
1358 m4_for([myvar], 2, 5, 2, [ myvar])
1359 m4_for([myvar],-5,-2, 2, [ myvar])
1360 m4_for([myvar], 5, 2,-2, [ myvar])
1361 m4_for([myvar],-2,-5,-2, [ myvar])
1362 dnl Make sure we do not divide by zero:
1363 m4_for([myvar], 1, 1,  , [ myvar])
1364 m4_for([myvar], 1, 1,+2, [ myvar])
1365 m4_for([myvar], 1, 1,-2, [ myvar])
1366 dnl Make sure we do not loop endlessly
1367 m4_for([myval], 1, 1, 0, [ myval])
1368 dnl Make sure to properly parenthesize
1369 m4_for([myvar], 3-5, -2+8, , [ myvar])
1370 m4_for([myvar], -2+8, 3-5, , [ myvar])
1371 m4_for([myvar], 8, 16, 3 * 2, [ myvar])
1372 m4_for([myvar], 8, 16, -3 * -2, [ myvar])
1373 m4_for([myvar], [2<<2], [2<<3], [-3 * (-2)], [ myvar])
1374 dnl Modifying var does not affect the number of iterations
1375 m4_for([myvar], 1, 5, , [ myvar[]m4_define([myvar], 5)])
1376 dnl Make sure we can do nameless iteration
1377 m4_for(, 1, 10, , -)
1378 dnl foreach tests
1379 m4_foreach([myvar], [[a], [b, c], [d], [e
1380 ],[f]], [ myvar|])
1381 m4_foreach_w([myvar], [a  b c, d,e f
1382 g], [ myvar|])
1383 myvar
1384 m4_map_args_w([a  b c, d,e f
1385 g], [ ], [|])
1386 m4_map_args_w([a b], [\1], [/])
1387 m4_define([dashes], [--])dnl
1388 m4_map_args_w([a b c], [/], [\1], [dashes])
1389 dnl only one side effect expansion, prior to visiting list elements
1390 m4_foreach([i], [[1], [2], [3]m4_errprintn([hi])], [m4_errprintn(i)])dnl
1391 dnl shifting forms an important part of loops
1392 m4_shift3:m4_shift3(1,2,3):m4_shift3(1,2,3,4)
1393 m4_shiftn(3,1,2,3):m4_shiftn(3,1,2,3,4)
1395 [[ 1 2 3
1396  1 2 3
1397  3 2 1
1398  3 2 1
1399  1 3
1400  3 1
1401  -1 -3
1402  -3 -1
1404  1 4
1405  22 17 12 7 2 -3
1406  -2 -6
1407  -7 -3
1408  2 4
1409  -5 -3
1410  5 3
1411  -2 -4
1416  -2 -1 0 1 2 3 4 5 6
1417  6 5 4 3 2 1 0 -1 -2
1418  8 14
1419  8 14
1420  8 14
1421  1 2 3 4 5
1422 ----------
1423  a| b, c| d| e
1424 | f|
1425  a| b| c,| d,e| f| g|
1426 outer value
1427  a| b| c,| d,e| f| g|
1428 \1a/\1b/
1429 /a\1--/b\1--/c\1
1432 ]], [[hi
1438 dnl bounds checking in m4_for
1439 AT_DATA_M4SUGAR([script.4s],
1440 [[m4_init
1441 m4_divert([0])dnl
1442 m4_for([myvar], 1, 3,-1, [ myvar])
1444 AT_CHECK_M4SUGAR([], 1, [],
1445 [[script.4s:3: error: assert failed: -1 > 0
1446 script.4s:3: the top level
1447 autom4te: m4 failed with exit status: 1
1450 AT_DATA_M4SUGAR([script.4s],
1451 [[m4_init
1452 m4_divert([0])dnl
1453 m4_for([myvar], 1, 2, 0, [ myvar])
1455 AT_CHECK_M4SUGAR([], 1, [],
1456 [[script.4s:3: error: assert failed: 0 > 0
1457 script.4s:3: the top level
1458 autom4te: m4 failed with exit status: 1
1461 AT_DATA_M4SUGAR([script.4s],
1462 [[m4_init
1463 m4_divert([0])dnl
1464 m4_for([myvar], 2, 1, 0, [ myvar])
1466 AT_CHECK_M4SUGAR([], 1, [],
1467 [[script.4s:3: error: assert failed: 0 < 0
1468 script.4s:3: the top level
1469 autom4te: m4 failed with exit status: 1
1472 dnl m4_shiftn also does bounds checking
1473 AT_DATA_M4SUGAR([script.4s],
1474 [[m4_init
1475 m4_divert([0])dnl
1476 m4_shiftn(3,1,2)
1478 AT_CHECK_M4SUGAR([], 1, [],
1479 [[script.4s:3: error: assert failed: 0 < 3 && 3 < 3
1480 script.4s:3: the top level
1481 autom4te: m4 failed with exit status: 1
1484 AT_CLEANUP
1487 ## --------------------- ##
1488 ## m4_map{,all}{,_sep}.  ##
1489 ## --------------------- ##
1491 AT_SETUP([m4@&t@_map])
1492 AT_KEYWORDS([m4@&t@_apply m4@&t@_map_sep m4@&t@_mapall m4@&t@_mapall_sep])
1493 AT_KEYWORDS([m4@&t@_count])
1495 AT_CHECK_M4SUGAR_TEXT([[dnl
1496 m4_map([m4_count], [])
1497 m4_map([ m4_count], [[],
1498                      [[1]],
1499                      [[1], [2]]])
1500 m4_mapall([ m4_count], [[],
1501                         [[1]],
1502                         [[1], [2]]])
1503 m4_map_sep([m4_eval], [,], [[[1+2]],
1504                             [[10], [16]]])
1505 m4_count(m4_map_sep([m4_echo], [,], [[], [[1]], [[2]]]))
1506 m4_count(m4_mapall_sep([m4_echo], [,], [[], [[1]], [[2]]]))
1507 m4_map_sep([m4_eval], [[,]], [[[1+2]],
1508                               [[10], [16]]])
1509 m4_count(m4_map_sep([m4_echo], [[,]], [[], [[1]], [[2]]]))
1510 m4_count(m4_mapall_sep([m4_echo], [[,]], [[], [[1]], [[2]]]))
1511 m4_map([-], [[]])
1512 m4_mapall([-], [[]])
1513 m4_map_sep([-], [:], [[]])
1514 m4_mapall_sep([-], [:], [[]])
1515 m4_define([a], [m4_if([$#], [0], [oops], [$1], [a], [pass], [oops])])dnl
1516 m4_define([a1], [oops])dnl
1517 m4_define([pass1], [oops])dnl
1518 m4_map([a], [[[a]]])1
1519 m4_map([m4_unquote([a])], [m4_dquote([a])])
1520 dnl only one side effect expansion, prior to visiting list elements
1521 m4_map([m4_errprintn], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1522 m4_map_sep([m4_errprintn], [], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1523 m4_mapall([m4_errprintn], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1524 m4_mapall_sep([m4_errprintn], [], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1527  1 2
1528  0 1 2
1539 pass1
1540 pass
1541 ]], [[hi
1559 AT_CLEANUP
1562 ## --------------------------------------- ##
1563 ## m4_map_args{,_sep,_pair} and m4_curry.  ##
1564 ## --------------------------------------- ##
1566 AT_SETUP([m4@&t@_map_args and m4@&t@_curry])
1567 AT_KEYWORDS([m4@&t@_map_args_sep m4@&t@_map_args_pair m4@&t@_reverse
1568 m4@&t@_map])
1570 dnl First, make sure we can curry in isolation.
1571 AT_CHECK_M4SUGAR_TEXT(
1572 [[m4_curry([m4_echo])([1])
1573 m4_curry([m4_curry], [m4_reverse], [1])([2])([3])
1574 m4_define([add], [m4_eval(([$1]) + ([$2]))])dnl
1575 m4_define([add_one], [m4_curry([add], [1])])dnl
1576 add_one()([4])
1579 3, 2, 1
1583 dnl Now, check that we can map a list of arguments.
1584 AT_CHECK_M4SUGAR_TEXT([[m4_define([active], [ACTIVE])dnl
1585 m4_map_args([ m4_echo])
1586 m4_map_args([ m4_echo], [plain], [active])
1587 m4_map_args([m4_unquote], [plain], [active])
1588 m4_map_args_pair([, m4_reverse], [])
1589 m4_map_args_pair([, m4_reverse], [], [1])
1590 m4_map_args_pair([, m4_reverse], [], [1], [2])
1591 m4_map_args_pair([, m4_reverse], [], [1], [2], [3])
1592 m4_map_args_pair([, m4_reverse], [], [1], [2], [3], [4])
1593 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1])
1594 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2])
1595 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3])
1596 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3], [4])
1597 m4_map_args_sep([<], [>], [:], [1], [2], [3])
1598 m4_map_args_sep([m4_echo(], [)], [ ], [plain], [active])
1601  plain active
1602 plainACTIVE
1604 , 1
1605 , 2, 1
1606 , 2, 1, 3
1607 , 2, 1, 4, 3
1608 , [1]
1609 , 2, 1
1610 , 2, 1, [3]
1611 , 2, 1, 4, 3
1612 <1>:<2>:<3>
1613 plain active
1616 dnl Finally, put the two concepts together, to show the real power of the API.
1617 AT_CHECK_M4SUGAR_TEXT(
1618 [[m4_define([add], [m4_eval(([$1]) + ([$2]))])dnl
1619 m4_define([list], [[-1], [0], [1]])dnl
1620 dnl list_add_n(value, arg...)
1621 dnl add VALUE to each ARG and output the resulting list
1622 m4_define([list_add_n],
1623   [m4_shift(m4_map_args([,m4_curry([add], [$1])], m4_shift($@)))])
1624 list_add_n([1], list)
1625 list_add_n([2], list)
1626 ]], [[
1627 0,1,2
1628 1,2,3
1631 AT_CLEANUP
1634 ## ------------ ##
1635 ## m4_combine.  ##
1636 ## ------------ ##
1638 AT_SETUP([m4@&t@_combine])
1640 AT_CHECK_M4SUGAR_TEXT([[m4_define([a], [oops])dnl
1641 m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
1642 m4_combine([, ], [[a], [b]], [-])
1643 m4_combine([, ], [[a], [b]], [-], [])
1644 m4_combine([, ], [], [-], [a], [b])
1645 m4_combine([, ], [[]], [-], [a], [b])
1646 m4_combine([ a ], [[-], [+]], [a], [-], [+])
1647 m4_combine([$* ], [[$1], [$2]], [$#], [$@])
1649 [[a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
1651 a-, b-
1653 -a, -b
1654 -a- a -a+ a +a- a +a+
1655 $1$#$@$* $2$#$@
1656 ]], [])
1658 AT_CLEANUP
1661 ## -------------- ##
1662 ## m4_{max,min}.  ##
1663 ## -------------- ##
1665 AT_SETUP([m4@&t@_max and m4@&t@_min])
1667 AT_DATA_M4SUGAR([script.4s],
1668 [[m4_max
1671 AT_CHECK_M4SUGAR([], 1, [],
1672 [[script.4s:1: error: too few arguments to m4@&t@_max
1673 script.4s:1: the top level
1674 autom4te: m4 failed with exit status: 1
1677 AT_DATA_M4SUGAR([script.4s],
1678 [[m4_min
1681 AT_CHECK_M4SUGAR([], 1, [],
1682 [[script.4s:1: error: too few arguments to m4@&t@_min
1683 script.4s:1: the top level
1684 autom4te: m4 failed with exit status: 1
1687 AT_CHECK_M4SUGAR_TEXT([[dnl
1688 m4_min(0)
1689 m4_min(0xa)
1690 m4_min(0, 0)
1691 m4_min(0, 1)
1692 m4_min(1, 0)
1693 m4_min(0+1, 1+1)
1694 m4_min(0+1, 1+0)
1695 m4_min(0, 1, 2)
1696 m4_min(2, 1, 0)
1697 m4_min(1m4_for([i], 2, 100, , [,i]))
1698 m4_min(m4_for([i], 100, 2, , [i,])1)
1699 ----
1700 m4_max(0)
1701 m4_max(0xa)
1702 m4_max(0, 0)
1703 m4_max(0, 1)
1704 m4_max(1, 0)
1705 m4_max(1+0, 1+1)
1706 m4_max(1+0, 1+0)
1707 m4_max(0, 1, 2)
1708 m4_max(2, 1, 0)
1709 m4_max(1m4_for([i], 2, 100, , [,i]))
1710 m4_max(m4_for([i], 100, 2, , [i,])1)
1723 ----
1735 ]], [])
1737 AT_CLEANUP
1740 ## ----------- ##
1741 ## Recursion.  ##
1742 ## ----------- ##
1744 AT_SETUP([recursion])
1746 AT_KEYWORDS([m4@&t@_foreach m4@&t@_foreach_w m4@&t@_case m4@&t@_cond
1747 m4@&t@_bpatsubsts m4@&t@_shiftn m4@&t@_do m4@&t@_dquote_elt m4@&t@_reverse
1748 m4@&t@_map m4@&t@_join m4@&t@_joinall m4@&t@_list_cmp m4@&t@_max m4@&t@_min
1749 m4@&t@_bmatch m4@&t@_map_args m4@&t@_map_args_pair])
1751 dnl This test completes in a reasonable time if m4_foreach is linear,
1752 dnl but thrashes if it is quadratic.  If we are testing with m4 1.4.x,
1753 dnl only the slower foreach.m4 implementation will work.  But if we
1754 dnl are testing with m4 1.6, we can rerun the test with __m4_version__
1755 dnl undefined to exercise the alternate code path.
1756 AT_DATA_M4SUGAR([script.4s],
1757 [[m4_init
1758 m4_divert_push(0)[]dnl
1759 m4_len(m4_foreach_w([j], m4_do(m4_for([i], [1], [10000], [], [,i ])), [j ]))
1760 m4_shiftn(9998m4_for([i], [1], [10000], [], [,i]))
1761 m4_len(m4_join([--],, m4_dquote_elt(m4_for([i], [1], [10000], [], [,i])),))
1762 m4_len(m4_joinall([--], m4_map([, m4_echo],
1763   m4_dquote([1]m4_for([i], [2], [10000], [], [,i])))))
1764 m4_max(m4_min([1]m4_for([i], [2], [10000], [],
1765   [,i]))m4_for([i], [2], [10000], [], [,i]))
1766 m4_case([10000]m4_for([i], [1], [10000], [], [,i]),[end])
1767 m4_list_cmp(m4_dquote(1m4_for([i], [2], [10000], [], [,i])),
1768   m4_dquote(m4_reverse(10000m4_for([i], [9999], [1], [], [,i])), [0]))
1769 m4_list_cmp([0], [0m4_for([i], [1], [10000], [], [,0])])
1770 m4_list_cmp([0m4_for([i], [1], [10000], [], [,0])], [0])
1771 m4_for([i], [1], [10000], [], [m4_define(i)])dnl
1772 m4_undefine(1m4_for([i], [2], [10000], [], [,i]))dnl
1773 m4_bpatsubsts([a1]m4_for([i], [1], [10000], [], [,i]), [a2], [A])
1774 m4_bmatch([9997]m4_for([i], [1], [10000], [], [,^i$]))
1775 m4_define([up], [m4_define([$1], m4_incr($1))$1])m4_define([j], 0)dnl
1776 m4_cond(m4_for([i], [1], [10000], [], [[up([j])], [9990], i,]) [oops]) j
1777 m4_count(m4_map_args_pair([,m4_quote], []m4_map_args([,m4_echo]m4_for([i],
1778   [1], [10000], [], [,i]))))
1779 m4_divert_pop(0)
1782 AT_CHECK_M4SUGAR([-o-], [0], [[48894
1783 9999,10000
1784 78896
1785 58894
1786 10000
1792 ^9998$
1793 9990 9990
1794 5001
1797 AT_DATA_M4SUGAR([script.4s],
1798 [[m4_ifdef([__m4_version__],
1799 [m4_undefine([__m4_version__])],
1800 [m4_divert_push(0)48894
1801 9999,10000
1802 78896
1803 58894
1804 10000
1810 ^9998$
1811 9990 9990
1812 5001
1813 m4_exit([0])])
1814 m4_init
1815 m4_divert_push(0)[]dnl
1816 m4_len(m4_foreach_w([j], m4_do(m4_for([i], [1], [10000], [], [,i ])), [j ]))
1817 m4_shiftn(9998m4_for([i], [1], [10000], [], [,i]))
1818 m4_len(m4_join([--],, m4_dquote_elt(m4_for([i], [1], [10000], [], [,i])),))
1819 m4_len(m4_joinall([--], m4_map([, m4_echo],
1820   m4_dquote([1]m4_for([i], [2], [10000], [], [,i])))))
1821 m4_max(m4_min([1]m4_for([i], [2], [10000], [],
1822   [,i]))m4_for([i], [2], [10000], [], [,i]))
1823 m4_case([10000]m4_for([i], [1], [10000], [], [,i]),[end])
1824 m4_list_cmp(m4_dquote(1m4_for([i], [2], [10000], [], [,i])),
1825   m4_dquote(m4_reverse(10000m4_for([i], [9999], [1], [], [,i])), [0]))
1826 m4_list_cmp([0], [0m4_for([i], [1], [10000], [], [,0])])
1827 m4_list_cmp([0m4_for([i], [1], [10000], [], [,0])], [0])
1828 m4_for([i], [1], [10000], [], [m4_define(i)])dnl
1829 m4_undefine(1m4_for([i], [2], [10000], [], [,i]))dnl
1830 m4_bpatsubsts([a1]m4_for([i], [1], [10000], [], [,i]), [a2], [A])
1831 m4_bmatch([9997]m4_for([i], [1], [10000], [], [,^i$]))
1832 m4_define([up], [m4_define([$1], m4_incr($1))$1])m4_define([j], 0)dnl
1833 m4_cond(m4_for([i], [1], [10000], [], [[up([j])], [9990], i,]) [oops]) j
1834 m4_count(m4_map_args_pair([,m4_quote], []m4_map_args([,m4_echo]m4_for([i],
1835   [1], [10000], [], [,i]))))
1836 m4_divert_pop(0)
1839 AT_CHECK_M4SUGAR([-o-], [0], [[48894
1840 9999,10000
1841 78896
1842 58894
1843 10000
1849 ^9998$
1850 9990 9990
1851 5001
1854 AT_CLEANUP
1857 ## ---------- ##
1858 ## m4_set_*.  ##
1859 ## ---------- ##
1861 AT_SETUP([m4@&t@_set])
1863 AT_KEYWORDS([m4@&t@_set_add m4@&t@_set_add_all m4@&t@_set_contains
1864 m4@&t@_set_contents m4@&t@_set_delete m4@&t@_set_difference m4@&t@_set_dump
1865 m4@&t@_set_empty m4@&t@_set_foreach m4@&t@_set_intersection m4@&t@_set_list
1866 m4@&t@_set_listc m4@&t@_set_map m4@&t@_set_remove m4@&t@_set_size
1867 m4@&t@_set_union])
1869 # Simple tests
1870 AT_CHECK_M4SUGAR_TEXT([[m4_set_contains([a], [1], [yes], [no])
1871 m4_set_add([a], [1], [added], [dup])
1872 m4_set_contains([a], [1], [yes], [no])
1873 m4_set_add([a], [1], [added], [dup])
1874 m4_set_contents([a])
1875 m4_set_remove([a], [1], [removed], [missing])
1876 m4_set_contains([a], [1], [yes], [no])
1877 m4_set_remove([a], [1], [removed], [missing])
1878 m4_set_add([a], [2], [added], [dup])
1879 m4_set_empty([a], [yes], [no])
1880 m4_set_delete([a])
1881 m4_set_empty([a], [yes], [no])
1882 m4_set_add_all([c], [1], [2], [3])
1883 m4_set_add_all([a]m4_set_listc([c]))
1884 m4_set_contents([c], [-])
1885 m4_set_dump([a], [-])
1886 m4_set_contents([a])
1887 m4_set_add_all([a], [1], [2], [3])m4_set_add_all([b], [3], [], [4])
1888 m4_set_difference([a], [b])
1889 m4_set_difference([b], [a])
1890 m4_set_intersection([a], [b])
1891 m4_set_union([a], [b])
1892 m4_define([printodd], [m4_if(m4_eval([$1 & 1]), [1], [:$1])])dnl
1893 m4_set_map([a], [printodd])
1894 m4_set_foreach([a], [i], [m4_if(m4_eval(i & 1), [1], [m4_set_remove([a], i)])])
1895 m4_set_list([a])
1896 m4_set_add([a], [])
1897 m4_set_list([a])
1898 m4_set_remove([a], [2])
1899 m4_dquote(m4_set_list([a]))
1900 m4_set_listc([a])
1901 m4_set_size([a])
1902 m4_set_delete([a])
1903 m4_dquote(m4_set_list([a]))
1904 m4_indir([m4_dquote]m4_set_listc([a]))
1905 m4_set_listc([a])
1906 m4_set_size([a])
1907 ]], [[no
1908 added
1912 removed
1914 missing
1915 added
1921 1-2-3
1922 3-2-1
1925 ,1,2
1928 ,1,2,3,,4
1929 :1:3
1945 # Stress tests - check for unusual names/values
1946 AT_CHECK_M4SUGAR_TEXT([[m4_define([a], [oops])dnl
1947 m4_set_add([a], [a])dnl
1948 m4_set_remove([a], [oops], [yes], [no])
1949 m4_set_add([a,b], [c])dnl
1950 m4_set_add([a,b], [$*[]])dnl
1951 m4_set_add_all([a], [b,c])dnl
1952 m4_set_size([a])
1953 m4_count(m4_set_contents([a], [,]))
1954 m4_count(m4_set_list([a], [,]))
1955 m4_set_dump([a], [,])
1956 m4_set_contents([a,b], [,])
1957 m4_set_list([a,b])
1958 m4_set_foreach([$*[]], [$*[]], [oops])
1959 m4_set_add([$*[]], [])dnl
1960 m4_set_remove([$*[]], [a], [yes], [no])
1961 m4_set_add([$*[]], [a])dnl
1962 m4_set_foreach([$*[]], [$*[]], [-m4_defn([$*[]])m4_indir([$*[]])-])
1963 m4_set_remove([$*[]], [], [yes], [no])
1964 m4_set_add([c], [,])dnl
1965 m4_set_foreach([a,b], [set], [:m4_set_listc(_m4_defn([set])):])
1966 ]],[[no
1970 b,c,a
1971 c,$*[]
1972 c,$*[]
1975 ---aoops-
1977 :,,::,a:
1980 # Stress tests - check for linear scaling (won't necessarily fail if
1981 # quadratic, but hopefully users will complain if it appears to hang)
1982 AT_CHECK_M4SUGAR_TEXT([[dnl
1983 m4_for([i], [1], [10000], [], [m4_set_add([a], i)])dnl
1984 m4_set_add_all([b]m4_for([i], [1], [10000], [], [,i]))dnl
1985 m4_set_remove([a], [1])dnl
1986 m4_set_remove([b], [10000])dnl
1987 m4_set_add_all([a]m4_for([i], [1], [10000], [], [,i]))dnl
1988 m4_for([i], [1], [10000], [], [m4_set_add([b], i)])dnl
1989 m4_len(m4_set_contents([a]))
1990 m4_len(m4_set_foreach([b], [b], [m4_if(m4_eval(b & 1), [1],
1991   [m4_set_remove([b], b, [-])])]))
1992 m4_set_size([b])
1993 m4_define([prune3x], [m4_if(m4_eval([$1 % 3]), [0],
1994   [m4_set_remove([a], [$1], [-])])])dnl
1995 m4_len(m4_set_map([a], [prune3x]))
1996 m4_count(m4_shift(m4_set_intersection([a], [b])))
1997 ]], [[38894
1998 5000
1999 5000
2000 3333
2001 3334
2004 AT_CLEANUP