rpy69-cs373-smp is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

Branchmaster
/
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python

# -------------------------------
# projects/smp/TestSMP.py
# -------------------------------

# -------
# imports
# -------

import StringIO
import unittest
import Person

from SMP import smp_read, smp_eval, smp_print, smp_solve

# -----------
# TestSMP
# -----------

class TestSMP (unittest.TestCase) :
    # ----
    # read
    # ----

    def test_read_one_line (self) :
        r = StringIO.StringIO("1 3 2 4 1\n")
        a = [0, 0, 0, 0, 0]
        b = smp_read(r, a)
        self.assert_(b == True)
        self.assert_(a[0] == 1)
        self.assert_(a[1] == 3)
        self.assert_(a[2] == 2)
        self.assert_(a[3] == 4)
        self.assert_(a[4] == 1)

    def test_read_two_line (self) :
        r = StringIO.StringIO("1 3 2 4 1\n2 4 1 2 3\n")
        a1 = [0, 0, 0, 0, 0]
        a2 = [0, 0, 0, 0, 0]
        b1 = smp_read(r, a1)
        b2 = smp_read(r, a2)
        self.assert_(b1 == True)
        self.assert_(a1[0] == 1)
        self.assert_(a1[1] == 3)
        self.assert_(a1[2] == 2)
        self.assert_(a1[3] == 4)
        self.assert_(a1[4] == 1)
        self.assert_(b2 == True)
        self.assert_(a2[0] == 2)
        self.assert_(a2[1] == 4)
        self.assert_(a2[2] == 1)
        self.assert_(a2[3] == 2)
        self.assert_(a2[4] == 3)

    # ----
    # eval
    # ----

    def test_eval_one_choice (self):
        manPrefs = [1]
        man = Person.Person(1,manPrefs,1)
        womanPrefs = [1]
        woman = Person.Person(1,womanPrefs,0)
        manArray = [man]
        womanArray = [woman]
        pairs = smp_eval(manArray,womanArray)
        self.assert_(pairs[0] == 1) #man 1 with woman 1

    def test_eval_same_prefs (self):
        manPrefs = [1,2]
        man1 = Person.Person(1,manPrefs,1)
        man2 = Person.Person(2,manPrefs,1)
        womanPrefs = [1,2]
        woman1 = Person.Person(1,womanPrefs,0)
        woman2 = Person.Person(2,womanPrefs,0)
        manArray = [man1,man2]
        womanArray = [woman1,woman2]
        pairs = smp_eval(manArray,womanArray)
        self.assert_(pairs[0] == 1) #man 1 with woman 1
        self.assert_(pairs[1] == 2) #man 2 with woman 2

    def test_eval_man1_jilted (self):
        manPrefs = [1,2]
        man1 = Person.Person(1,manPrefs,1)
        man2 = Person.Person(2,manPrefs,1)
        womanPrefs = [2,1]
        woman1 = Person.Person(1,womanPrefs,0)
        woman2 = Person.Person(2,womanPrefs,0)
        manArray = [man1,man2]
        womanArray = [woman1,woman2]
        pairs = smp_eval(manArray,womanArray)
        self.assert_(pairs[0] == 2) #man 2 with woman 1
        self.assert_(pairs[1] == 1) #man 1 with woman 2

    # -----
    # print
    # -----
    def test_pair_output (self) :
        w = StringIO.StringIO()
        smp_print(w, [4,3,1,2])
        self.assert_(w.getvalue() == "1 4\n2 3\n3 1\n4 2\n")


    def test_longer_pair_output (self) :
        w = StringIO.StringIO()
        smp_print(w, [3,5,2,4,6,1,7,8,9])
        self.assert_(w.getvalue() == "1 3\n2 5\n3 2\n4 4\n5 6\n6 1\n7 7\n8 8\n9 9\n")

    # -----
    # solve
    # -----
    def test_solve_one_pair (self):
        r = StringIO.StringIO("1\n1\n1 1\n1 1\n")
        w = StringIO.StringIO()
        smp_solve(r, w)
        self.assert_(w.getvalue() == "1 1\n")

    def test_solve_multiple_sets (self):
        r = StringIO.StringIO("2\n1\n1 1\n1 1\n1\n1 1\n1 1\n")
        w = StringIO.StringIO()
        smp_solve(r, w)
        self.assert_(w.getvalue() == "1 1\n1 1\n")

    def test_solve_sphere_test1 (self):
        r = StringIO.StringIO("1\n4\n1 4 3 1 2\n2 2 1 3 4\n3 1 3 4 2\n4 4 3 1 2\n1 3 2 4 1\n2 2 3 1 4\n3 3 1 2 4\n4 3 2 4 1\n")
        w = StringIO.StringIO()
        smp_solve(r, w)
        self.assert_(w.getvalue() == "1 3\n2 2\n3 1\n4 4\n")

    def test_solve_sphere_test2 (self):
        r = StringIO.StringIO("1\n7\n1 3 4 2 1 6 7 5\n2 6 4 2 3 5 1 7\n3 6 3 5 7 2 4 1\n4 1 6 3 2 4 7 5\n5 1 6 5 3 4 7 2\n6 1 7 3 4 5 6 2\n7 5 6 2 4 3 7 1\n1 4 5 3 7 2 6 1\n2 5 6 4 7 3 2 1\n3 1 6 5 4 3 7 2\n4 3 5 6 7 2 4 1\n5 1 7 6 4 3 5 2\n6 6 3 7 5 2 4 1\n7 1 7 4 2 6 5 3\n")
        w = StringIO.StringIO()
        smp_solve(r, w)
        self.assert_(w.getvalue() == "1 4\n2 5\n3 1\n4 3\n5 7\n6 6\n7 2\n")

    # ----------------
    # Person.checkBoot
    # ----------------

    def test_checkBoot_woman_unengaged (self):
        woman = Person.Person(1,[1],0) #unengaged by default
        man = Person.Person(1,[1],1)
        booted = woman.checkBoot(man)
        self.assert_(booted == "accepted")

    def test_checkBoot_prefer_old (self):
        man1 = Person.Person(1,[1,2],1)
        man2 = Person.Person(2,[1,2],1)
        woman = Person.Person(1,[1,2],0)
        woman.setEngaged(man1)
        booted = woman.checkBoot(man2)
        self.assert_(booted == man2)

    def test_checkBoot_prefer_new (self):
        man1 = Person.Person(1,[1,2],1)
        man2 = Person.Person(2,[1,2],1)
        woman = Person.Person(1,[2,1],0)
        woman.setEngaged(man1)
        booted = woman.checkBoot(man2)
        self.assert_(booted == man1)



# ----
# main
# ----

print "TestSMP.py"
unittest.main()
print "Done."
Ajax-loader Loading, please wait...