Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: LayoutTests/fast/encoding/api/utf-round-trip.html

Issue 15901002: Implement Encoding API (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebaseline webexposed global constructor tests Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 <!DOCTYPE html>
2 <script src="../../js/resources/js-test-pre.js"></script>
3 <script src="resources/shared.js"></script>
4 <script>
5
6 description("Sanity check the Encoding API's handling of UTF encodings.");
7
8 BATCH_SIZE = 0x1000; // Convert in batches spanning this made code points.
9 SKIP_SIZE = 0x77; // For efficiency, don't test every code point.
10 quiet = true; // Don't log every matching range.
11
12 function fromCodePoint(cp) {
13 if (0xd800 <= cp && cp <= 0xdfff) throw new Error('Invalid code point');
14
15 if (cp > 0xffff) {
16 // outside BMP - encode as surrogate pair
17 return String.fromCharCode(0xd800 + ((cp >> 10) & 0x3ff), 0xdc00 + (cp & 0x3ff));
18 }
19 return String.fromCharCode(i);
20 }
21
22 function makeBatch(cp) {
23 var string = '';
24 for (var i = cp; i < cp + BATCH_SIZE && cp < 0x10FFFF; i += SKIP_SIZE) {
25 if (0xd800 <= i && i <= 0xdfff) {
26 // surrogate half
27 continue;
28 }
29 string += fromCodePoint(i);
30 }
31 return string;
32 }
33
34 function testEncodeDecode(encoding, min, max) {
35 debug(encoding + " - Encode/Decode Range " + cpname(min) + " - " + cpname(ma x));
36
37 function cpname(n) {
38 return 'U+' + ((n <= 0xFFFF) ?
39 ('0000' + n.toString(16).toUpperCase()).slice(-4) :
40 n.toString(16).toUpperCase());
41 }
42
43 for (i = min; i < max; i += BATCH_SIZE) {
44 string = makeBatch(i);
45 encoded = new TextEncoder(encoding).encode(string);
46 decoded = new TextDecoder(encoding).decode(encoded);
47 shouldBe("string", "decoded", quiet);
48 }
49
50 debug("no output means all ranges matched");
51 debug("");
52 }
53
54 utf_encodings.forEach(function(encoding) {
55 testEncodeDecode(encoding, 0, 0x10FFFF);
56 });
57
58
59 // Inspired by:
60 // http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.htm l
61 function encode_utf8(string) {
62 var utf8 = unescape(encodeURIComponent(string));
63 var octets = [], i;
64 for (i = 0; i < utf8.length; i += 1) {
65 octets.push(utf8.charCodeAt(i));
66 }
67 return octets;
68 }
69
70 function decode_utf8(octets) {
71 var utf8 = String.fromCharCode.apply(null, octets);
72 return decodeURIComponent(escape(utf8));
73 }
74
75 debug("UTF-8 encoding (compare against unescape/encodeURIComponent)");
76 for (i = 0; i < 0x10FFFF; i += BATCH_SIZE) {
77 str = makeBatch(i);
78 expected = encode_utf8(str);
79 actual = new TextEncoder('UTF-8').encode(str);
80 shouldBe("actual", "expected", quiet);
81 }
82 debug("no output means all ranges matched");
83 debug("");
84
85 debug("UTF-8 decoding (compare against decodeURIComponent/escape)");
86 for (i = 0; i < 0x10FFFF; i += BATCH_SIZE) {
87 str = makeBatch(i);
88 encoded = encode_utf8(str);
89 expected = decode_utf8(encoded);
90 actual = new TextDecoder('UTF-8').decode(new Uint8Array(encoded));
91 shouldBe("actual", "expected", quiet);
92 }
93 debug("no output means all ranges matched");
94 debug("");
95
96 </script>
97 <script src="../../js/resources/js-test-post.js"></script>
OLDNEW
« no previous file with comments | « LayoutTests/fast/encoding/api/surrogate-pairs-expected.txt ('k') | LayoutTests/fast/encoding/api/utf-round-trip-expected.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698