test-seq-range-array.c revision 9f5db9cf866a978aab404772a0daf8794069bba5
183bea41fa640dc8117f3eb45ff935cd81377a84Timo Sirainen/* Copyright (c) 2007-2014 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenboundaries_permute(uint32_t *input, unsigned int i, unsigned int count)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen unsigned int j;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (j = i; j < count; j++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen tmp = input[i]; input[i] = input[j]; input[j] = tmp;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen tmp = input[i]; input[i] = input[j]; input[j] = tmp;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < count; i++)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic void test_seq_range_array_add_boundaries(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen static uint32_t input[] = { 0, 1, (uint32_t)-2, (uint32_t)-1 };
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen boundaries_permute(input, 0, N_ELEMENTS(input));
d22301419109ed4a38351715e6760011421dadecTimo Sirainenstatic void test_seq_range_array_add_merge(void)
d22301419109ed4a38351715e6760011421dadecTimo Sirainenstatic void test_seq_range_array_remove_nth(void)
d22301419109ed4a38351715e6760011421dadecTimo Sirainen const struct seq_range *r;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen r = array_idx(&range, 0); test_assert(r->seq1 == 3 && r->seq2 == 5);
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);
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);
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen unsigned char shadowbuf[SEQ_RANGE_TEST_BUFSIZE];
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (i = 0; i < SEQ_RANGE_TEST_COUNT; i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq2 = seq1 + rand() % (SEQ_RANGE_TEST_BUFSIZE - seq1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_add_range(&range, seq1, seq2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = seq_range_array_remove(&range, seq1) ? 1 : 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen ret = seq_range_array_remove_range(&range, seq1, seq2);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (j > 0 && seqs[j-1].seq2+1 >= seqs[j].seq1)
547e916f4e6f01af682f8b6e032c337f2a699364Timo Sirainen for (; seq1 < SEQ_RANGE_TEST_BUFSIZE; seq1++) {
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen test_out_reason("seq_range_array random", FALSE,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen t_strdup_printf("round %u test %d failed", i, test));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen static const unsigned int input_min = 1, input_max = 5;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen static const unsigned int input[] = {
fa7c76955c6bc62689fbdf39318194f85905e6e2Timo Sirainen for (i = num = 0; input[i] != UINT_MAX; num++, i++) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen seq_range_array_add_with_init(&range, 32, input[i]);
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen for (j = start; j < i; j++) {
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen seq_range_array_invert(&range, input_min, input_max);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen for (seq = input_min; seq <= input_max; seq++) {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen if (seq_range_exists(&range, seq) == old_exists)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen test_out(t_strdup_printf("seq_range_array_invert(%u)", num),
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainenstatic void test_seq_range_create(ARRAY_TYPE(seq_range) *array, uint8_t byte)
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen unsigned int i;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen for (i = 0; i < 8; i++) {
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainenstatic void test_seq_range_array_have_common(void)
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen unsigned int i, j;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen for (i = 0; i < 256; i++) {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen for (j = 0; j < 256; j++) {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen ret1 = seq_range_array_have_common(&arr1, &arr2);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen ret2 = (i & j) != 0;
1c288c40f4dce2e43daa6bc33b96f1618c94119aTimo Sirainen test_out("seq_range_array_have_common()", success);