Cambridge SMT System
taskinterface.gtest.cpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
21 #include <googletesting.h>
22 
23 #ifndef GMAINTEST
24 #include "main.custom_assert.hpp"
25 #include "main.logger.hpp"
26 #endif
27 
28 #include "taskinterface.hpp"
29 
31 namespace googletesting {
32 
34 class DataTest1 {
35  public:
39 };
40 
42 template<class Data>
43 class Task1: public uu::TaskInterface<Data> {
44  public:
45  bool run ( Data& d ) {
46  d.modifiedbytask1 = true;
47  return false;
48  };
49 
50 };
51 
53 template<class Data>
54 class Task2: public uu::TaskInterface<Data> {
55  public:
56  bool run ( Data& d ) {
57  d.modifiedbytask2 = true;
58  return false;
59  };
60 };
61 
63 template<class Data>
64 class Task3: public uu::TaskInterface<Data> {
65  public:
66  bool run ( Data& d ) {
67  d.modifiedbytask3 = true;
68  return false;
69  };
70 };
71 
76 TEST ( TaskInterface, chainoftasks1 ) {
78  t1.appendTask ( new Task2<DataTest1> );
79  t1.appendTask ( new Task3<DataTest1> );
80  DataTest1 d;
82  t1.chainrun ( d );
83  EXPECT_EQ ( d.modifiedbytask1, true );
84  EXPECT_EQ ( d.modifiedbytask2, true );
85  EXPECT_EQ ( d.modifiedbytask3, true );
86 }
87 
89 template<class Data>
90 class Task2b: public uu::TaskInterface<Data> {
91  public:
92  bool run ( Data& d ) {
93  d.modifiedbytask2 = true;
94  return true;
95  };
96 };
97 
102 TEST ( TaskInterface, chainoftasks2 ) {
103  Task1<DataTest1> t1;
104  t1.appendTask ( new Task2b<DataTest1> );
105  t1.appendTask ( new Task3<DataTest1> );
106  DataTest1 d;
108  bool result = t1.chainrun ( d );
109  EXPECT_EQ ( result, true );
110  EXPECT_EQ ( d.modifiedbytask1, true );
111  EXPECT_EQ ( d.modifiedbytask2, true );
112  EXPECT_EQ ( d.modifiedbytask3, false );
113 }
114 
116 TEST ( TaskInterface, chainoftasks3 ) {
117  Task1<DataTest1> t1;
118  t1 ( new Task2b<DataTest1> )
119  ( new Task3<DataTest1> );
120  DataTest1 d;
122  bool result = t1.chainrun ( d );
123  EXPECT_EQ ( result, true );
124  EXPECT_EQ ( d.modifiedbytask1, true );
125  EXPECT_EQ ( d.modifiedbytask2, true );
126  EXPECT_EQ ( d.modifiedbytask3, false );
127 }
128 
130 
131 TEST ( TaskInterface, chainoftasks4 ) {
132  Task1<DataTest1> t1;
133  t1 ( NULL )
134  ( new Task3<DataTest1> );
135  DataTest1 d;
137  bool result = t1.chainrun ( d );
138  EXPECT_EQ ( result, false );
139  EXPECT_EQ ( d.modifiedbytask1, true );
140  EXPECT_EQ ( d.modifiedbytask2, false );
141  EXPECT_EQ ( d.modifiedbytask3, true );
142 }
143 
145 class DataTest2 {
146  public:
147  unsigned idx;
148  std::vector<unsigned> v;
149 };
150 
152 template<class Data>
153 class Task4: public uu::TaskInterface<Data> {
154  public:
155  bool run ( Data& d ) {
156  d.v.push_back ( d.idx );
157  return false;
158  };
159 
160 };
161 
163 TEST ( TaskInterface, idx ) {
164  Task4<DataTest2> t4;
165  DataTest2 d;
166  for ( unsigned k = 0; k < 5; k += 2 ) {
167  d.idx = k;
168  t4.run ( d );
169  }
170  ASSERT_EQ ( d.v.size(), 3 );
171  EXPECT_EQ ( d.v[0], 0 );
172  EXPECT_EQ ( d.v[1], 2 );
173  EXPECT_EQ ( d.v[2], 4 );
174 }
175 
182 template<class Data>
183 class PostEditTask: public uu::TaskInterface<Data> {
184  private:
185  uu::TaskInterface<Data> *fullsystem_;
186  public:
187  PostEditTask ( uu::TaskInterface<Data> *fs ) : fullsystem_ ( fs ) {};
188  bool run ( Data& d ) {
189  //Run first time on sentence x
190  fullsystem_->chainrun ( d );
191  // Do stuff...
192  //Run second time on sentence x. d now contains certain modifications that could change the behaviour and output of the fullsystem.
193  fullsystem_->chainrun ( d );
194  return false;
195  };
196  inline ~PostEditTask() {
197  if ( fullsystem_ ) delete fullsystem_;
198  };
199 
200 };
201 
203 TEST ( TaskInterface, idx_chainrun ) {
205  DataTest2 d;
206  for ( unsigned k = 0; k < 3; k += 2 ) {
207  d.idx = k;
208  t5.run ( d );
209  }
210  ASSERT_EQ ( d.v.size(), 4 );
211  EXPECT_EQ ( d.v[0], 0 );
212  EXPECT_EQ ( d.v[1], 0 );
213  EXPECT_EQ ( d.v[2], 2 );
214  EXPECT_EQ ( d.v[3], 2 );
215 }
216 
217 };
218 
219 #ifndef GMAINTEST
220 
226 int main ( int argc, char **argv ) {
227  ::testing::InitGoogleTest ( &argc, argv );
228  return RUN_ALL_TESTS();
229 };
230 
231 #endif
Trivial task class implementing run method to modify second bool variable.
This is a test to show how an imaginary postedit class could work. The class would contain a pointer ...
Trivial data class with three bool variables.
Trivial task class implementing run method to modify first bool variable.
std::vector< unsigned > v
PostEditTask(uu::TaskInterface< Data > *fs)
Interfaces with basic methods for iteration.
TEST(FstIo, basic_test)
Definition: fstio.gtest.cpp:38
bool chainrun(Data &d)
Implements chain of responsability. Calls run method and, if there is another task, call its run method too.
Templated (hybrid) Interface for Task classes.
Trivial task class, implements run to modify DataTest2.
TaskInterface & appendTask(TaskInterface *t)
Appends a task class. If there is no task, append here, otherwise delegate in next task...
test-specific classes and functions
Definition: fstio.gtest.cpp:34
Static variables for logger. Include only once from main file.
int main(int argc, char **argv)
main function. If compiled individualy, will kickoff any tests in this file.
Trivial task class implementing run method to modify third bool variable.
Trivial task class implementing run method to modify second bool variable. Additionally, signals that execution is to stop after finishing with Task2b.
Trivial data class, now contains unsigned and vector<unsigned>.
Unit testing: google testing common header.
Static variable for custom_assert. Include only once from main file.