From 52cc6f141224e21b8e682240dc8cadd9d6f191e6 Mon Sep 17 00:00:00 2001 From: weilycoder Date: Sat, 5 Oct 2024 10:31:53 +0800 Subject: [PATCH 1/2] Fixed errors in Vector.random --- cyaron/vector.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cyaron/vector.py b/cyaron/vector.py index c6b70e4..c5ba73e 100644 --- a/cyaron/vector.py +++ b/cyaron/vector.py @@ -54,9 +54,9 @@ def random(num: int = 5, position_range: list = None, mode: VectorRandomMode = 0 result = [] if mode == VectorRandomMode.repeatable: - result = [[random.randint(x, y) for x, y in zip(offset, length)] for _ in range(num)] + result = [[random.randint(x, x + y) for x, y in zip(offset, length)] for _ in range(num)] elif mode == VectorRandomMode.float: - result = [[random.uniform(x, y) for x, y in zip(offset, length)] for _ in range(num)] + result = [[random.uniform(x, x + y) for x, y in zip(offset, length)] for _ in range(num)] elif mode == VectorRandomMode.unique and vector_space > 5 * num: # O(NlogN) num_set = set() From ef322760a046cfb31ae327df80ba9f19c3bf7dc4 Mon Sep 17 00:00:00 2001 From: weilycoder Date: Sat, 5 Oct 2024 11:01:56 +0800 Subject: [PATCH 2/2] add vector_test --- cyaron/tests/__init__.py | 1 + cyaron/tests/vector_test.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 cyaron/tests/vector_test.py diff --git a/cyaron/tests/__init__.py b/cyaron/tests/__init__.py index dabf4f9..328a930 100644 --- a/cyaron/tests/__init__.py +++ b/cyaron/tests/__init__.py @@ -4,3 +4,4 @@ from .polygon_test import TestPolygon from .compare_test import TestCompare from .graph_test import TestGraph +from .vector_test import TestVector diff --git a/cyaron/tests/vector_test.py b/cyaron/tests/vector_test.py new file mode 100644 index 0000000..1380de4 --- /dev/null +++ b/cyaron/tests/vector_test.py @@ -0,0 +1,33 @@ +import unittest +from cyaron.vector import * + + +def has_duplicates(lst: list): + return len(lst) != len(set(lst)) + + +class TestVector(unittest.TestCase): + def test_unique_vector(self): + v = Vector.random(10**5, [10**6]) + self.assertFalse(has_duplicates(list(map(lambda tp: tuple(tp), v)))) + self.assertTrue(all(map(lambda v: 0 <= v[0] <= 10**6, v))) + v = Vector.random(1000, [(10**5, 10**6)]) + self.assertTrue(all(map(lambda v: 10**5 <= v[0] <= 10**6, v))) + with self.assertRaises( + Exception, + msg="1st param is so large that CYaRon can not generate unique vectors", + ): + v = Vector.random(10**5, [10**4]) + + def test_repeatable_vector(self): + v = Vector.random(10**5 + 1, [10**5], VectorRandomMode.repeatable) + self.assertTrue(all(map(lambda v: 0 <= v[0] <= 10**5, v))) + self.assertTrue(has_duplicates(list(map(lambda tp: tuple(tp), v)))) + v = Vector.random(1000, [(10**5, 10**6)], VectorRandomMode.repeatable) + self.assertTrue(all(map(lambda v: 10**5 <= v[0] <= 10**6, v))) + + def test_float_vector(self): + v = Vector.random(10**5, [10**5], VectorRandomMode.float) + self.assertTrue(all(map(lambda v: 0 <= v[0] <= 10**5, v))) + v = Vector.random(10**5, [(24, 25)], VectorRandomMode.float) + self.assertTrue(all(map(lambda v: 24 <= v[0] <= 25, v)))