-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_S2.py
161 lines (124 loc) · 3.74 KB
/
test_S2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 29 21:44:54 2019
@author: chamiotk
"""
import pytest
import S1_algotools as s1
def inc_(x):
return x+1
def test_inc():
assert inc_(3)==4
def test_divide_by_zero():
with pytest.raises(ZeroDivisionError):
1/0
####
# Average unit tests
####
def test_average_correct():
tab_list=[1,2,3,-4,6,-9]
assert s1.average_above_zero(tab_list) == 3
def test_average_not_a_list():
with pytest.raises(TypeError):
s1.average_above_zero(3)
def test_average_empty_list():
with pytest.raises(ValueError):
s1.average_above_zero([])
def test_average_not_a_number_list():
with pytest.raises(ValueError):
s1.average_above_zero(["3","e"])
def test_average_only_negative_values():
tab_list = [-1, -6, -4, -6]
with pytest.raises(ZeroDivisionError):
s1.average_above_zero(tab_list)
####
# Max value unit tests
####
def test_max_value_correct():
tab_list=[1,2,3,-4,6,-9]
assert s1.max_value(tab_list) == (6, 4)
def test_max_value_not_a_list():
with pytest.raises(TypeError):
s1.max_value(3)
def test_max_value_empty_list():
with pytest.raises(ValueError):
s1.max_value([])
####
# Reverse table unit tests
####
def test_reverse_correct():
tab_list=[1,2,3,-4,6,-9]
assert s1.reverse_table(tab_list) == [-9, 6,-4, 3, 2, 1]
def test_reverse_not_a_list():
with pytest.raises(TypeError):
s1.reverse_table(3)
def test_reverse_empty_list():
with pytest.raises(ValueError):
s1.reverse_table([])
####
# Bounding box unit tests
####
import cv2
import numpy as np
def test_bounding_box_correct():
img=cv2.imread('img.png',0)
assert (s1.roi_bbox(img) == np.array([[ 91, 95], [ 91, 523], [434, 523], [434, 95]])).prod()
def test_bounding_box_not_a_numpy_array():
with pytest.raises(TypeError):
s1.roi_bbox(3)
def test_bouding_box_empty_array():
with pytest.raises(ValueError):
s1.roi_bbox(np.array([]))
####
# Random fill sparse unit tests
####
def test_random_fill_correct():
tab_list = np.ones((10,10), dtype=np.chararray)
tab_list *= ' '
tab_filled = s1.random_fill_sparse(tab_list,3)
assert len(np.argwhere(tab_filled=='X')) == 3
def test_random_fill_not_a_numpy_array():
with pytest.raises(TypeError):
s1.random_fill_sparse(3,3)
def test_random_fill_empty_list():
with pytest.raises(ValueError):
s1.random_fill_sparse(np.array([]),3)
def test_random_fill_not_a_number():
tab_list = np.array([1,2,3,4,5,6])
with pytest.raises(TypeError):
s1.random_fill_sparse(tab_list,"d")
####
# Remove whitespace unit tests
####
def test_remove_whitespace_correct():
string = "Texte avec des espaces "
assert s1.remove_whitespace(string) == "Texteavecdesespaces"
def test_remove_whitespace_empty_string():
string = ""
with pytest.raises(ValueError):
s1.remove_whitespace(string)
####
# Bubble sort unit tests
####
def test_bubble_correct():
tab_list = [10, 15, 7, 1, 3, 3, 9]
assert s1.sort_bubble(tab_list) == [1, 3, 3, 7, 9, 10, 15]
def test_bubble_not_a_list():
with pytest.raises(TypeError):
s1.sort_bubble(3)
def test_bubble_empty_list():
with pytest.raises(ValueError):
s1.sort_bubble([])
####
# Selective sort unit tests
####
def test_selective_correct():
tab_list = [10, 15, 7, 1, 3, 3, 9]
assert s1.sort_selective(tab_list) == [1, 3, 3, 7, 9, 10, 15]
def test_selective_not_a_list():
with pytest.raises(TypeError):
s1.sort_selective(3)
def test_selective_empty_list():
with pytest.raises(ValueError):
s1.sort_selective([])