|
| 1 | +#!/usr/bin/env -S python3 -B |
| 2 | +# |
| 3 | +# Copyright (c) 2024 Project CHIP Authors |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +import sys |
| 20 | +import typing |
| 21 | +from dataclasses import dataclass |
| 22 | + |
| 23 | +import chip.clusters as Clusters |
| 24 | +from chip.clusters import Attribute |
| 25 | +from chip.clusters.Types import NullValue |
| 26 | +from MockTestRunner import MockTestRunner |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class TestSpec(): |
| 31 | + min: typing.Optional[int] |
| 32 | + max: typing.Optional[int] |
| 33 | + measured: int |
| 34 | + tolerance: int |
| 35 | + expect_pass: bool |
| 36 | + |
| 37 | + |
| 38 | +TEST_CASES = [ |
| 39 | + # ============================== |
| 40 | + # Measured Test cases |
| 41 | + # ============================== |
| 42 | + # -------- |
| 43 | + # Neither min nor max specified |
| 44 | + # -------- |
| 45 | + # Measured lowest |
| 46 | + TestSpec(NullValue, NullValue, -27315, 10, True), |
| 47 | + # Measured highest |
| 48 | + TestSpec(NullValue, NullValue, 32767, 10, True), |
| 49 | + # Measured below |
| 50 | + TestSpec(NullValue, NullValue, -27316, 10, False), |
| 51 | + # Measured above |
| 52 | + TestSpec(NullValue, NullValue, 32768, 10, False), |
| 53 | + # Measured null |
| 54 | + TestSpec(NullValue, NullValue, NullValue, 10, True), |
| 55 | + |
| 56 | + # -------- |
| 57 | + # min only |
| 58 | + # -------- |
| 59 | + # Measured lowest |
| 60 | + TestSpec(5, NullValue, 5, 10, True), |
| 61 | + # Measured highest |
| 62 | + TestSpec(5, NullValue, 32767, 10, True), |
| 63 | + # Measured below |
| 64 | + TestSpec(5, NullValue, 4, 10, False), |
| 65 | + # Measured above |
| 66 | + TestSpec(5, NullValue, 32768, 10, False), |
| 67 | + # Measured null |
| 68 | + TestSpec(5, NullValue, NullValue, 10, True), |
| 69 | + |
| 70 | + # -------- |
| 71 | + # max only |
| 72 | + # -------- |
| 73 | + # Measured lowest |
| 74 | + TestSpec(NullValue, 5, -27315, 10, True), |
| 75 | + # Measured highest |
| 76 | + TestSpec(NullValue, 5, 5, 10, True), |
| 77 | + # Measured below |
| 78 | + TestSpec(NullValue, 5, -27316, 10, False), |
| 79 | + # Measured above |
| 80 | + TestSpec(NullValue, 5, 6, 10, False), |
| 81 | + # Measured null |
| 82 | + TestSpec(NullValue, 5, NullValue, 10, True), |
| 83 | + |
| 84 | + # -------- |
| 85 | + # both |
| 86 | + # -------- |
| 87 | + # Measured lowest |
| 88 | + TestSpec(-5, 5, -5, 10, True), |
| 89 | + # Measured highest |
| 90 | + TestSpec(-5, 5, 5, 10, True), |
| 91 | + # Measured below |
| 92 | + TestSpec(-5, 5, -6, 10, False), |
| 93 | + # Measured above |
| 94 | + TestSpec(-5, 5, 6, 10, False), |
| 95 | + # Measured null |
| 96 | + TestSpec(-5, 5, NullValue, 10, True), |
| 97 | + |
| 98 | + # ============================== |
| 99 | + # Min Test cases |
| 100 | + # ============================== |
| 101 | + # Max not specified, min OK bottom |
| 102 | + TestSpec(-27315, NullValue, 0, 10, True), |
| 103 | + # Max not specified, min OK top |
| 104 | + TestSpec(32766, NullValue, 32767, 10, True), |
| 105 | + # Max not specified, min out of range below |
| 106 | + TestSpec(-27316, NullValue, 0, 10, False), |
| 107 | + # Max not specified, min out of range above |
| 108 | + TestSpec(32767, NullValue, 32767, 10, False), |
| 109 | + # Max specified, min OK bottom |
| 110 | + TestSpec(-27315, 5, 0, 10, True), |
| 111 | + # Max specified, min OK top |
| 112 | + TestSpec(4, 5, 4, 10, True), |
| 113 | + # Max specified, min out of range below |
| 114 | + TestSpec(-27316, 5, 0, 10, False), |
| 115 | + # Max specified, min out of range above |
| 116 | + TestSpec(5, 5, 5, 10, False), |
| 117 | + |
| 118 | + # ============================== |
| 119 | + # Min Test cases |
| 120 | + # ============================== |
| 121 | + # min not specified, max OK bottom |
| 122 | + TestSpec(NullValue, -27314, -27315, 10, True), |
| 123 | + # min not specified, max OK top |
| 124 | + TestSpec(NullValue, 32767, 0, 10, True), |
| 125 | + # min not specified, max out of range bottom |
| 126 | + TestSpec(NullValue, -27315, -27315, 10, False), |
| 127 | + # min not specified, max out of range top |
| 128 | + TestSpec(NullValue, 32768, 0, 10, False), |
| 129 | + # min specified, max OK bottom |
| 130 | + TestSpec(0, 1, 0, 10, True), |
| 131 | + # min specified, max OK top |
| 132 | + TestSpec(0, 32767, 0, 10, True), |
| 133 | + # min specified, max out of range bottom |
| 134 | + TestSpec(0, 0, 0, 10, False), |
| 135 | + # min specified, max out of range top |
| 136 | + TestSpec(0, 32768, 0, 10, False), |
| 137 | + |
| 138 | + # ============================== |
| 139 | + # Tolerance test cases |
| 140 | + # ============================== |
| 141 | + # Tolerance OK bottom |
| 142 | + TestSpec(NullValue, NullValue, 0, 0, True), |
| 143 | + # Tolerance OK top |
| 144 | + TestSpec(NullValue, NullValue, 0, 2048, True), |
| 145 | + # Tolerance out of range bottom |
| 146 | + TestSpec(NullValue, NullValue, 0, -1, False), |
| 147 | + # Tolerance out of range top |
| 148 | + TestSpec(NullValue, NullValue, 0, 2049, False), |
| 149 | + |
| 150 | +] |
| 151 | + |
| 152 | + |
| 153 | +def test_spec_to_attribute_cache(test_spec: TestSpec) -> Attribute.AsyncReadTransaction.ReadResponse: |
| 154 | + c = Clusters.TemperatureMeasurement |
| 155 | + attr = Clusters.TemperatureMeasurement.Attributes |
| 156 | + resp = Attribute.AsyncReadTransaction.ReadResponse({}, [], {}) |
| 157 | + resp.attributes = {1: {c: {attr.MaxMeasuredValue: test_spec.max, |
| 158 | + attr.MinMeasuredValue: test_spec.min, attr.MeasuredValue: test_spec.measured, attr.Tolerance: test_spec.tolerance}}} |
| 159 | + return resp |
| 160 | + |
| 161 | + |
| 162 | +def main(): |
| 163 | + test_runner = MockTestRunner('TC_TMP_2_1', 'TC_TMP_2_1', 'test_TC_TMP_2_1') |
| 164 | + failures = [] |
| 165 | + for idx, t in enumerate(TEST_CASES): |
| 166 | + ok = test_runner.run_test_with_mock_read(test_spec_to_attribute_cache(t)) == t.expect_pass |
| 167 | + if not ok: |
| 168 | + failures.append(f"Measured test case failure: {idx} {t}") |
| 169 | + |
| 170 | + test_runner.Shutdown() |
| 171 | + print(f"Test of tests: run {len(TEST_CASES)}, test response correct: {len(TEST_CASES) - len(failures)} test response incorrect: {len(failures)}") |
| 172 | + for f in failures: |
| 173 | + print(f) |
| 174 | + |
| 175 | + return 1 if failures else 0 |
| 176 | + |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + sys.exit(main()) |
0 commit comments