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.

57 lines
852 B

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