test_sim.py :  » Network » Torrent-Swapper » swapper » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Torrent Swapper 
Torrent Swapper » swapper » test » test_sim.py
import os
import tempfile
import unittest
from sets import Set
from random import shuffle

from Swapper.BuddyCast.similarity import *

pref1 = [1,3,6,8,9,0,2,7,5,4]
pref2 = [1,2,3,4,5,6,7,8,9,0]
pref3 = [1,3,5,7,9, 11, 13]
pref4 = [11, 24, 25, 64]
pref5 = []
pref6 = [1, 66, 77, 88, 99, 100, 11]
        
class TestSim(unittest.TestCase):
    
    def setUp(self):
        pass
        
    def tearDown(self):
        pass
        
    def test_similarity(self):
        
        assert cooccurrence(pref1[:], pref2[:]) == 10 and P2PSim(pref1[:], pref2[:]) == 1000, \
              (cooccurrence(pref1[:], pref2[:]), P2PSim(pref1[:], pref2[:]))
        assert cooccurrence(pref1[:], pref3[:]) == 5  and P2PSim(pref1[:], pref3[:]) == 597
        assert cooccurrence(pref1[:], pref4[:]) == 0  and P2PSim(pref1[:], pref4[:]) == 0
        assert cooccurrence(pref1[:], pref5[:]) == 0  and P2PSim(pref1[:], pref5[:]) == 0
        assert cooccurrence(pref1[:], pref6[:]) == 1  and P2PSim(pref1[:], pref6[:]) == 119

    def test_selectByProbability(self):
        #pref = pref1
        #pref = range(100)
        pref = range(10)
        shuffle(pref)
        print pref
        stat = [0] * len(pref)
        for j in xrange(200000):
            x = selectByProbability(pref[:], 1, smooth=2)
            for i in x:
                stat[i] += 1
        base = min(stat)
        if base == 0:
            x = stat[:]
            x.sort()
            for y in x:
                if y > 0:
                    base = y
                    break
        for i in xrange(len(pref)):
            stat[i] = 1.0*stat[i]/base
            print "%.2f"%stat[i]
            
    def xxtest_selectByProbability2(self):
        pref = range(10)
        shuffle(pref)
        stat = {}
        for p in pref:
            k = 'peer'+str(p)
            stat[k] = 0
        for j in xrange(100000):
            v = []
            for p in pref:
                k = 'peer'+str(p)
                v.append(k)
            x = selectByProbability(pref[:], 1, smooth=0)
            for i in x:
                stat[i] += 1
        min = 10**10
        for k in stat:
            if stat[k] > 0 and stat[k] < min:
                min = stat[k]
        for k in stat:
            stat[k] = 1.0*stat[k] / min
            print k, "%.2f"%stat[k]
        
def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestSim))
    
    return suite
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.