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/functions.hpp>
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
55 BOOST_AUTO_TEST_CASE(tokenizer) {
58 lst.apply("In three words", " ");
59 //BOOST_REQUIRE(lst.size() == 3);
60 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);
69 lst.apply("In three words", ",");
70 BOOST_REQUIRE_EQUAL(lst.size(), 1);
73 BOOST_REQUIRE_EQUAL(lst.size(), 0);
74 BOOST_CHECK_THROW(lst.last(), anna::RuntimeException);
76 // std::string str = "";
77 // for (int k = 0; k < 100; k++)
79 // BOOST_CHECK_THROW(lst.apply(str, " "), anna::RuntimeException);
82 BOOST_AUTO_TEST_CASE(functions_asString) {
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");
88 sprintf (cad_aux, "%d", 123);
89 BOOST_REQUIRE_EQUAL(anna::functions::asString(123), cad_aux);
91 unsigned long ul = 43200111UL;
92 sprintf (cad_aux, "%lu", ul);
93 BOOST_REQUIRE_EQUAL(anna::functions::asString(ul), cad_aux);
95 unsigned long long ull = 4321000111ULL;
96 sprintf (cad_aux, "%llu", ull);
97 BOOST_REQUIRE_EQUAL(anna::functions::asString(ull), cad_aux);
100 BOOST_REQUIRE_EQUAL(anna::functions::asString(u64), cad_aux);
102 unsigned int ui = 1234567890U;
103 sprintf (cad_aux, "%u", ui);
104 BOOST_REQUIRE_EQUAL(anna::functions::asString(ui), cad_aux);
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);