summaryrefslogtreecommitdiff
path: root/subversion/tests/libsvn_delta/range-index-test.h
blob: f330a6a206f92547c550dfda8c5cafbca32386ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * range-index-test.h: An extension for random-test.
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

#ifndef SVN_RANGE_INDEX_TEST_H
#define SVN_RANGE_INDEX_TEST_H

#include "../../libsvn_delta/compose_delta.c"

static range_index_node_t *prev_node, *prev_prev_node;
static apr_size_t
walk_range_index(range_index_node_t *node, const char **msg)
{
  apr_off_t ret;

  if (node == NULL)
    return 0;

  ret = walk_range_index(node->left, msg);
  if (ret > 0)
    return ret;

  if (prev_node != NULL
      && node->target_offset > 0
      && (prev_node->offset >= node->offset
          || (prev_node->limit >= node->limit)))
    {
      ret = node->target_offset;
      node->target_offset = -node->target_offset;
      *msg = "Oops, the previous node ate me.";
      return ret;
    }
  if (prev_prev_node != NULL
      && prev_node->target_offset > 0
      && prev_prev_node->limit > node->offset)
    {
      ret = prev_node->target_offset;
      prev_node->target_offset = -prev_node->target_offset;
      *msg = "Arrgh, my neighbours are conspiring against me.";
      return ret;
    }
  prev_prev_node = prev_node;
  prev_node = node;

  return walk_range_index(node->right, msg);
}


static void
print_node_data(range_index_node_t *node, const char *msg, apr_off_t ndx)
{
  if (-node->target_offset == ndx)
    {
      printf("   * Node: [%3"APR_SIZE_T_FMT
             ",%3"APR_SIZE_T_FMT
             ") = %-5"APR_SIZE_T_FMT"%s\n",
             node->offset, node->limit, -node->target_offset, msg);
    }
  else
    {
      printf("     Node: [%3"APR_SIZE_T_FMT
             ",%3"APR_SIZE_T_FMT
             ") = %"APR_SIZE_T_FMT"\n",
             node->offset, node->limit,
             node->target_offset);
    }
}

static void
print_range_index_r(range_index_node_t *node, const char *msg, apr_off_t ndx)
{
  if (node == NULL)
    return;

  print_range_index_r(node->left, msg, ndx);
  print_node_data(node, msg, ndx);
  print_range_index_r(node->right, msg, ndx);
}

static void
print_range_index_i(range_index_node_t *node, const char *msg, apr_off_t ndx)
{
  if (node == NULL)
    return;

  while (node->prev)
    node = node->prev;

  do
    {
      print_node_data(node, msg, ndx);
      node = node->next;
    }
  while (node);
}

static void
print_range_index(range_index_node_t *node, const char *msg, apr_off_t ndx)
{
  printf("  (recursive)\n");
  print_range_index_r(node, msg, ndx);
  printf("  (iterative)\n");
  print_range_index_i(node, msg, ndx);
}


static void
check_copy_count(int src_cp, int tgt_cp)
{
  printf("Source copies: %d  Target copies: %d\n", src_cp, tgt_cp);
  if (src_cp > tgt_cp)
    printf("WARN: More source than target copies; inefficient combiner?\n");
}


static svn_error_t *
random_range_index_test(apr_pool_t *pool)
{
  apr_uint32_t seed, maxlen;
  apr_size_t bytes_range;
  int i, iterations, dump_files, print_windows;
  const char *random_bytes;
  range_index_t *ndx;
  int tgt_cp = 0, src_cp = 0;

  /* Initialize parameters and print out the seed in case we dump core
     or something. */
  init_params(&seed, &maxlen, &iterations, &dump_files, &print_windows,
              &random_bytes, &bytes_range, pool);

  /* ### This test is expected to fail randomly at the moment, so don't
     enable it by default. --xbc */

  ndx = create_range_index(pool);
  for (i = 1; i <= iterations; ++i)
    {
      apr_size_t offset = svn_test_rand(&seed) % 47;
      apr_size_t limit = offset + svn_test_rand(&seed) % 16 + 1;
      range_list_node_t *list, *r;
      apr_size_t ret;
      const char *msg2;

      printf("%3d: Inserting [%3"APR_SIZE_T_FMT",%3"APR_SIZE_T_FMT") ...",
             i, offset, limit);
      splay_range_index(offset, ndx);
      list = build_range_list(offset, limit, ndx);
      insert_range(offset, limit, i, ndx);
      prev_prev_node = prev_node = NULL;
      ret = walk_range_index(ndx->tree, &msg2);
      if (ret == 0)
        {
          for (r = list; r; r = r->next)
            printf(" %s[%3"APR_SIZE_T_FMT",%3"APR_SIZE_T_FMT")",
                   (r->kind == range_from_source ?
                    (++src_cp, "S") : (++tgt_cp, "T")),
                   r->offset, r->limit);
          free_range_list(list, ndx);
          printf(" OK\n");
        }
      else
        {
          printf(" Ooops!\n");
          print_range_index(ndx->tree, msg2, ret);
          check_copy_count(src_cp, tgt_cp);
          return svn_error_create(SVN_ERR_TEST_FAILED, NULL, "insert_range");
        }
    }

  printf("Final tree state:\n");
  print_range_index(ndx->tree, "", iterations + 1);
  check_copy_count(src_cp, tgt_cp);
  return SVN_NO_ERROR;
}


#endif /* SVN_RANGE_INDEX_TEST_H */