// https://github.com/RoaringBitmap/CRoaring/blob/master/tests/array_container_unit.c#L31
TEST(ArrayContainerTests, AddExisted)
{
    array_container_t *arr = array_container_create();
    EXPECT_TRUE(arr != nullptr);

    int expected_card = 0;

    for (auto i = 0; i < (1 << 16); i += 3)
    {
        EXPECT_TRUE(array_container_add(arr, i));
        EXPECT_TRUE(array_container_contains(arr, i));
        EXPECT_EQ(arr->cardinality, ++expected_card);
        EXPECT_FALSE(arr->cardinality > arr->cardinality);
    }

    for (auto i = 0; i < (1 << 16); ++i)
        EXPECT_EQ(array_container_contains(arr, i), (i / 3 * 3 == i));

    EXPECT_EQ(array_container_cardinality(arr), (1 << 16) / 3 + 1);

    for (auto i = 0; i < (1 << 16); i += 3)
    {
        EXPECT_TRUE(array_container_contains(arr, i));
        EXPECT_TRUE(array_container_remove(arr, i));
        EXPECT_EQ(arr->cardinality, --expected_card);
        EXPECT_FALSE(array_container_contains(arr, i));
    }

    EXPECT_EQ(array_container_cardinality(arr), 0);

    for (int i = 65535; i >= 0; i -= 3)
    {
        EXPECT_TRUE(array_container_add(arr, i));
        EXPECT_TRUE(array_container_contains(arr, i));
        EXPECT_EQ(arr->cardinality, ++expected_card);
        EXPECT_FALSE(arr->cardinality > arr->capacity);
    }

    EXPECT_EQ(array_container_cardinality(arr), expected_card);

    for (int i = 0; i < (1 << 16); ++i)
        EXPECT_EQ(array_container_contains(arr, i), (i / 3 * 3 == i));

    for (int i = 0; i < (1 << 16); i += 3)
    {
        EXPECT_TRUE(array_container_contains(arr, i));
        EXPECT_TRUE(array_container_remove(arr, i));
        EXPECT_EQ(arr->cardinality, --expected_card);
        EXPECT_FALSE(array_container_contains(arr, i));
    }

    array_container_free(arr);
}
int add_test(array_container_t* B) {
    int x;
    for (x = 0; x < (1 << 16); x += 3) {
        array_container_add(B, (uint16_t)x);
    }
    return 0;
}
int main() {
    int repeat = 500;
    int size = TESTSIZE;
    tellmeall();
    printf("array container benchmarks\n");
    array_container_t* B = array_container_create();
    BEST_TIME(add_test(B), 0, repeat, size);
    int answer = contains_test(B);
    size = 1 << 16;
    BEST_TIME(contains_test(B), answer, repeat, size);

    size = (1 << 16) / 3;
    BEST_TIME(remove_test(B), 0, repeat, size);
    array_container_free(B);

    for (int howmany = 32; howmany <= (1 << 16); howmany *= 8) {
        array_container_t* Bt = array_container_create();
        for (int j = 0; j < howmany; ++j) {
            array_container_add(Bt, (uint16_t)pcg32_random());
        }
        size_t nbrtestvalues = 1024;
        uint16_t* testvalues = malloc(nbrtestvalues * sizeof(uint16_t));
        printf("\n number of values in container = %d\n", Bt->cardinality);
        int card = array_container_cardinality(Bt);
        uint32_t* out = malloc(sizeof(uint32_t) * (unsigned long)card);
        BEST_TIME(array_container_to_uint32_array(out, Bt, 1234), card, repeat,
                  card);
        free(out);
        BEST_TIME_PRE_ARRAY(Bt, array_container_contains, array_cache_prefetch,
                            testvalues, nbrtestvalues);
        BEST_TIME_PRE_ARRAY(Bt, array_container_contains, array_cache_flush,
                            testvalues, nbrtestvalues);
        free(testvalues);
        array_container_free(Bt);
    }
    printf("\n");

    array_container_t* B1 = array_container_create();
    for (int x = 0; x < 1 << 16; x += 3) {
        array_container_add(B1, (uint16_t)x);
    }
    array_container_t* B2 = array_container_create();
    for (int x = 0; x < 1 << 16; x += 5) {
        array_container_add(B2, (uint16_t)x);
    }
    int32_t inputsize = B1->cardinality + B2->cardinality;
    array_container_t* BO = array_container_create();
    printf("\nUnion and intersections...\n");
    printf("\nNote:\n");
    printf(
        "union times are expressed in cycles per number of input elements "
        "(both arrays)\n");
    printf(
        "intersection times are expressed in cycles per number of output "
        "elements\n\n");
    printf("==intersection and union test 1 \n");
    printf("input 1 cardinality = %d, input 2 cardinality = %d \n",
           B1->cardinality, B2->cardinality);
    answer = union_test(B1, B2, BO);
    printf("union cardinality = %d \n", answer);
    printf("B1 card = %d B2 card = %d \n", B1->cardinality, B2->cardinality);
    BEST_TIME(union_test(B1, B2, BO), answer, repeat, inputsize);
    answer = intersection_test(B1, B2, BO);
    printf("intersection cardinality = %d \n", answer);
    BEST_TIME(intersection_test(B1, B2, BO), answer, repeat, answer);
    printf("==intersection and union test 2 \n");
    array_container_clear(B1);
    array_container_clear(B2);
    for (int x = 0; x < 1 << 16; x += 16) {
        array_container_add(B1, (uint16_t)x);
    }
    for (int x = 1; x < 1 << 16; x += x) {
        array_container_add(B2, (uint16_t)x);
    }
    printf("input 1 cardinality = %d, input 2 cardinality = %d \n",
           B1->cardinality, B2->cardinality);
    answer = union_test(B1, B2, BO);
    printf("union cardinality = %d \n", answer);
    printf("B1 card = %d B2 card = %d \n", B1->cardinality, B2->cardinality);
    BEST_TIME(union_test(B1, B2, BO), answer, repeat, inputsize);
    answer = intersection_test(B1, B2, BO);
    printf("intersection cardinality = %d \n", answer);
    BEST_TIME(intersection_test(B1, B2, BO), answer, repeat, answer);

    array_container_free(B1);
    array_container_free(B2);
    array_container_free(BO);
    return 0;
}