Utf8.cpp 1.1 KB

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