Skip to content

Commit

Permalink
Moved unit test also to Java.
Browse files Browse the repository at this point in the history
  • Loading branch information
rxin committed Jun 11, 2015
1 parent 4eff7bd commit 911c450
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ case class Substring(str: Expression, pos: Expression, len: Expression)
ba.slice(st, end)
case s: UTF8String =>
val (st, end) = slicePos(start, length, () => s.length())
s.slice(st, end)
s.substring(st, end)
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public byte[] getBytes() {
* @param start the position of first code point
* @param until the position after last code point, exclusive.
*/
public UTF8String slice(final int start, final int until) {
public UTF8String substring(final int start, final int until) {
if (until <= start || start >= bytes.length) {
return UTF8String.fromBytes(new byte[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.spark.unsafe.bitset;

import junit.framework.Assert;
import org.apache.spark.unsafe.bitset.BitSet;
import org.junit.Test;

import org.apache.spark.unsafe.memory.MemoryBlock;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.unsafe.types;

import java.io.UnsupportedEncodingException;

import junit.framework.Assert;
import org.junit.Test;

public class UTF8StringSuite {

private void checkBasic(String str, int len) throws UnsupportedEncodingException {
Assert.assertEquals(UTF8String.fromString(str).length(), len);
Assert.assertEquals(UTF8String.fromBytes(str.getBytes("utf8")).length(), len);

Assert.assertEquals(UTF8String.fromString(str), str);
Assert.assertEquals(UTF8String.fromBytes(str.getBytes("utf8")), str);
Assert.assertEquals(UTF8String.fromString(str).toString(), str);
Assert.assertEquals(UTF8String.fromBytes(str.getBytes("utf8")).toString(), str);
Assert.assertEquals(UTF8String.fromBytes(str.getBytes("utf8")), UTF8String.fromString(str));

Assert.assertEquals(UTF8String.fromString(str).hashCode(),
UTF8String.fromBytes(str.getBytes("utf8")).hashCode());
}

@Test
public void basicTest() throws UnsupportedEncodingException {
checkBasic("hello", 5);
checkBasic("世 界", 3);
}

@Test
public void contains() {
Assert.assertTrue(UTF8String.fromString("hello").contains(UTF8String.fromString("ello")));
Assert.assertFalse(UTF8String.fromString("hello").contains(UTF8String.fromString("vello")));
Assert.assertFalse(UTF8String.fromString("hello").contains(UTF8String.fromString("hellooo")));
Assert.assertTrue(UTF8String.fromString("大千世界").contains(UTF8String.fromString("千世")));
Assert.assertFalse(UTF8String.fromString("大千世界").contains(UTF8String.fromString("世千")));
Assert.assertFalse(
UTF8String.fromString("大千世界").contains(UTF8String.fromString("大千世界好")));
}

@Test
public void startsWith() {
Assert.assertTrue(UTF8String.fromString("hello").startsWith(UTF8String.fromString("hell")));
Assert.assertFalse(UTF8String.fromString("hello").startsWith(UTF8String.fromString("ell")));
Assert.assertFalse(UTF8String.fromString("hello").startsWith(UTF8String.fromString("hellooo")));
Assert.assertTrue(UTF8String.fromString("大千世界").startsWith(UTF8String.fromString("大千")));
Assert.assertFalse(UTF8String.fromString("大千世界").startsWith(UTF8String.fromString("千")));
Assert.assertFalse(
UTF8String.fromString("大千世界").startsWith(UTF8String.fromString("大千世界好")));
}

@Test
public void endsWith() {
Assert.assertTrue(UTF8String.fromString("hello").endsWith(UTF8String.fromString("ello")));
Assert.assertFalse(UTF8String.fromString("hello").endsWith(UTF8String.fromString("ellov")));
Assert.assertFalse(UTF8String.fromString("hello").endsWith(UTF8String.fromString("hhhello")));
Assert.assertTrue(UTF8String.fromString("大千世界").endsWith(UTF8String.fromString("世界")));
Assert.assertFalse(UTF8String.fromString("大千世界").endsWith(UTF8String.fromString("世")));
Assert.assertFalse(
UTF8String.fromString("大千世界").endsWith(UTF8String.fromString("我的大千世界")));
}

@Test
public void substring() {
Assert.assertEquals(
UTF8String.fromString("hello").substring(0, 0), UTF8String.fromString(""));
Assert.assertEquals(
UTF8String.fromString("hello").substring(1, 3), UTF8String.fromString("el"));
Assert.assertEquals(
UTF8String.fromString("大千世界").substring(0, 1), UTF8String.fromString("大"));
Assert.assertEquals(
UTF8String.fromString("大千世界").substring(1, 3), UTF8String.fromString("千世"));
Assert.assertEquals(
UTF8String.fromString("大千世界").substring(3, 5), UTF8String.fromString("界"));
}
}

0 comments on commit 911c450

Please sign in to comment.