OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.distiller; |
| 6 |
| 7 import java.util.ArrayList; |
| 8 import java.util.HashMap; |
| 9 import java.util.List; |
| 10 import java.util.Map; |
| 11 |
| 12 public class PageParamInfoTest extends DomDistillerJsTestCase { |
| 13 |
| 14 public void testArePageNumsAdjacentAndConsecutive() { |
| 15 { |
| 16 final int[] allNums = { 1, 2 }; |
| 17 final int[] selectedNums = { 1, 2 }; |
| 18 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 19 assertTrue(isAdjacent(result)); |
| 20 assertTrue(isConsecutive(result)); |
| 21 } |
| 22 { |
| 23 final int[] allNums = { 1, 2, 3 }; |
| 24 final int[] selectedNums = { 2, 3 }; |
| 25 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 26 assertTrue(isAdjacent(result)); |
| 27 assertTrue(isConsecutive(result)); |
| 28 } |
| 29 { |
| 30 final int[] allNums = { 1, 5, 6, 7, 10 }; |
| 31 final int[] selectedNums = { 1, 5, 7, 10 }; |
| 32 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 33 assertTrue(isAdjacent(result)); |
| 34 assertTrue(isConsecutive(result)); |
| 35 } |
| 36 { |
| 37 final int[] allNums = { 10, 25, 50 }; |
| 38 final int[] selectedNums = { 10, 25, 50 }; // No consecutive pairs. |
| 39 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 40 assertTrue(isAdjacent(result)); |
| 41 assertFalse(isConsecutive(result)); |
| 42 } |
| 43 { |
| 44 final int[] allNums = { 23, 24, 30 }; |
| 45 // This list doesn't satisfy consecutive rule. There should be "22"
on the left of "23", |
| 46 // or "25" on the right of "24", or "29" on the left of "30". |
| 47 final int[] selectedNums = { 23, 24, 30 }; |
| 48 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 49 assertTrue(isAdjacent(result)); |
| 50 assertFalse(isConsecutive(result)); |
| 51 } |
| 52 { |
| 53 final int[] allNums = { 1, 2, 3, 4, 5 }; |
| 54 final int[] selectedNums = { 1, 3, 5 }; // Two gaps. |
| 55 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 56 assertFalse(isAdjacent(result)); |
| 57 assertFalse(isConsecutive(result)); |
| 58 } |
| 59 { |
| 60 final int[] allNums = { 2, 3, 4, 5 }; |
| 61 final int[] selectedNums = { 2, 5 }; // A gap of 2 numbers. |
| 62 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 63 assertFalse(isAdjacent(result)); |
| 64 assertFalse(isConsecutive(result)); |
| 65 } |
| 66 } |
| 67 |
| 68 private static int arePageNumsAdjacentAndConsecutive(int[] selectedNums, int
[] allNums) { |
| 69 List<PageParamInfo.PageInfo> ascendingNumbers = new ArrayList<PageParamI
nfo.PageInfo>(); |
| 70 Map<Integer, Integer> numberToPos = new HashMap<Integer, Integer>(); |
| 71 |
| 72 for (int i = 0; i < allNums.length; i++) { |
| 73 final int number = allNums[i]; |
| 74 numberToPos.put(number, i); |
| 75 ascendingNumbers.add(new PageParamInfo.PageInfo(number, "")); |
| 76 } |
| 77 |
| 78 List<PageLinkInfo> allLinkInfo = new ArrayList<PageLinkInfo>(); |
| 79 for (int i = 0; i < selectedNums.length; i++) { |
| 80 final int number = selectedNums[i]; |
| 81 allLinkInfo.add(new PageLinkInfo(number, number, numberToPos.get(num
ber))); |
| 82 } |
| 83 |
| 84 return PageParamInfo.arePageNumsAdjacentAndConsecutive(allLinkInfo, asce
ndingNumbers); |
| 85 } |
| 86 |
| 87 private static boolean isAdjacent(int result) { |
| 88 return (result & PageParamInfo.PAGE_NUM_ADJACENT_MASK) == |
| 89 PageParamInfo.PAGE_NUM_ADJACENT_MASK; |
| 90 } |
| 91 |
| 92 private static boolean isConsecutive(int result) { |
| 93 return (result & PageParamInfo.PAGE_NUM_CONSECUTIVE_MASK) == |
| 94 PageParamInfo.PAGE_NUM_CONSECUTIVE_MASK; |
| 95 } |
| 96 |
| 97 } |
OLD | NEW |