<NME> TestByteIterator.java
<BEF> ADDFILE
<MSG> gh-69 Added a unit test for RandomByteIterator toArray() and toString() methods.

<DFF> @@ -0,0 +1,22 @@
+package com.yahoo.ycsb;
+
+import org.testng.annotations.Test;
+import static org.testng.AssertJUnit.*;
+
+public class TestByteIterator {
+  @Test
+  public void testRandomByteIterator() {
+    int size = 100;
+    ByteIterator itor = new RandomByteIterator(size);
+    assertTrue(itor.hasNext());
+    assertEquals(size, itor.bytesLeft());
+    assertEquals(size, itor.toString().getBytes().length);
+    assertFalse(itor.hasNext());
+    assertEquals(0, itor.bytesLeft());
+
+    itor = new RandomByteIterator(size);
+    assertEquals(size, itor.toArray().length);
+    assertFalse(itor.hasNext());
+    assertEquals(0, itor.bytesLeft());
+  }
+}

{'commit': 'diff --git a/core/pom.xml b/core/pom.xml\nindex d26d09a6..86f1f88c 100644\n--- a/core/pom.xml\n+++ b/core/pom.xml\n@@ -26,6 +26,12 @@\n       <artifactId>jackson-core-asl</artifactId>\n       <version>${jackson.api.version}</version>\n     </dependency>\n+    <dependency>\n+      <groupId>org.testng</groupId>\n+      <artifactId>testng</artifactId>\n+      <version>6.1.1</version>\n+      <scope>test</scope>\n+    </dependency>\n   </dependencies>\t\n \n </project>\ndiff --git a/core/src/main/java/com/yahoo/ycsb/RandomByteIterator.java b/core/src/main/java/com/yahoo/ycsb/RandomByteIterator.java\nindex 99239f85..65db4335 100644\n--- a/core/src/main/java/com/yahoo/ycsb/RandomByteIterator.java\n+++ b/core/src/main/java/com/yahoo/ycsb/RandomByteIterator.java\n@@ -1,84 +1,87 @@\n-/**                                                                                                                                                                                \n- * Copyright (c) 2010 Yahoo! Inc. All rights reserved.                                                                                                                             \n- *                                                                                                                                                                                 \n- * Licensed under the Apache License, Version 2.0 (the "License"); you                                                                                                             \n- * may not use this file except in compliance with the License. You                                                                                                                \n- * may obtain a copy of the License at                                                                                                                                             \n- *                                                                                                                                                                                 \n- * http://www.apache.org/licenses/LICENSE-2.0                                                                                                                                      \n- *                                                                                                                                                                                 \n- * Unless required by applicable law or agreed to in writing, software                                                                                                             \n- * distributed under the License is distributed on an "AS IS" BASIS,                                                                                                               \n- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or                                                                                                                 \n- * implied. See the License for the specific language governing                                                                                                                    \n- * permissions and limitations under the License. See accompanying                                                                                                                 \n- * LICENSE file.                                                                                                                                                                   \n+/**\n+ * Copyright (c) 2010 Yahoo! Inc. All rights reserved.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the "License"); you\n+ * may not use this file except in compliance with the License. You\n+ * may obtain a copy of the License at\n+ *                                                              \n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *                                                            \n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an "AS IS" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n+ * implied. See the License for the specific language governing\n+ * permissions and limitations under the License. See accompanying\n+ * LICENSE file.\n  */\n package com.yahoo.ycsb;\n \n+/**\n+ *  A ByteIterator that generates a random sequence of bytes.\n+ */\n public class RandomByteIterator extends ByteIterator {\n-\tlong len;\n-\tlong off;\n-\tint buf_off;\n-\tbyte[] buf;\n-\t\n-\t@Override\n-\tpublic boolean hasNext() {\n-\t\treturn (off + buf_off) < len;\n-\t}\n+  private long len;\n+  private long off;\n+  private int bufOff;\n+  private byte[] buf;\n+\n+  @Override\n+  public boolean hasNext() {\n+    return (off + bufOff) < len;\n+  }\n+\n+  private void fillBytesImpl(byte[] buffer, int base) {\n+    int bytes = Utils.random().nextInt();\n+    try {\n+      buffer[base+0] = (byte)(((bytes) & 31) + \' \');\n+      buffer[base+1] = (byte)(((bytes >> 5) & 31) + \' \');\n+      buffer[base+2] = (byte)(((bytes >> 10) & 31) + \' \');\n+      buffer[base+3] = (byte)(((bytes >> 15) & 31) + \' \');\n+      buffer[base+4] = (byte)(((bytes >> 20) & 31) + \' \');\n+      buffer[base+5] = (byte)(((bytes >> 25) & 31) + \' \');\n+    } catch (ArrayIndexOutOfBoundsException e) { /* ignore it */ }\n+  }\n+\n+  private void fillBytes() {\n+    if(bufOff ==  buf.length) {\n+      fillBytesImpl(buf, 0);\n+      bufOff = 0;\n+      off += buf.length;\n+    }\n+  }\n+\n+  public RandomByteIterator(long len) {\n+    this.len = len;\n+    this.buf = new byte[6];\n+    this.bufOff = buf.length;\n+    fillBytes();\n+    this.off = 0;\n+  }\n+\n+  public byte nextByte() {\n+    fillBytes();\n+    bufOff++;\n+    return buf[bufOff-1];\n+  }\n \n-\tprivate void fillBytesImpl(byte[] buf, int base) {\n-\t\tint bytes = Utils.random().nextInt();\n-\t\ttry {\n-\t\t\tbuf[base+0] = (byte)(((bytes      ) & 31) + \' \');\n-\t\t\tbuf[base+1] = (byte)(((bytes >> 5 ) & 31) + \' \');\n-\t\t\tbuf[base+2] = (byte)(((bytes >> 10) & 31) + \' \');\n-\t\t\tbuf[base+3] = (byte)(((bytes >> 15) & 31) + \' \');\n-\t\t\tbuf[base+4] = (byte)(((bytes >> 20) & 31) + \' \');\n-\t\t\tbuf[base+5] = (byte)(((bytes >> 25) & 31) + \' \');\n-\t\t} catch (ArrayIndexOutOfBoundsException e) { /* ignore it */ }\n-\t}\n-\t\n-\tprivate void fillBytes() {\n-\t\tif(buf_off ==  buf.length) {\n-\t\t\tfillBytesImpl(buf, 0);\n-\t\t\tbuf_off = 0;\n-\t\t\toff += buf.length;\n-\t\t}\n-\t}\n-\t\n-\tpublic RandomByteIterator(long len) {\n-\t\tthis.len = len;\n-\t\tthis.buf = new byte[6];\n-\t\tthis.buf_off = buf.length;\n-\t\tfillBytes();\n-\t\tthis.off = 0;\n-\t}\n+  @Override\n+  public int nextBuf(byte[] buffer, int bufferOffset) {\n+    int ret;\n+    if(len - off < buffer.length - bufferOffset) {\n+      ret = (int)(len - off);\n+    } else {\n+      ret = buffer.length - bufferOffset;\n+    }\n+    int i;\n+    for(i = 0; i < ret; i+=6) {\n+      fillBytesImpl(buffer, i + bufferOffset);\n+    }\n+    off+=ret;\n+    return ret + bufferOffset;\n+  }\n \n-\tpublic byte nextByte() {\n-\t\tfillBytes();\n-\t\tbuf_off++;\n-\t\treturn buf[buf_off-1];\n-\t}\n-\t@Override\n-\tpublic int nextBuf(byte[] b, int buf_off) {\n-\t\tint ret;\n-\t\tif(len - off < b.length - buf_off) {\n-\t\t\tret = (int)(len - off);\n-\t\t} else {\n-\t\t\tret = b.length - buf_off;\n-\t\t}\n-\t\tint i;\n-\t\tfor(i = 0; i < ret; i+=6) {\n-\t\t\tfillBytesImpl(b, i+buf_off);\n-\t\t}\n-\t\toff+=ret;\n-\t\treturn ret + buf_off;\n-\t}\n-\t\n-\t\n-\t@Override\n-\tpublic long bytesLeft() {\n-\t\treturn len - off;\n-\t}\n+  @Override\n+  public long bytesLeft() {\n+    return len - off - bufOff;\n+  }\n }\ndiff --git a/core/src/test/java/com/yahoo/ycsb/TestByteIterator.java b/core/src/test/java/com/yahoo/ycsb/TestByteIterator.java\nnew file mode 100644\nindex 00000000..b95415b0\n--- /dev/null\n+++ b/core/src/test/java/com/yahoo/ycsb/TestByteIterator.java\n@@ -0,0 +1,22 @@\n+package com.yahoo.ycsb;\n+\n+import org.testng.annotations.Test;\n+import static org.testng.AssertJUnit.*;\n+\n+public class TestByteIterator {\n+  @Test\n+  public void testRandomByteIterator() {\n+    int size = 100;\n+    ByteIterator itor = new RandomByteIterator(size);\n+    assertTrue(itor.hasNext());\n+    assertEquals(size, itor.bytesLeft());\n+    assertEquals(size, itor.toString().getBytes().length);\n+    assertFalse(itor.hasNext());\n+    assertEquals(0, itor.bytesLeft());\n+\n+    itor = new RandomByteIterator(size);\n+    assertEquals(size, itor.toArray().length);\n+    assertFalse(itor.hasNext());\n+    assertEquals(0, itor.bytesLeft());\n+  }\n+}\n', 'commit_subject': 'gh-69 Added a unit test for RandomByteIterator toArray() and toString() methods.', 'repo_name': 'brianfrankcooper/YCSB', 'committer': {'name': 'm1ch1', 'email': 'a75494f12f73453b2bd75573f87b8f21fcc50565@cs.stanford.edu', 'time_sec': '1330836634', 'tz_offset': '-480', 'date': {'seconds': '1330836634'}}, 'commit_tree': '09b704ec33e0dec55b1d69d2d3bc4443c018329f', 'commit_message': 'gh-69 Added a unit test for RandomByteIterator toArray() and toString() methods.\n', 'language_name': 'Java', 'license': 'apache-2.0', 'hunks': '@@ -0,0 +1,22 @@\n+package com.yahoo.ycsb;\n+\n+import org.testng.annotations.Test;\n+import static org.testng.AssertJUnit.*;\n+\n+public class TestByteIterator {\n+  @Test\n+  public void testRandomByteIterator() {\n+    int size = 100;\n+    ByteIterator itor = new RandomByteIterator(size);\n+    assertTrue(itor.hasNext());\n+    assertEquals(size, itor.bytesLeft());\n+    assertEquals(size, itor.toString().getBytes().length);\n+    assertFalse(itor.hasNext());\n+    assertEquals(0, itor.bytesLeft());\n+\n+    itor = new RandomByteIterator(size);\n+    assertEquals(size, itor.toArray().length);\n+    assertFalse(itor.hasNext());\n+    assertEquals(0, itor.bytesLeft());\n+  }\n+}\n', 'addition_count': 22, 'deletion_count': 0, 'src_file': '/dev/null', 'tgt_file': 'b/core/src/test/java/com/yahoo/ycsb/TestByteIterator.java', 'file_extension': '.java', 'before_file': 'ADDFILE', 'message': 'gh-69 Added a unit test for RandomByteIterator toArray() and toString() methods.\n', 'author_name': 'm1ch1', 'lang': 'java'}