2 <title>Encoding API: UTF encoding round trips</title>
3 <script src="../../../resources/testharness.js"></script>
4 <script src="../../../resources/testharnessreport.js"></script>
5 <script src="resources/shared.js"></script>
8 var BATCH_SIZE = 0x1000; // Convert in batches spanning this many code points.
9 var SKIP_SIZE = 0x77; // For efficiency, don't test every code point.
11 function fromCodePoint(cp) {
12 if (0xD800 <= cp && cp <= 0xDFFF) throw new Error('Invalid code point');
15 return String.fromCharCode(cp);
17 // outside BMP - encode as surrogate pair
18 return String.fromCharCode(0xD800 + ((cp >> 10) & 0x3FF), 0xDC00 + (cp & 0x3FF));
21 function makeBatch(cp) {
23 for (var i = cp; i < cp + BATCH_SIZE && cp < 0x10FFFF; i += SKIP_SIZE) {
24 if (0xD800 <= i && i <= 0xDFFF) {
28 string += fromCodePoint(i);
33 utf_encodings.forEach(function(encoding) {
35 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) {
36 var string = makeBatch(i);
37 var encoded = new TextEncoder(encoding).encode(string);
38 var decoded = new TextDecoder(encoding).decode(encoded);
39 assert_equals(decoded, string);
41 }, encoding + ' - encode/decode round trip');
46 // http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
47 function encode_utf8(string) {
48 var utf8 = unescape(encodeURIComponent(string));
50 for (var i = 0; i < utf8.length; i += 1)
51 octets.push(utf8.charCodeAt(i));
55 function decode_utf8(octets) {
56 var utf8 = String.fromCharCode.apply(null, octets);
57 return decodeURIComponent(escape(utf8));
61 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) {
62 var string = makeBatch(i);
63 var expected = encode_utf8(string);
64 var actual = new TextEncoder('UTF-8').encode(string);
65 assert_array_equals(actual, expected);
67 }, 'UTF-8 encoding (compare against unescape/encodeURIComponent)');
70 for (var i = 0; i < 0x10FFFF; i += BATCH_SIZE) {
71 var string = makeBatch(i);
72 var encoded = encode_utf8(string);
73 var expected = decode_utf8(encoded);
74 var actual = new TextDecoder('UTF-8').decode(new Uint8Array(encoded));
75 assert_equals(actual, expected);
77 }, 'UTF-8 decoding (compare against decodeURIComponent/escape)');