Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / encoding / api / utf-round-trip.html
1 <!DOCTYPE html>
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>
6 <script>
7
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.
10
11 function fromCodePoint(cp) {
12     if (0xD800 <= cp && cp <= 0xDFFF) throw new Error('Invalid code point');
13
14     if (cp <= 0xFFFF)
15         return String.fromCharCode(cp);
16
17     // outside BMP - encode as surrogate pair
18     return String.fromCharCode(0xD800 + ((cp >> 10) & 0x3FF), 0xDC00 + (cp & 0x3FF));
19 }
20
21 function makeBatch(cp) {
22     var string = '';
23     for (var i = cp; i < cp + BATCH_SIZE && cp < 0x10FFFF; i += SKIP_SIZE) {
24         if (0xD800 <= i && i <= 0xDFFF) {
25             // surrogate half
26             continue;
27         }
28         string += fromCodePoint(i);
29     }
30     return string;
31  }
32
33 utf_encodings.forEach(function(encoding) {
34     test(function() {
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);
40         }
41     }, encoding + ' - encode/decode round trip');
42 });
43
44
45 // Inspired by:
46 // http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
47 function encode_utf8(string) {
48     var utf8 = unescape(encodeURIComponent(string));
49     var octets = [];
50     for (var i = 0; i < utf8.length; i += 1)
51         octets.push(utf8.charCodeAt(i));
52     return octets;
53 }
54
55 function decode_utf8(octets) {
56     var utf8 = String.fromCharCode.apply(null, octets);
57     return decodeURIComponent(escape(utf8));
58 }
59
60 test(function() {
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);
66     }
67 }, 'UTF-8 encoding (compare against unescape/encodeURIComponent)');
68
69 test(function() {
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);
76     }
77 }, 'UTF-8 decoding (compare against decodeURIComponent/escape)');
78
79 </script>