Exemplo n.º 1
0
        public void TestEmptyString()
        {
            byte[]   data = new byte [] {};
            Encoding enc  = new UnixEncoding();

            string s = enc.GetString(data);

            Assert.AreEqual(s, "", "#1");
            char[] chars = enc.GetChars(data);
            Assert.AreEqual(chars.Length, 0, "#2");

            byte[] b1 = enc.GetBytes("");
            Assert.AreEqual(b1.Length, 0, "#3");
            byte[] b2 = enc.GetBytes(new char[] {});
            Assert.AreEqual(b2.Length, 0, "#3");
        }
Exemplo n.º 2
0
        public void TestEncodingGetBytes1()
        {
            UnixEncoding unixEnc = new UnixEncoding();
            string       UniCode = "\u0041\u2262\u0391\u002E";

            // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E
            // see (RFC 2044)
            byte[] unixBytes = unixEnc.GetBytes(UniCode);

            Assert.AreEqual(0x41, unixBytes [0], "UTF #1");
            Assert.AreEqual(0xE2, unixBytes [1], "UTF #2");
            Assert.AreEqual(0x89, unixBytes [2], "UTF #3");
            Assert.AreEqual(0xA2, unixBytes [3], "UTF #4");
            Assert.AreEqual(0xCE, unixBytes [4], "UTF #5");
            Assert.AreEqual(0x91, unixBytes [5], "UTF #6");
            Assert.AreEqual(0x2E, unixBytes [6], "UTF #7");
        }
Exemplo n.º 3
0
        public void TestEncodingGetBytes2()
        {
            UnixEncoding unixEnc = new UnixEncoding();
            string       UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";

            // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21
            // see (RFC 2044)
            byte[] unixBytes = new byte [11];

            int ByteCnt = unixEnc.GetBytes(UniCode.ToCharArray(), 0, UniCode.Length, unixBytes, 0);

            Assert.AreEqual(11, ByteCnt, "UTF #1");
            Assert.AreEqual(0x48, unixBytes [0], "UTF #2");
            Assert.AreEqual(0x69, unixBytes [1], "UTF #3");
            Assert.AreEqual(0x20, unixBytes [2], "UTF #4");
            Assert.AreEqual(0x4D, unixBytes [3], "UTF #5");
            Assert.AreEqual(0x6F, unixBytes [4], "UTF #6");
            Assert.AreEqual(0x6D, unixBytes [5], "UTF #7");
            Assert.AreEqual(0x20, unixBytes [6], "UTF #8");
            Assert.AreEqual(0xE2, unixBytes [7], "UTF #9");
            Assert.AreEqual(0x98, unixBytes [8], "UTF #10");
            Assert.AreEqual(0xBA, unixBytes [9], "UTF #11");
            Assert.AreEqual(0x21, unixBytes [10], "UTF #12");
        }
Exemplo n.º 4
0
        public void TestThrowOnInvalid()
        {
            UnixEncoding u = new UnixEncoding();

            byte[] data = new byte [] { 0xC0, 0xAF };
            string s    = u.GetString(data);

            Assert.AreEqual(4, s.Length);
            Assert.AreEqual(0x0000, (int)s [0]);
            Assert.AreEqual(0xC0, (int)s [1]);
            Assert.AreEqual(0x0000, (int)s [2]);
            Assert.AreEqual(0xAF, (int)s [3]);
            Assert.AreEqual("\u0000\xC0\u0000\xAF", s, "Output-TestThrowOnInvalid");
            Assert.AreEqual(BitConverter.ToString(data), BitConverter.ToString(unix.GetBytes(s)), "Reconverted-TestThrowOnInvalid");

            data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 };
            s    = u.GetString(data);
            Assert.AreEqual(8, s.Length);
            Assert.AreEqual(0x30, (int)s [0]);
            Assert.AreEqual(0x31, (int)s [1]);
            Assert.AreEqual(0x0000, (int)s [2]);
            Assert.AreEqual(0xC0, (int)s [3]);
            Assert.AreEqual(0x0000, (int)s [4]);
            Assert.AreEqual(0xAF, (int)s [5]);
            Assert.AreEqual(0x30, (int)s [6]);
            Assert.AreEqual(0x32, (int)s [7]);

            Assert.AreEqual("\x30\x31\u0000\xC0\u0000\xAF\x30\x32", s, "Output-TestThrowOnInvalid2");
            Assert.AreEqual(BitConverter.ToString(data), BitConverter.ToString(unix.GetBytes(s)), "Reconverted-TestThrowOnInvalid2");
        }