<commit_before>/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * 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.
 */

#include <folly/experimental/crypto/LtHash.h>

#include <algorithm>
#include <memory>
#include <random>
#include <string>
#include <vector>

#include <sodium.h>

#include <folly/Random.h>
#include <folly/String.h>
#include <folly/io/IOBuf.h>
#include <folly/portability/GTest.h>

using namespace ::testing;

using namespace folly::crypto;

namespace {
std::string toHex(const folly::IOBuf& buf) {
  return folly::hexlify({buf.data(), buf.length()});
}

std::unique_ptr<folly::IOBuf> makeRandomData(size_t length) {
  auto data = folly::IOBuf::create(static_cast<uint64_t>(length));
  data->append(length);
  randombytes_buf(data->writableData(), data->length());
  return data;
}

template <typename T>
struct IsLtHash {
  static inline constexpr bool value() { return false; }
};

template <std::size_t B, std::size_t N>
struct IsLtHash<LtHash<B, N>> {
  static inline constexpr bool value() { return true; }
};

} // namespace

// Needed so an EXPECT_EQ() or EXPECT_NE() failure prints the LtHash checksum.
template <std::size_t B, std::size_t N>
static std::ostream& operator<<(std::ostream& os, const LtHash<B, N>& h) {
  os << toHex(*h.getChecksum());
  return os;
}

// Note: the template parameter H must be an instance of LtHash<B, N> for some
// valid B and N.
template <typename H>
class LtHashTest : public ::testing::Test {
 protected:
  static_assert(
      IsLtHash<H>::value(),
      "template parameter H is not an instance of LtHash");

  std::array<unsigned char, 1> obj1_{{'a'}};
  std::array<unsigned char, 1> obj2_{{'b'}};

  static size_t kChecksumLength() {
    return H::getElementCount() / H::getElementsPerUint64() * sizeof(uint64_t);
  }
<commit_msg>fix fbcode-builder build with googletest-1.11<commit_after>/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * 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.
 */

#include <folly/experimental/crypto/LtHash.h>

#include <algorithm>
#include <memory>
#include <random>
#include <string>
#include <vector>

#include <sodium.h>

#include <folly/Random.h>
#include <folly/String.h>
#include <folly/io/IOBuf.h>
#include <folly/portability/GTest.h>

using namespace ::testing;

using namespace folly::crypto;

namespace {
std::string toHex(const folly::IOBuf& buf) {
  return folly::hexlify({buf.data(), buf.length()});
}

std::unique_ptr<folly::IOBuf> makeRandomData(size_t length) {
  auto data = folly::IOBuf::create(static_cast<uint64_t>(length));
  data->append(length);
  randombytes_buf(data->writableData(), data->length());
  return data;
}

template <typename T>
struct IsLtHash {
  static inline constexpr bool value() { return false; }
};

template <std::size_t B, std::size_t N>
struct IsLtHash<LtHash<B, N>> {
  static inline constexpr bool value() { return true; }
};

} // namespace

namespace folly::crypto {
// Needed so an EXPECT_EQ() or EXPECT_NE() failure prints the LtHash checksum.
template <std::size_t B, std::size_t N>
static std::ostream& operator<<(std::ostream& os, const LtHash<B, N>& h) {
  os << toHex(*h.getChecksum());
  return os;
}
} // namespace folly::crypto

// Note: the template parameter H must be an instance of LtHash<B, N> for some
// valid B and N.
template <typename H>
class LtHashTest : public ::testing::Test {
 protected:
  static_assert(
      IsLtHash<H>::value(),
      "template parameter H is not an instance of LtHash");

  std::array<unsigned char, 1> obj1_{{'a'}};
  std::array<unsigned char, 1> obj2_{{'b'}};

  static size_t kChecksumLength() {
    return H::getElementCount() / H::getElementsPerUint64() * sizeof(uint64_t);
  }

{'fraction_non_alphanumeric': 0.13479091488006792, 'fraction_numerical': 0.006580343876034812, 'mean_word_length': 4.64311377245509, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1075645', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1506, 'n_words': 490}