]>
git.proxmox.com Git - grub2.git/blob - tests/printf_unit_test.c
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2011 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/test.h>
22 #include <grub/misc.h>
24 #define MSG "printf test failed: %s, %s", real, expected
26 #if defined(__GNUC__) && __GNUC__ >= 7
27 #pragma GCC diagnostic ignored "-Wformat-truncation="
37 grub_snprintf (real
, sizeof (real
), "%s", null
);
38 snprintf (expected
, sizeof (expected
), "%s", null
);
39 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
41 grub_snprintf (real
, sizeof (real
), "%10s", null
);
42 snprintf (expected
, sizeof (expected
), "%10s", null
);
43 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
45 grub_snprintf (real
, sizeof (real
), "%-10s", null
);
46 snprintf (expected
, sizeof (expected
), "%-10s", null
);
47 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
49 grub_snprintf (real
, sizeof (real
), "%d%%", 10);
50 snprintf (expected
, sizeof (expected
), "%d%%", 10);
51 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
53 grub_snprintf (real
, sizeof (real
), "%d %%", 10);
54 snprintf (expected
, sizeof (expected
), "%d %%", 10);
55 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
57 grub_snprintf (real
, sizeof (real
), "%%");
58 snprintf (expected
, sizeof (expected
), "%%");
59 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
61 grub_snprintf (real
, sizeof (real
), "%d %d %d", 1, 2, 3);
62 snprintf (expected
, sizeof (expected
), "%d %d %d", 1, 2, 3);
63 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
64 grub_snprintf (real
, sizeof (real
), "%3$d %2$d %1$d", 1, 2, 3);
65 snprintf (expected
, sizeof (expected
), "%3$d %2$d %1$d", 1, 2, 3);
66 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
67 grub_snprintf (real
, sizeof (real
), "%d %lld %d", 1, 2LL, 3);
68 snprintf (expected
, sizeof (expected
), "%d %lld %d", 1, 2LL, 3);
69 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
70 grub_snprintf (real
, sizeof (real
), "%3$d %2$lld %1$d", 1, 2LL, 3);
71 snprintf (expected
, sizeof (expected
), "%3$d %2$lld %1$d", 1, 2LL, 3);
72 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
73 grub_snprintf (real
, sizeof (real
), "%%0%dd ", 1);
74 snprintf (expected
, sizeof (expected
), "%%0%dd ", 1);
75 grub_test_assert (strcmp (real
, expected
) == 0, MSG
);
78 GRUB_UNIT_TEST ("printf_unit_test", printf_test
);