1#
2# __init__.py
3#
4# This source file is part of the FoundationDB open source project
5#
6# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12#     http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20
21import math
22import sys
23import os
24
25sys.path[:0] = [os.path.join(os.path.dirname(__file__), '..', '..', 'bindings', 'python')]
26
27import util
28
29FDB_API_VERSION = 610
30
31LOGGING = {
32    'version': 1,
33    'disable_existing_loggers': False,
34    'formatters': {
35        'simple': {
36            'format': '%(message)s'
37        }
38    },
39    'handlers': {
40        'console': {
41            'level': 'NOTSET',
42            'class': 'logging.StreamHandler',
43            'stream': sys.stdout,
44            'formatter': 'simple'
45        }
46    },
47    'loggers': {
48        'foundationdb.bindingtester': {
49            'level': 'INFO',
50            'handlers': ['console']
51        }
52    }
53}
54
55
56class Result:
57    def __init__(self, subspace, key, values):
58        self.subspace_tuple = util.subspace_to_tuple(subspace)
59        self.key_tuple = subspace.unpack(key)
60        self.values = values
61
62    def key(self, specification):
63        return self.key_tuple[specification.key_start_index:]
64
65    @staticmethod
66    def elements_equal(el1, el2):
67        if type(el1) != type(el2):
68            return False
69
70        if isinstance(el1, tuple):
71            return Result.tuples_match(el1, el2)
72
73        if isinstance(el1, float) and math.isnan(el1):
74            return math.isnan(el2)
75
76        return el1 == el2
77
78    @staticmethod
79    def tuples_match(t1, t2):
80        if len(t1) != len(t2):
81            return False
82
83        return all([Result.elements_equal(x,y) for x,y in zip(t1, t2)])
84
85    def matches_key(self, rhs, specification):
86        if not isinstance(rhs, Result):
87            return False
88
89        return Result.tuples_match(self.key(specification), rhs.key(specification))
90
91    def matches(self, rhs, specification):
92        if not self.matches_key(rhs, specification):
93            return False
94
95        for value in self.values:
96            for rValue in rhs.values:
97                if value == rValue:
98                    return True
99
100        return False
101
102    def matches_global_error_filter(self, specification):
103        return any([specification.matches_global_error_filter(v) for v in self.values])
104
105    # A non-unique sequence of numbers used to align results from different testers
106    def sequence_num(self, specification):
107        if specification.ordering_index is not None:
108            return self.key_tuple[specification.ordering_index]
109
110        return None
111
112    def __str__(self):
113        if len(self.values) == 1:
114            value_str = repr(self.values[0])
115        else:
116            value_str = repr(self.values)
117
118        return '%s = %s' % (repr(self.subspace_tuple + self.key_tuple), value_str)
119