tree.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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(const void *a, const void *b)
  51. {
  52. return (const uint8_t *) a - (const uint8_t *) b;
  53. }
  54. int main(int argc, char **argv)
  55. {
  56. int i;
  57. void *k;
  58. AVTreeNode *root = NULL, *node = NULL;
  59. AVLFG prng;
  60. int log_level = argc <= 1 ? AV_LOG_INFO : atoi(argv[1]);
  61. av_log_set_level(log_level);
  62. av_lfg_init(&prng, 1);
  63. for (i = 0; i < 10000; i++) {
  64. intptr_t j = av_lfg_get(&prng) % 86294;
  65. if (check(root) > 999) {
  66. av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
  67. print(root, 0);
  68. return 1;
  69. }
  70. av_log(NULL, AV_LOG_DEBUG, "inserting %4d\n", (int)j);
  71. if (!node)
  72. node = av_tree_node_alloc();
  73. if (!node) {
  74. av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n");
  75. return 1;
  76. }
  77. av_tree_insert(&root, (void *)(j + 1), cmp, &node);
  78. j = av_lfg_get(&prng) % 86294;
  79. {
  80. AVTreeNode *node2 = NULL;
  81. av_log(NULL, AV_LOG_DEBUG, "removing %4d\n", (int)j);
  82. av_tree_insert(&root, (void *)(j + 1), cmp, &node2);
  83. k = av_tree_find(root, (void *)(j + 1), cmp, NULL);
  84. if (k)
  85. av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
  86. av_free(node2);
  87. }
  88. }
  89. av_free(node);
  90. av_tree_destroy(root);
  91. return 0;
  92. }