DynamicJsonBuffer_Basic_Tests.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. class DynamicJsonBuffer_Basic_Tests : public testing::Test {
  10. protected:
  11. DynamicJsonBuffer buffer;
  12. };
  13. TEST_F(DynamicJsonBuffer_Basic_Tests, InitialSizeIsZero) {
  14. ASSERT_EQ(0, buffer.size());
  15. }
  16. TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) {
  17. buffer.alloc(1);
  18. ASSERT_LE(1U, buffer.size());
  19. buffer.alloc(1);
  20. ASSERT_LE(2U, buffer.size());
  21. }
  22. TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
  23. void* p1 = buffer.alloc(1);
  24. void* p2 = buffer.alloc(2);
  25. ASSERT_NE(p1, p2);
  26. }
  27. TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
  28. size_t mask = sizeof(void*) - 1;
  29. for (size_t size = 1; size <= sizeof(void*); size++) {
  30. size_t addr = reinterpret_cast<size_t>(buffer.alloc(1));
  31. ASSERT_EQ(0, addr & mask);
  32. }
  33. }
  34. TEST_F(DynamicJsonBuffer_Basic_Tests, strdup) {
  35. char original[] = "hello";
  36. char* copy = buffer.strdup(original);
  37. strcpy(original, "world");
  38. ASSERT_STREQ("hello", copy);
  39. }
  40. TEST_F(DynamicJsonBuffer_Basic_Tests, strdup_givenNull) {
  41. const char* original = NULL;
  42. char* copy = buffer.strdup(original);
  43. ASSERT_EQ(NULL, copy);
  44. }