Source

ginsfsm / ginsfsm / tests / test_circular.py

The default branch has multiple heads

import unittest
from ginsfsm.circular import (
    Circular,
    CircularFullBufferError,
)


class TestCircular(unittest.TestCase):
    def setUp(self):
        self.circular = Circular(5)

    def test_getdata_putdata_without_overwrite(self):
        data = self.circular.getdata(1)
        self.assertEqual(data, b'')

        count = self.circular.free_space()
        self.assertEqual(count, 5)

        count = self.circular.putdata("123")
        self.assertEqual(count, 3)
        count = self.circular.free_space()
        self.assertEqual(count, 2)

        count = self.circular.putdata("ab")
        self.assertEqual(count, 2)
        count = self.circular.free_space()
        self.assertEqual(count, 0)

        self.assertRaises(CircularFullBufferError,
            self.circular.putdata,
            "XX")

        data = self.circular.getdata(5)
        self.assertEqual(data, b"123ab")

        data = self.circular.getdata(1)
        self.assertEqual(data, b'')

        count = self.circular.putdata("qw")
        self.assertEqual(count, 2)
        data = self.circular.getdata()
        self.assertEqual(data, b'qw')

    def test_getdata_putdata_with_overwrite(self):
        count = self.circular.putdata("123")
        self.assertEqual(count, 3)
        count = self.circular.free_space()
        self.assertEqual(count, 2)

        self.assertRaises(CircularFullBufferError,
            self.circular.putdata,
            "abc")

        data = self.circular.getdata(2)
        self.assertEqual(data, b"12")
        count = self.circular.free_space()
        self.assertEqual(count, 4)
        count = self.circular.busy_space()
        self.assertEqual(count, 1)

        count = self.circular.putdata("abcd")
        self.assertEqual(count, 4)

        data = self.circular.getdata(0)
        self.assertEqual(data, b"3abcd")
        count = self.circular.free_space()
        self.assertEqual(count, 5)

        count = self.circular.putdata("zxcvb")
        self.assertEqual(count, 5)
        count = self.circular.free_space()
        self.assertEqual(count, 0)

        data = self.circular.getdata(4)
        self.assertEqual(data, b"zxcv")
        count = self.circular.free_space()
        self.assertEqual(count, 4)

        data = self.circular.getdata(3)
        self.assertEqual(data, b"")
        count = self.circular.free_space()
        self.assertEqual(count, 5)

if __name__ == "__main__":
    circular = Circular(5)

    circular.putdata("123")
    print circular
    circular.getdata(2)
    print circular

    circular.putdata("abcd")
    print circular

    circular.getdata(0)
    print circular