Author: raja
Date: 2007-04-04 10:33:46 -0400 (Wed, 04 Apr 2007)
New Revision: 75388

Modified:
   trunk/mcs/class/System/Test/System.Text.RegularExpressions/ChangeLog
   trunk/mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs
Log:
* RegexTest.cs (PrimeRegex): New backtracking torture test using
a regex that recognizes composite numbers represented in unary.


Modified: trunk/mcs/class/System/Test/System.Text.RegularExpressions/ChangeLog
===================================================================
--- trunk/mcs/class/System/Test/System.Text.RegularExpressions/ChangeLog        
2007-04-04 13:54:51 UTC (rev 75387)
+++ trunk/mcs/class/System/Test/System.Text.RegularExpressions/ChangeLog        
2007-04-04 14:33:46 UTC (rev 75388)
@@ -1,3 +1,8 @@
+2007-04-04  Raja R Harinath  <[EMAIL PROTECTED]>
+
+       * RegexTest.cs (PrimeRegex): New backtracking torture test using
+       a regex that recognizes composite numbers represented in unary.
+
 2007-01-02  Raja R Harinath  <[EMAIL PROTECTED]>
 
        * RegexBugs.cs (GroupNumbers): New test based on #79472.

Modified: 
trunk/mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs
===================================================================
--- trunk/mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs     
2007-04-04 13:54:51 UTC (rev 75387)
+++ trunk/mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs     
2007-04-04 14:33:46 UTC (rev 75388)
@@ -11,6 +11,10 @@
 using System;
 using System.Text.RegularExpressions;
 
+#if NET_2_0
+using System.Collections.Generic;
+#endif
+
 using NUnit.Framework;
 
 namespace MonoTests.System.Text.RegularExpressions {
@@ -195,6 +199,63 @@
                {
                        Regex.CacheSize = Int32.MinValue;
                }
+
+               static IEnumerable<uint> Primes (uint m)
+               {
+                       if (m < 2)
+                               yield break;
+
+                       yield return 2;
+
+                       Dictionary<uint, uint> w = new Dictionary<uint, uint> 
();
+                       uint p2, n1;
+
+                       for (uint n = 3; n < m; n += 2) {
+                               if (w.TryGetValue (n, out p2)) {
+                                       w.Remove (n);
+                                       n1 = n + p2;
+                               } else {
+                                       yield return n;
+                                       n1 = n * n;
+                                       p2 = n + n;
+
+                                       // if there's an overflow, don't bother
+                                       if (n1 / n != n || n1 >= m)
+                                               continue;
+                               }
+
+                               while (w.ContainsKey (n1))
+                                       n1 += p2;
+                               w [n1] = p2;
+                       }
+               }
+
+               [Test]
+               public void PrimeRegex ()
+               {
+                       // Perl regex oneliner by: [EMAIL PROTECTED] (Abigail)
+                       // from: 
http://www.mit.edu:8008/bloom-picayune.mit.edu/perl/10138
+                       // perl -wle 'print "Prime" if (1 x shift) !~ 
/^1?$|^(11+?)\1+$/'
+
+                       // This is a backtracking torture test
+
+                       Regex composite = new Regex (@"^1?$|^(11+?)\1+$");
+
+                       uint i = 0;
+                       string x = "";
+
+                       foreach (uint p in Primes (3333)) {
+                               while (i < p) {
+                                       Assert.IsTrue (composite.IsMatch (x));
+                                       ++i;
+                                       x += "1";
+                               }
+                               // i == p
+                               Assert.IsFalse (composite.IsMatch (x));
+                               ++i;
+                               x += "1";
+                       }
+               }
 #endif
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to