michael@0: /* Test case for ISO-2022-KR michael@0: * michael@0: * Uses nsIConverterInputStream to decode ISO-2022-KR text. michael@0: * michael@0: * Sample text is: michael@0: * 1: 소 잃고 외양간 고친다 michael@0: * 2: 빈 수레가 요란하다 michael@0: * 3: 하늘의 별 따기 michael@0: * 4: 아는 길도 물어가라 michael@0: * michael@0: * (From http://en.wikiquote.org/wiki/Korean_proverbs) michael@0: */ michael@0: michael@0: const sample = "%1B%24%29C1%3A%20%0E%3CR%0F%20%0E%40R0m%0F%20%0E%3F%5C%3Eg0%23%0F%20%0E0mD%234Y%0A2%3A%20%0E%3As%0F%20%0E%3Cv790%21%0F%20%0E%3Fd6uGO4Y%0A3%3A%20%0EGO4C%40G%0F%20%0E%3A0%0F%20%0E5%7B1b%0A4%3A%20%0E%3EF4B%0F%20%0E1f55%0F%20%0E90%3En0%216s"; michael@0: michael@0: const expected = "1: \uC18C \uC783\uACE0 \uC678\uC591\uAC04 \uACE0\uCE5C\uB2E4\n2: \uBE48 \uC218\uB808\uAC00 \uC694\uB780\uD558\uB2E4\n3: \uD558\uB298\uC758 \uBCC4 \uB530\uAE30\n4: \uC544\uB294 \uAE38\uB3C4 \uBB3C\uC5B4\uAC00\uB77C\n"; michael@0: michael@0: const charset="ISO-2022-KR"; michael@0: michael@0: function testCase(bufferLength) michael@0: { michael@0: var dataURI = "data:text/plain;charset=" + charset + "," + sample; michael@0: michael@0: var IOService = Components.Constructor("@mozilla.org/network/io-service;1", michael@0: "nsIIOService"); michael@0: var ConverterInputStream = michael@0: Components.Constructor("@mozilla.org/intl/converter-input-stream;1", michael@0: "nsIConverterInputStream", michael@0: "init"); michael@0: michael@0: var ios = new IOService(); michael@0: var channel = ios.newChannel(dataURI, "", null); michael@0: var testInputStream = channel.open(); michael@0: var testConverter = new ConverterInputStream(testInputStream, michael@0: charset, michael@0: bufferLength, michael@0: 0xFFFD); michael@0: michael@0: if (!(testConverter instanceof michael@0: Components.interfaces.nsIUnicharLineInputStream)) michael@0: throw "not line input stream"; michael@0: michael@0: var outStr = ""; michael@0: var more; michael@0: do { michael@0: // read the line and check for eof michael@0: var line = {}; michael@0: more = testConverter.readLine(line); michael@0: outStr += line.value + "\n"; michael@0: } while (more); michael@0: michael@0: if (outStr != expected) { michael@0: dump("Failed with bufferLength = " + bufferLength + "\n"); michael@0: if (outStr.length == expected.length) { michael@0: for (i = 0; i < outStr.length; ++i) { michael@0: if (outStr.charCodeAt(i) != expected.charCodeAt(i)) { michael@0: dump(i + ": " + outStr.charCodeAt(i).toString(16) + " != " + expected.charCodeAt(i).toString(16) + "\n"); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // escape the strings before comparing for better readability michael@0: do_check_eq(escape(outStr), escape(expected)); michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: testCase(34); michael@0: testCase(35); michael@0: }