// Path: src/main/java/com/yahoo/memory4/Util.java
// public static final String characterPad(final String s, final int fieldLength, final char padChar,
//     final boolean postpend) {
//   final char[] chArr = s.toCharArray();
//   final int sLen = chArr.length;
//   if (sLen < fieldLength) {
//     final char[] out = new char[fieldLength];
//     final int blanks = fieldLength - sLen;
// 
//     if (postpend) {
//       for (int i = 0; i < sLen; i++) {
//         out[i] = chArr[i];
//       }
//       for (int i = sLen; i < fieldLength; i++) {
//         out[i] = padChar;
//       }
//     } else { //prepend
//       for (int i = 0; i < blanks; i++) {
//         out[i] = padChar;
//       }
//       for (int i = blanks; i < fieldLength; i++) {
//         out[i] = chArr[i - blanks];
//       }
//     }
// 
//     return String.valueOf(out);
//   }
//   return s;
// }
// 
// Path: src/main/java/com/yahoo/memory4/Util.java
// public static WritableMemory memoryRequestHandler(final WritableMemory origMem,
//     final long newCapacityBytes, final boolean copy) {
//   final MemoryRequest memReq = origMem.getMemoryRequest();
//   if (memReq == null) {
//     throw new IllegalArgumentException(
//         "Insufficient space. MemoryRequest callback cannot be null.");
//   }
//   final WritableMemory newDstMem = (copy)
//       ? memReq.request(origMem, origMem.getCapacity(), newCapacityBytes)
//       : memReq.request(newCapacityBytes);
// 
//   if (newDstMem == null) {
//     throw new IllegalArgumentException(
//         "Insufficient space and Memory returned by MemoryRequest cannot be null.");
//   }
//   final long newCap = newDstMem.getCapacity(); //may be more than requested, but not less.
//   if (newCap < newCapacityBytes) {
//     memReq.closeRequest(newDstMem);
//     throw new IllegalArgumentException(
//         "Insufficient space. Memory returned by MemoryRequest is not the requested capacity: "
//         + "Returned: " + newCap + " < Requested: " + newCapacityBytes);
//   }
// 
//   if (copy) { //copy and request free.
//     origMem.copyTo(0, newDstMem, 0, Math.min(origMem.getCapacity(), newCap));
//     memReq.closeRequest(origMem, newDstMem);
//   }
//   return newDstMem;
// }
// 
// Path: src/main/java/com/yahoo/memory4/Util.java
// public static final String zeroPad(final String s, final int fieldLength) {
//   return characterPad(s, fieldLength, '0', false);
// }


import static com.yahoo.memory4.Util.characterPad;
import static com.yahoo.memory4.Util.memoryRequestHandler;
import static com.yahoo.memory4.Util.zeroPad;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
    @Override //not used
    public void closeRequest(WritableMemory memToFree, WritableMemory newMem) { }
  } //end class BadMemoryManager1

  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////

  //Binary Search
  @Test
  public void checkBinarySearch() {
    int k = 1024; //longs
    WritableMemory wMem = WritableMemory.allocate(k << 3); //1024 longs
    for (int i = 0; i < k; i++) { wMem.putLong(i << 3, i); }
    long idx = Util.binarySearchLongs(wMem, 0, k - 1, k / 2);
    long val = wMem.getLong(idx << 3);
    assertEquals(idx, k/2);
    assertEquals(val, k/2);

    idx = Util.binarySearchLongs(wMem, 0, k - 1, k);
    assertEquals(idx, -1024);
  }

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void checkBoundsTest() {
    UnsafeUtil.checkBounds(999, 2, 1000);
  }

  @Test
  public void checkPadding() {
    String s = "123";
// Path: src/main/java/com/yahoo/memory4/Util.java
// public static final String characterPad(final String s, final int fieldLength, final char padChar,
//     final boolean postpend) {
//   final char[] chArr = s.toCharArray();
//   final int sLen = chArr.length;
//   if (sLen < fieldLength) {
//     final char[] out = new char[fieldLength];
//     final int blanks = fieldLength - sLen;
// 
//     if (postpend) {
//       for (int i = 0; i < sLen; i++) {
//         out[i] = chArr[i];
//       }
//       for (int i = sLen; i < fieldLength; i++) {
//         out[i] = padChar;
//       }
//     } else { //prepend
//       for (int i = 0; i < blanks; i++) {
//         out[i] = padChar;
//       }
//       for (int i = blanks; i < fieldLength; i++) {
//         out[i] = chArr[i - blanks];
//       }
//     }
// 
//     return String.valueOf(out);
//   }
//   return s;
// }
// 
// Path: src/main/java/com/yahoo/memory4/Util.java
// public static WritableMemory memoryRequestHandler(final WritableMemory origMem,
//     final long newCapacityBytes, final boolean copy) {
//   final MemoryRequest memReq = origMem.getMemoryRequest();
//   if (memReq == null) {
//     throw new IllegalArgumentException(
//         "Insufficient space. MemoryRequest callback cannot be null.");
//   }
//   final WritableMemory newDstMem = (copy)
//       ? memReq.request(origMem, origMem.getCapacity(), newCapacityBytes)
//       : memReq.request(newCapacityBytes);
// 
//   if (newDstMem == null) {
//     throw new IllegalArgumentException(
//         "Insufficient space and Memory returned by MemoryRequest cannot be null.");
//   }
//   final long newCap = newDstMem.getCapacity(); //may be more than requested, but not less.
//   if (newCap < newCapacityBytes) {
//     memReq.closeRequest(newDstMem);
//     throw new IllegalArgumentException(
//         "Insufficient space. Memory returned by MemoryRequest is not the requested capacity: "
//         + "Returned: " + newCap + " < Requested: " + newCapacityBytes);
//   }
// 
//   if (copy) { //copy and request free.
//     origMem.copyTo(0, newDstMem, 0, Math.min(origMem.getCapacity(), newCap));
//     memReq.closeRequest(origMem, newDstMem);
//   }
//   return newDstMem;
// }
// 
// Path: src/main/java/com/yahoo/memory4/Util.java
// public static final String zeroPad(final String s, final int fieldLength) {
//   return characterPad(s, fieldLength, '0', false);
// }


// Path: src/test/java/com/yahoo/memory4/UtilTest.java
import static com.yahoo.memory4.Util.characterPad;
import static com.yahoo.memory4.Util.memoryRequestHandler;
import static com.yahoo.memory4.Util.zeroPad;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

    @Override //not used
    public void closeRequest(WritableMemory memToFree, WritableMemory newMem) { }
  } //end class BadMemoryManager1

  //////////////////////////////////////////////////////
  //////////////////////////////////////////////////////

  //Binary Search
  @Test
  public void checkBinarySearch() {
    int k = 1024; //longs
    WritableMemory wMem = WritableMemory.allocate(k << 3); //1024 longs
    for (int i = 0; i < k; i++) { wMem.putLong(i << 3, i); }
    long idx = Util.binarySearchLongs(wMem, 0, k - 1, k / 2);
    long val = wMem.getLong(idx << 3);
    assertEquals(idx, k/2);
    assertEquals(val, k/2);

    idx = Util.binarySearchLongs(wMem, 0, k - 1, k);
    assertEquals(idx, -1024);
  }

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void checkBoundsTest() {
    UnsafeUtil.checkBounds(999, 2, 1000);
  }

  @Test
  public void checkPadding() {
    String s = "123";
    String t = zeroPad(s, 4);