# unittest.py
# Walker M. White (wmw2), Lillian Lee (LJL2)
# Jan 31, 2013
"""Unit-test support functions.

This module provides "user friendly" assert functions"""
import traceback
import math
import numpy


def assert_equals(expected,received):
    """Quit the program if expected and received differ, printing minimal debug info
    
    Definition of "differ" is != . 

    Example printouts:
        Expected 4 but instead got 5
        line 1 of <stdin>
        Quitting with Error
    
        Expected 'Hi there' but instead got 'i there'
        Line 32 of /Users/me/classes/cs1110/scratch/a1/a1test.py: assertEquals("Hi there", myvar)
        Quitting with Error"""
    
    if (expected != received):
        stack = traceback.extract_stack()
        frame = stack[-2]
        print "Expected " + `expected` + " but instead got " + `received`
        if (frame[3] is None):
            suffix = ""
        else:
            suffix = ": "+frame[3]
        print "Line "+`frame[1]`+" of "+ frame[0] + suffix
        print "Quitting with Error"
        quit()


def assert_true(received):
    """Quits the program if received is not true, printing minimal debug info
    
    Example printouts:
        Expected True but instead got False
        line 1 of <stdin>
        Quitting with Error
    
        Expected True but instead got False
        Line 32 of /Users/me/classes/cs1110/scratch/a1/a1test.py: assertTrue(myvar)
        Quitting with Error"""
    
    if (not received):
        stack = traceback.extract_stack()
        frame = stack[-2]
        print "Expected True but instead got False"
        if (frame[3] is None):
            suffix = ""
        else:
            suffix = ": "+frame[3]
        print "Line "+`frame[1]`+" of "+ frame[0] + suffix
        print "Quitting with Error"
        quit()

def assert_floats_equal(expected, received):
    """Quit the program if the floats expected and received differ, printing minimal debug info
    
    The definition of "differ" here is as numpy defines it.
    
    Example printouts:
        Expected 4.5 but instead got 5.315
        line 1 of <stdin>
        Quitting with Error
    
        Expected 0.4 but instead got 0.3
        Line 32 of /Users/me/classes/cs1110/scratch/a1/a1test.py: assertEquals(0.4, 0.1+0.2)
        Quitting with Error"""

    if (not numpy.allclose([expected],[received])):
        stack = traceback.extract_stack()
        frame = stack[-2]
        print "Expected " + `expected` + " but instead got " + `received`
        if (frame[3] is None):
            suffix = ""
        else:
            suffix = ": "+frame[3]
        print "Line "+`frame[1]`+" of "+ frame[0] + suffix
        print "Quitting with Error"
        quit()