Utf8.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <string>
  7. using namespace ARDUINOJSON_NAMESPACE;
  8. static void testCodepoint(uint32_t codepoint, std::string expected) {
  9. char buffer[4096];
  10. MemoryPool pool(buffer, 4096);
  11. StringBuilder str(&pool);
  12. CAPTURE(codepoint);
  13. Utf8::encodeCodepoint(codepoint, str);
  14. REQUIRE(str.complete() == expected);
  15. }
  16. TEST_CASE("Utf8::encodeCodepoint()") {
  17. SECTION("U+0000") {
  18. testCodepoint(0x0000, "");
  19. }
  20. SECTION("U+0001") {
  21. testCodepoint(0x0001, "\x01");
  22. }
  23. SECTION("U+007F") {
  24. testCodepoint(0x007F, "\x7f");
  25. }
  26. SECTION("U+0080") {
  27. testCodepoint(0x0080, "\xc2\x80");
  28. }
  29. SECTION("U+07FF") {
  30. testCodepoint(0x07FF, "\xdf\xbf");
  31. }
  32. SECTION("U+0800") {
  33. testCodepoint(0x0800, "\xe0\xa0\x80");
  34. }
  35. SECTION("U+FFFF") {
  36. testCodepoint(0xFFFF, "\xef\xbf\xbf");
  37. }
  38. SECTION("U+10000") {
  39. testCodepoint(0x10000, "\xf0\x90\x80\x80");
  40. }
  41. SECTION("U+10FFFF") {
  42. testCodepoint(0x10FFFF, "\xf4\x8f\xbf\xbf");
  43. }
  44. }