Skip to content

Commit e59ad2c

Browse files
committed
Add unit tests
1 parent c9c0fb4 commit e59ad2c

File tree

5 files changed

+134
-8
lines changed

5 files changed

+134
-8
lines changed

CMakeLists.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ include_directories(
3535
${PROJECT_SOURCE_DIR}/trantor/net/inner
3636
)
3737

38-
# lib directories
39-
# LINK_DIRECTORIES(
40-
# ${PROJECT_BINARY_DIR}
41-
# )
42-
4338
set(TRANTOR_SOURCES
4439
trantor/utils/AsyncFileLogger.cc
4540
trantor/utils/ConcurrentTaskQueue.cc
@@ -78,9 +73,6 @@ endif()
7873
find_path(CARES_INCLUDE_DIR ares.h)
7974
find_library(CARES_LIBRARY NAMES cares)
8075
if(CARES_INCLUDE_DIR AND CARES_LIBRARY)
81-
message(STATUS "found cares")
82-
message(STATUS "inc:" ${CARES_INCLUDE_DIR})
83-
message(STATUS "lib:" ${CARES_LIBRARY})
8476
include_directories(${CARES_INCLUDE_DIR})
8577
link_libraries(${CARES_LIBRARY})
8678
set(TRANTOR_SOURCES ${TRANTOR_SOURCES} trantor/net/inner/AresResolver.cc)
@@ -94,6 +86,12 @@ set_property(TARGET trantor PROPERTY CXX_EXTENSIONS OFF)
9486

9587
if (MAKETEST STREQUAL YES)
9688
add_subdirectory(trantor/tests)
89+
find_package(GTest)
90+
if(GTest_FOUND)
91+
include_directories(${GTEST_INCLUDE_DIRS})
92+
link_libraries(${GTEST_LIBRARIES})
93+
add_subdirectory(trantor/unittests)
94+
endif()
9795
endif ()
9896

9997
set(public_net_headers

trantor/unittests/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
link_libraries(trantor pthread)
2+
add_executable(msgbuffer_unittest MsgBufferUnittest.cc)
3+
add_executable(inetaddress_unittest InetAddressUnittest.cc)
4+
add_executable(date_unittest DateUnittest.cc)
5+
set(UNITTEST_TARGETS msgbuffer_unittest inetaddress_unittest date_unittest)
6+
set_property(TARGET ${UNITTEST_TARGETS} PROPERTY CXX_STANDARD 14)
7+
set_property(TARGET ${UNITTEST_TARGETS} PROPERTY CXX_STANDARD_REQUIRED ON)
8+
set_property(TARGET ${UNITTEST_TARGETS} PROPERTY CXX_EXTENSIONS OFF)

trantor/unittests/DateUnittest.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <trantor/utils/Date.h>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include <iostream>
5+
using namespace trantor;
6+
TEST(Date, constructorTest)
7+
{
8+
EXPECT_STREQ("1985-01-01 00:00:00",
9+
trantor::Date(1985, 1, 1).toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S").c_str());
10+
EXPECT_STREQ("2004-02-29 00:00:00.000000",
11+
trantor::Date(2004, 2, 29).toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true).c_str());
12+
EXPECT_STRNE("2001-02-29 00:00:00.000000",
13+
trantor::Date(2001, 2, 29).toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true).c_str());
14+
EXPECT_STREQ("2018-01-01 00:00:00.000000",
15+
trantor::Date(2018, 1, 1, 12, 12, 12, 2321).roundDay().toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true).c_str());
16+
}
17+
int main(int argc, char **argv)
18+
{
19+
testing::InitGoogleTest(&argc, argv);
20+
return RUN_ALL_TESTS();
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <trantor/net/InetAddress.h>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include <iostream>
5+
using namespace trantor;
6+
TEST(InetAddress, innerIpTest)
7+
{
8+
EXPECT_EQ(true,InetAddress("192.168.0.1",0).isIntranetIp());
9+
EXPECT_EQ(true,InetAddress("192.168.12.1",0).isIntranetIp());
10+
EXPECT_EQ(true,InetAddress("10.168.0.1",0).isIntranetIp());
11+
EXPECT_EQ(true,InetAddress("10.0.0.1",0).isIntranetIp());
12+
EXPECT_EQ(true,InetAddress("172.31.10.1",0).isIntranetIp());
13+
EXPECT_EQ(true,InetAddress("127.0.0.1",0).isIntranetIp());
14+
}
15+
int main( int argc, char **argv)
16+
{
17+
testing::InitGoogleTest(&argc, argv);
18+
return RUN_ALL_TESTS();
19+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <trantor/utils/MsgBuffer.h>
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include <iostream>
5+
using namespace trantor;
6+
TEST(MsgBufferTest, readableTest)
7+
{
8+
MsgBuffer buffer;
9+
10+
EXPECT_EQ(0,buffer.readableBytes());
11+
buffer.append(std::string(128,'a'));
12+
EXPECT_EQ(128,buffer.readableBytes());
13+
buffer.retrieve(100);
14+
EXPECT_EQ(28,buffer.readableBytes());
15+
EXPECT_EQ('a',buffer.peekInt8());
16+
buffer.retrieveAll();
17+
EXPECT_EQ(0,buffer.readableBytes());
18+
}
19+
TEST(MsgBufferTest, writableTest)
20+
{
21+
MsgBuffer buffer(100);
22+
23+
EXPECT_EQ(100,buffer.writableBytes());
24+
buffer.append("abcde");
25+
EXPECT_EQ(95,buffer.writableBytes());
26+
buffer.append(std::string(100,'x'));
27+
EXPECT_EQ(111,buffer.writableBytes());
28+
buffer.retrieve(100);
29+
EXPECT_EQ(111,buffer.writableBytes());
30+
buffer.append(std::string(112,'c'));
31+
EXPECT_EQ(99,buffer.writableBytes());
32+
buffer.retrieveAll();
33+
EXPECT_EQ(216,buffer.writableBytes());
34+
}
35+
36+
TEST(MsgBufferTest, addInFrontTest)
37+
{
38+
MsgBuffer buffer(100);
39+
40+
EXPECT_EQ(100,buffer.writableBytes());
41+
buffer.addInFrontInt8('a');
42+
EXPECT_EQ(100,buffer.writableBytes());
43+
buffer.addInFrontInt64(123);
44+
EXPECT_EQ(92,buffer.writableBytes());
45+
buffer.addInFrontInt64(100);
46+
EXPECT_EQ(84,buffer.writableBytes());
47+
buffer.addInFrontInt8(1);
48+
EXPECT_EQ(84,buffer.writableBytes());
49+
}
50+
51+
TEST(MsgBuffer,MoveContrustor)
52+
{
53+
54+
MsgBuffer buf1(100);
55+
const char *bufptr1=buf1.peek();
56+
MsgBuffer buffnew1=std::move(buf1);
57+
EXPECT_EQ(bufptr1,buffnew1.peek());
58+
59+
MsgBuffer buf2(100);
60+
const char *bufptr2=buf2.peek();
61+
MsgBuffer buffnew2(std::move(buf2));
62+
EXPECT_EQ(bufptr2,buffnew2.peek());
63+
64+
}
65+
66+
TEST(Msgbuffer, MoveAssignmentOperator)
67+
{
68+
MsgBuffer buf(100);
69+
const char *bufptr=buf.peek();
70+
size_t writable=buf.writableBytes();
71+
MsgBuffer buffnew(1000);
72+
buffnew=std::move(buf);
73+
EXPECT_EQ(bufptr,buffnew.peek());
74+
EXPECT_EQ(writable,buffnew.writableBytes());
75+
}
76+
int main( int argc, char **argv)
77+
{
78+
testing::InitGoogleTest(&argc, argv);
79+
return RUN_ALL_TESTS();
80+
}

0 commit comments

Comments
 (0)