DynamicJsonBuffer_Basic_Tests.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright Benoit Blanchon 2014-2017
  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. static bool isAligned(void* ptr) {
  28. const size_t mask = sizeof(void*) - 1;
  29. size_t addr = reinterpret_cast<size_t>(ptr);
  30. return (addr & mask) == 0;
  31. }
  32. TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
  33. // make room for tow but not three
  34. buffer = DynamicJsonBuffer(2 * sizeof(void*) + 1);
  35. ASSERT_TRUE(isAligned(buffer.alloc(1))); // this on is aligned by design
  36. ASSERT_TRUE(isAligned(buffer.alloc(1))); // this one fits in the first block
  37. ASSERT_TRUE(isAligned(buffer.alloc(1))); // this one requires a new block
  38. }
  39. TEST_F(DynamicJsonBuffer_Basic_Tests, strdup) {
  40. char original[] = "hello";
  41. char* copy = buffer.strdup(original);
  42. strcpy(original, "world");
  43. ASSERT_STREQ("hello", copy);
  44. }
  45. TEST_F(DynamicJsonBuffer_Basic_Tests, strdup_givenNull) {
  46. const char* original = NULL;
  47. char* copy = buffer.strdup(original);
  48. ASSERT_EQ(NULL, copy);
  49. }