Skip to content

Commit 5ef3d11

Browse files
committed
Add tests for string_utilities
1 parent 27b230a commit 5ef3d11

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

test/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
LDADD = $(top_builddir)/src/libhttpserver.la
2020
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
2121
METASOURCES = AUTO
22-
check_PROGRAMS = basic http_utils threaded
22+
check_PROGRAMS = basic http_utils threaded string_utilities
2323

2424
MOSTLYCLEANFILES = *.gcda *.gcno *.gcov
2525

2626
basic_SOURCES = integ/basic.cpp
2727
threaded_SOURCES = integ/threaded.cpp
2828
http_utils_SOURCES = unit/http_utils_test.cpp
29+
string_utilities_SOURCES = unit/string_utilities_test.cpp
2930

3031
noinst_HEADERS = littletest.hpp
3132
AM_CXXFLAGS += -lcurl -Wall -fPIC
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)