A Tester for the Check Rep

from unittest import TestCase
import sys
from random import randint

from nose.tools import raises

from softwaretesting import fixedsizequeue


MAXINT = sys.maxint
MININT = -sys.maxint - 1


class TestFixedCheckRep(TestCase):
def setUp(self):
self.size = randint(1, 100)
self.items = [randint(MININT, MAXINT) for item in range(self.size)]
self.q = fixedsizequeue.FixedsizeQueue(self.size)
return

def check_fail_reset(self):
"""
Checks if the check_rep fails then resets the q
"""

print("Testing: {0}".format(self.q))
self.assertRaises(AssertionError, self.q.check_representation)
self.q.reset()
print("Passed: {0}".format(self.q))
return

def test_okay(self):
"""
The enqueue takes a single integer and returns True if added.
"""

self.q.check_representation()
return

@raises(AssertionError)
def test_enqueue_wraparound_error(self):
"""
The tail should wrap around when it fills
"""

self.q.reset()
for item in self.items:
self.q.enqueue(item)
self.q.tail = 1
self.q.check_representation()
return

@raises(AssertionError)
def test_dequeue_wraparound_error(self):
"""
After emptying the queue, the head should be 0
"""

self.q.reset()
for item in self.items:
self.q.enqueue(item)
for item in self.items:
self.q.dequeue()
self.q.head = 1
self.q.check_representation()
return

def test_negative_error(self):
"""
The properties of the queue should never become negative.
"""

self.q.reset()
value = -1 * abs(self.items[0])
self.q.check_representation()
self.q.head = value
self.check_fail_reset()
self.q.tail = value
self.check_fail_reset()
self.q.head = value
self.check_fail_reset()


self.q.size = value
self.check_fail_reset()

self.q.max = value
self.check_fail_reset()
return

def test_negative_offset_error(self):
self.q.reset()
self.q.check_representation()
value = -1 * abs(self.items[0])
self.q.head += value
self.q.size -= value
self.check_fail_reset()

self.q.head -= value
self.q.size += value
self.check_fail_reset()
return
# end class TestCheckRep