More tests.

This commit is contained in:
Victor Zverovich 2014-06-10 06:21:41 -07:00
parent ed19147441
commit 546b62e74f
4 changed files with 39 additions and 16 deletions

View File

@ -29,7 +29,6 @@
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdarg>
#include <cstring>
#include <fstream>
#include <iomanip>
@ -109,17 +108,6 @@ struct WriteChecker {
#define CHECK_WRITE_WCHAR(value) \
EXPECT_PRED_FORMAT1(WriteChecker<wchar_t>(), value)
#ifdef _MSC_VER
# define vsnprintf vsprintf_s
#endif
void SPrintf(char *buffer, const char *format, ...) {
std::va_list args;
va_start(args, format);
vsnprintf(buffer, BUFFER_SIZE, format, args);
va_end(args);
}
std::string ReadFile(StringRef filename) {
std::ifstream out(filename.c_str());
std::stringstream content;

View File

@ -165,6 +165,9 @@ TEST(PrintfTest, PlusFlag) {
EXPECT_PRINTF("-42", "%+d", -42);
EXPECT_PRINTF("+0042", "%+05d", 42);
EXPECT_PRINTF("+0042", "%0++5d", 42);
// '+' flag is ignored for non-numeric types.
EXPECT_PRINTF("x", "%+c", 'x');
}
TEST(PrintfTest, MinusFlag) {
@ -177,6 +180,9 @@ TEST(PrintfTest, SpaceFlag) {
EXPECT_PRINTF("-42", "% d", -42);
EXPECT_PRINTF(" 0042", "% 05d", 42);
EXPECT_PRINTF(" 0042", "%0 5d", 42);
// ' ' flag is ignored for non-numeric types.
EXPECT_PRINTF("x", "% c", 'x');
}
TEST(PrintfTest, HashFlag) {
@ -189,15 +195,29 @@ TEST(PrintfTest, HashFlag) {
EXPECT_PRINTF("-0x42", "%#x", -0x42);
EXPECT_PRINTF("0", "%#x", 0);
EXPECT_PRINTF("0x0042", "%#06x", 0x42);
EXPECT_PRINTF("0x0042", "%0##6x", 0x42);
EXPECT_PRINTF("-42.0000", "%#g", -42.0);
//EXPECT_PRINTF("-4.200000e+01", "%#e", -42.0);
EXPECT_PRINTF("-42.000000", "%#f", -42.0);
EXPECT_PRINTF("-42.000000", "%#F", -42.0);
// TODO: more tests
char buffer[BUFFER_SIZE];
SPrintf(buffer, "%#e", -42.0);
EXPECT_PRINTF(buffer, "%#e", -42.0);
SPrintf(buffer, "%#E", -42.0);
EXPECT_PRINTF(buffer, "%#E", -42.0);
EXPECT_PRINTF("-42.0000", "%#g", -42.0);
EXPECT_PRINTF("-42.0000", "%#G", -42.0);
// TODO
//EXPECT_PRINTF("-0x1.5p+5", "%#a", -42.0);
//EXPECT_PRINTF("-0x1.5A+5", "%#A", -42.0);
// '#' flag is ignored for non-numeric types.
EXPECT_PRINTF("x", "%#c", 'x');
}
// TODO: test '*' width, precision, length and type specifier
#endif

View File

@ -26,8 +26,21 @@
*/
#include "util.h"
#include <cstdarg>
#include <cstdio>
#include <cstring>
#ifdef _MSC_VER
# define vsnprintf vsprintf_s
#endif
void SPrintf(char *buffer, const char *format, ...) {
std::va_list args;
va_start(args, format);
vsnprintf(buffer, BUFFER_SIZE, format, args);
va_end(args);
}
void Increment(char *s) {
for (int i = static_cast<int>(std::strlen(s)) - 1; i >= 0; --i) {
if (s[i] != '9') {

View File

@ -27,5 +27,7 @@
enum {BUFFER_SIZE = 256};
void SPrintf(char *buffer, const char *format, ...);
// Increment a number in a string.
void Increment(char *s);