tdd/generate_testcases.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 07 Sep 2010 16:49:56 +0530
changeset 129 6e237b9442cd
parent 113 38ba17aa202f
permissions -rw-r--r--
Adding a huge question for lab exercise 4.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
113
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
import random
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
def gcd(a, b):
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
    while b != 0:
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
        a, b = b, a % b
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
    return a
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
a = random.sample(xrange(100), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
b = random.sample(xrange(1000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
c = random.sample(xrange(10000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
c = random.sample(xrange(10000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
d = random.sample(xrange(100000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
e = random.sample(xrange(1000000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
f = a + b + c + d + e
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
f.sort()
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
a = random.sample(xrange(100), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
b = random.sample(xrange(1000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
c = random.sample(xrange(10000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
d = random.sample(xrange(100000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
e = random.sample(xrange(1000000), 10)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
g = a + b + c + d + e
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
testcases = []
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
for item in f:
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
    a = f[random.randrange(0, len(f))]
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
    b = g[random.randrange(0, len(g))]
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
    gc = gcd(a, b)
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
    testcases.append([a, b, gc])
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
sortedcases = sorted(testcases, key=lambda case: case[0])
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
fil = open('/home/madhu/Desktop/gcdtest.dat', 'w')
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
for case in sortedcases:
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
    fil.write('%d, %d, %d\n' % (case[0], case[1], case[2]))
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
38ba17aa202f Added the test cases for GCD function and the Python script that was used to generate it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
fil.close()