FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/utf8.c
Date: 2026-09-30 10:56:58
Exec Total Coverage
Lines: 24 29 82.8%
Functions: 3 3 100.0%
Branches: 11 14 78.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but 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
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdio.h>
22
23 #include "libavutil/avstring.h"
24 #include "libavutil/file.h"
25
26 static const uint8_t vectors[] =
27 "A\n"
28 "\xC3\xA9\n" /* U+00E9 */
29 "\xE2\x82\xAC\n" /* U+20AC */
30 "\xF0\x9F\x98\x80\n" /* U+1F600 */
31 "\xF4\x8F\xBF\xBF\n" /* U+10FFFF */
32 "\xF4\x90\x80\x80\n" /* U+110000 */
33 "\xF8\x88\x80\x80\x80\n" /* 5 byte form of U+200000 */
34 "\xC0\x80\n" /* overlong NUL */
35 "\xE0\x80\x80\n" /* overlong NUL */
36 "\xED\xA0\x80\n" /* U+D800 surrogate */
37 "\xEF\xBF\xBF\n" /* U+FFFF */
38 "\x80\n" /* stray continuation */
39 "\xFE\n" /* invalid lead byte */
40 "\xC3" "A\n" /* missing continuation */
41 "\xE2\x82"; /* truncated */
42
43 31 static void print_sequence(const char *p, int l, int indent)
44 {
45 int i;
46
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 31 times.
85 for (i = 0; i < l; i++)
47 54 printf("%02X", (uint8_t)p[i]);
48 31 printf("%*s", indent-l*2, "");
49 31 }
50
51 1 static void decode(const uint8_t *p, const uint8_t *endp)
52 {
53 uint32_t code;
54
55
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 times.
32 while (p < endp) {
56 int l, r;
57 31 const uint8_t *p0 = p;
58 31 code = UINT32_MAX;
59 31 r = av_utf8_decode(&code, &p, endp, 0);
60 31 l = (int)(p-p0);
61 31 print_sequence(p0, l, 20);
62
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 7 times.
31 if (code != UINT32_MAX) {
63 24 printf("%-10d 0x%-10X %-5d ", code, code, l);
64
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 4 times.
24 if (r >= 0) {
65
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6 times.
20 if (*p0 == '\n') printf("\\n\n");
66 6 else printf ("%.*s\n", l, p0);
67 } else {
68 4 printf("invalid code range\n");
69 }
70 } else {
71 7 printf("invalid sequence\n");
72 }
73 }
74 1 }
75
76 1 int main(int argc, char **argv)
77 {
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (argc > 1) {
79 uint8_t *file_buf;
80 size_t file_buf_size;
81 ✗ int ret = av_file_map(argv[1], &file_buf, &file_buf_size, 0, NULL);
82 ✗ if (ret < 0)
83 ✗ return 1;
84 ✗ decode(file_buf, file_buf + file_buf_size);
85 ✗ av_file_unmap(file_buf, file_buf_size);
86 } else {
87 1 decode(vectors, vectors + sizeof(vectors) - 1);
88 }
89
90 1 return 0;
91 }
92