FFmpeg
Loading...
Searching...
No Matches
hashtable.c
Go to the documentation of this file.
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"
26
27int main(void)
28{
29 struct FFHashtableContext *ctx;
30 uint8_t k;
31 uint64_t v;
32
33 // impossibly large allocation should fail gracefully
34 av_assert0(ff_hashtable_alloc(&ctx, -1, -1, -1) < 0);
35
36 // hashtable can store up to 3 uint8_t->uint64_t entries
37 av_assert0(!ff_hashtable_alloc(&ctx, sizeof(k), sizeof(v), 3));
38
39 // unsuccessful deletes return 0
40 k = 1;
42
43 // unsuccessful gets return 0
44 k = 1;
46
47 // successful sets returns 1
48 k = 1;
49 v = 1;
51
52 // get should now contain 1
53 k = 1;
54 v = 0;
56 av_assert0(v == 1);
57
58 // updating sets should return 1
59 k = 1;
60 v = 2;
62
63 // get should now contain 2
64 k = 1;
65 v = 0;
67 av_assert0(v == 2);
68
69 // fill the table
70 k = 2;
71 v = 2;
73 k = 3;
74 v = 3;
76
77 // inserting sets on a full table should return 0
78 k = 4;
79 v = 4;
81
82 // updating sets on a full table should return 1
83 k = 1;
84 v = 4;
86 v = 0;
88 av_assert0(v == 4);
89
90 // successful deletes should return 1
91 k = 1;
93
94 // get should now return 0
96
97 // sanity check remaining keys
98 k = 2;
99 v = 0;
101 av_assert0(v == 2);
102 k = 3;
103 v = 0;
105 av_assert0(v == 3);
106
108
109 return 0;
110}
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
av_cold int ff_hashtable_alloc(FFHashtableContext **ctx, size_t key_size, size_t val_size, size_t max_entries)
Create a fixed-sized Robin Hood hash table.
Definition hashtable.c:59
int ff_hashtable_set(struct FFHashtableContext *ctx, const void *key, const void *val)
Store a value in a hash table given a key.
Definition hashtable.c:119
int ff_hashtable_delete(struct FFHashtableContext *ctx, const void *key)
Delete a value from a hash table given a key.
Definition hashtable.c:163
int ff_hashtable_get(const struct FFHashtableContext *ctx, const void *key, void *val)
Look up a value from a hash table given a key.
Definition hashtable.c:97
av_cold void ff_hashtable_freep(FFHashtableContext **ctx)
Free a hash table.
Definition hashtable.c:206
int main(void)
Definition hashtable.c:27