Skip to content

Instantly share code, notes, and snippets.

@llimllib
Last active December 17, 2015 01:19
Show Gist options
  • Save llimllib/5527441 to your computer and use it in GitHub Desktop.
Save llimllib/5527441 to your computer and use it in GitHub Desktop.
from random import Random
from redis import Redis
RAND = Random()
MIN = 0
MAX = 50
KEYS = []
INTERVALS = []
def add(redis):
#add key and interval to both the tree and redis
interval = list(sorted([RAND.randint(MIN, MAX),
RAND.randint(MIN, MAX)]))
while interval in INTERVALS:
interval = list(sorted([RAND.randint(MIN, MAX),
RAND.randint(MIN, MAX)]))
key = "%s_%s" % (interval[0], interval[1])
KEYS.append(key)
INTERVALS.append(interval)
#print "adding %s" % key
print 'r iadd itmp %s %s %s' % (interval[0], interval[1], key)
redis.execute_command("IADD", "itmp", interval[0], interval[1], key)
#TODO: need an interval tree.
def rem(redis):
#rem key and interval from both the tree and redis
key = RAND.choice(KEYS)
KEYS.remove(key)
interval = map(int, key.split("_"))
INTERVALS.remove(interval)
#print "removing %s" % key
print 'r irem itmp %s' % (key)
redis.execute_command("IREM", "itmp", key)
def stab(redis):
i = RAND.randint(MIN-1, MAX+1)
contained_in = [x for x in INTERVALS if i >= x[0] and i <= x[1]]
expected_n = len(contained_in)
print 'r istab itmp %s' % (i)
n = len(redis.execute_command("ISTAB", "itmp", i))
if n != expected_n:
assert n == expected_n, "%s != %s %s" % (n, expected_n, list(sorted(contained_in)))
def fuzz(redis):
for i in range(1000000):
#print "%s keys" % len(KEYS)
if not len(KEYS) > 1:
add(redis)
else:
command = RAND.choice([rem, add, stab])
command(redis)
if __name__=="__main__":
r = Redis()
#the goal here is to generate test cases, so start from
#a fresh db
r.execute_command('flushdb')
fuzz(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment