features: add 'feature_bit_sub', which will subtract/unset bits

Given a two sets of featurebits, unset the featurebits in the first set
that are set in the second set
This commit is contained in:
niftynei
2020-10-22 13:18:38 -05:00
committed by neil saitug
parent 0871bf0999
commit ddc9500a64
3 changed files with 66 additions and 0 deletions

View File

@@ -179,6 +179,38 @@ static void test_feature_set_or(void)
}
}
static void test_feature_set_sub(void)
{
struct feature_set *f1, *f2, *control;
for (size_t i = 0; i < ARRAY_SIZE(f1->bits); i++) {
f1 = talz(tmpctx, struct feature_set);
f2 = talz(tmpctx, struct feature_set);
control = talz(tmpctx, struct feature_set);
f1->bits[i] = tal_arr(f1, u8, 0);
f2->bits[i] = tal_arr(f2, u8, 0);
control->bits[i] = tal_arr(control, u8, 0);
/* sub with nothing is a nop */
set_feature_bit(&f1->bits[i], 0);
set_feature_bit(&control->bits[i], 0);
assert(feature_set_eq(f1, control));
assert(feature_set_sub(f1, f2));
/* can't sub feature bit that's not set */
set_feature_bit(&f2->bits[i], 2);
assert(!feature_set_sub(f1, f2));
assert(feature_set_eq(f1, control));
assert(!feature_set_sub(f2, f1));
/* sub does the right thing */
set_feature_bit(&f1->bits[i], 2);
assert(feature_set_sub(f1, f2));
assert(feature_set_eq(f1, control));
assert(!feature_set_sub(f1, f2));
}
}
static void test_feature_trim(void)
{
struct feature_set *f;
@@ -299,6 +331,7 @@ int main(void)
test_featurebits_or();
test_feature_set_or();
test_feature_trim();
test_feature_set_sub();
wally_cleanup(0);
tal_free(tmpctx);