New Environment core class
[anna.git] / test / core / main.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #define BOOST_TEST_MODULE ANNA_CORE_TEST
38
39 #include <boost/test/included/unit_test.hpp>
40
41 #include <iostream>
42 #include <string>
43
44 #include <limits.h>
45
46 #include <anna/core/util/Tokenizer.hpp>
47 #include <anna/core/util/Configuration.hpp>
48 #include <anna/core/util/Environment.hpp>
49 #include <anna/core/functions.hpp>
50
51 using namespace std;
52 using namespace anna;
53
54 // see http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools.html
55 //     http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools/reference.html
56
57 BOOST_AUTO_TEST_CASE(tokenizer) {
58
59   anna::Tokenizer lst;
60   lst.apply("In three words", " ");
61   //BOOST_REQUIRE(lst.size() == 3);
62   BOOST_REQUIRE_EQUAL(lst.size(), 3);
63
64   anna::Tokenizer::const_iterator tok_iter(lst.begin());
65   BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "In");
66   BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "three");
67   BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "words");
68   BOOST_REQUIRE_EQUAL(tok_iter, lst.end()); 
69   BOOST_CHECK_THROW(lst.at(10), anna::RuntimeException);
70
71   lst.apply("In three words", ",");
72   BOOST_REQUIRE_EQUAL(lst.size(), 1);
73
74   lst.apply("", ",");
75   BOOST_REQUIRE_EQUAL(lst.size(), 0);
76   BOOST_CHECK_THROW(lst.last(), anna::RuntimeException);
77
78 //  std::string str = "";
79 //  for (int k = 0; k < 100; k++)
80 //    str += "x ";
81 //  BOOST_CHECK_THROW(lst.apply(str, " "), anna::RuntimeException);
82 }
83
84 BOOST_AUTO_TEST_CASE(functions_asString) {
85
86   std::string msg = anna::functions::asString("One %s has %d legs. Two %s have %d legs", "horse", 4, "horses", 8);
87   BOOST_REQUIRE_EQUAL(msg, "One horse has 4 legs. Two horses have 8 legs");
88
89   char cad_aux[128];
90   sprintf (cad_aux, "%d", 123);
91   BOOST_REQUIRE_EQUAL(anna::functions::asString(123), cad_aux);
92
93   unsigned long ul = 43200111UL;
94   sprintf (cad_aux, "%lu", ul);
95   BOOST_REQUIRE_EQUAL(anna::functions::asString(ul), cad_aux);
96
97   unsigned long long ull = 4321000111ULL;
98   sprintf (cad_aux, "%llu", ull);
99   BOOST_REQUIRE_EQUAL(anna::functions::asString(ull), cad_aux);
100
101   Unsigned64 u64 = ull;
102   BOOST_REQUIRE_EQUAL(anna::functions::asString(u64), cad_aux);
103
104   unsigned int ui = 1234567890U;
105   sprintf (cad_aux, "%u", ui);
106   BOOST_REQUIRE_EQUAL(anna::functions::asString(ui), cad_aux);
107
108   float f = 123.34;
109   double d = 3.1415;
110   sprintf (cad_aux, "%4.2f", f);
111   BOOST_REQUIRE_EQUAL(anna::functions::asString(f, "%4.2f"), cad_aux);
112   sprintf (cad_aux, "%4.2f", d);
113   BOOST_REQUIRE_EQUAL(anna::functions::asString(d, "%4.2f"), cad_aux);
114 }
115
116 BOOST_AUTO_TEST_CASE(configuration) {
117
118   anna::Configuration conf;
119
120   BOOST_CHECK_THROW(conf.load("missing_file.cnf"), anna::RuntimeException);
121   BOOST_CHECK_NO_THROW(conf.load("test/core/example.cnf"));
122   
123   //[ property ]
124   //thing = tshirt
125   //size = 1
126   //color = blue
127   //[ owner ]
128   //name = edu
129   //address = madrid
130   
131   BOOST_CHECK_THROW(conf.getValue("property", "WRONG_VAR"), anna::RuntimeException);
132   BOOST_CHECK_THROW(conf.getValue("WRONG_SECTION", "thing"), anna::RuntimeException);
133
134   std::string value;
135
136   BOOST_REQUIRE_EQUAL(value = conf.getValue("property", "thing"), "tshirt");
137   BOOST_REQUIRE_EQUAL(conf.getIntegerValue("property", "size"), 1);
138   BOOST_REQUIRE_EQUAL(std::string(conf.getValue("property", "color")), "blue");
139   BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "name")), "edu");
140   BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "address")), "madrid");
141
142   conf.setDefaultValue("owner", "phone", "555 55 55");
143   BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "phone")), "555 55 55");
144   BOOST_REQUIRE(!conf.getValue("owner", "phone", true) /* query value is NULL: 'true = strict' even with default value assigned */);
145   BOOST_REQUIRE(!conf.exists("owner", "phone"));
146   BOOST_REQUIRE(conf.exists("owner", "name"));
147 }
148
149 BOOST_AUTO_TEST_CASE(environment) {
150
151   anna::Environment &env = anna::Environment::instantiate();
152
153   BOOST_CHECK_THROW(env.getValue("WRONG_ENV_VAR", true /* exception if missing */), anna::RuntimeException);
154   BOOST_CHECK_THROW(env.getValue(NULL), anna::RuntimeException);
155   BOOST_REQUIRE_EQUAL(env.getValue("HOME"), std::string("/home/eramos"));
156   BOOST_REQUIRE_EQUAL(env.getValue("MISSING_ENV_VAR"), std::string(""));
157 }
158