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

130 строки
3.0 KiB

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