c32916fa54c18aa46e7d902af6eb637136d55f2b
[anna.git] / test / core / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #define BOOST_TEST_MODULE ANNA_CORE_TEST
10
11 #include <anna/test/clang_specific.hpp>
12 #include <boost/test/included/unit_test.hpp>
13
14 #include <iostream>
15 #include <string>
16
17 #include <limits.h>
18
19 #include <anna/core/util/Tokenizer.hpp>
20 #include <anna/core/util/Configuration.hpp>
21 #include <anna/core/util/Environment.hpp>
22 #include <anna/core/functions.hpp>
23
24 using namespace std;
25 using namespace anna;
26
27 // see http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools.html
28 //     http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools/reference.html
29
30 BOOST_AUTO_TEST_CASE(tokenizer) {
31   anna::Tokenizer lst;
32   lst.apply("In three words", " ");
33   //BOOST_REQUIRE(lst.size() == 3);
34   BOOST_REQUIRE_EQUAL(lst.size(), 3);
35   anna::Tokenizer::const_iterator tok_iter(lst.begin());
36   BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "In");
37   BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "three");
38   BOOST_REQUIRE_EQUAL(anna::Tokenizer::data(tok_iter++), "words");
39   BOOST_REQUIRE_EQUAL(tok_iter, lst.end());
40   BOOST_CHECK_THROW(lst.at(10), anna::RuntimeException);
41   lst.apply("In three words", ",");
42   BOOST_REQUIRE_EQUAL(lst.size(), 1);
43   lst.apply("", ",");
44   BOOST_REQUIRE_EQUAL(lst.size(), 0);
45   BOOST_CHECK_THROW(lst.last(), anna::RuntimeException);
46 //  std::string str = "";
47 //  for (int k = 0; k < 100; k++)
48 //    str += "x ";
49 //  BOOST_CHECK_THROW(lst.apply(str, " "), anna::RuntimeException);
50 }
51
52 BOOST_AUTO_TEST_CASE(functions_asString) {
53   std::string msg = anna::functions::asString("One %s has %d legs. Two %s have %d legs", "horse", 4, "horses", 8);
54   BOOST_REQUIRE_EQUAL(msg, "One horse has 4 legs. Two horses have 8 legs");
55   char cad_aux[128];
56   sprintf(cad_aux, "%d", 123);
57   BOOST_REQUIRE_EQUAL(anna::functions::asString(123), cad_aux);
58   unsigned long ul = 43200111UL;
59   sprintf(cad_aux, "%lu", ul);
60   BOOST_REQUIRE_EQUAL(anna::functions::asString(ul), cad_aux);
61   unsigned long long ull = 4321000111ULL;
62   sprintf(cad_aux, "%llu", ull);
63   BOOST_REQUIRE_EQUAL(anna::functions::asString(ull), cad_aux);
64   U64 u64 = ull;
65   BOOST_REQUIRE_EQUAL(anna::functions::asString(u64), cad_aux);
66   unsigned int ui = 1234567890U;
67   sprintf(cad_aux, "%u", ui);
68   BOOST_REQUIRE_EQUAL(anna::functions::asString(ui), cad_aux);
69   float f = 123.34;
70   double d = 3.1415;
71   sprintf(cad_aux, "%4.2f", f);
72   BOOST_REQUIRE_EQUAL(anna::functions::asString(f, "%4.2f"), cad_aux);
73   sprintf(cad_aux, "%4.2f", d);
74   BOOST_REQUIRE_EQUAL(anna::functions::asString(d, "%4.2f"), cad_aux);
75 }
76
77 BOOST_AUTO_TEST_CASE(configuration) {
78   anna::Configuration conf;
79   BOOST_CHECK_THROW(conf.load("missing_file.cnf"), anna::RuntimeException);
80   BOOST_CHECK_NO_THROW(conf.load("test/core/example.cnf"));
81   //[ property ]
82   //thing = tshirt
83   //size = 1
84   //color = blue
85   //[ owner ]
86   //name = edu
87   //address = madrid
88   BOOST_CHECK_THROW(conf.getValue("property", "WRONG_VAR"), anna::RuntimeException);
89   BOOST_CHECK_THROW(conf.getValue("WRONG_SECTION", "thing"), anna::RuntimeException);
90   std::string value;
91   BOOST_REQUIRE_EQUAL(value = conf.getValue("property", "thing"), "tshirt");
92   BOOST_REQUIRE_EQUAL(conf.getIntegerValue("property", "size"), 1);
93   BOOST_REQUIRE_EQUAL(std::string(conf.getValue("property", "color")), "blue");
94   BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "name")), "edu");
95   BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "address")), "madrid");
96   conf.setDefaultValue("owner", "phone", "555 55 55");
97   BOOST_REQUIRE_EQUAL(std::string(conf.getValue("owner", "phone")), "555 55 55");
98   BOOST_REQUIRE(!conf.getValue("owner", "phone", true) /* query value is NULL: 'true = strict' even with default value assigned */);
99   BOOST_REQUIRE(!conf.exists("owner", "phone"));
100   BOOST_REQUIRE(conf.exists("owner", "name"));
101 }
102
103 BOOST_AUTO_TEST_CASE(environment) {
104   anna::Environment &env = anna::Environment::instantiate();
105   env.initialize();
106   BOOST_CHECK_THROW(env.getValue("WRONG_ENV_VAR", true), anna::RuntimeException); // true => exception if missing
107   BOOST_CHECK_THROW(env.getValue(NULL), anna::RuntimeException);
108   env.setVariable("TEST_VAR", "my test var value");
109   BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("my test var value"));
110   env.setVariable("TEST_VAR", "my new test var value", false /* no overwritting */);
111   BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string("my test var value"));
112   env.unsetVariable("TEST_VAR");
113   BOOST_REQUIRE_EQUAL(env.getValue("TEST_VAR"), std::string(""));
114   BOOST_REQUIRE_EQUAL(env.getValue("MISSING_ENV_VAR"), std::string(""));
115 }
116