/src/System.Text.RegularExpressions/tests/RegexMatchTests4.cs

https://github.com/EricWhiteDev/corefx · C# · 345 lines · 253 code · 37 blank · 55 comment · 86 complexity · 9cc5add949baeddf72bbb20866b51855 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Text.RegularExpressions;
  6. using Xunit;
  7. public partial class RegexMatchTests
  8. {
  9. /*
  10. Tested Methods:
  11. public static Match Match(string input); Noncapturing group : Actual - "(a+)(?:b*)(ccc)"
  12. "aaabbbccc"
  13. public static Match Match(string input); Zero-width positive lookahead assertion: Actual - "abc(?=XXX)\\w+"
  14. "abcXXXdef"
  15. public static Match Match(string input); Zero-width negative lookahead assertion: Actual - "abc(?!XXX)\\w+"
  16. "abcXXXdef" - Negative
  17. public static Match Match(string input); Zero-width positive lookbehind assertion: Actual - "(\\w){6}(?< = XXX ) def "
  18. " abcXXXdef "
  19. public static Match Match ( string input ) ; Zero-width negative lookbehind assertion : Actual - " ( \ \ w ) { 6 } ( ? < ! XXX ) def "
  20. " XXXabcdef "
  21. public static Match Match ( string input ) ; Nonbacktracking subexpression : Actual - " [ ^ 0 - 9 ] + ( ?>[0-9]+)3"
  22. "abc123"
  23. */
  24. [Fact]
  25. public static void RegexMatchTestCase4()
  26. {
  27. //////////// Global Variables used for all tests
  28. String strLoc = "Loc_000oo";
  29. String strValue = String.Empty;
  30. int iCountErrors = 0;
  31. int iCountTestcases = 0;
  32. Regex r;
  33. Match match;
  34. String strMatch1 = "aaabbbccc";
  35. Int32[] iMatch1 =
  36. {
  37. 0, 9
  38. }
  39. ;
  40. String[] strGroup1 =
  41. {
  42. "aaabbbccc", "aaa", "ccc"
  43. }
  44. ;
  45. Int32[,] iGroup1 =
  46. {
  47. {
  48. 0, 9
  49. }
  50. , {
  51. 0, 3
  52. }
  53. , {
  54. 6, 3
  55. }
  56. }
  57. ;
  58. String[][] strGrpCap1 = new String[3][];
  59. strGrpCap1[0] = new String[]
  60. {
  61. "aaabbbccc"
  62. }
  63. ;
  64. strGrpCap1[1] = new String[]
  65. {
  66. "aaa"
  67. }
  68. ;
  69. strGrpCap1[2] = new String[]
  70. {
  71. "ccc"
  72. }
  73. ;
  74. Int32[][] iGrpCap1 = new Int32[3][];
  75. iGrpCap1[0] = new Int32[]
  76. {
  77. 5, 9
  78. }
  79. ; //This is ignored
  80. iGrpCap1[1] = new Int32[]
  81. {
  82. 0, 3
  83. }
  84. ; //The first half contains the Index and the latter half the Lengths
  85. iGrpCap1[2] = new Int32[]
  86. {
  87. 6, 3
  88. }
  89. ; //The first half contains the Index and the latter half the Lengths
  90. String strMatch2 = "abcXXXdef";
  91. Int32[] iMatch2 =
  92. {
  93. 0, 9
  94. }
  95. ;
  96. String[] strGroup2 =
  97. {
  98. "abcXXXdef"
  99. }
  100. ;
  101. Int32[,] iGroup2 =
  102. {
  103. {
  104. 0, 9
  105. }
  106. }
  107. ;
  108. String[][] strGrpCap2 = new String[1][];
  109. strGrpCap2[0] = new String[]
  110. {
  111. "abcXXXdef"
  112. }
  113. ;
  114. Int32[][] iGrpCap2 = new Int32[1][];
  115. iGrpCap2[0] = new Int32[]
  116. {
  117. 0, 9
  118. }
  119. ; //This is ignored
  120. try
  121. {
  122. ///////////////////////// START TESTS ////////////////////////////
  123. ///////////////////////////////////////////////////////////////////
  124. // [] public static Match Match(string input); Noncapturing group : Actual - "(a+)(?:b*)(ccc)"
  125. //"aaabbbccc"
  126. //-----------------------------------------------------------------
  127. strLoc = "Loc_498yg";
  128. iCountTestcases++;
  129. r = new Regex("(a+)(?:b*)(ccc)");
  130. match = r.Match("aaabbbccc");
  131. if (!match.Success)
  132. {
  133. iCountErrors++;
  134. Console.WriteLine("Err_78653refgs! doesnot match");
  135. }
  136. else
  137. {
  138. if (!match.Value.Equals(strMatch1) || (match.Index != iMatch1[0]) || (match.Length != iMatch1[1]) || (match.Captures.Count != 1))
  139. {
  140. iCountErrors++;
  141. Console.WriteLine("Err_98275dsg: unexpected return result");
  142. }
  143. //Match.Captures always is Match
  144. if (!match.Captures[0].Value.Equals(strMatch1) || (match.Captures[0].Index != iMatch1[0]) || (match.Captures[0].Length != iMatch1[1]))
  145. {
  146. iCountErrors++;
  147. Console.WriteLine("Err_2046gsg! unexpected return result");
  148. }
  149. if (match.Groups.Count != 3)
  150. {
  151. iCountErrors++;
  152. Console.WriteLine("Err_75324sg! unexpected return result");
  153. }
  154. //Group 0 always is the Match
  155. if (!match.Groups[0].Value.Equals(strMatch1) || (match.Groups[0].Index != iMatch1[0]) || (match.Groups[0].Length != iMatch1[1]) || (match.Groups[0].Captures.Count != 1))
  156. {
  157. iCountErrors++;
  158. Console.WriteLine("Err_2046gsg! unexpected return result");
  159. }
  160. //Group 0's Capture is always the Match
  161. if (!match.Groups[0].Captures[0].Value.Equals(strMatch1) || (match.Groups[0].Captures[0].Index != iMatch1[0]) || (match.Groups[0].Captures[0].Length != iMatch1[1]))
  162. {
  163. iCountErrors++;
  164. Console.WriteLine("Err_2975edg!! unexpected return result");
  165. }
  166. for (int i = 1; i < match.Groups.Count; i++)
  167. {
  168. if (!match.Groups[i].Value.Equals(strGroup1[i]) || (match.Groups[i].Index != iGroup1[i, 0]) || (match.Groups[i].Length != iGroup1[i, 1]) || (match.Groups[i].Captures.Count != 1))
  169. {
  170. iCountErrors++;
  171. Console.WriteLine("Err_1954eg_" + i + "! unexpected return result, Value = <{0}:{3}>, Index = <{1}:{4}>, Length = <{2}:{5}>", match.Groups[i].Value, match.Groups[i].Index, match.Groups[i].Length, strGroup1[i], iGroup1[i, 0], iGroup1[i, 1]);
  172. }
  173. for (int j = 0; j < match.Groups[i].Captures.Count; j++)
  174. {
  175. if (!match.Groups[i].Captures[j].Value.Equals(strGrpCap1[i][j]) || (match.Groups[i].Captures[j].Index != iGrpCap1[i][j]) || (match.Groups[i].Captures[j].Length != iGrpCap1[i][match.Groups[i].Captures.Count + j]))
  176. {
  177. iCountErrors++;
  178. Console.WriteLine("Err_5072dn_" + i + "_" + j + "! unexpected return result, Value = <{0}:{3}>, Index = <{1}:{4}>, Length = <{2}:{5}>", match.Groups[i].Captures[j].Value, match.Groups[i].Captures[j].Index, match.Groups[i].Captures[j].Length, strGrpCap1[i][j], iGrpCap1[i][j], iGrpCap1[i][match.Groups[i].Captures.Count + j]);
  179. }
  180. }
  181. }
  182. }
  183. // [] public static Match Match(string input); Zero-width positive lookahead assertion: Actual - "abc(?=XXX)\\w+"
  184. //"abcXXXdef"
  185. //-----------------------------------------------------------------
  186. strLoc = "Loc_298vy";
  187. iCountTestcases++;
  188. r = new Regex(@"abc(?=XXX)\w+");
  189. match = r.Match("abcXXXdef");
  190. if (!match.Success)
  191. {
  192. iCountErrors++;
  193. Console.WriteLine("Err_7453efg! doesnot match");
  194. }
  195. else
  196. {
  197. if (!match.Value.Equals(strMatch2) || (match.Index != iMatch2[0]) || (match.Length != iMatch2[1]) || (match.Captures.Count != 1))
  198. {
  199. iCountErrors++;
  200. Console.WriteLine("Err_827345sdf! unexpected return result");
  201. }
  202. //Match.Captures always is Match
  203. if (!match.Captures[0].Value.Equals(strMatch2) || (match.Captures[0].Index != iMatch2[0]) || (match.Captures[0].Length != iMatch2[1]))
  204. {
  205. iCountErrors++;
  206. Console.WriteLine("Err_1074sf! unexpected return result");
  207. }
  208. if (match.Groups.Count != 1)
  209. {
  210. iCountErrors++;
  211. Console.WriteLine("Err_2175sgg! unexpected return result");
  212. }
  213. //Group 0 always is the Match
  214. if (!match.Groups[0].Value.Equals(strMatch2) || (match.Groups[0].Index != iMatch2[0]) || (match.Groups[0].Length != iMatch2[1]) || (match.Groups[0].Captures.Count != 1))
  215. {
  216. iCountErrors++;
  217. Console.WriteLine("Err_68217fdgs! unexpected return result");
  218. }
  219. //Group 0's Capture is always the Match
  220. if (!match.Groups[0].Captures[0].Value.Equals(strMatch2) || (match.Groups[0].Captures[0].Index != iMatch2[0]) || (match.Groups[0].Captures[0].Length != iMatch2[1]))
  221. {
  222. iCountErrors++;
  223. Console.WriteLine("Err_139wn!! unexpected return result");
  224. }
  225. for (int i = 1; i < match.Groups.Count; i++)
  226. {
  227. if (!match.Groups[i].Value.Equals(strGroup2[i]) || (match.Groups[i].Index != iGroup2[i, 0]) || (match.Groups[i].Length != iGroup2[i, 1]) || (match.Groups[i].Captures.Count != 1))
  228. {
  229. iCountErrors++;
  230. Console.WriteLine("Err_107vxg_" + i + "! unexpected return result, Value = <{0}:{3}>, Index = <{1}:{4}>, Length = <{2}:{5}>", match.Groups[i].Value, match.Groups[i].Index, match.Groups[i].Length, strGroup2[i], iGroup2[i, 0], iGroup2[i, 1]);
  231. }
  232. for (int j = 0; j < match.Groups[i].Captures.Count; j++)
  233. {
  234. if (!match.Groups[i].Captures[j].Value.Equals(strGrpCap2[i][j]) || (match.Groups[i].Captures[j].Index != iGrpCap2[i][j]) || (match.Groups[i].Captures[j].Length != iGrpCap2[i][match.Groups[i].Captures.Count + j]))
  235. {
  236. iCountErrors++;
  237. Console.WriteLine("Err_745dsgg_" + i + "_" + j + "! unexpected return result, Value = <{0}:{3}>, Index = <{1}:{4}>, Length = <{2}:{5}>", match.Groups[i].Captures[j].Value, match.Groups[i].Captures[j].Index, match.Groups[i].Captures[j].Length, strGrpCap2[i][j], iGrpCap2[i][j], iGrpCap2[i][match.Groups[i].Captures.Count + j]);
  238. }
  239. }
  240. }
  241. }
  242. // [] public static Match Match(string input); Zero-width negative lookahead assertion: Actual - "abc(?!XXX)\\w+"
  243. //"abcXXXdef" - Negative
  244. //-----------------------------------------------------------------
  245. strLoc = "Loc_746tegd";
  246. iCountTestcases++;
  247. r = new Regex(@"abc(?!XXX)\w+");
  248. match = r.Match("abcXXXdef");
  249. if (match.Success)
  250. {
  251. iCountErrors++;
  252. Console.WriteLine("Err_756tdfg! doesnot match");
  253. }
  254. // [] public static Match Match(string input); Zero-width positive lookbehind assertion: Actual - "(\\w){6}(?<=XXX)def"
  255. //"abcXXXdef"
  256. //-----------------------------------------------------------------
  257. strLoc = "Loc_563rfg";
  258. iCountTestcases++;
  259. r = new Regex(@"(\w){6}(?<=XXX)def");
  260. match = r.Match("abcXXXdef");
  261. if (!match.Success)
  262. {
  263. iCountErrors++;
  264. Console.WriteLine("Err_1072gdg! doesnot match");
  265. }
  266. // [] public static Match Match(string input); Zero-width negative lookbehind assertion: Actual - "(\\w){6}(?<!XXX)def"
  267. //"XXXabcdef"
  268. //-----------------------------------------------------------------
  269. strLoc = "Loc_563rfg";
  270. iCountTestcases++;
  271. r = new Regex(@"(\w){6}(?<!XXX)def");
  272. match = r.Match("XXXabcdef");
  273. if (!match.Success)
  274. {
  275. iCountErrors++;
  276. Console.WriteLine("Err_532resgf! doesnot match");
  277. }
  278. // [] public static Match Match(string input); Nonbacktracking subexpression: Actual - "[^0-9]+(?>[0-9]+)3"
  279. //"abc123"
  280. //-----------------------------------------------------------------
  281. strLoc = "Loc_563rfg";
  282. iCountTestcases++;
  283. r = new Regex("[^0-9]+(?>[0-9]+)3");
  284. //The last 3 causes the match to fail, since the non backtracking subexpression does not give up the last digit it matched
  285. //for it to be a success. For a correct match, remove the last character, '3' from the pattern
  286. match = r.Match("abc123");
  287. if (match.Success)
  288. {
  289. iCountErrors++;
  290. Console.WriteLine("Err_234fsadg! doesnot match");
  291. }
  292. ///////////////////////////////////////////////////////////////////
  293. /////////////////////////// END TESTS /////////////////////////////
  294. }
  295. catch (Exception exc_general)
  296. {
  297. ++iCountErrors;
  298. Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
  299. }
  300. //// Finish Diagnostics
  301. Assert.Equal(0, iCountErrors);
  302. }
  303. }