|
| 1 | +#include "config.h" |
| 2 | +#include <assert.h> |
| 3 | +#include <ccan/array_size/array_size.h> |
| 4 | +#include <ccan/crypto/siphash24/siphash24.h> |
| 5 | +#include <ccan/htable/htable_type.h> |
| 6 | +#include <ccan/short_types/short_types.h> |
| 7 | +#include <ccan/tal/tal.h> |
| 8 | +#include <common/amount.h> |
| 9 | +#include <common/pseudorand.h> |
| 10 | +#include <common/setup.h> |
| 11 | +#include <common/utils.h> |
| 12 | +#include <inttypes.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <wire/wire.h> |
| 15 | + |
| 16 | +/* AUTOGENERATED MOCKS START */ |
| 17 | +/* Generated stub for amount_asset_is_main */ |
| 18 | +bool amount_asset_is_main(struct amount_asset *asset UNNEEDED) |
| 19 | +{ fprintf(stderr, "amount_asset_is_main called!\n"); abort(); } |
| 20 | +/* Generated stub for amount_asset_to_sat */ |
| 21 | +struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED) |
| 22 | +{ fprintf(stderr, "amount_asset_to_sat called!\n"); abort(); } |
| 23 | +/* Generated stub for amount_feerate */ |
| 24 | + bool amount_feerate(u32 *feerate UNNEEDED, struct amount_sat fee UNNEEDED, size_t weight UNNEEDED) |
| 25 | +{ fprintf(stderr, "amount_feerate called!\n"); abort(); } |
| 26 | +/* Generated stub for amount_sat */ |
| 27 | +struct amount_sat amount_sat(u64 satoshis UNNEEDED) |
| 28 | +{ fprintf(stderr, "amount_sat called!\n"); abort(); } |
| 29 | +/* Generated stub for amount_sat_add */ |
| 30 | + bool amount_sat_add(struct amount_sat *val UNNEEDED, |
| 31 | + struct amount_sat a UNNEEDED, |
| 32 | + struct amount_sat b UNNEEDED) |
| 33 | +{ fprintf(stderr, "amount_sat_add called!\n"); abort(); } |
| 34 | +/* Generated stub for amount_sat_eq */ |
| 35 | +bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) |
| 36 | +{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); } |
| 37 | +/* Generated stub for amount_sat_greater_eq */ |
| 38 | +bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED) |
| 39 | +{ fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); } |
| 40 | +/* Generated stub for amount_sat_sub */ |
| 41 | + bool amount_sat_sub(struct amount_sat *val UNNEEDED, |
| 42 | + struct amount_sat a UNNEEDED, |
| 43 | + struct amount_sat b UNNEEDED) |
| 44 | +{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); } |
| 45 | +/* Generated stub for amount_sat_to_asset */ |
| 46 | +struct amount_asset amount_sat_to_asset(struct amount_sat *sat UNNEEDED, const u8 *asset UNNEEDED) |
| 47 | +{ fprintf(stderr, "amount_sat_to_asset called!\n"); abort(); } |
| 48 | +/* Generated stub for amount_tx_fee */ |
| 49 | +struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED) |
| 50 | +{ fprintf(stderr, "amount_tx_fee called!\n"); abort(); } |
| 51 | +/* Generated stub for fromwire */ |
| 52 | +const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED) |
| 53 | +{ fprintf(stderr, "fromwire called!\n"); abort(); } |
| 54 | +/* Generated stub for fromwire_bool */ |
| 55 | +bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) |
| 56 | +{ fprintf(stderr, "fromwire_bool called!\n"); abort(); } |
| 57 | +/* Generated stub for fromwire_fail */ |
| 58 | +void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) |
| 59 | +{ fprintf(stderr, "fromwire_fail called!\n"); abort(); } |
| 60 | +/* Generated stub for fromwire_secp256k1_ecdsa_signature */ |
| 61 | +void fromwire_secp256k1_ecdsa_signature(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, |
| 62 | + secp256k1_ecdsa_signature *signature UNNEEDED) |
| 63 | +{ fprintf(stderr, "fromwire_secp256k1_ecdsa_signature called!\n"); abort(); } |
| 64 | +/* Generated stub for fromwire_sha256 */ |
| 65 | +void fromwire_sha256(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct sha256 *sha256 UNNEEDED) |
| 66 | +{ fprintf(stderr, "fromwire_sha256 called!\n"); abort(); } |
| 67 | +/* Generated stub for fromwire_tal_arrn */ |
| 68 | +u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED, |
| 69 | + const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED) |
| 70 | +{ fprintf(stderr, "fromwire_tal_arrn called!\n"); abort(); } |
| 71 | +/* Generated stub for fromwire_u32 */ |
| 72 | +u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) |
| 73 | +{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); } |
| 74 | +/* Generated stub for fromwire_u64 */ |
| 75 | +u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) |
| 76 | +{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); } |
| 77 | +/* Generated stub for fromwire_u8 */ |
| 78 | +u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) |
| 79 | +{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); } |
| 80 | +/* Generated stub for fromwire_u8_array */ |
| 81 | +void fromwire_u8_array(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, u8 *arr UNNEEDED, size_t num UNNEEDED) |
| 82 | +{ fprintf(stderr, "fromwire_u8_array called!\n"); abort(); } |
| 83 | +/* Generated stub for towire */ |
| 84 | +void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED) |
| 85 | +{ fprintf(stderr, "towire called!\n"); abort(); } |
| 86 | +/* Generated stub for towire_bool */ |
| 87 | +void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED) |
| 88 | +{ fprintf(stderr, "towire_bool called!\n"); abort(); } |
| 89 | +/* Generated stub for towire_secp256k1_ecdsa_signature */ |
| 90 | +void towire_secp256k1_ecdsa_signature(u8 **pptr UNNEEDED, |
| 91 | + const secp256k1_ecdsa_signature *signature UNNEEDED) |
| 92 | +{ fprintf(stderr, "towire_secp256k1_ecdsa_signature called!\n"); abort(); } |
| 93 | +/* Generated stub for towire_sha256 */ |
| 94 | +void towire_sha256(u8 **pptr UNNEEDED, const struct sha256 *sha256 UNNEEDED) |
| 95 | +{ fprintf(stderr, "towire_sha256 called!\n"); abort(); } |
| 96 | +/* Generated stub for towire_u32 */ |
| 97 | +void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED) |
| 98 | +{ fprintf(stderr, "towire_u32 called!\n"); abort(); } |
| 99 | +/* Generated stub for towire_u64 */ |
| 100 | +void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED) |
| 101 | +{ fprintf(stderr, "towire_u64 called!\n"); abort(); } |
| 102 | +/* Generated stub for towire_u8 */ |
| 103 | +void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED) |
| 104 | +{ fprintf(stderr, "towire_u8 called!\n"); abort(); } |
| 105 | +/* Generated stub for towire_u8_array */ |
| 106 | +void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED) |
| 107 | +{ fprintf(stderr, "towire_u8_array called!\n"); abort(); } |
| 108 | +/* AUTOGENERATED MOCKS END */ |
| 109 | + |
| 110 | +struct example { |
| 111 | + u32 val; |
| 112 | + u32 val2; |
| 113 | + struct example *ptr; |
| 114 | +}; |
| 115 | + |
| 116 | +static struct siphash_seed seed; |
| 117 | + |
| 118 | +static u64 example_key(const struct example *e) |
| 119 | +{ |
| 120 | + return (u64)e->val << 32 | e->val2; |
| 121 | +} |
| 122 | +static bool example_eq_key(const struct example *e, u64 key) |
| 123 | +{ |
| 124 | + return example_key(e) == key ; |
| 125 | +} |
| 126 | +static size_t example_key_hash(u64 key) |
| 127 | +{ |
| 128 | + return siphash24(&seed, &key, sizeof(key)); |
| 129 | +} |
| 130 | +HTABLE_DEFINE_TYPE(struct example, example_key, example_key_hash, example_eq_key, |
| 131 | + example_htable); |
| 132 | + |
| 133 | +static struct example *new_example(const tal_t *ctx, size_t i) |
| 134 | +{ |
| 135 | + struct example *e = tal(ctx, struct example); |
| 136 | + e->val = i; |
| 137 | + e->val2 = siphash24(&seed, &i, sizeof(i)); |
| 138 | + return e; |
| 139 | +} |
| 140 | + |
| 141 | +static void check_htable(struct example_htable *htable, |
| 142 | + struct example *arr[], |
| 143 | + size_t num_arr) |
| 144 | +{ |
| 145 | + for (size_t i = 0; i < num_arr; i++) { |
| 146 | + if (arr[i]) |
| 147 | + assert(example_htable_get(htable, example_key(arr[i])) == arr[i]); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +int main(int argc, char *argv[]) |
| 152 | +{ |
| 153 | + struct example *arr[3500]; |
| 154 | + struct example_htable *htable; |
| 155 | + size_t i; |
| 156 | + |
| 157 | + common_setup(argv[0]); |
| 158 | + |
| 159 | + seed = *siphash_seed(); |
| 160 | + if (argc > 1) |
| 161 | + seed.u.u64[0] = atoll(argv[1]); |
| 162 | + if (argc > 2) |
| 163 | + seed.u.u64[1] = atoll(argv[2]); |
| 164 | + |
| 165 | + printf("Seed = %"PRIu64"/%"PRIu64"\n", |
| 166 | + seed.u.u64[0], seed.u.u64[1]); |
| 167 | + |
| 168 | + |
| 169 | + /* Randomly add or delete, check itegrity */ |
| 170 | + i = 0; |
| 171 | + while (i < 10000) { |
| 172 | + htable = tal(tmpctx, struct example_htable); |
| 173 | + example_htable_init(htable); |
| 174 | + memset(arr, 0, sizeof(arr)); |
| 175 | + check_htable(htable, arr, ARRAY_SIZE(arr)); |
| 176 | + |
| 177 | + while (example_htable_count(htable) < 900) { |
| 178 | + size_t rand = siphash24(&seed, &i, sizeof(i)) % ARRAY_SIZE(arr); |
| 179 | + if (arr[rand]) { |
| 180 | + example_htable_del(htable, arr[rand]); |
| 181 | + arr[rand] = NULL; |
| 182 | + } else { |
| 183 | + arr[rand] = new_example(htable, rand); |
| 184 | + example_htable_add(htable, arr[rand]); |
| 185 | + } |
| 186 | + check_htable(htable, arr, ARRAY_SIZE(arr)); |
| 187 | + i++; |
| 188 | + } |
| 189 | + printf("%zu - %zu\n", i, example_htable_count(htable)); |
| 190 | + htable_check(&htable->raw, "check"); |
| 191 | + } |
| 192 | + common_shutdown(); |
| 193 | + return 0; |
| 194 | +} |
0 commit comments