#include "../tmain.hpp" /* Following are valid type specifications and their meaning: b boolean (true = 1, false = 0) c character d signed decimal integer i signed decimal integer o unsigned octal integer u unsigned decimal integer x unsigned hexadecimal integer (lower case) X unsigned hexadecimal integer (upper case) e signed floating-point value in the form [-]d.dddde[<sign>]dd[d] E signed floating-point value in the form [-]d.ddddE[<sign>]dd[d] f signed floating-point value in the form [-]dddd.dddd s std::string z std::size_t The following flags are supported: - left align the result within the given field width + prefix the output value with a sign (+ or -) if the output value is of a signed type 0 if width is prefixed with 0, zeros are added until the minimum width is reached # For o, x, X, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively; for e, E, f, the # flag forces the output value to contain a decimal point in all cases. The following modifiers are supported: (none) argument is char (c), int (d, i), unsigned (o, u, x, X) double (e, E, f, g, G) or string (s) l argument is long (d, i), unsigned long (o, u, x, X) or long double (e, E, f, g, G) L argument is long long (d, i), unsigned long long (o, u, x, X) h argument is short (d, i), unsigned short (o, u, x, X) or float (e, E, f, g, G) ? argument is any signed or unsigned int, short, long, or 64-bit integer (d, i, o, x, X) */ void test_FormatChar() { char c = 'a'; std::string s(Poco::format("%c", c)); assert (s == "a"); s = Poco::format("%2c", c); assert (s == " a"); s = Poco::format("%-2c", c); assert (s == "a "); s = Poco::format("%c", std::string("foo")); assert (s == "[ERRFMT]"); } void test_FormatInt() { int i = 42; std::string s(Poco::format("%d", i)); assert (s == "42"); s = Poco::format("%4d", i); assert (s == " 42"); s = Poco::format("%04d", i); assert (s == "0042"); unsigned int x = 0xaa; s = Poco::format("%x", x); assert (s == "aa"); s = Poco::format("%X", x); assert (s == "AA"); i = 42; s = Poco::format("%+d", i); assert (s == "+42"); } void test_FormatBoolean() { bool b = true; std::string s = Poco::format("%b", b); assert (s == "1"); b = false; s = Poco::format("%b", b); assert (s == "0"); std::vector<Poco::Any> bv; bv.push_back(false); bv.push_back(true); bv.push_back(false); bv.push_back(true); bv.push_back(false); bv.push_back(true); bv.push_back(false); bv.push_back(true); bv.push_back(false); bv.push_back(true); s.clear(); Poco::format(s, "%b%b%b%b%b%b%b%b%b%b", bv); assert (s == "0101010101"); } void test_FormatFloatFix() { double d = 1.5; std::string s(Poco::format("%f", d)); assert (s.find("1.50") == 0); s = Poco::format("%10f", d); assert (s.find(" 1.50") != std::string::npos); s = Poco::format("%6.2f", d); assert (s == " 1.50"); s = Poco::format("%-6.2f", d); assert (s == "1.50 "); float f = 1.5; s = Poco::format("%hf", f); assert (s.find("1.50") == 0); } void test_FormatFloatSci() { double d = 1.5; std::string s(Poco::format("%e", d)); assert (s.find("1.50") == 0); assert (s.find("0e+0") != std::string::npos); s = Poco::format("%20e", d); assert (s.find(" 1.50") != std::string::npos); assert (s.find("0e+0") != std::string::npos); s = Poco::format("%10.2e", d); assert (s == " 1.50e+000" || s == " 1.50e+00"); s = Poco::format("%-10.2e", d); assert (s == "1.50e+000 " || s == "1.50e+00 "); s = Poco::format("%-10.2E", d); assert (s == "1.50E+000 " || s == "1.50E+00 "); } void test_FormatString() { std::string foo("foo"); std::string s(Poco::format("%s", foo)); assert (s == "foo"); s = Poco::format("%5s", foo); assert (s == " foo"); s = Poco::format("%-5s", foo); assert (s == "foo "); s = Poco::format("%s%%a", foo); assert (s == "foo%a"); s = Poco::format("'%s%%''%s%%'", foo, foo); assert (s == "'foo%''foo%'"); } void test_FormateMutil() { std::string s(Poco::format("aaa%dbbb%4dccc", 1, 2)); assert (s == "aaa1bbb 2ccc"); s = Poco::format("%%%d%%%d%%%d", 1, 2, 3); assert (s == "%1%2%3"); s = Poco::format("%d%d%d%d", 1, 2, 3, 4); assert (s == "1234"); s = Poco::format("%d%d%d%d%d", 1, 2, 3, 4, 5); assert (s == "12345"); s = Poco::format("%d%d%d%d%d%d", 1, 2, 3, 4, 5, 6); assert (s == "123456"); } void test_FormatIndex() { std::string s(Poco::format("%[1]d%[0]d", 1, 2)); assert (s == "21"); s = Poco::format("%[5]d%[4]d%[3]d%[2]d%[1]d%[0]d", 1, 2, 3, 4, 5, 6); assert (s == "654321"); s = Poco::format("%%%[1]d%%%[2]d%%%d", 1, 2, 3); assert (s == "%2%3%1"); }