X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=mware%2Fsprintf_test.c;h=1bcedfd35175f289618f6494422d83f27d0019b4;hb=8ceb6ba0e3d44b47960b6e01554f624e2dbbb45e;hp=5e68e79fff77ecfb92e5ead6e78e87bb2522dc41;hpb=79a235ffd4713d62d952c1dbff8ce53721aae6d2;p=bertos.git diff --git a/mware/sprintf_test.c b/mware/sprintf_test.c old mode 100755 new mode 100644 index 5e68e79f..1bcedfd3 --- a/mware/sprintf_test.c +++ b/mware/sprintf_test.c @@ -1,5 +1,47 @@ +/** + * \file + * + * + * \brief sprintf() implementation based on _formatted_write() + * + * \version $Id$ + * \author Bernardo Innocenti + */ + #include "sprintf.c" #include "formatwr.c" +#include "hex.c" +#include +#include +#include #include /* assert() */ #include /* strcmp() */ @@ -17,14 +59,28 @@ int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv)) snprintf(buf, sizeof buf, "%S", test_string_pgm); assert(strcmp(buf, test_string_pgm) == 0); - snprintf(buf, sizeof buf, "%d", 12345); - assert(strcmp(buf, "12345") == 0); +#define TEST(FMT, VALUE, EXPECT) do { \ + snprintf(buf, sizeof buf, FMT, VALUE); \ + assert(strcmp(buf, EXPECT) == 0); \ + } while (0) + + TEST("%d", 12345, "12345"); + TEST("%ld", 123456789L, "123456789"); + TEST("%ld", -12345678L, "-12345678"); + TEST("%lu", 4294967295UL, "4294967295"); + TEST("%hd", -12345, "-12345"); + TEST("%hu", 65535U, "65535"); - snprintf(buf, sizeof buf, "%ld", 123456789L); - assert(strcmp(buf, "123456789") == 0); + TEST("%8d", 123, " 123"); + TEST("%8d", -123, " -123"); + TEST("%-8d", -123, "-123 "); + TEST("%08d", -123, "-0000123"); - snprintf(buf, sizeof buf, "%lu", 4294967295UL); - assert(strcmp(buf, "4294967295") == 0); + TEST("%8.2f", -123.456, " -123.46"); + TEST("%-8.2f", -123.456, "-123.46 "); + TEST("%8.0f", -123.456, " -123"); + +#undef TEST /* * Stress tests. @@ -37,3 +93,4 @@ int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv)) return 0; } +