aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test/DataStructures.py
blob: cab3a316b5fada14f4beb0a9652e04cadca6f32b (plain)
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
#!/usr/bin/env python

import unittest

from argaze import DataStructures

class TestTimeStampedBufferClass(unittest.TestCase):
    """Test TimeStampedBuffer class."""

    def test_new(self):
        """Test TimeStampedBuffer creation."""
        
        # Check TimeStampedBuffer length after creation
        self.assertEqual(len(DataStructures.TimeStampedBuffer()), 0)
        self.assertEqual(len(DataStructures.TimeStampedBuffer({0: ""})), 1)
        self.assertEqual(len(DataStructures.TimeStampedBuffer({0.1: ""})), 1)
        self.assertEqual(len(DataStructures.TimeStampedBuffer({0: "A", 1: "B"})), 2)

        # Check TimeStampedBuffer keys after creation
        self.assertEqual(list(DataStructures.TimeStampedBuffer().keys()), [])
        self.assertEqual(list(DataStructures.TimeStampedBuffer({0: ""}).keys()), [0])
        self.assertEqual(list(DataStructures.TimeStampedBuffer({0.1: ""}).keys()), [0.1])
        self.assertEqual(list(DataStructures.TimeStampedBuffer({0: "A", 1: "B"}).keys()), [0, 1])

        # Check TimeStampedBuffer items after creation
        self.assertEqual(list(DataStructures.TimeStampedBuffer().items()), [])
        self.assertEqual(list(DataStructures.TimeStampedBuffer({0: ""}).items()), [(0, "")])
        self.assertEqual(list(DataStructures.TimeStampedBuffer({0.1: ""}).items()), [(0.1, "")])
        self.assertEqual(list(DataStructures.TimeStampedBuffer({0: "A", 1: "B"}).items()), [(0, "A"), (1, "B")])

        # Check that TimeStampedBuffer creation fails when keys are not numbers
        with self.assertRaises(AssertionError):

            DataStructures.TimeStampedBuffer({"first": ""})

    def test_str(self):
        """Test TimeStampedBuffer string representation."""

        self.assertEqual(str(DataStructures.TimeStampedBuffer()), "{}")
        self.assertEqual(str(DataStructures.TimeStampedBuffer({0: ""})), "{\"0\": \"\"}")
        self.assertEqual(str(DataStructures.TimeStampedBuffer({0.1: ""})), "{\"0.1\": \"\"}")

    def test_append(self):
        """Test TimeStampedBuffer append method."""

        ts_buffer = DataStructures.TimeStampedBuffer({0: "A", 1: "B"})
        ts_buffer_next = DataStructures.TimeStampedBuffer({2: "C", 3: "D"})

        self.assertEqual(len(ts_buffer.append(ts_buffer_next)), 4)
        self.assertEqual(list(ts_buffer.append(ts_buffer_next).keys()), [0, 1, 2, 3])

if __name__ == '__main__':

    unittest.main()