b6d4c73ebdf036bd4de96742a044b1d113194fe6
[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   anna::Tokenizer lst;
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);
70   lst.apply("", ",");
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++)
75 //    str += "x ";
76 //  BOOST_CHECK_THROW(lst.apply(str, " "), anna::RuntimeException);
77 }
78
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");
82   char cad_aux[128];
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);
91   Unsigned64 u64 = ull;
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);
96   float f = 123.34;
97   double d = 3.1415;
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);
102 }
103
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"));
108   //[ property ]
109   //thing = tshirt
110   //size = 1
111   //color = blue
112   //[ owner ]
113   //name = edu
114   //address = madrid
115   BOOST_CHECK_THROW(conf.getValue("property", "WRONG_VAR"), anna::RuntimeException);
116   BOOST_CHECK_THROW(conf.getValue("WRONG_SECTION", "thing"), anna::RuntimeException);
117   std::string value;
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"));
128 }
129
130 BOOST_AUTO_TEST_CASE(environment) {
131   anna::Environment &env = anna::Environment::instantiate();
132   env.initialize();
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
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
143   BOOST_REQUIRE_EQUAL(env.getValue("MISSING_ENV_VAR"), std::string(""));
144 }
145