tests/subfiles.py

Mon, 22 Jan 2018 21:04:53 +0200

author
Santeri Piippo
date
Mon, 22 Jan 2018 21:04:53 +0200
changeset 25
8990ac138cc2
parent 24
f8080ffceaa9
child 26
7c263b864371
permissions
-rw-r--r--

added check for invalid colours

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
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
9 with open(ini_path) as file:
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):
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
13 yield from (
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
14 error(subfile_reference, 'zero-determinant')
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
15 for subfile_reference in model.subfile_references
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
16 if abs(subfile_reference.matrix.determinant() - 0) < 1e-15
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
17 )
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
18
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
19 def scaling_description(scaling, axes = 'xyz'):
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
20 '''
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
21 Returns a pretty description of a scaling vector. The axes parameter
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
22 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
23 uninteresting values.
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 return ', '.join(
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
26 str.format('{} = {}', letter, getattr(scaling, letter))
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
27 for letter in axes
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
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
30 def check_scaling(scaling, axes):
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
31 ''' 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
32 return all(
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
33 abs(getattr(scaling, axis) - 1) < 1e-5
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
34 for axis in axes
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
35 )
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
36
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
37 # Restriction to checking function mapping.
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
38 restriction_tests = {
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
39 'no scaling': lambda scaling: check_scaling(scaling, 'xyz'),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
40 'y-scaling only': lambda scaling: check_scaling(scaling, 'xz'),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
41 'stud3-like scaling': lambda scaling: all([
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
42 check_scaling(scaling, 'xz'),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
43 # check if y-scaling is 1 or -1
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
44 abs(abs(scaling.y) - 1) < 1e-5,
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
45 ]),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
46 }
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
47
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
48 def scaling_legality_test(model):
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
49 from fnmatch import fnmatch
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
50 scaling_restrictions = library_standards['scaling restrictions']
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
51 for subfile_reference in model.subfile_references:
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
52 primitive = subfile_reference.subfile_path.lower()
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
53 scaling = subfile_reference.matrix.scaling_vector()
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
54 # Find all restrictions that apply to this subfile reference.
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
55 restrictions = {
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
56 restriction
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
57 for pattern, restriction in scaling_restrictions.items()
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
58 if fnmatch(primitive, pattern)
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
59 }
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
60 # Check restrictions against the subfile. If any restrictions were
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
61 # 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
62 if restrictions and not any(
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
63 restriction_tests[restriction](scaling)
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
64 for restriction in restrictions
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
65 ):
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
66 interesting_axes = ''.join(
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
67 axis
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
68 for axis in 'xyz'
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
69 if abs(getattr(scaling, axis) - 1) > 1e-5
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
70 )
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
71 yield warning(subfile_reference, 'illegal-scaling',
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
72 primitive = primitive,
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
73 axes = interesting_axes,
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
74 scaling = scaling)
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
75
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
76 manifest = {
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
77 'tests': {
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
78 'determinant': determinant_test,
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
79 'scaling-legality': scaling_legality_test,
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
80 },
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
81 'messages': {
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
82 'zero-determinant': 'matrix determinant is zero '
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
83 '(row or column all zero)',
24
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
84 'illegal-scaling': lambda primitive, scaling, axes:
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
85 str.format('scaling of unscalable primitive {} ({})',
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
86 primitive,
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
87 scaling_description(scaling, axes),
f8080ffceaa9 added test for forbidden primitive scaling
Santeri Piippo
parents: 23
diff changeset
88 ),
23
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
89 },
1b9645b7ddb0 added matrix all zero test
Santeri Piippo
parents:
diff changeset
90 }

mercurial