test_vector.py
Go to the documentation of this file.
1 #!/usr/env python
2 
3 import math
4 import os
5 import sys
6 import unittest
7 
8 sys.path.insert(0, os.path.abspath(".."))
9 from gnsstk.test_utils import args, assertSequenceAlmostEqual, run_unit_tests
10 
11 import gnsstk
12 
13 
14 class TestVector(unittest.TestCase):
15 
16  def test_constructor(self):
17  # Construct vector with initial size
18  a = gnsstk.Vector_int(5) # Initial size
19  self.assertEqual(5, len(a), "Vector must initialize with provided size")
20 
21  # Construct vector with initial size and default value
22  a = gnsstk.Vector_int(6, 89)
23  self.assertSequenceEqual([89, 89, 89, 89, 89, 89], list(a), "Vector must initialize with provided size and default value")
24 
25  # Use copy constructor
26  a = gnsstk.Vector_int(6, 42)
27  b = gnsstk.Vector_int(a)
28  self.assertSequenceEqual([42, 42, 42, 42, 42, 42], list(b))
29  self.assertIsNot(a, b, "Copy constructor must make a new instance")
30 
31  def test_resize(self):
32  a = gnsstk.Vector_int(6, 23)
33  self.assertSequenceEqual([23, 23, 23, 23, 23, 23], list(a))
34 
35  a.resize(3)
36  self.assertSequenceEqual([23, 23, 23], list(a), "Vector resize must reduce the list")
37 
38  # Resize with a default value for the entire vector
39  # NOTE: this is different, but seems to be expected, from the C++ standard library vector
40  a.resize(5, 42)
41  self.assertSequenceEqual([42, 42, 42, 42, 42], list(a), "Vector resize must increase size with provided default value")
42 
43  # Really? The vector destroys itself if the resize is larger than the current size and no default value.
44  # a.resize(6)
45  # self.assertSequenceEqual([42, 42, 42, 42, 42, 0], list(a), "Vector resize must use value initialization if no default given")
46 
47  def test_append(self):
48  a = gnsstk.Vector_int()
49  self.assertEqual(0, len(a))
50 
51  for i in range(0, 10):
52  a = a.append(i)
53 
54  self.assertSequenceEqual(list(range(10)), list(a))
55 
56  def test_extend(self):
57  a = gnsstk.Vector_int(2, 1)
58  b = gnsstk.Vector_int(2, 2)
59  c = gnsstk.Vector_int(2, 3)
60 
61  # NOTE: this is different behavior from the standard Python list `extend()`
62  # The python list `extend` modifies the list and returns None, whereas,
63  # this method for Vector<int> returns a *new* instance with the modifications.
64  d = a.extend(b).extend(c)
65  self.assertSequenceEqual([1, 1, 2, 2, 3, 3], list(d))
66 
67  def test_iterator(self):
68  a = gnsstk.Vector_int(10, 0)
69  for i in range(len(a)):
70  a[i] = 2*i
71 
72  # Get iterator without failure and return the first few elements
73  a_iter = iter(a)
74  self.assertEqual(0, next(a_iter))
75  self.assertEqual(2, next(a_iter))
76  self.assertEqual(4, next(a_iter))
77 
78  # Exercise the iterator via list comprehension
79  a_dump = [v for v in a]
80  self.assertSequenceEqual([0, 2, 4, 6, 8, 10, 12, 14, 16, 18], a_dump)
81 
82  def test_to_std_vector(self):
83  a = gnsstk.Vector_int(10, 3)
84  self.assertIsInstance(a, gnsstk.Vector_int)
85 
86  b = a.toStdVector()
87  self.assertIsInstance(b, gnsstk.std_vector_int)
88 
89  self.assertSequenceEqual([3, 3, 3, 3, 3, 3, 3, 3, 3, 3], list(a))
90  self.assertSequenceEqual([3, 3, 3, 3, 3, 3, 3, 3, 3, 3], list(b))
91 
92  def test_vector_sum(self):
93  # Rather clunky syntax for creating a vector of [2, 2, 2, 2, 2, 4.5, 4.5, 4.5]
94  a = gnsstk.Vector_double(5, 2.0).extend(gnsstk.Vector_double(3, 4.5))
95  self.assertAlmostEqual(23.5, gnsstk.Vector_sum_double(a))
96 
97 
98 class TestVectorOperators(unittest.TestCase):
99 
101  a = gnsstk.Vector_int()
102  for i in [-1, -2, 56, -6, -89, -45]:
103  a = a.append(i)
104 
105  self.assertEqual(-89, gnsstk.Vector_min_int(a))
106  self.assertEqual(-1, gnsstk.Vector_minabs_int(a))
107  self.assertEqual(56, gnsstk.Vector_max_int(a))
108  # TODO: maxabs causes build to fail, see TemplateMacros.i
109  # self.assertEqual(-89, gnsstk.Vector_maxabs_int(a))
110 
111  def test_vector_norm(self):
112  a = gnsstk.Vector_double(3, 4)
113  a = a.extend(gnsstk.Vector_double(3, 2))
114  a = a.extend(gnsstk.Vector_double(3, -2.5))
115 
116  self.assertAlmostEqual(8.87411967, gnsstk.Vector_norm_double(a))
117 
118  def test_vector_dot(self):
119  a = gnsstk.Vector_int(3, 2)
120  a = a.extend(gnsstk.Vector_int(3, 23))
121  a = a.extend(gnsstk.Vector_int(3, -5))
122 
123  # There are two "dotscalar" functions that work the same but can take the arguments
124  # in either order
125  self.assertEqual(240, gnsstk.Vector_dotscalar_int(a, 4))
126  self.assertEqual(240, gnsstk.Vector_dotscalar_int(4, a))
127 
129  a = gnsstk.Vector_double(9, -34)
130  a = a.extend(gnsstk.Vector_double(9, -78))
131  a = a.extend(gnsstk.Vector_double(9, -74))
132 
133  b = gnsstk.Vector_normalize_double(a)
134  self.assertIsNot(a, b)
136  self,
137  [
138  -0.10050378, -0.10050378, -0.10050378, -0.10050378, -0.10050378, -0.10050378, -0.10050378, -0.10050378, -0.10050378,
139  -0.23056749, -0.23056749, -0.23056749, -0.23056749, -0.23056749, -0.23056749, -0.23056749, -0.23056749, -0.23056749,
140  -0.21874352, -0.21874352, -0.21874352, -0.21874352, -0.21874352, -0.21874352, -0.21874352, -0.21874352, -0.21874352,
141  ],
142  list(b)
143  )
144 
146  a = gnsstk.Vector_double(3, -7)
147  a = a.extend(gnsstk.Vector_double(3, 4.5))
148  a = a.extend(gnsstk.Vector_double(3, 20))
149 
150  self.assertAlmostEqual(37.51999466, gnsstk.Vector_RSS_double(a))
151  self.assertAlmostEqual(12.50666488, gnsstk.Vector_RMS_double(a))
152 
154 
155  # STD Unary Vector Operations
156  a = gnsstk.Vector_double(3, 0.45)
157  a = a.extend(gnsstk.Vector_double(3, 0.23))
158  a = a.extend(gnsstk.Vector_double(3, 0.1))
159 
160  gnsstk_vector_ops = [
161  gnsstk.Vector_abs_double,
162  gnsstk.Vector_acos_double,
163  gnsstk.Vector_asin_double,
164  gnsstk.Vector_atan_double,
165  gnsstk.Vector_cos_double,
166  gnsstk.Vector_cosh_double,
167  gnsstk.Vector_exp_double,
168  gnsstk.Vector_log_double,
169  gnsstk.Vector_log10_double,
170  gnsstk.Vector_sinh_double,
171  gnsstk.Vector_sin_double,
172  gnsstk.Vector_sqrt_double,
173  gnsstk.Vector_tan_double,
174  gnsstk.Vector_tanh_double,
175  ]
176 
177  python_math_ops = [
178  abs,
179  math.acos,
180  math.asin,
181  math.atan,
182  math.cos,
183  math.cosh,
184  math.exp,
185  math.log,
186  lambda v: math.log(v, 10), # Can't use partial() since there are no keywords
187  math.sinh,
188  math.sin,
189  math.sqrt,
190  math.tan,
191  math.tanh,
192  ]
193 
194  for gnsstk_op, python_op in zip(gnsstk_vector_ops, python_math_ops):
195  # Using `subTest` here will be useful when moving to python 3.4
196  # with self.subTest(gnsstk_op.__name__):
197 
198  # The gnsstk operators are "vectorized" while the python functions are not
199  assertSequenceAlmostEqual(self, list(gnsstk_op(a)), list(map(python_op, list(a))))
200 
201 
202 if __name__ == "__main__":
tests.test_vector.TestVector.test_append
def test_append(self)
Definition: test_vector.py:47
tests.test_vector.TestVector.test_resize
def test_resize(self)
Definition: test_vector.py:31
tests.test_vector.TestVector.test_vector_sum
def test_vector_sum(self)
Definition: test_vector.py:92
tests.test_vector.TestVectorOperators.test_vector_norm
def test_vector_norm(self)
Definition: test_vector.py:111
tests.test_vector.TestVector
Definition: test_vector.py:14
test_utils.assertSequenceAlmostEqual
def assertSequenceAlmostEqual(test_case, l1, l2, **kwargs)
Definition: test_utils.py:76
tests.test_vector.TestVectorOperators.test_vector_min_max_abs
def test_vector_min_max_abs(self)
Definition: test_vector.py:100
tests.test_vector.TestVectorOperators.test_vector_unary_operators
def test_vector_unary_operators(self)
Definition: test_vector.py:153
tests.test_vector.TestVectorOperators.test_vector_normalize
def test_vector_normalize(self)
Definition: test_vector.py:128
tests.test_vector.TestVectorOperators.test_vector_RSS_RMS
def test_vector_RSS_RMS(self)
Definition: test_vector.py:145
tests.test_vector.TestVector.test_to_std_vector
def test_to_std_vector(self)
Definition: test_vector.py:82
tests.test_vector.TestVectorOperators
Definition: test_vector.py:98
tests.test_vector.TestVector.test_extend
def test_extend(self)
Definition: test_vector.py:56
gnsstk::range
double range(const Position &A, const Position &B)
Definition: Position.cpp:1273
tests.test_vector.TestVector.test_iterator
def test_iterator(self)
Definition: test_vector.py:67
tests.test_vector.TestVectorOperators.test_vector_dot
def test_vector_dot(self)
Definition: test_vector.py:118
test_utils.run_unit_tests
def run_unit_tests()
Definition: test_utils.py:51
tests.test_vector.TestVector.test_constructor
def test_constructor(self)
Definition: test_vector.py:16


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:41