1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.harmony.unpack200;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import java.util.stream.Stream;
24
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.params.ParameterizedTest;
27 import org.junit.jupiter.params.provider.Arguments;
28 import org.junit.jupiter.params.provider.MethodSource;
29
30 class SegmentUtilsTest {
31
32 private static final class MultipleMatches implements IMatcher {
33
34 private final int divisor;
35
36 MultipleMatches(final int divisor) {
37 this.divisor = divisor;
38 }
39
40 @Override
41 public boolean matches(final long value) {
42 return value % divisor == 0;
43 }
44
45 }
46
47 public static final IMatcher even = new MultipleMatches(2);
48 public static final IMatcher five = new MultipleMatches(5);
49
50 static Stream<Arguments> countArgs() {
51 return Stream.of(Arguments.of("()V", 0), Arguments.of("(D)V", 1), Arguments.of("([D)V", 1), Arguments.of("([[D)V", 1), Arguments.of("(DD)V", 2),
52 Arguments.of("(DDD)V", 3), Arguments.of("(Lblah/blah;D)V", 2), Arguments.of("(Lblah/blah;DLbLah;)V", 3));
53 }
54
55 static Stream<Arguments> countInvokeInterfaceArgs() {
56 return Stream.of(Arguments.of("(Z)V", 1), Arguments.of("(D)V", 2), Arguments.of("(J)V", 2), Arguments.of("([D)V", 1), Arguments.of("([[D)V", 1),
57 Arguments.of("(DD)V", 4), Arguments.of("(Lblah/blah;D)V", 3), Arguments.of("(Lblah/blah;DLbLah;)V", 4),
58 Arguments.of("([Lblah/blah;DLbLah;)V", 4));
59 }
60
61 @ParameterizedTest
62 @MethodSource("countArgs")
63 void testCountArgs(final String descriptor, final int expectedArgsCount) {
64 assertEquals(expectedArgsCount, SegmentUtils.countArgs(descriptor));
65 }
66
67 @ParameterizedTest
68 @MethodSource("countInvokeInterfaceArgs")
69 void testCountInvokeInterfaceArgs(final String descriptor, final int expectedCountInvokeInterfaceArgs) {
70 assertEquals(expectedCountInvokeInterfaceArgs, SegmentUtils.countInvokeInterfaceArgs(descriptor));
71 }
72
73 @Test
74 void testMatches() {
75 final long[] oneToTen = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
76 assertEquals(6, SegmentUtils.countMatches(new long[][] { oneToTen, new long[] { 5, 6, 7 } }, even));
77 assertEquals(5, SegmentUtils.countMatches(new long[][] { oneToTen }, even));
78 assertEquals(5, SegmentUtils.countMatches(oneToTen, even));
79 assertEquals(3, SegmentUtils.countMatches(new long[][] { oneToTen, new long[] { 5, 6, 7 } }, five));
80 assertEquals(2, SegmentUtils.countMatches(new long[][] { oneToTen }, five));
81 assertEquals(2, SegmentUtils.countMatches(oneToTen, five));
82 }
83 }