2014-08-30 03:41:39 +01:00
|
|
|
/* 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>
|
2014-09-12 00:11:15 +01:00
|
|
|
#include <openssl/ssl.h>
|
2014-08-30 03:41:39 +01:00
|
|
|
|
|
|
|
|
2015-02-11 19:16:28 +00:00
|
|
|
static void clear_and_free_queue(pqueue q) {
|
|
|
|
for (;;) {
|
|
|
|
pitem *item = pqueue_pop(q);
|
|
|
|
if (item == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pitem_free(item);
|
|
|
|
}
|
|
|
|
pqueue_free(q);
|
|
|
|
}
|
|
|
|
|
2015-01-29 00:20:02 +00:00
|
|
|
static int trivial(void) {
|
2014-08-30 03:41:39 +01:00
|
|
|
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);
|
2015-02-11 19:16:28 +00:00
|
|
|
clear_and_free_queue(q);
|
2014-08-30 03:41:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NUM_ITEMS 10
|
|
|
|
|
2015-01-29 00:20:02 +00:00
|
|
|
static int fixed_random(void) {
|
2014-08-30 03:41:39 +01:00
|
|
|
/* Random order of 10 elements, chosen by
|
2014-11-04 07:16:34 +00:00
|
|
|
* random.choice(list(itertools.permutations(range(10)))) */
|
2014-08-30 03:41:39 +01:00
|
|
|
int ordering[NUM_ITEMS] = {9, 6, 3, 4, 0, 2, 7, 1, 8, 5};
|
|
|
|
int i;
|
|
|
|
pqueue q = pqueue_new();
|
2014-11-04 07:16:34 +00:00
|
|
|
uint8_t priority[8] = {0};
|
|
|
|
piterator iter;
|
|
|
|
pitem *curr, *item;
|
|
|
|
|
2014-08-30 03:41:39 +01:00
|
|
|
if (q == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-04 07:16:34 +00:00
|
|
|
|
2014-08-30 03:41:39 +01:00
|
|
|
/* Insert the elements */
|
|
|
|
for (i = 0; i < NUM_ITEMS; i++) {
|
|
|
|
priority[7] = ordering[i];
|
2014-11-04 07:16:34 +00:00
|
|
|
item = pitem_new(priority, &ordering[i]);
|
|
|
|
if (pqueue_insert(q, item) != item) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-08-30 03:41:39 +01:00
|
|
|
}
|
2014-11-04 07:16:34 +00:00
|
|
|
|
|
|
|
/* 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);
|
2014-08-30 03:41:39 +01:00
|
|
|
if (curr == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
pitem *next = pqueue_next(&iter);
|
2014-11-04 07:16:34 +00:00
|
|
|
int *curr_data, *next_data;
|
|
|
|
|
2014-08-30 03:41:39 +01:00
|
|
|
if (next == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
2014-11-04 07:16:34 +00:00
|
|
|
curr_data = (int*)curr->data;
|
|
|
|
next_data = (int*)next->data;
|
2014-08-30 03:41:39 +01:00
|
|
|
if (*curr_data >= *next_data) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
curr = next;
|
|
|
|
}
|
2015-02-11 19:16:28 +00:00
|
|
|
clear_and_free_queue(q);
|
2014-08-30 03:41:39 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2014-09-12 00:11:15 +01:00
|
|
|
SSL_library_init();
|
|
|
|
|
2014-08-30 03:41:39 +01:00
|
|
|
if (!trivial() || !fixed_random()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("PASS\n");
|
|
|
|
return 0;
|
|
|
|
}
|