| File: | .build-ci/../shared/test/test-wrap-util.c |
| Warning: | line 26, column 6 Value of 'errno' was not checked and may be overwritten by function 'fread' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later | |||
| 2 | /* | |||
| 3 | * This file is part of nvme-cli. | |||
| 4 | */ | |||
| 5 | #include <stdbool.h> | |||
| 6 | #include <stdio.h> | |||
| 7 | #include <stdlib.h> | |||
| 8 | #include <string.h> | |||
| 9 | ||||
| 10 | #include <wrap-util.h> | |||
| 11 | ||||
| 12 | static bool_Bool check_bool(const char *name, bool_Bool got) | |||
| 13 | { | |||
| 14 | printf(" - %s [%s]\n", name, got ? "PASS" : "FAIL"); | |||
| 15 | return got; | |||
| 16 | } | |||
| 17 | ||||
| 18 | static char *capture(const char *s, int indent, int start) | |||
| 19 | { | |||
| 20 | static char buf[1024]; | |||
| 21 | FILE *stream = tmpfile(); | |||
| 22 | size_t n; | |||
| 23 | ||||
| 24 | shr_print_word_wrapped(s, indent, start, stream); | |||
| 25 | rewind(stream); | |||
| 26 | n = fread(buf, 1, sizeof(buf) - 1, stream); | |||
| ||||
| 27 | buf[n] = '\0'; | |||
| 28 | fclose(stream); | |||
| 29 | ||||
| 30 | return buf; | |||
| 31 | } | |||
| 32 | ||||
| 33 | int main(void) | |||
| 34 | { | |||
| 35 | bool_Bool pass = true1; | |||
| 36 | char *out; | |||
| 37 | ||||
| 38 | printf("test_print_word_wrapped:\n"); | |||
| 39 | ||||
| 40 | out = capture("short text", 0, 0); | |||
| ||||
| 41 | pass &= check_bool("short text fits on one line", strchr(out, '\n')_Generic (0 ? (out) : (void *) 1, const void *: (const char * ) (strchr (out, '\n')), default: strchr (out, '\n')) == NULL((void*)0)); | |||
| 42 | pass &= check_bool("short text is printed verbatim", !strcmp(out, "short text")); | |||
| 43 | ||||
| 44 | out = capture("one two three four five six seven eight nine ten eleven twelve " | |||
| 45 | "thirteen fourteen fifteen sixteen", 4, 4); | |||
| 46 | pass &= check_bool("long text wraps onto more than one line", strchr(out, '\n')_Generic (0 ? (out) : (void *) 1, const void *: (const char * ) (strchr (out, '\n')), default: strchr (out, '\n')) != NULL((void*)0)); | |||
| 47 | pass &= check_bool("wrapped continuation line is indented", | |||
| 48 | strstr(out, "\n ")_Generic (0 ? (out) : (void *) 1, const void *: (const char * ) (strstr (out, "\n ")), default: strstr (out, "\n ")) != NULL((void*)0)); | |||
| 49 | ||||
| 50 | out = capture("line one\nline two", 2, 0); | |||
| 51 | pass &= check_bool("embedded newline forces a break", | |||
| 52 | strstr(out, "line one\n line two")_Generic (0 ? (out) : (void *) 1, const void *: (const char * ) (strstr (out, "line one\n line two")), default: strstr (out , "line one\n line two")) != NULL((void*)0)); | |||
| 53 | ||||
| 54 | fflush(stdoutstdout); | |||
| 55 | exit(pass ? EXIT_SUCCESS0 : EXIT_FAILURE1); | |||
| 56 | } |