RuneTests.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. using System;
  2. using System.Buffers;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using Xunit;
  8. namespace Terminal.Gui.TextTests;
  9. public class RuneTests {
  10. // IsCombiningMark tests
  11. [Theory]
  12. [InlineData (0x0338, true)] // Combining Long Solidus Overlay (U+0338) (e.g. ≠)
  13. [InlineData (0x0300, true)] // Combining Grave Accent
  14. [InlineData (0x0301, true)] // Combining acute accent (é)
  15. [InlineData (0x0302, true)] // Combining Circumflex Accent
  16. [InlineData (0x0328, true)] // Combining ogonek (a small hook or comma shape) U+0328
  17. [InlineData (0x00E9, false)] // Latin Small Letter E with Acute, Unicode U+00E9 é
  18. [InlineData (0x0061, false)] // Latin Small Letter A is U+0061.
  19. public void TestIsCombiningMark (int codepoint, bool expected)
  20. {
  21. var rune = new Rune (codepoint);
  22. Assert.Equal (expected, rune.IsCombiningMark ());
  23. }
  24. [Theory]
  25. [InlineData (0x0000001F, 0x241F)]
  26. [InlineData (0x0000007F, 0x247F)]
  27. [InlineData (0x0000009F, 0x249F)]
  28. [InlineData (0x0001001A, 0x1001A)]
  29. public void MakePrintable_Converts_Control_Chars_To_Proper_Unicode (int code, int expected)
  30. {
  31. var actual = ((Rune)code).MakePrintable ();
  32. Assert.Equal (expected, actual.Value);
  33. }
  34. [Theory]
  35. [InlineData (0x20)]
  36. [InlineData (0x7E)]
  37. [InlineData (0xA0)]
  38. [InlineData (0x010020)]
  39. public void MakePrintable_Does_Not_Convert_Ansi_Chars_To_Unicode (int code)
  40. {
  41. var actual = ((Rune)code).MakePrintable ();
  42. Assert.Equal (code, actual.Value);
  43. }
  44. [Theory]
  45. [InlineData (0x0338)] // Combining Long Solidus Overlay (U+0338) (e.g. ≠)
  46. [InlineData (0x0300)] // Combining Grave Accent
  47. [InlineData (0x0301)] // Combining acute accent (é)
  48. [InlineData (0x0302)] // Combining Circumflex Accent
  49. [InlineData (0x0061)] // Combining ogonek (a small hook or comma shape)
  50. public void MakePrintable_Combining_Character_Is_Not_Printable (int code)
  51. {
  52. var rune = new Rune (code);
  53. var actual = rune.MakePrintable ();
  54. Assert.Equal (code, actual.Value);
  55. }
  56. [Theory]
  57. [InlineData ('a', "a", 1, 1, 1)]
  58. [InlineData ('b', "b", 1, 1, 1)]
  59. [InlineData (123, "{", 1, 1, 1)] // { Left Curly Bracket
  60. [InlineData ('\u1150', "ᅐ", 2, 1, 3)] // ᅐ Hangul Choseong Ceongchieumcieuc
  61. [InlineData ('\u1161', "ᅡ", 0, 1, 3)] // ᅡ Hangul Jungseong A - Unicode Hangul Jamo for join with column width equal to 0 alone.
  62. [InlineData (31, "\u001f", -1, 1, 1)] // non printable character - Information Separator One
  63. [InlineData (127, "\u007f", -1, 1, 1)] // non printable character - Delete
  64. [InlineData ('\u20D0', "⃐", 0, 1, 3)] // ◌⃐ Combining Left Harpoon Above
  65. [InlineData ('\u25a0', "■", 1, 1, 3)] // ■ Black Square
  66. [InlineData ('\u25a1', "□", 1, 1, 3)] // □ White Square
  67. [InlineData ('\uf61e', "", 1, 1, 3)] // Private Use Area
  68. [InlineData ('\u2103', "℃", 1, 1, 3)] // ℃ Degree Celsius
  69. [InlineData ('\u1100', "ᄀ", 2, 1, 3)] // ᄀ Hangul Choseong Kiyeok
  70. [InlineData ('\u2501', "━", 1, 1, 3)] // ━ Box Drawings Heavy Horizontal
  71. [InlineData ('\u2615', "☕", 2, 1, 3)] // ☕ Hot Beverage
  72. [InlineData ('\u231a', "⌚", 2, 1, 3)] // ⌚ Watch
  73. [InlineData ('\u231b', "⌛", 2, 1, 3)] // ⌛ Hourglass
  74. [InlineData ('\u231c', "⌜", 1, 1, 3)] // ⌜ Top Left Corner
  75. [InlineData ('\u1dc0', "᷀", 0, 1, 3)] // ◌᷀ Combining Dotted Grave Accent
  76. public void GetColumns_With_Single_Code (int code, string str, int runeLength, int stringLength, int utf8Length)
  77. {
  78. var rune = new Rune (code);
  79. Assert.Equal (str, rune.ToString ());
  80. Assert.Equal (runeLength, rune.GetColumns ());
  81. Assert.Equal (stringLength, rune.ToString ().Length);
  82. Assert.Equal (utf8Length, rune.Utf8SequenceLength);
  83. Assert.True (Rune.IsValid (rune.Value));
  84. }
  85. [Theory]
  86. [InlineData (new byte [] { 0xf0, 0x9f, 0xa8, 0x81 }, "🨁", 1, 2)] // Neutral Chess Queen
  87. [InlineData (new byte [] { 0xf3, 0xa0, 0xbf, 0xa1 }, "󠿡", 1, 2)] // Undefined Character
  88. [InlineData (new byte [] { 0xf0, 0x9f, 0x8d, 0x95 }, "🍕", 2, 2)] // 🍕 Slice of Pizza
  89. [InlineData (new byte [] { 0xf0, 0x9f, 0xa4, 0x96 }, "🤖", 2, 2)] // 🤖 Robot Face
  90. [InlineData (new byte [] { 0xf0, 0x90, 0x90, 0xa1 }, "𐐡", 1, 2)] // 𐐡 Deseret Capital Letter Er
  91. [InlineData (new byte [] { 0xf0, 0x9f, 0x8c, 0xb9 }, "🌹", 2, 2)] // 🌹 Rose
  92. public void GetColumns_Utf8_Encode (byte [] code, string str, int runeLength, int stringLength)
  93. {
  94. var operationStatus = Rune.DecodeFromUtf8 (code, out Rune rune, out int bytesConsumed);
  95. Assert.Equal (OperationStatus.Done, operationStatus);
  96. Assert.Equal (str, rune.ToString ());
  97. Assert.Equal (runeLength, rune.GetColumns ());
  98. Assert.Equal (stringLength, rune.ToString ().Length);
  99. Assert.Equal (bytesConsumed, rune.Utf8SequenceLength);
  100. Assert.True (Rune.IsValid (rune.Value));
  101. }
  102. [Theory]
  103. [InlineData (new char [] { '\ud83e', '\ude01' }, "🨁", 1, 2, 4)] // Neutral Chess Queen
  104. [InlineData (new char [] { '\udb43', '\udfe1' }, "󠿡", 1, 2, 4)] // Undefined Character
  105. [InlineData (new char [] { '\ud83c', '\udf55' }, "🍕", 2, 2, 4)] // 🍕 Slice of Pizza
  106. [InlineData (new char [] { '\ud83e', '\udd16' }, "🤖", 2, 2, 4)] // 🤖 Robot Face
  107. [InlineData (new char [] { '\ud83e', '\udde0' }, "🧠", 2, 2, 4)] // 🧠 Brain
  108. [InlineData (new char [] { '\ud801', '\udc21' }, "𐐡", 1, 2, 4)] // 𐐡 Deseret Capital Letter Er
  109. [InlineData (new char [] { '\ud83c', '\udf39' }, "🌹", 2, 2, 4)] // 🌹 Rose
  110. public void GetColumns_Utf16_Encode (char [] code, string str, int runeLength, int stringLength, int utf8Length)
  111. {
  112. var rune = new Rune (code [0], code [1]);
  113. Assert.Equal (str, rune.ToString ());
  114. Assert.Equal (runeLength, rune.GetColumns ());
  115. Assert.Equal (stringLength, rune.ToString ().Length);
  116. Assert.Equal (utf8Length, rune.Utf8SequenceLength);
  117. Assert.True (Rune.IsValid (rune.Value));
  118. }
  119. [Theory]
  120. [InlineData ("\U0001fa01", "🨁", 1, 2)] // Neutral Chess Queen
  121. [InlineData ("\U000e0fe1", "󠿡", 1, 2)] // Undefined Character
  122. [InlineData ("\U0001F355", "🍕", 2, 2)] // 🍕 Slice of Pizza
  123. [InlineData ("\U0001F916", "🤖", 2, 2)] // 🤖 Robot Face
  124. [InlineData ("\U0001f9e0", "🧠", 2, 2)] // 🧠 Brain
  125. [InlineData ("\U00010421", "𐐡", 1, 2)] // 𐐡 Deseret Capital Letter Er
  126. [InlineData ("\U0001f339", "🌹", 2, 2)] // 🌹 Rose
  127. public void GetColumns_Utf32_Encode (string code, string str, int runeLength, int stringLength)
  128. {
  129. var operationStatus = Rune.DecodeFromUtf16 (code, out Rune rune, out int charsConsumed);
  130. Assert.Equal (OperationStatus.Done, operationStatus);
  131. Assert.Equal (str, rune.ToString ());
  132. Assert.Equal (runeLength, rune.GetColumns ());
  133. Assert.Equal (stringLength, rune.ToString ().Length);
  134. Assert.Equal (charsConsumed, rune.Utf16SequenceLength);
  135. Assert.True (Rune.IsValid (rune.Value));
  136. // with DecodeRune
  137. (var nrune, var size) = code.DecodeRune ();
  138. Assert.Equal (str, nrune.ToString ());
  139. Assert.Equal (runeLength, nrune.GetColumns ());
  140. Assert.Equal (stringLength, nrune.ToString ().Length);
  141. Assert.Equal (size, nrune.Utf8SequenceLength);
  142. for (int x = 0; x < code.Length - 1; x++) {
  143. Assert.Equal (nrune.Value, char.ConvertToUtf32 (code [x], code [x + 1]));
  144. Assert.True (RuneExtensions.EncodeSurrogatePair (code [x], code [x + 1], out Rune result));
  145. Assert.Equal (rune, result);
  146. }
  147. Assert.True (Rune.IsValid (nrune.Value));
  148. }
  149. [Theory]
  150. [InlineData ("\u2615\ufe0f", "☕️", 2, 2, 2)] // \ufe0f forces it to be rendered as a colorful image as compared to a monochrome text variant.
  151. [InlineData ("\u1107\u1165\u11b8", "법", 3, 2, 1)] // the letters 법 join to form the Korean word for "rice:" U+BC95 법 (read from top left to bottom right)
  152. [InlineData ("\U0001F468\u200D\U0001F469\u200D\U0001F467", "👨‍👩‍👧", 8, 6, 8)] // Man, Woman and Girl emoji.
  153. [InlineData ("\u0915\u093f", "कि", 2, 2, 2)] // Hindi कि with DEVANAGARI LETTER KA and DEVANAGARI VOWEL SIGN I
  154. [InlineData ("\u0e4d\u0e32", "ํา", 2, 1, 2)] // Decomposition: ํ (U+0E4D) - า (U+0E32) = U+0E33 ำ Thai Character Sara Am
  155. [InlineData ("\u0e33", "ำ", 1, 1, 1)] // Decomposition: ํ (U+0E4D) - า (U+0E32) = U+0E33 ำ Thai Character Sara Am
  156. public void GetColumns_String_Without_SurrogatePair (string code, string str, int codeLength, int runesLength, int stringLength)
  157. {
  158. Assert.Equal (str, code.Normalize ());
  159. Assert.Equal (codeLength, code.Length);
  160. Assert.Equal (runesLength, code.EnumerateRunes ().Sum (x => x.GetColumns ()));
  161. Assert.Equal (runesLength, str.GetColumns ());
  162. Assert.Equal (stringLength, str.Length);
  163. }
  164. [Theory]
  165. [InlineData (new char [] { '\ud799', '\udc21' })]
  166. public void Rune_Exceptions_Utf16_Encode (char [] code)
  167. {
  168. Assert.False (RuneExtensions.EncodeSurrogatePair (code [0], code [1], out Rune rune));
  169. Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (code [0], code [1]));
  170. }
  171. [Theory]
  172. [InlineData (0x12345678)]
  173. [InlineData ('\ud801')]
  174. public void Rune_Exceptions_Integers (int code)
  175. {
  176. Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (code));
  177. }
  178. [Fact]
  179. public void GetColumns_GetRuneCount ()
  180. {
  181. PrintTextElementCount ('\u00e1'.ToString (), "á", 1, 1, 1, 1);
  182. PrintTextElementCount ("\u0061\u0301", "á", 1, 2, 2, 1);
  183. PrintTextElementCount ("\u0061\u0301", "á", 1, 2, 2, 1);
  184. PrintTextElementCount ("\u0065\u0301", "é", 1, 2, 2, 1);
  185. PrintTextElementCount ("\U0001f469\U0001f3fd\u200d\U0001f692", "👩🏽‍🚒", 6, 4, 7, 1);
  186. PrintTextElementCount ("\ud801\udccf", "𐓏", 1, 1, 2, 1);
  187. }
  188. private void PrintTextElementCount (string us, string s, int consoleWidth, int runeCount, int stringCount, int txtElementCount)
  189. {
  190. Assert.Equal (us.Length, s.Length);
  191. Assert.Equal (us, s);
  192. Assert.Equal (consoleWidth, us.GetColumns ());
  193. Assert.Equal (runeCount, us.GetRuneCount ());
  194. Assert.Equal (stringCount, s.Length);
  195. TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator (s);
  196. int textElementCount = 0;
  197. while (enumerator.MoveNext ()) {
  198. textElementCount++; // For versions prior to Net5.0 the StringInfo class might handle some grapheme clusters incorrectly.
  199. }
  200. Assert.Equal (txtElementCount, textElementCount);
  201. }
  202. [Fact]
  203. public void TestRuneIsLetter ()
  204. {
  205. Assert.Equal (5, CountLettersInString ("Hello"));
  206. Assert.Equal (8, CountLettersInString ("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟"));
  207. }
  208. private int CountLettersInString (string s)
  209. {
  210. int letterCount = 0;
  211. foreach (Rune rune in s.EnumerateRunes ()) {
  212. if (Rune.IsLetter (rune)) { letterCount++; }
  213. }
  214. return letterCount;
  215. }
  216. [Fact]
  217. public void Test_SurrogatePair_From_String ()
  218. {
  219. Assert.True (ProcessTestStringUseChar ("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟"));
  220. Assert.Throws<Exception> (() => ProcessTestStringUseChar ("\ud801"));
  221. Assert.True (ProcessStringUseRune ("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟"));
  222. Assert.Throws<Exception> (() => ProcessStringUseRune ("\ud801"));
  223. }
  224. private bool ProcessTestStringUseChar (string s)
  225. {
  226. char surrogateChar = default;
  227. for (int i = 0; i < s.Length; i++) {
  228. Rune r;
  229. if (char.IsSurrogate (s [i])) {
  230. if (surrogateChar != default && char.IsSurrogate (surrogateChar)) {
  231. r = new Rune (surrogateChar, s [i]);
  232. Assert.True (r.IsSurrogatePair ());
  233. int codePoint = char.ConvertToUtf32 (surrogateChar, s [i]);
  234. RuneExtensions.EncodeSurrogatePair (surrogateChar, s [i], out Rune rune);
  235. Assert.Equal (codePoint, rune.Value);
  236. string sp = new string (new char [] { surrogateChar, s [i] });
  237. r = (Rune)codePoint;
  238. Assert.Equal (sp, r.ToString ());
  239. Assert.True (r.IsSurrogatePair ());
  240. surrogateChar = default;
  241. } else if (i < s.Length - 1) {
  242. surrogateChar = s [i];
  243. continue;
  244. } else {
  245. Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (s [i]));
  246. throw new Exception ("String was not well-formed UTF-16.");
  247. }
  248. } else {
  249. r = new Rune (s [i]);
  250. var buff = new byte [4];
  251. ((Rune)s [i]).Encode (buff);
  252. Assert.Equal ((int)s [i], buff [0]);
  253. Assert.Equal (s [i], r.Value);
  254. Assert.True (Rune.IsValid (r.Value));
  255. Assert.False (r.IsSurrogatePair ());
  256. }
  257. }
  258. return true;
  259. }
  260. private bool ProcessStringUseRune (string s)
  261. {
  262. var us = s;
  263. string rs = "";
  264. Rune codePoint;
  265. List<Rune> runes = new List<Rune> ();
  266. int colWidth = 0;
  267. for (int i = 0; i < s.Length; i++) {
  268. Rune rune = default;
  269. if (Rune.IsValid (s [i])) {
  270. rune = new Rune (s [i]);
  271. Assert.True (Rune.IsValid (rune.Value));
  272. runes.Add (rune);
  273. Assert.Equal (s [i], rune.Value);
  274. Assert.False (rune.IsSurrogatePair ());
  275. } else if (i + 1 < s.Length && (RuneExtensions.EncodeSurrogatePair (s [i], s [i + 1], out codePoint))) {
  276. Assert.Equal (0, rune.Value);
  277. Assert.False (Rune.IsValid (s [i]));
  278. rune = codePoint;
  279. runes.Add (rune);
  280. string sp = new string (new char [] { s [i], s [i + 1] });
  281. Assert.Equal (sp, codePoint.ToString ());
  282. Assert.True (codePoint.IsSurrogatePair ());
  283. i++; // Increment the iterator by the number of surrogate pair
  284. } else {
  285. Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (s [i]));
  286. throw new Exception ("String was not well-formed UTF-16.");
  287. }
  288. colWidth += rune.GetColumns (); // Increment the column width of this Rune
  289. rs += rune.ToString ();
  290. }
  291. Assert.Equal (us.GetColumns (), colWidth);
  292. Assert.Equal (s, rs);
  293. Assert.Equal (s, StringExtensions.ToString (runes));
  294. return true;
  295. }
  296. [Fact]
  297. public void TestSplit ()
  298. {
  299. string inputString = "🐂, 🐄, 🐆";
  300. string [] splitOnSpace = inputString.Split (' ');
  301. string [] splitOnComma = inputString.Split (',');
  302. Assert.Equal (3, splitOnSpace.Length);
  303. Assert.Equal (3, splitOnComma.Length);
  304. }
  305. [Theory]
  306. [InlineData (true, '\u1100')]
  307. [InlineData (true, '\ud83c', '\udf39')]
  308. [InlineData (true, '\udbff', '\udfff')]
  309. [InlineData (false, '\ud801')]
  310. [InlineData (false, '\ud83e')]
  311. public void Rune_IsValid (bool valid, params char [] chars)
  312. {
  313. Rune rune = default;
  314. bool isValid = true;
  315. if (chars.Length == 1) {
  316. try {
  317. rune = new Rune (chars [0]);
  318. } catch (Exception) {
  319. isValid = false;
  320. }
  321. } else {
  322. rune = new Rune (chars [0], chars [1]);
  323. }
  324. if (valid) {
  325. Assert.NotEqual (default, rune);
  326. Assert.True (Rune.IsValid (rune.Value));
  327. Assert.True (valid);
  328. } else {
  329. Assert.False (valid);
  330. Assert.False (isValid);
  331. }
  332. }
  333. [Theory]
  334. [InlineData ('\u006f', '\u0302', "\u006f\u0302", 1, 0, 2, "o", "̂", "ô", 1, 2)]
  335. [InlineData ('\u0065', '\u0301', "\u0065\u0301", 1, 0, 2, "e", "́", "é", 1, 2)]
  336. public void Test_NonSpacingChar (int code1, int code2, string code, int rune1Length, int rune2Length, int codeLength, string code1String, string code2String, string joinString, int joinLength, int bytesLength)
  337. {
  338. var rune = new Rune (code1);
  339. var nsRune = new Rune (code2);
  340. Assert.Equal (rune1Length, rune.GetColumns ());
  341. Assert.Equal (rune2Length, nsRune.GetColumns ());
  342. var ul = rune.ToString ();
  343. Assert.Equal (code1String, ul);
  344. var uns = nsRune.ToString ();
  345. Assert.Equal (code2String, uns);
  346. var f = $"{rune}{nsRune}".Normalize ();
  347. Assert.Equal (f, joinString);
  348. Assert.Equal (f, code.Normalize ());
  349. Assert.Equal (joinLength, f.GetColumns ());
  350. Assert.Equal (joinLength, code.EnumerateRunes ().Sum (c => c.GetColumns ()));
  351. Assert.Equal (codeLength, code.Length);
  352. (var nrune, var size) = f.DecodeRune ();
  353. Assert.Equal (f.ToRunes () [0], nrune);
  354. Assert.Equal (bytesLength, size);
  355. }
  356. [Theory]
  357. [InlineData (500000000)]
  358. [InlineData (0xf801, 0xdfff)]
  359. public void Test_MaxRune (params int [] codes)
  360. {
  361. if (codes.Length == 1) {
  362. Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (codes [0]));
  363. } else {
  364. Assert.Throws<ArgumentOutOfRangeException> (() => new Rune ((char)codes [0], (char)codes [1]));
  365. }
  366. }
  367. [Fact]
  368. public void Sum_Of_Rune_GetColumns_Is_Not_Always_Equal_To_String_GetColumns ()
  369. {
  370. const int start = 0x000000;
  371. const int end = 0x10ffff;
  372. for (int i = start; i <= end; i++) {
  373. if (char.IsSurrogate ((char)i)) {
  374. continue;
  375. }
  376. Rune r = new Rune ((uint)i);
  377. string us = r.ToString ();
  378. string hex = i.ToString ("x6");
  379. int v = int.Parse (hex, System.Globalization.NumberStyles.HexNumber);
  380. string s = char.ConvertFromUtf32 (v);
  381. if (!r.IsSurrogatePair ()) {
  382. Assert.Equal (r.ToString (), us);
  383. Assert.Equal (us, s);
  384. if (r.GetColumns () < 0) {
  385. Assert.NotEqual (r.GetColumns (), us.GetColumns ());
  386. Assert.NotEqual (s.EnumerateRunes ().Sum (c => c.GetColumns ()), us.GetColumns ());
  387. } else {
  388. Assert.Equal (r.GetColumns (), us.GetColumns ());
  389. Assert.Equal (s.EnumerateRunes ().Sum (c => c.GetColumns ()), us.GetColumns ());
  390. }
  391. Assert.Equal (us.GetRuneCount (), s.Length);
  392. } else {
  393. Assert.Equal (r.ToString (), us);
  394. Assert.Equal (us, s);
  395. Assert.Equal (r.GetColumns (), us.GetColumns ());
  396. Assert.Equal (s.GetColumns (), us.GetColumns ());
  397. Assert.Equal (1, us.GetRuneCount ()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
  398. Assert.Equal (2, s.Length); // String always preserves the originals values of each surrogate pair
  399. }
  400. }
  401. }
  402. [Theory]
  403. [InlineData (0x20D0, 0x20EF)]
  404. [InlineData (0x2310, 0x231F)]
  405. [InlineData (0x1D800, 0x1D80F)]
  406. public void Test_Range (int start, int end)
  407. {
  408. for (int i = start; i <= end; i++) {
  409. Rune r = new Rune ((uint)i);
  410. string us = r.ToString ();
  411. string hex = i.ToString ("x6");
  412. int v = int.Parse (hex, System.Globalization.NumberStyles.HexNumber);
  413. string s = char.ConvertFromUtf32 (v);
  414. if (!r.IsSurrogatePair ()) {
  415. Assert.Equal (r.ToString (), us);
  416. Assert.Equal (us, s);
  417. Assert.Equal (r.GetColumns (), us.GetColumns ());
  418. Assert.Equal (us.GetRuneCount (), s.Length); // For not surrogate pairs string.RuneCount is always equal to String.Length
  419. } else {
  420. Assert.Equal (r.ToString (), us);
  421. Assert.Equal (us, s);
  422. Assert.Equal (r.GetColumns (), us.GetColumns ());
  423. Assert.Equal (1, us.GetRuneCount ()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
  424. Assert.Equal (2, s.Length); // String always preserves the originals values of each surrogate pair
  425. }
  426. Assert.Equal (s.GetColumns (), us.GetColumns ());
  427. }
  428. }
  429. [Theory]
  430. [InlineData ('\ue0fd', false)]
  431. [InlineData ('\ud800', true)]
  432. [InlineData ('\udfff', true)]
  433. public void Test_IsSurrogate (char code, bool isSurrogate)
  434. {
  435. if (isSurrogate) {
  436. Assert.True (char.IsSurrogate (code.ToString (), 0));
  437. } else {
  438. Assert.False (char.IsSurrogate (code.ToString (), 0));
  439. }
  440. }
  441. [Theory]
  442. [InlineData (unchecked((char)0x40D7C0), (char)0xDC20, 0, "\0", false)]
  443. [InlineData ((char)0x0065, (char)0x0301, 0, "\0", false)]
  444. [InlineData ('\ud83c', '\udf56', 0x1F356, "🍖", true)] // 🍖 Meat On Bone
  445. public void Test_EncodeSurrogatePair (char highSurrogate, char lowSurrogate, int runeValue, string runeString, bool isSurrogatePair)
  446. {
  447. Rune rune;
  448. if (isSurrogatePair) {
  449. Assert.True (RuneExtensions.EncodeSurrogatePair ('\ud83c', '\udf56', out rune));
  450. } else {
  451. Assert.False (RuneExtensions.EncodeSurrogatePair (highSurrogate, lowSurrogate, out rune));
  452. }
  453. Assert.Equal (runeValue, rune.Value);
  454. Assert.Equal (runeString, rune.ToString ());
  455. }
  456. [Theory]
  457. [InlineData ('\uea85', null, "", false)] // Private Use Area
  458. [InlineData (0x1F356, new char [] { '\ud83c', '\udf56' }, "🍖", true)] // 🍖 Meat On Bone
  459. public void Test_DecodeSurrogatePair (int code, char [] charsValue, string runeString, bool isSurrogatePair)
  460. {
  461. Rune rune = new Rune (code);
  462. char [] chars;
  463. if (isSurrogatePair) {
  464. Assert.True (rune.DecodeSurrogatePair (out chars));
  465. Assert.Equal (2, chars.Length);
  466. Assert.Equal (charsValue [0], chars [0]);
  467. Assert.Equal (charsValue [1], chars [1]);
  468. Assert.Equal (runeString, new Rune (chars [0], chars [1]).ToString ());
  469. } else {
  470. Assert.False (rune.DecodeSurrogatePair (out chars));
  471. Assert.Null (chars);
  472. Assert.Equal (runeString, rune.ToString ());
  473. }
  474. Assert.Equal (chars, charsValue);
  475. }
  476. [Fact]
  477. public void Test_All_Surrogate_Pairs_Range ()
  478. {
  479. for (uint h = 0xd800; h <= 0xdbff; h++) {
  480. for (uint l = 0xdc00; l <= 0xdfff; l++) {
  481. Rune r = new Rune ((char)h, (char)l);
  482. string us = r.ToString ();
  483. string hex = r.Value.ToString ("x6");
  484. int v = int.Parse (hex, System.Globalization.NumberStyles.HexNumber);
  485. string s = char.ConvertFromUtf32 (v);
  486. Assert.True (v >= 0x10000 && v <= RuneExtensions.MaxUnicodeCodePoint);
  487. Assert.Equal (r.ToString (), us);
  488. Assert.Equal (us, s);
  489. Assert.Equal (r.GetColumns (), us.GetColumns ());
  490. Assert.Equal (s.GetColumns (), us.GetColumns ());
  491. Assert.Equal (1, us.GetRuneCount ()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
  492. Assert.Equal (2, s.Length); // String always preserves the originals values of each surrogate pair
  493. }
  494. }
  495. }
  496. [Theory]
  497. [InlineData ("Hello, 世界", 13, 11, 9)] // Without Surrogate Pairs
  498. [InlineData ("Hello, 𝔹𝕆𝔹", 19, 13, 13)] // With Surrogate Pairs
  499. public void Test_DecodeRune_Extension (string text, int bytesLength, int colsLength, int textLength)
  500. {
  501. List<Rune> runes = new List<Rune> ();
  502. int tSize = 0;
  503. for (int i = 0; i < text.GetRuneCount (); i++) {
  504. (Rune rune, int size) = text.DecodeRune (i);
  505. runes.Add (rune);
  506. tSize += size;
  507. }
  508. string result = StringExtensions.ToString (runes);
  509. Assert.Equal (text, result);
  510. Assert.Equal (bytesLength, tSize);
  511. Assert.Equal (colsLength, result.GetColumns ());
  512. Assert.Equal (textLength, result.Length);
  513. }
  514. [Theory]
  515. [InlineData ("Hello, 世界", 13, 11, 9, "界世 ,olleH")] // Without Surrogate Pairs
  516. [InlineData ("Hello, 𝔹𝕆𝔹", 19, 13, 13, "𝔹𝕆𝔹 ,olleH")] // With Surrogate Pairs
  517. public void Test_DecodeLastRune_Extension (string text, int bytesLength, int colsLength, int textLength, string encoded)
  518. {
  519. List<Rune> runes = new List<Rune> ();
  520. int tSize = 0;
  521. for (int i = text.GetRuneCount () - 1; i >= 0; i--) {
  522. (Rune rune, int size) = text.DecodeLastRune (i);
  523. runes.Add (rune);
  524. tSize += size;
  525. }
  526. string result = StringExtensions.ToString (runes);
  527. Assert.Equal (encoded, result);
  528. Assert.Equal (bytesLength, tSize);
  529. Assert.Equal (colsLength, result.GetColumns ());
  530. Assert.Equal (textLength, result.Length);
  531. }
  532. [Theory]
  533. [InlineData ("���", false)]
  534. [InlineData ("Hello, 世界", true)]
  535. [InlineData (new byte [] { 0xff, 0xfe, 0xfd }, false)]
  536. [InlineData (new byte [] { 0xf0, 0x9f, 0x8d, 0x95 }, true)]
  537. public void Test_CanBeEncodedAsRune_Extension (object text, bool canBeEncodedAsRune)
  538. {
  539. string str;
  540. if (text is string) {
  541. str = (string)text;
  542. if (canBeEncodedAsRune) {
  543. Assert.True (RuneExtensions.CanBeEncodedAsRune (Encoding.Unicode.GetBytes (str.ToCharArray ())));
  544. } else {
  545. Assert.False (RuneExtensions.CanBeEncodedAsRune (Encoding.Unicode.GetBytes (str.ToCharArray ())));
  546. }
  547. } else if (text is byte []) {
  548. str = StringExtensions.ToString ((byte [])text);
  549. if (canBeEncodedAsRune) {
  550. Assert.True (RuneExtensions.CanBeEncodedAsRune (Encoding.Unicode.GetBytes (str.ToCharArray ())));
  551. } else {
  552. Assert.False (RuneExtensions.CanBeEncodedAsRune (Encoding.Unicode.GetBytes (str.ToCharArray ())));
  553. }
  554. }
  555. }
  556. [Fact]
  557. public void Equals_ToRuneList ()
  558. {
  559. var a = new List<List<Rune>> () { "First line.".ToRuneList () };
  560. var b = new List<List<Rune>> () { "First line.".ToRuneList (), "Second line.".ToRuneList () };
  561. var c = new List<Rune> (a [0]);
  562. var d = a [0];
  563. Assert.Equal (a [0], b [0]);
  564. // Not the same reference
  565. Assert.False (a [0] == b [0]);
  566. Assert.NotEqual (a [0], b [1]);
  567. Assert.False (a [0] == b [1]);
  568. Assert.Equal (c, a [0]);
  569. Assert.False (c == a [0]);
  570. Assert.Equal (c, b [0]);
  571. Assert.False (c == b [0]);
  572. Assert.NotEqual (c, b [1]);
  573. Assert.False (c == b [1]);
  574. Assert.Equal (d, a [0]);
  575. // Is the same reference
  576. Assert.True (d == a [0]);
  577. Assert.Equal (d, b [0]);
  578. Assert.False (d == b [0]);
  579. Assert.NotEqual (d, b [1]);
  580. Assert.False (d == b [1]);
  581. Assert.True (a [0].SequenceEqual (b [0]));
  582. Assert.False (a [0].SequenceEqual (b [1]));
  583. Assert.True (c.SequenceEqual (a [0]));
  584. Assert.True (c.SequenceEqual (b [0]));
  585. Assert.False (c.SequenceEqual (b [1]));
  586. Assert.True (d.SequenceEqual (a [0]));
  587. Assert.True (d.SequenceEqual (b [0]));
  588. Assert.False (d.SequenceEqual (b [1]));
  589. }
  590. [Fact]
  591. public void Rune_GetColumns_Versus_String_GetColumns_With_Non_Printable_Characters ()
  592. {
  593. int sumRuneWidth = 0;
  594. int sumConsoleWidth = 0;
  595. for (uint i = 0; i < 32; i++) {
  596. sumRuneWidth += ((Rune)i).GetColumns ();
  597. sumConsoleWidth += ((Rune)i).ToString ().GetColumns ();
  598. }
  599. Assert.Equal (-32, sumRuneWidth);
  600. Assert.Equal (0, sumConsoleWidth);
  601. }
  602. [Theory]
  603. [InlineData ("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", 200, 200, 200)]
  604. [InlineData ("01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n", 201, 200, 199)] // has a '\n' newline
  605. [InlineData ("\t01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n", 202, 200, 198)] // has a '\t' and a '\n' newline
  606. public void Rune_ColumnWidth_Versus_String_ConsoleWidth (string text, int stringLength, int strCols, int runeCols)
  607. {
  608. Assert.Equal (stringLength, text.Length);
  609. Assert.Equal (stringLength, text.GetRuneCount ());
  610. Assert.Equal (strCols, text.GetColumns ());
  611. int sumRuneWidth = text.EnumerateRunes ().Sum (x => x.GetColumns ());
  612. Assert.Equal (runeCols, sumRuneWidth);
  613. }
  614. [Theory]
  615. [InlineData ('\ud800', true)]
  616. [InlineData ('\udbff', true)]
  617. [InlineData ('\udc00', false)]
  618. [InlineData ('\udfff', false)]
  619. [InlineData ('\uefff', null)]
  620. public void Rune_IsHighSurrogate_IsLowSurrogate (char code, bool? isHighSurrogate)
  621. {
  622. if (isHighSurrogate == true) {
  623. Assert.True (char.IsHighSurrogate (code));
  624. } else if (isHighSurrogate == false) {
  625. Assert.True (char.IsLowSurrogate (code));
  626. } else {
  627. Assert.False (char.IsHighSurrogate (code));
  628. Assert.False (char.IsLowSurrogate (code));
  629. }
  630. }
  631. [Theory]
  632. [InlineData ("First line.")]
  633. [InlineData ("Hello, 𝔹𝕆𝔹")]
  634. public void Rune_ToRunes (string text)
  635. {
  636. var runes = text.ToRunes ();
  637. for (int i = 0; i < runes.Length; i++) {
  638. Assert.Equal (text.EnumerateRunes ().ToArray () [i].Value, runes [i].Value);
  639. }
  640. }
  641. [Theory]
  642. [InlineData ('a', 1, 1)]
  643. [InlineData (31, 1, 1)]
  644. [InlineData (123, 1, 1)]
  645. [InlineData (127, 1, 1)]
  646. [InlineData ('\u1150', 1, 3)]
  647. [InlineData ('\u1161', 1, 3)]
  648. [InlineData (0x16fe0, 2, 4)]
  649. public void System_Text_Rune_SequenceLength (int code, int utf16Length, int utf8Length)
  650. {
  651. var r = new System.Text.Rune (code);
  652. Assert.Equal (utf16Length, r.Utf16SequenceLength);
  653. Assert.Equal (utf8Length, r.Utf8SequenceLength);
  654. }
  655. [Fact]
  656. public void Cast_To_Char_Durrogate_Pair_Return_UTF16 ()
  657. {
  658. Assert.NotEqual ("𝔹", $"{new Rune (unchecked((char)0x1d539))}");
  659. Assert.Equal ("픹", $"{new Rune (unchecked((char)0x1d539))}");
  660. Assert.Equal ("픹", $"{new Rune (0xd539)}");
  661. Assert.Equal ("𝔹", $"{new Rune (0x1d539)}");
  662. }
  663. }