View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.harmony.unpack200;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.util.stream.Stream;
22  
23  import org.junit.jupiter.api.Test;
24  import org.junit.jupiter.params.ParameterizedTest;
25  import org.junit.jupiter.params.provider.Arguments;
26  import org.junit.jupiter.params.provider.MethodSource;
27  
28  public class SegmentUtilsTest {
29  
30      private static final class MultipleMatches implements IMatcher {
31  
32          private final int divisor;
33  
34          MultipleMatches(final int divisor) {
35              this.divisor = divisor;
36          }
37  
38          @Override
39          public boolean matches(final long value) {
40              return value % divisor == 0;
41          }
42  
43      }
44  
45      public static final IMatcher even = new MultipleMatches(2);
46      public static final IMatcher five = new MultipleMatches(5);
47  
48      static Stream<Arguments> countArgs() {
49          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),
50                  Arguments.of("(DDD)V", 3), Arguments.of("(Lblah/blah;D)V", 2), Arguments.of("(Lblah/blah;DLbLah;)V", 3));
51      }
52  
53      static Stream<Arguments> countInvokeInterfaceArgs() {
54          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),
55                  Arguments.of("(DD)V", 4), Arguments.of("(Lblah/blah;D)V", 3), Arguments.of("(Lblah/blah;DLbLah;)V", 4),
56                  Arguments.of("([Lblah/blah;DLbLah;)V", 4));
57      }
58  
59      @ParameterizedTest
60      @MethodSource("countArgs")
61      public void testCountArgs(final String descriptor, final int expectedArgsCount) {
62          assertEquals(expectedArgsCount, SegmentUtils.countArgs(descriptor));
63      }
64  
65      @ParameterizedTest
66      @MethodSource("countInvokeInterfaceArgs")
67      public void testCountInvokeInterfaceArgs(final String descriptor, final int expectedCountInvokeInterfaceArgs) {
68          assertEquals(expectedCountInvokeInterfaceArgs, SegmentUtils.countInvokeInterfaceArgs(descriptor));
69      }
70  
71      @Test
72      public void testMatches() {
73          final long[] oneToTen = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
74          assertEquals(6, SegmentUtils.countMatches(new long[][] { oneToTen, new long[] { 5, 6, 7 } }, even));
75          assertEquals(5, SegmentUtils.countMatches(new long[][] { oneToTen }, even));
76          assertEquals(5, SegmentUtils.countMatches(oneToTen, even));
77          assertEquals(3, SegmentUtils.countMatches(new long[][] { oneToTen, new long[] { 5, 6, 7 } }, five));
78          assertEquals(2, SegmentUtils.countMatches(new long[][] { oneToTen }, five));
79          assertEquals(2, SegmentUtils.countMatches(oneToTen, five));
80      }
81  }