|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include "littletest.hpp" |
| 22 | +#include "string_utilities.hpp" |
| 23 | + |
| 24 | +#include <cstdio> |
| 25 | + |
| 26 | +using namespace httpserver; |
| 27 | +using namespace std; |
| 28 | + |
| 29 | +LT_BEGIN_SUITE(string_utilities_suite) |
| 30 | + void set_up() |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + void tear_down() |
| 35 | + { |
| 36 | + } |
| 37 | +LT_END_SUITE(string_utilities_suite) |
| 38 | + |
| 39 | +LT_BEGIN_AUTO_TEST(string_utilities_suite, to_upper_copy) |
| 40 | + LT_CHECK_EQ(string_utilities::to_upper_copy("test message"), string("TEST MESSAGE")); |
| 41 | + LT_CHECK_EQ(string_utilities::to_upper_copy("tEsT mEssAge 245&$"), string("TEST MESSAGE 245&$")); |
| 42 | +LT_END_AUTO_TEST(to_upper_copy) |
| 43 | + |
| 44 | +LT_BEGIN_AUTO_TEST(string_utilities_suite, to_upper) |
| 45 | + string value = "test message"; |
| 46 | + string_utilities::to_upper(value); |
| 47 | + LT_CHECK_EQ(value, string("TEST MESSAGE")); |
| 48 | + |
| 49 | + value = "tEsT mEssAge 245&$"; |
| 50 | + string_utilities::to_upper(value); |
| 51 | + LT_CHECK_EQ(value, string("TEST MESSAGE 245&$")); |
| 52 | +LT_END_AUTO_TEST(to_upper) |
| 53 | + |
| 54 | +LT_BEGIN_AUTO_TEST(string_utilities_suite, to_lower_copy) |
| 55 | + LT_CHECK_EQ(string_utilities::to_lower_copy("TEST MESSAGE"), string("test message")); |
| 56 | + LT_CHECK_EQ(string_utilities::to_lower_copy("tEsT mEssAge 245&$"), string("test message 245&$")); |
| 57 | +LT_END_AUTO_TEST(to_lower_copy) |
| 58 | + |
| 59 | +LT_BEGIN_AUTO_TEST(string_utilities_suite, split_string) |
| 60 | + string value = "test this message here"; |
| 61 | + string expected_arr[] = { "test", "this", "message", "here" }; |
| 62 | + vector<string> expected(expected_arr, expected_arr + sizeof(expected_arr) / sizeof(expected_arr[0])); |
| 63 | + vector<string> actual = string_utilities::string_split(value, ' ', false); |
| 64 | + |
| 65 | + LT_CHECK_COLLECTIONS_EQ(expected.begin(), expected.end(), actual.begin()); |
| 66 | +LT_END_AUTO_TEST(split_string) |
| 67 | + |
| 68 | +LT_BEGIN_AUTO_TEST(string_utilities_suite, split_string_multiple_spaces) |
| 69 | + string value = "test this message here"; |
| 70 | + string expected_arr[] = { "test", "", "this", "", "message", "", "here" }; |
| 71 | + vector<string> expected(expected_arr, expected_arr + sizeof(expected_arr) / sizeof(expected_arr[0])); |
| 72 | + vector<string> actual = string_utilities::string_split(value, ' ', false); |
| 73 | + |
| 74 | + LT_CHECK_COLLECTIONS_EQ(expected.begin(), expected.end(), actual.begin()); |
| 75 | +LT_END_AUTO_TEST(split_string_multiple_spaces) |
| 76 | + |
| 77 | +LT_BEGIN_AUTO_TEST(string_utilities_suite, split_string_multiple_spaces_collapse) |
| 78 | + string value = "test this message here"; |
| 79 | + string expected_arr[] = { "test", "this", "message", "here" }; |
| 80 | + vector<string> expected(expected_arr, expected_arr + sizeof(expected_arr) / sizeof(expected_arr[0])); |
| 81 | + vector<string> actual = string_utilities::string_split(value, ' ', true); |
| 82 | + |
| 83 | + LT_CHECK_COLLECTIONS_EQ(expected.begin(), expected.end(), actual.begin()); |
| 84 | +LT_END_AUTO_TEST(split_string_multiple_spaces_collapse) |
| 85 | + |
| 86 | +LT_BEGIN_AUTO_TEST(string_utilities_suite, split_string_end_space) |
| 87 | + string value = "test this message here "; |
| 88 | + string expected_arr[] = { "test", "this", "message", "here" }; |
| 89 | + vector<string> expected(expected_arr, expected_arr + sizeof(expected_arr) / sizeof(expected_arr[0])); |
| 90 | + vector<string> actual = string_utilities::string_split(value, ' ', false); |
| 91 | + |
| 92 | + LT_CHECK_COLLECTIONS_EQ(expected.begin(), expected.end(), actual.begin()); |
| 93 | +LT_END_AUTO_TEST(split_string_end_space) |
| 94 | + |
| 95 | +LT_BEGIN_AUTO_TEST(string_utilities_suite, regex_replace) |
| 96 | + LT_CHECK_EQ(string_utilities::regex_replace("test///message", "(\\/)+", "/"), "test/message"); |
| 97 | + LT_CHECK_EQ(string_utilities::regex_replace("test 1234 message", "([0-9])+", "bob"), "test bob message"); |
| 98 | +LT_END_AUTO_TEST(regex_replace) |
| 99 | + |
| 100 | +LT_BEGIN_AUTO_TEST_ENV() |
| 101 | + AUTORUN_TESTS() |
| 102 | +LT_END_AUTO_TEST_ENV() |
0 commit comments