|
| 1 | +"""Executable that tests the clamps utilities.""" |
| 2 | + |
| 3 | +# Copyright 2022 Yucheng Liu. GNU GPL3 license. |
| 4 | +# GNU GPL3 license copy: https://www.gnu.org/licenses/gpl-3.0.txt |
| 5 | +# First added by username: liu-yucheng |
| 6 | +# Last updated by username: liu-yucheng |
| 7 | + |
| 8 | +import os |
| 9 | +import pathlib |
| 10 | +# import shutil |
| 11 | +import typing |
| 12 | +import unittest |
| 13 | +from os import path as ospath |
| 14 | + |
| 15 | +import lyc_pyutils |
| 16 | + |
| 17 | +# _copytree = shutil.copytree |
| 18 | +_IO = typing.IO |
| 19 | +_join = ospath.join |
| 20 | +_makedirs = os.makedirs |
| 21 | +_Path = pathlib.Path |
| 22 | +# _rmtree = shutil.rmtree |
| 23 | +_TestCase = unittest.TestCase |
| 24 | + |
| 25 | +_lyc_clamp_float = lyc_pyutils.clamp_float |
| 26 | +_lyc_clamp_int = lyc_pyutils.clamp_int |
| 27 | + |
| 28 | +_tests_path = _Path(__file__).parent |
| 29 | +_repo_path = _Path(_tests_path).parent.parent |
| 30 | + |
| 31 | +# _default_configs_path = _join(_repo_path, "lyc_pyutils_default_configs") |
| 32 | +# _default_test_data_path = _join(_default_configs_path, "test_data") |
| 33 | + |
| 34 | +_test_data_path = _join(_repo_path, ".lyc_pyutils_test_data") |
| 35 | +_log_loc = _join(_test_data_path, "log.txt") |
| 36 | + |
| 37 | + |
| 38 | +def _dquote_repr(instr): |
| 39 | + instr = str(instr) |
| 40 | + |
| 41 | + result = repr(instr)[1: -1] |
| 42 | + result = f"\"{result}\"" |
| 43 | + return result |
| 44 | + |
| 45 | + |
| 46 | +class _BaseCase(_TestCase): |
| 47 | + |
| 48 | + def __init__(self, methodName): |
| 49 | + """Initializes self with the given args.""" |
| 50 | + super().__init__(methodName) |
| 51 | + |
| 52 | + self._log: _IO = None |
| 53 | + |
| 54 | + def setUp(self): |
| 55 | + """Sets up before the tests.""" |
| 56 | + super().setUp() |
| 57 | + |
| 58 | + _makedirs(_test_data_path, exist_ok=True) |
| 59 | + self._log = open(_log_loc, "a+") |
| 60 | + |
| 61 | + case_name = type(self).__name__ |
| 62 | + info = f"- Test-case {case_name}" |
| 63 | + self._logln(info) |
| 64 | + |
| 65 | + def tearDown(self): |
| 66 | + """Tears down after the tests.""" |
| 67 | + super().tearDown() |
| 68 | + |
| 69 | + case_name = type(self).__name__ |
| 70 | + info = f"- End of test-case {case_name}" |
| 71 | + self._logln(info) |
| 72 | + |
| 73 | + self._log.flush() |
| 74 | + self._log.close() |
| 75 | + |
| 76 | + def _logstr(self, str_to_log): |
| 77 | + str_to_log = str(str_to_log) |
| 78 | + |
| 79 | + if self._log is not None: |
| 80 | + self._log.write(str_to_log) |
| 81 | + |
| 82 | + def _logln(self, line): |
| 83 | + line = str(line) |
| 84 | + |
| 85 | + line = line + "\n" |
| 86 | + self._logstr(line) |
| 87 | + |
| 88 | + def _log_method_start(self, method_name): |
| 89 | + method_name = str(method_name) |
| 90 | + |
| 91 | + info = f"-- Test-method {method_name}" |
| 92 | + self._logln(info) |
| 93 | + |
| 94 | + def _log_method_end(self, method_name): |
| 95 | + method_name = str(method_name) |
| 96 | + |
| 97 | + info = f"-- End of test-method {method_name}" |
| 98 | + self._logln(info) |
| 99 | + |
| 100 | + |
| 101 | +class TestClampFloat(_BaseCase): |
| 102 | + """Tests for the clamp_float function.""" |
| 103 | + |
| 104 | + def _match_values(self, actual, expect, not_match_info, match_info): |
| 105 | + not_match_info = str(not_match_info) |
| 106 | + match_info = str(match_info) |
| 107 | + |
| 108 | + fail_msg = not_match_info.format(actual, expect) |
| 109 | + success_msg = match_info.format(actual) |
| 110 | + self.assertTrue(actual == expect, fail_msg) |
| 111 | + self._logln(success_msg) |
| 112 | + |
| 113 | + def test_norm(self): |
| 114 | + """Tests the normal use case.""" |
| 115 | + method_name = self.test_norm.__name__ |
| 116 | + self._log_method_start(method_name) |
| 117 | + |
| 118 | + not_match_info = str( |
| 119 | + f"Clamp result does not match\n" |
| 120 | + f"Actual: {{}}\n" |
| 121 | + f"Expected: {{}}\n" |
| 122 | + ) |
| 123 | + |
| 124 | + match_info = str( |
| 125 | + f"Clamp result matched\n" |
| 126 | + f"Actual and expected: {{}}\n" |
| 127 | + ) |
| 128 | + |
| 129 | + bound1 = float(0) |
| 130 | + bound2 = float(1) |
| 131 | + |
| 132 | + inval = 0.5 |
| 133 | + outval = _lyc_clamp_float(inval, bound1, bound2) |
| 134 | + self._match_values(outval, inval, not_match_info, match_info) |
| 135 | + |
| 136 | + inval = 1.5 |
| 137 | + outval = _lyc_clamp_float(inval, bound1, bound2) |
| 138 | + self._match_values(outval, bound2, not_match_info, match_info) |
| 139 | + |
| 140 | + inval = -0.5 |
| 141 | + outval = _lyc_clamp_float(inval, bound1, bound2) |
| 142 | + self._match_values(outval, bound1, not_match_info, match_info) |
| 143 | + |
| 144 | + inval = 0.5 |
| 145 | + outval = _lyc_clamp_float(inval, bound2, bound1) |
| 146 | + self._match_values(outval, inval, not_match_info, match_info) |
| 147 | + |
| 148 | + inval = 1.5 |
| 149 | + outval = _lyc_clamp_float(inval, bound2, bound1) |
| 150 | + self._match_values(outval, bound2, not_match_info, match_info) |
| 151 | + |
| 152 | + inval = -0.5 |
| 153 | + outval = _lyc_clamp_float(inval, bound2, bound1) |
| 154 | + self._match_values(outval, bound1, not_match_info, match_info) |
| 155 | + |
| 156 | + self._log_method_end(method_name) |
| 157 | + |
| 158 | + |
| 159 | +class TestClampInt(_BaseCase): |
| 160 | + """Tests for the clamp_int function.""" |
| 161 | + |
| 162 | + def _match_values(self, actual, expect, not_match_info, match_info): |
| 163 | + not_match_info = str(not_match_info) |
| 164 | + match_info = str(match_info) |
| 165 | + |
| 166 | + fail_msg = not_match_info.format(actual, expect) |
| 167 | + success_msg = match_info.format(actual) |
| 168 | + self.assertTrue(actual == expect, fail_msg) |
| 169 | + self._logln(success_msg) |
| 170 | + |
| 171 | + def test_norm(self): |
| 172 | + """Tests the normal use case.""" |
| 173 | + method_name = self.test_norm.__name__ |
| 174 | + self._log_method_start(method_name) |
| 175 | + |
| 176 | + not_match_info = str( |
| 177 | + f"Clamp result does not match\n" |
| 178 | + f"Actual: {{}}\n" |
| 179 | + f"Expected: {{}}\n" |
| 180 | + ) |
| 181 | + |
| 182 | + match_info = str( |
| 183 | + f"Clamp result matched\n" |
| 184 | + f"Actual and expected: {{}}\n" |
| 185 | + ) |
| 186 | + |
| 187 | + bound1 = 0 |
| 188 | + bound2 = 2 |
| 189 | + |
| 190 | + inval = 1 |
| 191 | + outval = _lyc_clamp_int(inval, bound1, bound2) |
| 192 | + self._match_values(outval, inval, not_match_info, match_info) |
| 193 | + |
| 194 | + inval = 3 |
| 195 | + outval = _lyc_clamp_int(inval, bound1, bound2) |
| 196 | + self._match_values(outval, bound2, not_match_info, match_info) |
| 197 | + |
| 198 | + inval = -1 |
| 199 | + outval = _lyc_clamp_int(inval, bound1, bound2) |
| 200 | + self._match_values(outval, bound1, not_match_info, match_info) |
| 201 | + |
| 202 | + inval = 1 |
| 203 | + outval = _lyc_clamp_int(inval, bound2, bound1) |
| 204 | + self._match_values(outval, inval, not_match_info, match_info) |
| 205 | + |
| 206 | + inval = 3 |
| 207 | + outval = _lyc_clamp_int(inval, bound2, bound1) |
| 208 | + self._match_values(outval, bound2, not_match_info, match_info) |
| 209 | + |
| 210 | + inval = -1 |
| 211 | + outval = _lyc_clamp_int(inval, bound2, bound1) |
| 212 | + self._match_values(outval, bound1, not_match_info, match_info) |
| 213 | + |
| 214 | + self._log_method_end(method_name) |
| 215 | + |
| 216 | + |
| 217 | +def main(): |
| 218 | + """Runs this module as an executable.""" |
| 219 | + unittest.main(verbosity=1) |
| 220 | + |
| 221 | + |
| 222 | +if __name__ == "__main__": |
| 223 | + main() |
0 commit comments