/////////////////////////////////////////////////////////////////////////////// // \author (c) Marco Paland (info@paland.com) // 2017, PALANDesign Hannover, Germany // // \license The MIT License (MIT) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // // \brief printf unit tests // /////////////////////////////////////////////////////////////////////////////// // use the 'catch' test framework #define CATCH_CONFIG_MAIN #include "catch.hpp" #include namespace test { // use functions in own test namespace to avoid stdio conflicts #include "../printf.h" #include "../printf.cpp" } // namespace test // dummy putchar int test::_putchar(char) { return 0; } TEST_CASE("printf", "[]" ) { char buffer[100] ; test::sprintf(buffer, "%.*f", 2, 0.33333333); REQUIRE(!strcmp(buffer, "0.33")); test::sprintf(buffer, "%.3s", "foobar"); REQUIRE(!strcmp(buffer, "foo")); test::sprintf(buffer, "%10.5d", 4); REQUIRE(!strcmp(buffer, " 00004")); test::sprintf(buffer, "%*sx", -3, "hi"); REQUIRE(!strcmp(buffer, "hi x")); } TEST_CASE("space flag", "[]" ) { char buffer[100] ; test::sprintf(buffer, "% d", 42); REQUIRE(!strcmp(buffer, " 42")); test::sprintf(buffer, "% d", -42); REQUIRE(!strcmp(buffer, "-42")); test::sprintf(buffer, "% 5d", 42); REQUIRE(!strcmp(buffer, " 42")); test::sprintf(buffer, "% 5d", -42); REQUIRE(!strcmp(buffer, " -42")); test::sprintf(buffer, "% 15d", 42); REQUIRE(!strcmp(buffer, " 42")); test::sprintf(buffer, "% 15d", -42); REQUIRE(!strcmp(buffer, " -42")); test::sprintf(buffer, "% 15d", -42); REQUIRE(!strcmp(buffer, " -42")); test::sprintf(buffer, "% s", "Hello testing"); REQUIRE(!strcmp(buffer, "Hello testing")); test::sprintf(buffer, "% d", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "% d", -1024); REQUIRE(!strcmp(buffer, "-1024")); test::sprintf(buffer, "% i", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "% i", -1024); REQUIRE(!strcmp(buffer, "-1024")); test::sprintf(buffer, "% u", 1024); REQUIRE(!strcmp(buffer, "1024")); test::sprintf(buffer, "% u", 4294966272U); REQUIRE(!strcmp(buffer, "4294966272")); test::sprintf(buffer, "% o", 511); REQUIRE(!strcmp(buffer, "777")); test::sprintf(buffer, "% o", 4294966785U); REQUIRE(!strcmp(buffer, "37777777001")); test::sprintf(buffer, "% x", 305441741); REQUIRE(!strcmp(buffer, "1234abcd")); test::sprintf(buffer, "% x", 3989525555U); REQUIRE(!strcmp(buffer, "edcb5433")); test::sprintf(buffer, "% X", 305441741); REQUIRE(!strcmp(buffer, "1234ABCD")); test::sprintf(buffer, "% X", 3989525555U); REQUIRE(!strcmp(buffer, "EDCB5433")); test::sprintf(buffer, "% c", 'x'); REQUIRE(!strcmp(buffer, "x")); } TEST_CASE("+ flag", "[]" ) { char buffer[100] ; test::sprintf(buffer, "%+d", 42); REQUIRE(!strcmp(buffer, "+42")); test::sprintf(buffer, "%+d", -42); REQUIRE(!strcmp(buffer, "-42")); test::sprintf(buffer, "%+5d", 42); REQUIRE(!strcmp(buffer, " +42")); test::sprintf(buffer, "%+5d", -42); REQUIRE(!strcmp(buffer, " -42")); test::sprintf(buffer, "%+15d", 42); REQUIRE(!strcmp(buffer, " +42")); test::sprintf(buffer, "%+15d", -42); REQUIRE(!strcmp(buffer, " -42")); test::sprintf(buffer, "%+s", "Hello testing"); REQUIRE(!strcmp(buffer, "Hello testing")); test::sprintf(buffer, "%+d", 1024); REQUIRE(!strcmp(buffer, "+1024")); test::sprintf(buffer, "%+d", -1024); REQUIRE(!strcmp(buffer, "-1024")); test::sprintf(buffer, "%+i", 1024); REQUIRE(!strcmp(buffer, "+1024")); test::sprintf(buffer, "%+i", -1024); REQUIRE(!strcmp(buffer, "-1024")); test::sprintf(buffer, "%+u", 1024); REQUIRE(!strcmp(buffer, "1024")); test::sprintf(buffer, "%+u", 4294966272U); REQUIRE(!strcmp(buffer, "4294966272")); test::sprintf(buffer, "%+o", 511); REQUIRE(!strcmp(buffer, "777")); test::sprintf(buffer, "%+o", 4294966785U); REQUIRE(!strcmp(buffer, "37777777001")); test::sprintf(buffer, "%+x", 305441741); REQUIRE(!strcmp(buffer, "1234abcd")); test::sprintf(buffer, "%+x", 3989525555U); REQUIRE(!strcmp(buffer, "edcb5433")); test::sprintf(buffer, "%+X", 305441741); REQUIRE(!strcmp(buffer, "1234ABCD")); test::sprintf(buffer, "%+X", 3989525555U); REQUIRE(!strcmp(buffer, "EDCB5433")); test::sprintf(buffer, "%+c", 'x'); REQUIRE(!strcmp(buffer, "x")); } TEST_CASE("0 flag", "[]" ) { char buffer[100] ; test::sprintf(buffer, "%0d", 42); REQUIRE(!strcmp(buffer, "42")); test::sprintf(buffer, "%0ld", 42L); REQUIRE(!strcmp(buffer, "42")); test::sprintf(buffer, "%0d", -42); REQUIRE(!strcmp(buffer, "-42")); test::sprintf(buffer, "%05d", 42); REQUIRE(!strcmp(buffer, "00042")); test::sprintf(buffer, "%05d", -42); REQUIRE(!strcmp(buffer, "-0042")); test::sprintf(buffer, "%015d", 42); REQUIRE(!strcmp(buffer, "000000000000042")); test::sprintf(buffer, "%015d", -42); REQUIRE(!strcmp(buffer, "-00000000000042")); } TEST_CASE("- flag", "[]" ) { char buffer[100]; test::sprintf(buffer, "%-d", 42); REQUIRE(!strcmp(buffer, "42")); test::sprintf(buffer, "%-d", -42); REQUIRE(!strcmp(buffer, "-42")); test::sprintf(buffer, "%-5d", 42); REQUIRE(!strcmp(buffer, "42 ")); test::sprintf(buffer, "%-5d", -42); REQUIRE(!strcmp(buffer, "-42 ")); test::sprintf(buffer, "%-15d", 42); REQUIRE(!strcmp(buffer, "42 ")); test::sprintf(buffer, "%-15d", -42); REQUIRE(!strcmp(buffer, "-42 ")); test::sprintf(buffer, "%-0d", 42); REQUIRE(!strcmp(buffer, "42")); test::sprintf(buffer, "%-0d", -42); REQUIRE(!strcmp(buffer, "-42")); test::sprintf(buffer, "%-05d", 42); REQUIRE(!strcmp(buffer, "42 ")); test::sprintf(buffer, "%-05d", -42); REQUIRE(!strcmp(buffer, "-42 ")); test::sprintf(buffer, "%-015d", 42); REQUIRE(!strcmp(buffer, "42 ")); test::sprintf(buffer, "%-015d", -42); REQUIRE(!strcmp(buffer, "-42 ")); test::sprintf(buffer, "%0-d", 42); REQUIRE(!strcmp(buffer, "42")); test::sprintf(buffer, "%0-d", -42); REQUIRE(!strcmp(buffer, "-42")); test::sprintf(buffer, "%0-5d", 42); REQUIRE(!strcmp(buffer, "42 ")); test::sprintf(buffer, "%0-5d", -42); REQUIRE(!strcmp(buffer, "-42 ")); test::sprintf(buffer, "%0-15d", 42); REQUIRE(!strcmp(buffer, "42 ")); test::sprintf(buffer, "%0-15d", -42); REQUIRE(!strcmp(buffer, "-42 ")); } TEST_CASE("specifier", "[]" ) { char buffer[100]; test::sprintf(buffer, "Hello testing"); REQUIRE(!strcmp(buffer, "Hello testing")); test::sprintf(buffer, "%s", "Hello testing"); REQUIRE(!strcmp(buffer, "Hello testing")); test::sprintf(buffer, "%d", 1024); REQUIRE(!strcmp(buffer, "1024")); test::sprintf(buffer, "%d", -1024); REQUIRE(!strcmp(buffer, "-1024")); test::sprintf(buffer, "%i", 1024); REQUIRE(!strcmp(buffer, "1024")); test::sprintf(buffer, "%i", -1024); REQUIRE(!strcmp(buffer, "-1024")); test::sprintf(buffer, "%u", 1024); REQUIRE(!strcmp(buffer, "1024")); test::sprintf(buffer, "%u", 4294966272U); REQUIRE(!strcmp(buffer, "4294966272")); test::sprintf(buffer, "%o", 511); REQUIRE(!strcmp(buffer, "777")); test::sprintf(buffer, "%o", 4294966785U); REQUIRE(!strcmp(buffer, "37777777001")); test::sprintf(buffer, "%x", 305441741); REQUIRE(!strcmp(buffer, "1234abcd")); test::sprintf(buffer, "%x", 3989525555U); REQUIRE(!strcmp(buffer, "edcb5433")); test::sprintf(buffer, "%X", 305441741); REQUIRE(!strcmp(buffer, "1234ABCD")); test::sprintf(buffer, "%X", 3989525555U); REQUIRE(!strcmp(buffer, "EDCB5433")); test::sprintf(buffer, "%%"); REQUIRE(!strcmp(buffer, "%")); } TEST_CASE("width", "[]" ) { char buffer[100]; test::sprintf(buffer, "%1s", "Hello testing"); REQUIRE(!strcmp(buffer, "Hello testing")); test::sprintf(buffer, "%1d", 1024); REQUIRE(!strcmp(buffer, "1024")); test::sprintf(buffer, "%1d", -1024); REQUIRE(!strcmp(buffer, "-1024")); test::sprintf(buffer, "%1i", 1024); REQUIRE(!strcmp(buffer, "1024")); test::sprintf(buffer, "%1i", -1024); REQUIRE(!strcmp(buffer, "-1024")); test::sprintf(buffer, "%1u", 1024); REQUIRE(!strcmp(buffer, "1024")); test::sprintf(buffer, "%1u", 4294966272U); REQUIRE(!strcmp(buffer, "4294966272")); test::sprintf(buffer, "%1o", 511); REQUIRE(!strcmp(buffer, "777")); test::sprintf(buffer, "%1o", 4294966785U); REQUIRE(!strcmp(buffer, "37777777001")); test::sprintf(buffer, "%1x", 305441741); REQUIRE(!strcmp(buffer, "1234abcd")); test::sprintf(buffer, "%1x", 3989525555U); REQUIRE(!strcmp(buffer, "edcb5433")); test::sprintf(buffer, "%1X", 305441741); REQUIRE(!strcmp(buffer, "1234ABCD")); test::sprintf(buffer, "%1X", 3989525555U); REQUIRE(!strcmp(buffer, "EDCB5433")); test::sprintf(buffer, "%1c", 'x'); REQUIRE(!strcmp(buffer, "x")); } TEST_CASE("width 20", "[]" ) { char buffer[100]; test::sprintf(buffer, "%20s", "Hello"); REQUIRE(!strcmp(buffer, " Hello")); test::sprintf(buffer, "%20d", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%20d", -1024); REQUIRE(!strcmp(buffer, " -1024")); test::sprintf(buffer, "%20i", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%20i", -1024); REQUIRE(!strcmp(buffer, " -1024")); test::sprintf(buffer, "%20u", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%20u", 4294966272U); REQUIRE(!strcmp(buffer, " 4294966272")); test::sprintf(buffer, "%20o", 511); REQUIRE(!strcmp(buffer, " 777")); test::sprintf(buffer, "%20o", 4294966785U); REQUIRE(!strcmp(buffer, " 37777777001")); test::sprintf(buffer, "%20x", 305441741); REQUIRE(!strcmp(buffer, " 1234abcd")); test::sprintf(buffer, "%20x", 3989525555U); REQUIRE(!strcmp(buffer, " edcb5433")); test::sprintf(buffer, "%20X", 305441741); REQUIRE(!strcmp(buffer, " 1234ABCD")); test::sprintf(buffer, "%20X", 3989525555U); REQUIRE(!strcmp(buffer, " EDCB5433")); test::sprintf(buffer, "%20c", 'x'); REQUIRE(!strcmp(buffer, " x")); } TEST_CASE("width *20", "[]" ) { char buffer[100]; test::sprintf(buffer, "%*s", 20, "Hello"); REQUIRE(!strcmp(buffer, " Hello")); test::sprintf(buffer, "%*d", 20, 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%*d", 20, -1024); REQUIRE(!strcmp(buffer, " -1024")); test::sprintf(buffer, "%*i", 20, 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%*i", 20, -1024); REQUIRE(!strcmp(buffer, " -1024")); test::sprintf(buffer, "%*u", 20, 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%*u", 20, 4294966272U); REQUIRE(!strcmp(buffer, " 4294966272")); test::sprintf(buffer, "%*o", 20, 511); REQUIRE(!strcmp(buffer, " 777")); test::sprintf(buffer, "%*o", 20, 4294966785U); REQUIRE(!strcmp(buffer, " 37777777001")); test::sprintf(buffer, "%*x", 20, 305441741); REQUIRE(!strcmp(buffer, " 1234abcd")); test::sprintf(buffer, "%*x", 20, 3989525555U); REQUIRE(!strcmp(buffer, " edcb5433")); test::sprintf(buffer, "%*X", 20, 305441741); REQUIRE(!strcmp(buffer, " 1234ABCD")); test::sprintf(buffer, "%*X", 20, 3989525555U); REQUIRE(!strcmp(buffer, " EDCB5433")); test::sprintf(buffer, "%*c", 20,'x'); REQUIRE(!strcmp(buffer, " x")); } TEST_CASE("width -20", "[]" ) { char buffer[100]; test::sprintf(buffer, "%-20s", "Hello"); REQUIRE(!strcmp(buffer, "Hello ")); test::sprintf(buffer, "%-20d", 1024); REQUIRE(!strcmp(buffer, "1024 ")); test::sprintf(buffer, "%-20d", -1024); REQUIRE(!strcmp(buffer, "-1024 ")); test::sprintf(buffer, "%-20i", 1024); REQUIRE(!strcmp(buffer, "1024 ")); test::sprintf(buffer, "%-20i", -1024); REQUIRE(!strcmp(buffer, "-1024 ")); test::sprintf(buffer, "%-20u", 1024); REQUIRE(!strcmp(buffer, "1024 ")); test::sprintf(buffer, "%-20u", 4294966272U); REQUIRE(!strcmp(buffer, "4294966272 ")); test::sprintf(buffer, "%-20o", 511); REQUIRE(!strcmp(buffer, "777 ")); test::sprintf(buffer, "%-20o", 4294966785U); REQUIRE(!strcmp(buffer, "37777777001 ")); test::sprintf(buffer, "%-20x", 305441741); REQUIRE(!strcmp(buffer, "1234abcd ")); test::sprintf(buffer, "%-20x", 3989525555U); REQUIRE(!strcmp(buffer, "edcb5433 ")); test::sprintf(buffer, "%-20X", 305441741); REQUIRE(!strcmp(buffer, "1234ABCD ")); test::sprintf(buffer, "%-20X", 3989525555U); REQUIRE(!strcmp(buffer, "EDCB5433 ")); test::sprintf(buffer, "%-20c", 'x'); REQUIRE(!strcmp(buffer, "x ")); } TEST_CASE("width 0-20", "[]" ) { char buffer[100]; test::sprintf(buffer, "%0-20s", "Hello"); REQUIRE(!strcmp(buffer, "Hello ")); test::sprintf(buffer, "%0-20d", 1024); REQUIRE(!strcmp(buffer, "1024 ")); test::sprintf(buffer, "%0-20d", -1024); REQUIRE(!strcmp(buffer, "-1024 ")); test::sprintf(buffer, "%0-20i", 1024); REQUIRE(!strcmp(buffer, "1024 ")); test::sprintf(buffer, "%0-20i", -1024); REQUIRE(!strcmp(buffer, "-1024 ")); test::sprintf(buffer, "%0-20u", 1024); REQUIRE(!strcmp(buffer, "1024 ")); test::sprintf(buffer, "%0-20u", 4294966272U); REQUIRE(!strcmp(buffer, "4294966272 ")); test::sprintf(buffer, "%0-20o", 511); REQUIRE(!strcmp(buffer, "777 ")); test::sprintf(buffer, "%0-20o", 4294966785U); REQUIRE(!strcmp(buffer, "37777777001 ")); test::sprintf(buffer, "%0-20x", 305441741); REQUIRE(!strcmp(buffer, "1234abcd ")); test::sprintf(buffer, "%0-20x", 3989525555U); REQUIRE(!strcmp(buffer, "edcb5433 ")); test::sprintf(buffer, "%0-20X", 305441741); REQUIRE(!strcmp(buffer, "1234ABCD ")); test::sprintf(buffer, "%0-20X", 3989525555U); REQUIRE(!strcmp(buffer, "EDCB5433 ")); test::sprintf(buffer, "%0-20c", 'x'); REQUIRE(!strcmp(buffer, "x ")); } TEST_CASE("padding 20", "[]" ) { char buffer[100]; test::sprintf(buffer, "%020d", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%020d", -1024); REQUIRE(!strcmp(buffer, "-0000000000000001024")); test::sprintf(buffer, "%020i", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%020i", -1024); REQUIRE(!strcmp(buffer, "-0000000000000001024")); test::sprintf(buffer, "%020u", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%020u", 4294966272U); REQUIRE(!strcmp(buffer, "00000000004294966272")); test::sprintf(buffer, "%020o", 511); REQUIRE(!strcmp(buffer, "00000000000000000777")); test::sprintf(buffer, "%020o", 4294966785U); REQUIRE(!strcmp(buffer, "00000000037777777001")); test::sprintf(buffer, "%020x", 305441741); REQUIRE(!strcmp(buffer, "0000000000001234abcd")); test::sprintf(buffer, "%020x", 3989525555U); REQUIRE(!strcmp(buffer, "000000000000edcb5433")); test::sprintf(buffer, "%020X", 305441741); REQUIRE(!strcmp(buffer, "0000000000001234ABCD")); test::sprintf(buffer, "%020X", 3989525555U); REQUIRE(!strcmp(buffer, "000000000000EDCB5433")); } TEST_CASE("padding .20", "[]" ) { char buffer[100]; test::sprintf(buffer, "%.20d", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%.20d", -1024); REQUIRE(!strcmp(buffer, "-00000000000000001024")); test::sprintf(buffer, "%.20i", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%.20i", -1024); REQUIRE(!strcmp(buffer, "-00000000000000001024")); test::sprintf(buffer, "%.20u", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%.20u", 4294966272U); REQUIRE(!strcmp(buffer, "00000000004294966272")); test::sprintf(buffer, "%.20o", 511); REQUIRE(!strcmp(buffer, "00000000000000000777")); test::sprintf(buffer, "%.20o", 4294966785U); REQUIRE(!strcmp(buffer, "00000000037777777001")); test::sprintf(buffer, "%.20x", 305441741); REQUIRE(!strcmp(buffer, "0000000000001234abcd")); test::sprintf(buffer, "%.20x", 3989525555U); REQUIRE(!strcmp(buffer, "000000000000edcb5433")); test::sprintf(buffer, "%.20X", 305441741); REQUIRE(!strcmp(buffer, "0000000000001234ABCD")); test::sprintf(buffer, "%.20X", 3989525555U); REQUIRE(!strcmp(buffer, "000000000000EDCB5433")); } TEST_CASE("padding #020", "[]" ) { char buffer[100]; test::sprintf(buffer, "%#020d", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%#020d", -1024); REQUIRE(!strcmp(buffer, "-0000000000000001024")); test::sprintf(buffer, "%#020i", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%#020i", -1024); REQUIRE(!strcmp(buffer, "-0000000000000001024")); test::sprintf(buffer, "%#020u", 1024); REQUIRE(!strcmp(buffer, "00000000000000001024")); test::sprintf(buffer, "%#020u", 4294966272U); REQUIRE(!strcmp(buffer, "00000000004294966272")); test::sprintf(buffer, "%#020o", 511); REQUIRE(!strcmp(buffer, "00000000000000000777")); test::sprintf(buffer, "%#020o", 4294966785U); REQUIRE(!strcmp(buffer, "00000000037777777001")); test::sprintf(buffer, "%#020x", 305441741); REQUIRE(!strcmp(buffer, "0x00000000001234abcd")); test::sprintf(buffer, "%#020x", 3989525555U); REQUIRE(!strcmp(buffer, "0x0000000000edcb5433")); test::sprintf(buffer, "%#020X", 305441741); REQUIRE(!strcmp(buffer, "0X00000000001234ABCD")); test::sprintf(buffer, "%#020X", 3989525555U); REQUIRE(!strcmp(buffer, "0X0000000000EDCB5433")); } TEST_CASE("padding #20", "[]" ) { char buffer[100]; test::sprintf(buffer, "%#20d", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%#20d", -1024); REQUIRE(!strcmp(buffer, " -1024")); test::sprintf(buffer, "%#20i", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%#20i", -1024); REQUIRE(!strcmp(buffer, " -1024")); test::sprintf(buffer, "%#20u", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%#20u", 4294966272U); REQUIRE(!strcmp(buffer, " 4294966272")); test::sprintf(buffer, "%#20o", 511); REQUIRE(!strcmp(buffer, " 0777")); test::sprintf(buffer, "%#20o", 4294966785U); REQUIRE(!strcmp(buffer, " 037777777001")); test::sprintf(buffer, "%#20x", 305441741); REQUIRE(!strcmp(buffer, " 0x1234abcd")); test::sprintf(buffer, "%#20x", 3989525555U); REQUIRE(!strcmp(buffer, " 0xedcb5433")); test::sprintf(buffer, "%#20X", 305441741); REQUIRE(!strcmp(buffer, " 0X1234ABCD")); test::sprintf(buffer, "%#20X", 3989525555U); REQUIRE(!strcmp(buffer, " 0XEDCB5433")); } TEST_CASE("padding 20.5", "[]" ) { char buffer[100]; test::sprintf(buffer, "%20.5d", 1024); REQUIRE(!strcmp(buffer, " 01024")); test::sprintf(buffer, "%20.5d", -1024); REQUIRE(!strcmp(buffer, " -01024")); test::sprintf(buffer, "%20.5i", 1024); REQUIRE(!strcmp(buffer, " 01024")); test::sprintf(buffer, "%20.5i", -1024); REQUIRE(!strcmp(buffer, " -01024")); test::sprintf(buffer, "%20.5u", 1024); REQUIRE(!strcmp(buffer, " 01024")); test::sprintf(buffer, "%20.5u", 4294966272U); REQUIRE(!strcmp(buffer, " 4294966272")); test::sprintf(buffer, "%20.5o", 511); REQUIRE(!strcmp(buffer, " 00777")); test::sprintf(buffer, "%20.5o", 4294966785U); REQUIRE(!strcmp(buffer, " 37777777001")); test::sprintf(buffer, "%20.5x", 305441741); REQUIRE(!strcmp(buffer, " 1234abcd")); test::sprintf(buffer, "%20.10x", 3989525555U); REQUIRE(!strcmp(buffer, " 00edcb5433")); test::sprintf(buffer, "%20.5X", 305441741); REQUIRE(!strcmp(buffer, " 1234ABCD")); test::sprintf(buffer, "%20.10X", 3989525555U); REQUIRE(!strcmp(buffer, " 00EDCB5433")); } TEST_CASE("length", "[]" ) { char buffer[100]; test::sprintf(buffer, "%.0s", "Hello testing"); REQUIRE(!strcmp(buffer, "")); test::sprintf(buffer, "%20.0s", "Hello testing"); REQUIRE(!strcmp(buffer, " ")); test::sprintf(buffer, "%.s", "Hello testing"); REQUIRE(!strcmp(buffer, "")); test::sprintf(buffer, "%20.s", "Hello testing"); REQUIRE(!strcmp(buffer, " ")); test::sprintf(buffer, "%20.0d", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%20.0d", -1024); REQUIRE(!strcmp(buffer, " -1024")); test::sprintf(buffer, "%20.d", 0); REQUIRE(!strcmp(buffer, " ")); test::sprintf(buffer, "%20.0i", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%20.i", -1024); REQUIRE(!strcmp(buffer, " -1024")); test::sprintf(buffer, "%20.i", 0); REQUIRE(!strcmp(buffer, " ")); test::sprintf(buffer, "%20.u", 1024); REQUIRE(!strcmp(buffer, " 1024")); test::sprintf(buffer, "%20.0u", 4294966272U); REQUIRE(!strcmp(buffer, " 4294966272")); test::sprintf(buffer, "%20.u", 0U); REQUIRE(!strcmp(buffer, " ")); test::sprintf(buffer, "%20.o", 511); REQUIRE(!strcmp(buffer, " 777")); test::sprintf(buffer, "%20.0o", 4294966785U); REQUIRE(!strcmp(buffer, " 37777777001")); test::sprintf(buffer, "%20.o", 0U); REQUIRE(!strcmp(buffer, " ")); test::sprintf(buffer, "%20.x", 305441741); REQUIRE(!strcmp(buffer, " 1234abcd")); test::sprintf(buffer, "%20.0x", 3989525555U); REQUIRE(!strcmp(buffer, " edcb5433")); test::sprintf(buffer, "%20.x", 0U); REQUIRE(!strcmp(buffer, " ")); test::sprintf(buffer, "%20.X", 305441741); REQUIRE(!strcmp(buffer, " 1234ABCD")); test::sprintf(buffer, "%20.0X", 3989525555U); REQUIRE(!strcmp(buffer, " EDCB5433")); test::sprintf(buffer, "%20.X", 0U); REQUIRE(!strcmp(buffer, " ")); } TEST_CASE("float", "[]" ) { char buffer[100]; test::sprintf(buffer, "%.4f", 3.1415354); REQUIRE(!strcmp(buffer, "3.1415")); test::sprintf(buffer, "%.3f", 30343.1415354); REQUIRE(!strcmp(buffer, "30343.142")); test::sprintf(buffer, "%.0f", 34.1415354); REQUIRE(!strcmp(buffer, "34")); test::sprintf(buffer, "%.2f", 42.8952); REQUIRE(!strcmp(buffer, "42.90")); test::sprintf(buffer, "%.9f", 42.8952); REQUIRE(!strcmp(buffer, "42.895200000")); test::sprintf(buffer, "%6.2f", 42.8952); REQUIRE(!strcmp(buffer, " 42.90")); test::sprintf(buffer, "%+6.2f", 42.8952); REQUIRE(!strcmp(buffer, "+42.90")); test::sprintf(buffer, "%+5.1f", 42.9252); REQUIRE(!strcmp(buffer, "+42.9")); test::sprintf(buffer, "%f", 42.5); REQUIRE(!strcmp(buffer, "42.500000")); test::sprintf(buffer, "%.1f", 42.5); REQUIRE(!strcmp(buffer, "42.5")); test::sprintf(buffer, "%f", (float)42167); REQUIRE(!strcmp(buffer, "42167.000000")); } TEST_CASE("types", "[]" ) { char buffer[100]; test::sprintf(buffer, "%i", 1234); REQUIRE(!strcmp(buffer, "1234")); test::sprintf(buffer, "%li", 30L); REQUIRE(!strcmp(buffer, "30")); test::sprintf(buffer, "%lli", 30LL); REQUIRE(!strcmp(buffer, "30")); test::sprintf(buffer, "%lu", 100000L); REQUIRE(!strcmp(buffer, "100000")); test::sprintf(buffer, "%llu", 281474976710656LLU); REQUIRE(!strcmp(buffer, "281474976710656")); test::sprintf(buffer, "%b", 60000); REQUIRE(!strcmp(buffer, "1110101001100000")); test::sprintf(buffer, "%lb", 12345678L); REQUIRE(!strcmp(buffer, "101111000110000101001110")); test::sprintf(buffer, "%o", 60000); REQUIRE(!strcmp(buffer, "165140")); test::sprintf(buffer, "%lo", 12345678L); REQUIRE(!strcmp(buffer, "57060516")); test::sprintf(buffer, "%lx", 0x12345678L); REQUIRE(!strcmp(buffer, "12345678")); test::sprintf(buffer, "%llx", 0x1234567891234567LLU); REQUIRE(!strcmp(buffer, "1234567891234567")); test::sprintf(buffer, "%lx", 0xabcdefabL); REQUIRE(!strcmp(buffer, "abcdefab")); test::sprintf(buffer, "%lX", 0xabcdefabL); REQUIRE(!strcmp(buffer, "ABCDEFAB")); test::sprintf(buffer, "%c", 'v'); REQUIRE(!strcmp(buffer, "v")); test::sprintf(buffer, "%cv", 'w'); REQUIRE(!strcmp(buffer, "wv")); test::sprintf(buffer, "%s", "A Test"); REQUIRE(!strcmp(buffer, "A Test")); } TEST_CASE("pointer", "[]" ) { char buffer[100]; test::sprintf(buffer, "%p", (void*)0x1234U); if (sizeof(void*) == 4U) { REQUIRE(!strcmp(buffer, "00001234")); } else { REQUIRE(!strcmp(buffer, "0000000000001234")); } test::sprintf(buffer, "%p", (void*)0x12345678U); if (sizeof(void*) == 4U) { REQUIRE(!strcmp(buffer, "12345678")); } else { REQUIRE(!strcmp(buffer, "0000000012345678")); } } TEST_CASE("unknown flag", "[]" ) { char buffer[100]; test::sprintf(buffer, "%kmarco", 42, 37); REQUIRE(!strcmp(buffer, "kmarco")); } TEST_CASE("misc", "[]" ) { char buffer[100]; test::sprintf(buffer, "%u%u%ctest%d %s", 5, 3000, 'a', -20, "bit"); REQUIRE(!strcmp(buffer, "53000atest-20 bit")); }