Leaked source code of windows server 2003
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.

187 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. tsplay.c
  5. Abstract:
  6. Test program for the Splay Procedures
  7. Author:
  8. Gary Kimura [GaryKi] 24-May-1989
  9. Revision History:
  10. --*/
  11. #define DbgPrint DbgPrint
  12. #include <stdio.h>
  13. #include "nt.h"
  14. #include "ntrtl.h"
  15. #include "triangle.h"
  16. ULONG RtlRandom (IN OUT PULONG Seed);
  17. typedef struct _TREE_NODE {
  18. CLONG Data;
  19. TRI_SPLAY_LINKS Links;
  20. } TREE_NODE;
  21. typedef TREE_NODE *PTREE_NODE;
  22. TREE_NODE Buffer[2048];
  23. PTREE_NODE
  24. TreeInsert (
  25. IN PTREE_NODE Root,
  26. IN PTREE_NODE Node
  27. );
  28. VOID
  29. PrintTree (
  30. IN PTREE_NODE Node
  31. );
  32. int
  33. _CDECL
  34. main(
  35. int argc,
  36. char *argv[]
  37. )
  38. {
  39. PTREE_NODE Root;
  40. ULONG i;
  41. ULONG Seed;
  42. DbgPrint("Start TriangleTest()\n");
  43. Root = NULL;
  44. Seed = 0;
  45. for (i=0; i<2048; i++) {
  46. Buffer[i].Data = RtlRandom(&Seed);
  47. Buffer[i].Data = Buffer[i].Data % 512;
  48. TriInitializeSplayLinks(&Buffer[i].Links);
  49. Root = TreeInsert(Root, &Buffer[i]);
  50. }
  51. PrintTree(Root);
  52. DbgPrint("End TriangleTest()\n");
  53. return TRUE;
  54. }
  55. PTREE_NODE
  56. TreeInsert (
  57. IN PTREE_NODE Root,
  58. IN PTREE_NODE Node
  59. )
  60. {
  61. PTRI_SPLAY_LINKS Temp;
  62. if (Root == NULL) {
  63. //DbgPrint("Add as root %u\n", Node->Data);
  64. return Node;
  65. }
  66. while (TRUE) {
  67. if (Root->Data == Node->Data) {
  68. //DbgPrint("Delete %u\n", Node->Data);
  69. Temp = TriDelete(&Root->Links);
  70. if (Temp == NULL) {
  71. return NULL;
  72. } else {
  73. return CONTAINING_RECORD(Temp, TREE_NODE, Links);
  74. }
  75. }
  76. if (Root->Data < Node->Data) {
  77. //
  78. // Go right
  79. //
  80. if (TriRightChild(&Root->Links) == NULL) {
  81. //DbgPrint("Add as right child %u\n", Node->Data);
  82. TriInsertAsRightChild(&Root->Links, &Node->Links);
  83. return CONTAINING_RECORD(TriSplay(&Node->Links), TREE_NODE, Links);
  84. } else {
  85. Root = CONTAINING_RECORD(TriRightChild(&Root->Links), TREE_NODE, Links);
  86. }
  87. } else {
  88. //
  89. // Go Left
  90. //
  91. if (TriLeftChild(&Root->Links) == NULL) {
  92. //DbgPrint("Add as left child %u\n", Node->Data);
  93. TriInsertAsLeftChild(&Root->Links, &Node->Links);
  94. return CONTAINING_RECORD(TriSplay(&Node->Links), TREE_NODE, Links);
  95. } else {
  96. Root = CONTAINING_RECORD(TriLeftChild(&Root->Links), TREE_NODE, Links);
  97. }
  98. }
  99. }
  100. }
  101. VOID
  102. PrintTree (
  103. IN PTREE_NODE Node
  104. )
  105. {
  106. PTRI_SPLAY_LINKS Temp;
  107. ULONG LastValue;
  108. if (Node == NULL) {
  109. return;
  110. }
  111. //
  112. // find smallest value
  113. //
  114. while (TriLeftChild(&Node->Links) != NULL) {
  115. Node = CONTAINING_RECORD(TriLeftChild(&Node->Links), TREE_NODE, Links);
  116. }
  117. LastValue = Node->Data;
  118. //DbgPrint("%u\n", Node->Data);
  119. //
  120. // while the is a real successor we print the successor value
  121. //
  122. while ((Temp = TriRealSuccessor(&Node->Links)) != NULL) {
  123. Node = CONTAINING_RECORD(Temp, TREE_NODE, Links);
  124. if (LastValue >= Node->Data) {
  125. DbgPrint("TestSplay Error\n");
  126. }
  127. LastValue = Node->Data;
  128. //DbgPrint("%u\n", Node->Data);
  129. }
  130. }