http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/iterator/TS_Iterator.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/iterator/TS_Iterator.java b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TS_Iterator.java new file mode 100644 index 0000000..37c8430 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TS_Iterator.java @@ -0,0 +1,38 @@ +/* + * 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.jena.atlas.iterator; + +import org.junit.runner.RunWith ; +import org.junit.runners.Suite ; + +@RunWith(Suite.class) [email protected]( { + TestIter.class + , TestIteratorPeek.class + , TestIteratorArray.class + , TestIteratorPushback.class + , TestIteratorWithHistory.class + , TestIteratorWithBuffer.class + , TestIteratorSlotted.class +} ) + +public class TS_Iterator +{ + +}
http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIter.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIter.java b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIter.java new file mode 100644 index 0000000..56cf8a9 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIter.java @@ -0,0 +1,539 @@ +/* + * 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.jena.atlas.iterator; + +import static org.junit.Assert.assertEquals ; +import static org.junit.Assert.assertFalse ; +import static org.junit.Assert.assertTrue ; + +import java.util.ArrayList ; +import java.util.Arrays ; +import java.util.Iterator ; +import java.util.List ; + +import org.junit.Test ; + +public class TestIter +{ + List<String> data0 = new ArrayList<>() ; + List<String> data1 = Arrays.asList("a") ; + List<String> data2 = Arrays.asList("x","y","z") ; + List<String> data3 = Arrays.asList(null, "x", null, null, null, "y", "z", null); + + @Test + public void append_1() + { + Iterator<String> iter = Iter.append(data1.iterator(), data0.iterator()) ; + test(iter, "a") ; + } + + + @Test + public void append_2() + { + Iterator<String> iter = Iter.append(data0.iterator(), data1.iterator()) ; + test(iter, "a") ; + } + + @Test + public void append_3() + { + Iterator<String> iter = Iter.append(data1.iterator(), data2.iterator()) ; + test(iter, "a", "x", "y", "z") ; + } + + @Test + public void append_4() + { + List<String> L = new ArrayList<>(3); + L.add("a"); + L.add("b"); + L.add("c"); + List<String> R = new ArrayList<>(3); + R.add("d"); + R.add("e"); + R.add("f"); + + + Iterator<String> LR = Iter.append(L.iterator(), R.iterator()) ; + + while (LR.hasNext()) + { + String s = LR.next(); + + if ("c".equals(s)) + { + LR.hasNext(); // test for JENA-60 + LR.remove(); + } + } + + assertEquals("ab", Iter.asString(L, "")); + assertEquals("def", Iter.asString(R, "")); + } + + @Test + public void append_5() + { + List<String> L = new ArrayList<>(3); + L.add("a"); + L.add("b"); + L.add("c"); + List<String> R = new ArrayList<>(3); + R.add("d"); + R.add("e"); + R.add("f"); + + + Iterator<String> LR = Iter.append(L.iterator(), R.iterator()) ; + + while (LR.hasNext()) + { + String s = LR.next(); + + if ("d".equals(s)) + { + LR.hasNext(); // test for JENA-60 + LR.remove(); + } + } + + assertEquals("abc", Iter.asString(L, "")); + assertEquals("ef", Iter.asString(R, "")); + } + + @Test + public void append_6() + { + List<String> L = new ArrayList<>(3); + L.add("a"); + L.add("b"); + L.add("c"); + List<String> R = new ArrayList<>(3); + R.add("d"); + R.add("e"); + R.add("f"); + + + Iterator<String> LR = Iter.append(L.iterator(), R.iterator()) ; + + while (LR.hasNext()) + { + LR.next() ; + } + LR.remove() ; + + assertEquals("abc", Iter.asString(L, "")) ; + assertEquals("de", Iter.asString(R, "")) ; + } + + @Test + public void asString_1() + { + String x = Iter.asString(data0, "") ; + assertEquals("", x) ; + } + + @Test + public void asString_2() + { + String x = Iter.asString(data1, "") ; + assertEquals("a", x) ; + } + + @Test + public void asString_3() + { + String x = Iter.asString(data1, "/") ; + assertEquals("a", x) ; + } + + @Test + public void asString_4() + { + String x = Iter.asString(data2, "/") ; + assertEquals("x/y/z", x) ; + } + + private void test(Iterator<?> iter, Object... items) + { + for ( Object x : items ) + { + assertTrue(iter.hasNext()) ; + assertEquals(x, iter.next()) ; + } + assertFalse(iter.hasNext()) ; + } + + static Iter.Folder<String, String> f1 = new Iter.Folder<String, String>() { + @Override + public String eval(String acc, String arg) + { + return acc + arg ; + } + } ; + + @Test + public void fold_01() + { + String[] x = { "a", "b", "c" } ; + String z = Iter.foldLeft(Arrays.asList(x), f1, "X") ; + assertEquals("Xabc", z) ; + } + + @Test + public void fold_02() + { + String[] x = { "a", "b", "c" } ; + String z = Iter.foldRight(Arrays.asList(x), f1, "X") ; + assertEquals("Xcba", z) ; + } + + @Test + public void fold_03() + { + String[] x = { } ; + String z = Iter.foldLeft(Arrays.asList(x), f1, "X") ; + assertEquals("X", z) ; + } + + @Test + public void fold_04() + { + String[] x = { } ; + String z = Iter.foldRight(Arrays.asList(x), f1, "X") ; + assertEquals("X", z) ; + } + + + @Test + public void map_01() + { + Iterator<String> it = Iter.map(data2.iterator(), new Transform<String,String>() + { + @Override + public String convert(String item) + { + return item + item; + } + }); + test(it, "xx", "yy", "zz"); + } + + @Test + public void mapMany_01() + { + Iterator<String> it = Iter.mapMany(data2.iterator(), new Transform<String,Iterator<String>>() + { + @Override + public Iterator<String> convert(String item) + { + List<String> l = new ArrayList<>(2); + l.add(item); + l.add(item + item); + return l.iterator(); + } + }); + + test(it, "x", "xx", "y", "yy", "z", "zz"); + } + + @Test + public void mapMany_02() + { + Iterator<String> it = Iter.mapMany(data2.iterator(), new Transform<String,Iterator<String>>() + { + @Override + public Iterator<String> convert(String item) + { + return Iter.nullIterator() ; + } + }); + + assertFalse(it.hasNext()) ; + } + + @Test + public void mapMany_03() + { + Iterator<String> it = Iter.mapMany(data2.iterator(), new Transform<String,Iterator<String>>() + { + int count = 0 ; + @Override + public Iterator<String> convert(String item) + { + count++ ; + if ( count%2 == 1 ) + return Iter.singleton(item) ; + else + return Iter.nullIterator() ; + } + }); + + test(it, "x", "z"); + } + + @Test + public void mapMany_04() + { + Iterator<String> it = Iter.mapMany(data2.iterator(), new Transform<String,Iterator<String>>() + { + int count = 0 ; + @Override + public Iterator<String> convert(String item) + { + count++ ; + if ( count%2 == 0 ) + return Iter.singleton(item) ; + else + return Iter.nullIterator() ; + } + }); + + test(it, "y"); + } + + + Filter<String> filter = new Filter<String>() { + @Override + public boolean accept(String item) + { + return item.length() == 1 ; + }} ; + + @Test + public void first_01() + { + Iter<String> iter = Iter.nullIter() ; + assertEquals(null, Iter.first(iter, filter)) ; + } + + @Test + public void first_02() + { + List<String> data = Arrays.asList( "11", "A", "B", "C") ; + assertEquals("A", Iter.first(data, filter)) ; + } + + @Test + public void first_03() + { + List<String> data = Arrays.asList( "11", "AA", "BB", "CC") ; + assertEquals(null, Iter.first(data, filter)) ; + } + + @Test + public void first_04() + { + Iter<String> iter = Iter.nullIter() ; + assertEquals(-1, Iter.firstIndex(iter, filter)) ; + } + + @Test + public void first_05() + { + List<String> data = Arrays.asList( "11", "A", "B", "C") ; + assertEquals(1, Iter.firstIndex(data, filter)) ; + } + + @Test + public void first_06() + { + List<String> data = Arrays.asList( "11", "AA", "BB", "CC") ; + assertEquals(-1, Iter.firstIndex(data, filter)) ; + } + + @Test + public void last_01() + { + Iter<String> iter = Iter.nullIter() ; + assertEquals(null, Iter.last(iter, filter)) ; + } + + @Test + public void last_02() + { + List<String> data = Arrays.asList( "11", "A", "B", "C") ; + assertEquals("C", Iter.last(data, filter)) ; + } + + @Test + public void last_03() + { + List<String> data = Arrays.asList( "11", "AA", "BB", "CC") ; + assertEquals(null, Iter.last(data, filter)) ; + } + + @Test + public void last_04() + { + Iter<String> iter = Iter.nullIter() ; + assertEquals(-1, Iter.lastIndex(iter, filter)) ; + } + + @Test + public void last_05() + { + List<String> data = Arrays.asList( "11", "A", "B", "C") ; + assertEquals(3, Iter.lastIndex(data, filter)) ; + } + + @Test + public void last_06() + { + List<String> data = Arrays.asList( "11", "AA", "BB", "CC") ; + assertEquals(-1, Iter.lastIndex(data, filter)) ; + } + + @Test + public void take_01() + { + List<String> data = Arrays.asList( "1", "A", "B", "CC") ; + List<String> data2 = Iter.take(data.iterator(), 2) ; + assertEquals(2, data2.size()) ; + assertEquals("1", data2.get(0)) ; + assertEquals("A", data2.get(1)) ; + } + + + @Test + public void take_02() + { + List<String> data = Arrays.asList( "1", "A", "B", "CC") ; + List<String> data2 = Iter.take(data.iterator(), 0) ; + assertEquals(0, data2.size()) ; + } + + @Test + public void take_03() + { + List<String> data = Arrays.asList( "1", "A", "B", "CC") ; + List<String> data2 = Iter.take(data.iterator(), 10) ; + assertEquals(4, data2.size()) ; + assertEquals("1", data2.get(0)) ; + assertEquals("A", data2.get(1)) ; + assertEquals("B", data2.get(2)) ; + assertEquals("CC", data2.get(3)) ; + } + + @Test + public void filter_01() + { + test(Iter.removeNulls(data3.iterator()), "x", "y", "z"); + } + + @Test + public void filter_02() + { + Iterator<String> it = Iter.filter(data3.iterator(), new Filter<String>() + { + @Override + public boolean accept(String item) + { + return "x".equals(item) || "z".equals(item) ; + } + }); + + test(it, "x", "z"); + } + + @Test + public void filter_03() + { + Iterator<String> it = Iter.filter(data3.iterator(), new Filter<String>() + { + @Override + public boolean accept(String item) + { + return (null == item) || "x".equals(item) ; + } + }); + + test(it, null, "x", null, null, null, null); + } + + @Test public void distinct_01() + { + List<String> x = Arrays.asList("a", "b", "a") ; + Iterator<String> iter = Iter.distinct(x.iterator()) ; + test(iter, "a", "b") ; + } + + @Test public void distinct_02() + { + List<String> x = Arrays.asList("a", "b", "a") ; + Iterator<String> iter = Iter.distinctAdjacent(x.iterator()) ; + test(iter, "a", "b", "a") ; + } + + @Test public void distinct_03() + { + List<String> x = Arrays.asList("a", "a", "b", "b", "b", "a", "a") ; + Iterator<String> iter = Iter.distinct(x.iterator()) ; + test(iter, "a", "b") ; + } + + @Test public void distinct_04() + { + List<String> x = Arrays.asList("a", "a", "b", "b", "b", "a", "a") ; + Iterator<String> iter = Iter.distinctAdjacent(x.iterator()) ; + test(iter, "a", "b", "a") ; + } + + private static class AlwaysAcceptFilter implements Filter<Object> { + @Override + public boolean accept(Object o) { + return true; + } + } + + private static class NeverAcceptFilter implements Filter<Object> { + @Override + public boolean accept(Object o) { + return false; + } + } + + + private static class AlwaysAcceptFilterStack extends FilterStack<Object> { + public AlwaysAcceptFilterStack(Filter<Object> f) { + super(f); + } + + @Override + public boolean acceptAdditional(Object o) { + return true; + } + } + + @Test + public void testFilterStack_01() { + Filter<Object> filter = new AlwaysAcceptFilter(); + FilterStack<Object> filterStack = new AlwaysAcceptFilterStack(filter); + assertTrue(filterStack.accept(new Object())); + } + + @Test + public void testFilterStack_02() { + Filter<Object> filter = new NeverAcceptFilter(); + FilterStack<Object> filterStack = new AlwaysAcceptFilterStack(filter); + assertFalse(filterStack.accept(new Object())); + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorArray.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorArray.java b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorArray.java new file mode 100644 index 0000000..5108865 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorArray.java @@ -0,0 +1,104 @@ +/* + * 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.jena.atlas.iterator; + +import java.util.Iterator ; +import java.util.NoSuchElementException ; + +import org.apache.jena.atlas.iterator.IteratorArray ; +import org.apache.jena.atlas.junit.BaseTest ; +import org.junit.Test ; + +public class TestIteratorArray extends BaseTest +{ + IteratorArray<String> create(String ... a) + { + return IteratorArray.create(a) ; + } + + IteratorArray<String> create(int start, int finish, String ... a) + { + return IteratorArray.create(a, start, finish) ; + } + + @Test public void arrayIterator_1() + { + Iterator<String> iter = create() ; + assertFalse(iter.hasNext()) ; + assertFalse(iter.hasNext()) ; + } + + @Test public void arrayIterator_2() + { + Iterator<String> iter = create("a") ; + assertTrue(iter.hasNext()) ; + assertEquals("a", iter.next()) ; + assertFalse(iter.hasNext()) ; + assertFalse(iter.hasNext()) ; + } + + + @Test public void arrayIterator_3() + { + Iterator<String> iter = create("a", "b", "c") ; + assertTrue(iter.hasNext()) ; + assertEquals("a", iter.next()) ; + assertTrue(iter.hasNext()) ; + assertEquals("b", iter.next()) ; + assertTrue(iter.hasNext()) ; + assertEquals("c", iter.next()) ; + assertFalse(iter.hasNext()) ; + assertFalse(iter.hasNext()) ; + } + + @Test public void arrayIterator_4() + { + Iterator<String> iter = create("a") ; + assertEquals("a", iter.next()) ; + try { iter.next() ; fail("Expected NoSuchElementException") ; } + catch (NoSuchElementException ex) {} + } + + @Test public void arrayIterator_5() + { + Iterator<String> iter = create(0,1, "a", "b", "c") ; + assertEquals("a", iter.next()) ; + assertFalse(iter.hasNext()) ; + } + + @Test public void arrayIterator_6() + { + Iterator<String> iter = create(1, 3, "a", "b", "c", "d") ; + assertEquals("b", iter.next()) ; + assertEquals("c", iter.next()) ; + assertFalse(iter.hasNext()) ; + } + + @Test public void arrayIterator_7() + { + IteratorArray<String> iter = create(1, 3, "a", "b", "c", "d") ; + assertEquals("b", iter.current()) ; + assertEquals("b", iter.current()) ; + assertEquals("b", iter.next()) ; + assertEquals("c", iter.current()) ; + assertEquals("c", iter.next()) ; + assertFalse(iter.hasNext()) ; + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorPeek.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorPeek.java b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorPeek.java new file mode 100644 index 0000000..33f0b70 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorPeek.java @@ -0,0 +1,107 @@ +/* + * 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.jena.atlas.iterator; + +import java.util.ArrayList ; +import java.util.List ; + +import org.apache.jena.atlas.iterator.Iter ; +import org.apache.jena.atlas.iterator.IteratorArray ; +import org.apache.jena.atlas.iterator.PeekIterator ; +import org.apache.jena.atlas.junit.BaseTest ; +import org.junit.Test ; + +public class TestIteratorPeek extends BaseTest +{ + List<String> data0 = new ArrayList<>() ; + List<String> data1 = new ArrayList<>() ; + { + data1.add("a") ; + } + + List<String> data2 = new ArrayList<>() ; + { + data2.add("x") ; + data2.add("y") ; + data2.add("z") ; + } + + @Test public void iter_01() + { + Iter<String> iter = Iter.iter(data2) ; + iter = iter.append(data2.iterator()) ; + test(iter, "x", "y", "z", "x", "y", "z") ; + } + + private void test(Iter<?> iter, Object... items) + { + for ( Object x : items ) + { + assertTrue(iter.hasNext()) ; + assertEquals(x, iter.next()) ; + } + assertFalse(iter.hasNext()) ; + } + + private static PeekIterator<String> create(String...a) + { + return new PeekIterator<>(IteratorArray.create(a)) ; + } + + @Test public void peek_1() + { + PeekIterator<String> peek = create("a", "b", "c") ; + assertEquals("a", peek.peek()) ; + test(Iter.iter(peek), "a", "b", "c") ; + } + + @Test public void peek_2() + { + PeekIterator<String> peek = create() ; + assertFalse(peek.hasNext()) ; + } + + @Test public void peek_3() + { + PeekIterator<String> peek = create("a") ; + assertEquals("a", peek.peek()) ; + } + + @Test public void peek_4() + { + PeekIterator<String> peek = create("a") ; + assertEquals("a", peek.peek()) ; + assertEquals("a", peek.peek()) ; + assertEquals("a", peek.next()) ; + assertFalse(peek.hasNext()) ; + } + + @Test public void peek_5() + { + PeekIterator<String> peek = create("a", "b") ; + assertEquals("a", peek.peek()) ; + assertEquals("a", peek.peek()) ; + assertEquals("a", peek.next()) ; + assertTrue(peek.hasNext()) ; + assertEquals("b", peek.peek()) ; + assertEquals("b", peek.peek()) ; + assertEquals("b", peek.next()) ; + assertFalse(peek.hasNext()) ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorPushback.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorPushback.java b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorPushback.java new file mode 100644 index 0000000..51916a3 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorPushback.java @@ -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.jena.atlas.iterator; + +import java.util.ArrayList ; +import java.util.List ; + +import org.apache.jena.atlas.iterator.Iter ; +import org.apache.jena.atlas.iterator.PushbackIterator ; +import org.apache.jena.atlas.junit.BaseTest ; +import org.junit.Test ; + +public class TestIteratorPushback extends BaseTest +{ + + static List<String> data = new ArrayList<>() ; + static { + data.add("a") ; + data.add("b") ; + data.add("c") ; + } + + @Test(expected=IllegalArgumentException.class) + public void pushback01() { new PushbackIterator<String>(null) ; } + + @Test public void pushback02() + { + PushbackIterator<String> iter = new PushbackIterator<>(data.iterator()) ; + assertEquals("a", iter.next()) ; + assertEquals("b", iter.next()) ; + assertEquals("c", iter.next()) ; + assertFalse(iter.hasNext()) ; + } + + @Test public void pushback03() + { + PushbackIterator<String> iter = new PushbackIterator<>(data.iterator()) ; + iter.pushback("x") ; + assertEquals("x", iter.next()) ; + assertEquals("a", iter.next()) ; + assertEquals(2, Iter.count(iter)) ; + } + + @Test public void pushback04() + { + PushbackIterator<String> iter = new PushbackIterator<>(data.iterator()) ; + assertEquals("a", iter.next()) ; + iter.pushback("x") ; + assertEquals("x", iter.next()) ; + assertEquals("b", iter.next()) ; + assertEquals(1, Iter.count(iter)) ; + } + + @Test public void pushback05() + { + PushbackIterator<String> iter = new PushbackIterator<>(data.iterator()) ; + assertEquals("a", iter.next()) ; + iter.pushback("x") ; + iter.pushback("y") ; + assertEquals("y", iter.next()) ; + assertEquals("x", iter.next()) ; + assertEquals("b", iter.next()) ; + assertEquals(1, Iter.count(iter)) ; + } + + @Test public void pushback06() + { + PushbackIterator<String> iter = new PushbackIterator<>(data.iterator()) ; + assertEquals(3, Iter.count(iter)) ; + iter.pushback("x") ; + iter.pushback("y") ; + assertEquals("y", iter.next()) ; + assertEquals("x", iter.next()) ; + assertFalse(iter.hasNext()) ; + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorSlotted.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorSlotted.java b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorSlotted.java new file mode 100644 index 0000000..8abf440 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorSlotted.java @@ -0,0 +1,136 @@ +/* + * 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.jena.atlas.iterator; + +import java.util.Arrays ; +import java.util.Collection ; +import java.util.Iterator ; +import java.util.List ; + +import org.apache.jena.atlas.junit.BaseTest ; +import org.junit.Test ; +import org.junit.runner.RunWith ; +import org.junit.runners.Parameterized ; +import org.junit.runners.Parameterized.Parameters ; + +@RunWith(Parameterized.class) + +public class TestIteratorSlotted extends BaseTest +{ + @Parameters(name = "{index}: {0}") + public static Collection<Object[]> implementations() { + IterFactory factory1 = new IterFactory() { + @Override + public IteratorSlotted<String> create(String... array) { + return new IterStr1(array) ; + }} ; + IterFactory factory2 = new IterFactory() { + @Override + public IteratorSlotted<String> create(String... array) { + return new IterStr2(array) ; + }} ; + return Arrays.asList(new Object[][] { {"hasMore accurate", factory1}, {"hasMore always true", factory2} }) ; + } + + /** Accurate hasMore */ + static class IterStr1 extends IteratorSlotted<String> + { + private List<String> array ; + private Iterator<String> iter ; + + IterStr1(String... array) { + this.array = Arrays.asList(array) ; + iter = this.array.iterator() ; + } + + @Override + protected String moveToNext() { + return iter.next() ; + } + + @Override + protected boolean hasMore() { + return iter.hasNext() ; + } + } + + /** hasMore ios always true, returns null in moveToNext */ + static class IterStr2 extends IteratorSlotted<String> + { + private List<String> array ; + private Iterator<String> iter ; + + IterStr2(String... array) { + this.array = Arrays.asList(array) ; + iter = this.array.iterator() ; + } + + @Override + protected String moveToNext() { + if ( !iter.hasNext() ) + return null ; + return iter.next() ; + } + + @Override + protected boolean hasMore() { + return true ; + } + } + + + interface IterFactory { IteratorSlotted<String> create(String...array) ; } + + + private IterFactory factory ; + + public TestIteratorSlotted(String name, IterFactory factory) { + this.factory = factory ; + } + + @Test public void iter_01() + { + IteratorSlotted<String> iter = factory.create() ; + assertFalse(iter.hasNext()) ; + } + + @Test public void iter_02() + { + IteratorSlotted<String> iter = factory.create("A") ; + assertTrue(iter.hasNext()) ; + assertEquals("A", iter.peek()) ; + assertEquals("A", iter.peek()) ; + assertEquals("A", iter.next()) ; + assertFalse(iter.hasNext()) ; + assertNull(iter.peek()) ; + } + + @Test public void iter_03() + { + IteratorSlotted<String> iter = factory.create("A", "B") ; + assertTrue(iter.hasNext()) ; + assertEquals("A", iter.peek()) ; + assertEquals("A", iter.next()) ; + assertEquals("B", iter.peek()) ; + assertEquals("B", iter.next()) ; + assertFalse(iter.hasNext()) ; + } + + +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorWithBuffer.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorWithBuffer.java b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorWithBuffer.java new file mode 100644 index 0000000..5bc70fd --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorWithBuffer.java @@ -0,0 +1,97 @@ +/* + * 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.jena.atlas.iterator; + +import java.util.Arrays ; +import java.util.List ; + +import org.apache.jena.atlas.iterator.IteratorWithBuffer ; +import org.apache.jena.atlas.junit.BaseTest ; +import org.junit.Test ; + +public class TestIteratorWithBuffer extends BaseTest +{ + + @Test public void iterBuffer_01() + { + IteratorWithBuffer<String> iter = createBuffered(1, "a", "b", "c") ; + assertEquals(1, iter.currentSize()) ; + assertEquals("a", iter.peek(0)) ; + assertEquals(1, iter.currentSize()) ; + assertEquals("a", iter.next()) ; + assertEquals(1, iter.currentSize()) ; + assertEquals("b", iter.peek(0)) ; + assertEquals(1, iter.currentSize()) ; + assertEquals("b", iter.next()) ; + assertEquals(1, iter.currentSize()) ; + assertEquals("c", iter.peek(0)) ; + assertEquals(1, iter.currentSize()) ; + assertEquals("c", iter.next()) ; + assertEquals(0, iter.currentSize()) ; + assertEquals(null, iter.peek(0)) ; + assertEquals(0, iter.currentSize()) ; + } + + @Test public void iterBuffer_02() + { + IteratorWithBuffer<String> iter = createBuffered(2, "a", "b", "c") ; + assertEquals(2, iter.currentSize()) ; + assertEquals("a", iter.peek(0)) ; + assertEquals("b", iter.peek(1)) ; + assertEquals("a", iter.next()) ; + + assertEquals("b", iter.peek(0)) ; + assertEquals("c", iter.peek(1)) ; + assertEquals("b", iter.next()) ; + + assertEquals("c", iter.peek(0)) ; + assertEquals(null, iter.peek(1)) ; + assertEquals("c", iter.next()) ; + assertEquals(null, iter.peek(0)) ; + } + + @Test public void iterBuffer_03() + { + IteratorWithBuffer<String> iter = createBuffered(1) ; + assertEquals(null, iter.peek(0)) ; + } + + @Test(expected=IndexOutOfBoundsException.class) + public void iterBuffer_04() + { + IteratorWithBuffer<String> iter = createBuffered(0, "a") ; + assertEquals(null, iter.peek(0)) ; + } + + @Test public void iterBuffer_05() + { + IteratorWithBuffer<String> iter = createBuffered(2, "a") ; + assertEquals("a", iter.peek(0)) ; + assertEquals(null, iter.peek(1)) ; + assertEquals("a", iter.next()) ; + } + + private IteratorWithBuffer<String> createBuffered(int N, String... strings) + { + List<String> data = Arrays.asList(strings) ; + IteratorWithBuffer<String> iter = new IteratorWithBuffer<>(data.iterator(), N) ; + return iter ; + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorWithHistory.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorWithHistory.java b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorWithHistory.java new file mode 100644 index 0000000..b2545b2 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/iterator/TestIteratorWithHistory.java @@ -0,0 +1,87 @@ +/* + * 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.jena.atlas.iterator; + +import java.util.Arrays ; +import java.util.List ; + +import org.apache.jena.atlas.iterator.IteratorWithHistory ; +import org.apache.jena.atlas.junit.BaseTest ; +import org.junit.Test ; + +public class TestIteratorWithHistory extends BaseTest +{ + @Test public void iterHistory_01() + { + IteratorWithHistory<String> iter = createHistory(1, "a", "b", "c") ; + assertEquals(0, iter.currentSize()) ; + assertEquals(null, iter.getPrevious(0)) ; + } + + @Test public void iterHistory_02() + { + IteratorWithHistory<String> iter = createHistory(1, "a", "b", "c") ; + assertEquals("a", iter.next()) ; + assertEquals(1, iter.currentSize()) ; + } + + @Test public void iterHistory_03() + { + IteratorWithHistory<String> iter = createHistory(2, "a", "b", "c") ; + assertEquals("a", iter.next()) ; + assertEquals("b", iter.next()) ; + assertEquals(2, iter.currentSize()) ; + assertEquals("b", iter.getPrevious(0)) ; + assertEquals("a", iter.getPrevious(1)) ; + } + + @Test(expected=IndexOutOfBoundsException.class) + public void iterHistory_04() + { + IteratorWithHistory<String> iter = createHistory(2, "a", "b", "c") ; + iter.getPrevious(2) ; + } + + @Test + public void iterHistory_05() + { + IteratorWithHistory<String> iter = createHistory(2, "a", "b", "c") ; + assertEquals("a", iter.next()) ; + assertEquals("a", iter.getPrevious(0)) ; + assertEquals(1, iter.currentSize()) ; + + assertEquals("b", iter.next()) ; + assertEquals("b", iter.getPrevious(0)) ; + assertEquals("a", iter.getPrevious(1)) ; + assertEquals(2, iter.currentSize()) ; + + assertEquals("c", iter.next()) ; + assertEquals(2, iter.currentSize()) ; + assertEquals("c", iter.getPrevious(0)) ; + assertEquals("b", iter.getPrevious(1)) ; + } + + private IteratorWithHistory<String> createHistory(int N, String... strings) + { + List<String> data = Arrays.asList(strings) ; + IteratorWithHistory<String> iter = new IteratorWithHistory<>(data.iterator(), N) ; + return iter ; + } + +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/junit/BaseTest.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/junit/BaseTest.java b/jena-base/src/test/java/org/apache/jena/atlas/junit/BaseTest.java new file mode 100644 index 0000000..6d5f15a --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/junit/BaseTest.java @@ -0,0 +1,45 @@ +/* + * 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.jena.atlas.junit; + +import java.util.ArrayList ; +import java.util.List ; +import java.util.Locale ; + +import org.junit.Assert ; + +public class BaseTest extends Assert +{ + public static void assertEqualsIgnoreCase(String msg, String a, String b) + { + a = a.toLowerCase(Locale.ROOT) ; + b = b.toLowerCase(Locale.ROOT) ; + assertEquals(msg, a, b) ; + } + + public static <T> void assertEqualsUnordered(List<T> list1, List<T> list2) { + if ( list1.size() != list2.size() ) + fail("Expected: "+list1+" : Actual: "+list2) ; + List<T> list2a = new ArrayList<>(list2) ; + for ( T elt : list1 ) + list2a.remove(elt) ; + if ( list2a.size() != 0 ) + fail("Expected: "+list1+" : Actual: "+list2) ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/lib/TS_Lib.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TS_Lib.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TS_Lib.java new file mode 100644 index 0000000..b91d4ad --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TS_Lib.java @@ -0,0 +1,57 @@ +/* + * 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.jena.atlas.lib; + + +import org.junit.runner.RunWith ; +import org.junit.runners.Suite ; + +/** + * Tests for the Atlas lib package + * + */ +@RunWith(Suite.class) [email protected]( { + TestAlg.class + , TestBitsLong.class + , TestBitsInt.class + , TestBytes.class + , TestHex.class + , TestListUtils.class + , TestSetUtils.class + , TestCache.class + , TestCache2.class + , TestColumnMap.class + , TestFileOps.class + , TestStrUtils.class + , TestXMLLib.class + , TestAlarmClock.class + , TestRefLong.class + , TestReverseComparator.class + , TestTrie.class + , TestFilenameProcessing.class + , TestMultiSet.class + , TestNumberUtils.class + , TestDateTimeUtils.class +} ) + +public class TS_Lib +{ + +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java new file mode 100644 index 0000000..7d2fbfe --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlarmClock.java @@ -0,0 +1,92 @@ +/* + * 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.jena.atlas.lib ; + +import static org.apache.jena.atlas.lib.Lib.sleep ; + +import java.util.concurrent.atomic.AtomicInteger ; + +import org.apache.jena.atlas.junit.BaseTest ; +import org.junit.Test ; + +public class TestAlarmClock extends BaseTest { + AtomicInteger count = new AtomicInteger(0) ; + Runnable callback = new Runnable() { + @Override + public void run() { + count.getAndIncrement() ; + } + } ; + + @Test + public void alarm_01() { + AlarmClock alarmClock = new AlarmClock() ; + // Very long - never happens. + alarmClock.add(callback, 10000000) ; + alarmClock.cancel(callback) ; + assertEquals(0, count.get()) ; + alarmClock.release() ; + } + + @Test + public void alarm_02() { + AlarmClock alarmClock = new AlarmClock() ; + // Short - happens. + alarmClock.add(callback, 10) ; + sleep(150) ; + assertEquals(1, count.get()) ; + // try to cancel anyway. + alarmClock.cancel(callback) ; + alarmClock.release() ; + } + + @Test + public void alarm_03() { + AlarmClock alarmClock = new AlarmClock() ; + alarmClock.add(callback, 10) ; + alarmClock.add(callback, 1000000) ; + sleep(150) ; + // ping1 went off. + assertEquals(1, count.get()) ; + alarmClock.cancel(callback) ; + alarmClock.release() ; + } + + @Test + public void alarm_04() { + AlarmClock alarmClock = new AlarmClock() ; + alarmClock.add(callback, 10) ; + alarmClock.add(callback, 20) ; + sleep(200) ; + // ping1 went off. ping2 went off. + assertEquals(2, count.get()) ; + alarmClock.release() ; + } + + @Test + public void alarm_05() { + AlarmClock alarmClock = new AlarmClock() ; + alarmClock.add(callback, 1000) ; + alarmClock.reset(callback, 2000) ; + sleep(50) ; + // The reset should have removed the callback before it happened. + assertEquals(0, count.get()) ; + alarmClock.release() ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlg.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlg.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlg.java new file mode 100644 index 0000000..b7eb6e5 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestAlg.java @@ -0,0 +1,226 @@ +/* + * 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.jena.atlas.lib; + +import java.nio.ByteBuffer ; +import java.nio.ByteOrder ; +import java.nio.IntBuffer ; + +import org.apache.jena.atlas.junit.BaseTest ; +import org.apache.jena.atlas.lib.Alg ; +import org.junit.Test ; + +public class TestAlg extends BaseTest +{ + // Linear search is really there to test binary search. + @Test public void linear1() + { + int[] data = {1, 2, 3} ; + IntBuffer b = make(data) ; + int idx = 0 ; + + idx = Alg.linearSearch(b, 1) ; + assertEquals(0, idx) ; + + idx = Alg.linearSearch(b, 2) ; + assertEquals(1, idx) ; + + idx = Alg.linearSearch(b, 3) ; + assertEquals(2, idx) ; + + } + + @Test public void linear2() + { + int[] data = {2, 4, 6} ; + IntBuffer b = make(data) ; + int idx = 0 ; + + idx = Alg.linearSearch(b, 1) ; + assertEquals(-1, idx) ; + + idx = Alg.linearSearch(b, 3) ; + assertEquals(-2, idx) ; + + idx = Alg.linearSearch(b, 5) ; + assertEquals(-3, idx) ; + + idx = Alg.linearSearch(b, 7) ; + assertEquals(-4, idx) ; + } + + @Test public void linear3() + { + int[] data = {} ; + IntBuffer b = make(data) ; + int idx = Alg.linearSearch(b, 1) ; + assertEquals(-1, idx) ; + } + + @Test public void linear4() + { + int[] data = {9} ; + IntBuffer b = make(data) ; + int idx ; + idx = Alg.linearSearch(b, 1) ; + assertEquals(-1, idx) ; + idx = Alg.linearSearch(b, 9) ; + assertEquals(0, idx) ; + idx = Alg.linearSearch(b, 100) ; + assertEquals(-2, idx) ; + } + + @Test public void linear5() + { + int[] data = {2, 4, 6, 8, 10} ; + IntBuffer b = make(data) ; + int idx = 0 ; + idx = Alg.linearSearch(b, 1, 4, 6) ; + assertEquals(2, idx) ; + + idx = Alg.linearSearch(b, 1, 4, 5) ; + assertEquals(-3, idx) ; + + idx = Alg.linearSearch(b, 1, 4, 2) ; + assertEquals(-2, idx) ; + + idx = Alg.linearSearch(b, 1, 4, 10) ; + assertEquals(-5, idx) ; + } + + @Test public void linear6() + { + int[] data = {2, 4, 6, 8, 10} ; + IntBuffer b = make(data) ; + int idx = 0 ; + idx = Alg.linearSearch(b, 3, 3, 6) ; + assertEquals(-4, idx) ; + idx = Alg.linearSearch(b, 3, 3, 5) ; + assertEquals(-4, idx) ; + idx = Alg.linearSearch(b, 3, 3, 50) ; + assertEquals(-4, idx) ; + } + + @Test public void linear7() + { + int[] data = {2, 4, 4, 8, 8} ; + IntBuffer b = make(data) ; + int idx = 0 ; + idx = Alg.linearSearch(b, 4) ; + assertEquals(1, idx) ; + idx = Alg.linearSearch(b, 8) ; + assertEquals(3, idx) ; + } + + @Test public void binary1() + { + int[] data = {1, 2, 3} ; + IntBuffer b = make(data) ; + search(b, 1) ; + search(b, 2) ; + search(b, 3) ; + } + + @Test public void binary2() + { + int[] data = {2, 4, 6} ; + IntBuffer b = make(data) ; + search(b, 1) ; + search(b, 3) ; + search(b, 5) ; + search(b, 7) ; + } + + @Test public void binary3() + { + int[] data = {} ; + IntBuffer b = make(data) ; + search(b, 1) ; + } + + @Test public void binary4() + { + int[] data = {9} ; + IntBuffer b = make(data) ; + search(b, 1) ; + search(b, 9) ; + search(b, 100) ; + } + + @Test public void binary5() + { + int[] data = {2, 4, 6, 8, 10} ; + IntBuffer b = make(data) ; + search(b, 6, 1, 4) ; + search(b, 5, 1, 4) ; + search(b, 2, 1, 4) ; + search(b, 10, 1, 4) ; + } + + @Test public void binary6() + { + int[] data = {2, 4, 6, 8, 10} ; + IntBuffer b = make(data) ; + search(b, 6, 3, 3) ; + search(b, 5, 3, 3) ; + search(b, 50, 3, 3) ; + } + + // Binary serach does not state which index is returned for sequenences of same value + @Test public void binary7() + { + int[] data = {2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8} ; + IntBuffer b = make(data) ; + int idx = 0 ; + idx = Alg.binarySearch(b, 4) ; + assertEquals(4, data[idx]) ; + idx = Alg.linearSearch(b, 8) ; + assertEquals(8, data[idx]) ; + + search(b, 3) ; + search(b, 5) ; + search(b, 9) ; + } + + private static IntBuffer make(int[] data) + { + //IntBuffer x = IntBuffer.allocate(data.length) ; + ByteBuffer z = ByteBuffer.allocate(4*data.length) ; + z.order(ByteOrder.BIG_ENDIAN) ; + IntBuffer x = z.asIntBuffer() ; + + for ( int i = 0 ; i < data.length ; i++ ) + x.put(i,data[i]) ; + return x ; + } + + private static void search(IntBuffer b, int k) + { + int idx1 = Alg.linearSearch(b, k) ; + int idx2 = Alg.binarySearch(b, k) ; + assertEquals(idx1, idx2) ; + } + + private static void search(IntBuffer b, int k, int low, int high) + { + int idx1 = Alg.linearSearch(b, low, high, k) ; + int idx2 = Alg.binarySearch(b, low, high, k) ; + assertEquals(idx1, idx2) ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBitsInt.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBitsInt.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBitsInt.java new file mode 100644 index 0000000..769aeb2 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBitsInt.java @@ -0,0 +1,468 @@ +/* + * 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.jena.atlas.lib; + +import org.apache.jena.atlas.junit.BaseTest ; +import org.apache.jena.atlas.lib.BitsInt ; +import org.junit.Test ; + + +public class TestBitsInt extends BaseTest +{ + @Test public void testMask1() + { + int v = BitsInt.mask(0,1) ; + check(0x1, v) ; + } + + @Test public void testMask2() + { + int v = BitsInt.mask(0,2) ; + check(0x3, v) ; + } + + @Test public void testMask3() + { + int v = BitsInt.mask(1,2) ; + check(0x2, v) ; + } + + @Test public void testMask4() + { + int v = BitsInt.mask(0,32) ; + check(-1, v) ; + } + + @Test public void testMask5() + { + int v = BitsInt.mask(16,24) ; + check(0x00FF0000, v) ; + } + + @Test public void testMask6() + { + int v = BitsInt.mask(16,32) ; + check(0xFFFF0000, v) ; + } + + @Test public void testMask7() + { + int v = BitsInt.mask(0, 0) ; + check(0, v) ; + } + + @Test public void testMaskZero1() + { + int v = BitsInt.maskZero(0,1) ; + check(~0x1, v) ; + } + + @Test public void testMaskZero2() + { + int v = BitsInt.maskZero(0,2) ; + check(~0x3, v) ; + } + + @Test public void testMaskZero3() + { + int v = BitsInt.maskZero(1,2) ; + check(0xFFFFFFFD, v) ; + } + + @Test public void testMaskZero4() + { + int v = BitsInt.maskZero(0,32) ; + check(0, v) ; + } + + @Test public void testMaskZero5() + { + int v = BitsInt.maskZero(16,24) ; + check(0xFF00FFFF, v) ; + } + + @Test public void testMaskZero6() + { + int v = BitsInt.maskZero(16,32) ; + check(0xFFFF, v) ; + } + + @Test public void testMaskZero7() + { + int v = BitsInt.maskZero(0, 0) ; + check(-1, v) ; + } + + @Test public void testClear1() + { + int v = 0xF0F0 ; + v = BitsInt.clear(v, 4, 8) ; + String s = Integer.toHexString(v) ; + check(0xF000, v ) ; + } + + @Test public void testClear2() + { + int v = 0x80000000; + v = BitsInt.clear(v, 31, 32) ; + String s = Integer.toHexString(v) ; + check(0x0, v ) ; + } + + @Test public void testClear3() + { + int v = 0xC0000000; + v = BitsInt.clear(v, 31, 32) ; + String s = Integer.toHexString(v) ; + check(0x40000000, v ) ; + } + + @Test public void testClear4() + { + int v = -1 ; + v = BitsInt.clear(v, 31, 32) ; + String s = Integer.toHexString(v) ; + check(0x7FFFFFFF, v ) ; + } + + @Test public void testClear5() + { + int v = -1 ; + v = BitsInt.clear(v, 16, 32) ; + String s = Integer.toHexString(v) ; + check(0x0000FFFF, v ) ; + } + + @Test public void testClear6() + { + int v = -1 ; + v = BitsInt.clear(v, 0, 16) ; + String s = Integer.toHexString(v) ; + check(0xFFFF0000, v ) ; + } + + @Test public void testClear7() + { + int v = -1 ; + v = BitsInt.clear(v, 0, 0) ; + String s = Integer.toHexString(v) ; + check(-1, v ) ; + } + + @Test public void testSet1() + { + int v = 0x0 ; + v = BitsInt.set(v, 0, 1) ; + check(1, v) ; + } + + @Test public void testSet2() + { + int v = 0x1 ; + v = BitsInt.set(v, 0, 1) ; + check(1, v) ; + } + + @Test public void testSet3() + { + int v = 0xF0 ; + v = BitsInt.set(v, 0, 1) ; + check(0xF1, v) ; + } + + @Test public void testSet4() + { + int v = 0xF0F0F0F0 ; + v = BitsInt.set(v, 0, 8) ; + check(0xF0F0F0FF, v) ; + } + + @Test public void testSet5() + { + int v = 0 ; + v = BitsInt.set(v, 16, 24) ; + check(0x00FF0000, v) ; + } + + @Test public void testSet6() + { + int v = 0 ; + v = BitsInt.set(v, 31, 32) ; + check(0x80000000, v) ; + } + + @Test public void testSet7() + { + int v = 0 ; + v = BitsInt.set(v, 30, 32) ; + check(0xC0000000, v) ; + } + + @Test public void testSet8() + { + int v = 0 ; + v = BitsInt.set(v, 0, 32) ; + check(-1, v) ; + } + + @Test public void testSet9() + { + int v = 0 ; + v = BitsInt.set(v, 10, 10) ; + check(0, v) ; + } + + @Test public void testSetBit1() + { + int v = 0 ; + v = BitsInt.set(v, 0) ; + check(1, v) ; + } + + @Test public void testSetBit2() + { + int v = 0 ; + v = BitsInt.set(v, 1) ; + check(2, v) ; + } + + @Test public void testSetBit3() + { + int v = 1 ; + v = BitsInt.set(v, 0) ; + check(1, v) ; + } + + @Test public void testSetBit4() + { + int v = -1 ; + v = BitsInt.set(v, 0) ; + check(-1, v) ; + } + + @Test public void testSetBit5() + { + int v = 0 ; + v = BitsInt.set(v, 30) ; + check(0x40000000, v) ; + } + + @Test public void testSetBit6() + { + int v = 0 ; + v = BitsInt.set(v, 31) ; + check(0x80000000, v) ; + } + + @Test public void testBitTest1() + { + int v = 0 ; + assertTrue(BitsInt.test(v, false, 0)) ; + } + + @Test public void testBitTest2() + { + int v = 1 ; + assertTrue(BitsInt.test(v, true, 0)) ; + } + + @Test public void testBitTest3() + { + int v = -1 ; + assertTrue(BitsInt.test(v, true, 31)) ; + } + + @Test public void testBitTest4() + { + int v = 0x7FFFFFFF ; + assertTrue(BitsInt.test(v, false, 31)) ; + } + + @Test public void testBitsTest1() + { + int v = 0x76543210 ; + assertTrue(BitsInt.test(v, 0x0, 0, 4)) ; + } + + @Test public void testBitsTest2() + { + int v = 0x76543210 ; + assertTrue(BitsInt.test(v, 0x10, 0, 8)) ; + } + + @Test public void testBitsTest3() + { + int v = 0x76543210 ; + assertTrue(BitsInt.test(v, v, 0, 32)) ; + } + + @Test public void testBitsTest4() + { + int v = 0x76543210 ; + assertFalse(BitsInt.test(v, 0, 0, 32)) ; + } + + @Test public void testBitsTest5() + { + int v = 0x76543210 ; + assertTrue(BitsInt.test(v, 0x00543200, 8, 24)) ; + } + + @Test public void testIsSet1() + { + int v = 0x00000010 ; + BitsInt.isSet(v, 4) ; + assertTrue(BitsInt.isSet(v, 4)) ; + assertFalse(BitsInt.isSet(v, 3)) ; + assertFalse(BitsInt.isSet(v, 5)) ; + } + + @Test public void testAccess1() + { + int v = -1 ; + v = BitsInt.access(v, 4, 8) ; + check(0xF0, v ) ; + } + + @Test public void testAccess2() + { + int v = 0x76543210 ; + v = BitsInt.access(v, 0, 8) ; + check(0x10, v ) ; + } + + @Test public void testAccess3() + { + int v = 0x76543210 ; + v = BitsInt.access(v, 0, 32) ; + check(0x76543210, v ) ; + } + + @Test public void testAccess4() + { + int v = 0xFEF43210 ; + v = BitsInt.access(v, 30, 32) ; + check(0xC0000000, v ) ; + } + + @Test public void testAccess5() + { + int v = 0x76543210 ; + v = BitsInt.access(v, 0, 2) ; + check(0, v ) ; + } + + @Test public void testPack1() + { + int v = 0 ; + v = BitsInt.pack(v, 0xF, 0, 4) ; + check(0xF, v ) ; + } + + @Test public void testPack2() + { + int v = 0xF0 ; + v = BitsInt.pack(v, 0x2, 0, 4) ; + check(0xF2, v ) ; + } + + @Test public void testPack3() + { + int v = -1 ; + v = BitsInt.pack(v, 0x2, 0, 8) ; + check(0xFFFFFF02, v ) ; + } + + @Test public void testPack4() + { + int v = 0xFFFF0000 ; + v = BitsInt.pack(v, 0x2, 8, 16) ; + check(0xFFFF0200, v ) ; + } + + @Test public void testPack5() + { + int v = 0xFFFF0000 ; + v = BitsInt.pack(v, 0xFF, 8, 16) ; + check(0xFFFFFF00, v ) ; + } + + @Test public void testUnpack1() + { + int v = 0xABCDABCD ; + v = BitsInt.unpack(v, 0, 4) ; + check(0xD, v ) ; + } + + @Test public void testUnpack2() + { + int v = 0xABCDABCD ; + v = BitsInt.unpack(v, 31, 32) ; + check(1, v ) ; + } + + @Test public void testUnpack3() + { + int v = 0xABCDABCD ; + v = BitsInt.unpack(v, 24, 32) ; + check(0xAB, v ) ; + } + + @Test public void testUnpack4() + { + int v = 0xAB1234CD ; + v = BitsInt.unpack(v, 8, 24) ; + check(0x1234, v ) ; + } + + @Test public void testUnpackStr1() + { + String s = "ABCD" ; + int v = BitsInt.unpack(s, 0, 4) ; + check(0xABCD, v ) ; + } + + @Test public void testUnpackStr2() + { + String s = "ABCD" ; + int v = BitsInt.unpack(s, 2, 4) ; + check(0xCD, v ) ; + } + + @Test public void testUnpackStr3() + { + String s = "ABCD" ; + int v = BitsInt.unpack(s, 0, 2) ; + check(0xAB, v ) ; + } + + private void check(int expected, int actual) + { + check(null, expected, actual) ; + } + + private void check(String msg, int expected, int actual) + { + if ( expected == actual ) return ; + String s = "Expected: "+Integer.toHexString(expected)+" : Got: "+Integer.toHexString(actual) ; + if ( msg != null ) + s = msg+": "+s ; + assertFalse(s, true) ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBitsLong.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBitsLong.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBitsLong.java new file mode 100644 index 0000000..1fea546 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBitsLong.java @@ -0,0 +1,468 @@ +/* + * 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.jena.atlas.lib; + +import org.apache.jena.atlas.junit.BaseTest ; +import org.apache.jena.atlas.lib.BitsLong ; +import org.junit.Test ; + + +public class TestBitsLong extends BaseTest +{ + @Test public void testMask1() + { + long v = BitsLong.mask(0,1) ; + check(0x1L, v) ; + } + + @Test public void testMask2() + { + long v = BitsLong.mask(0,2) ; + check(0x3L, v) ; + } + + @Test public void testMask3() + { + long v = BitsLong.mask(1,2) ; + check(0x2L, v) ; + } + + @Test public void testMask4() + { + long v = BitsLong.mask(0,64) ; + check(-1L, v) ; + } + + @Test public void testMask5() + { + long v = BitsLong.mask(16,48) ; + check(0x0000FFFFFFFF0000L, v) ; + } + + @Test public void testMask6() + { + long v = BitsLong.mask(16,64) ; + check(0xFFFFFFFFFFFF0000L, v) ; + } + + @Test public void testMask7() + { + long v = BitsLong.mask(0, 0) ; + check(0L, v) ; + } + + @Test public void testMaskZero1() + { + long v = BitsLong.maskZero(0,1) ; + check(~0x1L, v) ; + } + + @Test public void testMaskZero2() + { + long v = BitsLong.maskZero(0,2) ; + check(~0x3L, v) ; + } + + @Test public void testMaskZero3() + { + long v = BitsLong.maskZero(1,2) ; + check(0xFFFFFFFFFFFFFFFDL, v) ; + } + + @Test public void testMaskZero4() + { + long v = BitsLong.maskZero(0,64) ; + check(0, v) ; + } + + @Test public void testMaskZero5() + { + long v = BitsLong.maskZero(16,48) ; + check(0xFFFF00000000FFFFL, v) ; + } + + @Test public void testMaskZero6() + { + long v = BitsLong.maskZero(16,64) ; + check(0xFFFFL, v) ; + } + + @Test public void testMaskZero7() + { + long v = BitsLong.maskZero(0, 0) ; + check(-1L, v) ; + } + + @Test public void testClear1() + { + long v = 0xF0F0 ; + v = BitsLong.clear(v, 4, 8) ; + String s = Long.toHexString(v) ; + check(0xF000L, v ) ; + } + + @Test public void testClear2() + { + long v = 0x8000000000000000L; + v = BitsLong.clear(v, 63, 64) ; + String s = Long.toHexString(v) ; + check(0x0L, v ) ; + } + + @Test public void testClear3() + { + long v = 0xC000000000000000L; + v = BitsLong.clear(v, 63, 64) ; + String s = Long.toHexString(v) ; + check(0x4000000000000000L, v ) ; + } + + @Test public void testClear4() + { + long v = -1 ; + v = BitsLong.clear(v, 63, 64) ; + String s = Long.toHexString(v) ; + check(0x7FFFFFFFFFFFFFFFL, v ) ; + } + + @Test public void testClear5() + { + long v = -1 ; + v = BitsLong.clear(v, 32, 64) ; + String s = Long.toHexString(v) ; + check(0x00000000FFFFFFFFL, v ) ; + } + + @Test public void testClear6() + { + long v = -1 ; + v = BitsLong.clear(v, 0, 32) ; + String s = Long.toHexString(v) ; + check(0xFFFFFFFF00000000L, v ) ; + } + + @Test public void testClear7() + { + long v = -1L ; + v = BitsLong.clear(v, 0, 0) ; + String s = Long.toHexString(v) ; + check(-1L, v ) ; + } + + @Test public void testSet1() + { + long v = 0x0 ; + v = BitsLong.set(v, 0, 1) ; + check(1, v) ; + } + + @Test public void testSet2() + { + long v = 0x1 ; + v = BitsLong.set(v, 0, 1) ; + check(1, v) ; + } + + @Test public void testSet3() + { + long v = 0xF0 ; + v = BitsLong.set(v, 0, 1) ; + check(0xF1, v) ; + } + + @Test public void testSet4() + { + long v = 0xF0F0F0F0F0F0F0F0L ; + v = BitsLong.set(v, 0, 8) ; + check(0xF0F0F0F0F0F0F0FFL, v) ; + } + + @Test public void testSet5() + { + long v = 0 ; + v = BitsLong.set(v, 16, 48) ; + check(0x0000FFFFFFFF0000L, v) ; + } + + @Test public void testSet6() + { + long v = 0 ; + v = BitsLong.set(v, 63, 64) ; + check(0x8000000000000000L, v) ; + } + + @Test public void testSet7() + { + long v = 0 ; + v = BitsLong.set(v, 62, 64) ; + check(0xC000000000000000L, v) ; + } + + @Test public void testSet8() + { + long v = 0 ; + v = BitsLong.set(v, 0, 64) ; + check(-1L, v) ; + } + + @Test public void testSet9() + { + long v = 0 ; + v = BitsLong.set(v, 10, 10) ; + check(0, v) ; + } + + @Test public void testSetBit1() + { + long v = 0 ; + v = BitsLong.set(v, 0) ; + check(1, v) ; + } + + @Test public void testSetBit2() + { + long v = 0 ; + v = BitsLong.set(v, 1) ; + check(2, v) ; + } + + @Test public void testSetBit3() + { + long v = 1 ; + v = BitsLong.set(v, 0) ; + check(1, v) ; + } + + @Test public void testSetBit4() + { + long v = -1 ; + v = BitsLong.set(v, 0) ; + check(-1, v) ; + } + + @Test public void testSetBit5() + { + long v = 0 ; + v = BitsLong.set(v, 62) ; + check(0x4000000000000000L, v) ; + } + + @Test public void testSetBit6() + { + long v = 0 ; + v = BitsLong.set(v, 63) ; + check(0x8000000000000000L, v) ; + } + + @Test public void testBitTest1() + { + long v = 0 ; + assertTrue(BitsLong.test(v, false, 0)) ; + } + + @Test public void testBitTest2() + { + long v = 1 ; + assertTrue(BitsLong.test(v, true, 0)) ; + } + + @Test public void testBitTest3() + { + long v = -1 ; + assertTrue(BitsLong.test(v, true, 63)) ; + } + + @Test public void testBitTest4() + { + long v = 0x7FFFFFFFFFFFFFFFL ; + assertTrue(BitsLong.test(v, false, 63)) ; + } + + @Test public void testBitsTest1() + { + long v = 0xFEDCBA9876543210L ; + assertTrue(BitsLong.test(v, 0x0, 0, 4)) ; + } + + @Test public void testBitsTest2() + { + long v = 0xFEDCBA9876543210L ; + assertTrue(BitsLong.test(v, 0x10, 0, 8)) ; + } + + @Test public void testBitsTest3() + { + long v = 0xFEDCBA9876543210L ; + assertTrue(BitsLong.test(v, v, 0, 64)) ; + } + + @Test public void testBitsTest4() + { + long v = 0xFEDCBA9876543210L ; + assertFalse(BitsLong.test(v, 0, 0, 64)) ; + } + + @Test public void testBitsTest5() + { + long v = 0xFEDCBA9876543210L ; + assertTrue(BitsLong.test(v, 0x0000BA9876540000L, 16, 48)) ; + } + + @Test public void testIsSet1() + { + long v = 0x0000000000000010L ; + BitsLong.isSet(v, 4) ; + assertTrue(BitsLong.isSet(v, 4)) ; + assertFalse(BitsLong.isSet(v, 3)) ; + assertFalse(BitsLong.isSet(v, 5)) ; + } + + @Test public void testAccess1() + { + long v = -1 ; + v = BitsLong.access(v, 4, 8) ; + check(0xF0L, v ) ; + } + + @Test public void testAccess2() + { + long v = 0xFEDCBA9876543210L ; + v = BitsLong.access(v, 0, 8) ; + check(0x10L, v ) ; + } + + @Test public void testAccess3() + { + long v = 0xFEDCBA9876543210L ; + v = BitsLong.access(v, 0, 64) ; + check(0xFEDCBA9876543210L, v ) ; + } + + @Test public void testAccess4() + { + long v = 0xFEDCBA9876543210L ; + v = BitsLong.access(v, 62, 64) ; + check(0xC000000000000000L, v ) ; + } + + @Test public void testAccess5() + { + long v = 0xFEDCBA9876543210L ; + v = BitsLong.access(v, 0, 2) ; + check(0L, v ) ; + } + + @Test public void testPack1() + { + long v = 0 ; + v = BitsLong.pack(v, 0xFL, 0, 4) ; + check(0xFL, v ) ; + } + + @Test public void testPack2() + { + long v = 0xF0 ; + v = BitsLong.pack(v, 0x2, 0, 4) ; + check(0xF2L, v ) ; + } + + @Test public void testPack3() + { + long v = -1 ; + v = BitsLong.pack(v, 0x2, 0, 8) ; + check(0xFFFFFFFFFFFFFF02L, v ) ; + } + + @Test public void testPack4() + { + long v = 0xFFFFFFFF00000000L ; + v = BitsLong.pack(v, 0x2, 16, 32) ; + check(0xFFFFFFFF00020000L, v ) ; + } + + @Test public void testPack5() + { + long v = 0xFFFFFFFF00000000L ; + v = BitsLong.pack(v, 0xFFFF, 16, 32) ; + check(0xFFFFFFFFFFFF0000L, v ) ; + } + + @Test public void testUnpack1() + { + long v = 0xABCDABCDABCDABCDL ; + v = BitsLong.unpack(v, 0, 4) ; + check(0xDL, v ) ; + } + + @Test public void testUnpack2() + { + long v = 0xABCDABCDABCDABCDL ; + v = BitsLong.unpack(v, 63, 64) ; + check(1L, v ) ; + } + + @Test public void testUnpack3() + { + long v = 0xABCDABCDABCDABCDL ; + v = BitsLong.unpack(v, 56, 64) ; + check(0xABL, v ) ; + } + + @Test public void testUnpack4() + { + long v = 0xABCD12345678ABCDL ; + v = BitsLong.unpack(v, 32, 48) ; + check(0x1234L, v ) ; + } + + @Test public void testUnpackStr1() + { + String s = "ABCD" ; + long v = BitsLong.unpack(s, 0, 4) ; + check(0xABCDL, v ) ; + } + + @Test public void testUnpackStr2() + { + String s = "ABCD" ; + long v = BitsLong.unpack(s, 2, 4) ; + check(0xCDL, v ) ; + } + + @Test public void testUnpackStr3() + { + String s = "ABCD" ; + long v = BitsLong.unpack(s, 0, 2) ; + check(0xABL, v ) ; + } + + private void check(long expected, long actual) + { + check(null, expected, actual) ; + } + + private void check(String msg, long expected, long actual) + { + if ( expected == actual ) return ; + String s = "Expected: "+Long.toHexString(expected)+" : Got: "+Long.toHexString(actual) ; + if ( msg != null ) + s = msg+": "+s ; + assertFalse(s, true) ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/1320f8db/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBytes.java ---------------------------------------------------------------------- diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBytes.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBytes.java new file mode 100644 index 0000000..a857509 --- /dev/null +++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestBytes.java @@ -0,0 +1,198 @@ +/* + * 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.jena.atlas.lib; + +import java.nio.ByteBuffer ; + +import org.apache.jena.atlas.junit.BaseTest ; +import org.apache.jena.atlas.lib.Bytes ; +import org.junit.Test ; + +public class TestBytes extends BaseTest +{ + @Test public void packInt1() + { + byte[] b = new byte[4] ; + Bytes.setInt(0x01020304,b) ; + assertEquals(0x01, b[0]) ; + assertEquals(0x02, b[1]) ; + assertEquals(0x03, b[2]) ; + assertEquals(0x04, b[3]) ; + } + + @Test public void packInt2() + { + byte[] b = new byte[8] ; + Bytes.setInt(0x01020304,b,0) ; + Bytes.setInt(0x05060708,b,4) ; + assertEquals(0x01, b[0]) ; + assertEquals(0x02, b[1]) ; + assertEquals(0x03, b[2]) ; + assertEquals(0x04, b[3]) ; + assertEquals(0x05, b[4]) ; + assertEquals(0x06, b[5]) ; + assertEquals(0x07, b[6]) ; + assertEquals(0x08, b[7]) ; + } + + @Test public void packInt3() + { + byte[] b = new byte[4] ; + Bytes.setInt(0xF1F2F3F4,b) ; + int i = Bytes.getInt(b) ; + assertEquals(0xF1F2F3F4, i) ; + } + + @Test public void packInt4() + { + byte[] b = new byte[8] ; + Bytes.setInt(0x01020304,b,0) ; + Bytes.setInt(0x05060708,b,4) ; + + int i1 = Bytes.getInt(b,0) ; + int i2 = Bytes.getInt(b,4) ; + assertEquals(0x01020304, i1) ; + assertEquals(0x05060708, i2) ; + } + + @Test public void packLong5() + { + byte[] b = new byte[8] ; + Bytes.setLong(0x0102030405060708L,b) ; + assertEquals(0x01, b[0]) ; + assertEquals(0x02, b[1]) ; + assertEquals(0x03, b[2]) ; + assertEquals(0x04, b[3]) ; + assertEquals(0x05, b[4]) ; + assertEquals(0x06, b[5]) ; + assertEquals(0x07, b[6]) ; + assertEquals(0x08, b[7]) ; + } + + @Test public void packLong6() + { + byte[] b = new byte[16] ; + Bytes.setLong(0x0102030405060708L,b,0) ; + Bytes.setLong(0x1112131415161718L,b,8) ; + assertEquals(0x01, b[0]) ; + assertEquals(0x02, b[1]) ; + assertEquals(0x03, b[2]) ; + assertEquals(0x04, b[3]) ; + assertEquals(0x05, b[4]) ; + assertEquals(0x06, b[5]) ; + assertEquals(0x07, b[6]) ; + assertEquals(0x08, b[7]) ; + + assertEquals(0x11, b[0+8]) ; + assertEquals(0x12, b[1+8]) ; + assertEquals(0x13, b[2+8]) ; + assertEquals(0x14, b[3+8]) ; + assertEquals(0x15, b[4+8]) ; + assertEquals(0x16, b[5+8]) ; + assertEquals(0x17, b[6+8]) ; + assertEquals(0x18, b[7+8]) ; + } + + @Test public void packLong7() + { + byte[] b = new byte[8] ; + Bytes.setLong(0xF1F2F3F4F5F6F7F8L,b) ; + long i = Bytes.getLong(b) ; + assertEquals (0xF1F2F3F4F5F6F7F8L,i) ; + } + + @Test public void packLong8() + { + byte[] b = new byte[16] ; + Bytes.setLong(0xF1F2F3F4F5F6F7F8L,b,0) ; + Bytes.setLong(0xA1A2A3A4A5A6A7A8L,b,8) ; + + long i1 = Bytes.getLong(b,0) ; + long i2 = Bytes.getLong(b,8) ; + assertEquals(0xF1F2F3F4F5F6F7F8L,i1) ; + assertEquals(0xA1A2A3A4A5A6A7A8L,i2) ; + } + + @Test public void compare1() { compare(0, new byte[]{}, new byte[]{}) ; } + @Test public void compare2() { compare(+1, new byte[]{1}, new byte[]{}) ; } + @Test public void compare3() { compare(-1, new byte[]{1}, new byte[]{1,2}) ; } + + @Test public void compare4() { compare(+1, new byte[]{1,3}, new byte[]{1,2}) ; } + @Test public void compare5() { compare(+1, new byte[]{1,3}, new byte[]{1,2,3}) ; } + @Test public void compare6() { compare(-1, new byte[]{1,2}, new byte[]{1,2,3}) ; } + + byte[] bytes = { 1, 2, 3, 4, 5, 6 } ; + + @Test public void slice1() + { + byte[] x = Bytes.copyOf(bytes) ; + assertArrayEquals(bytes, x) ; + } + + @Test public void slice3() + { + byte[] x = Bytes.copyOf(bytes,3) ; + byte[] y = new byte[]{4,5,6} ; + assertArrayEquals(y, x) ; + } + + @Test public void slice2() + { + byte[] x = Bytes.copyOf(bytes,3,2) ; + byte[] y = new byte[]{4,5} ; + assertArrayEquals(y, x) ; + } + + private static void compare(int expected, byte[] b1, byte[] b2) + { + int x = Bytes.compare(b1, b2) ; + if ( x > 0 && expected > 0 ) return ; + if ( x == 0 && expected == 0 ) return ; + if ( x < 0 && expected < 0 ) return ; + fail("Does not compare: "+Bytes.asHex(b1)+" :: "+Bytes.asHex(b2)) ; + } + + private static void codec(String str) + { + ByteBuffer bb = ByteBuffer.allocate(16) ; + Bytes.toByteBuffer(str, bb) ; + bb.flip() ; + String str2 = Bytes.fromByteBuffer(bb) ; + assertEquals(str, str2) ; + } + + static private final String asciiBase = "abc" ; + static private final String latinBase = "ÃéÃÿ" ; + static private final String latinExtraBase = "ỹï¬ï¬" ; // fi-ligature, fl-ligature + static private final String greekBase = "αβγ" ; + static private final String hewbrewBase = "×××" ; + static private final String arabicBase = "ءآأ"; + static private final String symbolsBase = "âºâ»âªâ«" ; + static private final String chineseBase = "å«åå µæ³" ; // The Art of War + static private final String japaneseBase = "æ¥æ¬" ; // Japanese + + @Test public void codec1() { codec(asciiBase) ; } + @Test public void codec2() { codec("") ; } + @Test public void codec3() { codec(greekBase) ; } + @Test public void codec4() { codec(hewbrewBase) ; } + @Test public void codec5() { codec(arabicBase) ; } + @Test public void codec6() { codec(symbolsBase) ; } + @Test public void codec7() { codec(chineseBase) ; } + @Test public void codec8() { codec(japaneseBase) ; } +}
