Skip to content

Commit 93a9f5b

Browse files
authored
fix(incarnation-sapere): validate chemotaxis argument explicitly (#5326)
1 parent 0b71e90 commit 93a9f5b

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

alchemist-incarnation-sapere/src/main/java/it/unibo/alchemist/model/sapere/actions/SAPEREChemotaxis.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ public SAPEREChemotaxis(
5454
final int idPosition
5555
) {
5656
super(environment, node, response);
57-
/*
58-
* Thanks Java, for not having unsigned primitives! -.-
59-
*/
60-
assert idPosition >= 0 : "Argument number must be positive";
57+
if (idPosition < 0) {
58+
throw new IllegalArgumentException("idPosition must be non-negative, but was: " + idPosition);
59+
}
6160
this.response = response;
6261
this.gradient = gradient;
6362
this.idPosition = idPosition;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2010-2025, Danilo Pianini and contributors
3+
* listed, for each module, in the respective subproject's build.gradle.kts file.
4+
*
5+
* This file is part of Alchemist, and is distributed under the terms of the
6+
* GNU General Public License, with a linking exception,
7+
* as described in the file LICENSE in the Alchemist distribution's top directory.
8+
*/
9+
package it.unibo.alchemist.model.sapere
10+
11+
import io.kotest.assertions.throwables.shouldThrow
12+
import io.kotest.core.spec.style.StringSpec
13+
import io.kotest.matchers.string.shouldContain
14+
import it.unibo.alchemist.model.environments.Continuous2DEnvironment
15+
import it.unibo.alchemist.model.incarnations.SAPEREIncarnation
16+
import it.unibo.alchemist.model.positions.Euclidean2DPosition
17+
import it.unibo.alchemist.model.sapere.actions.SAPEREChemotaxis
18+
import it.unibo.alchemist.model.sapere.nodes.LsaNode
19+
20+
class TestSAPEREChemotaxis :
21+
StringSpec(
22+
{
23+
"SAPEREChemotaxis constructor should throw IllegalArgumentException for negative idPosition" {
24+
val incarnation = SAPEREIncarnation<Euclidean2DPosition>()
25+
val environment = Continuous2DEnvironment<List<ILsaMolecule>>(incarnation)
26+
val node = LsaNode(environment)
27+
val response = incarnation.createMolecule("response")
28+
val gradient = incarnation.createMolecule("gradient, Dest")
29+
val invalidPosition = -1
30+
val exception = shouldThrow<IllegalArgumentException> {
31+
SAPEREChemotaxis(environment, node, response, gradient, invalidPosition)
32+
}
33+
exception.message shouldContain "idPosition"
34+
exception.message shouldContain invalidPosition.toString()
35+
}
36+
},
37+
)

0 commit comments

Comments
 (0)