<commit_before>// Copyright 2020 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "iree/base/internal/file_io.h"

#include "iree/base/config.h"

#if IREE_FILE_IO_ENABLE

#include <cstdlib>
#include <cstring>
#include <ostream>
#include <string>
#include <type_traits>
#include <utility>

#include "iree/base/status_cc.h"
#include "iree/testing/gtest.h"
#include "iree/testing/status_matchers.h"

namespace iree {
namespace file_io {
namespace {

using ::iree::testing::status::StatusIs;

std::string GetUniquePath(const char* unique_name) {
  char* test_tmpdir = getenv("TEST_TMPDIR");
  if (!test_tmpdir) {
    test_tmpdir = getenv("TMPDIR");
  }
  if (!test_tmpdir) {
    test_tmpdir = getenv("TEMP");
  }
  if (!test_tmpdir) {
    std::cerr << "TEST_TMPDIR/TMPDIR/TEMP not defined\n";
    exit(1);
  }
  return test_tmpdir + std::string("/iree_test_") + unique_name;
}

std::string GetUniqueContents(const char* unique_name) {
  return std::string("Test with name ") + unique_name + "\n";
}

TEST(FileIO, ReadWriteContents) {
  constexpr const char* kUniqueName = "ReadWriteContents";
  auto path = GetUniquePath(kUniqueName);

  // File must not exist.
  iree_status_t status = iree_file_exists(path.c_str());
  IREE_EXPECT_STATUS_IS(IREE_STATUS_NOT_FOUND, status);
  iree_status_free(status);
<commit_msg>file_io_test was noisy when run on multiple parallel processes (#10122)<commit_after>// Copyright 2020 The IREE Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "iree/base/internal/file_io.h"

#include "iree/base/config.h"

#if IREE_FILE_IO_ENABLE

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ostream>
#include <random>
#include <string>
#include <type_traits>
#include <utility>

#include "iree/base/status_cc.h"
#include "iree/testing/gtest.h"
#include "iree/testing/status_matchers.h"

namespace iree {
namespace file_io {
namespace {

using ::iree::testing::status::StatusIs;

std::uint64_t GetTrueRandomUint64() {
  std::random_device d;
  return (static_cast<std::uint64_t>(d()) << 32) | d();
}

std::string GetUniquePath(const char* unique_name) {
  char* test_tmpdir = getenv("TEST_TMPDIR");
  if (!test_tmpdir) {
    test_tmpdir = getenv("TMPDIR");
  }
  if (!test_tmpdir) {
    test_tmpdir = getenv("TEMP");
  }
  if (!test_tmpdir) {
    std::cerr << "TEST_TMPDIR/TMPDIR/TEMP not defined\n";
    exit(1);
  }
  // This test might be running on multiple parallel processes, for example with
  //   ctest --repeat-until-fail 10 -j 10
  // It's hard to make this test completely race-free in that two processes
  // could generate the same "unique path" concurrently, and a serious attempt
  // will likely compromise on portability and/or complexity. Since this is only
  // test code and we only care to avoid intermittent test failures, let's try
  // just a random 64bit value.
  char unique_path[256];
  std::uint64_t random = GetTrueRandomUint64();
  snprintf(unique_path, sizeof unique_path, "%s/iree_test_%" PRIx64 "_%s",
           test_tmpdir, random, unique_name);
  return unique_path;
}

std::string GetUniqueContents(const char* unique_name) {
  return std::string("Test with name ") + unique_name + "\n";
}

TEST(FileIO, ReadWriteContents) {
  constexpr const char* kUniqueName = "ReadWriteContents";
  auto path = GetUniquePath(kUniqueName);

  // File must not exist.
  iree_status_t status = iree_file_exists(path.c_str());
  IREE_EXPECT_STATUS_IS(IREE_STATUS_NOT_FOUND, status);
  iree_status_free(status);

{'fraction_non_alphanumeric': 0.12833377343543703, 'fraction_numerical': 0.012146818061790335, 'mean_word_length': 4.6706586826347305, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 18, 'https://': 2, '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': '4815272', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1314, 'n_words': 324}