summaryrefslogtreecommitdiffstats
path: root/src/ssl/pqueue/pqueue_test.c
blob: cb688f72260cb5769c33e6535fe27c8f6bcbf1c3 (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
/* Copyright (c) 2014, Google Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

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

#include <openssl/pqueue.h>
#include <openssl/ssl.h>


static void clear_and_free_queue(pqueue q) {
  for (;;) {
    pitem *item = pqueue_pop(q);
    if (item == NULL) {
      break;
    }
    pitem_free(item);
  }
  pqueue_free(q);
}

static int trivial(void) {
  pqueue q = pqueue_new();
  if (q == NULL) {
    return 0;
  }
  int32_t data = 0xdeadbeef;
  uint8_t priority[8] = {0};
  pitem *item = pitem_new(priority, &data);
  if (item == NULL ||
      pqueue_insert(q, item) != item ||
      pqueue_size(q) != 1 ||
      pqueue_peek(q) != item ||
      pqueue_pop(q) != item ||
      pqueue_size(q) != 0 ||
      pqueue_pop(q) != NULL) {
    return 0;
  }
  pitem_free(item);
  clear_and_free_queue(q);
  return 1;
}

#define NUM_ITEMS 10

static int fixed_random(void) {
  /* Random order of 10 elements, chosen by
   * random.choice(list(itertools.permutations(range(10)))) */
  int ordering[NUM_ITEMS] = {9, 6, 3, 4, 0, 2, 7, 1, 8, 5};
  int i;
  pqueue q = pqueue_new();
  uint8_t priority[8] = {0};
  piterator iter;
  pitem *curr, *item;

  if (q == NULL) {
    return 0;
  }

  /* Insert the elements */
  for (i = 0; i < NUM_ITEMS; i++) {
    priority[7] = ordering[i];
    item = pitem_new(priority, &ordering[i]);
    if (pqueue_insert(q, item) != item) {
      return 0;
    }
  }

  /* Insert the elements again. This inserts duplicates and should
   * fail. */
  for (i = 0; i < NUM_ITEMS; i++) {
    priority[7] = ordering[i];
    item = pitem_new(priority, &ordering[i]);
    if (pqueue_insert(q, item) != NULL) {
      return 0;
    }
    pitem_free(item);
  }

  if (pqueue_size(q) != NUM_ITEMS) {
    return 0;
  }

  /* Iterate over the elements. */
  iter = pqueue_iterator(q);
  curr = pqueue_next(&iter);
  if (curr == NULL) {
    return 0;
  }
  while (1) {
    pitem *next = pqueue_next(&iter);
    int *curr_data, *next_data;

    if (next == NULL) {
      break;
    }
    curr_data = (int*)curr->data;
    next_data = (int*)next->data;
    if (*curr_data >= *next_data) {
      return 0;
    }
    curr = next;
  }
  clear_and_free_queue(q);
  return 1;
}

int main(void) {
  SSL_library_init();

  if (!trivial() || !fixed_random()) {
    return 1;
  }

  printf("PASS\n");
  return 0;
}