1 /*=============================================================================
2     Copyright (c) 2002-2003 Martin Wille
3     http://spirit.sourceforge.net/
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 // vim:ts=4:sw=4:et
10 
11 #if defined(BOOST_BUILD_PCH_ENABLED) && defined(BOOST_SPIRIT_THREADSAFE)
12 # error BOOST_SPIRIT_THREADSAFE has to be undefined for this test
13 #endif
14 #undef BOOST_SPIRIT_THREADSAFE
15 #include <boost/spirit/home/classic/core/non_terminal/impl/object_with_id.ipp>
16 #include <boost/detail/lightweight_test.hpp>
17 #include <iostream>
18 
19 using BOOST_SPIRIT_CLASSIC_NS::impl::object_with_id;
20 
21 struct tag1 {};
22 struct tag2 {};
23 
24 typedef object_with_id<tag1> class1;
25 typedef object_with_id<tag2> class2;
26 
27 int
main()28 main()
29 {
30     std::cout << "/////////////////////////////////////////////////////////\n";
31     std::cout << "\n";
32     std::cout << "          object_with_id test (ST)\n";
33     std::cout << "\n";
34     std::cout << "/////////////////////////////////////////////////////////\n";
35     std::cout << "\n";
36 
37     class1 *c1o1 = new class1;
38     class1 *c1o2 = new class1;
39     class1 *c1o3 = new class1;
40 
41     // test wether the objects have consecutive numbers
42     BOOST_TEST(c1o1->get_object_id()==1);
43     BOOST_TEST(c1o2->get_object_id()==2);
44     BOOST_TEST(c1o3->get_object_id()==3);
45 
46     // test wether number recycling works
47     delete c1o3;
48     c1o3 = new class1;
49     BOOST_TEST(c1o3->get_object_id()==3);
50 
51     delete c1o2;
52     c1o2 = new class1;
53     BOOST_TEST(c1o2->get_object_id()==2);
54 
55     delete c1o2;
56     delete c1o3;
57     c1o2 = new class1;
58     c1o3 = new class1;
59     BOOST_TEST(c1o3->get_object_id()==3);
60     BOOST_TEST(c1o2->get_object_id()==2);
61 
62     // test whether objects of different classes are numbered independently
63     class2 *c2o1 = new class2;
64     class2 *c2o2 = new class2;
65     class2 *c2o3 = new class2;
66     BOOST_TEST(c2o1->get_object_id()==1);
67     BOOST_TEST(c2o2->get_object_id()==2);
68     BOOST_TEST(c2o3->get_object_id()==3);
69 
70     //
71     delete c1o1;
72     delete c2o2;
73     c2o2 = new class2;
74     c1o1 = new class1;
75     BOOST_TEST(c1o1->get_object_id()==1);
76     BOOST_TEST(c2o2->get_object_id()==2);
77 
78     // test wether the copy ctor doesn't copy the id
79     delete c1o1;
80     c1o1 = new class1(*c1o2);
81     BOOST_TEST(c1o1->get_object_id()==1);
82 
83     // test wether the assignment operator doesn't assign the id
84     *c1o1 = *c1o2;
85     BOOST_TEST(c1o1->get_object_id()==1);
86 
87     return boost::report_errors();
88 }
89