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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
Index: LayoutTests/fast/encoding/api/utf-round-trip.html
diff --git a/LayoutTests/fast/encoding/api/utf-round-trip.html b/LayoutTests/fast/encoding/api/utf-round-trip.html
new file mode 100644
index 0000000000000000000000000000000000000000..ef50a1b6f027de8d5981daa83faa114c2d0c8a6f
--- /dev/null
+++ b/LayoutTests/fast/encoding/api/utf-round-trip.html
@@ -0,0 +1,97 @@
+<!DOCTYPE html>
+<script src="../../js/resources/js-test-pre.js"></script>
+<script src="resources/shared.js"></script>
+<script>
+
+description("Sanity check the Encoding API's handling of UTF encodings.");
+
+BATCH_SIZE = 0x1000; // Convert in batches spanning this made code points.
+SKIP_SIZE = 0x77; // For efficiency, don't test every code point.
+quiet = true; // Don't log every matching range.
+
+function fromCodePoint(cp) {
+ if (0xd800 <= cp && cp <= 0xdfff) throw new Error('Invalid code point');
+
+ if (cp > 0xffff) {
+ // outside BMP - encode as surrogate pair
+ return String.fromCharCode(0xd800 + ((cp >> 10) & 0x3ff), 0xdc00 + (cp & 0x3ff));
+ }
+ return String.fromCharCode(i);
+}
+
+function makeBatch(cp) {
+ var string = '';
+ for (var i = cp; i < cp + BATCH_SIZE && cp < 0x10FFFF; i += SKIP_SIZE) {
+ if (0xd800 <= i && i <= 0xdfff) {
+ // surrogate half
+ continue;
+ }
+ string += fromCodePoint(i);
+ }
+ return string;
+ }
+
+function testEncodeDecode(encoding, min, max) {
+ debug(encoding + " - Encode/Decode Range " + cpname(min) + " - " + cpname(max));
+
+ function cpname(n) {
+ return 'U+' + ((n <= 0xFFFF) ?
+ ('0000' + n.toString(16).toUpperCase()).slice(-4) :
+ n.toString(16).toUpperCase());
+ }
+
+ for (i = min; i < max; i += BATCH_SIZE) {
+ string = makeBatch(i);
+ encoded = new TextEncoder(encoding).encode(string);
+ decoded = new TextDecoder(encoding).decode(encoded);
+ shouldBe("string", "decoded", quiet);
+ }
+
+ debug("no output means all ranges matched");
+ debug("");
+}
+
+utf_encodings.forEach(function(encoding) {
+ testEncodeDecode(encoding, 0, 0x10FFFF);
+});
+
+
+// Inspired by:
+// http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
+function encode_utf8(string) {
+ var utf8 = unescape(encodeURIComponent(string));
+ var octets = [], i;
+ for (i = 0; i < utf8.length; i += 1) {
+ octets.push(utf8.charCodeAt(i));
+ }
+ return octets;
+}
+
+function decode_utf8(octets) {
+ var utf8 = String.fromCharCode.apply(null, octets);
+ return decodeURIComponent(escape(utf8));
+}
+
+debug("UTF-8 encoding (compare against unescape/encodeURIComponent)");
+for (i = 0; i < 0x10FFFF; i += BATCH_SIZE) {
+ str = makeBatch(i);
+ expected = encode_utf8(str);
+ actual = new TextEncoder('UTF-8').encode(str);
+ shouldBe("actual", "expected", quiet);
+}
+debug("no output means all ranges matched");
+debug("");
+
+debug("UTF-8 decoding (compare against decodeURIComponent/escape)");
+for (i = 0; i < 0x10FFFF; i += BATCH_SIZE) {
+ str = makeBatch(i);
+ encoded = encode_utf8(str);
+ expected = decode_utf8(encoded);
+ actual = new TextDecoder('UTF-8').decode(new Uint8Array(encoded));
+ shouldBe("actual", "expected", quiet);
+}
+debug("no output means all ranges matched");
+debug("");
+
+</script>
+<script src="../../js/resources/js-test-post.js"></script>
« 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