Skip to content

Commit f3b7118

Browse files
onionpsysimonresch
authored andcommitted
feat: add Finite annotation
1 parent d7a5229 commit f3b7118

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

docs/mutation-framework.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ package.
9393
| `@InRange` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long` | Specifies `min` and `max` values of generated integrals |
9494
| `@FloatInRange` | `float`, `Float` | Specifies `min` and `max` values of generated floats |
9595
| `@DoubleInRange` | `double`, `Double` | Specifies `min` and `max` values of generated doubles |
96-
| `Positive` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long`, `float`, `Float`, `double`, `Double` | Specifies that only positive values are generated |
97-
| `Negative` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long`, `float`, `Float`, `double`, `Double` | Specifies that only negative values are generated |
98-
| `NonPositive` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long`, `float`, `Float`, `double`, `Double` | Specifies that only non-positive values are generated |
99-
| `NonNegative` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long`, `float`, `Float`, `double`, `Double` | Specifies that only non-negative values are generated |
96+
| `@Positive` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long`, `float`, `Float`, `double`, `Double` | Specifies that only positive values are generated |
97+
| `@Negative` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long`, `float`, `Float`, `double`, `Double` | Specifies that only negative values are generated |
98+
| `@NonPositive` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long`, `float`, `Float`, `double`, `Double` | Specifies that only non-positive values are generated |
99+
| `@NonNegative` | `byte`, `Byte`, `short`, `Short`, `int`, `Integer`, `long`, `Long`, `float`, `Float`, `double`, `Double` | Specifies that only non-negative values are generated |
100+
| `@Finite` | `float`, `Float`, `double`, `Double` | Specifies that only finite values are generated |
100101
| `@NotNull` | | Specifies that a reference type should not be `null` |
101102
| `@WithLength` | `byte[]` | Specifies the length of the generated byte array |
102103
| `@WithUtf8Length` | `java.lang.String` | Specifies the length of the generated string in UTF-8 bytes, see annotation Javadoc for further information |
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.code_intelligence.jazzer.mutation.annotation;
18+
19+
import static java.lang.annotation.ElementType.TYPE_USE;
20+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
21+
22+
import com.code_intelligence.jazzer.mutation.utils.AppliesTo;
23+
import com.code_intelligence.jazzer.mutation.utils.PropertyConstraint;
24+
import java.lang.annotation.Retention;
25+
import java.lang.annotation.Target;
26+
27+
@Target(TYPE_USE)
28+
@Retention(RUNTIME)
29+
@AppliesTo({float.class, Float.class, double.class, Double.class})
30+
@PropertyConstraint
31+
public @interface Finite {
32+
/**
33+
* Defines the scope of the annotation. Possible values are defined in {@link
34+
* com.code_intelligence.jazzer.mutation.utils.PropertyConstraint}.
35+
*/
36+
String constraint() default PropertyConstraint.DECLARATION;
37+
}

src/main/java/com/code_intelligence/jazzer/mutation/support/RangeSupport.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ public static FloatRange resolveFloatRange(
120120
minValue = defaultMin;
121121
maxValue = 0.0f;
122122
allowNaN = false;
123+
} else if (annotation instanceof Finite) {
124+
minValue = -Float.MAX_VALUE;
125+
maxValue = Float.MAX_VALUE;
126+
allowNaN = false;
123127
}
124128
}
125129
return new FloatRange(minValue, maxValue, allowNaN);
@@ -152,6 +156,10 @@ public static DoubleRange resolveDoubleRange(
152156
minValue = defaultMin;
153157
maxValue = 0.0;
154158
allowNaN = false;
159+
} else if (annotation instanceof Finite) {
160+
minValue = -Double.MAX_VALUE;
161+
maxValue = Double.MAX_VALUE;
162+
allowNaN = false;
155163
}
156164
}
157165
return new DoubleRange(minValue, maxValue, allowNaN);

src/test/java/com/code_intelligence/jazzer/mutation/support/AnnotationSupportTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.code_intelligence.jazzer.mutation.annotation.Ascii;
2424
import com.code_intelligence.jazzer.mutation.annotation.DoubleInRange;
25+
import com.code_intelligence.jazzer.mutation.annotation.Finite;
2526
import com.code_intelligence.jazzer.mutation.annotation.FloatInRange;
2627
import com.code_intelligence.jazzer.mutation.annotation.InRange;
2728
import com.code_intelligence.jazzer.mutation.annotation.Negative;
@@ -93,6 +94,7 @@ static Stream<Arguments> validateAnnotationUsageCases_throw() {
9394
arguments(new TypeHolder<@Negative String>() {}.annotatedType()),
9495
arguments(new TypeHolder<@NonNegative String>() {}.annotatedType()),
9596
arguments(new TypeHolder<@NonPositive String>() {}.annotatedType()),
97+
arguments(new TypeHolder<@Finite String>() {}.annotatedType()),
9698
// deep
9799
arguments(
98100
new TypeHolder<

src/test/java/com/code_intelligence/jazzer/mutation/support/TypeSupportTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.junit.jupiter.params.provider.Arguments.arguments;
3030

3131
import com.code_intelligence.jazzer.mutation.annotation.DoubleInRange;
32+
import com.code_intelligence.jazzer.mutation.annotation.Finite;
3233
import com.code_intelligence.jazzer.mutation.annotation.FloatInRange;
3334
import com.code_intelligence.jazzer.mutation.annotation.InRange;
3435
import com.code_intelligence.jazzer.mutation.annotation.Negative;
@@ -465,6 +466,10 @@ static Stream<Arguments> forwardAnnotationCases() {
465466
new TypeHolder<@NonNegative Float>() {}.annotatedType(),
466467
new TypeHolder<@NotNull Float>() {}.annotatedType(),
467468
new TypeHolder<@NotNull @NonNegative Float>() {}.annotatedType()),
469+
arguments(
470+
new TypeHolder<@Finite Double>() {}.annotatedType(),
471+
new TypeHolder<@NotNull Double>() {}.annotatedType(),
472+
new TypeHolder<@NotNull @Finite Double>() {}.annotatedType()),
468473
arguments(
469474
new TypeHolder<@Negative Double>() {}.annotatedType(),
470475
new TypeHolder<@NotNull Double>() {}.annotatedType(),
@@ -476,7 +481,11 @@ static Stream<Arguments> forwardAnnotationCases() {
476481
arguments(
477482
new TypeHolder<@NonNegative Double>() {}.annotatedType(),
478483
new TypeHolder<@NotNull Double>() {}.annotatedType(),
479-
new TypeHolder<@NotNull @NonNegative Double>() {}.annotatedType()));
484+
new TypeHolder<@NotNull @NonNegative Double>() {}.annotatedType()),
485+
arguments(
486+
new TypeHolder<@Finite Double>() {}.annotatedType(),
487+
new TypeHolder<@NotNull Double>() {}.annotatedType(),
488+
new TypeHolder<@NotNull @Finite Double>() {}.annotatedType()));
480489
}
481490

482491
@ParameterizedTest

0 commit comments

Comments
 (0)