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.

184 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. #include <stdio.h>
  12. #include "nt.h"
  13. #include "ntrtl.h"
  14. ULONG RtlRandom ( IN OUT PULONG Seed );
  15. typedef struct _TREE_NODE {
  16. CLONG Data;
  17. RTL_SPLAY_LINKS Links;
  18. } TREE_NODE;
  19. typedef TREE_NODE *PTREE_NODE;
  20. TREE_NODE Buffer[2048];
  21. PTREE_NODE
  22. TreeInsert (
  23. IN PTREE_NODE Root,
  24. IN PTREE_NODE Node
  25. );
  26. VOID
  27. PrintTree (
  28. IN PTREE_NODE Node
  29. );
  30. int
  31. _CDECL
  32. main(
  33. int argc,
  34. char *argv[]
  35. )
  36. {
  37. PTREE_NODE Root;
  38. ULONG i;
  39. ULONG Seed;
  40. DbgPrint("Start SplayTest()\n");
  41. Root = NULL;
  42. Seed = 0;
  43. for (i=0; i<2048; i++) {
  44. Buffer[i].Data = RtlRandom(&Seed);
  45. Buffer[i].Data = Buffer[i].Data % 512;
  46. RtlInitializeSplayLinks(&Buffer[i].Links);
  47. Root = TreeInsert(Root, &Buffer[i]);
  48. }
  49. PrintTree(Root);
  50. DbgPrint("End SplayTest()\n");
  51. return TRUE;
  52. }
  53. PTREE_NODE
  54. TreeInsert (
  55. IN PTREE_NODE Root,
  56. IN PTREE_NODE Node
  57. )
  58. {
  59. PRTL_SPLAY_LINKS Temp;
  60. if (Root == NULL) {
  61. //DbgPrint("Add as root %u\n", Node->Data);
  62. return Node;
  63. }
  64. while (TRUE) {
  65. if (Root->Data == Node->Data) {
  66. //DbgPrint("Delete %u\n", Node->Data);
  67. Temp = RtlDelete(&Root->Links);
  68. if (Temp == NULL) {
  69. return NULL;
  70. } else {
  71. return CONTAINING_RECORD(Temp, TREE_NODE, Links);
  72. }
  73. }
  74. if (Root->Data < Node->Data) {
  75. //
  76. // Go right
  77. //
  78. if (RtlRightChild(&Root->Links) == NULL) {
  79. //DbgPrint("Add as right child %u\n", Node->Data);
  80. RtlInsertAsRightChild(&Root->Links, &Node->Links);
  81. return CONTAINING_RECORD(RtlSplay(&Node->Links), TREE_NODE, Links);
  82. } else {
  83. Root = CONTAINING_RECORD(RtlRightChild(&Root->Links), TREE_NODE, Links);
  84. }
  85. } else {
  86. //
  87. // Go Left
  88. //
  89. if (RtlLeftChild(&Root->Links) == NULL) {
  90. //DbgPrint("Add as left child %u\n", Node->Data);
  91. RtlInsertAsLeftChild(&Root->Links, &Node->Links);
  92. return CONTAINING_RECORD(RtlSplay(&Node->Links), TREE_NODE, Links);
  93. } else {
  94. Root = CONTAINING_RECORD(RtlLeftChild(&Root->Links), TREE_NODE, Links);
  95. }
  96. }
  97. }
  98. }
  99. VOID
  100. PrintTree (
  101. IN PTREE_NODE Node
  102. )
  103. {
  104. PRTL_SPLAY_LINKS Temp;
  105. ULONG LastValue;
  106. if (Node == NULL) {
  107. return;
  108. }
  109. //
  110. // find smallest value
  111. //
  112. while (RtlLeftChild(&Node->Links) != NULL) {
  113. Node = CONTAINING_RECORD(RtlLeftChild(&Node->Links), TREE_NODE, Links);
  114. }
  115. LastValue = Node->Data;
  116. //DbgPrint("%u\n", Node->Data);
  117. //
  118. // while the is a real successor we print the successor value
  119. //
  120. while ((Temp = RtlRealSuccessor(&Node->Links)) != NULL) {
  121. Node = CONTAINING_RECORD(Temp, TREE_NODE, Links);
  122. if (LastValue >= Node->Data) {
  123. DbgPrint("TestSplay Error\n");
  124. }
  125. LastValue = Node->Data;
  126. //DbgPrint("%u\n", Node->Data);
  127. }
  128. }