From cb99919912c4af4eddfab6769ee0419256cf5f6a Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 7 Dec 2012 09:02:15 -0800 Subject: [PATCH] Add tests. --- .gitmodules | 3 +++ CMakeLists.txt | 11 +++++++++++ format_test.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ gtest | 1 + 4 files changed, 57 insertions(+) create mode 100644 .gitmodules create mode 100644 format_test.cc create mode 160000 gtest diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..122954d5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "gtest"] + path = gtest + url = git@github.com:vitaut/gtest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index d4c6eaf1..b177b596 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,14 @@ cmake_minimum_required(VERSION 2.6) project(FORMAT) + add_library(format format.cc) + +if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt) + add_subdirectory(gtest) + include_directories(gtest/include) + link_directories(${CMAKE_CURRENT_BINARY_DIR}/gtest) + enable_testing() + add_executable(format_test format_test.cc gtest/src/gtest_main.cc) + target_link_libraries(format_test format gtest) + add_test(format_test format_test) +endif () diff --git a/format_test.cc b/format_test.cc new file mode 100644 index 00000000..41866efd --- /dev/null +++ b/format_test.cc @@ -0,0 +1,42 @@ +/* + Formatting library tests. + Author: Victor Zverovich + */ + +#include +#include +#include +#include +#include "format.h" + +using std::size_t; +using std::sprintf; + +using fmt::Formatter; +using fmt::Format; + +TEST(FormatterTest, FormatNoArgs) { + Formatter format; + format("test"); + EXPECT_STREQ("test", format.c_str()); +} + +TEST(FormatterTest, FormatComplex) { + EXPECT_STREQ("1.2340000000:0042:+3.13:str:0x3e8:X:%", + c_str(Format("{0:0.10f}:{1:04}:{2:+g}:{3}:{4}:{5}:%") + << 1.234 << 42 << 3.13 << "str" << reinterpret_cast(1000) + << 'X')); + printf("%0.*f:%04d:%+g:%s:%p:%c:%%\n", + 10, 1.234, 42, 3.13, "str", (void*)1000, (int)'X'); + printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n", + 1.234, 42, 3.13, "str", (void*)1000, (int)'X'); +} + +TEST(FormatterTest, FormatInt) { + EXPECT_STREQ("42", c_str(Format("{0}") << 42)); + EXPECT_STREQ("before 42 after", c_str(Format("before {0} after") << 42)); + printf("%0.10f:%04d:%+g:%s:%p:%c:%%\n", + 1.234, 42, 3.13, "str", (void*)1000, (int)'X'); +} + +// TODO diff --git a/gtest b/gtest new file mode 160000 index 00000000..2084bf04 --- /dev/null +++ b/gtest @@ -0,0 +1 @@ +Subproject commit 2084bf04aa7b22808bf449cf72dedf7774776feb