Best Python code snippet using avocado_python
set.py
Source:set.py
1# Natural Language Toolkit: Test Code for Sets2#3# Copyright (C) 2001 University of Pennsylvania4# Author: Edward Loper <edloper@gradient.cis.upenn.edu>5# URL: <http://lib.nltk.sf.net>6# For license information, see LICENSE.TXT7#8# $Id: set.py,v 1.6 2004/03/18 21:02:35 edloper Exp $9"""10Unit testing for L{lib.nltk.set}.11"""12from lib.nltk.set import *13##//////////////////////////////////////////////////////14## Test code15##//////////////////////////////////////////////////////16import unittest17class SetTestCase(unittest.TestCase):18 """19 Unit test cases for L{lib.nltk.set.Set}20 """21 def _makeset(self, *values):22 """23 This is so the subclass L{MutableSetTestCase} will create24 mutable sets, instead of sets.25 """26 return Set(*values)27 28 def testConstructor(self):29 "lib.nltk.set.Set: constructor tests"30 s1 = self._makeset(1,2,5,4,6,3)31 self.failUnlessEqual(len(s1), 6)32 self.failUnless(1 in s1 and 2 in s1 and 3 in s1 and33 4 in s1 and 5 in s1 and 6 in s1 and34 0 not in s1 and 7 not in s1 and35 8 not in s1)36 s2 = self._makeset()37 self.failUnlessEqual(len(s2), 0)38 self.failIf(-1 in s2 or 0 in s2 or 1 in s2)39 s3 = self._makeset( (1,2,3) )40 self.failUnlessEqual(len(s3), 1)41 self.failIf(1 in s3 or 2 in s3 or 3 in s3)42 self.failUnless( (1,2,3) in s3)43 def testUnion(self):44 "lib.nltk.set.Set: union tests"45 s1 = self._makeset(1,3,5,7,9,11,13,15,17,19,21,23,25)46 s2 = self._makeset(2,3,5,7,11,13,17,19,23)47 s3 = self._makeset(2,4,9,16,25)48 s1s2 = self._makeset(1,2,3,5,7,9,11,13,15,17,19,21,23,25)49 s1s3 = self._makeset(1,2,3,4,5,7,9,11,13,15,16,17,19,21,23,25)50 s2s3 = self._makeset(2,3,4,5,7,9,11,13,16,17,19,23,25)51 s1s2s3 = s1s352 self.failUnlessEqual(s1.union(s2), s1s2)53 self.failUnlessEqual(s1.union(s3), s1s3)54 self.failUnlessEqual(s2.union(s1), s1s2)55 self.failUnlessEqual(s2.union(s3), s2s3)56 self.failUnlessEqual(s3.union(s1), s1s3)57 self.failUnlessEqual(s3.union(s2), s2s3)58 self.failUnlessEqual(s1.union(s2).union(s3), s1s2s3)59 self.failUnlessEqual(s1.union(s3).union(s2), s1s2s3)60 self.failUnlessEqual(s2.union(s1).union(s3), s1s2s3)61 self.failUnlessEqual(s2.union(s3).union(s1), s1s2s3)62 self.failUnlessEqual(s3.union(s2).union(s1), s1s2s3)63 self.failUnlessEqual(s3.union(s1).union(s2), s1s2s3)64 65 self.failUnlessEqual(s1 | s2, s1s2)66 self.failUnlessEqual(s1 | s3, s1s3)67 self.failUnlessEqual(s2 | s1, s1s2)68 self.failUnlessEqual(s2 | s3, s2s3)69 self.failUnlessEqual(s3 | s1, s1s3)70 self.failUnlessEqual(s3 | s2, s2s3)71 self.failUnlessEqual(s1 | s2 | s3, s1s2s3)72 self.failUnlessEqual(s1 | s3 | s2, s1s2s3)73 self.failUnlessEqual(s2 | s1 | s3, s1s2s3)74 self.failUnlessEqual(s2 | s3 | s1, s1s2s3)75 self.failUnlessEqual(s3 | s2 | s1, s1s2s3)76 self.failUnlessEqual(s3 | s1 | s2, s1s2s3)77 78 def testIntersection(self):79 "lib.nltk.set.Set: intersection tests"80 s1 = self._makeset(1,3,5,7,9,11,13,15,17,19,21,23,25)81 s2 = self._makeset(2,3,5,7,11,13,17,19,23)82 s3 = self._makeset(2,4,9,16,25)83 s1s2 = self._makeset(3,5,7,11,13,17,19,23)84 s1s3 = self._makeset(9,25)85 s2s3 = self._makeset(2)86 s1s2s3 = self._makeset()87 self.failUnlessEqual(s1.intersection(s2), s1s2)88 self.failUnlessEqual(s1.intersection(s3), s1s3)89 self.failUnlessEqual(s2.intersection(s1), s1s2)90 self.failUnlessEqual(s2.intersection(s3), s2s3)91 self.failUnlessEqual(s3.intersection(s1), s1s3)92 self.failUnlessEqual(s3.intersection(s2), s2s3)93 self.failUnlessEqual(s1.intersection(s2).intersection(s3), s1s2s3)94 self.failUnlessEqual(s1.intersection(s3).intersection(s2), s1s2s3)95 self.failUnlessEqual(s2.intersection(s1).intersection(s3), s1s2s3)96 self.failUnlessEqual(s2.intersection(s3).intersection(s1), s1s2s3)97 self.failUnlessEqual(s3.intersection(s2).intersection(s1), s1s2s3)98 self.failUnlessEqual(s3.intersection(s1).intersection(s2), s1s2s3)99 100 self.failUnlessEqual(s1 & s2, s1s2)101 self.failUnlessEqual(s1 & s3, s1s3)102 self.failUnlessEqual(s2 & s1, s1s2)103 self.failUnlessEqual(s2 & s3, s2s3)104 self.failUnlessEqual(s3 & s1, s1s3)105 self.failUnlessEqual(s3 & s2, s2s3)106 self.failUnlessEqual(s1 & s2 & s3, s1s2s3)107 self.failUnlessEqual(s1 & s3 & s2, s1s2s3)108 self.failUnlessEqual(s2 & s1 & s3, s1s2s3)109 self.failUnlessEqual(s2 & s3 & s1, s1s2s3)110 self.failUnlessEqual(s3 & s2 & s1, s1s2s3)111 self.failUnlessEqual(s3 & s1 & s2, s1s2s3)112 113 def testDifference(self):114 "lib.nltk.set.Set: difference tests"115 s1 = self._makeset(1,3,5,7,9,11,13,15,17,19,21,23,25)116 s2 = self._makeset(2,3,5,7,11,13,17,19,23)117 s3 = self._makeset(2,4,9,16,25)118 s1s2 = self._makeset(1,9,15,21,25)119 s1s3 = self._makeset(1,3,5,7,11,13,15,17,19,21,23)120 s2s3 = self._makeset(3,5,7,11,13,17,19,23)121 s2s1 = self._makeset(2)122 s3s1 = self._makeset(2,4,16)123 s3s2 = self._makeset(4,9,16,25)124 s1s2s3 = self._makeset(1,15,21)125 self.failUnlessEqual(s1.difference(s2), s1s2)126 self.failUnlessEqual(s1.difference(s3), s1s3)127 self.failUnlessEqual(s2.difference(s1), s2s1)128 self.failUnlessEqual(s2.difference(s3), s2s3)129 self.failUnlessEqual(s3.difference(s1), s3s1)130 self.failUnlessEqual(s3.difference(s2), s3s2)131 self.failUnlessEqual(s1.difference(s2).difference(s3), s1s2s3)132 133 self.failUnlessEqual(s1 - s2, s1s2)134 self.failUnlessEqual(s1 - s3, s1s3)135 self.failUnlessEqual(s2 - s1, s2s1)136 self.failUnlessEqual(s2 - s3, s2s3)137 self.failUnlessEqual(s3 - s1, s3s1)138 self.failUnlessEqual(s3 - s2, s3s2)139 self.failUnlessEqual(s1 - s2 - s3, s1s2s3)140 def testContains(self):141 "lib.nltk.set.Set: containership tests"142 s = self._makeset(2,3,5,7,11,13,17,19,23)143 self.failUnless(s.contains(11))144 self.failUnless(s.contains(2))145 self.failUnless(s.contains(23))146 self.failUnless(s.contains(11))147 self.failUnless(s.contains(7))148 self.failIf(s.contains(0))149 self.failIf(s.contains(1))150 self.failIf(s.contains(4))151 self.failIf(s.contains(24))152 self.failUnless(11 in s)153 self.failUnless(2 in s)154 self.failUnless(23 in s)155 self.failUnless(11 in s)156 self.failUnless(7 in s)157 self.failIf(0 in s)158 self.failIf(1 in s)159 self.failIf(4 in s)160 self.failIf(24 in s)161 def testCopy(self):162 "lib.nltk.set.Set: copy tests"163 s1 = self._makeset(1,2,3)164 s2 = s1.copy()165 self.failUnlessEqual(s1, self._makeset(1,2,3))166 self.failUnlessEqual(s2, self._makeset(1,2,3))167 def testRepr(self):168 "lib.nltk.set.Set: representation tests (glass-box)."169 170 s1 = self._makeset(1,2,3)171 s2 = self._makeset('a', 'b', (1, '3'))172 # These may break, depending on hasing. :-/173 self.failUnless(repr(s1) in ("{1, 2, 3}", "{1, 3, 2}",174 "{2, 1, 3}", "{2, 3, 1}",175 "{3, 2, 1}", "{3, 1, 2}"))176 self.failUnless(repr(s2) in ("{'a', 'b', (1, '3')}",177 "{'a', (1, '3'), 'b'}",178 "{'b', 'a', (1, '3')}",179 "{'b', (1, '3'), 'a'}",180 "{(1, '3'), 'b', 'a'}",181 "{(1, '3'), 'a', 'b'}"))182 self.failUnlessEqual(repr(self._makeset()), '{}')183 def testCount(self):184 "lib.nltk.set.Set: count tests"185 self.failUnlessEqual(self._makeset(1,2,3).count(), 3)186 self.failUnlessEqual(self._makeset(1,(2,3)).count(), 2)187 self.failUnlessEqual(self._makeset((1,2,3)).count(), 1)188 self.failUnlessEqual(self._makeset('abc').count(), 1)189 self.failUnlessEqual(self._makeset().count(), 0)190 self.failUnlessEqual(len(self._makeset(1,2,3)), 3)191 self.failUnlessEqual(len(self._makeset(1,(2,3))), 2)192 self.failUnlessEqual(len(self._makeset((1,2,3))), 1)193 self.failUnlessEqual(len(self._makeset('abc')), 1)194 self.failUnlessEqual(len(self._makeset()), 0)195 def testEqual(self):196 "lib.nltk.set.Set: equality comparison test"197 s1 = self._makeset(1, 2, (3, 5))198 s2 = self._makeset(2, (3, 5))199 self.failIfEqual(s1, s2)200 s2 = s2.union(self._makeset(1))201 self.failUnlessEqual(s1, s2)202 s1 = s1.union(self._makeset((1,7)))203 self.failIfEqual(s1, s2)204 s2 = s2.union(self._makeset((1,7)))205 self.failUnlessEqual(s1, s2)206 self.failUnlessEqual(self._makeset(), self._makeset())207 # Explicitly check ==/!=208 s1 = self._makeset(1, 2, (3, 5))209 s2 = self._makeset(2, (3, 5))210 self.failIf(s1 == s2)211 s2 = s2.union(self._makeset(1))212 self.failIf(s1 != s2)213 s1 = s1.union(self._makeset((1,7)))214 self.failIf(s1 == s2)215 s2 = s2.union(self._makeset((1,7)))216 self.failIf(s1 != s2)217 self.failIf(self._makeset() != self._makeset())218 # Explicitly check cmp()219 s1 = self._makeset(1, 2, (3, 5))220 s2 = self._makeset(2, (3, 5))221 self.failIf(cmp(s1,s2) == 0)222 s2 = s2.union(self._makeset(1))223 self.failIf(cmp(s1,s2) != 0)224 s1 = s1.union(self._makeset((1,7)))225 self.failIf(cmp(s1,s2) == 0)226 s2 = s2.union(self._makeset((1,7)))227 self.failIf(cmp(s1,s2) != 0)228 self.failIf(cmp(self._makeset(), self._makeset()) != 0)229 def testElements(self):230 "lib.nltk.set.Set: elements accessor tests"231 eltsLists = ([1, 3, (5,6)],232 [],233 ['asdf', ('a', 's', 'd', 'f'), 123])234 for elts in eltsLists:235 s = self._makeset(*elts)236 s_elts = s.elements()237 elts.sort()238 s_elts.sort()239 self.failUnlessEqual(elts, s_elts)240 def testHash(self):241 "lib.nltk.set.Set: hash() tests"242 s1 = self._makeset(1,2,3)243 s2 = self._makeset(1,2,3)244 self.failUnlessEqual(hash(s1), hash(s2))245 def testPrecision(self):246 "lib.nltk.set.Set: precision() tests"247 s1 = self._makeset(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)248 s2 = self._makeset(2, 4, 6, 8, 10, 12, 14, 16)249 self.failUnlessEqual(s1.precision(s1), 1)250 self.failUnlessEqual(s2.precision(s2), 1)251 self.failUnlessEqual(s1.precision(s2), 5./8)252 self.failUnlessEqual(s2.precision(s1), 5./10)253 def testRecall(self):254 "lib.nltk.set.Set: recall() tests"255 s1 = self._makeset(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)256 s2 = self._makeset(2, 4, 6, 8, 10, 12, 14, 16)257 self.failUnlessEqual(s1.recall(s1), 1)258 self.failUnlessEqual(s2.recall(s2), 1)259 self.failUnlessEqual(s1.recall(s2), 5./10)260 self.failUnlessEqual(s2.recall(s1), 5./8)261 def testFMeasure(self):262 "lib.nltk.set.Set: f_measure() tests"263 s1 = self._makeset(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)264 s2 = self._makeset(2, 4, 6, 8, 10, 12, 14, 16)265 self.failUnlessEqual(s1.f_measure(s1), 1)266 self.failUnlessEqual(s2.f_measure(s2), 1)267 self.failUnlessEqual(s1.f_measure(s2), self._fm(5./8, 5./10))268 self.failUnlessEqual(s2.f_measure(s1), self._fm(5./10, 5./8))269 # alpha=0 gives recall270 self.failUnlessEqual(s1.f_measure(s2,0), 5./10)271 self.failUnlessEqual(s2.f_measure(s1,0), 5./8)272 # alpha=1 gives precision273 self.failUnlessEqual(s1.f_measure(s2,1), 5./8)274 self.failUnlessEqual(s2.f_measure(s1,1), 5./10)275 # test some intermediate alpha values276 self.failUnlessEqual(s1.f_measure(s2, .3), self._fm(5./8, 5./10, .3))277 self.failUnlessEqual(s2.f_measure(s1, .3), self._fm(5./10, 5./8, .3))278 self.failUnlessEqual(s1.f_measure(s2, .8), self._fm(5./8, 5./10, .8))279 self.failUnlessEqual(s2.f_measure(s1, .8), self._fm(5./10, 5./8, .8))280 def _fm(self, p, r, alpha=.5):281 return 1/(alpha/p + (1-alpha)/r)282class MutableSetTestCase(SetTestCase):283 """284 Unit test cases for L{lib.nltk.set.MutableSet}285 """286 def _makeset(self, *values):287 return MutableSet(*values)288 def testInsert(self):289 "lib.nltk.set.MutableSet: insertion tests"290 s1 = MutableSet(5,7)291 self.failUnlessEqual(s1.insert(3), 1)292 self.failUnlessEqual(s1.insert(5), 0)293 self.failUnlessEqual(s1.insert(3), 0)294 self.failUnlessEqual(s1.insert(5), 0)295 self.failUnlessEqual(s1.insert(8), 1)296 self.failUnlessEqual(s1.insert(7), 0)297 self.failUnlessEqual(s1, Set(3,5,8,7))298 s2 = MutableSet('a', 'z', 'abcd', (1,2), 3)299 self.failUnlessEqual(s2.insert(1), 1)300 self.failUnlessEqual(s2.insert((1,2)), 0)301 self.failUnlessEqual(s2.insert('b'), 1)302 self.failUnlessEqual(s2.insert('abc'), 1)303 self.failUnlessEqual(s2.insert('a'), 0)304 self.failUnlessEqual(s2.insert('abcd'), 0)305 self.failUnlessEqual(s2.insert('abc'), 0)306 self.failUnlessEqual(s2.insert((1,2)), 0)307 self.failUnlessEqual(s2, Set('a', 'z', 'abcd', (1,2), 3,308 1, 'b', 'a', 'abc'))309 def testHash(self):310 "lib.nltk.set.MutableSet: hash() tests"311 s1 = MutableSet(1,2,3)312 self.failUnlessRaises(TypeError, hash, s1)313 def testCopy(self):314 "lib.nltk.set.MutableSet: copy tests"315 s1 = MutableSet(1,2,3)316 s2 = s1.copy()317 s1.insert(4)318 s2.insert(5)319 self.failUnlessEqual(s1, MutableSet(1,2,3,4))320 self.failUnlessEqual(s2, MutableSet(1,2,3,5))321 def testEqual(self):322 "lib.nltk.set.MutableSet: equality comparison test"323 s1 = MutableSet(1, 2, (3, 5))324 s2 = MutableSet(2, (3, 5))325 self.failIfEqual(s1, s2)326 s2.insert(1)327 self.failUnlessEqual(s1, s2)328 s1.insert((1,7))329 self.failIfEqual(s1, s2)330 s2.insert((1,7))331 self.failUnlessEqual(s1, s2)332 self.failUnlessEqual(MutableSet(), MutableSet())333def testsuite():334 """335 Return a PyUnit testsuite for the set module.336 """337 t1 = unittest.makeSuite(SetTestCase, 'test')338 t2 = unittest.makeSuite(MutableSetTestCase, 'test')339 return unittest.TestSuite( (t1, t2) )340def test():341 import unittest342 runner = unittest.TextTestRunner()343 runner.run(testsuite())344if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!