Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <stdio.h>
  15. #include <string.h>
  16. #include <openssl/pqueue.h>
  17. #include <openssl/ssl.h>
  18. static void clear_and_free_queue(pqueue q) {
  19. for (;;) {
  20. pitem *item = pqueue_pop(q);
  21. if (item == NULL) {
  22. break;
  23. }
  24. pitem_free(item);
  25. }
  26. pqueue_free(q);
  27. }
  28. static int trivial(void) {
  29. pqueue q = pqueue_new();
  30. if (q == NULL) {
  31. return 0;
  32. }
  33. int32_t data = 0xdeadbeef;
  34. uint8_t priority[8] = {0};
  35. pitem *item = pitem_new(priority, &data);
  36. if (item == NULL ||
  37. pqueue_insert(q, item) != item ||
  38. pqueue_size(q) != 1 ||
  39. pqueue_peek(q) != item ||
  40. pqueue_pop(q) != item ||
  41. pqueue_size(q) != 0 ||
  42. pqueue_pop(q) != NULL) {
  43. return 0;
  44. }
  45. pitem_free(item);
  46. clear_and_free_queue(q);
  47. return 1;
  48. }
  49. #define NUM_ITEMS 10
  50. static int fixed_random(void) {
  51. /* Random order of 10 elements, chosen by
  52. * random.choice(list(itertools.permutations(range(10)))) */
  53. int ordering[NUM_ITEMS] = {9, 6, 3, 4, 0, 2, 7, 1, 8, 5};
  54. int i;
  55. pqueue q = pqueue_new();
  56. uint8_t priority[8] = {0};
  57. piterator iter;
  58. pitem *curr, *item;
  59. if (q == NULL) {
  60. return 0;
  61. }
  62. /* Insert the elements */
  63. for (i = 0; i < NUM_ITEMS; i++) {
  64. priority[7] = ordering[i];
  65. item = pitem_new(priority, &ordering[i]);
  66. if (item == NULL || pqueue_insert(q, item) != item) {
  67. return 0;
  68. }
  69. }
  70. /* Insert the elements again. This inserts duplicates and should
  71. * fail. */
  72. for (i = 0; i < NUM_ITEMS; i++) {
  73. priority[7] = ordering[i];
  74. item = pitem_new(priority, &ordering[i]);
  75. if (item == NULL || pqueue_insert(q, item) != NULL) {
  76. return 0;
  77. }
  78. pitem_free(item);
  79. }
  80. if (pqueue_size(q) != NUM_ITEMS) {
  81. return 0;
  82. }
  83. /* Iterate over the elements. */
  84. iter = pqueue_iterator(q);
  85. curr = pqueue_next(&iter);
  86. if (curr == NULL) {
  87. return 0;
  88. }
  89. while (1) {
  90. pitem *next = pqueue_next(&iter);
  91. int *curr_data, *next_data;
  92. if (next == NULL) {
  93. break;
  94. }
  95. curr_data = (int*)curr->data;
  96. next_data = (int*)next->data;
  97. if (*curr_data >= *next_data) {
  98. return 0;
  99. }
  100. curr = next;
  101. }
  102. clear_and_free_queue(q);
  103. return 1;
  104. }
  105. int main(void) {
  106. SSL_library_init();
  107. if (!trivial() || !fixed_random()) {
  108. return 1;
  109. }
  110. printf("PASS\n");
  111. return 0;
  112. }