File: assert_for_each.c

package info (click to toggle)
metalang99 1.13.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,428 kB
  • sloc: ansic: 6,748; python: 53; sh: 48; makefile: 18
file content (31 lines) | stat: -rw-r--r-- 1,194 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Asserts multiple expressions at once.

#include <metalang99.h>

#include <assert.h>

#define ASSERT_FOR_EACH(...)                                                                       \
    do {                                                                                           \
        ML99_EVAL(ML99_variadicsForEach(                                                           \
            ML99_compose(v(ML99_semicoloned), ML99_reify(v(assert))),                              \
            v(__VA_ARGS__)))                                                                       \
    } while (0)

int main(void) {
    ASSERT_FOR_EACH(123 == 123, 2 + 2 == 4, "foo"[1] == 'o');

    /*
     * If we combine multiple assertions with the && operator, we will not be able to distinguish
     * them if one of them fails apparently:
     *
     * main: Assertion `123 == 321 && 2 + 2 == 4 && "foo"[1] == 'o' failed.
     * assert(123 == 321 && 2 + 2 == 4 && "foo"[1] == 'o');
     */

    /*
     * ... unlike `ASSERT_FOR_EACH` telling us which one has failed:
     *
     * main: Assertion `123 == 321' failed.
     * ASSERT_FOR_EACH(123 == 321, 2 + 2 == 4, "foo"[1] == 'o');
     */
}