<commit_before>// Copyright (c) 2013, <NAME>,  Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
//
//     mFAST is free software: you can redistribute it and/or modify
//     it under the terms of the GNU Lesser General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
//
//     mFAST is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General Public License
//     along with mFast.  If not, see <http://www.gnu.org/licenses/>.
//
#include <mfast/coder/encoder/fast_ostream.h>
#include <mfast/coder/encoder/fast_ostream_inserter.h>
#include <mfast/coder/encoder/encoder_presence_map.h>
#include <mfast/output.h>
#define BOOST_TEST_DYN_LINK
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>

#include "debug_allocator.h"
#include <stdexcept>
#include "byte_stream.h"

using namespace mfast;

template <typename T>
boost::test_tools::predicate_result
encode_integer(T value, bool nullable, const byte_stream& result)
{
  char buffer[16];
  debug_allocator alloc;
  fast_ostreambuf sb(buffer);
  fast_ostream strm(&alloc);
  strm.rdbuf(&sb);

  strm.encode(value, nullable);

  if (byte_stream(sb) == result)
    return true;

  boost::test_tools::predicate_result res( false );
  res.message() << "Got \"" << byte_stream(sb) << "\" instead.";
  return res;

}

BOOST_AUTO_TEST_SUITE( test_fast_ostream )


BOOST_AUTO_TEST_CASE(int_test)
{

  BOOST_CHECK(encode_integer(INT32_C(942755), true, "\x39\x45\xa4" ));
  BOOST_CHECK(encode_integer(INT32_C(942755), false, "\x39\x45\xa3"));
  BOOST_CHECK(encode_integer(INT32_C(-942755), true, "\x46\x3a\xdd"));
  BOOST_CHECK(encode_integer(INT32_C(-7942755), false, "\x7c\x1b\x1b\x9d"));
  BOOST_CHECK(encode_integer(INT32_C(8193), false, "\x00\x40\x81"));
  BOOST_CHECK(encode_integer(INT32_C(-8193), false, "\x7F\x3f\xff"));

  BOOST_CHECK(encode_integer(UINT32_C(0), true, "\x81"));
  BOOST_CHECK(encode_integer(UINT32_C(1), true, "\x82"));
  BOOST_CHECK(encode_integer(UINT32_C(942755), true, "\x39\x45\xa4"));
  BOOST_CHECK(encode_integer(UINT32_C(0), false, "\x80"));
  BOOST_CHECK(encode_integer(UINT32_C(1), false, "\x81"));
  BOOST_CHECK(encode_integer(UINT32_C(942755), false, "\x39\x45\xa3"));
  BOOST_CHECK(encode_integer(std::numeric_limits<uint32_t>::max(), true,"\x10\x00\x00\x00\x80"));

  BOOST_CHECK(encode_integer(std::numeric_limits<int64_t>::max(), true, "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x80"));
  BOOST_CHECK(encode_integer(std::numeric_limits<uint64_t>::max(), true, "\x02\x00\x00\x00\x00\x00\x00\x00\x00\x80"));
}


boost::test_tools::predicate_result
encode_string(const char* str,std::size_t len, bool nullable, const byte_stream& result)
{
  char buffer[16];<commit_msg>Added tests for encode small integers.<commit_after>// Copyright (c) 2013, <NAME>,  Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
//
//     mFAST is free software: you can redistribute it and/or modify
//     it under the terms of the GNU Lesser General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
//
//     mFAST is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General Public License
//     along with mFast.  If not, see <http://www.gnu.org/licenses/>.
//
#include <mfast/coder/encoder/fast_ostream.h>
#include <mfast/coder/encoder/fast_ostream_inserter.h>
#include <mfast/coder/encoder/encoder_presence_map.h>
#include <mfast/output.h>
#define BOOST_TEST_DYN_LINK
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>

#include "debug_allocator.h"
#include <stdexcept>
#include "byte_stream.h"

using namespace mfast;

template <typename T>
boost::test_tools::predicate_result
encode_integer(T value, bool nullable, const byte_stream& result)
{
  char buffer[16];
  debug_allocator alloc;
  fast_ostreambuf sb(buffer);
  fast_ostream strm(&alloc);
  strm.rdbuf(&sb);

  strm.encode(value, nullable);

  if (byte_stream(sb) == result)
    return true;

  boost::test_tools::predicate_result res( false );
  res.message() << "Got \"" << byte_stream(sb) << "\" instead.";
  return res;

}

BOOST_AUTO_TEST_SUITE( test_fast_ostream )


BOOST_AUTO_TEST_CASE(int_test)
{

  BOOST_CHECK(encode_integer<int8_t>(2, false, "\x82") );
  BOOST_CHECK(encode_integer<int8_t>(2, true, "\x83") );
  BOOST_CHECK(encode_integer<int8_t>(-2, false, "\xFE") );
  BOOST_CHECK(encode_integer<int8_t>(-2, true, "\xFE") );

  BOOST_CHECK(encode_integer(INT32_C(942755), true, "\x39\x45\xa4" ));
  BOOST_CHECK(encode_integer(INT32_C(942755), false, "\x39\x45\xa3"));
  BOOST_CHECK(encode_integer(INT32_C(-942755), true, "\x46\x3a\xdd"));
  BOOST_CHECK(encode_integer(INT32_C(-7942755), false, "\x7c\x1b\x1b\x9d"));
  BOOST_CHECK(encode_integer(INT32_C(8193), false, "\x00\x40\x81"));
  BOOST_CHECK(encode_integer(INT32_C(-8193), false, "\x7F\x3f\xff"));

  BOOST_CHECK(encode_integer(UINT32_C(0), true, "\x81"));
  BOOST_CHECK(encode_integer(UINT32_C(1), true, "\x82"));
  BOOST_CHECK(encode_integer(UINT32_C(942755), true, "\x39\x45\xa4"));
  BOOST_CHECK(encode_integer(UINT32_C(0), false, "\x80"));
  BOOST_CHECK(encode_integer(UINT32_C(1), false, "\x81"));
  BOOST_CHECK(encode_integer(UINT32_C(942755), false, "\x39\x45\xa3"));
  BOOST_CHECK(encode_integer(std::numeric_limits<uint32_t>::max(), true,"\x10\x00\x00\x00\x80"));

  BOOST_CHECK(encode_integer(std::numeric_limits<int64_t>::max(), true, "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x80"));
  BOOST_CHECK(encode_integer(std::numeric_limits<uint64_t>::max(), true, "\x02\x00\x00\x00\x00\x00\x00\x00\x00\x80"));
}


boost::test_tools::predicate_result
encode_string(const char* str,std::size_t len, bool nullable, const byte_stream& result)
{
  char buffer[16];
{'fraction_non_alphanumeric': 0.1516057934508816, 'fraction_numerical': 0.05887909319899244, 'mean_word_length': 4.970864661654136, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 33, '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': '490150', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2325, 'n_words': 482}