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.

208 lines
3.3 KiB

  1. /*
  2. * COMMAND LINE: -Ox -GX
  3. */
  4. /*
  5. Copyright (c) 1997 Microsoft Corporation
  6. Module Name:
  7. IhateEH.cxx
  8. Abstract:
  9. Tests some nasty EH unwinds, throwing from weird ctors and dtors
  10. Author:
  11. Louis Lafreniere (louisl) 1997
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #define FALSE 0
  16. #define TRUE 1
  17. #define NO_CTOR_THROW 1
  18. #define NO_DTOR_THROW 2
  19. int Object[100];
  20. int CurrentObjectNumber, ThrowCount, MaxObjectCount = 1;
  21. int Fail;
  22. void FAIL(int i)
  23. {
  24. printf("FAILED on %d\n", i);
  25. Fail++;
  26. }
  27. void dealloc(int i, int no_throw)
  28. {
  29. /* Make sure i is valid, and object exists */
  30. if(i<0 || i>=MaxObjectCount || !Object[i])
  31. FAIL(i);
  32. Object[i] = 0;
  33. /* Try throw in this dtor.. */
  34. #ifdef TEST_DTOR
  35. if(i+MaxObjectCount == ThrowCount && !no_throw){
  36. printf("Throwing\n");
  37. throw(1);
  38. }
  39. #endif
  40. }
  41. void alloc(int i, int no_throw)
  42. {
  43. if(CurrentObjectNumber > MaxObjectCount)
  44. MaxObjectCount = CurrentObjectNumber;
  45. /* Object already exists? */
  46. if(Object[i]) FAIL(i);
  47. /* Try throw in this ctor.. */
  48. if(i == ThrowCount && !no_throw){
  49. printf("Throwing\n");
  50. throw(1);
  51. }
  52. Object[i] = 1;
  53. }
  54. class B
  55. {
  56. public:
  57. int i;
  58. int flag;
  59. B();
  60. B(int);
  61. ~B();
  62. };
  63. B::B()
  64. {
  65. i = CurrentObjectNumber++;
  66. printf("B ctor. i = %d\n", i);
  67. alloc(i, FALSE);
  68. }
  69. B::B(int f)
  70. {
  71. i = CurrentObjectNumber++;
  72. flag = f;
  73. printf("B ctor. i = %d\n", i);
  74. alloc(i, flag==NO_CTOR_THROW);
  75. }
  76. B::~B()
  77. {
  78. printf("B dtor. i = %d\n", i);
  79. dealloc(i, flag==NO_DTOR_THROW);
  80. }
  81. class A
  82. {
  83. public:
  84. int i;
  85. A();
  86. A(int)
  87. {
  88. i = CurrentObjectNumber++;
  89. printf("A(int) ctor. i = %d\n", i);
  90. alloc(i, FALSE);
  91. }
  92. A operator+(A a);
  93. A(const A &a)
  94. {
  95. /* Try objects in ctor */
  96. B b1 = NO_DTOR_THROW, b2 = NO_DTOR_THROW;
  97. i = CurrentObjectNumber++;
  98. printf("A copy ctor. i = %d\n", i);
  99. alloc(i, FALSE);
  100. }
  101. ~A(){
  102. /* Try objects in dtor */
  103. B b1 = NO_CTOR_THROW, b2 = NO_CTOR_THROW;
  104. printf("A dtor. i = %d\n", i);
  105. dealloc(i, FALSE);
  106. };
  107. };
  108. A::A()
  109. {
  110. i=CurrentObjectNumber++;
  111. printf("A ctor. i = %d\n", i);
  112. alloc(i, FALSE);
  113. }
  114. A A::operator+(A a)
  115. {
  116. printf("A%d + A%d\n", i, a.i);
  117. return A();
  118. }
  119. A foo(A a1, A a2)
  120. {
  121. return a1+a2;
  122. };
  123. A test()
  124. {
  125. /* Try simple ctor */
  126. A a1;
  127. /* Try question op ctor */
  128. A a2 = (ThrowCount & 1) ? A() : ThrowCount;
  129. /* try mbarg copy ctors, and return UDT */
  130. A a3 = foo(a1, a3);
  131. /* Try temporary expressions, and return UDT */
  132. return a1 + A() + a2 + a3;
  133. }
  134. void main()
  135. {
  136. int i;
  137. /* Call test(), with a different ctor/dtor throwing each time */
  138. for(ThrowCount = 0; ThrowCount < MaxObjectCount*2; ThrowCount++) {
  139. printf("ThrowCount = %d MaxObjectCount = %d\n", ThrowCount, MaxObjectCount);
  140. CurrentObjectNumber = 0;
  141. try {
  142. test();
  143. }catch(int){
  144. printf("In catch\n");
  145. }
  146. /* Any objects which didn't get dtor'd? */
  147. for(i = 0; i < MaxObjectCount; i++) {
  148. if(Object[i]) {
  149. FAIL(i);
  150. Object[i] = 0;
  151. }
  152. }
  153. printf("\n");
  154. }
  155. printf("\n");
  156. if(Fail)
  157. printf("FAILED %d tests\n", Fail);
  158. else
  159. printf("Passed\n");
  160. }