test_printf.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdio.h>
  2. #include "tinystdio.h"
  3. #include <string.h>
  4. void my_printf(const char* fmt, ...)
  5. {
  6. char buffer[100];
  7. va_list args;
  8. va_start (args, fmt);
  9. tfp_vsprintf(buffer, fmt, args);
  10. printf("%s", buffer);
  11. va_end (args);
  12. }
  13. void print_test(int test_nr, const char* fmt, ...)
  14. {
  15. char stdio_buff[100];
  16. char tiny_buff[100];
  17. va_list args;
  18. va_start (args, fmt);
  19. tfp_vsprintf(tiny_buff, fmt, args);
  20. vsprintf(stdio_buff, fmt, args);
  21. if (strcmp(stdio_buff, tiny_buff) != 0)
  22. {
  23. printf("%d. Test failed.\n\rExpected: %s\n\rGot: %s\n\r\n\r",test_nr, stdio_buff, tiny_buff);
  24. }
  25. else
  26. printf("%d. Test passed.\n\r\t%s\n\r\n\r", test_nr, tiny_buff);
  27. va_end (args);
  28. }
  29. int test_printf(void)
  30. {
  31. /*
  32. 1. Test passed.
  33. Hello, my friend.
  34. 2. Test passed.
  35. This is decimal number 00053
  36. 3. Test passed.
  37. 512 2312 000666 553
  38. 4. Test passed.
  39. Floats 2.1 5.33 2.312000, 6.6600000 0003.21
  40. 5. Test passed.
  41. Written in HEX! 0004E 23 ffffffec 5 0000098
  42. 6. Test passed.
  43. Pointer address! 0x07b
  44. 7. Test failed.
  45. Expected: 0.41200 -0.02 0.000231 -1.123400 -0.666687 0.300000
  46. Got: 0.41200 -0.02 0.000231 -1.123399 -0.666687 0.30
  47. */
  48. printf("Running test:\n\r");
  49. print_test(1, "Hello, my friend.");
  50. print_test(2, "This is decimal number %05d", 53);
  51. print_test(3, "%d %03d %06d %6d", 512, 2312, 666, 553, 3223);
  52. print_test(4, "Floats %7.1f %3.2f %05.6f, %05.7f %07.2f", 2.1, 5.33, 2.3120001, 6.66, 3.215);
  53. print_test(5, "Written in HEX! %05X %x %06x %6x %07X", 78, 35, -20, 5, 152);
  54. print_test(6, "%s address! %05p", "Pointer", 123);
  55. print_test(7, "%3.5f %6.2f %2.6f %03.6f %2.6f %f", 0.412, -0.02, 0.000231, -1.1234, -0.666687, 0.3);
  56. return 0;
  57. }