First commit
[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/functions.hpp>
48
49 using namespace std;
50 using namespace anna;
51
52 // see http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools.html
53 //     http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/utf/testing-tools/reference.html
54
55 BOOST_AUTO_TEST_CASE(tokenizer) {
56
57   anna::Tokenizer lst;
58   lst.apply("In three words", " ");
59   //BOOST_REQUIRE(lst.size() == 3);
60   BOOST_REQUIRE_EQUAL(lst.size(), 3);
61
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
69   lst.apply("In three words", ",");
70   BOOST_REQUIRE_EQUAL(lst.size(), 1);
71
72   lst.apply("", ",");
73   BOOST_REQUIRE_EQUAL(lst.size(), 0);
74   BOOST_CHECK_THROW(lst.last(), anna::RuntimeException);
75
76 //  std::string str = "";
77 //  for (int k = 0; k < 100; k++)
78 //    str += "x ";
79 //  BOOST_CHECK_THROW(lst.apply(str, " "), anna::RuntimeException);
80 }
81
82 BOOST_AUTO_TEST_CASE(functions_asString) {
83
84   std::string msg = anna::functions::asString("One %s has %d legs. Two %s have %d legs", "horse", 4, "horses", 8);
85   BOOST_REQUIRE_EQUAL(msg, "One horse has 4 legs. Two horses have 8 legs");
86
87   char cad_aux[128];
88   sprintf (cad_aux, "%d", 123);
89   BOOST_REQUIRE_EQUAL(anna::functions::asString(123), cad_aux);
90
91   unsigned long ul = 43200111UL;
92   sprintf (cad_aux, "%lu", ul);
93   BOOST_REQUIRE_EQUAL(anna::functions::asString(ul), cad_aux);
94
95   unsigned long long ull = 4321000111ULL;
96   sprintf (cad_aux, "%llu", ull);
97   BOOST_REQUIRE_EQUAL(anna::functions::asString(ull), cad_aux);
98
99   Unsigned64 u64 = ull;
100   BOOST_REQUIRE_EQUAL(anna::functions::asString(u64), cad_aux);
101
102   unsigned int ui = 1234567890U;
103   sprintf (cad_aux, "%u", ui);
104   BOOST_REQUIRE_EQUAL(anna::functions::asString(ui), cad_aux);
105
106   float f = 123.34;
107   double d = 3.1415;
108   sprintf (cad_aux, "%4.2f", f);
109   BOOST_REQUIRE_EQUAL(anna::functions::asString(f, "%4.2f"), cad_aux);
110   sprintf (cad_aux, "%4.2f", d);
111   BOOST_REQUIRE_EQUAL(anna::functions::asString(d, "%4.2f"), cad_aux);
112 }
113