bool ProcessTestStringUseChar(string s) { for (int i = 0; i < s.Length; i++) { Rune r = new Rune(s[i]); if (!char.IsSurrogate(s[i])) { var buff = new byte[4]; Rune.EncodeRune(s[i], buff); Assert.AreEqual((int)(s[i]), buff[0]); Assert.AreEqual(s[i], r.Value); Assert.True(r.IsValid); Assert.False(r.IsSurrogatePair); } else if (i + 1 < s.Length && char.IsSurrogatePair(s[i], s[i + 1])) { int codePoint = char.ConvertToUtf32(s[i], s[i + 1]); Rune.EncodeSurrogatePair(s[i], s[i + 1], out Rune rune); Assert.AreEqual(codePoint, rune.Value); string sp = new string(new char[] { s[i], s[i + 1] }); r = (uint)codePoint; Assert.AreEqual(sp, r.ToString()); Assert.True(r.IsSurrogatePair); i++; // Increment the iterator by the number of surrogate pair } else { Assert.False(r.IsValid); throw new Exception("String was not well-formed UTF-16."); } } return(true); }
public void TestValid() { var rune1 = new Rune('\ud83c', '\udf39'); var buff1 = new byte[4]; Assert.AreEqual(4, Rune.EncodeRune(rune1, buff1)); Assert.IsTrue(Rune.Valid(buff1)); Assert.AreEqual(2, rune1.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(rune1)); var rune2 = (uint)'\ud801'; // To avoid throwing an exception set as uint instead a Rune instance. var buff2 = new byte[4]; Assert.AreEqual(3, Rune.EncodeRune(rune2, buff2)); Assert.IsFalse(Rune.Valid(buff2)); // To avoid throwing an exception pass as uint parameter instead Rune. Assert.AreEqual(5, rune2.ToString().Length); // Invalid string. It returns the decimal 55297 representation of the 0xd801 hexadecimal. Assert.AreEqual(-1, Rune.RuneLen(rune2)); Assert.Throws <ArgumentException>(() => Rune.EncodeRune(new Rune('\ud801'), buff2)); }
bool ProcessTestStringUseChar(string s) { for (int i = 0; i < s.Length; i++) { if (!char.IsSurrogate(s[i])) { var buff = new byte[4]; Rune.EncodeRune(s[i], buff); Assert.AreEqual((int)(s[i]), buff[0]); } else if (i + 1 < s.Length && char.IsSurrogatePair(s[i], s[i + 1])) { int codePoint = char.ConvertToUtf32(s[i], s[i + 1]); Assert.AreEqual(codePoint, Rune.DecodeSurrogatePair(s[i], s[i + 1])); i++; // so that when the loop iterates it's actually +2 } else { throw new Exception("String was not well-formed UTF-16."); } } return(true); }
public void TestColumnWidth() { Rune a = 'a'; Rune b = 'b'; Rune c = 123; Rune d = '\u1150'; // 0x1150 ᅐ Unicode Technical Report #11 Rune e = '\u1161'; // 0x1161 ᅡ null character with column equal to 0 Rune f = 31; // non printable character Rune g = 127; // non printable character string h = "\U0001fa01"; string i = "\U000e0fe1"; Rune j = '\u20D0'; Rune k = '\u25a0'; Rune l = '\u25a1'; Rune m = '\uf61e'; byte[] n = new byte[4] { 0xf0, 0x9f, 0x8d, 0x95 }; // UTF-8 Encoding Rune o = new Rune('\ud83c', '\udf55'); // UTF-16 Encoding; string p = "\U0001F355"; // UTF-32 Encoding Rune q = '\u2103'; Rune r = '\u1100'; Rune s = '\u2501'; Assert.AreEqual(1, Rune.ColumnWidth(a)); Assert.AreEqual("a", a.ToString()); Assert.AreEqual(1, a.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(a)); Assert.AreEqual(1, Rune.ColumnWidth(b)); Assert.AreEqual("b", b.ToString()); Assert.AreEqual(1, b.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(b)); var rl = a < b; Assert.IsTrue(rl); Assert.AreEqual(1, Rune.ColumnWidth(c)); Assert.AreEqual("{", c.ToString()); Assert.AreEqual(1, c.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(c)); Assert.AreEqual(2, Rune.ColumnWidth(d)); Assert.AreEqual("ᅐ", d.ToString()); Assert.AreEqual(1, d.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(d)); Assert.AreEqual(2, Rune.ColumnWidth(e)); string join = "\u1104\u1161"; Assert.AreEqual("따", join); Assert.AreEqual(4, join.Sum(x => Rune.ColumnWidth(x))); Assert.IsFalse(Rune.DecodeSurrogatePair(join, out _)); Assert.AreEqual("ᅡ", e.ToString()); Assert.AreEqual(1, e.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(e)); Assert.AreEqual(-1, Rune.ColumnWidth(f)); Assert.AreEqual(1, f.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(f)); Assert.AreEqual(-1, Rune.ColumnWidth(g)); Assert.AreEqual(1, g.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(g)); var uh = ustring.Make(h); (var runeh, var sizeh) = Rune.DecodeRune(uh); Assert.AreEqual(2, Rune.ColumnWidth(runeh)); Assert.AreEqual("🨁", h); Assert.AreEqual(2, runeh.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(runeh)); Assert.AreEqual(sizeh, Rune.RuneLen(runeh)); for (int x = 0; x < uh.Length - 1; x++) { Assert.False(Rune.EncodeSurrogatePair(uh[x], uh[x + 1], out _)); } Assert.IsTrue(Rune.ValidRune(runeh)); Assert.True(Rune.Valid(uh.ToByteArray())); Assert.True(Rune.FullRune(uh.ToByteArray())); Assert.AreEqual(1, Rune.RuneCount(uh)); (var runelh, var sizelh) = Rune.DecodeLastRune(uh); Assert.AreEqual(2, Rune.ColumnWidth(runelh)); Assert.AreEqual(2, runelh.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(runelh)); Assert.AreEqual(sizelh, Rune.RuneLen(runelh)); Assert.IsTrue(Rune.ValidRune(runelh)); var ui = ustring.Make(i); (var runei, var sizei) = Rune.DecodeRune(ui); Assert.AreEqual(1, Rune.ColumnWidth(runei)); Assert.AreEqual("", i); Assert.AreEqual(2, runei.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(runei)); Assert.AreEqual(sizei, Rune.RuneLen(runei)); for (int x = 0; x < ui.Length - 1; x++) { Assert.False(Rune.EncodeSurrogatePair(ui[x], ui[x + 1], out _)); } Assert.IsTrue(Rune.ValidRune(runei)); Assert.True(Rune.Valid(ui.ToByteArray())); Assert.True(Rune.FullRune(ui.ToByteArray())); (var runeli, var sizeli) = Rune.DecodeLastRune(ui); Assert.AreEqual(1, Rune.ColumnWidth(runeli)); Assert.AreEqual(2, runeli.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(runeli)); Assert.AreEqual(sizeli, Rune.RuneLen(runeli)); Assert.IsTrue(Rune.ValidRune(runeli)); Assert.AreNotEqual(Rune.ColumnWidth(runeh), Rune.ColumnWidth(runei)); Assert.AreNotEqual(h, i); Assert.AreEqual(runeh.ToString().Length, runei.ToString().Length); Assert.AreEqual(Rune.RuneLen(runeh), Rune.RuneLen(runei)); Assert.AreEqual(Rune.RuneLen(runeh), Rune.RuneLen(runei)); var uj = ustring.Make(j); (var runej, var sizej) = Rune.DecodeRune(uj); Assert.AreEqual(0, Rune.ColumnWidth(j)); Assert.AreEqual(0, Rune.ColumnWidth(uj.RuneAt(0))); Assert.AreEqual(j, uj.RuneAt(0)); Assert.AreEqual("⃐", j.ToString()); Assert.AreEqual("⃐", uj.ToString()); Assert.AreEqual(1, j.ToString().Length); Assert.AreEqual(1, runej.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(j)); Assert.AreEqual(sizej, Rune.RuneLen(runej)); Assert.AreEqual(1, Rune.ColumnWidth(k)); Assert.AreEqual("■", k.ToString()); Assert.AreEqual(1, k.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(k)); Assert.AreEqual(1, Rune.ColumnWidth(l)); Assert.AreEqual("□", l.ToString()); Assert.AreEqual(1, l.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(l)); Assert.AreEqual(1, Rune.ColumnWidth(m)); Assert.AreEqual("", m.ToString()); Assert.AreEqual(1, m.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(m)); var rn = Rune.DecodeRune(ustring.Make(n)).rune; Assert.AreEqual(1, Rune.ColumnWidth(rn)); Assert.AreEqual("🍕", rn.ToString()); Assert.AreEqual(2, rn.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(rn)); Assert.AreEqual(1, Rune.ColumnWidth(o)); Assert.AreEqual("🍕", o.ToString()); Assert.AreEqual(2, o.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(o)); var rp = Rune.DecodeRune(ustring.Make(p)).rune; Assert.AreEqual(1, Rune.ColumnWidth(rp)); Assert.AreEqual("🍕", p); Assert.AreEqual(2, p.Length); Assert.AreEqual(4, Rune.RuneLen(rp)); Assert.AreEqual(1, Rune.ColumnWidth(q)); Assert.AreEqual("℃", q.ToString()); Assert.AreEqual(1, q.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(q)); var rq = Rune.DecodeRune(ustring.Make(q)).rune; Assert.AreEqual(1, Rune.ColumnWidth(rq)); Assert.AreEqual("℃", rq.ToString()); Assert.AreEqual(1, rq.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(rq)); Assert.AreEqual(2, Rune.ColumnWidth(r)); Assert.AreEqual("ᄀ", r.ToString()); Assert.AreEqual(1, r.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(r)); Assert.AreEqual(1, Rune.ColumnWidth(s)); Assert.AreEqual("━", s.ToString()); Assert.AreEqual(1, s.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(s)); var buff = new byte[4]; var sb = Rune.EncodeRune('\u2503', buff); Assert.AreEqual(1, Rune.ColumnWidth('\u2503')); (var rune, var size) = Rune.DecodeRune(ustring.Make('\u2503')); Assert.AreEqual(sb, size); Assert.AreEqual('\u2503', (uint)rune); var scb = char.ConvertToUtf32("℃", 0); var scr = '℃'.ToString().Length; Assert.AreEqual(scr, Rune.ColumnWidth((uint)scb)); buff = new byte[4]; sb = Rune.EncodeRune('\u1100', buff); Assert.AreEqual(2, Rune.ColumnWidth('\u1100')); Assert.AreEqual(2, ustring.Make('\u1100').ConsoleWidth); Assert.AreEqual(1, '\u1100'.ToString().Length); // Length as string returns 1 but in reality it occupies 2 columns. (rune, size) = Rune.DecodeRune(ustring.Make('\u1100')); Assert.AreEqual(sb, size); Assert.AreEqual('\u1100', (uint)rune); string str = "\u2615"; Assert.AreEqual("☕", str); Assert.AreEqual(1, str.Sum(x => Rune.ColumnWidth(x))); Assert.AreEqual(1, ((ustring)str).ConsoleWidth); Assert.AreEqual(1, Rune.RuneCount(str)); Assert.AreEqual(1, str.Length); str = "\u2615\ufe0f"; // Identical but \ufe0f forces it to be rendered as a colorful image as compared to a monochrome text variant. Assert.AreEqual("☕️", str); Assert.AreEqual(1, str.Sum(x => Rune.ColumnWidth(x))); Assert.AreEqual(1, ((ustring)str).ConsoleWidth); Assert.AreEqual(2, Rune.RuneCount(str)); Assert.AreEqual(2, str.Length); str = "\u231a"; Assert.AreEqual("⌚", str); Assert.AreEqual(2, str.Sum(x => Rune.ColumnWidth(x))); Assert.AreEqual(2, ((ustring)str).ConsoleWidth); Assert.AreEqual(1, Rune.RuneCount(str)); Assert.AreEqual(1, str.Length); str = "\u231b"; Assert.AreEqual("⌛", str); Assert.AreEqual(2, str.Sum(x => Rune.ColumnWidth(x))); Assert.AreEqual(2, ((ustring)str).ConsoleWidth); Assert.AreEqual(1, Rune.RuneCount(str)); Assert.AreEqual(1, str.Length); str = "\u231c"; Assert.AreEqual("⌜", str); Assert.AreEqual(1, str.Sum(x => Rune.ColumnWidth(x))); Assert.AreEqual(1, ((ustring)str).ConsoleWidth); Assert.AreEqual(1, Rune.RuneCount(str)); Assert.AreEqual(1, str.Length); str = "\u1dc0"; Assert.AreEqual("᷀", str); Assert.AreEqual(0, str.Sum(x => Rune.ColumnWidth(x))); Assert.AreEqual(0, ((ustring)str).ConsoleWidth); Assert.AreEqual(1, Rune.RuneCount(str)); Assert.AreEqual(1, str.Length); str = "\ud83e\udd16"; Assert.AreEqual("🤖", str); Assert.AreEqual(2, str.Sum(x => Rune.ColumnWidth(x))); Assert.AreEqual(2, ((ustring)str).ConsoleWidth); Assert.AreEqual(1, Rune.RuneCount(str)); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF Assert.AreEqual(2, str.Length); // String always preserves the originals values of each surrogate pair str = "\U0001f9e0"; Assert.AreEqual("🧠", str); Assert.AreEqual(2, str.Sum(x => Rune.ColumnWidth(x))); Assert.AreEqual(2, ((ustring)str).ConsoleWidth); Assert.AreEqual(1, Rune.RuneCount(str)); Assert.AreEqual(2, str.Length); }
public void TestColumnWidth() { Assert.AreEqual(1, Rune.ColumnWidth(a)); Assert.AreEqual("a", a.ToString()); Assert.AreEqual(1, a.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(a)); Assert.AreEqual(1, Rune.ColumnWidth(b)); Assert.AreEqual("b", b.ToString()); Assert.AreEqual(1, b.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(b)); var rl = a < b; Assert.IsTrue(rl); Assert.AreEqual(1, Rune.ColumnWidth(c)); Assert.AreEqual("{", c.ToString()); Assert.AreEqual(1, c.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(c)); Assert.AreEqual(2, Rune.ColumnWidth(d)); Assert.AreEqual("ᅐ", d.ToString()); Assert.AreEqual(1, d.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(d)); Assert.AreEqual(0, Rune.ColumnWidth(e)); Assert.AreEqual("ᅡ", e.ToString()); Assert.AreEqual(1, e.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(e)); Assert.AreEqual(-1, Rune.ColumnWidth(f)); Assert.AreEqual(1, f.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(f)); Assert.AreEqual(-1, Rune.ColumnWidth(g)); Assert.AreEqual(1, g.ToString().Length); Assert.AreEqual(1, Rune.RuneLen(g)); var uh = ustring.Make(h); (var runeh, var sizeh) = Rune.DecodeRune(uh); Assert.AreEqual(2, Rune.ColumnWidth(runeh)); Assert.AreEqual("🨁", h); Assert.AreEqual(2, runeh.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(runeh)); Assert.AreEqual(sizeh, Rune.RuneLen(runeh)); for (int i = 0; i < uh.Length - 1; i++) { Assert.False(Rune.DecodeSurrogatePair(uh[i], uh[i + 1]) > 0); } Assert.IsTrue(Rune.ValidRune(runeh)); Assert.True(Rune.Valid(uh.ToByteArray())); Assert.True(Rune.FullRune(uh.ToByteArray())); Assert.AreEqual(1, Rune.RuneCount(uh)); (var runelh, var sizelh) = Rune.DecodeLastRune(uh); Assert.AreEqual(2, Rune.ColumnWidth(runelh)); Assert.AreEqual(2, runelh.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(runelh)); Assert.AreEqual(sizelh, Rune.RuneLen(runelh)); Assert.IsTrue(Rune.ValidRune(runelh)); var ui = ustring.Make(i); (var runei, var sizei) = Rune.DecodeRune(ui); Assert.AreEqual(1, Rune.ColumnWidth(runei)); Assert.AreEqual("", i); Assert.AreEqual(2, runei.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(runei)); Assert.AreEqual(sizei, Rune.RuneLen(runei)); for (int i = 0; i < ui.Length - 1; i++) { Assert.False(Rune.DecodeSurrogatePair(ui[i], ui[i + 1]) > 0); } Assert.IsTrue(Rune.ValidRune(runei)); Assert.True(Rune.Valid(ui.ToByteArray())); Assert.True(Rune.FullRune(ui.ToByteArray())); (var runeli, var sizeli) = Rune.DecodeLastRune(ui); Assert.AreEqual(1, Rune.ColumnWidth(runeli)); Assert.AreEqual(2, runeli.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(runeli)); Assert.AreEqual(sizeli, Rune.RuneLen(runeli)); Assert.IsTrue(Rune.ValidRune(runeli)); Assert.AreNotEqual(Rune.ColumnWidth(runeh), Rune.ColumnWidth(runei)); Assert.AreNotEqual(h, i); Assert.AreEqual(runeh.ToString().Length, runei.ToString().Length); Assert.AreEqual(Rune.RuneLen(runeh), Rune.RuneLen(runei)); Assert.AreEqual(Rune.RuneLen(runeh), Rune.RuneLen(runei)); var uj = ustring.Make(j); (var runej, var sizej) = Rune.DecodeRune(uj); Assert.AreEqual(0, Rune.ColumnWidth(j)); Assert.AreEqual(0, Rune.ColumnWidth(uj.RuneAt(0))); Assert.AreEqual(j, uj.RuneAt(0)); Assert.AreEqual("⃐", j.ToString()); Assert.AreEqual("⃐", uj.ToString()); Assert.AreEqual(1, j.ToString().Length); Assert.AreEqual(1, runej.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(j)); Assert.AreEqual(sizej, Rune.RuneLen(runej)); Assert.AreEqual(1, Rune.ColumnWidth(k)); Assert.AreEqual("■", k.ToString()); Assert.AreEqual(1, k.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(k)); Assert.AreEqual(1, Rune.ColumnWidth(l)); Assert.AreEqual("□", l.ToString()); Assert.AreEqual(1, l.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(l)); Assert.AreEqual(1, Rune.ColumnWidth(m)); Assert.AreEqual("", m.ToString()); Assert.AreEqual(1, m.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(m)); var rn = Rune.DecodeRune(ustring.Make(n)).rune; Assert.AreEqual(1, Rune.ColumnWidth(rn)); Assert.AreEqual("🍕", rn.ToString()); Assert.AreEqual(2, rn.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(rn)); Assert.AreEqual(1, Rune.ColumnWidth(o)); Assert.AreEqual("🍕", o.ToString()); Assert.AreEqual(2, o.ToString().Length); Assert.AreEqual(4, Rune.RuneLen(o)); var rp = Rune.DecodeRune(ustring.Make(p)).rune; Assert.AreEqual(1, Rune.ColumnWidth(rp)); Assert.AreEqual("🍕", p); Assert.AreEqual(2, p.Length); Assert.AreEqual(4, Rune.RuneLen(rp)); Assert.AreEqual(1, Rune.ColumnWidth(q)); Assert.AreEqual("℃", q.ToString()); Assert.AreEqual(1, q.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(q)); var rq = Rune.DecodeRune(ustring.Make(q)).rune; Assert.AreEqual(1, Rune.ColumnWidth(rq)); Assert.AreEqual("℃", rq.ToString()); Assert.AreEqual(1, rq.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(rq)); Assert.AreEqual(2, Rune.ColumnWidth(r)); Assert.AreEqual("ᄀ", r.ToString()); Assert.AreEqual(1, r.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(r)); Assert.AreEqual(1, Rune.ColumnWidth(s)); Assert.AreEqual("━", s.ToString()); Assert.AreEqual(1, s.ToString().Length); Assert.AreEqual(3, Rune.RuneLen(s)); var buff = new byte[4]; var sb = Rune.EncodeRune('\u2503', buff); var scb = char.ConvertToUtf32("℃", 0); var scr = '℃'.ToString().Length; }