| 103 | |
| 104 | |
| 105 | int unitTests() |
| 106 | { |
| 107 | int nfailed = 0; |
| 108 | # if defined(_MSC_VER) && _MSC_VER < 1900 // VC++ older than 2015 |
| 109 | // floats are printed with three digit exponents on windows, which messes |
| 110 | // up the tests. Turn this off for consistency: |
| 111 | _set_output_format(_TWO_DIGIT_EXPONENT); |
| 112 | # endif |
| 113 | |
| 114 | //------------------------------------------------------------ |
| 115 | // Test various basic format specs against results of sprintf |
| 116 | CHECK_EQUAL(tfm::format("%s", "asdf"), "asdf"); |
| 117 | CHECK_EQUAL(tfm::format("%d", 1234), "1234"); |
| 118 | CHECK_EQUAL(tfm::format("%i", -5678), "-5678"); |
| 119 | CHECK_EQUAL(tfm::format("%o", 012), "12"); |
| 120 | CHECK_EQUAL(tfm::format("%u", 123456u), "123456"); |
| 121 | CHECK_EQUAL(tfm::format("%x", 0xdeadbeef), "deadbeef"); |
| 122 | CHECK_EQUAL(tfm::format("%X", 0xDEADBEEF), "DEADBEEF"); |
| 123 | CHECK_EQUAL(tfm::format("%e", 1.23456e10), "1.234560e+10"); |
| 124 | CHECK_EQUAL(tfm::format("%E", -1.23456E10), "-1.234560E+10"); |
| 125 | CHECK_EQUAL(tfm::format("%f", -9.8765), "-9.876500"); |
| 126 | CHECK_EQUAL(tfm::format("%F", 9.8765), "9.876500"); |
| 127 | # ifndef _MSC_VER |
| 128 | CHECK_EQUAL(tfm::format("%a", -1.671111047267913818359375), "-0x1.abcdefp+0"); |
| 129 | CHECK_EQUAL(tfm::format("%A", 1.671111047267913818359375), "0X1.ABCDEFP+0"); |
| 130 | # else |
| 131 | CHECK_EQUAL(tfm::format("%a", -1.671111047267913818359375), "-0x1.abcdef0000000p+0"); |
| 132 | CHECK_EQUAL(tfm::format("%A", 1.671111047267913818359375), "0X1.ABCDEF0000000P+0"); |
| 133 | # endif |
| 134 | CHECK_EQUAL(tfm::format("%g", 10), "10"); |
| 135 | CHECK_EQUAL(tfm::format("%G", 100), "100"); |
| 136 | CHECK_EQUAL(tfm::format("%c", 65), "A"); |
| 137 | CHECK_EQUAL(tfm::format("%hc", (short)65), "A"); |
| 138 | CHECK_EQUAL(tfm::format("%lc", (long)65), "A"); |
| 139 | CHECK_EQUAL(tfm::format("%s", "asdf_123098"), "asdf_123098"); |
| 140 | // Test "%%" - (note plain "%%" format gives warning with gcc printf) |
| 141 | CHECK_EQUAL(tfm::format("%%%s", "asdf"), "%asdf"); |
| 142 | // Check that 0-argument formatting is printf-compatible |
| 143 | CHECK_EQUAL(tfm::format("100%%"), "100%"); |
| 144 | |
| 145 | // Test printing of pointers. Note that there's no standard numerical |
| 146 | // representation so this is platform and OS dependent. |
| 147 | // |
| 148 | // Also test special case when %p is used with `char*` pointer types. In |
| 149 | // this case the implementation needs to take care to cast to void* |
| 150 | // before invoking `operator<<`; it must not dereference the pointer and |
| 151 | // print as a string. |
| 152 | # ifdef _MSC_VER |
| 153 | # ifdef _WIN64 |
| 154 | CHECK_EQUAL(tfm::format("%p", (void*)0x12345), "0000000000012345"); |
| 155 | CHECK_EQUAL(tfm::format("%p", (const char*)0x10), "0000000000000010"); |
| 156 | # else |
| 157 | CHECK_EQUAL(tfm::format("%p", (void*)0x12345), "00012345"); |
| 158 | CHECK_EQUAL(tfm::format("%p", (const char*)0x10), "00000010"); |
| 159 | # endif // _WIN64 |
| 160 | # else |
| 161 | CHECK_EQUAL(tfm::format("%p", (void*)0x12345), "0x12345"); |
| 162 | CHECK_EQUAL(tfm::format("%p", (const char*)0x10), "0x10"); |