constructor_test.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // constructor_test.cc -- a test case for gold global constructors
  2. // Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program 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
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. // This file just runs some global constructors and destructors. The
  18. // last global destructor verifies that the state is as expected, and
  19. // we assume that it runs correctly itself.
  20. #include <cassert>
  21. #include <cstdlib>
  22. // These counters let us verify the state.
  23. int c1_count;
  24. int c2_count;
  25. int atexit_count;
  26. // This class verifies that there are no objects left when it is
  27. // destroyed. Therefore, we can only have one instance of this
  28. // object.
  29. class c1
  30. {
  31. public:
  32. static int count;
  33. c1()
  34. { ++c1_count; }
  35. ~c1()
  36. {
  37. --c1_count;
  38. assert(c1_count == 0 && c2_count == 0 && atexit_count == 0);
  39. }
  40. };
  41. c1 c1v;
  42. // A function called at atexit time.
  43. void
  44. atexit_function()
  45. {
  46. --atexit_count;
  47. assert(atexit_count == c2_count);
  48. }
  49. // A class which counts itself and also calls atexit.
  50. class c2
  51. {
  52. public:
  53. c2()
  54. {
  55. assert(atexit_count == c2_count);
  56. ++c2_count;
  57. atexit(atexit_function);
  58. ++atexit_count;
  59. }
  60. ~c2()
  61. { --c2_count; }
  62. };
  63. c2 c2v1;
  64. c2 c2v2;
  65. int
  66. main()
  67. {
  68. return 0;
  69. }