| 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 1 not taken.
✓ Branch 2 taken 1 times.
|
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 1 not taken.
✓ Branch 2 taken 1 times.
|
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 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(!ff_hashtable_delete(ctx, &k)); |
| 42 | |||
| 43 | // unsuccessful gets return 0 | ||
| 44 | 1 | k = 1; | |
| 45 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
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 1 not taken.
✓ Branch 2 taken 1 times.
|
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 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(ff_hashtable_get(ctx, &k, &v)); |
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(v == 1); |
| 57 | |||
| 58 | // updating sets should return 1 | ||
| 59 | 1 | k = 1; | |
| 60 | 1 | v = 2; | |
| 61 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
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 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(ff_hashtable_get(ctx, &k, &v)); |
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(v == 2); |
| 68 | |||
| 69 | // fill the table | ||
| 70 | 1 | k = 2; | |
| 71 | 1 | v = 2; | |
| 72 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(ff_hashtable_set(ctx, &k, &v)); |
| 73 | 1 | k = 3; | |
| 74 | 1 | v = 3; | |
| 75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
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 1 not taken.
✓ Branch 2 taken 1 times.
|
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 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(ff_hashtable_set(ctx, &k, &v)); |
| 86 | 1 | v = 0; | |
| 87 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(ff_hashtable_get(ctx, &k, &v)); |
| 88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(v == 4); |
| 89 | |||
| 90 | // successful deletes should return 1 | ||
| 91 | 1 | k = 1; | |
| 92 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(ff_hashtable_delete(ctx, &k)); |
| 93 | |||
| 94 | // get should now return 0 | ||
| 95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
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 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(ff_hashtable_get(ctx, &k, &v)); |
| 101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(v == 2); |
| 102 | 1 | k = 3; | |
| 103 | 1 | v = 0; | |
| 104 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | av_assert0(ff_hashtable_get(ctx, &k, &v)); |
| 105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(v == 3); |
| 106 | |||
| 107 | 1 | ff_hashtable_freep(&ctx); | |
| 108 | |||
| 109 | 1 | return 0; | |
| 110 | } | ||
| 111 |