<commit_before>/*
 * Copyright 2015-16, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */
package com.yahoo.sketches.memory;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;
import com.yahoo.sketches.memory.NativeMemory;
import com.yahoo.sketches.memory.AllocMemory;
import static com.yahoo.sketches.hash.MurmurHash3Adaptor.*;

public class AllocMemoryTest {

  @Test
  public void checkReallocateNativeMemory() {
    int longs = 1024;
    int bytes = longs << 3;
    NativeMemory mem1 = new AllocMemory(bytes);
    for (int i = 0; i<longs; i++) {
      mem1.putLong(i<<3, hashToLongs(i, 0)[0]);
    }
    long mem1add = mem1.getAddress(0);
    println("Add: "+mem1add+", Cap: "+mem1.getCapacity());
    
    //reallocate at twice the size.
    NativeMemory mem2 = new AllocMemory(mem1, 2L * bytes, null);
    long mem2add = mem2.getAddress(0);
    boolean equal = (mem1add == mem2add);
    String eqStr = (equal)? ", EQUAL, " : ", NOT EQUAL, ";
    println("Add: "+mem2add+eqStr+"Cap: "+mem2.getCapacity());
    
    //Check the new mem up to original size
    for (int i = 0; i<longs; i++) {
      assertEquals(mem2.getLong(i<<3), hashToLongs(i, 0)[0]);
    }
    mem1.freeMemory();
    mem2.freeMemory();
  }
  
  @Test
  public void checkAllocateCopyClear() {
    int longs = 16;
    int bytes = longs << 3;
    NativeMemory mem1 = new AllocMemory(bytes);
    for (int i = 0; i<longs; i++) {
      mem1.putLong(i<<3, hashToLongs(i, 0)[0]);
    }
    long mem1add = mem1.getAddress(0);
    println("Add: "+mem1add+", Cap: "+mem1.getCapacity());
    
    //new allocate at twice the size.
    NativeMemory mem2 = new AllocMemory(mem1, bytes, 2*bytes, null);
    long mem2add = mem2.getAddress(0);
    println("Add: "+mem2add+", Cap: "+mem2.getCapacity());
    
    //Check the new mem up to copy size
    for (int i = 0; i<longs; i++) {
      assertEquals(mem2.getLong(i<<3), hashToLongs(i, 0)[0]);
    }
    mem1.freeMemory();
    mem2.freeMemory();
  }
  
  
  @Test
  public void printlnTest() {
    //println("PRINTING: "+this.getClass().getName());
  }
  
  /**
   * @param s value to print 
   */
  static void println(String s) {<commit_msg>Removed Memory test package dependency on MurmurHash3<commit_after>/*
 * Copyright 2015-16, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */
package com.yahoo.sketches.memory;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public class AllocMemoryTest {

  @Test
  public void checkReallocateNativeMemory() {
    int longs = 1024;
    int bytes = longs << 3;
    NativeMemory mem1 = new AllocMemory(bytes);
    for (int i = 0; i<longs; i++) {
      mem1.putLong(i << 3, i);
    }
    long mem1add = mem1.getAddress(0);
    println("Add: " + mem1add + ", Cap: " + mem1.getCapacity());
    
    //reallocate at twice the size.
    NativeMemory mem2 = new AllocMemory(mem1, 2L * bytes, null);
    long mem2add = mem2.getAddress(0);
    boolean equal = (mem1add == mem2add);
    String eqStr = (equal)? ", EQUAL, " : ", NOT EQUAL, ";
    println("Add: " + mem2add + eqStr + "Cap: " + mem2.getCapacity());
    
    //Check the new mem up to original size
    for (int i = 0; i<longs; i++) {
      assertEquals(mem2.getLong(i << 3), i);
    }
    mem1.freeMemory();
    mem2.freeMemory();
  }
  
  @Test
  public void checkAllocateCopyClear() {
    int longs = 16;
    int bytes = longs << 3;
    NativeMemory mem1 = new AllocMemory(bytes);
    for (int i = 0; i<longs; i++) {
      mem1.putLong(i << 3, i);
    }
    long mem1add = mem1.getAddress(0);
    println("Add: " + mem1add + ", Cap: " + mem1.getCapacity());
    
    //new allocate at twice the size.
    NativeMemory mem2 = new AllocMemory(mem1, bytes, 2*bytes, null);
    long mem2add = mem2.getAddress(0);
    println("Add: " + mem2add + ", Cap: " + mem2.getCapacity());
    
    //Check the new mem up to copy size
    for (int i = 0; i<longs; i++) {
      assertEquals(mem2.getLong(i << 3), i);
    }
    mem1.freeMemory();
    mem2.freeMemory();
  }
  
  
  @Test
  public void printlnTest() {
    println("PRINTING: "+this.getClass().getName());
  }
  
  /**
   * @param s value to print 
   */
  static void println(String s) {
{'fraction_non_alphanumeric': 0.1487014479430016, 'fraction_numerical': 0.030797517811997244, 'mean_word_length': 2.945602901178604, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4370381', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1517, 'n_words': 407}