MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / testString

Function testString

test/core/test_utf16.cpp:30–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28});
29
30static void testString(const char16_t* arg) {
31 // Test with null-terminated string.
32 std::u16string strz(arg);
33 char16_t* result = new char16_t[strz.size() + 1]();
34 int resultBytes = (strz.size() + 1) * sizeof(char16_t);
35
36 roundtripString(strz.data(), -1, result, resultBytes);
37 // Compare strings after taking a route through JS side.
38 assert(std::equal(result, result + strz.size() + 1, strz.data()));
39
40 // Same test with non-null-terminated string and explicit length.
41 std::vector<char16_t> str(strz.begin(), strz.end());
42 std::fill_n(result, str.size() + 1, 0);
43
44 roundtripString(str.data(), str.size() * sizeof(char16_t), result, resultBytes);
45 assert(std::equal(result, result + str.size(), str.data()));
46
47 // Test again with some garbage at the end of the string.
48 std::vector<char16_t> strx(str);
49 strx.insert(strx.end(), 10, 'x');
50 std::fill_n(result, str.size() + 1, 0);
51
52 roundtripString(strx.data(), str.size() * sizeof(char16_t), result, resultBytes);
53 assert(std::equal(result, result + str.size(), str.data()));
54
55 delete[] result;
56}
57
58// This code tests that UTF16 strings can be marshalled between C++ and JS.
59int main() {

Callers 1

mainFunction · 0.85

Calls 8

sizeMethod · 0.65
assertFunction · 0.50
equalFunction · 0.50
fill_nFunction · 0.50
dataMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected