View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }