Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
833 B

  1. #include <string.h>
  2. #include "unity_fixture.h"
  3. TEST_GROUP(sprintf);
  4. static char output[100];
  5. static const char *expected;
  6. // This is run before each test
  7. TEST_SETUP(sprintf) {
  8. memset(output, 0xaa, sizeof(output));
  9. expected = "";
  10. }
  11. // This id run after each test
  12. TEST_TEAR_DOWN(sprintf) {
  13. }
  14. static void expect(const char *s) {
  15. expected = s;
  16. }
  17. static void given(int charsWritten) {
  18. TEST_ASSERT_EQUAL(strlen(expected), charsWritten);
  19. TEST_ASSERT_EQUAL_STRING(expected, &output[1]);
  20. TEST_ASSERT_EQUAL_HEX8(0xaa, output[strlen(expected)+2]);
  21. TEST_ASSERT_EQUAL_HEX8(0xaa, output[0]);
  22. }
  23. TEST(sprintf, NoFormatOperations) {
  24. expect("hey");
  25. given(sprintf(&output[1], "%s", expected));
  26. }
  27. TEST(sprintf, InsertString) {
  28. expect("Hello world\n");
  29. given(sprintf(&output[1], "Hello %s\n", "world"));
  30. }