Пример #1
0
 @Test
 public void testGetSum() {
   int n = 2000;
   long[] A = randomArray(n);
   FenwickTree fw = fenwickTree(A);
   for (int i = 0, cumul = 0; i < A.length; i++) {
     cumul += A[i];
     assertEquals(cumul, fw.getSum(i + 1));
   }
 }
Пример #2
0
 @Test
 public void testFindHigherValueIndex() {
   int n = 500;
   long[] A = randomArray(n);
   FenwickTree fw = fenwickTree(A);
   for (int i = 0; i < A.length; i++) {
     long sum = fw.getSum(i + 1);
     assertEquals(findHigherSumIndex(A, sum) + 1, fw.findHigherSumIndex(sum));
   }
   long sum = 10000000;
   assertEquals(findHigherSumIndex(A, sum) + 1, fw.findHigherSumIndex(sum));
   sum = -10000000;
   assertEquals(findHigherSumIndex(A, sum) + 1, fw.findHigherSumIndex(sum));
 }
  private void testRangeSum() {
    long actualSum = 0;
    long expSum = 0;
    for (int i = 0; i < nums.length; i++) {
      expSum = 0;
      for (int j = i; j < nums.length; j++) {
        expSum += nums[j];
        actualSum = testClass.getSum(i, j);

        if (expSum != actualSum) {
          fail("problem for sum from -->" + i + " to -->" + j);
        }
      }
    }
  }