All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.util.BytesJUnitTest Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.internal.util;

import java.nio.ByteBuffer;

import junit.framework.TestCase;

public class BytesJUnitTest extends TestCase {
  private ByteBuffer buf = ByteBuffer.allocate(8);
  
  public void testShort() {
    short[] val = { 666, -1, Short.MIN_VALUE, 0, 12, Short.MAX_VALUE };
    for (int i = 0; i < val.length; i++) {
      buf.putShort(val[i]).flip();
      assertEquals(val[i], Bytes.toShort(buf.get(), buf.get()));
      
      buf.rewind();
    }
  }
  
  public void testChar() {
    char[] val = { 'a', 'b', 'c' };
    for (int i = 0; i < val.length; i++) {
      buf.putChar(val[i]).flip();
      assertEquals(val[i], Bytes.toChar(buf.get(), buf.get()));
      
      buf.rewind();
    }
  }
  
  public void testUnsignedShort() {
    int[] val = { 0, 1, Short.MAX_VALUE + 1, 2 * Short.MAX_VALUE };
    for (int i = 0; i < val.length; i++) {
      buf.put(Bytes.int2(val[i])).put(Bytes.int3(val[i])).flip();
      assertEquals(val[i], Bytes.toUnsignedShort(buf.get(), buf.get()));
      
      buf.rewind();
    }
  }
  
  public void testInt() {
    int[] val = { 666, -1, Integer.MIN_VALUE, 0, 1, Integer.MAX_VALUE };
    for (int i = 0; i < val.length; i++) {
      buf.putInt(val[i]).flip();
      assertEquals(val[i], Bytes.toInt(buf.get(), buf.get(), buf.get(), buf.get()));
      
      buf.rewind();
      
      byte[] bytes = new byte[4];
      Bytes.putInt(val[i], bytes, 0);
      assertEquals(val[i], Bytes.toInt(bytes[0], bytes[1], bytes[2], bytes[3]));
    }
  }
  
  public void testLong() {
    long[] val = { 666, -1, Long.MIN_VALUE, 0, 1, Long.MAX_VALUE };
    for (int i = 0; i < val.length; i++) {
      buf.putLong(val[i]).flip();
      assertEquals(val[i], Bytes.toLong(buf.get(), buf.get(), buf.get(), buf.get(),
          buf.get(), buf.get(), buf.get(), buf.get()));
      
      buf.rewind();
    }
  }
  
  
  public void testVarint() {
    ByteBuffer buf = ByteBuffer.allocate(5);
    checkVarint(0, buf);
    
    // 1 byte
    checkVarint(1, buf);
    checkVarint(0x7f, buf);
    
    // 2 byte
    checkVarint(0x80, buf);
    checkVarint(0x7fff, buf);
    
    // 3 byte
    checkVarint(0x8000, buf);
    checkVarint(0x7fffff, buf);
    
    // 4 byte
    checkVarint(0x800000, buf);
    checkVarint(0x7fffffff, buf);
  }
  
  private void checkVarint(int v, ByteBuffer buf) {
    Bytes.putVarInt(v, buf);
    buf.rewind();
    
    int v2 = Bytes.getVarInt(buf);
    assertEquals(v, v2);
    buf.clear();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy