test-seq-range-array.c revision 9f5db9cf866a978aab404772a0daf8794069bba5
183bea41fa640dc8117f3eb45ff935cd81377a84Timo Sirainen/* Copyright (c) 2007-2014 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "test-lib.h"
bca919b207e27d0d08b431bdb0f2ac099ef8b512Timo Sirainen#include "array.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "seq-range-array.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
7a7d2aa11e46195e2d92d6c337d7e78052a5ce67Timo Sirainen#include <stdlib.h>
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenboundaries_permute(uint32_t *input, unsigned int i, unsigned int count)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ARRAY_TYPE(seq_range) range;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const struct seq_range *seqs;
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen unsigned int seqs_count;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t tmp;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int j;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (i+1 < count) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (j = i; j < count; j++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen tmp = input[i]; input[i] = input[j]; input[j] = tmp;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen boundaries_permute(input, i+1, count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen tmp = input[i]; input[i] = input[j]; input[j] = tmp;
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
afa201e7e1d2447e8dfa1aff43de0fdad564105fTimo Sirainen t_array_init(&range, 4);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_add(&range, input[i]);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seqs = array_get(&range, &seqs_count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(seqs_count == 2);
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen test_assert(seqs[0].seq1 == 0);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(seqs[0].seq2 == 1);
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen test_assert(seqs[1].seq1 == (uint32_t)-2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_assert(seqs[1].seq2 == (uint32_t)-1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
d22301419109ed4a38351715e6760011421dadecTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void test_seq_range_array_add_boundaries(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen static uint32_t input[] = { 0, 1, (uint32_t)-2, (uint32_t)-1 };
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen boundaries_permute(input, 0, N_ELEMENTS(input));
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainenstatic void test_seq_range_array_add_merge(void)
d22301419109ed4a38351715e6760011421dadecTimo Sirainen{
d22301419109ed4a38351715e6760011421dadecTimo Sirainen ARRAY_TYPE(seq_range) range;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainen test_begin("seq_range_array_add() merging");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen t_array_init(&range, 8);
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen seq_range_array_add(&range, 4);
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen seq_range_array_add(&range, 1);
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen seq_range_array_add(&range, 2);
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen test_assert(array_count(&range) == 2);
b780aa272b742a43579cdb523cc79cc8d4521306Timo Sirainen test_end();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
d22301419109ed4a38351715e6760011421dadecTimo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainenstatic void test_seq_range_array_remove_nth(void)
d22301419109ed4a38351715e6760011421dadecTimo Sirainen{
d22301419109ed4a38351715e6760011421dadecTimo Sirainen ARRAY_TYPE(seq_range) range;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen const struct seq_range *r;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_begin("seq_range_array_remove_nth()");
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen t_array_init(&range, 8);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_add_range(&range, 1, 5);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen seq_range_array_add(&range, 7);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_add_range(&range, 10,20);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen test_assert(array_count(&range) == 3);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_remove_nth(&range, 0, 2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen r = array_idx(&range, 0); test_assert(r->seq1 == 3 && r->seq2 == 5);
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_remove_nth(&range, 1, 4);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen r = array_idx(&range, 0); test_assert(r->seq1 == 3 && r->seq2 == 3);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen r = array_idx(&range, 1); test_assert(r->seq1 == 11 && r->seq2 == 20);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen seq_range_array_remove_nth(&range, 5, (uint32_t)-1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen r = array_idx(&range, 1); test_assert(r->seq1 == 11 && r->seq2 == 14);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen test_assert(array_count(&range) == 2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test_end();
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainenstatic void test_seq_range_array_random(void)
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen{
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen#define SEQ_RANGE_TEST_BUFSIZE 20
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen#define SEQ_RANGE_TEST_COUNT 10000
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen unsigned char shadowbuf[SEQ_RANGE_TEST_BUFSIZE];
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen ARRAY_TYPE(seq_range) range;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen const struct seq_range *seqs;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen uint32_t seq1, seq2;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen unsigned int i, j, ret, ret2, count;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen int test = -1;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = ret2 = 0;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen i_array_init(&range, 1);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen memset(shadowbuf, 0, sizeof(shadowbuf));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < SEQ_RANGE_TEST_COUNT; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq1 = rand() % SEQ_RANGE_TEST_BUFSIZE;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq2 = seq1 + rand() % (SEQ_RANGE_TEST_BUFSIZE - seq1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen test = rand() % 4;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen switch (test) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen case 0:
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_add(&range, seq1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen shadowbuf[seq1] = 1;
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen break;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen case 1:
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_add_range(&range, seq1, seq2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen memset(shadowbuf + seq1, 1, seq2 - seq1 + 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen break;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen case 2:
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = seq_range_array_remove(&range, seq1) ? 1 : 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret2 = shadowbuf[seq1] != 0 ? 1 : 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen shadowbuf[seq1] = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen break;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen case 3:
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = seq_range_array_remove_range(&range, seq1, seq2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (ret2 = 0; seq1 <= seq2; seq1++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (shadowbuf[seq1] != 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret2++;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen shadowbuf[seq1] = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen break;
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen }
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen if (ret != ret2)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen break;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen seqs = array_get(&range, &count);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (j = 0, seq1 = 0; j < count; j++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (j > 0 && seqs[j-1].seq2+1 >= seqs[j].seq1)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen goto fail;
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen for (; seq1 < seqs[j].seq1; seq1++) {
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen if (shadowbuf[seq1] != 0)
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen goto fail;
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (; seq1 <= seqs[j].seq2; seq1++) {
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen if (shadowbuf[seq1] == 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen goto fail;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
547e916f4e6f01af682f8b6e032c337f2a699364Timo Sirainen }
547e916f4e6f01af682f8b6e032c337f2a699364Timo Sirainen i_assert(seq1 <= SEQ_RANGE_TEST_BUFSIZE);
547e916f4e6f01af682f8b6e032c337f2a699364Timo Sirainen for (; seq1 < SEQ_RANGE_TEST_BUFSIZE; seq1++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (shadowbuf[seq1] != 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen goto fail;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenfail:
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen if (i == SEQ_RANGE_TEST_COUNT)
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen test_out("seq_range_array random", TRUE);
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen else {
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen test_out_reason("seq_range_array random", FALSE,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen t_strdup_printf("round %u test %d failed", i, test));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen array_free(&range);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void test_seq_range_array_invert(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen static const unsigned int input_min = 1, input_max = 5;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen static const unsigned int input[] = {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen 1, 2, 3, 4, 5, UINT_MAX,
ee9254cc7875519a9c71cc58a40610e6f320e907Timo Sirainen 2, 3, 4, UINT_MAX,
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen 1, 2, 4, 5, UINT_MAX,
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen 1, 3, 5, UINT_MAX,
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen 1, UINT_MAX,
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen 5, UINT_MAX,
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen UINT_MAX
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen };
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen ARRAY_TYPE(seq_range) range = ARRAY_INIT;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int i, j, seq, start, num;
ca98d6a1bbe73499da758a36bfab2963375c8d06Timo Sirainen bool old_exists, success;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
fa7c76955c6bc62689fbdf39318194f85905e6e2Timo Sirainen for (i = num = 0; input[i] != UINT_MAX; num++, i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen success = TRUE;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen start = i;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (; input[i] != UINT_MAX; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_add_with_init(&range, 32, input[i]);
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen for (j = start; j < i; j++) {
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen if (!seq_range_exists(&range, input[j]))
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen success = FALSE;
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen }
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen }
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen seq_range_array_invert(&range, input_min, input_max);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (seq = input_min; seq <= input_max; seq++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (j = start; input[j] != UINT_MAX; j++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (input[j] == seq)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen break;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen }
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen old_exists = input[j] != UINT_MAX;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen if (seq_range_exists(&range, seq) == old_exists)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen success = FALSE;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen }
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen test_out(t_strdup_printf("seq_range_array_invert(%u)", num),
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen success);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen array_free(&range);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen }
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen}
0a7b04ec6441fdcf083392888b2e30844fc3e86dTimo Sirainen
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainenstatic void test_seq_range_create(ARRAY_TYPE(seq_range) *array, uint8_t byte)
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen{
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen unsigned int i;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen array_clear(array);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen for (i = 0; i < 8; i++) {
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen if ((byte & (1 << i)) != 0)
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen seq_range_array_add(array, i + 1);
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen }
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen}
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainenstatic void test_seq_range_array_have_common(void)
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen{
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen ARRAY_TYPE(seq_range) arr1, arr2;
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen unsigned int i, j;
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen bool ret1, ret2, success = TRUE;
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen t_array_init(&arr1, 8);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen t_array_init(&arr2, 8);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen for (i = 0; i < 256; i++) {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen test_seq_range_create(&arr1, i);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen for (j = 0; j < 256; j++) {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen test_seq_range_create(&arr2, j);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen ret1 = seq_range_array_have_common(&arr1, &arr2);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen ret2 = (i & j) != 0;
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen if (ret1 != ret2)
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen success = FALSE;
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen }
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen }
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen test_out("seq_range_array_have_common()", success);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen}
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainenvoid test_seq_range_array(void)
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen{
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen test_seq_range_array_add_boundaries();
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen test_seq_range_array_add_merge();
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen test_seq_range_array_remove_nth();
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen test_seq_range_array_invert();
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen test_seq_range_array_have_common();
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen test_seq_range_array_random();
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen}
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen