Package lepl :: Package regexp :: Package _test :: Module random
[hide private]
[frames] | no frames]

Source Code for Module lepl.regexp._test.random

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Tests for the lepl.regexp package.  We generate random expressions and 
 32  test the results against the python regexp matcher. 
 33  ''' 
 34   
 35  from logging import basicConfig, DEBUG, getLogger 
 36  from random import randint, choice 
 37  from re import compile as compile_ 
 38  from sys import exc_info 
 39  from unittest import TestCase 
 40   
 41  from lepl.regexp.matchers import DfaRegexp, NfaRegexp 
 42  from lepl.support.lib import fmt 
 43   
44 -def randbool(weight=1):
45 return choice([True] * weight + [False])
46
47 -def random_expression(depth_left, alphabet):
48 ''' 49 Generate an expression. If depth_left is 0 then the result must be 50 a simple character; other levels build on this. Alphabet is a list of 51 possible regular characters. 52 ''' 53 if depth_left: 54 return choice([random_sequence, 55 random_option, 56 random_repeat, 57 random_choice, 58 random_range, 59 random_expression])(depth_left-1, alphabet) 60 else: 61 return choice(alphabet + '.')
62
63 -def random_sequence(depth_left, alphabet):
64 return ''.join(random_expression(depth_left, alphabet) 65 for _ in range(randint(1, 3)))
66
67 -def random_option(depth_left, alphabet):
68 subexpr = random_expression(depth_left, alphabet) 69 if len(subexpr) > 1: 70 return fmt('(?:{0})?', subexpr) 71 else: 72 return subexpr + '?'
73
74 -def random_repeat(depth_left, alphabet):
75 subexpr = random_expression(depth_left, alphabet) 76 if len(subexpr) > 1: 77 return fmt('(?:{0})*', subexpr) 78 else: 79 return subexpr + '*'
80
81 -def random_choice(depth_left, alphabet):
82 return fmt('(?:{0})', '|'.join(random_expression(depth_left, alphabet) 83 for _ in range(randint(1, 3))))
84
85 -def random_range(_depth_left, alphabet):
86 def random_chars(): 87 subexpr = '' 88 for _ in range(randint(1, 2)): 89 if randbool(): 90 subexpr += choice(alphabet) 91 else: 92 a, b = choice(alphabet), choice(alphabet) 93 if a > b: 94 a, b = b, a 95 subexpr += fmt('{0}-{1}', a, b) 96 return subexpr
97 def random_content(): 98 if randbool(len(alphabet)): 99 return random_content() 100 else: 101 return '.' 102 # cannot use random_content below with current lepl regexp 103 if randbool(): 104 return fmt('[{0}]', random_chars()) 105 else: 106 return fmt('[^{0}]', random_chars()) 107
108 -def random_string(depth_left, alphabet):
109 if depth_left: 110 return choice(alphabet) + random_string(depth_left-1, alphabet) 111 else: 112 return ''
113
114 -class RandomTest(TestCase):
115
116 - def test_random(self):
117 ''' 118 Compares lepl + python expressions. This runs 'til it fails, and it 119 always does fail, because lepl's expressions are guaranteed greedy 120 while python's aren't. This is "normal" (Perl is the same as Python) 121 but I cannot fathom why it should be - it seems *harder* to make them 122 work that way... 123 ''' 124 #basicConfig(level=DEBUG) 125 log = getLogger('lepl.regexp._test.random') 126 match_alphabet = '012' 127 string_alphabet = '013' 128 for _ in range(100): 129 expression = random_expression(3, match_alphabet) 130 string = random_string(3, string_alphabet) 131 matcher = DfaRegexp(expression) 132 # matcher = NfaRegexp(expression) 133 matcher.config.no_full_first_match() 134 lepl_result = matcher.parse(string) 135 if lepl_result: 136 lepl_result = lepl_result[0] 137 log.debug(fmt('{0} {1} {2}', expression, string, lepl_result)) 138 try: 139 python_result = compile_(expression).match(string) 140 if python_result: 141 python_result = python_result.group() 142 assert lepl_result == python_result, \ 143 fmt('{0} != {1}\n{2} {3}', 144 lepl_result, python_result, expression, string) 145 except: 146 (e, v, _t) = exc_info() 147 if repr(v) == "error('nothing to repeat',)": 148 pass 149 else: 150 raise e
151