<commit_before>/* value_function_test.cpp                                 -*- C++ -*-
   <NAME> (<EMAIL>), 27 Mar 2014
   FreeBSD-style copyright and disclaimer apply

   Tests for ValueFunction.
*/

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#define REFLECT_USE_EXCEPTIONS 1

#include "reflect.h"
#include "types/primitives.h"

#include <boost/test/unit_test.hpp>

using namespace std;
using namespace reflect;


/******************************************************************************/
/* FUNCTION                                                                   */
/******************************************************************************/

void foo(unsigned& value, int other)
{
    value += other;
}

BOOST_AUTO_TEST_CASE(fn)
{
    auto valueFn = makeValueFunction(&foo);

    unsigned value = 0;

    Value ret = valueFn(Value(value), Value(10));
    BOOST_CHECK(ret.isVoid());
    BOOST_CHECK_EQUAL(value, 10);

    valueFn(Value(value), Value(10));
    BOOST_CHECK_EQUAL(value, 20);
}


/******************************************************************************/
/* LAMBDA                                                                     */
/******************************************************************************/

BOOST_AUTO_TEST_CASE(lambda)
{
    unsigned value = 0;
    auto lambda = [&](unsigned x, unsigned&& z) {
        return value = x * x + std::move(z);
    };

    auto valueFn = makeValueFunction(lambda);

    Value ret = valueFn(Value(10u), Value(10u));
    BOOST_CHECK_EQUAL(value, 110);
    BOOST_CHECK_EQUAL(ret.get<unsigned>(), 110);
}


/******************************************************************************/
/* FUNCTOR                                                                    */
/******************************************************************************/

struct Functor
{
    Functor() : value(0) {}

    unsigned& operator() (unsigned i)
    {
        value += i;
        return value;
    }

private:
    unsigned value;
};


BOOST_AUTO_TEST_CASE(functor)
{
    auto valueFn = makeValueFunction(Functor());

    {
        Value ret = valueFn(Value(10u));
        BOOST_CHECK_EQUAL(ret.refType(), RefType::LValue);
        BOOST_CHECK(ret.castable<unsigned>());
        BOOST_CHECK_EQUAL(ret.cast<unsigned>(), 10);
    }

    {
        Value ret = valueFn(Value(10u));
        BOOST_CHECK_EQUAL(ret.cast<unsigned>(), 20);
    }
}


/******************************************************************************/
/* MEMBER FUNCTION                                                            */
/******************************************************************************/

struct Foo
{
    Foo() : value(0) {}

    unsigned& bar(unsigned i)
    {
        value += i;
        return value;
    }

    unsigned value;
};

reflectClass(Foo) {}

BOOST_AUTO_TEST_CASE(memberFn)
{
    Foo foo;
    auto valueFn = makeValueFunction(&Foo::bar);

    {
        Value ret = valueFn(Value(foo), Value(10u));
        BOOST_CHECK_EQUAL(ret.refType(), RefType::LValue);
        BOOST_CHECK(ret.castable<unsigned>());
        BOOST_CHECK_EQUAL(ret.cast<unsigned>(), 10);
        BOOST_CHECK_EQUAL(foo.value, 10);
    }

    {
        Value ret = valueFn(Value(foo), Value(10u));
        BOOST_CHECK_EQUAL(ret.cast<unsigned>(), 20);
        BOOST_CHECK_EQUAL(foo.value, 20);
    }<commit_msg>Compile fix for the ValueFunction tests.<commit_after>/* value_function_test.cpp                                 -*- C++ -*-
   <NAME> (<EMAIL>), 27 Mar 2014
   FreeBSD-style copyright and disclaimer apply

   Tests for ValueFunction.
*/

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#define REFLECT_USE_EXCEPTIONS 1

#include "reflect.h"
#include "types/primitives.h"

#include <boost/test/unit_test.hpp>

using namespace std;
using namespace reflect;


/******************************************************************************/
/* FUNCTION                                                                   */
/******************************************************************************/

void foo(unsigned& value, int other)
{
    value += other;
}

BOOST_AUTO_TEST_CASE(fn)
{
    auto valueFn = makeValueFunction(&foo);

    unsigned value = 0;

    Value ret = valueFn(Value(value), Value(10));
    BOOST_CHECK(ret.isVoid());
    BOOST_CHECK_EQUAL(value, 10);

    valueFn(Value(value), Value(10));
    BOOST_CHECK_EQUAL(value, 20);
}


/******************************************************************************/
/* LAMBDA                                                                     */
/******************************************************************************/

BOOST_AUTO_TEST_CASE(lambda)
{
    unsigned value = 0;
    auto lambda = [&](unsigned x, unsigned&& z) {
        return value = x * x + std::move(z);
    };

    auto valueFn = makeValueFunction(lambda);

    Value ret = valueFn(Value(10u), Value(10u));
    BOOST_CHECK_EQUAL(value, 110);
    BOOST_CHECK_EQUAL(ret.get<unsigned>(), 110);
}


/******************************************************************************/
/* FUNCTOR                                                                    */
/******************************************************************************/

struct Functor
{
    Functor() : value(0) {}

    unsigned& operator() (unsigned i)
    {
        value += i;
        return value;
    }

private:
    unsigned value;
};


BOOST_AUTO_TEST_CASE(functor)
{
    auto valueFn = makeValueFunction(Functor());

    {
        Value ret = valueFn(Value(10u));
        BOOST_CHECK_EQUAL(ret.refType(), RefType::LValue);
        BOOST_CHECK(ret.isCastable<unsigned>());
        BOOST_CHECK_EQUAL(ret.cast<unsigned>(), 10);
    }

    {
        Value ret = valueFn(Value(10u));
        BOOST_CHECK_EQUAL(ret.cast<unsigned>(), 20);
    }
}


/******************************************************************************/
/* MEMBER FUNCTION                                                            */
/******************************************************************************/

struct Foo
{
    Foo() : value(0) {}

    unsigned& bar(unsigned i)
    {
        value += i;
        return value;
    }

    unsigned value;
};

reflectClass(Foo) {}

BOOST_AUTO_TEST_CASE(memberFn)
{
    Foo foo;
    auto valueFn = makeValueFunction(&Foo::bar);

    {
        Value ret = valueFn(Value(foo), Value(10u));
        BOOST_CHECK_EQUAL(ret.refType(), RefType::LValue);
        BOOST_CHECK(ret.isCastable<unsigned>());
        BOOST_CHECK_EQUAL(ret.cast<unsigned>(), 10);
        BOOST_CHECK_EQUAL(foo.value, 10);
    }

    {
        Value ret = valueFn(Value(foo), Value(10u));
        BOOST_CHECK_EQUAL(ret.cast<unsigned>(), 20);
        BOOST_CHECK_EQUAL(foo.value, 20);
    }
{'fraction_non_alphanumeric': 0.29231223255127636, 'fraction_numerical': 0.01446067581525749, 'mean_word_length': 2.693732970027248, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3687410', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1739, 'n_words': 329}