Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Macros | Functions
SkUTFTest.cpp File Reference
#include "include/core/SkTypes.h"
#include "src/base/SkUTF.h"
#include "tests/Test.h"
#include <array>
#include <cstdint>
#include <cstddef>
#include <cstring>

Go to the source code of this file.

Macros

#define ASCII_BYTE   "X"
 
#define CONTINUATION_BYTE   "\xA1"
 
#define LEADING_TWO_BYTE   "\xC2"
 
#define LEADING_THREE_BYTE   "\xE1"
 
#define LEADING_FOUR_BYTE   "\xF0"
 
#define INVALID_BYTE   "\xFC"
 

Functions

 DEF_TEST (SkUTF_UTF16, reporter)
 
 DEF_TEST (SkUTF_UTF8, reporter)
 
 DEF_TEST (SkUTF_CountUTF8, r)
 
 DEF_TEST (SkUTF_NextUTF8_ToUTF8, r)
 

Macro Definition Documentation

◆ ASCII_BYTE

#define ASCII_BYTE   "X"

Definition at line 60 of file SkUTFTest.cpp.

◆ CONTINUATION_BYTE

#define CONTINUATION_BYTE   "\xA1"

Definition at line 61 of file SkUTFTest.cpp.

◆ INVALID_BYTE

#define INVALID_BYTE   "\xFC"

Definition at line 65 of file SkUTFTest.cpp.

◆ LEADING_FOUR_BYTE

#define LEADING_FOUR_BYTE   "\xF0"

Definition at line 64 of file SkUTFTest.cpp.

◆ LEADING_THREE_BYTE

#define LEADING_THREE_BYTE   "\xE1"

Definition at line 63 of file SkUTFTest.cpp.

◆ LEADING_TWO_BYTE

#define LEADING_TWO_BYTE   "\xC2"

Definition at line 62 of file SkUTFTest.cpp.

Function Documentation

◆ DEF_TEST() [1/4]

DEF_TEST ( SkUTF_CountUTF8  ,
 
)

Definition at line 66 of file SkUTFTest.cpp.

66 {
67 static const struct {
68 int expectedCount;
69 const char* utf8String;
70 } testCases[] = {
71 { 0, "" },
72 { 1, ASCII_BYTE },
84 { -1, INVALID_BYTE },
88 { -1, LEADING_TWO_BYTE },
89 { -1, CONTINUATION_BYTE },
95 };
96 for (auto testCase : testCases) {
97 const char* str = testCase.utf8String;
98 REPORTER_ASSERT(r, testCase.expectedCount == SkUTF::CountUTF8(str, strlen(str)));
99 }
100}
#define CONTINUATION_BYTE
Definition SkUTFTest.cpp:61
#define ASCII_BYTE
Definition SkUTFTest.cpp:60
#define INVALID_BYTE
Definition SkUTFTest.cpp:65
#define LEADING_THREE_BYTE
Definition SkUTFTest.cpp:63
#define LEADING_TWO_BYTE
Definition SkUTFTest.cpp:62
#define LEADING_FOUR_BYTE
Definition SkUTFTest.cpp:64
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
SK_SPI int CountUTF8(const char *utf8, size_t byteLength)
Definition SkUTF.cpp:47

◆ DEF_TEST() [2/4]

DEF_TEST ( SkUTF_NextUTF8_ToUTF8  ,
 
)

Definition at line 102 of file SkUTFTest.cpp.

