Cambridge SMT System
registrypo.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 namespace bfs = boost::filesystem;
29 
31 TEST ( registrypo, alternativeconstructor ) {
32  unordered_map<std::string, boost::any> v;
33  v["hobbits"] = 4;
34  v["raistlin"] = std::string ( "shirak" );
35  v["seoman"] = float ( 3.05f );
36  uu::RegistryPO j ( v );
37  EXPECT_EQ ( j.get<int> ( "hobbits" ), 4 );
38  EXPECT_EQ ( j.get<std::string> ( "raistlin" ), "shirak" );
39  EXPECT_EQ ( j.get<float> ( "seoman" ), 3.05f );
40 }
41 
43 TEST ( registrypo, dump ) {
44  unordered_map<std::string, boost::any> v;
45  v["hobbits"] = unsigned ( 4 );
46  v["raistlin"] = std::string ( "shirak" );
47  v["seoman"] = float ( 3.05f );
48  uu::RegistryPO j ( v );
49  EXPECT_EQ ( j.dump(),
50  "\n\t\t+hobbits=4\n\t\t+raistlin=shirak\n\t\t+seoman=3.05\n\n" );
51 }
52 
54 TEST ( registrypo, alternativegetmethods ) {
55  unordered_map<std::string, boost::any> v;
56  v["goblin"] = std::string ( "S,X,V" );
57  v["turin"] = std::string ( "X,1,Y,2" );
58  v["turinwrong"] = std::string ( "X,A,Y,B" );
59  v["turambar"] = std::string ( "" );
60  v["smaug"] = std::string ( "T" );
61  uu::RegistryPO j ( v );
62  ASSERT_EQ ( j.getVectorString ( "goblin" ).size(), 3 );
63  EXPECT_EQ ( j.getVectorString ( "goblin" ) [0], "S" );
64  EXPECT_EQ ( j.getVectorString ( "goblin" ) [1], "X" );
65  EXPECT_EQ ( j.getVectorString ( "goblin" ) [2], "V" );
66  EXPECT_EQ ( j.getVectorString ( "goblin", 0 ) , "S" );
67  EXPECT_EQ ( j.getVectorString ( "goblin", 1 ) , "X" );
68  EXPECT_EQ ( j.getVectorString ( "goblin", 2 ) , "V" );
69  ASSERT_EQ ( j.getMappedStringIndex ( "goblin" ).size(), 3 );
70  EXPECT_EQ ( j.getMappedStringIndex ( "goblin" ) ["S"], 1 );
71  EXPECT_EQ ( j.getMappedStringIndex ( "goblin" ) ["X"], 2 );
72  EXPECT_EQ ( j.getMappedStringIndex ( "goblin" ) ["V"], 3 );
73  ASSERT_EQ ( j.getMappedIndexString ( "goblin" ).size(), 3 );
74  EXPECT_EQ ( j.getMappedIndexString ( "goblin" ) [1], "S" );
75  EXPECT_EQ ( j.getMappedIndexString ( "goblin" ) [2], "X" );
76  EXPECT_EQ ( j.getMappedIndexString ( "goblin" ) [3], "V" );
77  ASSERT_EQ ( j.getPairMappedStringUInt ( "turin" ).size(), 2 );
78  EXPECT_EQ ( j.getPairMappedStringUInt ( "turin" ) ["X"], 1 );
79  EXPECT_EQ ( j.getPairMappedStringUInt ( "turin" ) ["Y"], 2 );
80  user_check_ok = true;
81  ASSERT_EQ ( j.getPairMappedStringUInt ( "turinwrong" ).size(), 2 );
82  EXPECT_EQ ( user_check_ok, false );
83  user_check_ok = true;
84  unordered_set<std::string> aux = j.getSetString ( "goblin" );
85  EXPECT_EQ ( aux.size(), 3 );
86  EXPECT_TRUE ( aux.find ( "S" ) != aux.end() );
87  EXPECT_TRUE ( aux.find ( "X" ) != aux.end() );
88  EXPECT_TRUE ( aux.find ( "V" ) != aux.end() );
89  ASSERT_EQ ( j.getVectorString ( "turambar" ).size(), 0 );
90  ASSERT_EQ ( j.getMappedIndexString ( "turambar" ).size(), 0 );
91  ASSERT_EQ ( j.getMappedStringIndex ( "turambar" ).size(), 0 );
92  ASSERT_EQ ( j.getPairMappedStringUInt ( "turambar" ).size(), 0 );
93  ASSERT_EQ ( j.getVectorString ( "smaug" ).size(), 1 );
94  EXPECT_EQ ( j.getVectorString ( "smaug" ) [0], "T" );
95 }
96 
98 TEST ( registrypo, get_string ) {
99  unordered_map<std::string, boost::any> v;
100  v["hobbits"] = std::string ( "file://hobbits" );
101  uu::oszfstream o ( "hobbits" );
102  o << "frodo,bilbo" << endl;
103  o << "sam" << endl;
104  o << "merry" << endl;
105  o.close();
106  uu::RegistryPO j ( v );
107  EXPECT_EQ ( j.getString ( "hobbits" ), "frodo,bilbo sam merry" );
108  bfs::remove ( bfs::path ( "hobbits" ) );
109 }
110 
111 #ifndef GMAINTEST
112 
118 int main ( int argc, char **argv ) {
119  ::testing::InitGoogleTest ( &argc, argv );
120  return RUN_ALL_TESTS();
121 }
122 #endif
Wrapper stream class that writes to pipes, text files or gzipped files.
Definition: szfstream.hpp:200
int main(int argc, char **argv)
main function. If compiled individualy, will kickoff any tests in this file.
std::vector< std::string > getVectorString(const std::string &key) const
Convenience method that returns a vector of strings taking "," as the separator character.
Definition: registrypo.hpp:245
T get(const std::string &key) const
Returns parsed value associated to key.
Definition: registrypo.hpp:194
TEST(registrypo, alternativeconstructor)
Test alternative constructor for testing.
bool user_check_ok
unordered_map< uint, std::string > getMappedIndexString(const std::string &key) const
Convenience method that returns a hash of strings indexed by position and taking "," as the separator character.
Definition: registrypo.hpp:308
unordered_map< std::string, uint > getMappedStringIndex(const std::string &key) const
Convenience method that returns a hash of indices indexed by string and taking "," as the separator c...
Definition: registrypo.hpp:323
unordered_map< std::string, uint > getPairMappedStringUInt(const std::string &key) const
Convenience method that builds a hash with pairs taking "," as the separator character. Pair elements assumed to be unsigned integers. For instance, –param=X,3,T,5 => hash["X"]=3 and hash["T"]=5.
Definition: registrypo.hpp:340
Static variables for logger. Include only once from main file.
std::string getString(const std::string &key) const
Performs get<string> and checks whether the real value is to be loaded from file (–param=file://.....)
Definition: registrypo.hpp:205
std::string dump(const std::string &decorator_start="", const std::string &decorator_end="")
Dumps all configuration parameters into a string with a reasonably pretty format. ...
Definition: registrypo.hpp:108
Unit testing: google testing common header.
unordered_set< std::string > getSetString(const std::string &key) const
Convenience method that returns a set of strings taking "," as the separator character.
Definition: registrypo.hpp:279
Static variable for custom_assert. Include only once from main file.
void close()
Closes the file.
Definition: szfstream.hpp:323