Skip to content

Commit 54a35b8

Browse files
authored
Merge pull request #339 from alphaville/hotfix/increase-test-coverage
Increase of testing coverage
2 parents 8e19414 + eafbf29 commit 54a35b8

5 files changed

Lines changed: 63 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Note: This is the main Changelog file for the Rust solver. The Changelog file fo
1111
<!-- ---------------------
1212
Unreleased
1313
--------------------- -->
14+
## [v0.9.2] - Unreleased
1415

16+
### Changed
17+
18+
- Update version of `rand`, `ndarray`, and `modcholesky` in `Cargo.toml`
1519

1620
<!-- ---------------------
1721
v0.9.0
@@ -298,6 +302,7 @@ This is a breaking API change.
298302
--------------------- -->
299303

300304
<!-- Releases -->
305+
[v0.9.2]: https://github.com/alphaville/optimization-engine/compare/v0.9.1...v0.9.2
301306
[v0.9.1]: https://github.com/alphaville/optimization-engine/compare/v0.9.0...v0.9.1
302307
[v0.9.0]: https://github.com/alphaville/optimization-engine/compare/v0.8.1...v0.9.0
303308
[v0.8.1]: https://github.com/alphaville/optimization-engine/compare/v0.8.0...v0.8.1

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ unit_test_utils = "0.1.3"
135135
# instead, use:
136136
icasadi_test = "0.0.2"
137137
# Random number generators for unit tests:
138-
rand = "0.8"
138+
rand = "0.9"
139139

140140

141141
# --------------------------------------------------------------------------

open-codegen/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
Note: This is the Changelog file of `opengen` - the Python interface of OpEn
99

10+
## [0.9.5] - Unreleased
11+
12+
### Changed
13+
14+
- Additional unit tests: increased coverage to 92%
15+
1016
## [0.9.4] - 2025-05-08
1117

1218

open-codegen/test/test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import opengen as og
55
import subprocess
66
import logging
7+
import numpy as np
78

89

910
class RustBuildTestCase(unittest.TestCase):
@@ -527,6 +528,26 @@ def test_tcp_manager_remote_port_no_ip(self):
527528
with self.assertRaises(Exception) as __context:
528529
_remote_tcp_manager = og.tcp.OptimizerTcpManager(port=8888)
529530

531+
def test_set_y(self):
532+
c = og.constraints.Ball2(radius=1)
533+
y_calc = og.builder.SetYCalculator(c)
534+
y = y_calc.obtain()
535+
536+
def test_squared_norm(self):
537+
u = np.array([3, 4])
538+
y = og.functions.norm2_squared(u)
539+
self.assertAlmostEqual(25., y, places=12)
540+
541+
u = [3, 4]
542+
y = og.functions.norm2_squared(u)
543+
self.assertAlmostEqual(25., y, places=12)
544+
545+
u = cs.SX.sym("u", 2)
546+
f = og.functions.norm2_squared(u)
547+
fun = cs.Function('fun', [u], [f])
548+
y = fun([3, 4])
549+
self.assertAlmostEqual(25., y, places=12)
550+
530551

531552
if __name__ == '__main__':
532553
logging.getLogger('retry').setLevel(logging.ERROR)

open-codegen/test/test_constraints.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ def test_cartesian_sx(self):
315315
_sqd_sx = cartesian.distance_squared(u_sx)
316316
u_mx = cs.SX.sym("u", 9, 1)
317317
_sqd_mx = cartesian.distance_squared(u_mx)
318+
self.assertEqual(2, cartesian.segment_dimension(0))
319+
self.assertEqual(3, cartesian.segment_dimension(1))
318320

319321
def test_cartesian_segments_not_increasing(self):
320322
no_constraints = og.constraints.NoConstraints()
@@ -343,6 +345,24 @@ def test_cartesian_segments_empty_args(self):
343345
with self.assertRaises(ValueError) as __context:
344346
og.constraints.CartesianProduct([], sets)
345347

348+
def test_cartesian_convex(self):
349+
ball_inf = og.constraints.BallInf(None, 1)
350+
ball_eucl = og.constraints.Ball2(None, 1)
351+
cartesian = og.constraints.CartesianProduct(
352+
[5, 10], [ball_inf, ball_eucl])
353+
self.assertTrue(cartesian.is_convex())
354+
self.assertTrue(cartesian.is_compact())
355+
356+
finite_set = og.constraints.FiniteSet([[1, 2, 3], [4, 5, 6]])
357+
cartesian = og.constraints.CartesianProduct(
358+
[5, 10, 13], [ball_inf, ball_eucl, finite_set])
359+
self.assertFalse(cartesian.is_convex())
360+
361+
free = og.constraints.NoConstraints()
362+
cartesian = og.constraints.CartesianProduct(
363+
[5, 10, 11], [ball_inf, ball_eucl, free])
364+
self.assertFalse(cartesian.is_compact())
365+
346366
# -----------------------------------------------------------------------
347367
# Finite Set
348368
# -----------------------------------------------------------------------
@@ -375,6 +395,10 @@ def test_finite_set_compact(self):
375395
c = og.constraints.FiniteSet([[1, 2, 3], [4, 5, 6]])
376396
self.assertTrue(c.is_compact())
377397

398+
def test_finite_set_dimension(self):
399+
c = og.constraints.FiniteSet([[1, 2, 3], [4, 5, 6]])
400+
self.assertEqual(3, c.dimension())
401+
378402
# -----------------------------------------------------------------------
379403
# Halfspaces
380404
# -----------------------------------------------------------------------
@@ -513,6 +537,12 @@ def test_sphere2_sq_distance_symbolic(self):
513537
z = fun([1, 1, 0, 0])
514538
self.assertAlmostEqual(0.835786437626905, z, places=12)
515539

540+
def test_sphere2_no_center(self):
541+
sphere = og.constraints.Sphere2(radius=0.5)
542+
u = [0, 0, 0, 0]
543+
dist = sphere.distance_squared(u)
544+
self.assertAlmostEqual(0.25, dist, places=12)
545+
516546

517547
if __name__ == '__main__':
518548
unittest.main()

0 commit comments

Comments
 (0)