chiark / gitweb /
sd-login: fix memleak when output argument is NULL
[elogind.git] / src / test / test-strip-tab-ansi.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2012 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdio.h>
21
22 #include "string-util.h"
23 #include "terminal-util.h"
24 #include "util.h"
25
26 int main(int argc, char *argv[]) {
27         char *p;
28
29         assert_se(p = strdup("\tFoobar\tbar\twaldo\t"));
30         assert_se(strip_tab_ansi(&p, NULL));
31         fprintf(stdout, "<%s>\n", p);
32         assert_se(streq(p, "        Foobar        bar        waldo        "));
33         free(p);
34
35         assert_se(p = strdup(ANSI_HIGHLIGHT "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
36         assert_se(strip_tab_ansi(&p, NULL));
37         fprintf(stdout, "<%s>\n", p);
38         assert_se(streq(p, "Hello world!"));
39         free(p);
40
41         assert_se(p = strdup("\x1B[\x1B[\t\x1B[" ANSI_HIGHLIGHT "\x1B[" "Hello" ANSI_NORMAL ANSI_HIGHLIGHT_RED " world!" ANSI_NORMAL));
42         assert_se(strip_tab_ansi(&p, NULL));
43         assert_se(streq(p, "\x1B[\x1B[        \x1B[\x1B[Hello world!"));
44         free(p);
45
46         assert_se(p = strdup("\x1B[waldo"));
47         assert_se(strip_tab_ansi(&p, NULL));
48         assert_se(streq(p, "\x1B[waldo"));
49         free(p);
50
51         return 0;
52 }