bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainenboundaries_permute(uint32_t *input, unsigned int i, unsigned int count)
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainen unsigned int j;
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainen for (j = i; j < count; j++) {
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainen tmp = input[i]; input[i] = input[j]; input[j] = tmp;
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainen tmp = input[i]; input[i] = input[j]; input[j] = tmp;
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainen for (i = 0; i < count; i++)
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainenstatic void test_seq_range_array_add_boundaries(void)
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainen static uint32_t input[] = { 0, 1, (uint32_t)-2, (uint32_t)-1 };
4082d5b171d1c3a00ba705093d62b8afc9cf17aeTimo Sirainen boundaries_permute(input, 0, N_ELEMENTS(input));
d06d6667bac64aabe1efb216af56ca45108d63b0Timo Sirainenstatic void test_seq_range_array_add_merge(void)
c8bac7666cdc780a3390110e420350fffb62b909Timo Sirainen seq_range_array_add_range(&range, 1, (uint32_t)-1);
c8bac7666cdc780a3390110e420350fffb62b909Timo Sirainen seq_range_array_add_range(&range, 1, (uint32_t)-1);
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainenstatic void test_seq_range_array_remove_nth(void)
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen const struct seq_range *r;
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen r = array_idx(&range, 0); test_assert(r->seq1 == 3 && r->seq2 == 5);
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen r = array_idx(&range, 0); test_assert(r->seq1 == 3 && r->seq2 == 3);
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen r = array_idx(&range, 1); test_assert(r->seq1 == 11 && r->seq2 == 20);
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen seq_range_array_remove_nth(&range, 5, (uint32_t)-1);
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen r = array_idx(&range, 1); test_assert(r->seq1 == 11 && r->seq2 == 14);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen unsigned char shadowbuf[SEQ_RANGE_TEST_BUFSIZE];
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen for (i = 0; i < SEQ_RANGE_TEST_COUNT; i++) {
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi seq2 = seq1 + i_rand() % (SEQ_RANGE_TEST_BUFSIZE - seq1);
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody ret = seq_range_array_add(&range, seq1) ? 0 : 1; /* FALSE == added */
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody ret = seq_range_array_add_range_count(&range, seq1, seq2);
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainen ret = seq_range_array_remove(&range, seq1) ? 1 : 0;
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainen ret = seq_range_array_remove_range(&range, seq1, seq2);
d06d6667bac64aabe1efb216af56ca45108d63b0Timo Sirainen if (j > 0 && seqs[j-1].seq2+1 >= seqs[j].seq1)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen for (; seq1 < SEQ_RANGE_TEST_BUFSIZE; seq1++) {
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen test_out_reason("seq_range_array random", FALSE,
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen t_strdup_printf("round %u test %d failed", i, test));
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainenstatic void test_seq_range_array_invert_minmax(uint32_t min, uint32_t max)
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen unsigned int n, inverse_mask, mask_inside, mask_size = max-min+1;
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen i_assert(mask_size <= sizeof(unsigned int)*8);
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen for (unsigned int mask = 0; mask < mask_size; mask++) {
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen for (unsigned int i = 0; i < mask_size; i++) {
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen seq_range_array_iter_init(&iter, &range); n = 0;
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen while (seq_range_array_iter_nth(&iter, n++, &seq)) {
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen test_assert_idx((inverse_mask & ~mask_inside) == 0, mask);
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen test_assert_idx(inverse_mask == (mask ^ mask_inside), mask);
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen /* first numbers */
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen for (unsigned int max = min; max <= 7; max++) T_BEGIN {
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen /* last numbers */
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen for (uint64_t min = 0xffffffff-7; min <= 0xffffffff; min++) {
f7bd22354fb9311dfbf3e340a69c15d8b00facc1Timo Sirainen for (uint64_t max = min; max <= 0xffffffff; max++) T_BEGIN {
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainenstatic void test_seq_range_array_invert_edges(void)
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen int64_t resa_seq1, resa_seq2, resb_seq1, resb_seq2;
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) T_BEGIN {
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen seq_range_array_add_range(&range, tests[i].a_seq1, tests[i].a_seq2);
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen seq_range_array_add_range(&range, tests[i].b_seq1, tests[i].b_seq2);
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen seq_range_array_invert(&range, 0, 0xffffffff);
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen test_assert(result[0].seq1 == tests[i].resa_seq1);
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen test_assert(result[0].seq2 == tests[i].resa_seq2);
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen test_assert(result[1].seq1 == tests[i].resb_seq1);
369764cc0856a4c5e9b4081de8c4e29e90f11ccdTimo Sirainen test_assert(result[1].seq2 == tests[i].resb_seq2);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenstatic void test_seq_range_create(ARRAY_TYPE(seq_range) *array, uint8_t byte)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen unsigned int i;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen for (i = 0; i < 8; i++) {
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainenstatic void test_seq_range_array_have_common(void)
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen unsigned int i, j;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen for (i = 0; i < 256; i++) {
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen for (j = 0; j < 256; j++) {
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen ret1 = seq_range_array_have_common(&arr1, &arr2);
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen ret2 = (i & j) != 0;
48acc31adebfdd4e4945ee76e1f5259e4b1b6fffTimo Sirainen test_out("seq_range_array_have_common()", success);