File: rng.py

package info (click to toggle)
python-igraph 0.7.1.post6-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,288 kB
  • ctags: 2,287
  • sloc: ansic: 20,069; python: 14,108; sh: 56; makefile: 9
file content (54 lines) | stat: -rw-r--r-- 1,240 bytes parent folder | download | duplicates (2)
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
import random
import unittest
from igraph import *


class FakeRNG(object):
    @staticmethod
    def random():
        return 0.1

    @staticmethod
    def randint(a, b):
        return a

    @staticmethod
    def gauss(mu, sigma):
        return 0.3

class InvalidRNG(object):
    pass


class RandomNumberGeneratorTests(unittest.TestCase):
    def tearDown(self):
        set_random_number_generator(random)

    def testSetRandomNumberGenerator(self):
        set_random_number_generator(FakeRNG)
        graph = Graph.GRG(10, 0.2)
        self.assertEqual(graph.vs["x"], [0.1] * 10)
        self.assertEqual(graph.vs["y"], [0.1] * 10)

        self.assertRaises(AttributeError, set_random_number_generator,
                InvalidRNG)

    def testSeeding(self):
        state = random.getstate()
        g1 = Graph.Erdos_Renyi(n=1000, m=5000)
        random.setstate(state)
        g2 = Graph.Erdos_Renyi(n=1000, m=5000)
        self.assertTrue(g1.get_edgelist() == g2.get_edgelist())


def suite():
    random_suite = unittest.makeSuite(RandomNumberGeneratorTests)
    return unittest.TestSuite([random_suite])

def test():
    runner = unittest.TextTestRunner()
    runner.run(suite())
    
if __name__ == "__main__":
    test()