<commit_before>/*
 * Copyright 2016 Facebook, Inc.
 *
 * 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/Conv.h>
#include <folly/Foreach.h>
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>
#include <limits>
#include <stdexcept>

using namespace std;
using namespace folly;


TEST(Conv, digits10) {
  char buffer[100];
  uint64_t power;

  // first, some basic sniff tests
  EXPECT_EQ( 1, digits10(0));
  EXPECT_EQ( 1, digits10(1));
  EXPECT_EQ( 1, digits10(9));
  EXPECT_EQ( 2, digits10(10));
  EXPECT_EQ( 2, digits10(99));
  EXPECT_EQ( 3, digits10(100));
  EXPECT_EQ( 3, digits10(999));
  EXPECT_EQ( 4, digits10(1000));
  EXPECT_EQ( 4, digits10(9999));
  EXPECT_EQ(20, digits10(18446744073709551615ULL));

  // try the first X nonnegatives.
  // Covers some more cases of 2^p, 10^p
  for (uint64_t i = 0; i < 100000; i++) {
    snprintf(buffer, sizeof(buffer), "%lu", i);
    EXPECT_EQ(strlen(buffer), digits10(i));
  }

  // try powers of 2
  power = 1;
  for (int p = 0; p < 64; p++) {
    snprintf(buffer, sizeof(buffer), "%lu", power);
    EXPECT_EQ(strlen(buffer), digits10(power));
    snprintf(buffer, sizeof(buffer), "%lu", power - 1);
    EXPECT_EQ(strlen(buffer), digits10(power - 1));
    snprintf(buffer, sizeof(buffer), "%lu", power + 1);
    EXPECT_EQ(strlen(buffer), digits10(power + 1));
    power *= 2;
  }

  // try powers of 10
  power = 1;
  for (int p = 0; p < 20; p++) {
    snprintf(buffer, sizeof(buffer), "%lu", power);
    EXPECT_EQ(strlen(buffer), digits10(power));
    snprintf(buffer, sizeof(buffer), "%lu", power - 1);
    EXPECT_EQ(strlen(buffer), digits10(power - 1));
    snprintf(buffer, sizeof(buffer), "%lu", power + 1);
    EXPECT_EQ(strlen(buffer), digits10(power + 1));
    power *= 10;
  }
}

// Test to<T>(T)
TEST(Conv, Type2Type) {
  bool boolV = true;
  EXPECT_EQ(to<bool>(boolV), true);

  int intV = 42;
  EXPECT_EQ(to<int>(intV), 42);

  float floatV = 4.2;
  EXPECT_EQ(to<float>(floatV), 4.2f);

  double doubleV = 0.42;
  EXPECT_EQ(to<double>(doubleV), 0.42);<commit_msg>Fix printing of uint64_t in ConvTest<commit_after>/*
 * Copyright 2016 Facebook, Inc.
 *
 * 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/Conv.h>
#include <folly/Foreach.h>
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>
#include <limits>
#include <stdexcept>

using namespace std;
using namespace folly;


TEST(Conv, digits10) {
  char buffer[100];
  uint64_t power;

  // first, some basic sniff tests
  EXPECT_EQ( 1, digits10(0));
  EXPECT_EQ( 1, digits10(1));
  EXPECT_EQ( 1, digits10(9));
  EXPECT_EQ( 2, digits10(10));
  EXPECT_EQ( 2, digits10(99));
  EXPECT_EQ( 3, digits10(100));
  EXPECT_EQ( 3, digits10(999));
  EXPECT_EQ( 4, digits10(1000));
  EXPECT_EQ( 4, digits10(9999));
  EXPECT_EQ(20, digits10(18446744073709551615ULL));

  // try the first X nonnegatives.
  // Covers some more cases of 2^p, 10^p
  for (uint64_t i = 0; i < 100000; i++) {
    snprintf(buffer, sizeof(buffer), "%" PRIu64, i);
    EXPECT_EQ(strlen(buffer), digits10(i));
  }

  // try powers of 2
  power = 1;
  for (int p = 0; p < 64; p++) {
    snprintf(buffer, sizeof(buffer), "%" PRIu64, power);
    EXPECT_EQ(strlen(buffer), digits10(power));
    snprintf(buffer, sizeof(buffer), "%" PRIu64, power - 1);
    EXPECT_EQ(strlen(buffer), digits10(power - 1));
    snprintf(buffer, sizeof(buffer), "%" PRIu64, power + 1);
    EXPECT_EQ(strlen(buffer), digits10(power + 1));
    power *= 2;
  }

  // try powers of 10
  power = 1;
  for (int p = 0; p < 20; p++) {
    snprintf(buffer, sizeof(buffer), "%" PRIu64, power);
    EXPECT_EQ(strlen(buffer), digits10(power));
    snprintf(buffer, sizeof(buffer), "%" PRIu64, power - 1);
    EXPECT_EQ(strlen(buffer), digits10(power - 1));
    snprintf(buffer, sizeof(buffer), "%" PRIu64, power + 1);
    EXPECT_EQ(strlen(buffer), digits10(power + 1));
    power *= 10;
  }
}

// Test to<T>(T)
TEST(Conv, Type2Type) {
  bool boolV = true;
  EXPECT_EQ(to<bool>(boolV), true);

  int intV = 42;
  EXPECT_EQ(to<int>(intV), 42);

  float floatV = 4.2;
  EXPECT_EQ(to<float>(floatV), 4.2f);

  double doubleV = 0.42;
  EXPECT_EQ(to<double>(doubleV), 0.42);
{'fraction_non_alphanumeric': 0.1522075910147173, 'fraction_numerical': 0.061192873741285826, 'mean_word_length': 3.9425837320574164, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2190720', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1849, 'n_words': 532}