Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/statechart/test/InStateReactionTest.cpp @ 45

Last change on this file since 45 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.0 KB
Line 
1//////////////////////////////////////////////////////////////////////////////
2// Copyright 2005-2006 Andreas Huber Doenni
3// Distributed under the Boost Software License, Version 1.0. (See accompany-
4// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5//////////////////////////////////////////////////////////////////////////////
6
7
8
9#include <boost/statechart/state_machine.hpp>
10#include <boost/statechart/event.hpp>
11#include <boost/statechart/simple_state.hpp>
12#include <boost/statechart/in_state_reaction.hpp>
13#include <boost/statechart/result.hpp>
14
15#include <boost/mpl/list.hpp>
16
17#include <boost/test/test_tools.hpp>
18
19namespace sc = boost::statechart;
20namespace mpl = boost::mpl;
21
22
23struct E : sc::event< E > {};
24struct F : sc::event< F > {};
25struct G : sc::event< G > {};
26struct H : sc::event< H > {};
27
28struct A;
29struct InStateReactionTest :
30  sc::state_machine< InStateReactionTest, A > {};
31
32struct B;
33struct A : sc::simple_state< A, InStateReactionTest, B >
34{
35  A() : eventCount_( 0 ) {}
36
37  // The following 3 functions could be implemented with one function
38  // template, but this causes problems with CW and Intel 9.1.
39  void IncrementCount( const sc::event_base & ) { ++eventCount_; }
40  void IncrementCount( const E & ) { ++eventCount_; }
41  void IncrementCount( const G & ) { ++eventCount_; }
42
43  typedef mpl::list<
44    sc::in_state_reaction< E, A, &A::IncrementCount >,
45    sc::in_state_reaction< sc::event_base, A, &A::IncrementCount >
46  > reactions;
47
48  unsigned int eventCount_;
49};
50
51  struct B : sc::simple_state< B, A >
52  {
53    B() : eventCount_( 0 ) {}
54
55    void IncrementCount( const F & )
56    {
57      ++eventCount_;
58    }
59
60    typedef mpl::list<
61      sc::in_state_reaction< F, B, &B::IncrementCount >,
62      sc::in_state_reaction< G, A, &A::IncrementCount >
63    > reactions;
64
65    unsigned int eventCount_;
66  };
67
68
69
70int test_main( int, char* [] )
71{
72  InStateReactionTest machine;
73  machine.initiate();
74
75  BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 0 );
76  BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 0 );
77  machine.process_event( F() );
78  BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 0 );
79  BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 1 );
80  machine.process_event( E() );
81  BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 1 );
82  BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 1 );
83  machine.process_event( E() );
84  machine.process_event( F() );
85  BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 2 );
86  BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 2 );
87  machine.process_event( G() );
88  BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 3 );
89  BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 2 );
90  machine.process_event( H() );
91  BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 4 );
92  BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 2 );
93
94  return 0;
95}
Note: See TracBrowser for help on using the repository browser.