FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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"
25 #include "libavcodec/hashtable.h"
26 
27 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  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;
45  av_assert0(!ff_hashtable_get(ctx, &k, &v));
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;
80  av_assert0(!ff_hashtable_set(ctx, &k, &v));
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
95  av_assert0(!ff_hashtable_get(ctx, &k, &v));
96 
97  // sanity check remaining keys
98  k = 2;
99  v = 0;
100  av_assert0(ff_hashtable_get(ctx, &k, &v));
101  av_assert0(v == 2);
102  k = 3;
103  v = 0;
104  av_assert0(ff_hashtable_get(ctx, &k, &v));
105  av_assert0(v == 3);
106 
108 
109  return 0;
110 }
ff_hashtable_delete
int ff_hashtable_delete(struct FFHashtableContext *ctx, const void *key)
Delete a value from a hash table given a key.
Definition: hashtable.c:163
ff_hashtable_freep
av_cold void ff_hashtable_freep(FFHashtableContext **ctx)
Free a hash table.
Definition: hashtable.c:206
avassert.h
FFHashtableContext
Definition: hashtable.c:34
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
ctx
AVFormatContext * ctx
Definition: movenc.c:49
hashtable.h
ff_hashtable_set
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
main
int main(void)
Definition: hashtable.c:27
ff_hashtable_get
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
ff_hashtable_alloc
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