Cambridge SMT System
globalfunctions.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 
22 #include <googletesting.h>
23 
24 #ifndef GMAINTEST
25 #include "main.custom_assert.hpp"
26 #include "main.logger.hpp"
27 #endif
28 
29 #include "params.hpp"
30 
32 TEST ( numberconversions, int2string ) {
33  std::string y = ucam::util::toString<unsigned> ( 3 );
34  EXPECT_EQ ( y, "3" );
35 }
36 
38 TEST ( numberconversions, string2int ) {
40  unsigned t = toNumber<unsigned> ( "35" );
41  EXPECT_EQ ( t, 35 );
42  user_check_ok = true;
43  t = toNumber<unsigned> ( "35ab" );
44  EXPECT_EQ ( t, 35 ); //Partially good result.
45  EXPECT_EQ ( user_check_ok, false ); //User check has been triggered.
46  user_check_ok = true;
47  t = toNumber<unsigned> ( "ab" );
48  EXPECT_EQ ( user_check_ok, false ); //User check should have been triggered.
49  user_check_ok = true;
50 }
51 
53 TEST ( stringutil, countneedles ) {
55  std::string y = "3_5_7";
56  EXPECT_EQ ( count_needles ( y, '_', 0, y.size() ), 2 );
57  std::string y2 = "3";
58  EXPECT_EQ ( count_needles ( y2, '_', 0, y2.size() ), 0 );
59  std::string y3 = "";
60  EXPECT_EQ ( count_needles ( y3, '_', 0, y3.size() ), 0 );
61  std::string y4 = "__3__";
62  EXPECT_EQ ( count_needles ( y4, '_', 0, y4.size() ), 4 );
63 }
64 
66 TEST ( stringutil, trim_spaces ) {
68  std::string input = " a b c d";
69  std::string reference = "a b c d";
70  std::string output;
71  trim_spaces ( input, &output );
72  EXPECT_EQ ( output, reference );
74  input = "";
75  reference = "";
76  trim_spaces ( input, &output );
77  EXPECT_EQ ( output, reference );
78 };
79 
81 TEST ( stringutil, validate_source_sentence ) {
83  std::string input1 = " a b c d";
84  std::string input2 = " 1 2 3 4";
85  std::string input3 = "";
86  std::string input4 = "1 2 3 4";
87  std::string input5 = "567";
88  EXPECT_EQ ( validate_source_sentence ( input1 ), false );
89  EXPECT_EQ ( validate_source_sentence ( input2 ), false );
90  EXPECT_EQ ( validate_source_sentence ( input3 ), false );
91  EXPECT_EQ ( validate_source_sentence ( input4 ), true );
92  EXPECT_EQ ( validate_source_sentence ( input5 ), true );
93 }
94 
96 
97 TEST ( dotproduct, basic_test ) {
99  std::vector<float> v1;
100  std::vector<float> v2;
101  v1.push_back ( 1 );
102  v2.push_back ( 5 );
103  EXPECT_EQ ( dotproduct ( v1, v2 ), 5 );
104  v1.push_back ( 10 );
105  v2.push_back ( 50 );
106  EXPECT_EQ ( dotproduct ( v1, v2 ), 505 );
107  v1.push_back ( 9 );
108  EXPECT_EQ ( dotproduct ( v1, v2 ), 505 );
109  v2.push_back ( 1000 );
110  EXPECT_EQ ( dotproduct ( v1, v2 ), 9505 );
111 }
112 
114 TEST ( stringutil, parseparamstring ) {
116  std::string rule = "X 35 26 35.2 43.7 86.3";
117  std::vector<float> v = ParseParamString<float> ( rule, 8 );
118  ASSERT_EQ ( v.size(), 3 );
119  EXPECT_EQ ( v[0], 35.2f );
120  EXPECT_EQ ( v[1], 43.7f );
121  EXPECT_EQ ( v[2], 86.3f );
122  std::string rule2 = "X 35_47 43_55_58 0.45";
123  std::vector<float> v2 = ParseParamString<float> ( rule2, 17 );
124  ASSERT_EQ ( v2.size(), 1 );
125  EXPECT_EQ ( v2[0], 0.45f );
126 }
127 
129 TEST ( stringutil, trim_trailing_zeros ) {
131  std::string f = "0.370000";
133  EXPECT_EQ (f, "0.37");
134  std::string f2 = "30";
135  trim_trailing_zeros (f2);
136  EXPECT_EQ (f2, "30");
137  std::string f3 = "30.00";
138  trim_trailing_zeros (f3);
139  EXPECT_EQ (f3, "30");
140 }
141 
142 TEST ( stringutil , ends_with ) {
143  using ucam::util::ends_with;
144  std::string s1 = "data/rules/trivial.grammar";
145  std::string s2 = "";
146  std::string s3 = "silly.gz";
147  std::string s4 = "silly.gz.jopas";
148  EXPECT_EQ (ends_with (s1, ".gz"), false);
149  EXPECT_EQ (ends_with (s2, ".gz"), false);
150  EXPECT_EQ (ends_with (s3, ".gz"), true);
151  EXPECT_EQ (ends_with (s4, ".gz"), false);
152 }
153 
154 #ifndef GMAINTEST
155 
161 int main ( int argc, char **argv ) {
162  ::testing::InitGoogleTest ( &argc, argv );
163  return RUN_ALL_TESTS();
164 }
165 #endif
void trim_spaces(const std::string &input, std::string *output)
Trims spaces at the edges (no spaces) and also between words (only one space)
std::vector< T > ParseParamString(const std::string &stringparams, size_t pos=0)
Function to parse string of parameters, e.g. separated by commas.
Definition: params.hpp:51
bool user_check_ok
void trim_trailing_zeros(std::string &snumber)
Static variables for logger. Include only once from main file.
Convenience functions to parse parameters from a string.
uint count_needles(const std::string &haystack, const char needle, std::size_t start, std::size_t end)
Convenience function that counts the number of times a needle appears.
float dotproduct(std::vector< float > &v1, std::vector< float > &v2)
Implements dot product.
T toNumber(const std::string &x)
Converts a string to an arbitrary number Converts strings to a number. Quits execution if conversion ...
int main(int argc, char **argv)
main function. If compiled individualy, will kickoff any tests in this file.
TEST(numberconversions, int2string)
Test toString<unsigned>
bool ends_with(std::string const &haystack, std::string const &needle)
Unit testing: google testing common header.
Static variable for custom_assert. Include only once from main file.
bool validate_source_sentence(const std::string &s)
Checks whether the sentence is in format ^\d+( \d+)*$.