Skip to content

Commit

Permalink
add logging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuo committed May 20, 2012
1 parent c76b6f1 commit e01433a
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ find_path(CARES_INCLUDE_DIR ares.h)
find_library(CARES_LIBRARY NAMES cares)
find_path(MHD_INCLUDE_DIR microhttpd.h)
find_library(MHD_LIBRARY NAMES microhttpd)
find_library(BOOSTTEST_LIBRARY NAMES boost_unit_test_framework)

include_directories(${Boost_INCLUDE_DIRS})

Expand Down
2 changes: 1 addition & 1 deletion muduo/base/LogStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FixedBuffer : boost::noncopyable
void append(const char* /*restrict*/ buf, size_t len)
{
// FIXME: append partially
if (cur_+len < end())
if (implicit_cast<size_t>(avail()) > len)
{
memcpy(cur_, buf, len);
cur_ += len;
Expand Down
11 changes: 11 additions & 0 deletions muduo/base/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ target_link_libraries(fileutil_test muduo_base)
add_executable(fork_test Fork_test.cc)
target_link_libraries(fork_test muduo_base)

add_executable(logging_test Logging_test.cc)
target_link_libraries(logging_test muduo_base)

add_executable(logstream_bench LogStream_bench.cc)
target_link_libraries(logstream_bench muduo_base)

if(BOOSTTEST_LIBRARY)
add_executable(logstream_test LogStream_test.cc)
target_link_libraries(logstream_test muduo_base boost_unit_test_framework)
endif()

add_executable(mutex_test Mutex_test.cc)
target_link_libraries(mutex_test muduo_base)

Expand Down
82 changes: 82 additions & 0 deletions muduo/base/tests/LogStream_bench.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <muduo/base/LogStream.h>
#include <muduo/base/Timestamp.h>

#include <sstream>
#include <stdio.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>

using namespace muduo;

const int N = 1000000;

#pragma GCC diagnostic ignored "-Wold-style-cast"

template<typename T>
void benchPrintf(const char* fmt)
{
char buf[32];
Timestamp start(Timestamp::now());
for (int i = 0; i < N; ++i)
snprintf(buf, sizeof buf, fmt, (T)(i));
Timestamp end(Timestamp::now());

printf("benchPrintf %f\n", timeDifference(end, start));
}

template<typename T>
void benchStringStream()
{
Timestamp start(Timestamp::now());
std::ostringstream os;

for (int i = 0; i < N; ++i)
{
os << (T)(i);
os.seekp(0, std::ios_base::beg);
}
Timestamp end(Timestamp::now());

printf("benchStringStream %f\n", timeDifference(end, start));
}

template<typename T>
void benchLogStream()
{
Timestamp start(Timestamp::now());
LogStream os;
for (int i = 0; i < N; ++i)
{
os << (T)(i);
os.resetBuffer();
}
Timestamp end(Timestamp::now());

printf("benchLogStream %f\n", timeDifference(end, start));
}

int main()
{
benchPrintf<int>("%d");

puts("int");
benchPrintf<int>("%d");
benchStringStream<int>();
benchLogStream<int>();

puts("double");
benchPrintf<double>("%.12g");
benchStringStream<double>();
benchLogStream<double>();

puts("int64_t");
benchPrintf<int64_t>("%" PRId64);
benchStringStream<int64_t>();
benchLogStream<int64_t>();

puts("void*");
benchPrintf<void*>("%p");
benchStringStream<void*>();
benchLogStream<void*>();

}
235 changes: 235 additions & 0 deletions muduo/base/tests/LogStream_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
#include <muduo/base/LogStream.h>

#include <limits>
#include <stdint.h>

//#define BOOST_TEST_MODULE LogStreamTest
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

using muduo::string;

BOOST_AUTO_TEST_CASE(testLogStreamBooleans)
{
muduo::LogStream os;
const muduo::LogStream::Buffer& buf = os.buffer();
BOOST_CHECK_EQUAL(buf.asString(), string(""));
os << true;
BOOST_CHECK_EQUAL(buf.asString(), string("1"));
os << '\n';
BOOST_CHECK_EQUAL(buf.asString(), string("1\n"));
os << false;
BOOST_CHECK_EQUAL(buf.asString(), string("1\n0"));
}

BOOST_AUTO_TEST_CASE(testLogStreamIntegers)
{
muduo::LogStream os;
const muduo::LogStream::Buffer& buf = os.buffer();
BOOST_CHECK_EQUAL(buf.asString(), string(""));
os << 1;
BOOST_CHECK_EQUAL(buf.asString(), string("1"));
os << 0;
BOOST_CHECK_EQUAL(buf.asString(), string("10"));
os << -1;
BOOST_CHECK_EQUAL(buf.asString(), string("10-1"));
os.resetBuffer();

os << 0 << " " << 123 << 'x' << 0x64;
BOOST_CHECK_EQUAL(buf.asString(), string("0 123x100"));
}

BOOST_AUTO_TEST_CASE(testLogStreamIntegerLimits)
{
muduo::LogStream os;
const muduo::LogStream::Buffer& buf = os.buffer();
os << -2147483647;
BOOST_CHECK_EQUAL(buf.asString(), string("-2147483647"));
os << static_cast<int>(-2147483648);
BOOST_CHECK_EQUAL(buf.asString(), string("-2147483647-2147483648"));
os << ' ';
os << 2147483647;
BOOST_CHECK_EQUAL(buf.asString(), string("-2147483647-2147483648 2147483647"));
os.resetBuffer();

os << std::numeric_limits<int16_t>::min();
BOOST_CHECK_EQUAL(buf.asString(), string("-32768"));
os.resetBuffer();

os << std::numeric_limits<int16_t>::max();
BOOST_CHECK_EQUAL(buf.asString(), string("32767"));
os.resetBuffer();

os << std::numeric_limits<unsigned int16_t>::min();
BOOST_CHECK_EQUAL(buf.asString(), string("0"));
os.resetBuffer();

os << std::numeric_limits<unsigned int16_t>::max();
BOOST_CHECK_EQUAL(buf.asString(), string("65535"));
os.resetBuffer();

os << std::numeric_limits<int32_t>::min();
BOOST_CHECK_EQUAL(buf.asString(), string("-2147483648"));
os.resetBuffer();

os << std::numeric_limits<int32_t>::max();
BOOST_CHECK_EQUAL(buf.asString(), string("2147483647"));
os.resetBuffer();

os << std::numeric_limits<unsigned int32_t>::min();
BOOST_CHECK_EQUAL(buf.asString(), string("0"));
os.resetBuffer();

os << std::numeric_limits<unsigned int32_t>::max();
BOOST_CHECK_EQUAL(buf.asString(), string("4294967295"));
os.resetBuffer();

os << std::numeric_limits<int64_t>::min();
BOOST_CHECK_EQUAL(buf.asString(), string("-9223372036854775808"));
os.resetBuffer();

os << std::numeric_limits<int64_t>::max();
BOOST_CHECK_EQUAL(buf.asString(), string("9223372036854775807"));
os.resetBuffer();

os << std::numeric_limits<unsigned int64_t>::min();
BOOST_CHECK_EQUAL(buf.asString(), string("0"));
os.resetBuffer();

os << std::numeric_limits<unsigned int64_t>::max();
BOOST_CHECK_EQUAL(buf.asString(), string("18446744073709551615"));
os.resetBuffer();

int16_t a = 0;
int32_t b = 0;
int64_t c = 0;
os << a;
os << b;
os << c;
BOOST_CHECK_EQUAL(buf.asString(), string("000"));
}

BOOST_AUTO_TEST_CASE(testLogStreamFloats)
{
muduo::LogStream os;
const muduo::LogStream::Buffer& buf = os.buffer();

os << 0.0;
BOOST_CHECK_EQUAL(buf.asString(), string("0"));
os.resetBuffer();

os << 1.0;
BOOST_CHECK_EQUAL(buf.asString(), string("1"));
os.resetBuffer();

os << 0.1;
BOOST_CHECK_EQUAL(buf.asString(), string("0.1"));
os.resetBuffer();

os << 0.05;
BOOST_CHECK_EQUAL(buf.asString(), string("0.05"));
os.resetBuffer();

os << 0.15;
BOOST_CHECK_EQUAL(buf.asString(), string("0.15"));
os.resetBuffer();

double a = 0.1;
os << a;
BOOST_CHECK_EQUAL(buf.asString(), string("0.1"));
os.resetBuffer();

double b = 0.05;
os << b;
BOOST_CHECK_EQUAL(buf.asString(), string("0.05"));
os.resetBuffer();

double c = 0.15;
os << c;
BOOST_CHECK_EQUAL(buf.asString(), string("0.15"));
os.resetBuffer();

os << a+b;
BOOST_CHECK_EQUAL(buf.asString(), string("0.15"));
os.resetBuffer();

BOOST_CHECK(a+b != c);

os << 1.23456789;
BOOST_CHECK_EQUAL(buf.asString(), string("1.23456789"));
os.resetBuffer();

os << 1.234567;
BOOST_CHECK_EQUAL(buf.asString(), string("1.234567"));
os.resetBuffer();

os << -123.456;
BOOST_CHECK_EQUAL(buf.asString(), string("-123.456"));
os.resetBuffer();
}

BOOST_AUTO_TEST_CASE(testLogStreamVoid)
{
muduo::LogStream os;
const muduo::LogStream::Buffer& buf = os.buffer();

os << static_cast<void*>(0);
BOOST_CHECK_EQUAL(buf.asString(), string("0x0"));
os.resetBuffer();

os << reinterpret_cast<void*>(8888);
BOOST_CHECK_EQUAL(buf.asString(), string("0x22B8"));
os.resetBuffer();
}

BOOST_AUTO_TEST_CASE(testLogStreamStrings)
{
muduo::LogStream os;
const muduo::LogStream::Buffer& buf = os.buffer();

os << "Hello ";
BOOST_CHECK_EQUAL(buf.asString(), string("Hello "));

string chenshuo = "Shuo Chen";
os << chenshuo;
BOOST_CHECK_EQUAL(buf.asString(), string("Hello Shuo Chen"));
}

BOOST_AUTO_TEST_CASE(testLogStreamFmts)
{
muduo::LogStream os;
const muduo::LogStream::Buffer& buf = os.buffer();

os << muduo::Fmt("%4d", 1);
BOOST_CHECK_EQUAL(buf.asString(), string(" 1"));
os.resetBuffer();

os << muduo::Fmt("%4.2f", 1.2);
BOOST_CHECK_EQUAL(buf.asString(), string("1.20"));
os.resetBuffer();

os << muduo::Fmt("%4.2f", 1.2) << muduo::Fmt("%4d", 43);
BOOST_CHECK_EQUAL(buf.asString(), string("1.20 43"));
os.resetBuffer();
}

BOOST_AUTO_TEST_CASE(testLogStreamLong)
{
muduo::LogStream os;
const muduo::LogStream::Buffer& buf = os.buffer();
for (int i = 0; i < 399; ++i)
{
os << "123456789 ";
BOOST_CHECK_EQUAL(buf.length(), 10*(i+1));
BOOST_CHECK_EQUAL(buf.avail(), 4000 - 10*(i+1));
}

os << "abcdefghi ";
BOOST_CHECK_EQUAL(buf.length(), 3990);
BOOST_CHECK_EQUAL(buf.avail(), 10);

os << "abcdefghi";
BOOST_CHECK_EQUAL(buf.length(), 3999);
BOOST_CHECK_EQUAL(buf.avail(), 1);
}
Loading

0 comments on commit e01433a

Please sign in to comment.