@Test
  // Test basic truncate to vector.
  public void testTruncateScalar()  {
      int largeMaxLength = 100;

      byte[] result;

      // No truncate (ASCII) -- maximum length large.
      Assert.assertTrue(StringExpr.characterCount(blue) == 4);
      result = StringExpr.truncateScalar(blue, largeMaxLength);
      Assert.assertTrue(Arrays.equals(blue, result));

      Assert.assertTrue(StringExpr.characterCount(redgreen) == 8);
      result = StringExpr.truncateScalar(redgreen, largeMaxLength);
      Assert.assertTrue(Arrays.equals(redgreen, result));

      Assert.assertTrue(StringExpr.characterCount(ascii_sentence) == 31);
      result = StringExpr.truncateScalar(ascii_sentence, largeMaxLength);
      Assert.assertTrue(Arrays.equals(ascii_sentence, result));

      Assert.assertTrue(StringExpr.characterCount(blanksLeft) == 5);
      result = StringExpr.truncateScalar(blanksLeft, largeMaxLength);
      Assert.assertTrue(Arrays.equals(blanksLeft, result));

      // No truncate (ASCII) -- same maximum length.
      Assert.assertTrue(StringExpr.characterCount(blue) == 4);
      result = StringExpr.truncateScalar(blue, blue.length);
      Assert.assertTrue(Arrays.equals(blue, result));

      Assert.assertTrue(StringExpr.characterCount(redgreen) == 8);
      result = StringExpr.truncateScalar(redgreen, redgreen.length);
      Assert.assertTrue(Arrays.equals(redgreen, result));

      Assert.assertTrue(StringExpr.characterCount(ascii_sentence) == 31);
      result = StringExpr.truncateScalar(ascii_sentence, ascii_sentence.length);
      Assert.assertTrue(Arrays.equals(ascii_sentence, result));

      Assert.assertTrue(StringExpr.characterCount(blanksLeft) == 5);
      result = StringExpr.truncateScalar(blanksLeft, blanksLeft.length);
      Assert.assertTrue(Arrays.equals(blanksLeft, result));

      // Simple truncation.
      result = StringExpr.truncateScalar(blue, 3);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(blue, 3), result));

      result = StringExpr.truncateScalar(redgreen, 6);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(redgreen, 6), result));

      result = StringExpr.truncateScalar(ascii_sentence, 14);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(ascii_sentence, 14), result));

      result = StringExpr.truncateScalar(blanksLeft, 2);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(blanksLeft, 2), result));

      result = StringExpr.truncateScalar(blanksRight, 4);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(blanksRight, 4), result));

      result = StringExpr.truncateScalar(blanksBoth, 2);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(blanksBoth, 2), result));

      result = StringExpr.truncateScalar(blankString, 1);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(blankString, 1), result));

      result = StringExpr.truncateScalar(blankRanges, 29);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(blankRanges, 29), result));

      // Multi-byte truncation.
      byte[] scratch = new byte[100];
      byte[] multiByte;

      addMultiByteCharRightPadded1_1(scratch);
      multiByte = Arrays.copyOf(scratch, 4);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 4) == 2);
      result = StringExpr.truncateScalar(multiByte, 1);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(multiByte, 3), result));

      addMultiByteCharRightPadded1_2(scratch);
      multiByte = Arrays.copyOf(scratch, 5);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 5) == 3);
      result = StringExpr.truncateScalar(multiByte, 2);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(multiByte, 4), result));

      addMultiByteCharRightPadded1_3(scratch);
      multiByte = Arrays.copyOf(scratch, 9);
      Assert.assertTrue(StringExpr.characterCount(multiByte, 0, 9) == 4);
      result = StringExpr.truncateScalar(multiByte, 2);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(multiByte, 5), result));

      addMultiByteCharRightPadded1_2(scratch);
      multiByte = Arrays.copyOfRange(scratch, 3, 3 + 2);
      Assert.assertTrue(StringExpr.characterCount(multiByte) == 2);
      result = StringExpr.truncateScalar(multiByte, 1);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(multiByte, 1), result));

      int sentenceOneLen = addMultiByteCharSentenceOne(scratch, 0);
      byte[] sentenceOne = Arrays.copyOf(scratch, sentenceOneLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceOne) == 10);
      result = StringExpr.truncateScalar(sentenceOne, 8);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceOne, 20), result));

      byte[] sentenceOnePortion = Arrays.copyOf(sentenceOne, sentenceOneLen - 3);
      Assert.assertTrue(StringExpr.characterCount(sentenceOnePortion) == 9);
      result = StringExpr.truncateScalar(sentenceOnePortion, 3);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceOnePortion, 9), result));

      int sentenceTwoLen = addMultiByteCharSentenceTwo(scratch, 0);
      byte[] sentenceTwo = Arrays.copyOf(scratch, sentenceTwoLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwo) == 13);
      result = StringExpr.truncateScalar(sentenceTwo, 9);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceTwo, 16), result));

      byte[] sentenceTwoPortion = Arrays.copyOf(sentenceTwo, sentenceTwoLen - 5);
      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPortion) == 10);
      result = StringExpr.truncateScalar(sentenceTwoPortion, 6);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceTwoPortion, 11), result));

      int start;

      // Left pad longer strings with multi-byte characters.
      start = addPads(scratch, 0, 3);
      int sentenceOnePaddedLeftLen = addMultiByteCharSentenceOne(scratch, start);
      byte[] sentenceOnePaddedLeft = Arrays.copyOf(scratch, sentenceOnePaddedLeftLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedLeft) == 3 + 10);
      result = StringExpr.truncateScalar(sentenceOnePaddedLeft, 4);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceOnePaddedLeft, 6), result));

      byte[] sentenceOnePaddedLeftPortion = Arrays.copyOf(sentenceOnePaddedLeft, sentenceOnePaddedLeftLen - 3);
      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedLeftPortion) == 3 + 9);
      result = StringExpr.truncateScalar(sentenceOnePaddedLeftPortion, 7);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceOnePaddedLeftPortion, 13), result));

      start = addPads(scratch, 0, 2);
      int sentenceTwoPaddedLeftLen = addMultiByteCharSentenceTwo(scratch, start);
      byte[] sentenceTwoPaddedLeft = Arrays.copyOf(scratch, sentenceTwoPaddedLeftLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedLeft) == 2 + 13);
      result = StringExpr.truncateScalar(sentenceTwoPaddedLeft, 14);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceTwoPaddedLeft, 24), result));

      byte[] sentenceTwoPaddedLeftPortion = Arrays.copyOf(sentenceTwoPaddedLeft, sentenceTwoPaddedLeftLen - 5);
      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedLeftPortion) == 2 + 10);
      result = StringExpr.truncateScalar(sentenceTwoPaddedLeftPortion, 9);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceTwoPaddedLeftPortion, 15), result));

      // Right pad longer strings with multi-byte characters.
      start = addMultiByteCharSentenceOne(scratch, 0);
      int sentenceOnePaddedRightLen = addPads(scratch, start, 4);
      byte[] sentenceOnePaddedRight = Arrays.copyOf(scratch, sentenceOnePaddedRightLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedRight) == 10 + 4);
      result = StringExpr.truncateScalar(sentenceOnePaddedRight, 1);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceOnePaddedRight, 3), result));

      byte[] sentenceOnePaddedRightPortion = Arrays.copyOf(sentenceOnePaddedRight, sentenceOnePaddedRightLen - 3 - 4);
      Assert.assertTrue(StringExpr.characterCount(sentenceOnePaddedRightPortion) == 9);
      result = StringExpr.truncateScalar(sentenceOnePaddedRightPortion, 5);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceOnePaddedRightPortion, 13), result));

      start = addMultiByteCharSentenceTwo(scratch, 0);
      int sentenceTwoPaddedRightLen = addPads(scratch, start, 1);
      byte[] sentenceTwoPaddedRight = Arrays.copyOf(scratch, sentenceTwoPaddedRightLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedRight) == 13 + 1);
      result = StringExpr.truncateScalar(sentenceTwoPaddedRight, 6);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceTwoPaddedRight, 11), result));

      byte[] sentenceTwoPaddedRightPortion = Arrays.copyOf(sentenceTwoPaddedRight, sentenceTwoPaddedRightLen - 5 - 1);
      Assert.assertTrue(StringExpr.characterCount(sentenceTwoPaddedRightPortion) == 10);
      result = StringExpr.truncateScalar(sentenceTwoPaddedRightPortion, 8);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceTwoPaddedRightPortion, 14), result));

      // Multi-byte characters with blank ranges.
      int sentenceBlankRangesLen = addMultiByteCharSentenceBlankRanges(scratch, 0);
      byte[] sentenceBlankRanges = Arrays.copyOf(scratch, sentenceBlankRangesLen);

      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRanges) == 17);
      result = StringExpr.truncateScalar(sentenceBlankRanges, 4);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceBlankRanges, 9), result));

      byte[] sentenceBlankRangesPortion = Arrays.copyOf(sentenceBlankRanges, sentenceBlankRangesLen - 3);
      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRangesPortion) == 16);
      result = StringExpr.truncateScalar(sentenceBlankRangesPortion, 14);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceBlankRangesPortion, 23), result));

      sentenceBlankRangesPortion = Arrays.copyOfRange(sentenceBlankRanges, 7, 7 + 17);
      Assert.assertTrue(StringExpr.characterCount(sentenceBlankRangesPortion) == 13);
      result = StringExpr.truncateScalar(sentenceBlankRangesPortion, 11);
      Assert.assertTrue(Arrays.equals(Arrays.copyOf(sentenceBlankRangesPortion, 15), result));
      Assert.assertTrue(StringExpr.characterCount(result) == 11);
  }
Test vectorized expression and filter evaluation for strings.
ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorStringExpressions.java