Skip to content

Commit 5797f3c

Browse files
authored
Improving TagMap coverage (#9470)
* Cleaning up numeric entry tests * Cleaning up numeric entry tests * spotless * Improving coverage of fuzz tests - adding in more variations - frequency of sets, gets, & removes is the same, but the exact method is now varied between legacy and optimized versions - also added operations from ledger * Removing some test randomness for predictable coverage - replaced some random size selection with TagMapScenario-s - TagMapScenario is mix of TagMapType and varying number of prepopulated elements - the prepopulating helps cover the 3 different branch cases inside OptimizedTagMap: null, TagMap.Entry, BucketGroup - also added calls to checkIntegry and toInternalString, since those methods were largely uncovered - still need to work on better coverage of putAllMerge, probably need a pair of TagMapScenario-s * Adding final
1 parent c7436ae commit 5797f3c

File tree

6 files changed

+871
-219
lines changed

6 files changed

+871
-219
lines changed

internal-api/src/main/java/datadog/trace/api/TagMap.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ public boolean isNumber() {
466466
return _isNumericPrimitive(curType) || (this.rawObj instanceof Number);
467467
}
468468

469-
private static boolean _isNumericPrimitive(byte type) {
469+
static boolean _isNumericPrimitive(byte type) {
470470
return (type >= BYTE);
471471
}
472472

@@ -1080,7 +1080,7 @@ abstract class TagMapFactory<MapT extends TagMap> {
10801080
createFactory(Config.get().isOptimizedMapEnabled());
10811081

10821082
static final TagMapFactory<?> createFactory(boolean useOptimized) {
1083-
return useOptimized ? new OptimizedTagMapFactory() : new LegacyTagMapFactory();
1083+
return useOptimized ? OptimizedTagMapFactory.INSTANCE : LegacyTagMapFactory.INSTANCE;
10841084
}
10851085

10861086
public abstract MapT create();
@@ -1091,6 +1091,10 @@ static final TagMapFactory<?> createFactory(boolean useOptimized) {
10911091
}
10921092

10931093
final class OptimizedTagMapFactory extends TagMapFactory<OptimizedTagMap> {
1094+
static final OptimizedTagMapFactory INSTANCE = new OptimizedTagMapFactory();
1095+
1096+
private OptimizedTagMapFactory() {}
1097+
10941098
@Override
10951099
public OptimizedTagMap create() {
10961100
return new OptimizedTagMap();
@@ -1108,6 +1112,10 @@ public OptimizedTagMap empty() {
11081112
}
11091113

11101114
final class LegacyTagMapFactory extends TagMapFactory<LegacyTagMap> {
1115+
static final LegacyTagMapFactory INSTANCE = new LegacyTagMapFactory();
1116+
1117+
private LegacyTagMapFactory() {}
1118+
11111119
@Override
11121120
public LegacyTagMap create() {
11131121
return new LegacyTagMap();

internal-api/src/test/java/datadog/trace/api/TagMapEntryTest.java

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@
3232
* @author dougqh
3333
*/
3434
public class TagMapEntryTest {
35+
@Test
36+
public void isNumericPrimitive() {
37+
assertFalse(TagMap.Entry._isNumericPrimitive(TagMap.Entry.ANY));
38+
assertFalse(TagMap.Entry._isNumericPrimitive(TagMap.Entry.BOOLEAN));
39+
assertFalse(TagMap.Entry._isNumericPrimitive(TagMap.Entry.CHAR));
40+
assertFalse(TagMap.Entry._isNumericPrimitive(TagMap.Entry.OBJECT));
41+
42+
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.BYTE));
43+
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.SHORT));
44+
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.INT));
45+
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.LONG));
46+
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.FLOAT));
47+
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.DOUBLE));
48+
}
49+
3550
@Test
3651
public void objectEntry() {
3752
test(
@@ -42,7 +57,8 @@ public void objectEntry() {
4257
checkKey("foo", entry),
4358
checkValue("bar", entry),
4459
checkEquals("bar", entry::stringValue),
45-
checkTrue(entry::isObject)));
60+
checkTrue(entry::isObject),
61+
checkFalse(entry::isNumber)));
4662
}
4763

