Statistics
| Branch: | Revision:

iof-tools / tests / test_chain_gadget.py @ 4bc771cd

History | View | Annotate | Download (6.25 KB)

1
from chain_gadget import gen_chain_gadget, ATTR_NODE_TYPE, \
2
    ATTR_EDGE_TYPE, ATTR_TIMER, DEFAULT_MRAI_TIMER
3

    
4

    
5
def test_mini_chain():
6
    """
7
    test the smallest possible chain, i.e., with only a single ring. The
8
    topology is the following
9
          1
10
       2     0
11
    with the following edges:
12
    - inner ring: (1, 0), (2, 1)
13
    - first node to all inner nodes 1: (2, 0)
14
    """
15
    g = gen_chain_gadget(1, 1, False, "T", "transit", True)
16
    timers = {2: DEFAULT_MRAI_TIMER, 1: DEFAULT_MRAI_TIMER,
17
              0: DEFAULT_MRAI_TIMER/2}
18
    assert(g.number_of_nodes() == 3)
19
    assert((1, 0) in g.edges)
20
    assert((2, 1) in g.edges)
21
    assert((2, 0) in g.edges)
22
    for i in range(g.number_of_nodes()):
23
        assert(g.nodes[i][ATTR_NODE_TYPE] == "T")
24
        assert(g.nodes[i][ATTR_TIMER] == timers[i])
25
    for e in g.edges:
26
        assert(g.edges[e][ATTR_EDGE_TYPE] == "transit")
27

    
28

    
29
def test_simplest_chain():
30
    """
31
    test one of the simplest possible chains, i.e., the one without outer nodes
32
    2 rings and 1 inner node. The topology is the following:
33
          3     1
34
       4     2     0
35
    with the following edges:
36
    - inner ring 1: (1, 0), (2, 1)
37
    - first node to all inner nodes 1: (2, 0)
38
    - inner ring 2: (3, 2), (4, 3)
39
    - first node to all inner nodes 2: (4, 2)
40
    """
41
    g = gen_chain_gadget(2, 1, False, "T", "transit", True)
42
    timers = {5: DEFAULT_MRAI_TIMER, 4: DEFAULT_MRAI_TIMER,
43
              3: DEFAULT_MRAI_TIMER, 2: DEFAULT_MRAI_TIMER/2,
44
              1: DEFAULT_MRAI_TIMER/2, 0: DEFAULT_MRAI_TIMER/4}
45
    assert(g.number_of_nodes() == 5)
46
    assert((1, 0) in g.edges)
47
    assert((2, 1) in g.edges)
48
    assert((2, 0) in g.edges)
49
    assert((3, 2) in g.edges)
50
    assert((4, 3) in g.edges)
51
    assert((4, 2) in g.edges)
52
    for i in range(g.number_of_nodes()):
53
        assert(g.nodes[i][ATTR_NODE_TYPE] == "T")
54
        assert(g.nodes[i][ATTR_TIMER] == timers[i])
55
    for e in g.edges:
56
        assert(g.edges[e][ATTR_EDGE_TYPE] == "transit")
57

    
58

    
59
def test_no_outer_chain():
60
    """
61
    test one of the simplest possible chains, i.e., the one without outer nodes
62
    2 rings and 2 inner nodes. The topology is the following:
63
          5  4     2  1
64
       6        3        0
65
    with the following edges:
66
    - inner ring 1: (1, 0), (2, 1), (3, 2)
67
    - first node to all inner nodes 1: (2, 0), (3, 0)
68
    - inner ring 2: (4, 3), (5, 4), (6, 5)
69
    - first node to all inner nodes 2: (5, 3), (6, 3)
70
    """
71
    g = gen_chain_gadget(2, 2, False, "T", "transit", True)
72
    timers = {7: DEFAULT_MRAI_TIMER, 6: DEFAULT_MRAI_TIMER,
73
              5: DEFAULT_MRAI_TIMER, 4: DEFAULT_MRAI_TIMER,
74
              3: DEFAULT_MRAI_TIMER/2, 2: DEFAULT_MRAI_TIMER/2,
75
              1: DEFAULT_MRAI_TIMER/2, 0: DEFAULT_MRAI_TIMER/4}
76
    assert(g.number_of_nodes() == 7)
77
    assert((1, 0) in g.edges)
78
    assert((2, 1) in g.edges)
79
    assert((3, 2) in g.edges)
80
    assert((2, 0) in g.edges)
81
    assert((3, 0) in g.edges)
82
    assert((4, 3) in g.edges)
83
    assert((5, 4) in g.edges)
84
    assert((6, 5) in g.edges)
85
    assert((5, 3) in g.edges)
86
    assert((6, 3) in g.edges)
