"""Unit test for the adaptive sampling""" # Copyright 2005, 2006 EIAO Consoritum # This program is distributed under the terms of the GNU General # Public License. # # This file is part of the European Internet Accessibility Observatory # (EIAO) # # EIAO is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # EIAO is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with EIAO; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, # MA 02110-1301 USA __author__ = "Morten Goodwin Olsen" __maintainer__ = "Morten Goodwin Olsen" __version__ = "$Id$" import adaptivesampling from adaptivesampling import * import unittest, doctest result = [] result.append(doctest.testmod(adaptivesampling)) class TestSequenceFunctions(unittest.TestCase): def setUp(self): reload(adaptivesampling) from urlrep import URLRepUtils3 self.urlrep = URLRepUtils3(sco=sc) def testSimpleAdaptiveSampling(self): """Testing that agregation function works""" result = getEARLCWAM2(''.join(open('input.earl','r').readlines())) #Need to round of to closest 10 decimals, because of Python float representation #print result self.assert_(round(result,10)==round(0.5,10)) def testUWEM10Aggregation(self): """Testing that the UWEM1.0 Aggregation works""" result = getEARLCWAM2(''.join(open('input3.earl','r').readlines())) #Need to round of to closest 10 decimals, because of Python float representation #input3.earl contains two equal fails, the result should still be 0.05 TG: no not anymore #print result self.assert_(round(result,10)==round(0.666666666667,10)) def testEqualInputEqualResult(self): """Testing that equal input gives equal results""" self.assert_(getEARLCWAM2(''.join(open('input.earl','r').readlines()))==getEARLCWAM2(''.join(open('input.earl','r').readlines()))) def testSimpleDynamic(self): """Testing that dynamic sampling of one sample is the same as aggregation of one sample""" self.assert_(getEARLCWAM2(''.join(open('input.earl','r').readlines()))==dynamic(0.05,'www.test.no',[''.join(open('input.earl','r').readlines())],self.urlrep,1,10)[1]) def testDynamicReachesTrueValues(self): """Testing that the adaptive sampling is actually adaptive""" finito = False while not finito: dynamic(0.05,'www.test.no',[''.join(open('input.earl','r').readlines())],self.urlrep,1,10) dynamic(0.05,'www.test.no',[''.join(open('input2.earl','r').readlines())],self.urlrep,1,10) dynamic(0.05,'www.test.no',[''.join(open('input3.earl','r').readlines())],self.urlrep,1,10) dynamic(0.05,'www.test.no',[''.join(open('input4.earl','r').readlines())],self.urlrep,1,10) finito,res = dynamic(0.05,'www.test.no',[''.join(open('input5.earl','r').readlines())],self.urlrep,1,10)[0:2] self.urlrep.addSample('www.test.no',5) self.assert_(res==getAvgCWAM('www.test.no',1)[0]) def testRetrieveUnwantedSiteInformation(self): """Testing that correct exceptions are raised when unwanted site information is retrieved""" fail = False try: getAvgCWAM('www.test.no',1) except NoSiteInformationError: fail = True self.assert_(fail) fail = False dynamic(0.05,'www.test.no',[''.join(open('input.earl','r').readlines())],self.urlrep,1,10) try: getAvgCWAM('www.test.no',1) except NoSiteInformationError: fail = True self.assert_(not fail) def testReachCorrectResult(self): """Testing that the dynamic sampling reaches correct results""" finito = False while not finito: dynamic(0.05,'www.test.no',[''.join(open('input.earl','r').readlines())],self.urlrep,1,10) dynamic(0.05,'www.test.no',[''.join(open('input2.earl','r').readlines())],self.urlrep,1,10) dynamic(0.05,'www.test.no',[''.join(open('input3.earl','r').readlines())],self.urlrep,1,10) dynamic(0.05,'www.test.no',[''.join(open('input4.earl','r').readlines())],self.urlrep,1,10) finito,res = dynamic(0.05,'www.test.no',[''.join(open('input5.earl','r').readlines())],self.urlrep,1,10)[0:2] self.urlrep.addSample('www.test.no',5) #print getAvgCWAM('www.test.no')[0] self.assert_(round(getAvgCWAM('www.test.no',1)[0], 10)==round(0.0748898678414, 10)) if __name__ == "__main__": unittest.main()