Utf8.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2021
  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. StringCopier str(pool);
  12. str.startString();
  13. CAPTURE(codepoint);
  14. Utf8::encodeCodepoint(codepoint, str);
  15. str.append('\0');
  16. REQUIRE(str.c_str() == expected);
  17. }
  18. TEST_CASE("Utf8::encodeCodepoint()") {
  19. SECTION("U+0000") {
  20. testCodepoint(0x0000, "");
  21. }
  22. SECTION("U+0001") {
  23. testCodepoint(0x0001, "\x01");
  24. }
  25. SECTION("U+007F") {
  26. testCodepoint(0x007F, "\x7f");
  27. }
  28. SECTION("U+0080") {
  29. testCodepoint(0x0080, "\xc2\x80");
  30. }
  31. SECTION("U+07FF") {
  32. testCodepoint(0x07FF, "\xdf\xbf");
  33. }
  34. SECTION("U+0800") {
  35. testCodepoint(0x0800, "\xe0\xa0\x80");
  36. }
  37. SECTION("U+FFFF") {
  38. testCodepoint(0xFFFF, "\xef\xbf\xbf");
  39. }
  40. SECTION("U+10000") {
  41. testCodepoint(0x10000, "\xf0\x90\x80\x80");
  42. }
  43. SECTION("U+10FFFF") {
  44. testCodepoint(0x10FFFF, "\xf4\x8f\xbf\xbf");
  45. }
  46. }