File: test.py

package info (click to toggle)
python-roundrobin 0.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 116 kB
  • sloc: python: 140; makefile: 5
file content (48 lines) | stat: -rw-r--r-- 1,378 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
import unittest

import roundrobin


class WeightCase(unittest.TestCase):

    def test_basic(self):
        data = ["A", "B", "C"]
        get_next = roundrobin.basic(data)
        result = ''.join([get_next() for _ in range(7)])
        self.assertEqual(result, 'ABCABCA')

    def test_smooth(self):
        data = [("A", 5), ("B", 1), ("C", 1)]
        get_next = roundrobin.smooth(data)
        result = ''.join([get_next() for _ in range(7)])
        self.assertEqual(result, 'AABACAA')

    def test_weighted_not_smooth(self):
        data = [("A", 5), ("B", 1), ("C", 1)]
        get_next = roundrobin.weighted(data)
        result = ''.join([get_next() for _ in range(7)])
        self.assertEqual(result, 'AAAAABC')

    def test_weighted(self):
        data = [("A", 50), ("B", 35), ("C", 15)]
        get_next = roundrobin.weighted(data)

        cnt = {}
        for _ in range(100):
            key = get_next()
            cnt[key] = cnt.get(key, 0) + 1
        self.assertTrue(cnt['A'] == 50)
        self.assertTrue(cnt['B'] == 35)
        self.assertTrue(cnt['C'] == 15)

        cnt = {}
        for _ in range(200):
            key = get_next()
            cnt[key] = cnt.get(key, 0) + 1
        self.assertTrue(cnt['A'] == 100)
        self.assertTrue(cnt['B'] == 70)
        self.assertTrue(cnt['C'] == 30)


if __name__ == "__main__":
    unittest.main()