<commit_before>/*
 * Copyright 2017, Yahoo! Inc. Licensed under the terms of the
 * Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.memory;

import static com.yahoo.memory.AllocateDirectMap.isFileReadOnly;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;

import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

public class AllocateDirectWritableMapMemoryTest {

  //@Test  //TODO
  public void simpleMap() throws Exception {
    File file =
        new File(getClass().getClassLoader().getResource("GettysburgAddress.txt").getFile());
    try (MapHandle h = Memory.map(file); WritableMapHandle wh = (WritableMapHandle) h) {
      Memory mem = h.get();
      byte[] bytes = new byte[(int)mem.getCapacity()];
      mem.getByteArray(0, bytes, 0, bytes.length);
      String text = new String(bytes, UTF_8);
      println(text);
      try {
        wh.force();
        fail();
      } catch (ReadOnlyException e) {
        //OK
      }
    }
  }

  @Test
  public void copyOffHeapToMemoryMappedFile() throws Exception {
    long bytes = 1L << 10; //small for unit tests.  Make it larger than 2GB if you like.
    long longs = bytes >>> 3;

    File file = new File("TestFile.bin");
    if (file.exists()) {
      try {
        java.nio.file.Files.delete(file.toPath());<commit_msg>Troubleshooting<commit_after>/*
 * Copyright 2017, Yahoo! Inc. Licensed under the terms of the
 * Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.memory;

import static com.yahoo.memory.AllocateDirectMap.isFileReadOnly;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermissions;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class AllocateDirectWritableMapMemoryTest {

  @BeforeClass
  public void setReadOnly() {
    File file =
        new File(getClass().getClassLoader().getResource("GettysburgAddress.txt").getFile());
    try {
    Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("rw-r--r--"));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  //@Test  //TODO
  public void simpleMap() throws Exception {
    File file =
        new File(getClass().getClassLoader().getResource("GettysburgAddress.txt").getFile());
    try (MapHandle h = Memory.map(file); WritableMapHandle wh = (WritableMapHandle) h) {
      Memory mem = h.get();
      byte[] bytes = new byte[(int)mem.getCapacity()];
      mem.getByteArray(0, bytes, 0, bytes.length);
      String text = new String(bytes, UTF_8);
      println(text);
      try {
        wh.force();
        fail();
      } catch (ReadOnlyException e) {
        //OK
      }
    }
  }

  @Test
  public void copyOffHeapToMemoryMappedFile() throws Exception {
    long bytes = 1L << 10; //small for unit tests.  Make it larger than 2GB if you like.
    long longs = bytes >>> 3;

    File file = new File("TestFile.bin");
    if (file.exists()) {
      try {
        java.nio.file.Files.delete(file.toPath());
{'fraction_non_alphanumeric': 0.11871545143458805, 'fraction_numerical': 0.007896814951302974, 'mean_word_length': 3.491725768321513, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1374723', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1147, 'n_words': 294}