tests/subfiles.py

Wed, 29 May 2019 16:36:23 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 29 May 2019 16:36:23 +0300
changeset 53
0cc196c634f1
parent 37
e46fa477007b
child 54
0c686d10eb49
permissions
-rw-r--r--

fixed the formatting of the license

24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
1 from testsuite import warning, error
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
2 from geometry import *
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
3 from os.path import dirname
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
4 from pathlib import Path
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
5 from configparser import ConfigParser
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
6 ini_path = Path(dirname(__file__)) / 'library-standards.ini'
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
7 library_standards = ConfigParser()
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
8
37
e46fa477007b Fix operation under Python 3.4
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
9 with ini_path.open() as file:
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
10 library_standards.read_file(file)
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
11
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
12 def determinant_test(model):
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
13 '''
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
14 Checks all subfile references for matrices with rows or columns all
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
15 zero.
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
16 '''
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
17 yield from (
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
18 error(subfile_reference, 'zero-determinant')
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
19 for subfile_reference in model.subfile_references
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
20 if abs(subfile_reference.matrix.determinant() - 0) < 1e-15
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
21 )
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
22
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
23 def scaling_description(scaling, axes = 'xyz'):
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
24 '''
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
25 Returns a pretty description of a scaling vector. The axes parameter
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
26 controls what axes are printed and can be used to filter away
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
27 uninteresting values.
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
28 '''
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
29 return ', '.join(
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
30 str.format('{} = {}', letter, getattr(scaling, letter))
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
31 for letter in axes
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
32 )
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
33
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
34 def check_scaling(scaling, axes):
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
35 ''' Returns whether all given axes on the given scaling vector are 1. '''
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
36 return all(
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
37 abs(getattr(scaling, axis) - 1) < 1e-5
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
38 for axis in axes
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
39 )
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
40
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
41 # Restriction to checking function mapping.
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
42 restriction_tests = {
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
43 'no scaling': lambda scaling: check_scaling(scaling, 'xyz'),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
44 'y-scaling only': lambda scaling: check_scaling(scaling, 'xz'),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
45 'stud3-like scaling': lambda scaling: all([
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
46 check_scaling(scaling, 'xz'),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
47 # check if y-scaling is 1 or -1
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
48 abs(abs(scaling.y) - 1) < 1e-5,
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
49 ]),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
50 }
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
51
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
52 def scaling_legality_test(model):
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
53 '''
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
54 Checks the part against primitive references with bad scaling. Some
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
55 primitives (e.g. pegs) are not allowed to be scaled in the
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
56 X or Z directions. Some (e.g. most studs) are not allowed to be scaled
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
57 in the Y direction either.
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 24
diff changeset
58 '''
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
59 from fnmatch import fnmatch
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
60 scaling_restrictions = library_standards['scaling restrictions']
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
61 for subfile_reference in model.subfile_references:
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
62 primitive = subfile_reference.subfile_path.lower()
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
63 scaling = subfile_reference.matrix.scaling_vector()
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
64 # Find all restrictions that apply to this subfile reference.
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
65 restrictions = {
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
66 restriction
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
67 for pattern, restriction in scaling_restrictions.items()
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
68 if fnmatch(primitive, pattern)
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
69 }
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
70 # Check restrictions against the subfile. If any restrictions were
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
71 # found then the scaling vector must pass at least one of them.
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
72 if restrictions and not any(
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
73 restriction_tests[restriction](scaling)
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
74 for restriction in restrictions
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
75 ):
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
76 interesting_axes = ''.join(
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
77 axis
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
78 for axis in 'xyz'
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
79 if abs(getattr(scaling, axis) - 1) > 1e-5
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
80 )
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
81 yield warning(subfile_reference, 'illegal-scaling',
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
82 primitive = primitive,
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
83 axes = interesting_axes,
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
84 scaling = scaling)
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
85
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
86 manifest = {
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
87 'tests': {
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
88 'determinant': determinant_test,
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
89 'scaling-legality': scaling_legality_test,
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
90 },
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
91 'messages': {
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
92 'zero-determinant': 'matrix determinant is zero '
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
93 '(row or column all zero)',
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
94 'illegal-scaling': lambda primitive, scaling, axes:
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
95 str.format('scaling of unscalable primitive {} ({})',
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
96 primitive,
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
97 scaling_description(scaling, axes),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
98 ),
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
99 },
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
100 }

mercurial