JsonValue_PrintTo_Tests.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include "CppUnitTest.h"
  6. #include "StringBuilder.h"
  7. #include "JsonValue.h"
  8. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  9. using namespace ArduinoJson::Generator;
  10. using namespace ArduinoJson::Internals;
  11. namespace JsonGeneratorTests
  12. {
  13. TEST_CLASS(JsonValue_PrintTo_Tests)
  14. {
  15. char buffer[1024];
  16. size_t returnValue;
  17. public:
  18. TEST_METHOD(String)
  19. {
  20. setValueTo("hello");
  21. outputMustBe("\"hello\"");
  22. }
  23. TEST_METHOD(ZeroFloat)
  24. {
  25. setValueTo(0.0f);
  26. outputMustBe("0.0");
  27. }
  28. TEST_METHOD(Float)
  29. {
  30. setValueTo(3.1415f);
  31. outputMustBe("3.14");
  32. }
  33. TEST_METHOD(DoubleZeroDigits)
  34. {
  35. setValueTo<0>(3.14159265358979323846);
  36. outputMustBe("3");
  37. }
  38. TEST_METHOD(DoubleOneDigit)
  39. {
  40. setValueTo<1>(3.14159265358979323846);
  41. outputMustBe("3.1");
  42. }
  43. TEST_METHOD(DoubleTwoDigits)
  44. {
  45. setValueTo<2>(3.14159265358979323846);
  46. outputMustBe("3.14");
  47. }
  48. TEST_METHOD(Integer)
  49. {
  50. setValueTo(314);
  51. outputMustBe("314");
  52. }
  53. TEST_METHOD(Char)
  54. {
  55. setValueTo('A');
  56. outputMustBe("65");
  57. }
  58. TEST_METHOD(Short)
  59. {
  60. setValueTo((short)314);
  61. outputMustBe("314");
  62. }
  63. TEST_METHOD(Long)
  64. {
  65. setValueTo(314159265L);
  66. outputMustBe("314159265");
  67. }
  68. private:
  69. template<int DIGITS>
  70. void setValueTo(double value)
  71. {
  72. StringBuilder sb(buffer, sizeof(buffer));
  73. JsonValue jsonValue;
  74. jsonValue.set<DIGITS>(value);
  75. returnValue = jsonValue.printTo(sb);
  76. }
  77. template<typename T>
  78. void setValueTo(T value)
  79. {
  80. StringBuilder sb(buffer, sizeof(buffer));
  81. JsonValue jsonValue;
  82. jsonValue = value;
  83. returnValue = jsonValue.printTo(sb);
  84. }
  85. void outputMustBe(const char* expected)
  86. {
  87. Assert::AreEqual(expected, buffer);
  88. Assert::AreEqual(strlen(expected), returnValue);
  89. }
  90. };
  91. }