docs: avoid first person, and credit history to David MacKenzie
[autoconf.git] / tests / m4sugar.at
blobf5182dc922e0c9dfda4bc080d52719712ff66667
1 #                                                       -*- Autotest -*-
3 AT_BANNER([M4sugar.])
5 # Copyright (C) 2000, 2001, 2002, 2005, 2006, 2007, 2008, 2009, 2010
6 # Free 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 3 of the License, or
11 # (at your option) 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, see <http://www.gnu.org/licenses/>.
22 # AT_CHECK_M4SUGAR_TEXT(CODE, STDOUT, STDERR)
23 # -------------------------------------------
24 # Check that m4sugar CODE expands to STDOUT and emits STDERR.
25 m4_define([AT_CHECK_M4SUGAR_TEXT],
27 AT_DATA_M4SUGAR([script.4s],
28 [[m4_init
29 m4_divert_push([])[]dnl
30 ]$1[[]dnl
31 m4_divert_pop([])
32 ]])
34 AT_CHECK_M4SUGAR([-o-],, [$2], [$3])
35 ])# AT_CHECK_M4SUGAR_TEXT
38 ## ------------------ ##
39 ## m4_stack_foreach.  ##
40 ## ------------------ ##
42 AT_SETUP([m4@&t@_stack])
44 AT_KEYWORDS([m4@&t@_stack_foreach m4@&t@_stack_foreach_lifo])
45 AT_KEYWORDS([m4@&t@_stack_foreach_sep m4@&t@_stack_foreach_sep_lifo])
46 AT_KEYWORDS([m4@&t@_copy m4@&t@_n])
48 # Test the semantics of macros to walk stacked macro definitions.
49 AT_CHECK_M4SUGAR_TEXT([[dnl
50 m4_pushdef([abc], [def])dnl
51 m4_pushdef([abc], [ghi])dnl
52 m4_pushdef([abc], [jkl])dnl
53 m4_stack_foreach([abc], [m4_n])
54 abc
55 m4_stack_foreach_lifo([abc], [m4_n])
56 m4_stack_foreach([abc], [m4_n])
57 m4_copy([abc], [foo])dnl
58 m4_stack_foreach([foo], [m4_n])
59 m4_stack_foreach_lifo([foo], [m4_n])
60 m4_stack_foreach_sep([abc], [ m4_index([abcdefghijkl],], [)])
61 m4_define([colon], [:])m4_define([lt], [<])m4_define([gt], [>])dnl
62 m4_stack_foreach_sep_lifo([abc], [lt], [gt], [colon])
63 m4_pushdef([xyz], [123])dnl
64 m4_pushdef([xyz], [456])dnl
65 m4_define([doit], [[$1](m4_stack_foreach_sep([xyz], [m4_dquote(], [)], [,]))
66 ])dnl
67 m4_stack_foreach([abc], [doit])]],
68 [[def
69 ghi
70 jkl
72 jkl
73 jkl
74 ghi
75 def
77 def
78 ghi
79 jkl
81 def
82 ghi
83 jkl
85 jkl
86 ghi
87 def
89  3 6 9
90 <jkl>:<ghi>:<def>
91 def([123],[456])
92 ghi([123],[456])
93 jkl([123],[456])
94 ]])
96 AT_CLEANUP
99 ## --------- ##
100 ## m4_defn.  ##
101 ## --------- ##
103 AT_SETUP([m4@&t@_defn])
105 AT_KEYWORDS([m4@&t@_popdef m4@&t@_undefine m4@&t@_copy m4@&t@_rename
106 m4@&t@_copy_force m4@&t@_rename_force])
108 # Ensure that m4sugar dies when dereferencing undefined macros, whether
109 # this is provided by m4 natively or faked by wrappers in m4sugar.
111 AT_DATA_M4SUGAR([script.4s],
112 [[m4_define([good])
113 m4_defn([good], [oops])
116 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
117 AT_CHECK([grep good stderr], [1])
118 AT_CHECK([grep 'm4@&t@_defn: undefined.*oops' stderr], [0], [ignore])
120 AT_DATA_M4SUGAR([script.4s],
121 [[m4_define([good])
122 m4_popdef([good], [oops])
125 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
126 AT_CHECK([grep good stderr], [1])
127 AT_CHECK([grep 'm4@&t@_popdef: undefined.*oops' stderr], [0], [ignore])
129 AT_DATA_M4SUGAR([script.4s],
130 [[m4_define([good])
131 m4_undefine([good], [oops])
134 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
135 AT_CHECK([grep good stderr], [1])
136 AT_CHECK([grep 'm4@&t@_undefine: undefined.*oops' stderr], [0], [ignore])
138 # Cannot rename an undefined macro.
139 AT_DATA_M4SUGAR([script.4s],
140 [[m4_rename([oops], [good])
143 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
144 AT_CHECK([grep 'm4@&t@_undefine: undefined.*oops' stderr], [0], [ignore])
146 # Check that pushdef stacks can be renamed.
147 AT_CHECK_M4SUGAR_TEXT([[m4_pushdef([a], [1])dnl
148 m4_pushdef([a], [2])dnl
149 m4_pushdef([a], m4_defn([m4_divnum]))dnl
150 a b c
151 m4_rename([a], [b])dnl
152 a b c
153 m4_copy([b], [c])dnl
154 a b c
155 m4_popdef([b], [c])dnl
156 a b c
157 m4_popdef([b], [c])dnl
158 a b c
159 m4_popdef([b], [c])dnl
160 a b c
161 dnl m4_copy is intentionally a no-op on undefined source
162 m4_copy([oops], [dummy])m4_ifdef([dummy], [[oops]])dnl
163 dnl allow forceful overwrites
164 m4_define([d], [4])m4_define([e], [5])m4_define([f], [6])dnl
165 m4_copy_force([d], [e])dnl
166 m4_rename_force([d], [f])dnl
167 d e f
168 m4_popdef([e], [f])dnl
169 d e f
170 ]], [[0 b c
171 a 0 c
172 a 0 0
173 a 2 2
174 a 1 1
175 a b c
176 d 4 4
177 d e f
180 AT_CLEANUP
183 ## ------------ ##
184 ## m4_dumpdef.  ##
185 ## ------------ ##
187 AT_SETUP([m4@&t@_dumpdef])
189 AT_KEYWORDS([m4@&t@_dumpdefs])
191 # Ensure that m4sugar dies when dereferencing undefined macros.
193 AT_DATA_M4SUGAR([script.4s],
194 [[m4_define([good], [yep])
195 m4_dumpdef([good], [oops])
198 AT_CHECK_M4SUGAR([-o-], 1, [], [stderr])
199 AT_CHECK([grep '^good:  \[[yep\]]$' stderr], [0], [ignore])
200 AT_CHECK([grep 'm4@&t@_dumpdef: undefined.*oops' stderr], [0], [ignore])
202 # Check that pushdef stacks can be dumped.
203 AT_CHECK_M4SUGAR_TEXT([[m4_divert_push([KILL])
204 m4_pushdef([a], [1])
205 m4_pushdef([a], [2])
206 m4_dumpdef([a])
207 m4_dumpdefs([oops], [a])
208 m4_divert_pop([KILL])dnl
209 ]], [],
210 [[a:    [2]
211 a:      [2]
212 a:      [1]
215 # Check behavior when dumping builtins.  Unfortunately, when using M4 1.4.x
216 # (or more precisely, when __m4_version__ is undefined), builtins get
217 # flattened to an empty string.  It takes M4 1.6 to work around this.
218 AT_DATA_M4SUGAR([script.4s],
219 [[m4_ifdef([__m4_version__], [_m4_undefine([__m4_version__])])
220 m4_init
221 m4_dumpdef([m4_define])
224 AT_CHECK_M4SUGAR([-o-], [0], [],
225 [[m4_define:    []
228 AT_DATA_M4SUGAR([script.4s],
229 [[m4_init
230 m4_ifdef([__m4_version__],
231 [m4_dumpdef([m4_define])],
232 [m4_errprintn([m4_define:       <define>])])
235 AT_CHECK_M4SUGAR([-o-], [0], [],
236 [[m4_define:    <define>
239 AT_CLEANUP
242 ## --------- ##
243 ## m4_warn.  ##
244 ## --------- ##
246 AT_SETUP([m4@&t@_warn])
248 AT_DATA_M4SUGAR([script.4s],
249 [[m4_init
250 m4_defun([cross_warning], [m4_warn([cross], [cross])])
252 m4_divert([0])dnl
253 m4_warn([obsolete], [obsolete])dnl
254 cross_warning[]dnl
255 m4_warn([syntax], [syntax])dnl
256 cross_warning[]dnl
257 m4_warn([syntax], [syntax])dnl
260 AT_CHECK_M4SUGAR([-o-], 0, [],
261 [script.4s:4: warning: prefer named diversions
262 script.4s:7: warning: syntax
263 script.4s:9: warning: syntax
266 AT_CHECK_M4SUGAR([-o- -Wall], 0, [],
267 [script.4s:4: warning: prefer named diversions
268 script.4s:5: warning: obsolete
269 script.4s:6: warning: cross
270 script.4s:2: cross_warning is expanded from...
271 script.4s:6: the top level
272 script.4s:7: warning: syntax
273 script.4s:8: warning: cross
274 script.4s:2: cross_warning is expanded from...
275 script.4s:8: the top level
276 script.4s:9: warning: syntax
279 AT_CHECK_M4SUGAR([-o- -Wnone,cross], 0, [],
280 [script.4s:6: warning: cross
281 script.4s:2: cross_warning is expanded from...
282 script.4s:6: the top level
283 script.4s:8: warning: cross
284 script.4s:2: cross_warning is expanded from...
285 script.4s:8: the top level
288 AT_CHECK_M4SUGAR([-o- -Wnone,cross,error], 1, [],
289 [[script.4s:6: warning: cross
290 script.4s:2: cross_warning is expanded from...
291 script.4s:6: the top level
292 script.4s:8: warning: cross
293 script.4s:2: cross_warning is expanded from...
294 script.4s:8: the top level
297 AT_CLEANUP
300 ## ----------------- ##
301 ## m4_divert_stack.  ##
302 ## ----------------- ##
304 AT_SETUP([m4@&t@_divert_stack])
305 AT_KEYWORDS([m4@&t@_divert m4@&t@_divert_push m4@&t@_divert_pop
306 m4@&t@_undivert m4@&t@_cleardivert m4@&t@_divert_text])
308 dnl This test names some diversions to avoid a warning.
309 AT_CHECK_M4SUGAR_TEXT([[m4_define([_m4_divert(ten)], [10])dnl
310 m4_define([_m4_divert(twenty)], [20])dnl
311 m4_define([_m4_divert(thirty)], [30])dnl
312 1.m4_divert_stack
313 m4_divert_push([ten])2.m4_divert_stack
314 m4_divert_text([twenty], [3.m4_divert_stack])dnl
315 m4_divert([thirty])4.m4_divert_stack
316 m4_divert_pop([thirty])dnl
317 5.m4_undivert([twenty], [thirty])
318 m4_pattern_allow([^m4_divert])dnl
319 ]], [[1.script.4s:2: m4@&t@_divert_push:
320 script.4s:1: m4@&t@_divert: KILL
321 5.3.script.4s:8: m4@&t@_divert_push: twenty
322 script.4s:7: m4@&t@_divert_push: ten
323 script.4s:2: m4@&t@_divert_push:
324 script.4s:1: m4@&t@_divert: KILL
325 4.script.4s:9: m4@&t@_divert: thirty
326 script.4s:2: m4@&t@_divert_push:
327 script.4s:1: m4@&t@_divert: KILL
329 2.script.4s:7: m4@&t@_divert_push: ten
330 script.4s:2: m4@&t@_divert_push:
331 script.4s:1: m4@&t@_divert: KILL
334 AT_CHECK_M4SUGAR_TEXT([[dnl
335 m4_divert_text([3], [three])dnl
336 m4_divert_text([4], [four])dnl
337 m4_divert_text([1], [one])dnl
338 m4_divert_text([2], [two])dnl
339 m4_cleardivert([2], [3])dnl
341 [[one
342 four
344 [[script.4s:4: warning: prefer named diversions
345 script.4s:5: warning: prefer named diversions
346 script.4s:6: warning: prefer named diversions
347 script.4s:7: warning: prefer named diversions
348 script.4s:8: warning: prefer named diversions
351 AT_DATA_M4SUGAR([script.4s],
352 [[m4_divert_pop
354 AT_CHECK_M4SUGAR([-o-], [1], [],
355 [[script.4s:1: error: too many m4@&t@_divert_pop
356 script.4s:1: the top level
357 autom4te: m4 failed with exit status: 1
360 AT_DATA_M4SUGAR([script.4s],
361 [[m4_init
362 m4_divert_push([1])
363 m4_divert_pop([2])
365 AT_CHECK_M4SUGAR([-o-], [1], [],
366 [[script.4s:3: error: m4@&t@_divert_pop(2): diversion mismatch:
367 script.4s:2: m4@&t@_divert_push: 1
368 script.4s:1: m4@&t@_divert: KILL
369 script.4s:3: the top level
370 autom4te: m4 failed with exit status: 1
373 AT_DATA_M4SUGAR([script.4s],
374 [[m4_divert([1])
375 m4_init
376 m4_divert_push([2])
378 AT_CHECK_M4SUGAR([-o-], [1], [],
379 [[script.4s:2: error: m4@&t@_init: unbalanced m4@&t@_divert_push:
380 script.4s:3: m4@&t@_divert_push: 2
381 script.4s:2: m4@&t@_divert: KILL
382 script.4s:2: the top level
383 autom4te: m4 failed with exit status: 1
386 AT_CLEANUP
389 ## -------------------- ##
390 ## m4_expansion_stack.  ##
391 ## -------------------- ##
393 AT_SETUP([m4@&t@_expansion_stack])
395 AT_CHECK_M4SUGAR_TEXT([[1.m4_expansion_stack
396 m4_defun([a], [b])dnl
397 m4_define([c], [d])dnl
398 m4_defun([d], [2.m4_expansion_stack])dnl
399 m4_defun([b], [c])dnl
401 3.m4_ifdef([_m4_expansion_stack], [m4_expansion_stack])
402 ]], [[1.script.4s:3: the top level
403 2.script.4s:6: d is expanded from...
404 script.4s:7: b is expanded from...
405 script.4s:4: a is expanded from...
406 script.4s:8: the top level
410 AT_CLEANUP
413 ## --------------------------- ##
414 ## m4_require: error message.  ##
415 ## --------------------------- ##
417 AT_SETUP([m4@&t@_require: error message])
418 AT_KEYWORDS([m4@&t@_require])
420 AT_DATA_M4SUGAR([script.4s],
421 [[m4_defun([foo], [FOO])
422 m4_require([foo])
425 AT_CHECK_M4SUGAR([], 1, [],
426 [[script.4s:2: error: m4@&t@_require(foo): cannot be used outside of an m4_defun'd macro
427 script.4s:2: the top level
428 autom4te: m4 failed with exit status: 1
430 AT_CLEANUP
433 ## ----------------------------------- ##
434 ## m4_require: circular dependencies.  ##
435 ## ----------------------------------- ##
437 AT_SETUP([m4@&t@_require: circular dependencies])
438 AT_KEYWORDS([m4@&t@_require])
440 AT_DATA_M4SUGAR([script.4s],
441 [[m4_defun([foo], [m4_require([bar])])
443 m4_defun([bar], [m4_require([foo])])
445 m4_defun([baz], [m4_require([foo])])
447 m4_init
448 m4_divert([0])dnl
452 AT_CHECK_M4SUGAR([], 1, [],
453 [[script.4s:9: error: m4@&t@_require: circular dependency of foo
454 script.4s:3: bar is expanded from...
455 script.4s:1: foo is expanded from...
456 script.4s:5: baz is expanded from...
457 script.4s:9: the top level
458 autom4te: m4 failed with exit status: 1
460 AT_CLEANUP
463 ## ---------------------- ##
464 ## m4_require: one-shot.  ##
465 ## ---------------------- ##
467 AT_SETUP([m4@&t@_require: one-shot initialization])
468 AT_KEYWORDS([m4@&t@_require])
469 AT_KEYWORDS([m4@&t@_defun_init m4@&t@_copy m4@&t@_defun_once])
471 dnl check out m4_defun_init, m4_copy, and odd macro names
472 AT_CHECK_M4SUGAR_TEXT([[
473 m4_define([t], [text])dnl
474 m4_defun_init([a], [[init a
475 ]], [[common a] t])dnl
476 m4_defun([b], [[b]m4_require([a])])dnl
477 m4_defun([c], [[c]m4_require([a])])dnl
480 a()dnl
482 m4_defun_init([-], [hello, ], [m4_if([$#], [0], [world], [[$1]])])dnl
483 m4_copy([-], [.])dnl
484 m4_indir([.])
485 m4_indir([.], [goodbye])
486 m4_indir([-], [again])
487 ]], [[
488 init a
489 common a text
492 common a text
493 hello, world
494 goodbye
495 hello, again
498 dnl Check m4_defun_once behavior
499 AT_CHECK_M4SUGAR_TEXT([[
500 m4_defun_once([a], [[a]])dnl
501 m4_defun([b], [[b]m4_require([a])])dnl
502 m4_defun([c], [[c]a[]m4_require([b])])dnl
505 m4_defun_once([d], [[d]m4_require([a])])dnl
507 m4_defun_once([e], [[e]])dnl
508 m4_defun([f], [[f]m4_require([e])e])dnl
510 ]], [[
521 AT_CLEANUP
524 ## -------------------- ##
525 ## m4_require: nested.  ##
526 ## -------------------- ##
528 AT_SETUP([m4@&t@_require: nested])
529 AT_KEYWORDS([m4@&t@_require m4@&t@_defun])
531 dnl From the m4sugar.m4 discourse: Require chains, top level
532 AT_CHECK_M4SUGAR_TEXT([[dnl
533 m4_defun([a], [[a]])dnl aka TEST2a
534 m4_defun([b], [[b]m4_require([a])])dnl aka TEST3
535 m4_defun([c], [[c]m4_require([b])])dnl aka TEST2b
536 m4_defun([d], [[d]m4_require([a])m4_require([c])])dnl aka TEST1
540 post
542 [[pre
548 post
551 dnl From the m4sugar.m4 discourse: Require chains, nested
552 AT_CHECK_M4SUGAR_TEXT([[dnl
553 m4_defun([a], [[a]])dnl aka TEST2a
554 m4_defun([b], [[b]m4_require([a])])dnl aka TEST3
555 m4_defun([c], [[c]m4_require([b])])dnl aka TEST2b
556 m4_defun([d], [[d]m4_require([a])m4_require([c])])dnl aka TEST1
557 m4_defun([wrap],
558 [pre
561 post])dnl
562 wrap
570 post
573 dnl Direct invocation, nested requires, top level
574 AT_CHECK_M4SUGAR_TEXT([[dnl
575 m4_defun([a], [[a]])dnl
576 m4_defun([b], [[b]m4_require([a])])dnl
577 m4_defun([c], [[c]m4_require([b])])dnl
583 post
585 [[pre
591 post
594 dnl Direct invocation, nested requires, nested defun.  This is an example
595 dnl of expansion before requirement, such that b occurs before its
596 dnl prerequisite a.  This indicates a bug in the macros (but not in
597 dnl autoconf), so we should be emitting a warning.
598 AT_CHECK_M4SUGAR_TEXT([[dnl
599 m4_defun([a], [[a]])dnl
600 m4_defun([b], [[b]m4_require([a])])dnl
601 m4_defun([c], [[c]m4_require([b])])dnl
602 dnl the extra macro layer works around line number differences in older m4
603 m4_define([foo], [m4_defun([outer],
604 [pre
609 post])])foo[]dnl
610 outer
619 post
621 [[script.4s:15: warning: m4@&t@_require: `a' was expanded before it was required
622 script.4s:5: b is expanded from...
623 script.4s:6: c is expanded from...
624 script.4s:14: outer is expanded from...
625 script.4s:15: the top level
628 dnl Direct invocation, expand-before-require but no nested require.  As this
629 dnl is common in real life, but does not result in out-of-order expansion,
630 dnl we silently permit this.
631 AT_CHECK_M4SUGAR_TEXT([[dnl
632 m4_defun([a], [[a]])dnl
633 m4_defun([b], [[b]m4_require([a])])dnl
634 m4_defun([c], [[c]])dnl
635 m4_defun([d], [[d]m4_require([c])])dnl
636 pre1
641 post1
642 m4_defun([outer],
643 [pre2
648 post2])dnl
649 outer
650 m4_defun([e], [[e]])dnl
651 m4_defun([f], [[f]m4_require([e])])dnl
652 m4_defun([g], [[g]
654 f])dnl
655 m4_defun([h], [[h]m4_require([g])])dnl
657 m4_defun([i], [[i]])dnl
658 m4_defun([j], [[j]
659 i])dnl
660 m4_defun([k], [[k]m4_require([i])])dnl
661 m4_defun([l], [[l]m4_require([k])])dnl
662 m4_defun([m], [[m]m4_require([j])m4_require([l])])dnl
665 [[pre1
670 post1
671 pre2
676 post2
688 AT_CLEANUP
691 ## ------------------------------------------------- ##
692 ## m4_ifval, m4_ifblank, m4_ifset, m4_default, etc.  ##
693 ## ------------------------------------------------- ##
695 AT_SETUP([m4sugar shorthand conditionals])
696 AT_KEYWORDS([m4@&t@_ifval m4@&t@_ifblank m4@&t@_ifnblank m4@&t@_ifset
697 m4@&t@_default m4@&t@_default_quoted m4@&t@_default_nblank
698 m4@&t@_default_nblank_quoted])
700 AT_CHECK_M4SUGAR_TEXT([[m4_define([active], [ACTIVE])m4_define([empty])
701 m4_ifval([active], [yes], [no])
702 m4_ifval([empty], [yes], [no])
703 m4_ifval([ ], [yes], [no])
704 m4_ifval([], [yes], [no])
705 m4_ifblank([active], [yes], [no])
706 m4_ifblank([empty], [yes], [no])
707 m4_ifblank([ ], [yes], [no])
708 m4_ifblank([], [yes], [no])
709 m4_ifnblank([active], [yes], [no])
710 m4_ifnblank([empty], [yes], [no])
711 m4_ifnblank([ ], [yes], [no])
712 m4_ifnblank([], [yes], [no])
713 m4_ifset([active], [yes], [no])
714 m4_ifset([empty], [yes], [no])
715 m4_ifset([ ], [yes], [no])
716 m4_ifset([], [yes], [no])
718 m4_define([demo1], [m4_default([$1], [$2])])dnl
719 m4_define([demo2], [m4_default_quoted([$1], [$2])])dnl
720 m4_define([demo3], [m4_default_nblank([$1], [$2])])dnl
721 m4_define([demo4], [m4_default_nblank_quoted([$1], [$2])])dnl
722 demo1([active], [default])
723 demo1([], [active])
724 demo1([empty], [text])
725 -demo1([ ], [active])-
726 demo2([active], [default])
727 demo2([], [active])
728 demo2([empty], [text])
729 -demo2([ ], [active])-
730 demo3([active], [default])
731 demo3([], [active])
732 demo3([empty], [text])
733 -demo3([ ], [active])-
734 demo4([active], [default])
735 demo4([], [active])
736 demo4([empty], [text])
737 -demo4([ ], [active])-
738 ]], [[
756 ACTIVE
757 ACTIVE
759 - -
760 active
761 active
762 empty
763 - -
764 ACTIVE
765 ACTIVE
767 -ACTIVE-
768 active
769 active
770 empty
771 -active-
774 AT_CLEANUP
776 ## --------- ##
777 ## m4_cond.  ##
778 ## --------- ##
780 AT_SETUP([m4@&t@_cond])
782 AT_CHECK_M4SUGAR_TEXT([[m4_define([side], [m4_errprintn([$1])$1])
783 m4_cond([side(1)], [1], [a],
784         [side(1)], [1], [b],
785         [side(1)], [2], [c])
786 m4_cond([side(2)], [1], [a],
787         [side(2)], [1], [b],
788         [side(2)], [2], [c],
789         [side(2)])
790 m4_cond([side(3)], [1], [a],
791         [side(3)], [1], [b],
792         [side(3)], [2], [c],
793         [side(3)])
794 m4_cond([a,a], [a,a], [yes], [no])
795 m4_cond([[a,a]], [a,a], [yes])
796 m4_cond([a,a], [a,b], [yes], [no])
797 m4_cond([a,a], [a,b], [yes])
798 m4_cond([m4_eval([0xa])])
799 m4_define([ab], [AB])dnl
800 m4_cond([a])b
801 m4_cond([1], [1], [a])b
802 m4_cond([1], [2], [3], [a])b
803 ]], [[
815 ]], [[1
825 AT_CLEANUP
828 ## ---------- ##
829 ## m4 lists.  ##
830 ## ---------- ##
832 AT_SETUP([m4 lists])
834 AT_KEYWORDS([m4@&t@_car m4@&t@_cdr m4@&t@_argn _m4@&t_cdr])
836 AT_CHECK_M4SUGAR_TEXT([[dnl
837 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])
838 m4_argn([1], [a], [b], [c])
839 m4_argn([2], [a], [b], [c])
840 m4_argn([3], [a], [b], [c])
841 m4_argn([4], [a], [b], [c])
842 m4_car([a], [b], [c])
843 m4_cdr([a], [b], [c])
844 m4_cdr([a], [b])
845 m4_cdr([a])
846 _m4_cdr([a], [b], [c])
847 _m4_cdr([a], [b])
848 _m4_cdr([a])
849 m4_if(m4_cdr([], []), [[]], [good], [bad])
850 m4_if(m4_cdr([]), [], [good], [bad])
851 ]], [[
857 [b],[c]
860 , [b],[c]
861 , [b]
863 good
864 good
867 AT_DATA_M4SUGAR([script.4s],
868 [[m4_init
869 m4_argn([0], [a], [b], [c])
871 AT_CHECK_M4SUGAR([-o-], [1], [],
872 [[script.4s:2: error: assert failed: 0 < 0
873 script.4s:2: the top level
874 autom4te: m4 failed with exit status: 1
877 AT_CLEANUP
880 ## ---------- ##
881 ## m4_split.  ##
882 ## ---------- ##
884 AT_SETUP([m4@&t@_split])
886 AT_CHECK_M4SUGAR_TEXT(
887 [[m4_define([active], [ACT, IVE])m4_define([bd], [oops])
888 m4_split
889 m4_split([[]])
890 m4_split([ ])
891 m4_split([active])
892 m4_split([ active       active ])end
893 m4_split([ ], [ ])
894 m4_split([active], [ ])
895 m4_split([ active       active ], [ ])end
896 m4_split([abcde], [bd])
897 m4_split([abcde], [[bd]])
898 m4_split([foo=`` bar=''])
899 m4_split([foo='' bar=``])
900 dnl these next two are from the manual; keep this in sync if the internal
901 dnl quoting strings in m4_split are changed
902 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])dnl
903 m4_split([a )}>=- b -=<{( c])
904 m4_split([a )}@&t@>=- b -=<@&t@{( c])
908 [[]]
909 [], []
910 [active]
911 [], [active], [active], []end
912 [], []
913 [active]
914 [], [active     active], []end
915 [abcde]
916 [a], [c], [e]
917 [foo=``], [bar='']
918 [foo=''], [bar=``]
919 [a], [], [B], [], [c]
920 [a], [)}>=@&t@-], [b], [-@&t@=<{(], [c]
923 AT_CLEANUP
926 ## ------- ##
927 ## m4_do.  ##
928 ## ------- ##
930 AT_SETUP([m4@&t@_do])
932 AT_CHECK_M4SUGAR_TEXT(
933 [[m4_define([ab], [1])m4_define([bc], [2])m4_define([abc], [3])dnl
934 m4_define([AB], [4])m4_define([BC], [5])m4_define([ABC], [6])dnl
935 m4_do
936 m4_do([a])
937 m4_do([a], [b])c
938 m4_unquote(m4_join([], [a], [b]))c
939 m4_define([a], [A])m4_define([b], [B])m4_define([c], [C])dnl
940 m4_do([a], [b])c
941 m4_unquote(m4_join([], [a], [b]))c
951 AT_CLEANUP
954 ## ----------- ##
955 ## m4_append.  ##
956 ## ----------- ##
958 AT_SETUP([m4@&t@_append])
959 AT_KEYWORDS([m4@&t@_append_uniq m4@&t@_append_uniq_w])
961 AT_CHECK_M4SUGAR_TEXT(
962 [[m4_define([active], [ACTIVE])dnl
963 m4_append([sentence], [This is an])dnl
964 m4_append([sentence], [ active ])dnl
965 m4_append([sentence], [symbol.])dnl
966 sentence
967 m4_undefine([active])dnl
968 sentence
969 m4_define([active], [ACTIVE])dnl
970 m4_append([hooks], [m4_define([act1], [act2])])dnl
971 m4_append([hooks], [m4_define([act2], [active])])dnl
972 m4_undefine([active])dnl
973 act1
974 hooks
975 act1
976 dnl Test for bug fixed in 2.62 when separator is active.
977 m4_define([a], [A])dnl
978 m4_append_uniq([foo], [-], [a])dnl
979 m4_append_uniq([foo], [-], [a])dnl
980 m4_append_uniq([bar], [-], [a])dnl
981 m4_append_uniq([bar], [~], [a])dnl
982 m4_append_uniq([bar], [-], [a])dnl
983 m4_defn([foo])
984 m4_defn([bar])
987 m4_append_uniq([blah], [one], [, ], [new], [existing])
988 m4_append_uniq([blah], [two], [, ], [new], [existing])
989 m4_append_uniq([blah], [two], [, ], [new], [existing])
990 m4_append_uniq([blah], [three], [, ], [new], [existing])
991 m4_append([blah], [two], [, ])dnl
992 blah
993 m4_dquote(blah)
994 m4_append([list], [one], [[, ]])dnl
995 m4_append([list], [two], [[, ]])dnl
996 m4_append([list], [three], [[, ]])dnl
997 list
998 m4_dquote(list)
999 m4_append_uniq_w([numbers], [1 1 2])dnl
1000 m4_append_uniq_w([numbers], [ 2 3 ])dnl
1001 numbers
1003 [[This is an ACTIVE symbol.
1004 This is an active symbol.
1005 act1
1007 active
1014 existing
1016 one, two, three, two
1017 [one],[two],[three],[two]
1018 one, two, three
1019 [one, two, three]
1020 1 2 3
1023 AT_DATA_M4SUGAR([script.4s],
1024 [[m4_init[]dnl
1025 m4_append_uniq([str], [a], [ ])
1026 m4_append_uniq([str], [a b], [ ])
1027 m4_divert([])dnl
1031 AT_CHECK_M4SUGAR([-o-], 0, [[a a b
1032 ]], [[script.4s:3: warning: m4@&t@_append_uniq: `a b' contains ` '
1035 AT_CLEANUP
1038 ## --------- ##
1039 ## m4_join.  ##
1040 ## --------- ##
1042 AT_SETUP([m4@&t@_join])
1044 AT_KEYWORDS([m4@&t@_joinall])
1046 AT_CHECK_M4SUGAR_TEXT(
1047 [[m4_define([active], [ACTIVE])
1048 m4_join
1049 m4_join([|])
1050 m4_join([, ], [one], [two])
1051 m4_dquote(m4_join([, ], [one], [two]))
1052 m4_join([|], [active], [active])
1053 m4_join([|], ,,,[one])
1054 m4_join([|], [one],,,)
1055 m4_join([], ,,,[two])
1056 m4_join([], [two],,,)
1057 m4_join([ active ], [one], , [two])
1058 m4_join([], [one], [two])
1059 m4_joinall([-], [one], [], [two])
1060 m4_joinall([-], [], [], [three], [], [])
1061 m4_joinall([], [one], [], [two])
1062 m4_joinall
1063 m4_joinall([-])
1064 m4_joinall([-], [one])
1069 one, two
1070 [one, two]
1071 active|active
1076 one active two
1077 onetwo
1078 one--two
1079 --three--
1080 onetwo
1086 AT_CLEANUP
1089 ## ----------- ##
1090 ## m4_expand.  ##
1091 ## ----------- ##
1093 AT_SETUP([m4@&t@_expand])
1095 AT_CHECK_M4SUGAR_TEXT(
1096 [[m4_define([active], [ACTIVE])dnl
1097 m4_expand([#active
1098 active])
1099 m4_expand([[active]])
1100 dnl properly quoted case statements
1101 m4_expand([case a in @%:@(
1102   *) echo active, ;;
1103 esac
1104 case b in
1105   *[)] echo active, ;;
1106 esac])
1107 dnl unbalanced underquoted `)', but we manage anyway (gasp!)
1108 m4_expand([case c in #(
1109   *) echo active, ;;
1110 esac
1111 case d in
1112   *) echo active, ;;
1113 esac])
1114 dnl unterminated comment/dnl
1115 m4_expand([active # active])
1116 m4_expand([a
1117 dnl])
1118 m4_expand([a
1119 -dnl])
1121 [[#active
1122 ACTIVE
1123 active
1124 case a in #(
1125   *) echo ACTIVE, ;;
1126 esac
1127 case b in
1128   *) echo ACTIVE, ;;
1129 esac
1130 case c in #(
1131   *) echo ACTIVE, ;;
1132 esac
1133 case d in
1134   *) echo ACTIVE, ;;
1135 esac
1136 ACTIVE # active
1142 AT_CLEANUP
1145 ## ------------- ##
1146 ## m4_text_box.  ##
1147 ## ------------- ##
1149 AT_SETUP([m4@&t@_text_box])
1151 AT_CHECK_M4SUGAR_TEXT([[
1152 m4_text_box([a $1 @&t@b])
1153 m4_text_box([a $1 @&t@b], [$])
1154 m4_text_box([a $1 @&t@b], [,])
1155 ]], [[
1156 ## ------ ##
1157 ## a $1 b ##
1158 ## ------ ##
1159 ## $$$$$$ ##
1160 ## a $1 b ##
1161 ## $$$$$$ ##
1162 ## ,,,,,, ##
1163 ## a $1 b ##
1164 ## ,,,,,, ##
1167 AT_CLEANUP
1169 ## -------------- ##
1170 ## m4_text_wrap.  ##
1171 ## -------------- ##
1173 AT_SETUP([m4@&t@_text_wrap])
1174 AT_KEYWORDS([m4@&t@_escape])
1176 # m4_text_wrap is used to display the help strings.  Also, check that
1177 # commas and $ are not swallowed.  This can easily happen because of
1178 # m4-listification.
1180 AT_DATA_M4SUGAR([script.4s],
1181 [[m4_init[]m4_divert([])dnl
1182 m4_define([a], [OOPS])dnl
1183 m4_escape([a[b $c#]d])
1184 m4_if(m4_escape([a[b $c#]d]), [a[b $c#]d], [oops],
1185       m4_escape([a[b $c#]d]), [a@<:@b @S|@c@%:@@:>@d], [pass], [oops])
1187 m4_text_wrap([Short string */], [   ], [/* ], 20)
1189 m4_text_wrap([Much longer string */], [   ], [/* ], 20)
1191 m4_text_wrap([Short doc.], [          ], [  --short ], 30)
1193 m4_text_wrap([Short doc.], [          ], [  --too-wide], 30)
1195 m4_text_wrap([Super long documentation.], [          ], [  --too-wide], 30)
1197 m4_text_wrap([First, second  , third, [,quoted  space]])
1198 m4_define([xfff], [oops])
1199 m4_text_wrap([Some $1 $2 $3 $4 embedded dollars.], [ $* ], [ $@ ], [0xfff & 20])
1202 AT_DATA([expout],
1203 [[a[b $c#]d
1204 pass
1206 /* Short string */
1208 /* Much longer
1209    string */
1211   --short Short doc.
1213   --too-wide
1214           Short doc.
1216   --too-wide
1217           Super long
1218           documentation.
1220 First, second , third, [,quoted space]
1222  $@ Some $1 $2 $3
1223  $* $4 embedded
1224  $* dollars.
1227 AT_CHECK_M4SUGAR([-o-], 0, [expout])
1229 AT_CLEANUP
1231 ## -------------------- ##
1232 ## m4_version_compare.  ##
1233 ## -------------------- ##
1235 AT_SETUP([m4@&t@_version_compare])
1237 AT_KEYWORDS([m4@&t@_list_cmp])
1239 AT_CHECK_M4SUGAR_TEXT(
1240 [[m4_version_compare([1.1], [2.0])
1241 m4_version_compare([2.0b], [2.0a])
1242 m4_version_compare([2.0z], [2.0y])
1243 m4_version_compare([1.1.1], [1.1.1a])
1244 m4_version_compare([1.2], [1.1.1a])
1245 m4_version_compare([1.0], [1])
1246 m4_version_compare([1.0a], [1.0a])
1247 m4_version_compare([1.1a], [1.1a.1])
1248 m4_version_compare([1.10], [1.1a])
1249 m4_version_compare([1-1a], [1,1A])
1250 m4_define([a], [oops])dnl
1251 m4_version_compare([1.1a], [1.1A])
1252 m4_version_compare([1z], [1aa])
1253 m4_version_compare([2.61a], [2.61a-248-dc51])
1254 m4_version_compare([2.61b], [2.61a-248-dc51])
1255 m4_version_compare([08], [09])
1256 m4_version_compare([010], [8])
1257 dnl Test that side effects to m4_list_cmp occur exactly once
1258 m4_list_cmp([[0], [0], [0]m4_errprintn([hi])],
1259             [[0], [0], [0]m4_errprintn([hi])])
1260 m4_list_cmp([[0], [0], [0]m4_errprintn([hi])],
1261             [[0], [0], [0]m4_errprintn([bye])])
1263 [[-1
1281 ]], [[hi
1287 AT_CLEANUP
1289 ## ------------------------------ ##
1290 ## Standard regular expressions.  ##
1291 ## ------------------------------ ##
1293 AT_SETUP([Standard regular expressions])
1295 # AT_CHECK_M4RE(RE-NAME, TEXT, INTENT = `ok' | `')
1296 # ------------------------------------------------
1297 # Check whether RE-NAME (a macro whose definition is a regular expression)
1298 # matches TEXT.  INTENT = `ok' if the match should succeed or else empty.
1299 m4_define([AT_CHECK_M4RE],
1300 [AT_CHECK_M4SUGAR_TEXT(
1301 [[m4_bregexp([$2], ^m4_defn([$1])$, [ok])
1302 ]], [$3
1303 ])])
1305 AT_CHECK_M4RE([m4_re_word], [ab9_c], [ok])
1306 AT_CHECK_M4RE([m4_re_word], [_9abc], [ok])
1307 AT_CHECK_M4RE([m4_re_word], [9ab_c])
1309 AT_CHECK_M4RE([m4_re_string], [ab9_c], [ok])
1310 AT_CHECK_M4RE([m4_re_string], [_9abc], [ok])
1311 AT_CHECK_M4RE([m4_re_string], [9ab_c], [ok])
1312 AT_CHECK_M4RE([m4_re_string], [9a@_c])
1314 AT_CLEANUP
1316 ## ----------- ##
1317 ## m4_bmatch.  ##
1318 ## ----------- ##
1320 AT_SETUP([m4@&t@_bmatch])
1322 AT_CHECK_M4SUGAR_TEXT(
1323 [[m4_bmatch([abc], [default\])
1324 m4_bmatch([abc], [^a], [yes])
1325 m4_bmatch([abc], [^a], [yes], [no])
1326 m4_bmatch([abc], [^.a], [yes])
1327 m4_bmatch([abc], [^.a], [yes], [no\])
1328 m4_bmatch([abc], [a], [1], [b], [2])
1329 m4_bmatch([abc], [A], [1], [b], [2])
1330 m4_define([ab], [AB])dnl
1331 m4_bmatch([$*], [a])b
1332 m4_bmatch([$*], [\*], [a])b
1333 m4_bmatch([$*], [1], [2], [a])b
1334 ]], [[default\
1346 AT_CLEANUP
1348 ## ------------------------ ##
1349 ## m4_toupper, m4_tolower.  ##
1350 ## ------------------------ ##
1352 AT_SETUP([m4@&t@_toupper and m4@&t@_tolower])
1354 AT_CHECK_M4SUGAR_TEXT(
1355 [[m4_define([abc], [hI])m4_define([ABC], [Hi])
1356 m4_toupper(abc aBc ABC)
1357 m4_tolower(abc aBc ABC)
1358 m4_toupper([abc aBc ABC])
1359 m4_tolower([abc aBc ABC])
1360 m4_echo(m4_toupper(abc aBc ABC))
1361 m4_echo(m4_tolower(abc aBc ABC))
1362 m4_echo(m4_toupper([abc aBc ABC]))
1363 m4_echo(m4_tolower([abc aBc ABC]))
1364 m4_do(m4_toupper(abc aBc ABC))
1365 m4_do(m4_tolower(abc aBc ABC))
1366 m4_do(m4_toupper([abc aBc ABC]))
1367 m4_do(m4_tolower([abc aBc ABC]))
1368 ]], [[
1369 HI ABC HI
1370 hi abc hi
1371 ABC ABC ABC
1372 abc abc abc
1373 HI ABC HI
1374 hi abc hi
1375 ABC ABC ABC
1376 abc abc abc
1377 HI Hi HI
1378 hi hI hi
1379 Hi Hi Hi
1380 hI hI hI
1383 AT_CLEANUP
1385 ## --------------- ##
1386 ## m4_bpatsubsts.  ##
1387 ## --------------- ##
1389 AT_SETUP([m4@&t@_bpatsubsts])
1391 AT_CHECK_M4SUGAR_TEXT(
1392 [[m4_bpatsubsts([11], [^..$])
1393 m4_bpatsubsts([11], [\(.\)1], [\12])
1394 m4_bpatsubsts([11], [^..$], [], [1], [2])
1395 m4_bpatsubsts([11], [\(.\)1], [\12], [1], [3])
1396 m4_define([a], [oops])m4_define([c], [oops])dnl
1397 m4_define([AB], [good])m4_define([bc], [good])dnl
1398 m4_bpatsubsts([abc], [a], [A], [b], [B], [c])
1399 m4_bpatsubsts([ab], [a])c
1400 m4_bpatsubsts([ab], [c], [C], [a])c
1401 m4_bpatsubsts([$1$*$@], [\$\*], [$#])
1402 ]], [[11
1406 good
1407 good
1408 good
1409 $1$#$@
1412 AT_CLEANUP
1414 ## -------------- ##
1415 ## m4_esyscmd_s.  ##
1416 ## -------------- ##
1418 AT_SETUP([m4@&t@_esyscmd_s])
1419 AT_KEYWORDS([m4@&t@_chomp m4@&t@_chomp_all])
1421 AT_CHECK_M4SUGAR_TEXT(
1422 [[m4_define([world], [WORLD])dnl
1423 m4_chomp([abc])
1424 m4_chomp([world
1427 m4_esyscmd_s([echo hello world])
1428 m4_esyscmd_s([echo '[goodbye,
1429 cruel world
1431 ]'])
1432 ]], [[abc
1433 world
1435 hello WORLD
1436 goodbye,
1437 cruel world
1440 AT_CLEANUP
1442 ## ---------- ##
1443 ## M4 Loops.  ##
1444 ## ---------- ##
1446 AT_SETUP([M4 loops])
1448 AT_KEYWORDS([m4@&t@_for m4@&t@_foreach m4@&t@_foreach_w m4@&t@_map_args_w])
1450 AT_CHECK_M4SUGAR_TEXT([[dnl
1451 m4_define([myvar], [outer value])dnl
1452 m4_for([myvar], 1, 3, 1, [ myvar])
1453 m4_for([myvar], 1, 3,  , [ myvar])
1454 m4_for([myvar], 3, 1,-1, [ myvar])
1455 m4_for([myvar], 3, 1,  , [ myvar])
1456 m4_for([myvar], 1, 3, 2, [ myvar])
1457 m4_for([myvar], 3, 1,-2, [ myvar])
1458 m4_for([myvar],-1,-3,-2, [ myvar])
1459 m4_for([myvar],-3,-1, 2, [ myvar])
1460 dnl Make sure we recalculate the bounds correctly:
1461 m4_for([myvar], 1, 3, 3, [ myvar])
1462 m4_for([myvar], 1, 6, 3, [ myvar])
1463 m4_for([myvar],22,-7,-5, [ myvar])
1464 m4_for([myvar],-2,-7,-4, [ myvar])
1465 m4_for([myvar],-7,-2, 4, [ myvar])
1466 dnl Make sure we are not exposed to division truncation:
1467 m4_for([myvar], 2, 5, 2, [ myvar])
1468 m4_for([myvar],-5,-2, 2, [ myvar])
1469 m4_for([myvar], 5, 2,-2, [ myvar])
1470 m4_for([myvar],-2,-5,-2, [ myvar])
1471 dnl Make sure we do not divide by zero:
1472 m4_for([myvar], 1, 1,  , [ myvar])
1473 m4_for([myvar], 1, 1,+2, [ myvar])
1474 m4_for([myvar], 1, 1,-2, [ myvar])
1475 dnl Make sure we do not loop endlessly
1476 m4_for([myval], 1, 1, 0, [ myval])
1477 dnl Make sure to properly parenthesize
1478 m4_for([myvar], 3-5, -2+8, , [ myvar])
1479 m4_for([myvar], -2+8, 3-5, , [ myvar])
1480 m4_for([myvar], 8, 16, 3 * 2, [ myvar])
1481 m4_for([myvar], 8, 16, -3 * -2, [ myvar])
1482 m4_for([myvar], [2<<2], [2<<3], [-3 * (-2)], [ myvar])
1483 dnl Modifying var does not affect the number of iterations
1484 m4_for([myvar], 1, 5, , [ myvar[]m4_define([myvar], 5)])
1485 dnl Make sure we can do nameless iteration
1486 m4_for(, 1, 10, , -)
1487 dnl foreach tests
1488 m4_foreach([myvar], [[a], [b, c], [d], [e
1489 ],[f]], [ myvar|])
1490 m4_foreach_w([myvar], [a  b c, d,e f
1491 g], [ myvar|])
1492 myvar
1493 m4_map_args_w([a  b c, d,e f
1494 g], [ ], [|])
1495 m4_map_args_w([a b], [\1], [/])
1496 m4_define([dashes], [--])dnl
1497 m4_map_args_w([a b c], [/], [\1], [dashes])
1498 dnl only one side effect expansion, prior to visiting list elements
1499 m4_foreach([i], [[1], [2], [3]m4_errprintn([hi])], [m4_errprintn(i)])dnl
1500 dnl shifting forms an important part of loops
1501 m4_shift3:m4_shift3(1,2,3):m4_shift3(1,2,3,4)
1502 m4_shiftn(3,1,2,3):m4_shiftn(3,1,2,3,4)
1504 [[ 1 2 3
1505  1 2 3
1506  3 2 1
1507  3 2 1
1508  1 3
1509  3 1
1510  -1 -3
1511  -3 -1
1513  1 4
1514  22 17 12 7 2 -3
1515  -2 -6
1516  -7 -3
1517  2 4
1518  -5 -3
1519  5 3
1520  -2 -4
1525  -2 -1 0 1 2 3 4 5 6
1526  6 5 4 3 2 1 0 -1 -2
1527  8 14
1528  8 14
1529  8 14
1530  1 2 3 4 5
1531 ----------
1532  a| b, c| d| e
1533 | f|
1534  a| b| c,| d,e| f| g|
1535 outer value
1536  a| b| c,| d,e| f| g|
1537 \1a/\1b/
1538 /a\1--/b\1--/c\1
1541 ]], [[hi
1547 dnl bounds checking in m4_for
1548 AT_DATA_M4SUGAR([script.4s],
1549 [[m4_init
1550 m4_divert([0])dnl
1551 m4_for([myvar], 1, 3,-1, [ myvar])
1553 AT_CHECK_M4SUGAR([], 1, [],
1554 [[script.4s:3: error: assert failed: -1 > 0
1555 script.4s:3: the top level
1556 autom4te: m4 failed with exit status: 1
1559 AT_DATA_M4SUGAR([script.4s],
1560 [[m4_init
1561 m4_divert([0])dnl
1562 m4_for([myvar], 1, 2, 0, [ myvar])
1564 AT_CHECK_M4SUGAR([], 1, [],
1565 [[script.4s:3: error: assert failed: 0 > 0
1566 script.4s:3: the top level
1567 autom4te: m4 failed with exit status: 1
1570 AT_DATA_M4SUGAR([script.4s],
1571 [[m4_init
1572 m4_divert([0])dnl
1573 m4_for([myvar], 2, 1, 0, [ myvar])
1575 AT_CHECK_M4SUGAR([], 1, [],
1576 [[script.4s:3: error: assert failed: 0 < 0
1577 script.4s:3: the top level
1578 autom4te: m4 failed with exit status: 1
1581 dnl m4_shiftn also does bounds checking
1582 AT_DATA_M4SUGAR([script.4s],
1583 [[m4_init
1584 m4_divert([0])dnl
1585 m4_shiftn(3,1,2)
1587 AT_CHECK_M4SUGAR([], 1, [],
1588 [[script.4s:3: error: assert failed: 0 < 3 && 3 < 3
1589 script.4s:3: the top level
1590 autom4te: m4 failed with exit status: 1
1593 AT_CLEANUP
1596 ## --------------------- ##
1597 ## m4_map{,all}{,_sep}.  ##
1598 ## --------------------- ##
1600 AT_SETUP([m4@&t@_map])
1601 AT_KEYWORDS([m4@&t@_apply m4@&t@_map_sep m4@&t@_mapall m4@&t@_mapall_sep])
1602 AT_KEYWORDS([m4@&t@_count])
1604 AT_CHECK_M4SUGAR_TEXT([[dnl
1605 m4_map([m4_count], [])
1606 m4_map([ m4_count], [[],
1607                      [[1]],
1608                      [[1], [2]]])
1609 m4_mapall([ m4_count], [[],
1610                         [[1]],
1611                         [[1], [2]]])
1612 m4_map_sep([m4_eval], [,], [[[1+2]],
1613                             [[10], [16]]])
1614 m4_count(m4_map_sep([m4_echo], [,], [[], [[1]], [[2]]]))
1615 m4_count(m4_mapall_sep([m4_echo], [,], [[], [[1]], [[2]]]))
1616 m4_map_sep([m4_eval], [[,]], [[[1+2]],
1617                               [[10], [16]]])
1618 m4_count(m4_map_sep([m4_echo], [[,]], [[], [[1]], [[2]]]))
1619 m4_count(m4_mapall_sep([m4_echo], [[,]], [[], [[1]], [[2]]]))
1620 m4_map([-], [[]])
1621 m4_mapall([-], [[]])
1622 m4_map_sep([-], [:], [[]])
1623 m4_mapall_sep([-], [:], [[]])
1624 m4_define([a], [m4_if([$#], [0], [oops], [$1], [a], [pass], [oops])])dnl
1625 m4_define([a1], [oops])dnl
1626 m4_define([pass1], [oops])dnl
1627 m4_map([a], [[[a]]])1
1628 m4_map([m4_unquote([a])], [m4_dquote([a])])
1629 dnl only one side effect expansion, prior to visiting list elements
1630 m4_map([m4_errprintn], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1631 m4_map_sep([m4_errprintn], [], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1632 m4_mapall([m4_errprintn], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1633 m4_mapall_sep([m4_errprintn], [], [[[1]], [[2]], [[3]]m4_errprintn([hi])])dnl
1636  1 2
1637  0 1 2
1648 pass1
1649 pass
1650 ]], [[hi
1668 AT_CLEANUP
1671 ## --------------------------------------- ##
1672 ## m4_map_args{,_sep,_pair} and m4_curry.  ##
1673 ## --------------------------------------- ##
1675 AT_SETUP([m4@&t@_map_args and m4@&t@_curry])
1676 AT_KEYWORDS([m4@&t@_map_args_sep m4@&t@_map_args_pair m4@&t@_reverse
1677 m4@&t@_map])
1679 dnl First, make sure we can curry in isolation.
1680 AT_CHECK_M4SUGAR_TEXT(
1681 [[m4_curry([m4_echo])([1])
1682 m4_curry([m4_curry], [m4_reverse], [1])([2])([3])
1683 m4_define([add], [m4_eval(([$1]) + ([$2]))])dnl
1684 m4_define([add_one], [m4_curry([add], [1])])dnl
1685 add_one()([4])
1688 3, 2, 1
1692 dnl Now, check that we can map a list of arguments.
1693 AT_CHECK_M4SUGAR_TEXT([[m4_define([active], [ACTIVE])dnl
1694 m4_map_args([ m4_echo])
1695 m4_map_args([ m4_echo], [plain], [active])
1696 m4_map_args([m4_unquote], [plain], [active])
1697 m4_map_args_pair([, m4_reverse], [])
1698 m4_map_args_pair([, m4_reverse], [], [1])
1699 m4_map_args_pair([, m4_reverse], [], [1], [2])
1700 m4_map_args_pair([, m4_reverse], [], [1], [2], [3])
1701 m4_map_args_pair([, m4_reverse], [], [1], [2], [3], [4])
1702 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1])
1703 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2])
1704 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3])
1705 m4_map_args_pair([, m4_reverse], [, m4_dquote], [1], [2], [3], [4])
1706 m4_map_args_sep([<], [>], [:], [1], [2], [3])
1707 m4_map_args_sep([m4_echo(], [)], [ ], [plain], [active])
1710  plain active
1711 plainACTIVE
1713 , 1
1714 , 2, 1
1715 , 2, 1, 3
1716 , 2, 1, 4, 3
1717 , [1]
1718 , 2, 1
1719 , 2, 1, [3]
1720 , 2, 1, 4, 3
1721 <1>:<2>:<3>
1722 plain active
1725 dnl Finally, put the two concepts together, to show the real power of the API.
1726 AT_CHECK_M4SUGAR_TEXT(
1727 [[m4_define([add], [m4_eval(([$1]) + ([$2]))])dnl
1728 m4_define([list], [[-1], [0], [1]])dnl
1729 dnl list_add_n(value, arg...)
1730 dnl add VALUE to each ARG and output the resulting list
1731 m4_define([list_add_n],
1732   [m4_shift(m4_map_args([,m4_curry([add], [$1])], m4_shift($@)))])
1733 list_add_n([1], list)
1734 list_add_n([2], list)
1735 ]], [[
1736 0,1,2
1737 1,2,3
1740 AT_CLEANUP
1743 ## ------------ ##
1744 ## m4_combine.  ##
1745 ## ------------ ##
1747 AT_SETUP([m4@&t@_combine])
1749 AT_CHECK_M4SUGAR_TEXT([[m4_define([a], [oops])dnl
1750 m4_combine([, ], [[a], [b], [c]], [-], [1], [2], [3])
1751 m4_combine([, ], [[a], [b]], [-])
1752 m4_combine([, ], [[a], [b]], [-], [])
1753 m4_combine([, ], [], [-], [a], [b])
1754 m4_combine([, ], [[]], [-], [a], [b])
1755 m4_combine([ a ], [[-], [+]], [a], [-], [+])
1756 m4_combine([$* ], [[$1], [$2]], [$#], [$@])
1758 [[a-1, a-2, a-3, b-1, b-2, b-3, c-1, c-2, c-3
1760 a-, b-
1762 -a, -b
1763 -a- a -a+ a +a- a +a+
1764 $1$#$@$* $2$#$@
1765 ]], [])
1767 AT_CLEANUP
1770 ## -------------- ##
1771 ## m4_{max,min}.  ##
1772 ## -------------- ##
1774 AT_SETUP([m4@&t@_max and m4@&t@_min])
1776 AT_DATA_M4SUGAR([script.4s],
1777 [[m4_max
1780 AT_CHECK_M4SUGAR([], 1, [],
1781 [[script.4s:1: error: too few arguments to m4@&t@_max
1782 script.4s:1: the top level
1783 autom4te: m4 failed with exit status: 1
1786 AT_DATA_M4SUGAR([script.4s],
1787 [[m4_min
1790 AT_CHECK_M4SUGAR([], 1, [],
1791 [[script.4s:1: error: too few arguments to m4@&t@_min
1792 script.4s:1: the top level
1793 autom4te: m4 failed with exit status: 1
1796 AT_CHECK_M4SUGAR_TEXT([[dnl
1797 m4_min(0)
1798 m4_min(0xa)
1799 m4_min(0, 0)
1800 m4_min(0, 1)
1801 m4_min(1, 0)
1802 m4_min(0+1, 1+1)
1803 m4_min(0+1, 1+0)
1804 m4_min(0, 1, 2)
1805 m4_min(2, 1, 0)
1806 m4_min(1m4_for([i], 2, 100, , [,i]))
1807 m4_min(m4_for([i], 100, 2, , [i,])1)
1808 ----
1809 m4_max(0)
1810 m4_max(0xa)
1811 m4_max(0, 0)
1812 m4_max(0, 1)
1813 m4_max(1, 0)
1814 m4_max(1+0, 1+1)
1815 m4_max(1+0, 1+0)
1816 m4_max(0, 1, 2)
1817 m4_max(2, 1, 0)
1818 m4_max(1m4_for([i], 2, 100, , [,i]))
1819 m4_max(m4_for([i], 100, 2, , [i,])1)
1832 ----
1844 ]], [])
1846 AT_CLEANUP
1849 ## ----------- ##
1850 ## Recursion.  ##
1851 ## ----------- ##
1853 AT_SETUP([recursion])
1855 AT_KEYWORDS([m4@&t@_foreach m4@&t@_foreach_w m4@&t@_case m4@&t@_cond
1856 m4@&t@_bpatsubsts m4@&t@_shiftn m4@&t@_do m4@&t@_dquote_elt m4@&t@_reverse
1857 m4@&t@_map m4@&t@_join m4@&t@_joinall m4@&t@_list_cmp m4@&t@_max m4@&t@_min
1858 m4@&t@_bmatch m4@&t@_map_args m4@&t@_map_args_pair])
1860 dnl This test completes in a reasonable time if m4_foreach is linear,
1861 dnl but thrashes if it is quadratic.  If we are testing with m4 1.4.x,
1862 dnl only the slower foreach.m4 implementation will work.  But if we
1863 dnl are testing with m4 1.6, we can rerun the test with __m4_version__
1864 dnl undefined to exercise the alternate code path.
1865 AT_DATA_M4SUGAR([script.4s],
1866 [[m4_init
1867 m4_divert_push([])[]dnl
1868 m4_len(m4_foreach_w([j], m4_do(m4_for([i], [1], [10000], [], [,i ])), [j ]))
1869 m4_shiftn(9998m4_for([i], [1], [10000], [], [,i]))
1870 m4_len(m4_join([--],, m4_dquote_elt(m4_for([i], [1], [10000], [], [,i])),))
1871 m4_len(m4_joinall([--], m4_map([, m4_echo],
1872   m4_dquote([1]m4_for([i], [2], [10000], [], [,i])))))
1873 m4_max(m4_min([1]m4_for([i], [2], [10000], [],
1874   [,i]))m4_for([i], [2], [10000], [], [,i]))
1875 m4_case([10000]m4_for([i], [1], [10000], [], [,i]),[end])
1876 m4_list_cmp(m4_dquote(1m4_for([i], [2], [10000], [], [,i])),
1877   m4_dquote(m4_reverse(10000m4_for([i], [9999], [1], [], [,i])), [0]))
1878 m4_list_cmp([0], [0m4_for([i], [1], [10000], [], [,0])])
1879 m4_list_cmp([0m4_for([i], [1], [10000], [], [,0])], [0])
1880 m4_for([i], [1], [10000], [], [m4_define(i)])dnl
1881 m4_undefine(1m4_for([i], [2], [10000], [], [,i]))dnl
1882 m4_bpatsubsts([a1]m4_for([i], [1], [10000], [], [,i]), [a2], [A])
1883 m4_bmatch([9997]m4_for([i], [1], [10000], [], [,^i$]))
1884 m4_define([up], [m4_define([$1], m4_incr($1))$1])m4_define([j], 0)dnl
1885 m4_cond(m4_for([i], [1], [10000], [], [[up([j])], [9990], i,]) [oops]) j
1886 m4_count(m4_map_args_pair([,m4_quote], []m4_map_args([,m4_echo]m4_for([i],
1887   [1], [10000], [], [,i]))))
1888 m4_divert_pop([])
1891 AT_CHECK_M4SUGAR([-o-], [0], [[48894
1892 9999,10000
1893 78896
1894 58894
1895 10000
1901 ^9998$
1902 9990 9990
1903 5001
1906 AT_DATA_M4SUGAR([script.4s],
1907 [[m4_ifdef([__m4_version__],
1908 [m4_undefine([__m4_version__])],
1909 [m4_divert_push([])48894
1910 9999,10000
1911 78896
1912 58894
1913 10000
1919 ^9998$
1920 9990 9990
1921 5001
1922 m4_exit([0])])
1923 m4_init
1924 m4_divert_push([])[]dnl
1925 m4_len(m4_foreach_w([j], m4_do(m4_for([i], [1], [10000], [], [,i ])), [j ]))
1926 m4_shiftn(9998m4_for([i], [1], [10000], [], [,i]))
1927 m4_len(m4_join([--],, m4_dquote_elt(m4_for([i], [1], [10000], [], [,i])),))
1928 m4_len(m4_joinall([--], m4_map([, m4_echo],
1929   m4_dquote([1]m4_for([i], [2], [10000], [], [,i])))))
1930 m4_max(m4_min([1]m4_for([i], [2], [10000], [],
1931   [,i]))m4_for([i], [2], [10000], [], [,i]))
1932 m4_case([10000]m4_for([i], [1], [10000], [], [,i]),[end])
1933 m4_list_cmp(m4_dquote(1m4_for([i], [2], [10000], [], [,i])),
1934   m4_dquote(m4_reverse(10000m4_for([i], [9999], [1], [], [,i])), [0]))
1935 m4_list_cmp([0], [0m4_for([i], [1], [10000], [], [,0])])
1936 m4_list_cmp([0m4_for([i], [1], [10000], [], [,0])], [0])
1937 m4_for([i], [1], [10000], [], [m4_define(i)])dnl
1938 m4_undefine(1m4_for([i], [2], [10000], [], [,i]))dnl
1939 m4_bpatsubsts([a1]m4_for([i], [1], [10000], [], [,i]), [a2], [A])
1940 m4_bmatch([9997]m4_for([i], [1], [10000], [], [,^i$]))
1941 m4_define([up], [m4_define([$1], m4_incr($1))$1])m4_define([j], 0)dnl
1942 m4_cond(m4_for([i], [1], [10000], [], [[up([j])], [9990], i,]) [oops]) j
1943 m4_count(m4_map_args_pair([,m4_quote], []m4_map_args([,m4_echo]m4_for([i],
1944   [1], [10000], [], [,i]))))
1945 m4_divert_pop([])
1948 AT_CHECK_M4SUGAR([-o-], [0], [[48894
1949 9999,10000
1950 78896
1951 58894
1952 10000
1958 ^9998$
1959 9990 9990
1960 5001
1963 AT_CLEANUP
1966 ## ---------- ##
1967 ## m4_set_*.  ##
1968 ## ---------- ##
1970 AT_SETUP([m4@&t@_set])
1972 AT_KEYWORDS([m4@&t@_set_add m4@&t@_set_add_all m4@&t@_set_contains
1973 m4@&t@_set_contents m4@&t@_set_delete m4@&t@_set_difference m4@&t@_set_dump
1974 m4@&t@_set_empty m4@&t@_set_foreach m4@&t@_set_intersection m4@&t@_set_list
1975 m4@&t@_set_listc m4@&t@_set_map m4@&t@_set_remove m4@&t@_set_size
1976 m4@&t@_set_union])
1978 # Simple tests
1979 AT_CHECK_M4SUGAR_TEXT([[m4_set_contains([a], [1], [yes], [no])
1980 m4_set_add([a], [1], [added], [dup])
1981 m4_set_contains([a], [1], [yes], [no])
1982 m4_set_add([a], [1], [added], [dup])
1983 m4_set_contents([a])
1984 m4_set_remove([a], [1], [removed], [missing])
1985 m4_set_contains([a], [1], [yes], [no])
1986 m4_set_remove([a], [1], [removed], [missing])
1987 m4_set_add([a], [2], [added], [dup])
1988 m4_set_empty([a], [yes], [no])
1989 m4_set_delete([a])
1990 m4_set_empty([a], [yes], [no])
1991 m4_set_add_all([c], [1], [2], [3])
1992 m4_set_add_all([a]m4_set_listc([c]))
1993 m4_set_contents([c], [-])
1994 m4_set_dump([a], [-])
1995 m4_set_contents([a])
1996 m4_set_add_all([a], [1], [2], [3])m4_set_add_all([b], [3], [], [4])
1997 m4_set_difference([a], [b])
1998 m4_set_difference([b], [a])
1999 m4_set_intersection([a], [b])
2000 m4_set_union([a], [b])
2001 m4_define([printodd], [m4_if(m4_eval([$1 & 1]), [1], [:$1])])dnl
2002 m4_set_map([a], [printodd])
2003 m4_set_foreach([a], [i], [m4_if(m4_eval(i & 1), [1], [m4_set_remove([a], i)])])
2004 m4_set_list([a])
2005 m4_set_add([a], [])
2006 m4_set_list([a])
2007 m4_set_remove([a], [2])
2008 m4_dquote(m4_set_list([a]))
2009 m4_set_listc([a])
2010 m4_set_size([a])
2011 m4_set_delete([a])
2012 m4_dquote(m4_set_list([a]))
2013 m4_indir([m4_dquote]m4_set_listc([a]))
2014 m4_set_listc([a])
2015 m4_set_size([a])
2016 ]], [[no
2017 added
2021 removed
2023 missing
2024 added
2030 1-2-3
2031 3-2-1
2034 ,1,2
2037 ,1,2,3,,4
2038 :1:3
2054 # Stress tests - check for unusual names/values
2055 AT_CHECK_M4SUGAR_TEXT([[m4_define([a], [oops])dnl
2056 m4_set_add([a], [a])dnl
2057 m4_set_remove([a], [oops], [yes], [no])
2058 m4_set_add([a,b], [c])dnl
2059 m4_set_add([a,b], [$*[]])dnl
2060 m4_set_add_all([a], [b,c])dnl
2061 m4_set_size([a])
2062 m4_count(m4_set_contents([a], [,]))
2063 m4_count(m4_set_list([a], [,]))
2064 m4_set_dump([a], [,])
2065 m4_set_contents([a,b], [,])
2066 m4_set_list([a,b])
2067 m4_set_foreach([$*[]], [$*[]], [oops])
2068 m4_set_add([$*[]], [])dnl
2069 m4_set_remove([$*[]], [a], [yes], [no])
2070 m4_set_add([$*[]], [a])dnl
2071 m4_set_foreach([$*[]], [$*[]], [-m4_defn([$*[]])m4_indir([$*[]])-])
2072 m4_set_remove([$*[]], [], [yes], [no])
2073 m4_set_add([c], [,])dnl
2074 m4_set_foreach([a,b], [set], [:m4_set_listc(_m4_defn([set])):])
2075 ]],[[no
2079 b,c,a
2080 c,$*[]
2081 c,$*[]
2084 ---aoops-
2086 :,,::,a:
2089 # Stress tests - check for linear scaling (won't necessarily fail if
2090 # quadratic, but hopefully users will complain if it appears to hang)
2091 AT_CHECK_M4SUGAR_TEXT([[dnl
2092 m4_for([i], [1], [10000], [], [m4_set_add([a], i)])dnl
2093 m4_set_add_all([b]m4_for([i], [1], [10000], [], [,i]))dnl
2094 m4_set_remove([a], [1])dnl
2095 m4_set_remove([b], [10000])dnl
2096 m4_set_add_all([a]m4_for([i], [1], [10000], [], [,i]))dnl
2097 m4_for([i], [1], [10000], [], [m4_set_add([b], i)])dnl
2098 m4_len(m4_set_contents([a]))
2099 m4_len(m4_set_foreach([b], [b], [m4_if(m4_eval(b & 1), [1],
2100   [m4_set_remove([b], b, [-])])]))
2101 m4_set_size([b])
2102 m4_define([prune3x], [m4_if(m4_eval([$1 % 3]), [0],
2103   [m4_set_remove([a], [$1], [-])])])dnl
2104 m4_len(m4_set_map([a], [prune3x]))
2105 m4_count(m4_shift(m4_set_intersection([a], [b])))
2106 ]], [[38894
2107 5000
2108 5000
2109 3333
2110 3334
2113 AT_CLEANUP
2116 ## ---------------------- ##
2117 ## __file__ and __line__. ##
2118 ## ---------------------- ##
2120 AT_SETUP([[__file__ and __line__]])
2122 # Check that __file__ and __line__ work.
2123 # Check that m4__file__ and m4__line__ are not defined
2124 # (and get them to pass by the undefined-macro check).
2125 # Try to not assume too much about AT_CHECK_M4SUGAR_TEXT.
2126 AT_CHECK_M4SUGAR_TEXT([[dnl
2127 m4_pattern_allow([m4__file__])dnl
2128 m4_pattern_allow([m4__line__])dnl
2129 m4__file__
2130 m4__line__
2131 __file__
2132 m4_define([first], __line__)dnl
2133 m4_define([second], __line__)dnl
2134 m4_assert(first + 1 == second)dnl
2135 ]], [[m4@&t@__@&t@file__
2136 m4@&t@__@&t@line__
2137 script.4s
2140 AT_CLEANUP