1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #define BOOST_TEST_MODULE ANNA_CORE_TEST
39 #include <boost/test/included/unit_test.hpp>
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>
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
57 BOOST_AUTO_TEST_CASE(tokenizer) {
59 lst.apply("In three words", " ");
60 //BOOST_REQUIRE(lst.size() == 3);
61 BOOST_REQUIRE_EQUAL(lst.size(), 3);
62 anna::Tokenizer::const_iterator tok_iter(lst.begin());
63 BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "In");
64 BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "three");
65 BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "words");
66 BOOST_REQUIRE_EQUAL(tok_iter, lst.end());
67 BOOST_CHECK_THROW(lst.at(10), anna::RuntimeException);
68 lst.apply("In three words", ",");
69 BOOST_REQUIRE_EQUAL(lst.size(), 1);
71 BOOST_REQUIRE_EQUAL(lst.size(), 0);
72 BOOST_CHECK_THROW(lst.last(), anna::RuntimeException);
73 // std::string str = "";
74 // for (int k = 0; k < 100; k++)
76 // BOOST_CHECK_THROW(lst.apply(str, " "), anna::RuntimeException);
79 BOOST_AUTO_TEST_CASE(functions_asString) {
80 std::string msg = anna::functions::asString("One %s has %d legs. Two %s have %d legs", "horse", 4, "horses", 8);
81 BOOST_REQUIRE_EQUAL(msg, "One horse has 4 legs. Two horses have 8 legs");
83 sprintf(cad_aux, "%d", 123);
84 BOOST_REQUIRE_EQUAL(anna::functions::asString(123), cad_aux);
85 unsigned long ul = 43200111UL;
86 sprintf(cad_aux, "%lu", ul);
87 BOOST_REQUIRE_EQUAL(anna::functions::asString(ul), cad_aux);
88 unsigned long long ull = 4321000111ULL;
89 sprintf(cad_aux, "%llu", ull);
90 BOOST_REQUIRE_EQUAL(anna::functions::asString(ull), cad_aux);
92 BOOST_REQUIRE_EQUAL(anna::functions::asString(u64), cad_aux);
93 unsigned int ui = 1234567890U;
94 sprintf(cad_aux, "%u", ui);
95 BOOST_REQUIRE_EQUAL(anna::functions::asString(ui), cad_aux);
98 sprintf(cad_aux, "%4.2f", f);
99 BOOST_REQUIRE_EQUAL(anna::functions::asString(f, "%4.2f"), cad_aux);
100 sprintf(cad_aux, "%4.2f", d);
101 BOOST_REQUIRE_EQUAL(anna::functions::asString(d, "%4.2f"), cad_aux);
104 BOOST_AUTO_TEST_CASE(configuration) {
105 anna::Configuration conf;
106 BOOST_CHECK_THROW(conf.load("missing_file.cnf"), anna::RuntimeException);
107 BOOST_CHECK_NO_THROW(conf.load("test/core/example.cnf"));
115 BOOST_CHECK_THROW(conf.getValue("property", "WRONG_VAR"), anna::RuntimeException);
116 BOOST_CHECK_THROW(conf.getValue("WRONG_SECTION", "thing"), anna::RuntimeException);
118 BOOST_REQUIRE_EQUAL(value = conf.getValue("property", "thing"), "tshirt");
119 BOOST_REQUIRE_EQUAL(conf.getIntegerValue("property", "size"), 1);
120 BOOST_REQUIRE_EQUAL(std::string(conf.getValue("property", "color")), "blue");
121 BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "name")), "edu");
122 BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "address")), "madrid");
123 conf.setDefaultValue("owner", "phone", "555 55 55");
124 BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "phone")), "555 55 55");
125 BOOST_REQUIRE(!conf.getValue("owner", "phone", true) /* query value is NULL: 'true = strict' even with default value assigned */);
126 BOOST_REQUIRE(!conf.exists("owner", "phone"));
127 BOOST_REQUIRE(conf.exists("owner", "name"));
130 BOOST_AUTO_TEST_CASE(environment) {
131 anna::Environment &env = anna::Environment::instantiate();
133 BOOST_CHECK_THROW(env.getValue("WRONG_ENV_VAR", true), anna::RuntimeException); // true => exception if missing
134 BOOST_CHECK_THROW(env.getValue(NULL), anna::RuntimeException);
135 env.setVariable("TEST_VAR", "my test var value");
136 BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("my test var value"));
137 env.setVariable("TEST_VAR", "my new test var value", false /* no overwritting */);
138 BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("my test var value"));
139 env.unsetVariable("TEST_VAR");
140 BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string(""));
141 BOOST_REQUIRE_EQUAL(env.getValue("MISSING_ENV_VAR"), std::string(""));