Add tests.

This commit is contained in:
Victor Zverovich 2012-12-07 09:02:15 -08:00
parent 15755cfa69
commit cb99919912
4 changed files with 57 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "gtest"]
path = gtest
url = git@github.com:vitaut/gtest.git

View File

@ -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 ()

42
format_test.cc Normal file
View File

@ -0,0 +1,42 @@
/*
Formatting library tests.
Author: Victor Zverovich
*/
#include <cfloat>
#include <climits>
#include <cstring>
#include <gtest/gtest.h>
#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<void*>(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

1
gtest Submodule

@ -0,0 +1 @@
Subproject commit 2084bf04aa7b22808bf449cf72dedf7774776feb