Added google test framework.

This commit is contained in:
yhirose 2012-09-26 21:35:49 -04:00
parent 6818f18275
commit 4781156fa1
6 changed files with 28771 additions and 7 deletions

View file

@ -161,13 +161,14 @@ inline socket_t create_server_socket(const char* ipaddr_or_hostname, int port)
return sock;
}
inline void close_socket(socket_t sock)
inline int close_server_socket(socket_t sock)
{
#ifdef _WIN32
closesocket(sock);
shutdown(sock, SD_BOTH);
return closesocket(sock);
#else
shutdown(sock, SHUT_RDWR);
close(sock);
return close(sock);
#endif
}
@ -256,7 +257,7 @@ inline bool Server::run()
return true;
}
close_socket(sock_);
close_server_socket(sock_);
return false;
}
@ -272,7 +273,7 @@ inline bool Server::run()
process_request(fp_read, fp_write);
fflush(fp_write);
close_socket(fd);
close_server_socket(fd);
}
// NOTREACHED
@ -280,9 +281,8 @@ inline bool Server::run()
inline void Server::stop()
{
auto sock = sock_;
close_server_socket(sock_);
sock_ = -1;
close_socket(sock);
}
inline bool read_request_line(FILE* fp, Request& request)

17
test/Makefile Normal file
View file

@ -0,0 +1,17 @@
USE_CLANG = 1
ifdef USE_CLANG
CC = clang++
CFLAGS = -std=c++0x -stdlib=libc++ -g -DGTEST_USE_OWN_TR1_TUPLE
else
CC = g++
CFLAGS = -std=c++11 -g
endif
test : test.cc ../httpsvrkit.h
$(CC) -o test $(CFLAGS) -I.. -I. test.cc gtest/gtest-all.cc gtest/gtest_main.cc
.PHONY : run
run: test
./test

9118
test/gtest/gtest-all.cc Normal file

File diff suppressed because it is too large Load diff

19537
test/gtest/gtest.h Normal file

File diff suppressed because it is too large Load diff

39
test/gtest/gtest_main.cc Normal file
View file

@ -0,0 +1,39 @@
// Copyright 2006, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <iostream>
#include "gtest/gtest.h"
GTEST_API_ int main(int argc, char **argv) {
std::cout << "Running main() from gtest_main.cc\n";
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

53
test/test.cc Normal file
View file

@ -0,0 +1,53 @@
#include <gtest/gtest.h>
#include <httpsvrkit.h>
using namespace std;
using namespace httpsvrkit;
TEST(SplitTest, ParseQueryString)
{
string s = "key1=val1&key2=val2&key3=val3";
map<string, string> dic;
split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) {
string key, val;
split(b, e, '=', [&](const char* b, const char* e) {
if (key.empty()) {
key.assign(b, e);
} else {
val.assign(b, e);
}
});
dic[key] = val;
});
ASSERT_EQ("val1", dic["key1"]);
ASSERT_EQ("val2", dic["key2"]);
ASSERT_EQ("val3", dic["key3"]);
}
TEST(SocketTest, OpenClose)
{
socket_t sock = create_server_socket("localhost", 1914);
ASSERT_NE(-1, sock);
auto ret = close_server_socket(sock);
ASSERT_EQ(0, ret);
}
TEST(GetHeaderValueTest, DefaultValue)
{
MultiMap map = {{"Dummy","Dummy"}};
auto val = get_header_value(map, "Content-Type", "text/plain");
ASSERT_STREQ("text/plain", val);
}
TEST(GetHeaderValueTest, RegularValue)
{
MultiMap map = {{"Content-Type","text/html"}, {"Dummy", "Dummy"}};
auto val = get_header_value(map, "Content-Type", "text/plain");
ASSERT_STREQ("text/html", val);
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix