1/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <stdio.h>
16#include <string.h>
17
18#include <openssl/pqueue.h>
19#include <openssl/ssl.h>
20
21
22static int trivial() {
23  pqueue q = pqueue_new();
24  if (q == NULL) {
25    return 0;
26  }
27  int32_t data = 0xdeadbeef;
28  uint8_t priority[8] = {0};
29  pitem *item = pitem_new(priority, &data);
30  if (item == NULL ||
31      pqueue_insert(q, item) != item ||
32      pqueue_size(q) != 1 ||
33      pqueue_peek(q) != item ||
34      pqueue_pop(q) != item ||
35      pqueue_size(q) != 0 ||
36      pqueue_pop(q) != NULL) {
37    return 0;
38  }
39  pitem_free(item);
40  pqueue_free(q);
41  return 1;
42}
43
44#define NUM_ITEMS 10
45
46static int fixed_random() {
47  /* Random order of 10 elements, chosen by
48     random.choice(list(itertools.permutations(range(10)))) */
49  int ordering[NUM_ITEMS] = {9, 6, 3, 4, 0, 2, 7, 1, 8, 5};
50  int i;
51  pqueue q = pqueue_new();
52  if (q == NULL) {
53    return 0;
54  }
55  uint8_t priority[8] = {0};
56  /* Insert the elements */
57  for (i = 0; i < NUM_ITEMS; i++) {
58    priority[7] = ordering[i];
59    pitem *item = pitem_new(priority, &ordering[i]);
60    pqueue_insert(q, item);
61  }
62  piterator iter = pqueue_iterator(q);
63  pitem *curr = pqueue_next(&iter);
64  if (curr == NULL) {
65    return 0;
66  }
67  while (1) {
68    pitem *next = pqueue_next(&iter);
69    if (next == NULL) {
70      break;
71    }
72    int *curr_data = (int*)curr->data;
73    int *next_data = (int*)next->data;
74    if (*curr_data >= *next_data) {
75      return 0;
76    }
77    curr = next;
78  }
79  return 1;
80}
81
82int main(void) {
83  SSL_library_init();
84
85  if (!trivial() || !fixed_random()) {
86    return 1;
87  }
88
89  printf("PASS\n");
90  return 0;
91}
92