OSDN Git Service

first commit.
[gintenlib/gintenlib.git] / tests / value_saver.cc
1 #include "../gintenlib/value_saver.hpp"
2 #include "../gintenlib/typed_saver.hpp"
3
4 // boost ¤ÎñÂΥƥ¹¥È¥Õ¥ì¡¼¥à¥ï¡¼¥¯
5 #include <boost/test/minimal.hpp>
6
7 // ¥Æ¥¹¥ÈËÜÂΤϥƥó¥×¥ì¡¼¥È¤Çµ­½Ò
8 template<typename T, typename Saver>
9 void test( const T& x0, const T& x1 )
10 {
11   T x = x0;
12   
13   {
14     // ¤È¤ê¤¢¤¨¤º x ¤ÎÃͤò¥Á¥§¥Ã¥¯
15     BOOST_CHECK( x == x0 );
16     
17     // Êݸ
18     Saver saver(x);
19     // saver ¤ËÆͤùþ¤ó¤À»þÅÀ¤Ç¤ÏÃͤÏÊѤï¤é¤Ê¤¤
20     BOOST_CHECK( x == x0 );
21     
22     // ÃͤòÊѹ¹
23     x = x1;
24     BOOST_CHECK( x == x1 );
25     
26     // ¤³¤³¤Ç¸µ¤ËÌá¤ë¤Ï¤º
27   }
28   BOOST_CHECK( x == x0 );
29   
30   {
31     // ¤Þ¤¿Êݸ
32     Saver saver(x);
33     // ÃͤòÊѹ¹
34     x = x1;
35     BOOST_CHECK( x == x1 );
36     
37     // ÌÀ¼¨Åª¤ÊÃͤÎÉü¸µ
38     saver.restore();
39     // Éü¸µ¤Ç¤­¤¿¤«¥Á¥§¥Ã¥¯
40     BOOST_CHECK( x == x0 );
41     
42     // restore() ¸å¤Ï¥Ç¥¹¥È¥é¥¯¥¿¤ÇÉü¸µ¤·¤Ê¤¤¡Ê¤¤¤¤¤«°­¤¤¤«¤ÏÊ̤Ȥ·¤Æ¡Ë
43     x = x1;
44   }
45   BOOST_CHECK( x == x1 );
46   
47   x = x0;
48   {
49     // Êݸ¤ÈƱ»þ¤ËÃͤò½ñ¤­´¹¤¨¤ë¡¢¤È¤¤¤¦¤Î¤Ï³ä¤È¤è¤¯¤¢¤ëÆ°ºî¤Ê¤Î¤Ç
50     // ³Ú¤Ç¤­¤ë¤è¤¦¤ËÀìÍÑ¥³¥ó¥¹¥È¥é¥¯¥¿¤òÍÑ°Õ¤·¤Æ¤¢¤ê¤Þ¤¹
51     Saver saver( x, x1 );
52     BOOST_CHECK( x == x1 );
53     
54     // ¤­¤Á¤ó¤ÈÉü¸µ¤Ç¤­¤ë¡©
55   }
56   BOOST_CHECK( x == x0 );
57   
58   {
59     // Êݸ¤ÈƱ»þ¤ËÃͤò½ñ¤­´¹¤¨¤ë¡¢¤È¤¤¤¦¤Î¤Ï³ä¤È¤è¤¯¤¢¤ëÆ°ºî¤Ê¤Î¤Ç
60     // ³Ú¤Ç¤­¤ë¤è¤¦¤ËÀìÍÑ¥³¥ó¥¹¥È¥é¥¯¥¿¤òÍÑ°Õ¤·¤Æ¤¢¤ê¤Þ¤¹
61     Saver saver( x, x1 );
62     BOOST_CHECK( x == x1 );
63     
64     // release() ¤ò¸Æ¤Ö¤È
65     saver.release();
66     BOOST_CHECK( x == x1 );
67   }
68   // Éü¸µ¤µ¤ì¤Ê¤¤
69   BOOST_CHECK( x == x1 );
70 }
71
72 template<typename T>
73 void test_saver( const T& x0, const T& x1 )
74 {
75   // ·¿ÉÕ¤ÈÈÆÍÑ¡¢Î¾Êý¥Æ¥¹¥È
76   test< T, gintenlib::typed_saver<T> >( x0, x1 );
77   test< T, gintenlib::value_saver    >( x0, x1 );
78 }
79
80 #include <complex>
81 #include <boost/shared_ptr.hpp>
82
83 int test_main( int argc, char* argv[] )
84 {
85   // ÁȤ߹þ¤ß·¿
86   test_saver<int>( 0, 1 );
87   
88   // ¥æ¡¼¥¶ÄêµÁ·¿¡Ê e.g. std::complex ¡Ë
89   std::complex<double> j( 0, 1 );
90   test_saver< std::complex<double> >( 1.0, j );
91   
92   // ¥æ¡¼¥¶ÄêµÁ·¿¤½¤Î¤Ë¡¢boost::shared_ptr
93   boost::shared_ptr<int> p0, p1( new int(0) );
94   test_saver< boost::shared_ptr<int> >( p0, p1 );
95   
96   return 0;
97 }