4864
@Test
@@ -55,6 +71,7 @@ public void anyEntry_object() {
5571
checkKey("foo", entry),
5672
checkValue("bar", entry),
5773
checkTrue(entry::isObject),
74+
checkFalse(entry::isNumber),
5875
checkKey("foo", entry),
5976
checkValue("bar", entry)));
6077
}
@@ -70,6 +87,7 @@ public void booleanEntry(boolean value) {
7087
checkKey("foo", entry),
7188
checkValue(value, entry),
7289
checkFalse(entry::isNumericPrimitive),
90+
checkFalse(entry::isNumber),
7391
checkType(TagMap.Entry.BOOLEAN, entry)));
7492
}
7593

@@ -84,6 +102,7 @@ public void booleanEntry_boxed(boolean value) {
84102
checkKey("foo", entry),
85103
checkValue(value, entry),
86104
checkFalse(entry::isNumericPrimitive),
105+
checkFalse(entry::isNumber),
87106
checkType(TagMap.Entry.BOOLEAN, entry)));
88107
}
89108

@@ -98,6 +117,7 @@ public void anyEntry_boolean(boolean value) {
98117
checkKey("foo", entry),
99118
checkValue(value, entry),
100119
checkFalse(entry::isNumericPrimitive),
120+
checkFalse(entry::isNumber),
101121
checkType(TagMap.Entry.BOOLEAN, entry),
102122
checkValue(value, entry)));
103123
}
@@ -112,7 +132,8 @@ public void intEntry(int value) {
112132
multiCheck(
113133
checkKey("foo", entry),
114134
checkValue(value, entry),
115-
checkTrue(entry::isNumericPrimitive),
135+
checkIsNumericPrimitive(entry),
136+
checkInstanceOf(Number.class, entry),
116137
checkType(TagMap.Entry.INT, entry)));
117138
}
118139

@@ -126,7 +147,8 @@ public void intEntry_boxed(int value) {
126147
multiCheck(
127148
checkKey("foo", entry),
128149
checkValue(value, entry),
129-
checkTrue(entry::isNumericPrimitive),
150+
checkIsNumericPrimitive(entry),
151+
checkInstanceOf(Number.class, entry),
130152
checkType(TagMap.Entry.INT, entry)));
131153
}
132154

@@ -140,7 +162,8 @@ public void anyEntry_int(int value) {
140162
multiCheck(
141163
checkKey("foo", entry),
142164
checkValue(value, entry),
143-
checkTrue(entry::isNumericPrimitive),
165+
checkIsNumericPrimitive(entry),
166+
checkInstanceOf(Number.class, entry),
144167
checkType(TagMap.Entry.INT, entry),
145168
checkValue(value, entry)));
146169
}
@@ -199,7 +222,7 @@ public void longEntry_boxed(long value) {
199222
multiCheck(
200223
checkKey("foo", entry),
201224
checkValue(value, entry),
202-
checkTrue(entry::isNumericPrimitive),
225+
checkIsNumericPrimitive(entry),
203226
checkType(TagMap.Entry.LONG, entry)));
204227
}
205228

@@ -228,7 +251,7 @@ public void anyEntry_long(long value) {
228251
multiCheck(
229252
checkKey("foo", entry),
230253
checkValue(value, entry),
231-
checkTrue(entry::isNumericPrimitive),
254+
checkIsNumericPrimitive(entry),
232255
checkTrue(() -> entry.is(TagMap.Entry.LONG)),
233256
checkValue(value, entry)));
234257
}
@@ -257,7 +280,7 @@ public void floatEntry_boxed(float value) {
257280
multiCheck(
258281
checkKey("foo", entry),
259282
checkValue(value, entry),
260-
checkTrue(entry::isNumericPrimitive),
283+
checkIsNumericPrimitive(entry),
261284
checkType(TagMap.Entry.FLOAT, entry)));
262285
}
263286

@@ -286,7 +309,7 @@ public void doubleEntry(double value) {
286309
multiCheck(
287310
checkKey("foo", entry),
288311
checkValue(value, entry),
289-
checkTrue(entry::isNumericPrimitive),
312+
checkIsNumericPrimitive(entry),
290313
checkType(TagMap.Entry.DOUBLE, entry)));
291314
}
292315

