@Theory
  public void testCopyAttributes(UnaryArithmeticFactory factory, RAbstractVector originalOperand) {
    RAbstractVector operand = originalOperand.copy();
    // we have to e careful not to change mutable vectors
    RAbstractVector a = operand.copy();
    if (a instanceof RShareable) {
      ((RShareable) a).incRefCount();
    }

    RVector<?> aMaterialized = a.copy().materialize();
    aMaterialized.setAttr("a", "a");
    assertAttributes(executeArithmetic(factory, aMaterialized.copy()), "a");
  }
 @Theory
 public void testGeneric(UnaryArithmeticFactory factory) {
   // this should trigger the generic case
   for (RAbstractVector vector : ALL_VECTORS) {
     executeArithmetic(factory, vector.copy());
   }
 }
Exemple #3
0
 /** Extend or truncate the vector to a specified length. */
 private static RVector<?> handleLengthOut(
     RAbstractVector x, int lengthOut, boolean copyIfSameSize) {
   if (x.getLength() == lengthOut) {
     return (RVector<?>) (copyIfSameSize ? x.copy() : x);
   }
   return x.copyResized(lengthOut, false);
 }
 @Theory
 public void testPlusFolding(RAbstractVector originalOperand) {
   RAbstractVector operand = originalOperand.copy();
   assumeThat(operand, is(not(instanceOf(RScalarVector.class))));
   if (operand.getRType() == getArgumentType(operand)) {
     assertFold(true, operand, PLUS);
   } else {
     assertFold(false, operand, PLUS);
   }
 }
  @Theory
  public void testVectorResult(UnaryArithmeticFactory factory, RAbstractVector originalOperand) {
    RAbstractVector operand = originalOperand.copy();
    assumeThat(operand, is(not(instanceOf(RScalarVector.class))));

    Object result = executeArithmetic(factory, operand);
    Assert.assertFalse(isPrimitive(result));
    assumeThat(result, is(instanceOf(RAbstractVector.class)));
    RAbstractVector resultCast = (RAbstractVector) result;

    assertThat(resultCast.getLength(), is(equalTo(operand.getLength())));
  }
  @Theory
  public void testCompleteness(UnaryArithmeticFactory factory, RAbstractVector originalOperand) {
    RAbstractVector operand = originalOperand.copy();
    Object result = executeArithmetic(factory, operand);

    boolean resultComplete = isPrimitive(result) ? true : ((RAbstractVector) result).isComplete();

    if (operand.getLength() == 0) {
      Assert.assertTrue(resultComplete);
    } else {
      boolean expectedComplete = operand.isComplete();
      Assert.assertEquals(expectedComplete, resultComplete);
    }
  }
  @Theory
  public void testSharing(UnaryArithmeticFactory factory, RAbstractVector originalOperand) {
    RAbstractVector operand = originalOperand.copy();
    // sharing does not work if a is a scalar vector
    assumeThat(true, is(isShareable(operand, operand.getRType())));

    RType resultType = getArgumentType(operand);
    Object sharedResult = null;
    if (isShareable(operand, resultType)) {
      sharedResult = operand;
    }

    Object result = executeArithmetic(factory, operand);
    if (sharedResult == null) {
      Assert.assertNotSame(operand, result);
    } else {
      Assert.assertSame(sharedResult, result);
    }
  }
Exemple #8
0
 /** Replicate the vector a given number of times. */
 private RVector<?> handleTimes(
     RAbstractVector x, RAbstractIntVector times, boolean copyIfSameSize) {
   if (oneTimeGiven.profile(times.getLength() == 1)) {
     // only one times value is given
     final int howManyTimes = times.getDataAt(0);
     if (howManyTimes < 0) {
       errorBranch.enter();
       throw invalidTimes();
     }
     if (replicateOnce.profile(howManyTimes == 1)) {
       return (RVector<?>) (copyIfSameSize ? x.copy() : x);
     } else {
       return x.copyResized(x.getLength() * howManyTimes, false);
     }
   } else {
     // times is a vector with several elements
     if (x.getLength() != times.getLength()) {
       errorBranch.enter();
       invalidTimes();
     }
     // iterate once over the times vector to determine result vector size
     int resultLength = 0;
     for (int i = 0; i < times.getLength(); i++) {
       int t = times.getDataAt(i);
       if (t < 0) {
         errorBranch.enter();
         throw invalidTimes();
       }
       resultLength += t;
     }
     // create and populate result vector
     RVector<?> r = x.createEmptySameType(resultLength, x.isComplete());
     int wp = 0; // write pointer
     for (int i = 0; i < x.getLength(); i++) {
       for (int j = 0; j < times.getDataAt(i); ++j, ++wp) {
         r.transferElementSameType(wp, x, i);
       }
     }
     return r;
   }
 }