summaryrefslogtreecommitdiff
path: root/subversion/tests/libsvn_subr/priority-queue-test.c
blob: 3fc5791242c39e6fb24a13777bf4316348affcbe (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * priority-queue-test.c:  a collection of svn_priority_queue__* tests
 *
 * ====================================================================
 *    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.
 * ====================================================================
 */

/* ====================================================================
   To add tests, look toward the bottom of this file.

*/



#include <stdio.h>
#include <string.h>
#include <apr_pools.h>

#include "../svn_test.h"

#include "svn_error.h"
#include "private/svn_sorts_private.h"

/* priority queue test:
 * items in the queue are simple integers, in ascending order */

/* number of items to put into the queue */
enum {NUMBER_COUNT = 11};

/* the actual values in the order we add them to the queue */
static const int numbers[NUMBER_COUNT]
  = { 8395, 0, -1, 3885, 1,  -435, 99993, 10, 0, 1,  8395 };

/* test_update will modify in-queue data and expects the queue to return
   the values in the following order: */
static const int expected_modified[NUMBER_COUNT]
  = { -431, 0, 1, 3,  5, 10, 16, 3889, 8395, 8403,  99997 };

/* standard compare function for integers */
static int
compare_func(const void *lhs, const void *rhs)
{
  return *(const int *)lhs - *(const int *)rhs;
}

/* Check that QUEUE is empty and the usual operations still work */
static svn_error_t *
verify_empty_queue(svn_priority_queue__t *queue)
{
  /* it's an empty queue */
  SVN_TEST_ASSERT(svn_priority_queue__size(queue) == 0);
  SVN_TEST_ASSERT(svn_priority_queue__peek(queue) == NULL);

  /* these should be no-ops */
  svn_priority_queue__update(queue);
  svn_priority_queue__pop(queue);

  return SVN_NO_ERROR;
}

/* check that the tip of QUEUE equals EXPECTED and remove the first element */
static svn_error_t *
extract_expected(svn_priority_queue__t *queue, int expected)
{
  int value = *(int *)svn_priority_queue__peek(queue);
  SVN_TEST_ASSERT(value == expected);
  svn_priority_queue__pop(queue);

  return SVN_NO_ERROR;
}

/* Verify that QUEUE returns all elements in the proper order.
   Also check that data can be added & removed without disturbing the order.
 */
static svn_error_t *
verify_queue_order(svn_priority_queue__t *queue)
{
  int sorted[NUMBER_COUNT];
  int i;

  /* reference order */
  memcpy(sorted, numbers, sizeof(numbers));
  qsort(sorted, NUMBER_COUNT, sizeof(sorted[0]), compare_func);

  /* verify that the queue returns the data in the same order */
  for (i = 0; i < NUMBER_COUNT; ++i)
    {
      int item = *(int *)svn_priority_queue__peek(queue);
      int to_insert;

      /* is this the value we expected? */
      SVN_TEST_ASSERT(item == sorted[i]);

      /* add two items at the tip of the queue */
      to_insert = item - 1;
      svn_priority_queue__push(queue, &to_insert);
      svn_priority_queue__push(queue, &item);

      /* check queue length */
      SVN_TEST_ASSERT(svn_priority_queue__size(queue) == NUMBER_COUNT-i+2);

      /* now, lets extract all 3 of them */
      SVN_ERR(extract_expected(queue, item-1));
      SVN_ERR(extract_expected(queue, item));
      SVN_ERR(extract_expected(queue, item));

      /* check queue length */
      SVN_TEST_ASSERT(svn_priority_queue__size(queue) == NUMBER_COUNT-i-1);
    }

  /* the queue should now be empty */
  SVN_ERR(verify_empty_queue(queue));

  return SVN_NO_ERROR;
}

/* return a queue allocated in POOL containing all items of NUMBERS */
static svn_priority_queue__t *
create_standard_queue(apr_pool_t *pool)
{
  apr_array_header_t *elements
    = apr_array_make(pool, 11, sizeof(numbers[0]));

  /* build queue */
  int i;
  for (i = 0; i < NUMBER_COUNT; ++i)
    APR_ARRAY_PUSH(elements, int) = numbers[i];

  return svn_priority_queue__create(elements, compare_func);
}


static svn_error_t *
test_empty_queue(apr_pool_t *pool)
{
  apr_array_header_t *elements
    = apr_array_make(pool, 0, sizeof(int));
  svn_priority_queue__t *queue
    = svn_priority_queue__create(elements, compare_func);

  SVN_ERR(verify_empty_queue(queue));

  return SVN_NO_ERROR;
}

static svn_error_t *
test_sort_queue(apr_pool_t *pool)
{
  svn_priority_queue__t *queue = create_standard_queue(pool);

  /* data should come out of the queue in sorted order */
  SVN_ERR(verify_queue_order(queue));

  return SVN_NO_ERROR;
}

static svn_error_t *
test_push(apr_pool_t *pool)
{
  apr_array_header_t *elements
    = apr_array_make(pool, 3, sizeof(int));
  svn_priority_queue__t *queue
    = svn_priority_queue__create(elements, compare_func);

  /* build queue */
  int i;
  for (i = 0; i < NUMBER_COUNT; ++i)
    svn_priority_queue__push(queue, &numbers[i]);

  /* data should come out of the queue in sorted order */
  SVN_ERR(verify_queue_order(queue));

  return SVN_NO_ERROR;
}

static svn_error_t *
test_update(apr_pool_t *pool)
{
  svn_priority_queue__t *queue = create_standard_queue(pool);

  /* modify all items in the queue */
  int i;
  for (i = 0; i < NUMBER_COUNT; ++i)
    {
      int *tip = svn_priority_queue__peek(queue);
      *tip += 4;
      svn_priority_queue__update(queue);

      /* extract and verify tip */
      SVN_TEST_ASSERT(*(int *)svn_priority_queue__peek(queue)
                      == expected_modified[i]);
      svn_priority_queue__pop(queue);

      /* this should be a no-op now */
      svn_priority_queue__update(queue);

      SVN_TEST_ASSERT(svn_priority_queue__size(queue) == NUMBER_COUNT-i-1);
    }

  /* the queue should now be empty */
  SVN_ERR(verify_empty_queue(queue));

  return SVN_NO_ERROR;
}

/* An array of all test functions */

static int max_threads = 1;

static struct svn_test_descriptor_t test_funcs[] =
  {
    SVN_TEST_NULL,
    SVN_TEST_PASS2(test_empty_queue,
                   "test empty queue"),
    SVN_TEST_PASS2(test_sort_queue,
                   "data returned by a priority queue shall be ordered"),
    SVN_TEST_PASS2(test_push,
                   "priority queues can be built up incrementally"),
    SVN_TEST_PASS2(test_update,
                   "updating the head of the queue"),
    SVN_TEST_NULL
  };

SVN_TEST_MAIN