87
    for i in range(g.number_of_nodes()):
88
        assert(g.nodes[i][ATTR_NODE_TYPE] == "T")
89
        assert(g.nodes[i][ATTR_TIMER] == timers[i])
90
    for e in g.edges:
91
        assert(g.edges[e][ATTR_EDGE_TYPE] == "transit")
92

    
93

    
94
def test_simple_chain():
95
    """
96
    test the simple chain with 1 ring with 1 inner node. The topology is
97
    the following
98
           3   1
99
             2
100
         4       0
101
    with the following edges:
102
    - inner ring: (2, 0), (4, 2)
103
    - outer ring: (1, 0), (2, 1), (3, 2), (4, 3)
104
    - first node to all inner nodes: (4, 0)
105
    """
106
    g = gen_chain_gadget(1, 1, True, "T", "transit", True)
107
    assert(g.number_of_nodes() == 5)
108
    assert((2, 0) in g.edges)
109
    assert((4, 2) in g.edges)
110
    assert((1, 0) in g.edges)
111
    assert((2, 1) in g.edges)
112
    assert((3, 2) in g.edges)
113
    assert((4, 3) in g.edges)
114
    assert((4, 0) in g.edges)
115
    timers = dict([(i, DEFAULT_MRAI_TIMER) for i in range(6)])
116
    timers[0] = DEFAULT_MRAI_TIMER/2
117
    for i in range(g.number_of_nodes()):
118
        assert(g.nodes[i][ATTR_NODE_TYPE] == "T")
119
        assert(g.nodes[i][ATTR_TIMER] == timers[i])
120
    for e in g.edges:
121
        assert(g.edges[e][ATTR_EDGE_TYPE] == "transit")
122

    
123

    
124
def test_chain():
125
    """
126
    tests a more sophisticated chain with additional nodes. 2 rings with 2
127
    inner nodes. The topology is the following
128
            11      9     7     5     3     1
129
                10     8           4     2
130
        12                   6                 0
131
    with the following edges:
132
    - inner ring 1: (2, 0), (4, 2), (6, 4)
133
    - outer ring 1: (1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5)
134
    - first node to all inner nodes 1: (4, 0), (6, 0)
135
    - inner ring 2: (8, 6), (10, 8), (12, 10)
136
    - outer ring 2: (7, 6), (8, 7), (9, 8), (10, 9), (11, 10), (12, 11)
137
    - first node to all inner nodes 2: (10, 6), (12, 6)
138
    """
139
    g = gen_chain_gadget(2, 2, True, "T", "transit", True)
140
    timers = {13: DEFAULT_MRAI_TIMER, 12: DEFAULT_MRAI_TIMER,
141
              11: DEFAULT_MRAI_TIMER, 10: DEFAULT_MRAI_TIMER,
142
              9: DEFAULT_MRAI_TIMER, 8: DEFAULT_MRAI_TIMER,
143
              7: DEFAULT_MRAI_TIMER, 6: DEFAULT_MRAI_TIMER/2,
144
              5: DEFAULT_MRAI_TIMER/2, 4: DEFAULT_MRAI_TIMER/2,
145
              3: DEFAULT_MRAI_TIMER/2, 2: DEFAULT_MRAI_TIMER/2,
146
              1: DEFAULT_MRAI_TIMER/2, 0: DEFAULT_MRAI_TIMER/4}
147
    assert(g.number_of_nodes() == 13)
148
    assert((2, 0) in g.edges)
149
    assert((4, 2) in g.edges)
150
    assert((6, 4) in g.edges)
151
    assert((1, 0) in g.edges)
152
    assert((2, 1) in g.edges)
153
    assert((3, 2) in g.edges)
154
    assert((4, 3) in g.edges)
155
    assert((5, 4) in g.edges)
156
    assert((6, 5) in g.edges)
157
    assert((4, 0) in g.edges)
158
    assert((6, 0) in g.edges)
159
    assert((8, 6) in g.edges)
160
    assert((10, 8) in g.edges)
161
    assert((12, 10) in g.edges)
162
    assert((7, 6) in g.edges)
163
    assert((8, 7) in g.edges)
164
    assert((9, 8) in g.edges)
165
    assert((10, 9) in g.edges)
166
    assert((11, 10) in g.edges)
167
    assert((12, 11) in g.edges)
168
    assert((10, 6) in g.edges)
169
    assert((12, 6) in g.edges)
170
    for i in range(g.number_of_nodes()):
171
        print(i)
172
        assert(g.nodes[i][ATTR_NODE_TYPE] == "T")
173
        assert(g.nodes[i][ATTR_TIMER] == timers[i])
174
    for e in g.edges:
175
        assert(g.edges[e][ATTR_EDGE_TYPE] == "transit")
176