01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.harmony.pack200.tests;
18:
19: import junit.framework.TestCase;
20:
21: import org.apache.harmony.pack200.IMatcher;
22: import org.apache.harmony.pack200.SegmentUtils;
23:
24: public class SegmentUtilsTest extends TestCase {
25: private static class MultipleMatches implements IMatcher {
26: private final int divisor;
27:
28: public MultipleMatches(int divisor) {
29: this .divisor = divisor;
30: }
31:
32: public boolean matches(long value) {
33: return value % divisor == 0;
34: }
35:
36: }
37:
38: public static final IMatcher even = new MultipleMatches(2);
39: public static final IMatcher five = new MultipleMatches(5);
40:
41: public void testCountArgs() {
42: assertEquals(0, SegmentUtils.countArgs("()V"));
43: assertEquals(1, SegmentUtils.countArgs("(D)V"));
44: assertEquals(1, SegmentUtils.countArgs("([D)V"));
45: assertEquals(1, SegmentUtils.countArgs("([[D)V"));
46: assertEquals(2, SegmentUtils.countArgs("(DD)V"));
47: assertEquals(3, SegmentUtils.countArgs("(DDD)V"));
48: assertEquals(2, SegmentUtils.countArgs("(Lblah/blah;D)V"));
49: assertEquals(3, SegmentUtils.countArgs("(Lblah/blah;DLbLah;)V"));
50: }
51:
52: public void testCountInvokeInterfaceArgs() {
53: assertEquals(1, SegmentUtils.countInvokeInterfaceArgs("(Z)V"));
54: assertEquals(2, SegmentUtils.countInvokeInterfaceArgs("(D)V"));
55: assertEquals(2, SegmentUtils.countInvokeInterfaceArgs("(J)V"));
56: assertEquals(1, SegmentUtils.countInvokeInterfaceArgs("([D)V"));
57: assertEquals(1, SegmentUtils.countInvokeInterfaceArgs("([[D)V"));
58: assertEquals(4, SegmentUtils.countInvokeInterfaceArgs("(DD)V"));
59: assertEquals(3, SegmentUtils
60: .countInvokeInterfaceArgs("(Lblah/blah;D)V"));
61: assertEquals(4, SegmentUtils
62: .countInvokeInterfaceArgs("(Lblah/blah;DLbLah;)V"));
63: assertEquals(4, SegmentUtils
64: .countInvokeInterfaceArgs("([Lblah/blah;DLbLah;)V"));
65: }
66:
67: public void testMatches() {
68: long[] oneToTen = new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
69: assertEquals(6, SegmentUtils.countMatches(new long[][] {
70: oneToTen, new long[] { 5, 6, 7 } }, even));
71: assertEquals(5, SegmentUtils.countMatches(
72: new long[][] { oneToTen }, even));
73: assertEquals(5, SegmentUtils.countMatches(oneToTen, even));
74: assertEquals(3, SegmentUtils.countMatches(new long[][] {
75: oneToTen, new long[] { 5, 6, 7 } }, five));
76: assertEquals(2, SegmentUtils.countMatches(
77: new long[][] { oneToTen }, five));
78: assertEquals(2, SegmentUtils.countMatches(oneToTen, five));
79: }
80: }
|