|
| 1 | +# ------------------------------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) Leo Hanisch. All rights reserved. |
| 3 | +# Licensed under the BSD 3-Clause License. See LICENSE.txt in the project root for license information. |
| 4 | +# ------------------------------------------------------------------------------------------------------ |
| 5 | + |
| 6 | +import numpy as np |
| 7 | + |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from swarmlib.util.coordinate import Coordinate |
| 12 | + |
| 13 | +# pylint: disable=unused-variable |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def test_func(): |
| 18 | + return lambda x: np.sum(x) # pylint: disable=unnecessary-lambda |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def test_object(test_func): |
| 23 | + return Coordinate( |
| 24 | + function=test_func, |
| 25 | + bit_generator=np.random.default_rng(3), |
| 26 | + lower_boundary=0.1, |
| 27 | + upper_boundary=3.9) |
| 28 | + |
| 29 | + |
| 30 | +def describe_coordinate(): |
| 31 | + def describe_constructor(): |
| 32 | + def describe_raise_error(): |
| 33 | + def if_bit_generator_missing(): |
| 34 | + with pytest.raises(KeyError): |
| 35 | + Coordinate(function='foo') |
| 36 | + |
| 37 | + def if_function_is_missing(): |
| 38 | + with pytest.raises(KeyError): |
| 39 | + Coordinate(bit_generator='foo') |
| 40 | + |
| 41 | + def initializes_correctly(test_object): |
| 42 | + np.testing.assert_array_equal(test_object.position, [0.42546683514577255, 0.9998799250651788]) |
| 43 | + np.testing.assert_equal(test_object.value, 1.4253467602109513) |
| 44 | + np.testing.assert_array_less(test_object.position, 3.9) |
| 45 | + np.testing.assert_array_less(0.1, test_object.position) |
| 46 | + |
| 47 | + def describe_comparison(): |
| 48 | + @pytest.fixture |
| 49 | + def other(test_func): |
| 50 | + return Coordinate( |
| 51 | + function=test_func, |
| 52 | + bit_generator=np.random.default_rng(4), |
| 53 | + lower_boundary=0.1, |
| 54 | + upper_boundary=3.9) |
| 55 | + |
| 56 | + def equal(test_object): |
| 57 | + assert test_object == test_object # pylint: disable=comparison-with-itself |
| 58 | + |
| 59 | + def not_equal(test_object, other): |
| 60 | + assert test_object != other |
| 61 | + |
| 62 | + def less(test_object, other): |
| 63 | + assert test_object < other |
| 64 | + |
| 65 | + def less_equal(test_object, other): |
| 66 | + assert test_object <= other |
| 67 | + assert test_object <= test_object # pylint: disable=comparison-with-itself |
| 68 | + |
| 69 | + def greater(test_object, other): |
| 70 | + assert other > test_object |
| 71 | + |
| 72 | + def greater_equal(test_object, other): |
| 73 | + assert other >= test_object |
| 74 | + assert test_object >= test_object # pylint: disable=comparison-with-itself |
| 75 | + |
| 76 | + def describe_position(): |
| 77 | + def setter_clips_position_and_updates_value(test_object): |
| 78 | + test_object._position = [-5, 7] # pylint: disable=protected-access |
| 79 | + |
| 80 | + np.testing.assert_array_equal(test_object.position, [0.1, 3.9]) |
| 81 | + np.testing.assert_equal(test_object.value, 4) |
0 commit comments