test_ios_prop.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #include "test_locale.hpp"
  9. #include "../src/shared/ios_prop.hpp"
  10. #include <sstream>
  11. #include <locale>
  12. int counter=0;
  13. int imbued=0;
  14. struct propery {
  15. propery(int xx=-1) : x(xx) { counter ++; }
  16. propery(propery const &other) { counter++; x=other.x; }
  17. propery const &operator=(propery const &other) {
  18. x=other.x;
  19. return *this;
  20. };
  21. int x;
  22. void on_imbue() {imbued++; }
  23. ~propery() { counter--; }
  24. };
  25. typedef boost::locale::impl::ios_prop<propery> prop_type;
  26. struct init {
  27. init() { prop_type::global_init(); }
  28. };
  29. int main()
  30. {
  31. try {
  32. {
  33. std::stringstream ss;
  34. TEST(!prop_type::has(ss));
  35. TEST(prop_type::get(ss).x==-1);
  36. TEST(prop_type::has(ss));
  37. TEST(counter==1);
  38. }
  39. TEST(counter==0);
  40. {
  41. std::stringstream ss;
  42. prop_type::set(propery(1),ss);
  43. TEST(counter==1);
  44. TEST(prop_type::get(ss).x==1);
  45. }
  46. TEST(counter==0);
  47. {
  48. std::stringstream ss;
  49. prop_type::set(propery(1),ss);
  50. TEST(counter==1);
  51. TEST(prop_type::get(ss).x==1);
  52. }
  53. TEST(counter==0);
  54. {
  55. std::stringstream ss,ss2;
  56. prop_type::set(propery(2),ss);
  57. ss2.copyfmt(ss);
  58. TEST(prop_type::get(ss).x==2);
  59. TEST(prop_type::has(ss2));
  60. TEST(prop_type::has(ss));
  61. TEST(prop_type::get(ss2).x==2);
  62. prop_type::get(ss2).x=3;
  63. TEST(prop_type::get(ss2).x==3);
  64. TEST(prop_type::get(ss).x==2);
  65. TEST(counter==2);
  66. TEST(imbued==0);
  67. ss2.imbue(std::locale::classic());
  68. TEST(imbued==1);
  69. }
  70. TEST(counter==0);
  71. }catch(std::exception const &e) {
  72. std::cerr << "Fail:" << e.what() << std::endl;
  73. return EXIT_FAILURE;
  74. }
  75. FINALIZE();
  76. return 0;
  77. }
  78. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  79. // boostinspect:noascii