../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
49 / 49
100.0%
Branches:
22 / 44
50.0%
Functions:
1 / 1
100.0%

libavcodec/tests/hashtable.c
Line Branch Exec Source
1 /*
2 * Generic hashtable tests
3 * Copyright (C) 2024 Emma Worley <emma@emma.gg>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavcodec/hashtable.h"
26
27 1 int main(void)
28 {
29 struct FFHashtableContext *ctx;
30 uint8_t k;
31 uint64_t v;
32
33 // impossibly large allocation should fail gracefully
34
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 1 time.
1 av_assert0(ff_hashtable_alloc(&ctx, -1, -1, -1) < 0);
35
36 // hashtable can store up to 3 uint8_t->uint64_t entries
37
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 1 time.
1 av_assert0(!ff_hashtable_alloc(&ctx, sizeof(k), sizeof(v), 3));
38
39 // unsuccessful deletes return 0
40 1 k = 1;
41
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 1 time.
1 av_assert0(!ff_hashtable_delete(ctx, &k));
42
43 // unsuccessful gets return 0
44 1 k = 1;
45
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 1 time.
1 av_assert0(!ff_hashtable_get(ctx, &k, &v));
46
47 // successful sets returns 1
48 1 k = 1;
49 1 v = 1;
50
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 22 taken 1 time.
1 av_assert0(ff_hashtable_set(ctx, &k, &v));
51
52 // get should now contain 1
53 1 k = 1;
54 1 v = 0;
55
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 26 taken 1 time.
1 av_assert0(ff_hashtable_get(ctx, &k, &v));
56
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 29 taken 1 time.
1 av_assert0(v == 1);
57
58 // updating sets should return 1
59 1 k = 1;
60 1 v = 2;
61
1/2
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 1 time.
1 av_assert0(ff_hashtable_set(ctx, &k, &v));
62
63 // get should now contain 2
64 1 k = 1;
65 1 v = 0;
66
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 37 taken 1 time.
1 av_assert0(ff_hashtable_get(ctx, &k, &v));
67
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 1 time.
1 av_assert0(v == 2);
68
69 // fill the table
70 1 k = 2;
71 1 v = 2;
72
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 1 time.
1 av_assert0(ff_hashtable_set(ctx, &k, &v));
73 1 k = 3;
74 1 v = 3;
75
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 48 taken 1 time.
1 av_assert0(ff_hashtable_set(ctx, &k, &v));
76
77 // inserting sets on a full table should return 0
78 1 k = 4;
79 1 v = 4;
80
1/2
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 52 taken 1 time.
1 av_assert0(!ff_hashtable_set(ctx, &k, &v));
81
82 // updating sets on a full table should return 1
83 1 k = 1;
84 1 v = 4;
85
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 56 taken 1 time.
1 av_assert0(ff_hashtable_set(ctx, &k, &v));
86 1 v = 0;
87
1/2
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 60 taken 1 time.
1 av_assert0(ff_hashtable_get(ctx, &k, &v));
88
1/2
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 63 taken 1 time.
1 av_assert0(v == 4);
89
90 // successful deletes should return 1
91 1 k = 1;
92
1/2
✗ Branch 64 → 65 not taken.
✓ Branch 64 → 67 taken 1 time.
1 av_assert0(ff_hashtable_delete(ctx, &k));
93
94 // get should now return 0
95
1/2
✗ Branch 68 → 69 not taken.
✓ Branch 68 → 71 taken 1 time.
1 av_assert0(!ff_hashtable_get(ctx, &k, &v));
96
97 // sanity check remaining keys
98 1 k = 2;
99 1 v = 0;
100
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 75 taken 1 time.
1 av_assert0(ff_hashtable_get(ctx, &k, &v));
101
1/2
✗ Branch 75 → 76 not taken.
✓ Branch 75 → 78 taken 1 time.
1 av_assert0(v == 2);
102 1 k = 3;
103 1 v = 0;
104
1/2
✗ Branch 79 → 80 not taken.
✓ Branch 79 → 82 taken 1 time.
1 av_assert0(ff_hashtable_get(ctx, &k, &v));
105
1/2
✗ Branch 82 → 83 not taken.
✓ Branch 82 → 85 taken 1 time.
1 av_assert0(v == 3);
106
107 1 ff_hashtable_freep(&ctx);
108
109 1 return 0;
110 }
111