# -*- coding: utf-8 -*-
"""
Created on Sat Dec 22 11:55:48 2018

@author: mmeites
"""

""" Basic introduction to unit testing with Python
@brief get started with python testing by looking at https://docs.pytest.org/en/latest/getting-started.html#getstarted. Note that any script to be ran within the python testing framework (pytest) should follow the standard test discovery rules (https://docs.pytest.org/en/latest/goodpractices.html#test-discovery)
"""

def test_basicTrue():
    """ one of the simplest test that does nothing except saying it works..."""
    assert True


#testing session 1 functions
def load_S1_script():
    """
        utility function that tris to load the script written along the first lesson
        @throws an ImportError exception if the script file does not exist
        @return the script as a loaded module
    """
    S1_script_filename='assignments/Session1/S1_algotools.py'
    import imp
    s1_algotools=imp.load_source('session_1_script', S1_script_filename)
    return  s1_algotools




#load the scripts to check
def test_session1script_exists():
    try:
        load_S1_script()
        assert True
    except  ImportError:
        print('Expected script not found, carrefuly check the assignement instructions ')
        assert False

def check_S1_selective_average(testList):
    ##
    # utility function that asserts if load_S1_script().average_above_zero works fine
    # @param testList a list of values onto average_above_zero is applied
    # @test ensures the function returns the correct average value
    import numpy as np
    #another way to process the positive elements average to compare with
    positive_elements_float_array=np.array([i for i in testList if i >= 0], dtype=float)
    reference_average_value=np.mean(positive_elements_float_array)
    assert load_S1_script().average_above_zero(testList) ==reference_average_value # mm

def test_S1_selective_average_non_zeros_values():
    ##
    # @test validates average_above_zero works fine with integer values >0
    check_S1_selective_average([1,2,3,4,-7])

def test_S1_selective_average_with_zeros_values():
    ##
    # @test validates average_above_zero works fine with integer values >=0
    check_S1_selective_average([0,1,2,3,4,-7])

def test_S1_selective_average_with_negative_values():
    ##
    # @test validates average_above_zero works fine with integer values <=0
    check_S1_selective_average([0,-7])

def test_S1_selective_average_with_string_values():
    ##
    # @test validates average_above_zero works fine with integer values <=0
    check_S1_selective_average(['ab','c'])

def test_S1_selective_average_with_empty_list():
    ##
    # @test validates average_above_zero works fine with an empty list
    try:
        check_S1_selective_average([])
        assert False
    except ValueError:
        assert True

###
#Exercice2	
###
def check_S1_max_value(testList):	
# @param testList a list of values onto max_value is applied
   #import numpy as np
   #another way to search the maximum value in an array
   #elements_float_array=np.array([i for i in testList if i >= 0 ], dtype=float)
   #reference_max_value = np.amax(elements_float_array)
   assert load_S1_script().max_value(testList)  == ( 4,3) , "values aren't equals"
   
def test_S1_max_values_basic():
	check_S1_max_value([1,2,3,4,-7])
	
def test_S1_max_values_with_negative_values():
	check_S1_max_value([-3,-7])
    
def test_S1_max_values_with_string_values():
	check_S1_max_value(['ab','c'])
	
def test_S1_max_value_empty_list():
    try:
        check_S1_max_value([])
        assert False
    except ValueError:
        assert True

	

###
#Exercice3	
###
def check_S1_reverse_table(testList):	
    assert load_S1_script().reverse_table(testList)   ==[-5, 3, 2, 1]
    
def test_S1_reverse_table_basic():
	check_S1_reverse_table([1,2,3,-7])
    
def test_S1_reverse_table_with_string_values():
	check_S1_max_value(['ab','c'])
    
def test_S1_reverse_table_empty_list():
    try:
        check_S1_reverse_table([])
        assert False
    except ValueError:
        assert True
	
        
###
#Exercice4	
###
def check_S1_bounding_box(inputMat):
    import numpy as np	
    np.testing.assert_array_equal(load_S1_script().roi_bbox(inputMat),[[2,3],[2,4],[3,3],[3,4]])
    
def test_S1_bounding_box_basic():
    inputMat=[[False,False,False,False,False,False]
 [False,False,False,False,False,False]
 [False,False,False,True,True,False]
 [False,False,False,True,True,False]
 [False,False,False,False,False,False]]
    check_S1_bounding_box(inputMat)

def test_S1_bounding_box_with_integer_values():
    inputMat=[[0,1,2,3,4,5],[5,4,3,2,1,0]]
    check_S1_bounding_box(inputMat)
    
def test_S1_bounding_box_without_matrix_argument():
    inputMat=[1]
    check_S1_bounding_box(inputMat)
