tdd/gcd.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 31 Aug 2010 20:14:18 +0530
changeset 114 83b3e357ed08
parent 111 a6a442d1bbd9
permissions -rw-r--r--
Manipulated the python gcd script to accommodate new way of testing.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
111
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
def gcd(a, b):
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
    while b != 0:
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
        a, b = b, a % b
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
    return a
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
if __name__ == '__main__':
114
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
     7
    for line in open('gcd_testcases.dat'):
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
     8
        values = line.split(', ')
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
     9
        a = int(values[0])
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
    10
        b = int(values[1])
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
    11
        g = int(values[2])
111
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
114
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
    13
        tc = gcd(a, b)
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
    14
        if tc != g:
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
    15
            print "Test failed for the case a=%d and b=%d. Expected %d. Obtained %d instead." % (a, b, g, tc)
83b3e357ed08 Manipulated the python gcd script to accommodate new way of testing.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 111
diff changeset
    16
            exit(1)
111
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
a6a442d1bbd9 Add the gcd.py sample file being used in the hand out.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
    print "All tests passed!"