-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
103 lines (88 loc) · 4.02 KB
/
test.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
from __future__ import print_function
import sys
import os
from wand.color import Color
from wand.image import Image
from wand.drawing import Drawing
PASS_THRESHOLD = 0.999
EXAMPLE = """
*******
**********
***********
*************
************* ***
************ ***********
*************************
*************************
******* *************************
********* **********************
************ *********************
************ ********************
*********************** ******* **********
*************************************
***************************************
*******************************************
*********************************************
***********************************************
************************************************
**************************************************
***************************************************
************* ************** *******************
********** **** ******** **** *****************
********** ******** ***** ******** ****************
******** ********* ***** ********* ***************
******** ********* ***** ********** ***************
********** ********* ***** ********** *****************
*********** ********* ***** ********** *******************
************* ******** ****** ******* ********************
*************** *********** *********************
**************************************************************
**************************************************************
**************************************************************
**************************************************************
************************************************************
**********************************************************
*********************************************************
*******************************************************
****************************************************
***********************************************
**************************************
"""
def fetch_result():
text = os.popen('python pupu.py').read()
return text
def create_image(text):
split_by_rows = text.split('\n')
split_by_rows = list(filter(lambda n: len(n.strip()) > 0, split_by_rows))
rows = len(split_by_rows)
cols = max(map(lambda n: len(n), split_by_rows))
image = Image(width=cols, height=rows, background=Color('white'))
with Drawing() as draw:
for y, row in enumerate(split_by_rows):
if len(row.strip()) == 0:
continue
for x, point in enumerate(row):
if point != ' ':
draw.point(x, y)
draw(image)
return image
def calculate_similarity(a, b):
pixels = 0
match = 0
for ay, by in zip(a, b):
for apx, bpx in zip(ay, by):
if (apx == bpx):
match += 1
pixels += 1
return float(match)/pixels
def do_test():
a = create_image(EXAMPLE)
b = create_image(fetch_result())
similarity = calculate_similarity(a, b)
if similarity < PASS_THRESHOLD:
print('Validation failed. (similarity: %.2f%%)' % similarity)
sys.exit(1)
else:
print('Passed')
if __name__ == '__main__':
do_test()