#!/usr/bin/python import os import unittest #mport functools import exceptions import ring_buffer_mod def cat(filename): file_ = open(filename, 'r') retval = file_.read() file_.close() return retval class test_ring_buffer(unittest.TestCase): # def setUp(self): # self.seq = range(10) # def tearDown(self): # pass # assertEqual # assert_ # assertRaises def fill_with_alphabet(self): self.rb = ring_buffer_mod.Ring_buffer(buffer_size=27) for string_ in ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz' ]: self.rb.add_from_string(string_) def test_empty(self): rb = ring_buffer_mod.Ring_buffer(50) self.assert_(rb.empty()) def test_full(self): #self.assertEqual(self.seq, range(10)) self.fill_with_alphabet() self.assert_(self.rb.full()) def test_alphabet_subtract(self): #self.assertEqual(self.seq, range(10)) self.fill_with_alphabet() self.assert_(self.rb.subtract_to_string(26) == 'abcdefghijklmnopqrstuvwxyz') def test_alphabet_peek(self): #self.assertEqual(self.seq, range(10)) self.fill_with_alphabet() self.assert_(self.rb.peek_to_string(26) == 'abcdefghijklmnopqrstuvwxyz') def test_simple_len(self): rb = ring_buffer_mod.Ring_buffer(buffer_size = 27) rb.add_from_string('abcdefghijklm') length = len(rb) self.assert_(length == 13, "Expected length of 13, got %s" % length) def test_partial_simple_remove(self): rb = ring_buffer_mod.Ring_buffer(buffer_size = 27) rb.add_from_string('abcdefghijklm') length = len(rb) string_ = rb.subtract_to_string(5) self.assert_(string_ == 'abcde') def test_len_add_13_sub_5(self): rb = ring_buffer_mod.Ring_buffer(buffer_size = 27) rb.add_from_string('abcdefghijklm') string_ = rb.subtract_to_string(5) length = len(rb) self.assert_(length == 8, "Expected length of 8, got %s" % length) def test_peek_add_5_sub_3(self): rb = ring_buffer_mod.Ring_buffer(buffer_size = 10) rb.add_from_string('12345') # should now have 12345 string_ = rb.subtract_to_string(3) self.assert_(string_ == '123') self.assert_(rb.peek_to_string() == '45') def test_peek_add_5_sub_3_add_7(self): rb = ring_buffer_mod.Ring_buffer(buffer_size = 10) rb.add_from_string('12345') # should now have 12345 string_ = rb.subtract_to_string(3) self.assert_(string_ == '123') self.assert_(rb.peek_to_string() == '45') # this should wrap rb.add_from_string('6789012') value = rb.peek_to_string() self.assert_(value == '456789012', "Got %s" % value) def test_len_add_5_sub_3_add_7(self): rb = ring_buffer_mod.Ring_buffer(buffer_size = 10) rb.add_from_string('12345') # should now have 12345 string_ = rb.subtract_to_string(3) # should now have 45 rb.add_from_string('6789012') # should now have 456789012 length = len(rb) self.assert_(length == 9, "Expected length of 9, got %s" % length) def test_mirrored_in_string(self): rb = ring_buffer_mod.Ring_buffer(buffer_size = 10) s = '' for i in xrange(5): rb.add_from_string('abc') s += 'abc' dummy = rb.subtract_to_string(2) s = s[2:] self.assert_(rb.peek_to_string() == s) def test_fileno_to_file(self): file_ = open('input-file', 'w') file_.write('abc') file_.close() rb = ring_buffer_mod.Ring_buffer(buffer_size = 16) for i in xrange(5): input_fileno = os.open('input-file', os.O_RDONLY) rb.add_from_fileno(input_fileno, 3) os.close(input_fileno) self.assert_(rb.peek_to_string() == 'abc' * 5) def test_fileno_from_file(self): file_ = open('input-file', 'w') file_.write('abc' * 15) file_.close() rb = ring_buffer_mod.Ring_buffer(buffer_size = 16) input_fileno = os.open('input-file', os.O_RDONLY) for i in xrange(5): rb.add_from_fileno(input_fileno, 3) os.close(input_fileno) self.assert_(rb.peek_to_string() == 'abc' * 5) def test_fileno_from_file_overflow(self): file_ = open('input-file', 'w') file_.write('abc' * 15) file_.close() rb = ring_buffer_mod.Ring_buffer(buffer_size = 10) input_fileno = os.open('input-file', os.O_RDONLY) # with a buffer size of n, we can only put n-1 items in the buffer for i in xrange(9): rb.add_from_fileno(input_fileno, 1) self.assertRaises(exceptions.BufferError, rb.add_from_fileno, input_fileno, 1) def test_from_string_overflow(self): rb = ring_buffer_mod.Ring_buffer(buffer_size = 10) # with a buffer size of n, we can only put n-1 items in the buffer for i in xrange(9): rb.add_from_string('a') self.assertRaises(exceptions.BufferError, rb.add_from_string, 'b') unittest.main()