Cambridge SMT System
range.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 "range.hpp"
29 
31 TEST ( range, sequence_of_integers1 ) {
32  std::vector<unsigned> x;
33  uu::getRange ( "1:3", x );
34  ASSERT_EQ ( x.size(), 3 );
35  EXPECT_EQ ( x[0], 1 );
36  EXPECT_EQ ( x[1], 2 );
37  EXPECT_EQ ( x[2], 3 );
38 }
39 
41 TEST ( range, sequence_of_integers2 ) {
42  std::vector<unsigned> x;
43  uu::getRange ( "1:30:50", x );
44  ASSERT_EQ ( x.size(), 2 );
45  EXPECT_EQ ( x[0], 1 );
46  EXPECT_EQ ( x[1], 31 );
47 }
48 
50 TEST ( range, sequence_of_integers3 ) {
51  std::vector<unsigned> x;
52  uu::getRange ( "5,10,15:30:50,52", x );
53  ASSERT_EQ ( x.size(), 5 );
54  EXPECT_EQ ( x[0], 5 );
55  EXPECT_EQ ( x[1], 10 );
56  EXPECT_EQ ( x[2], 15 );
57  EXPECT_EQ ( x[3], 45 );
58  EXPECT_EQ ( x[4], 52 );
59 }
60 
62 TEST ( range, sequence_of_floats ) {
63  std::vector<float> x;
64  uu::getRange ( "-1.0:0.5:1.0", x );
65  ASSERT_EQ ( x.size(), 5 );
66  EXPECT_EQ ( x[0], -1.0f );
67  EXPECT_EQ ( x[1], -0.5f );
68  EXPECT_EQ ( x[2], 0.0f );
69  EXPECT_EQ ( x[3], 0.5f );
70  EXPECT_EQ ( x[4], 1.0f );
71 }
72 
74 TEST (range, floatrange) {
75  std::vector<float> x;
76  std::vector <float> result;
77  uu::getRange ("5.3,10.2", x);
79  for ( ir.start(); !ir.done(); ir.next() ) {
80  result.push_back ( ir.get() );
81  }
82  ASSERT_EQ ( result.size(), 2 );
83  EXPECT_EQ ( result[0], 5.3f );
84  EXPECT_EQ ( result[1], 10.2f );
85 }
86 
88 TEST ( range, integerrange ) {
89  std::vector<unsigned> x;
90  std::vector<unsigned> result;
91  uu::getRange ( "5,10", x );
93  for ( ir.start(); !ir.done(); ir.next() ) {
94  result.push_back ( ir.get() );
95  }
96  ASSERT_EQ ( result.size(), 2 );
97  EXPECT_EQ ( result[0], 5 );
98  EXPECT_EQ ( result[1], 10 );
99 }
100 
102 TEST ( range, infiniterange ) {
104  EXPECT_EQ ( x.done(), false );
105  EXPECT_EQ ( x.get(), 5 );
106  x.next();
107  EXPECT_EQ ( x.get(), 6 );
108  x.next();
109  EXPECT_EQ ( x.get(), 7 );
110 }
111 
113 TEST ( range, onerange ) {
115  EXPECT_EQ ( x.done(), false );
116  x.start();
117  EXPECT_EQ ( x.done(), false );
118  EXPECT_EQ ( x.get(), 5 );
119  x.next();
120  EXPECT_EQ ( x.get(), 5 );
121  EXPECT_EQ ( x.done(), true );
122  x.next();
123  EXPECT_EQ ( x.get(), 5 );
124  EXPECT_EQ ( x.done(), true );
125  x.start();
126  EXPECT_EQ ( x.done(), false );
127 }
128 
129 #ifndef GMAINTEST
130 
135 int main ( int argc, char **argv ) {
136  ::testing::InitGoogleTest ( &argc, argv );
137  return RUN_ALL_TESTS();
138 }
139 #endif
void next(void)
Empty implementation.
Definition: range.hpp:150
NumberType get(void)
Always returns sid_.
Definition: range.hpp:191
Implements a Range iterator that will never finish.
Definition: range.hpp:173
NumberType get(void)
Returns range value at position k_.
Definition: range.hpp:119
void start(void)
Empty implementation.
Definition: range.hpp:146
void start(void)
Empty implementation.
Definition: range.hpp:107
void next(void)
Increment index.
Definition: range.hpp:111
void next(void)
Empty implementation.
Definition: range.hpp:182
Implements a Range iterator that only runs once. This is useful e.g. for fsttools that process a batc...
Definition: range.hpp:138
Static variables for logger. Include only once from main file.
bool done(void)
return true.
Definition: range.hpp:155
bool done(void)
Checks if reached the last element.
Definition: range.hpp:115
int main(int argc, char **argv)
main function. If compiled individualy, will kickoff any tests in this file.
NumberType get(void)
Always returns sid_.
Definition: range.hpp:159
bool done(void)
Always return false.
Definition: range.hpp:187
Handles different type of integer ranges.
TEST(range, sequence_of_integers1)
Test getRange with 1:3.
Definition: range.gtest.cpp:31
void getRange(const std::string &range, std::vector< NumberType > &x)
Generates ranges from a compact string parameter such as 1,3:5,10.
Definition: range.hpp:40
Unit testing: google testing common header.
Static variable for custom_assert. Include only once from main file.