1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
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 the copyright holder 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 <anna/test/clang_specific.hpp>
40 #include <boost/test/included/unit_test.hpp>
47 #include <anna/core/util/Tokenizer.hpp>
48 #include <anna/core/util/Configuration.hpp>
49 #include <anna/core/util/Environment.hpp>
50 #include <anna/core/functions.hpp>
55 // see http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools.html
56 // http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools/reference.html
58 BOOST_AUTO_TEST_CASE(tokenizer) {
60 lst.apply("In three words", " ");
61 //BOOST_REQUIRE(lst.size() == 3);
62 BOOST_REQUIRE_EQUAL(lst.size(), 3);
63 anna::Tokenizer::const_iterator tok_iter(lst.begin());
64 BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "In");
65 BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "three");
66 BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "words");
67 BOOST_REQUIRE_EQUAL(tok_iter, lst.end());
68 BOOST_CHECK_THROW(lst.at(10), anna::RuntimeException);
69 lst.apply("In three words", ",");
70 BOOST_REQUIRE_EQUAL(lst.size(), 1);
72 BOOST_REQUIRE_EQUAL(lst.size(), 0);
73 BOOST_CHECK_THROW(lst.last(), anna::RuntimeException);
74 // std::string str = "";
75 // for (int k = 0; k < 100; k++)
77 // BOOST_CHECK_THROW(lst.apply(str, " "), anna::RuntimeException);
80 BOOST_AUTO_TEST_CASE(functions_asString) {
81 std::string msg = anna::functions::asString("One %s has %d legs. Two %s have %d legs", "horse", 4, "horses", 8);
82 BOOST_REQUIRE_EQUAL(msg, "One horse has 4 legs. Two horses have 8 legs");
84 sprintf(cad_aux, "%d", 123);
85 BOOST_REQUIRE_EQUAL(anna::functions::asString(123), cad_aux);
86 unsigned long ul = 43200111UL;
87 sprintf(cad_aux, "%lu", ul);
88 BOOST_REQUIRE_EQUAL(anna::functions::asString(ul), cad_aux);
89 unsigned long long ull = 4321000111ULL;
90 sprintf(cad_aux, "%llu", ull);
91 BOOST_REQUIRE_EQUAL(anna::functions::asString(ull), cad_aux);
93 BOOST_REQUIRE_EQUAL(anna::functions::asString(u64), cad_aux);
94 unsigned int ui = 1234567890U;
95 sprintf(cad_aux, "%u", ui);
96 BOOST_REQUIRE_EQUAL(anna::functions::asString(ui), cad_aux);
99 sprintf(cad_aux, "%4.2f", f);
100 BOOST_REQUIRE_EQUAL(anna::functions::asString(f, "%4.2f"), cad_aux);
101 sprintf(cad_aux, "%4.2f", d);
102 BOOST_REQUIRE_EQUAL(anna::functions::asString(d, "%4.2f"), cad_aux);
105 BOOST_AUTO_TEST_CASE(configuration) {
106 anna::Configuration conf;
107 BOOST_CHECK_THROW(conf.load("missing_file.cnf"), anna::RuntimeException);
108 BOOST_CHECK_NO_THROW(conf.load("test/core/example.cnf"));
116 BOOST_CHECK_THROW(conf.getValue("property", "WRONG_VAR"), anna::RuntimeException);
117 BOOST_CHECK_THROW(conf.getValue("WRONG_SECTION", "thing"), anna::RuntimeException);
119 BOOST_REQUIRE_EQUAL(value = conf.getValue("property", "thing"), "tshirt");
120 BOOST_REQUIRE_EQUAL(conf.getIntegerValue("property", "size"), 1);
121 BOOST_REQUIRE_EQUAL(std::string(conf.getValue("property", "color")), "blue");
122 BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "name")), "edu");
123 BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "address")), "madrid");
124 conf.setDefaultValue("owner", "phone", "555 55 55");
125 BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "phone")), "555 55 55");
126 BOOST_REQUIRE(!conf.getValue("owner", "phone", true) /* query value is NULL: 'true = strict' even with default value assigned */);
127 BOOST_REQUIRE(!conf.exists("owner", "phone"));
128 BOOST_REQUIRE(conf.exists("owner", "name"));
131 BOOST_AUTO_TEST_CASE(environment) {
132 anna::Environment &env = anna::Environment::instantiate();
134 BOOST_CHECK_THROW(env.getValue("WRONG_ENV_VAR", true), anna::RuntimeException); // true => exception if missing
135 BOOST_CHECK_THROW(env.getValue(NULL), anna::RuntimeException);
136 env.setVariable("TEST_VAR", "my test var value");
137 BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("my test var value"));
138 env.setVariable("TEST_VAR", "my new test var value", false /* no overwritting */);
139 BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("my test var value"));
140 env.unsetVariable("TEST_VAR");
141 BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string(""));
142 BOOST_REQUIRE_EQUAL(env.getValue("MISSING_ENV_VAR"), std::string(""));