| 28 | }); |
| 29 | |
| 30 | static 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. |
| 59 | int main() { |