You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/tree.c"
  19. #include <stdint.h>
  20. #include "libavutil/common.h"
  21. #include "libavutil/lfg.h"
  22. #include "libavutil/log.h"
  23. static int check(AVTreeNode *t)
  24. {
  25. if (t) {
  26. int left = check(t->child[0]);
  27. int right = check(t->child[1]);
  28. if (left > 999 || right > 999)
  29. return 1000;
  30. if (right - left != t->state)
  31. return 1000;
  32. if (t->state > 1 || t->state < -1)
  33. return 1000;
  34. return FFMAX(left, right) + 1;
  35. }
  36. return 0;
  37. }
  38. static void print(AVTreeNode *t, int depth)
  39. {
  40. int i;
  41. for (i = 0; i < depth * 4; i++)
  42. av_log(NULL, AV_LOG_ERROR, " ");
  43. if (t) {
  44. av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem);
  45. print(t->child[0], depth + 1);
  46. print(t->child[1], depth + 1);
  47. } else
  48. av_log(NULL, AV_LOG_ERROR, "NULL\n");
  49. }
  50. static int cmp(void *a, const void *b)
  51. {
  52. return (uint8_t *) a - (const uint8_t *) b;
  53. }
  54. int main(void)
  55. {
  56. int i;
  57. AVTreeNode *root = NULL, *node = NULL;
  58. AVLFG prng;
  59. av_lfg_init(&prng, 1);
  60. for (i = 0; i < 10000; i++) {
  61. AVTreeNode *node2 = NULL;
  62. intptr_t j = av_lfg_get(&prng) % 86294;
  63. void *ret, *jj = (void *)(j + 1);
  64. while (ret = av_tree_find(root, jj, cmp, NULL)) {
  65. j = av_lfg_get(&prng) % 86294;
  66. jj = (void *)(j + 1);
  67. }
  68. if (check(root) > 999) {
  69. av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
  70. print(root, 0);
  71. return 1;
  72. }
  73. if (!node)
  74. node = av_tree_node_alloc();
  75. if (!node) {
  76. av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n");
  77. return 1;
  78. }
  79. av_tree_insert(&root, jj, cmp, &node);
  80. while (ret = av_tree_find(root, jj, cmp, NULL)) {
  81. j = av_lfg_get(&prng) % 86294;
  82. jj = (void *)(j + 1);
  83. }
  84. ret = av_tree_insert(&root, jj, cmp, &node2);
  85. if (ret != jj)
  86. av_tree_destroy(node2);
  87. ret = av_tree_find(root, jj, cmp, NULL);
  88. if (ret)
  89. av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
  90. }
  91. av_tree_destroy(root);
  92. return 0;
  93. }