102 {
103 struct {
104 SkUnichar expected;
105 const char* utf8String;
106 } testCases[] = {
107 { -1, INVALID_BYTE },
108 { -1, "" },
109 { 0x0058, ASCII_BYTE },
112 { 0x010330, LEADING_FOUR_BYTE "\x90\x8C\xB0" },
113 };
114 for (auto testCase : testCases) {
115 const char* str = testCase.utf8String;
116 SkUnichar uni = SkUTF::NextUTF8(&str, str + strlen(str));
117 REPORTER_ASSERT(r, str == testCase.utf8String + strlen(testCase.utf8String));
118 REPORTER_ASSERT(r, uni == testCase.expected);
119 char buff[5] = {0, 0, 0, 0, 0};
120 size_t len = SkUTF::ToUTF8(uni, buff);
121 if (buff[len] != 0) {
122 ERRORF(r, "unexpected write");
123 continue;
124 }
125 if (uni == -1) {
126 REPORTER_ASSERT(r, len == 0);
127 continue;
128 }
129 if (len == 0) {
130 ERRORF(r, "unexpected failure.");
131 continue;
132 }
133 if (len > 4) {
134 ERRORF(r, "wrote too much");
135 continue;
136 }
137 str = testCase.utf8String;
138 REPORTER_ASSERT(r, len == strlen(buff));
139 REPORTER_ASSERT(r, len == strlen(str));
140 REPORTER_ASSERT(r, 0 == strcmp(str, buff));
141 }
142}
int32_t SkUnichar
Definition SkTypes.h:175
#define ERRORF(r,...)
Definition Test.h:293
SK_SPI size_t ToUTF8(SkUnichar uni, char utf8[kMaxBytesInUTF8Sequence]=nullptr)
SK_SPI SkUnichar NextUTF8(const char **ptr, const char *end)
Definition SkUTF.cpp:118

◆ DEF_TEST() [3/4]

DEF_TEST ( SkUTF_UTF16  ,
reporter   
)

Definition at line 13 of file SkUTFTest.cpp.

13 {
14 // Test non-basic-multilingual-plane unicode.
15 static const SkUnichar gUni[] = {
16 0x10000, 0x18080, 0x20202, 0xFFFFF, 0x101234
17 };
18 for (SkUnichar uni : gUni) {
19 uint16_t buf[2];
20 size_t count = SkUTF::ToUTF16(uni, buf);
22 size_t count2 = SkUTF::CountUTF16(buf, sizeof(buf));
23 REPORTER_ASSERT(reporter, count2 == 1);
24 const uint16_t* ptr = buf;
25 SkUnichar c = SkUTF::NextUTF16(&ptr, buf + std::size(buf));
26 REPORTER_ASSERT(reporter, c == uni);
27 REPORTER_ASSERT(reporter, ptr - buf == 2);
28 }
29}
reporter
int count
SK_SPI int CountUTF16(const uint16_t *utf16, size_t byteLength)
Definition SkUTF.cpp:70
SK_SPI SkUnichar NextUTF16(const uint16_t **ptr, const uint16_t *end)
Definition SkUTF.cpp:159
SK_SPI size_t ToUTF16(SkUnichar uni, uint16_t utf16[2]=nullptr)
Definition SkUTF.cpp:243

◆ DEF_TEST() [4/4]

DEF_TEST ( SkUTF_UTF8  ,
reporter   
)

Definition at line 31 of file SkUTFTest.cpp.

31 {
32 static const struct {
33 const char* fUtf8;
34 SkUnichar fUni;
35 } gTest[] = {
36 { "a", 'a' },
37 { "\x7f", 0x7f },
38 { "\xC2\x80", 0x80 },
39 { "\xC3\x83", (3 << 6) | 3 },
40 { "\xDF\xBF", 0x7ff },
41 { "\xE0\xA0\x80", 0x800 },
42 { "\xE0\xB0\xB8", 0xC38 },
43 { "\xE3\x83\x83", (3 << 12) | (3 << 6) | 3 },
44 { "\xEF\xBF\xBF", 0xFFFF },
45 { "\xF0\x90\x80\x80", 0x10000 },
46 { "\xF3\x83\x83\x83", (3 << 18) | (3 << 12) | (3 << 6) | 3 }
47 };
48 for (auto test : gTest) {
49 const char* p = test.fUtf8;
50 const char* stop = p + strlen(p);
51 int n = SkUTF::CountUTF8(p, strlen(p));
52 SkUnichar u1 = SkUTF::NextUTF8(&p, stop);
53
55 REPORTER_ASSERT(reporter, u1 == test.fUni);
56 REPORTER_ASSERT(reporter, p - test.fUtf8 == (int)strlen(test.fUtf8));
57 }
58}