@@ -417,6 +440,20 @@ static final Check checkKey(String expected, TagMap.Entry entry) {
417440
return multiCheck(checkEquals(expected, entry::tag), checkEquals(expected, entry::getKey));
418441
}
419442

443+
static final Check checkIsNumericPrimitive(TagMap.Entry entry) {
444+
return multiCheck(
445+
checkTrue(entry::isNumericPrimitive),
446+
checkTrue(entry::isNumber),
447+
checkInstanceOf(Number.class, entry));
448+
}
449+
450+
static final Check checkIsBigNumber(TagMap.Entry entry) {
451+
return multiCheck(
452+
checkFalse(entry::isNumericPrimitive),
453+
checkTrue(entry::isNumber),
454+
checkInstanceOf(Number.class, entry));
455+
}
456+
420457
static final Check checkValue(Object expected, TagMap.Entry entry) {
421458
return multiCheck(
422459
checkEquals(expected, entry::objectValue),
@@ -468,6 +505,16 @@ static final Check checkValue(double expected, TagMap.Entry entry) {
468505
checkEquals(Double.toString(expected), entry::stringValue));
469506
}
470507

508+
public static Check checkNumber(Number number, TagMap.Entry entry) {
509+
return multiCheck(
510+
checkEquals(number, entry::objectValue),
511+
checkEquals(number.intValue(), entry::intValue),
512+
checkEquals(number.longValue(), entry::longValue),
513+
checkEquals(number.floatValue(), entry::floatValue),
514+
checkEquals(number.doubleValue(), entry::doubleValue),
515+
checkEquals(number.toString(), entry::stringValue));
516+
}
517+
471518
static final Check checkValue(float expected, TagMap.Entry entry) {
472519
return multiCheck(
473520
checkEquals(expected, entry::floatValue),
@@ -479,6 +526,13 @@ static final Check checkValue(float expected, TagMap.Entry entry) {
479526
checkEquals(Float.toString(expected), entry::stringValue));
480527
}
481528

529+
static final Check checkInstanceOf(Class<?> klass, TagMap.Entry entry) {
530+
return () ->
531+
assertTrue(
532+
klass.isAssignableFrom(entry.objectValue().getClass()),
533+
"instanceof " + klass.getSimpleName());
534+
}
535+
482536
static final Check checkType(byte entryType, TagMap.Entry entry) {
483537
return () -> assertTrue(entry.is(entryType), "type is " + entryType);
484538
}
@@ -496,23 +550,23 @@ static final Check checkTrue(Supplier<Boolean> actual) {
496550
}
497551

498552
static final Check checkEquals(float expected, Supplier<Float> actual) {
499-
return () -> assertEquals(expected, actual.get(), actual.toString());
553+
return () -> assertEquals(expected, actual.get().floatValue(), actual.toString());
500554
}
501555

502556
static final Check checkEquals(int expected, Supplier<Integer> actual) {
503-
return () -> assertEquals(expected, actual.get(), actual.toString());
557+
return () -> assertEquals(expected, actual.get().intValue(), actual.toString());
504558
}
505559

506560
static final Check checkEquals(double expected, Supplier<Double> actual) {
507-
return () -> assertEquals(expected, actual.get(), actual.toString());
561+
return () -> assertEquals(expected, actual.get().doubleValue(), actual.toString());
508562
}
509563

510564
static final Check checkEquals(long expected, Supplier<Long> actual) {
511-
return () -> assertEquals(expected, actual.get(), actual.toString());
565+
return () -> assertEquals(expected, actual.get().longValue(), actual.toString());
512566
}
513567

514568
static final Check checkEquals(boolean expected, Supplier<Boolean> actual) {
515-
return () -> assertEquals(expected, actual.get(), actual.toString());
569+
return () -> assertEquals(expected, actual.get().booleanValue(), actual.toString());
516570
}
517571

518572
static final Check checkEquals(Object expected, Supplier<Object> actual) {

0 commit comments

Comments
 (0)