<commit_before>#pragma once
namespace mtao::geometry {
    template <typename PDerived, typename VDerived, typename BeginIt, typename EndIt>
    typename VDerived::Scalar winding_number(const Eigen::MatrixBase<VDerived>& V, const BeginIt& beginit, const EndIt& endit, const Eigen::MatrixBase<PDerived>& p) {
        using S = typename VDerived::Scalar;
        S value = 0;
        auto it = beginit;
        for(; it != endit; ++it) {
            auto it1 = it;
            it1++;
            if(it1 == endit) { it1 = beginit; }

            auto a = V.col(*it) - p;
            auto b = V.col(*it1) - p;
            S aa = std::atan2(a.y(),a.x());
            S ba = std::atan2(b.y(),b.x());
            S ang = ba - aa;
            if(ang > M_PI) {
                ang -= 2 * M_PI;
            } else if(ang <= -M_PI) {
                ang += 2*M_PI;
            }
            value += ang;

        }
        return value;
    }
    template <typename PDerived, typename VDerived, typename Container>
    typename VDerived::Scalar winding_number(const Eigen::MatrixBase<VDerived>& V, const Container& C, const Eigen::MatrixBase<PDerived>& p) {
        return winding_number(V,C.begin(),C.end(),p);
    }
    template <typename PDerived, typename VDerived>
    typename VDerived::Scalar winding_number(const Eigen::MatrixBase<VDerived>& V, const std::initializer_list<int>& C, const Eigen::MatrixBase<PDerived>& p) {
        return winding_number(V,C.begin(),C.end(),p);
    }
    template <typename PDerived, typename VDerived, typename BeginIt, typename EndIt>
    bool interior_winding_number(const Eigen::MatrixBase<VDerived>& V, const BeginIt& beginit, const EndIt& endit, const Eigen::MatrixBase<PDerived>& p) {
        return winding_number(V,beginit,endit,p) > 1;
    }
    template <typename PDerived, typename VDerived, typename Container>
    bool interior_winding_number(const Eigen::MatrixBase<VDerived>& V, const Container& C, const Eigen::MatrixBase<PDerived>& p) {
        return interior_winding_number(V,C.begin(),C.end(),p);
    }
    template <typename PDerived, typename VDerived>
    bool interior_winding_number(const Eigen::MatrixBase<VDerived>& V, const std::initializer_list<int>& C, const Eigen::MatrixBase<PDerived>& p) {
        return interior_winding_number(V,C.begin(),C.end(),p);
    }
}
<commit_msg>winding number checks for M_PI isntead of just hoping for >1<commit_after>#pragma once
namespace mtao::geometry {
    template <typename PDerived, typename VDerived, typename BeginIt, typename EndIt>
    typename VDerived::Scalar winding_number(const Eigen::MatrixBase<VDerived>& V, const BeginIt& beginit, const EndIt& endit, const Eigen::MatrixBase<PDerived>& p) {
        using S = typename VDerived::Scalar;
        S value = 0;
        auto it = beginit;
        for(; it != endit; ++it) {
            auto it1 = it;
            it1++;
            if(it1 == endit) { it1 = beginit; }

            auto a = V.col(*it) - p;
            auto b = V.col(*it1) - p;
            S aa = std::atan2(a.y(),a.x());
            S ba = std::atan2(b.y(),b.x());
            S ang = ba - aa;
            if(ang > M_PI) {
                ang -= 2 * M_PI;
            } else if(ang <= -M_PI) {
                ang += 2*M_PI;
            }
            value += ang;

        }
        return value;
    }
    template <typename PDerived, typename VDerived, typename Container>
    typename VDerived::Scalar winding_number(const Eigen::MatrixBase<VDerived>& V, const Container& C, const Eigen::MatrixBase<PDerived>& p) {
        return winding_number(V,C.begin(),C.end(),p);
    }
    template <typename PDerived, typename VDerived>
    typename VDerived::Scalar winding_number(const Eigen::MatrixBase<VDerived>& V, const std::initializer_list<int>& C, const Eigen::MatrixBase<PDerived>& p) {
        return winding_number(V,C.begin(),C.end(),p);
    }
    template <typename PDerived, typename VDerived, typename BeginIt, typename EndIt>
    bool interior_winding_number(const Eigen::MatrixBase<VDerived>& V, const BeginIt& beginit, const EndIt& endit, const Eigen::MatrixBase<PDerived>& p) {
        auto v = winding_number(V,beginit,endit,p);
        return std::abs(v - M_PI) < 1;
    }
    template <typename PDerived, typename VDerived, typename Container>
    bool interior_winding_number(const Eigen::MatrixBase<VDerived>& V, const Container& C, const Eigen::MatrixBase<PDerived>& p) {
        return interior_winding_number(V,C.begin(),C.end(),p);
    }
    template <typename PDerived, typename VDerived>
    bool interior_winding_number(const Eigen::MatrixBase<VDerived>& V, const std::initializer_list<int>& C, const Eigen::MatrixBase<PDerived>& p) {
        return interior_winding_number(V,C.begin(),C.end(),p);
    }
}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: Eservices.cxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: obo $ $Date: 2004-03-15 12:47:35 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (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.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _CONNECTIVITY_FLAT_EDRIVER_HXX_
#include "flat/EDriver.hxx"
#endif
#ifndef _CPPUHELPER_FACTORY_HXX_
#include <cppuhelper/factory.hxx>
#endif
#ifndef _OSL_DIAGNOSE_H_
#include <osl/diagnose.h>
#endif

using namespace connectivity::flat;
using ::rtl::OUString;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::registry::XRegistryKey;
using ::com::sun::star::lang::XSingleServiceFactory;
using ::com::sun::star::lang::XMultiServiceFactory;

typedef Reference< XSingleServiceFactory > (SAL_CALL *createFactoryFunc)
        (
            const Reference< XMultiServiceFactory > & rServiceManager,
            const OUString & rComponentName,
            ::cppu::ComponentInstantiation pCreateFunction,
            const Sequence< OUString > & rServiceNames,
            rtl_ModuleCount* _pT
        );

//***************************************************************************************
//
// Die vorgeschriebene C-Api muss erfuellt werden!
// Sie besteht aus drei Funktionen, die von dem Modul exportiert werden muessen.
//

//---------------------------------------------------------------------------------------
void REGISTER_PROVIDER(
        const OUString& aServiceImplName,
        const Sequence< OUString>& Services,
        const Reference< ::com::sun::star::registry::XRegistryKey > & xKey)
{
    OUString aMainKeyName;
    aMainKeyName = OUString::createFromAscii("/");
    aMainKeyName += aServiceImplName;
    aMainKeyName += OUString::createFromAscii("/UNO/SERVICES");

    Reference< ::com::sun::star::registry::XRegistryKey >  xNewKey( xKey->createKey(aMainKeyName) );
    OSL_ENSURE(xNewKey.is(), "FILE::component_writeInfo : could not create a registry key !");

    for (sal_Int32 i=0; i<Services.getLength(); ++i)
        xNewKey->createKey(Services[i]);
}

//---------------------------------------------------------------------------------------
struct ProviderRequest
{
    Reference< XSingleServiceFactory > xRet;
    Reference< XMultiServiceFactory > const xServiceManager;
    OUString const sImplementationName;

    ProviderRequest(
        void* pServiceManager,
        sal_Char const* pImplementationName
    )
    : xServiceManager(reinterpret_cast<XMultiServiceFactory*>(pServiceManager))
    , sImplementationName(OUString::createFromAscii(pImplementationName))
    {
    }

    inline
    sal_Bool CREATE_PROVIDER(
                const OUString& Implname,
                const Sequence< OUString > & Services,
                ::cppu::ComponentInstantiation Factory,
                createFactoryFunc creator
            )
    {
        if (!xRet.is() && (Implname == sImplementationName))
        try
        {
            xRet = creator( xServiceManager, sImplementationName,Factory, Services,0);
        }
        catch(...)
        {
        }
        return xRet.is();
    }

    void* getProvider() const { return xRet.get(); }
};

//---------------------------------------------------------------------------------------

extern "C" void SAL_CALL component_getImplementationEnvironment(
                const sal_Char  **ppEnvTypeName,
                uno_Environment **ppEnv
            )
{
    *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
}

//---------------------------------------------------------------------------------------
extern "C" sal_Bool SAL_CALL component_writeInfo(
                void* pServiceManager,
                void* pRegistryKey
            )
{
    if (pRegistryKey)
    try
    {
        Reference< ::com::sun::star::registry::XRegistryKey > xKey(reinterpret_cast< ::com::sun::star::registry::XRegistryKey*>(pRegistryKey));

        REGISTER_PROVIDER(
            ODriver::getImplementationName_Static(),
            ODriver::getSupportedServiceNames_Static(), xKey);

        return sal_True;
    }
    catch (::com::sun::star::registry::InvalidRegistryException& )
    {
        OSL_ENSURE(sal_False, "FILE::component_writeInfo : could not create a registry key ! ## InvalidRegistryException !");
    }

    return sal_False;
}

//---------------------------------------------------------------------------------------
extern "C" void* SAL_CALL component_getFactory(
                    const sal_Char* pImplementationName,
                    void* pServiceManager,
                    void* pRegistryKey)
{
    void* pRet = 0;
    if (pServiceManager)
    {
        ProviderRequest aReq(pServiceManager,pImplementationName);

        aReq.CREATE_PROVIDER(
            ODriver::getImplementationName_Static(),
            ODriver::getSupportedServiceNames_Static(),
            ODriver_CreateInstance, ::cppu::createSingleFactory)
        ;

        if(aReq.xRet.is())
            aReq.xRet->acquire();

        pRet = aReq.getProvider();
    }

    return pRet;
};

<commit_msg>INTEGRATION: CWS ooo19126 (1.4.160); FILE MERGED 2005/09/05 17:23:58 rt 1.4.160.1: #i54170# Change license header: remove SISSL<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: Eservices.cxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 06:02:07 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef _CONNECTIVITY_FLAT_EDRIVER_HXX_
#include "flat/EDriver.hxx"
#endif
#ifndef _CPPUHELPER_FACTORY_HXX_
#include <cppuhelper/factory.hxx>
#endif
#ifndef _OSL_DIAGNOSE_H_
#include <osl/diagnose.h>
#endif

using namespace connectivity::flat;
using ::rtl::OUString;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::registry::XRegistryKey;
using ::com::sun::star::lang::XSingleServiceFactory;
using ::com::sun::star::lang::XMultiServiceFactory;

typedef Reference< XSingleServiceFactory > (SAL_CALL *createFactoryFunc)
        (
            const Reference< XMultiServiceFactory > & rServiceManager,
            const OUString & rComponentName,
            ::cppu::ComponentInstantiation pCreateFunction,
            const Sequence< OUString > & rServiceNames,
            rtl_ModuleCount* _pT
        );

//***************************************************************************************
//
// Die vorgeschriebene C-Api muss erfuellt werden!
// Sie besteht aus drei Funktionen, die von dem Modul exportiert werden muessen.
//

//---------------------------------------------------------------------------------------
void REGISTER_PROVIDER(
        const OUString& aServiceImplName,
        const Sequence< OUString>& Services,
        const Reference< ::com::sun::star::registry::XRegistryKey > & xKey)
{
    OUString aMainKeyName;
    aMainKeyName = OUString::createFromAscii("/");
    aMainKeyName += aServiceImplName;
    aMainKeyName += OUString::createFromAscii("/UNO/SERVICES");

    Reference< ::com::sun::star::registry::XRegistryKey >  xNewKey( xKey->createKey(aMainKeyName) );
    OSL_ENSURE(xNewKey.is(), "FILE::component_writeInfo : could not create a registry key !");

    for (sal_Int32 i=0; i<Services.getLength(); ++i)
        xNewKey->createKey(Services[i]);
}

//---------------------------------------------------------------------------------------
struct ProviderRequest
{
    Reference< XSingleServiceFactory > xRet;
    Reference< XMultiServiceFactory > const xServiceManager;
    OUString const sImplementationName;

    ProviderRequest(
        void* pServiceManager,
        sal_Char const* pImplementationName
    )
    : xServiceManager(reinterpret_cast<XMultiServiceFactory*>(pServiceManager))
    , sImplementationName(OUString::createFromAscii(pImplementationName))
    {
    }

    inline
    sal_Bool CREATE_PROVIDER(
                const OUString& Implname,
                const Sequence< OUString > & Services,
                ::cppu::ComponentInstantiation Factory,
                createFactoryFunc creator
            )
    {
        if (!xRet.is() && (Implname == sImplementationName))
        try
        {
            xRet = creator( xServiceManager, sImplementationName,Factory, Services,0);
        }
        catch(...)
        {
        }
        return xRet.is();
    }

    void* getProvider() const { return xRet.get(); }
};

//---------------------------------------------------------------------------------------

extern "C" void SAL_CALL component_getImplementationEnvironment(
                const sal_Char  **ppEnvTypeName,
                uno_Environment **ppEnv
            )
{
    *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
}

//---------------------------------------------------------------------------------------
extern "C" sal_Bool SAL_CALL component_writeInfo(
                void* pServiceManager,
                void* pRegistryKey
            )
{
    if (pRegistryKey)
    try
    {
        Reference< ::com::sun::star::registry::XRegistryKey > xKey(reinterpret_cast< ::com::sun::star::registry::XRegistryKey*>(pRegistryKey));

        REGISTER_PROVIDER(
            ODriver::getImplementationName_Static(),
            ODriver::getSupportedServiceNames_Static(), xKey);

        return sal_True;
    }
    catch (::com::sun::star::registry::InvalidRegistryException& )
    {
        OSL_ENSURE(sal_False, "FILE::component_writeInfo : could not create a registry key ! ## InvalidRegistryException !");
    }

    return sal_False;
}

//---------------------------------------------------------------------------------------
extern "C" void* SAL_CALL component_getFactory(
                    const sal_Char* pImplementationName,
                    void* pServiceManager,
                    void* pRegistryKey)
{
    void* pRet = 0;
    if (pServiceManager)
    {
        ProviderRequest aReq(pServiceManager,pImplementationName);

        aReq.CREATE_PROVIDER(
            ODriver::getImplementationName_Static(),
            ODriver::getSupportedServiceNames_Static(),
            ODriver_CreateInstance, ::cppu::createSingleFactory)
        ;

        if(aReq.xRet.is())
            aReq.xRet->acquire();

        pRet = aReq.getProvider();
    }

    return pRet;
};

<|endoftext|>


<commit_before>// Copyright (c) 2017-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/

#ifndef TAO_PEGTL_CONTRIB_PARSE_TREE_HPP
#define TAO_PEGTL_CONTRIB_PARSE_TREE_HPP

#include <cassert>
#include <memory>
#include <tuple>
#include <type_traits>
#include <typeinfo>
#include <utility>
#include <vector>

#include "../config.hpp"
#include "../normal.hpp"
#include "../nothing.hpp"
#include "../parse.hpp"

#include "../analysis/counted.hpp"
#include "../analysis/generic.hpp"
#include "../internal/conditional.hpp"
#include "../internal/demangle.hpp"
#include "../internal/iterator.hpp"

namespace tao
{
   namespace TAO_PEGTL_NAMESPACE
   {
      namespace parse_tree
      {
         template< typename T >
         struct basic_node
         {
            using node_t = T;
            using children_t = std::vector< std::unique_ptr< node_t > >;
            children_t children;

            const std::type_info* id = nullptr;
            std::string source;

            TAO_PEGTL_NAMESPACE::internal::iterator m_begin;
            TAO_PEGTL_NAMESPACE::internal::iterator m_end;

            // each node will be default constructed
            basic_node() = default;

            // no copy/move is necessary
            // (nodes are always owned/handled by a std::unique_ptr)
            basic_node( const basic_node& ) = delete;
            basic_node( basic_node&& ) = delete;

            ~basic_node() = default;

            // no assignment either
            basic_node& operator=( const basic_node& ) = delete;
            basic_node& operator=( basic_node&& ) = delete;

            bool is_root() const noexcept
            {
               return id == nullptr;
            }

            template< typename U >
            bool is() const noexcept
            {
               return id == &typeid( U );
            }

            std::string name() const
            {
               assert( !is_root() );
               return TAO_PEGTL_NAMESPACE::internal::demangle( id->name() );
            }

            position begin() const
            {
               return position( m_begin, source );
            }

            position end() const
            {
               return position( m_end, source );
            }

            bool has_content() const noexcept
            {
               return m_end.data != nullptr;
            }

            std::string content() const
            {
               assert( has_content() );
               return std::string( m_begin.data, m_end.data );
            }

            template< typename... States >
            void remove_content( States&&... /*unused*/ ) noexcept
            {
               m_end.reset();
            }

            // all non-root nodes are initialized by calling this method
            template< typename Rule, typename Input, typename... States >
            void start( const Input& in, States&&... /*unused*/ )
            {
               id = &typeid( Rule );
               source = in.source();
               m_begin = in.iterator();
            }

            // if parsing of the rule succeeded, this method is called
            template< typename Rule, typename Input, typename... States >
            void success( const Input& in, States&&... /*unused*/ ) noexcept
            {
               m_end = in.iterator();
            }

            // if parsing of the rule failed, this method is called
            template< typename Rule, typename Input, typename... States >
            void failure( const Input& /*unused*/, States&&... /*unused*/ ) noexcept
            {
            }

            // if parsing succeeded and the (optional) transform call
            // did not discard the node, it is appended to its parent.
            // note that "child" is the node whose Rule just succeeded
            // and "*this" is the parent where the node should be appended.
            template< typename... States >
            void emplace_back( std::unique_ptr< node_t > child, States&&... /*unused*/ )
            {
               assert( child );
               children.emplace_back( std::move( child ) );
            }
         };

         struct node
            : basic_node< node >
         {
         };

         namespace internal
         {
            template< typename Node >
            struct state
            {
               std::vector< std::unique_ptr< Node > > stack;

               state()
               {
                  emplace_back();
               }

               void emplace_back()
               {
                  stack.emplace_back( std::unique_ptr< Node >( new Node ) );  // NOLINT: std::make_unique requires C++14
               }

               std::unique_ptr< Node >& back() noexcept
               {
                  return stack.back();
               }

               void pop_back() noexcept
               {
                  return stack.pop_back();
               }
            };

            template< typename Selector, typename... States >
            void transform( States&&... /*unused*/ ) noexcept
            {
            }

            template< typename Selector, typename Node, typename... States >
            auto transform( std::unique_ptr< Node >& n, States&&... st ) noexcept( noexcept( Selector::transform( n, st... ) ) )
               -> decltype( Selector::transform( n, st... ), void() )
            {
               Selector::transform( n, st... );
            }

            template< unsigned Level, typename Analyse, template< typename... > class Selector >
            struct is_leaf
               : std::false_type
            {
            };

            template< analysis::rule_type Type, template< typename... > class Selector >
            struct is_leaf< 0, analysis::generic< Type >, Selector >
               : std::true_type
            {
            };

            template< analysis::rule_type Type, unsigned Count, template< typename... > class Selector >
            struct is_leaf< 0, analysis::counted< Type, Count >, Selector >
               : std::true_type
            {
            };

            template< analysis::rule_type Type, typename... Rules, template< typename... > class Selector >
            struct is_leaf< 0, analysis::generic< Type, Rules... >, Selector >
               : std::false_type
            {
            };

            template< analysis::rule_type Type, unsigned Count, typename... Rules, template< typename... > class Selector >
            struct is_leaf< 0, analysis::counted< Type, Count, Rules... >, Selector >
               : std::false_type
            {
            };

            template< bool... >
            struct bool_sequence;

            template< bool... Bs >
            using is_all = std::is_same< bool_sequence< Bs..., true >, bool_sequence< true, Bs... > >;

            template< unsigned Level, typename Rule, template< typename... > class Selector >
            using is_unselected_leaf = std::integral_constant< bool, !Selector< Rule >::value && is_leaf< Level, typename Rule::analyze_t, Selector >::value >;

            template< unsigned Level, analysis::rule_type Type, typename... Rules, template< typename... > class Selector >
            struct is_leaf< Level, analysis::generic< Type, Rules... >, Selector >
               : is_all< is_unselected_leaf< Level - 1, Rules, Selector >::value... >
            {
            };

            template< unsigned Level, analysis::rule_type Type, unsigned Count, typename... Rules, template< typename... > class Selector >
            struct is_leaf< Level, analysis::counted< Type, Count, Rules... >, Selector >
               : is_all< is_unselected_leaf< Level - 1, Rules, Selector >::value... >
            {
            };

            template< typename Node, template< typename... > class Selector, template< typename... > class Control >
            struct make_control
            {
               template< typename Rule, bool, bool >
               struct control;

               template< typename Rule >
               using type = control< Rule, Selector< Rule >::value, is_leaf< 8, typename Rule::analyze_t, Selector >::value >;
            };

            template< typename Node, template< typename... > class Selector, template< typename... > class Control >
            template< typename Rule >
            struct make_control< Node, Selector, Control >::control< Rule, false, true >
               : Control< Rule >
            {
            };

            template< typename Node, template< typename... > class Selector, template< typename... > class Control >
            template< typename Rule >
            struct make_control< Node, Selector, Control >::control< Rule, false, false >
               : Control< Rule >
            {
               template< typename Input, typename... States >
               static void start( const Input& in, States&&... st )
               {
                  Control< Rule >::start( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  state.emplace_back();
               }

               template< typename Input, typename... States >
               static void success( const Input& in, States&&... st )
               {
                  Control< Rule >::success( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  auto n = std::move( state.back() );
                  state.pop_back();
                  for( auto& c : n->children ) {
                     state.back()->children.emplace_back( std::move( c ) );
                  }
               }

               template< typename Input, typename... States >
               static void failure( const Input& in, States&&... st ) noexcept( noexcept( Control< Rule >::failure( in, st... ) ) )
               {
                  Control< Rule >::failure( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  state.pop_back();
               }
            };

            template< typename Node, template< typename... > class Selector, template< typename... > class Control >
            template< typename Rule, bool B >
            struct make_control< Node, Selector, Control >::control< Rule, true, B >
               : Control< Rule >
            {
               template< typename Input, typename... States >
               static void start( const Input& in, States&&... st )
               {
                  Control< Rule >::start( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  state.emplace_back();
                  state.back()->template start< Rule >( in, st... );
               }

               template< typename Input, typename... States >
               static void success( const Input& in, States&&... st )
               {
                  Control< Rule >::success( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  auto n = std::move( state.back() );
                  state.pop_back();
                  n->template success< Rule >( in, st... );
                  transform< Selector< Rule > >( n, st... );
                  if( n ) {
                     state.back()->emplace_back( std::move( n ), st... );
                  }
               }

               template< typename Input, typename... States >
               static void failure( const Input& in, States&&... st ) noexcept( noexcept( Control< Rule >::failure( in, st... ) ) && noexcept( std::declval< node& >().template failure< Rule >( in, st... ) ) )
               {
                  Control< Rule >::failure( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  state.back()->template failure< Rule >( in, st... );
                  state.pop_back();
               }
            };

            template< typename >
            struct element
            {
            };

            template< typename >
            struct store_all : std::true_type
            {
            };

         }  // namespace internal

         using store_content = std::true_type;

         // some nodes don't need to store their content
         struct remove_content : std::true_type
         {
            template< typename Node, typename... States >
            static void transform( std::unique_ptr< Node >& n, States&&... st ) noexcept( noexcept( n->Node::remove_content( st... ) ) )
            {
               n->remove_content( st... );
            }
         };

         // if a node has only one child, replace the node with its child, otherwise apply B
         template< typename Base >
         struct fold_one_or : Base
         {
            template< typename Node, typename... States >
            static void transform( std::unique_ptr< Node >& n, States&&... st ) noexcept( noexcept( n->children.size(), Base::transform( n, st... ) ) )
            {
               if( n->children.size() == 1 ) {
                  n = std::move( n->children.front() );
               }
               else {
                  Base::transform( n, st... );
               }
            }
         };

         // if a node has no children, discard the node, otherwise apply B
         template< typename Base >
         struct discard_empty_or : Base
         {
            template< typename Node, typename... States >
            static void transform( std::unique_ptr< Node >& n, States&&... st ) noexcept( noexcept( n->children.empty(), Base::transform( n, st... ) ) )
            {
               if( n->children.empty() ) {
                  n.reset();
               }
               else {
                  Base::transform( n, st... );
               }
            }
         };

         using fold_one = fold_one_or< remove_content >;
         using discard_empty = discard_empty_or< remove_content >;

         template< typename Rule, typename... Collections >
         struct selector : std::false_type
         {
         };

         // TODO: Implement in a non-recursive way
         // TODO: Check for multiple matches (currently: first match wins)
         template< typename Rule, typename Collection, typename... Collections >
         struct selector< Rule, Collection, Collections... >
            : TAO_PEGTL_NAMESPACE::internal::conditional< Collection::template contains< Rule >::value >::template type< typename Collection::type, selector< Rule, Collections... > >
         {
         };

         template< typename Base >
         struct apply
         {
            template< typename... Rules >
            struct to
               : internal::element< Rules >...
            {
               using type = Base;

               template< typename Rule >
               using contains = std::is_base_of< internal::element< Rule >, to >;
            };
         };

         using apply_store_content = apply< store_content >;
         using apply_remove_content = apply< remove_content >;
         using apply_fold_one = apply< fold_one >;
         using apply_discard_empty = apply< discard_empty >;

         template< typename Rule,
                   typename Node,
                   template< typename... > class Selector = internal::store_all,
                   template< typename... > class Action = nothing,
                   template< typename... > class Control = normal,
                   typename Input,
                   typename... States >
         std::unique_ptr< Node > parse( Input&& in, States&&... st )
         {
            internal::state< Node > state;
            if( !TAO_PEGTL_NAMESPACE::parse< Rule, Action, internal::make_control< Node, Selector, Control >::template type >( in, st..., state ) ) {
               return nullptr;
            }
            assert( state.stack.size() == 1 );
            return std::move( state.back() );
         }

         template< typename Rule,
                   template< typename... > class Selector = internal::store_all,
                   template< typename... > class Action = nothing,
                   template< typename... > class Control = normal,
                   typename Input,
                   typename... States >
         std::unique_ptr< node > parse( Input&& in, States&&... st )
         {
            return parse< Rule, node, Selector, Action, Control >( in, st... );
         }

      }  // namespace parse_tree

   }  // namespace TAO_PEGTL_NAMESPACE

}  // namespace tao

#endif
<commit_msg>Provide a memory input for the content<commit_after>// Copyright (c) 2017-2018 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/

#ifndef TAO_PEGTL_CONTRIB_PARSE_TREE_HPP
#define TAO_PEGTL_CONTRIB_PARSE_TREE_HPP

#include <cassert>
#include <memory>
#include <tuple>
#include <type_traits>
#include <typeinfo>
#include <utility>
#include <vector>

#include "../config.hpp"
#include "../memory_input.hpp"
#include "../normal.hpp"
#include "../nothing.hpp"
#include "../parse.hpp"

#include "../analysis/counted.hpp"
#include "../analysis/generic.hpp"
#include "../internal/conditional.hpp"
#include "../internal/demangle.hpp"
#include "../internal/iterator.hpp"

namespace tao
{
   namespace TAO_PEGTL_NAMESPACE
   {
      namespace parse_tree
      {
         template< typename T >
         struct basic_node
         {
            using node_t = T;
            using children_t = std::vector< std::unique_ptr< node_t > >;
            children_t children;

            const std::type_info* id = nullptr;
            std::string source;

            TAO_PEGTL_NAMESPACE::internal::iterator m_begin;
            TAO_PEGTL_NAMESPACE::internal::iterator m_end;

            // each node will be default constructed
            basic_node() = default;

            // no copy/move is necessary
            // (nodes are always owned/handled by a std::unique_ptr)
            basic_node( const basic_node& ) = delete;
            basic_node( basic_node&& ) = delete;

            ~basic_node() = default;

            // no assignment either
            basic_node& operator=( const basic_node& ) = delete;
            basic_node& operator=( basic_node&& ) = delete;

            bool is_root() const noexcept
            {
               return id == nullptr;
            }

            template< typename U >
            bool is() const noexcept
            {
               return id == &typeid( U );
            }

            std::string name() const
            {
               assert( !is_root() );
               return TAO_PEGTL_NAMESPACE::internal::demangle( id->name() );
            }

            position begin() const
            {
               return position( m_begin, source );
            }

            position end() const
            {
               return position( m_end, source );
            }

            bool has_content() const noexcept
            {
               return m_end.data != nullptr;
            }

            std::string content() const
            {
               assert( has_content() );
               return std::string( m_begin.data, m_end.data );
            }

            template< tracking_mode P = tracking_mode::IMMEDIATE, typename Eol = eol::lf_crlf >
            memory_input< P, Eol > as_memory_input() const
            {
               assert( has_content() );
               return memory_input< P, Eol >( m_begin.data, m_end.data - m_begin.data, source, m_begin.byte, m_begin.line, m_begin.byte_in_line );
            }

            template< typename... States >
            void remove_content( States&&... /*unused*/ ) noexcept
            {
               m_end.reset();
            }

            // all non-root nodes are initialized by calling this method
            template< typename Rule, typename Input, typename... States >
            void start( const Input& in, States&&... /*unused*/ )
            {
               id = &typeid( Rule );
               source = in.source();
               m_begin = in.iterator();
            }

            // if parsing of the rule succeeded, this method is called
            template< typename Rule, typename Input, typename... States >
            void success( const Input& in, States&&... /*unused*/ ) noexcept
            {
               m_end = in.iterator();
            }

            // if parsing of the rule failed, this method is called
            template< typename Rule, typename Input, typename... States >
            void failure( const Input& /*unused*/, States&&... /*unused*/ ) noexcept
            {
            }

            // if parsing succeeded and the (optional) transform call
            // did not discard the node, it is appended to its parent.
            // note that "child" is the node whose Rule just succeeded
            // and "*this" is the parent where the node should be appended.
            template< typename... States >
            void emplace_back( std::unique_ptr< node_t > child, States&&... /*unused*/ )
            {
               assert( child );
               children.emplace_back( std::move( child ) );
            }
         };

         struct node
            : basic_node< node >
         {
         };

         namespace internal
         {
            template< typename Node >
            struct state
            {
               std::vector< std::unique_ptr< Node > > stack;

               state()
               {
                  emplace_back();
               }

               void emplace_back()
               {
                  stack.emplace_back( std::unique_ptr< Node >( new Node ) );  // NOLINT: std::make_unique requires C++14
               }

               std::unique_ptr< Node >& back() noexcept
               {
                  return stack.back();
               }

               void pop_back() noexcept
               {
                  return stack.pop_back();
               }
            };

            template< typename Selector, typename... States >
            void transform( States&&... /*unused*/ ) noexcept
            {
            }

            template< typename Selector, typename Node, typename... States >
            auto transform( std::unique_ptr< Node >& n, States&&... st ) noexcept( noexcept( Selector::transform( n, st... ) ) )
               -> decltype( Selector::transform( n, st... ), void() )
            {
               Selector::transform( n, st... );
            }

            template< unsigned Level, typename Analyse, template< typename... > class Selector >
            struct is_leaf
               : std::false_type
            {
            };

            template< analysis::rule_type Type, template< typename... > class Selector >
            struct is_leaf< 0, analysis::generic< Type >, Selector >
               : std::true_type
            {
            };

            template< analysis::rule_type Type, unsigned Count, template< typename... > class Selector >
            struct is_leaf< 0, analysis::counted< Type, Count >, Selector >
               : std::true_type
            {
            };

            template< analysis::rule_type Type, typename... Rules, template< typename... > class Selector >
            struct is_leaf< 0, analysis::generic< Type, Rules... >, Selector >
               : std::false_type
            {
            };

            template< analysis::rule_type Type, unsigned Count, typename... Rules, template< typename... > class Selector >
            struct is_leaf< 0, analysis::counted< Type, Count, Rules... >, Selector >
               : std::false_type
            {
            };

            template< bool... >
            struct bool_sequence;

            template< bool... Bs >
            using is_all = std::is_same< bool_sequence< Bs..., true >, bool_sequence< true, Bs... > >;

            template< unsigned Level, typename Rule, template< typename... > class Selector >
            using is_unselected_leaf = std::integral_constant< bool, !Selector< Rule >::value && is_leaf< Level, typename Rule::analyze_t, Selector >::value >;

            template< unsigned Level, analysis::rule_type Type, typename... Rules, template< typename... > class Selector >
            struct is_leaf< Level, analysis::generic< Type, Rules... >, Selector >
               : is_all< is_unselected_leaf< Level - 1, Rules, Selector >::value... >
            {
            };

            template< unsigned Level, analysis::rule_type Type, unsigned Count, typename... Rules, template< typename... > class Selector >
            struct is_leaf< Level, analysis::counted< Type, Count, Rules... >, Selector >
               : is_all< is_unselected_leaf< Level - 1, Rules, Selector >::value... >
            {
            };

            template< typename Node, template< typename... > class Selector, template< typename... > class Control >
            struct make_control
            {
               template< typename Rule, bool, bool >
               struct control;

               template< typename Rule >
               using type = control< Rule, Selector< Rule >::value, is_leaf< 8, typename Rule::analyze_t, Selector >::value >;
            };

            template< typename Node, template< typename... > class Selector, template< typename... > class Control >
            template< typename Rule >
            struct make_control< Node, Selector, Control >::control< Rule, false, true >
               : Control< Rule >
            {
            };

            template< typename Node, template< typename... > class Selector, template< typename... > class Control >
            template< typename Rule >
            struct make_control< Node, Selector, Control >::control< Rule, false, false >
               : Control< Rule >
            {
               template< typename Input, typename... States >
               static void start( const Input& in, States&&... st )
               {
                  Control< Rule >::start( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  state.emplace_back();
               }

               template< typename Input, typename... States >
               static void success( const Input& in, States&&... st )
               {
                  Control< Rule >::success( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  auto n = std::move( state.back() );
                  state.pop_back();
                  for( auto& c : n->children ) {
                     state.back()->children.emplace_back( std::move( c ) );
                  }
               }

               template< typename Input, typename... States >
               static void failure( const Input& in, States&&... st ) noexcept( noexcept( Control< Rule >::failure( in, st... ) ) )
               {
                  Control< Rule >::failure( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  state.pop_back();
               }
            };

            template< typename Node, template< typename... > class Selector, template< typename... > class Control >
            template< typename Rule, bool B >
            struct make_control< Node, Selector, Control >::control< Rule, true, B >
               : Control< Rule >
            {
               template< typename Input, typename... States >
               static void start( const Input& in, States&&... st )
               {
                  Control< Rule >::start( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  state.emplace_back();
                  state.back()->template start< Rule >( in, st... );
               }

               template< typename Input, typename... States >
               static void success( const Input& in, States&&... st )
               {
                  Control< Rule >::success( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  auto n = std::move( state.back() );
                  state.pop_back();
                  n->template success< Rule >( in, st... );
                  transform< Selector< Rule > >( n, st... );
                  if( n ) {
                     state.back()->emplace_back( std::move( n ), st... );
                  }
               }

               template< typename Input, typename... States >
               static void failure( const Input& in, States&&... st ) noexcept( noexcept( Control< Rule >::failure( in, st... ) ) && noexcept( std::declval< node& >().template failure< Rule >( in, st... ) ) )
               {
                  Control< Rule >::failure( in, st... );
                  auto& state = std::get< sizeof...( st ) - 1 >( std::tie( st... ) );
                  state.back()->template failure< Rule >( in, st... );
                  state.pop_back();
               }
            };

            template< typename >
            struct element
            {
            };

            template< typename >
            struct store_all : std::true_type
            {
            };

         }  // namespace internal

         using store_content = std::true_type;

         // some nodes don't need to store their content
         struct remove_content : std::true_type
         {
            template< typename Node, typename... States >
            static void transform( std::unique_ptr< Node >& n, States&&... st ) noexcept( noexcept( n->Node::remove_content( st... ) ) )
            {
               n->remove_content( st... );
            }
         };

         // if a node has only one child, replace the node with its child, otherwise apply B
         template< typename Base >
         struct fold_one_or : Base
         {
            template< typename Node, typename... States >
            static void transform( std::unique_ptr< Node >& n, States&&... st ) noexcept( noexcept( n->children.size(), Base::transform( n, st... ) ) )
            {
               if( n->children.size() == 1 ) {
                  n = std::move( n->children.front() );
               }
               else {
                  Base::transform( n, st... );
               }
            }
         };

         // if a node has no children, discard the node, otherwise apply B
         template< typename Base >
         struct discard_empty_or : Base
         {
            template< typename Node, typename... States >
            static void transform( std::unique_ptr< Node >& n, States&&... st ) noexcept( noexcept( n->children.empty(), Base::transform( n, st... ) ) )
            {
               if( n->children.empty() ) {
                  n.reset();
               }
               else {
                  Base::transform( n, st... );
               }
            }
         };

         using fold_one = fold_one_or< remove_content >;
         using discard_empty = discard_empty_or< remove_content >;

         template< typename Rule, typename... Collections >
         struct selector : std::false_type
         {
         };

         // TODO: Implement in a non-recursive way
         // TODO: Check for multiple matches (currently: first match wins)
         template< typename Rule, typename Collection, typename... Collections >
         struct selector< Rule, Collection, Collections... >
            : TAO_PEGTL_NAMESPACE::internal::conditional< Collection::template contains< Rule >::value >::template type< typename Collection::type, selector< Rule, Collections... > >
         {
         };

         template< typename Base >
         struct apply
         {
            template< typename... Rules >
            struct to
               : internal::element< Rules >...
            {
               using type = Base;

               template< typename Rule >
               using contains = std::is_base_of< internal::element< Rule >, to >;
            };
         };

         using apply_store_content = apply< store_content >;
         using apply_remove_content = apply< remove_content >;
         using apply_fold_one = apply< fold_one >;
         using apply_discard_empty = apply< discard_empty >;

         template< typename Rule,
                   typename Node,
                   template< typename... > class Selector = internal::store_all,
                   template< typename... > class Action = nothing,
                   template< typename... > class Control = normal,
                   typename Input,
                   typename... States >
         std::unique_ptr< Node > parse( Input&& in, States&&... st )
         {
            internal::state< Node > state;
            if( !TAO_PEGTL_NAMESPACE::parse< Rule, Action, internal::make_control< Node, Selector, Control >::template type >( in, st..., state ) ) {
               return nullptr;
            }
            assert( state.stack.size() == 1 );
            return std::move( state.back() );
         }

         template< typename Rule,
                   template< typename... > class Selector = internal::store_all,
                   template< typename... > class Action = nothing,
                   template< typename... > class Control = normal,
                   typename Input,
                   typename... States >
         std::unique_ptr< node > parse( Input&& in, States&&... st )
         {
            return parse< Rule, node, Selector, Action, Control >( in, st... );
         }

      }  // namespace parse_tree

   }  // namespace TAO_PEGTL_NAMESPACE

}  // namespace tao

#endif
<|endoftext|>


<commit_before>#ifndef NON_EMPTY_SEQS_IMPLEMENTED
#define NON_EMPTY_SEQS_IMPLEMENTED

#include <yaml/config.hpp>
#include <yaml/core.hpp>
#include <yaml/sequence/sequence_def.hpp>
#include <yaml/sequence/fold_right.hpp>

BEGIN_YAML_NSP

BEGIN_DETAIL_NSP

template<typename S> class non_empty_seqs_tmpl {

  template<typename> struct impl;

  template<> struct impl<empty_seq> {
    using type = empty_seq;
  };

  template<typename H, typename R> struct impl<seq<H, R>> {

    template<typename Y, typename R> struct f_tmpl {
      using type = cons::ret<Y, cons::ret<cons::ret<H, Y> , R>>;
    };

    using f = make_curried_t<f_tmpl>;

    using type = cons::ret<seq<H, empty_seq>,
      fold_right::ret<f, empty_seq, impl<force_t<R>>>>;

  };

public:

  using type = typename impl<force_t<S>>::type;

};

END_DETAIL_NSP

/// brief Returns the list of all subsequences of the argument.
using non_empty_seqs = make_curried_t<DETAIL_NSP_REF non_empty_seqs_tmpl>;

END_YAML_NSP

#endif NON_EMPTY_SEQS_IMPLEMENTED<commit_msg>fix non_empty_seqs brief comment<commit_after>#ifndef NON_EMPTY_SEQS_IMPLEMENTED
#define NON_EMPTY_SEQS_IMPLEMENTED

#include <yaml/config.hpp>
#include <yaml/core.hpp>
#include <yaml/sequence/sequence_def.hpp>
#include <yaml/sequence/fold_right.hpp>

BEGIN_YAML_NSP

BEGIN_DETAIL_NSP

template<typename S> class non_empty_seqs_tmpl {

  template<typename> struct impl;

  template<> struct impl<empty_seq> {
    using type = empty_seq;
  };

  template<typename H, typename R> struct impl<seq<H, R>> {

    template<typename Y, typename R> struct f_tmpl {
      using type = cons::ret<Y, cons::ret<cons::ret<H, Y> , R>>;
    };

    using f = make_curried_t<f_tmpl>;

    using type = cons::ret<seq<H, empty_seq>,
      fold_right::ret<f, empty_seq, impl<force_t<R>>>>;

  };

public:

  using type = typename impl<force_t<S>>::type;

};

END_DETAIL_NSP

/// brief Returns the list of all subsequences of the argument,
///       except for the empty list.
using non_empty_seqs = make_curried_t<DETAIL_NSP_REF non_empty_seqs_tmpl>;

END_YAML_NSP

#endif NON_EMPTY_SEQS_IMPLEMENTED<|endoftext|>


<commit_before>#include <cppunit/TestCase.h>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TextTestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <netinet/in.h>

#include "Calculator.hpp"

#include <iostream>
#include <string>
#include <list>

using namespace CppUnit;
using namespace std;

//-----------------------------------------------------------------------------

class TestCalculator : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(TestCalculator);
    CPPUNIT_TEST(testSummation);
    CPPUNIT_TEST(testDifference);
    CPPUNIT_TEST(testMultiplication);
    CPPUNIT_TEST(testDivision);
    CPPUNIT_TEST_SUITE_END();

public:
    void setUp(void);
    void tearDown(void);

protected:
    void testSummation(void);
    void testDifference(void);
    void testMultiplication(void);
    void testDivision(void);

private:

    Calculator *mTestObj;
};

//-----------------------------------------------------------------------------

void
TestCalculator::testSummation(void)
{
    CPPUNIT_ASSERT(9 == mTestObj->Summation(6,3));
}

void
TestCalculator::testDifference(void)
{
    CPPUNIT_ASSERT(3 == mTestObj->Difference(6,3));
}

void
TestCalculator::testMultiplication(void)
{
    CPPUNIT_ASSERT(18 == mTestObj->Multiplication(6,3));
}

void
TestCalculator::testDivision(void)
{
    CPPUNIT_ASSERT(2 == mTestObj->Division(6,3));
}

void
TestCalculator::setUp(void)
{
    mTestObj = new Calculator();
}

void TestCalculator::tearDown(void)
{
    delete mTestObj;
}

//-----------------------------------------------------------------------------

CPPUNIT_TEST_SUITE_REGISTRATION( TestCalculator );

int main(int argc, char* argv[])
{
    // informs test-listener about testresults
    CPPUNIT_NS::TestResult testresult;

    // register listener for collecting the test-results
    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener (&collectedresults);

    // register listener for per-test progress output
    CPPUNIT_NS::BriefTestProgressListener progress;
    testresult.addListener (&progress);

    // insert test-suite at test-runner by registry
    CPPUNIT_NS::TestRunner testrunner;
    testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
    testrunner.run(testresult);

    // output results in compiler-format
    CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
    compileroutputter.write ();

    // Output XML for Jenkins CPPunit plugin
    ofstream xmlFileOut("cppTestBasicMathResults.xml");
    XmlOutputter xmlOut(&collectedresults, xmlFileOut);
    xmlOut.write();

    // return 0 if tests were successful
    return collectedresults.wasSuccessful() ? 0 : 1;
}

<commit_msg>Create TestCalculator.cpp<commit_after>#include <cppunit/TestCase.h>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TextTestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/XmlOutputter.h>
#include <netinet/in.h>

#include <iostream>
#include <string>
#include <list>

#include "Calculator.hpp"

using namespace CppUnit;
using namespace std;

//-----------------------------------------------------------------------------

class TestCalculator : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE(TestCalculator);
    CPPUNIT_TEST(testSummation);
    CPPUNIT_TEST(testDifference);
    CPPUNIT_TEST(testMultiplication);
    CPPUNIT_TEST(testDivision);
    CPPUNIT_TEST_SUITE_END();

public:
    void setUp(void);
    void tearDown(void);

protected:
    void testSummation(void);
    void testDifference(void);
    void testMultiplication(void);
    void testDivision(void);

private:

    Calculator *mTestObj;
};

//-----------------------------------------------------------------------------

void
TestCalculator::testSummation(void)
{
    CPPUNIT_ASSERT(9 == mTestObj->Summation(6,3));
}

void
TestCalculator::testDifference(void)
{
    CPPUNIT_ASSERT(3 == mTestObj->Difference(6,3));
}

void
TestCalculator::testMultiplication(void)
{
    CPPUNIT_ASSERT(18 == mTestObj->Multiplication(6,3));
}

void
TestCalculator::testDivision(void)
{
    CPPUNIT_ASSERT(2 == mTestObj->Division(6,3));
}

void
TestCalculator::setUp(void)
{
    mTestObj = new Calculator();
}

void TestCalculator::tearDown(void)
{
    delete mTestObj;
}

//-----------------------------------------------------------------------------

CPPUNIT_TEST_SUITE_REGISTRATION( TestCalculator );

int main(int argc, char* argv[])
{
    // informs test-listener about testresults
    CPPUNIT_NS::TestResult testresult;

    // register listener for collecting the test-results
    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener (&collectedresults);

    // register listener for per-test progress output
    CPPUNIT_NS::BriefTestProgressListener progress;
    testresult.addListener (&progress);

    // insert test-suite at test-runner by registry
    CPPUNIT_NS::TestRunner testrunner;
    testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
    testrunner.run(testresult);

    // output results in compiler-format
    CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
    compileroutputter.write ();

    // Output XML for Jenkins CPPunit plugin
    ofstream xmlFileOut("cppTestBasicMathResults.xml");
    XmlOutputter xmlOut(&collectedresults, xmlFileOut);
    xmlOut.write();

    // return 0 if tests were successful
    return collectedresults.wasSuccessful() ? 0 : 1;
}

<|endoftext|>


<commit_before>#include <iostream>

#include <seqan/base.h>
#include <seqan/align.h>
#include <seqan/file.h>  // for printint strings

int main()
{
    seqan::Dna5String seqH = "CGATT";
    seqan::Dna5String seqV = "CGAAATT";

    seqan::Align<Dna5String> align;
    resize(rows(align), 2);
    assignSource(row(align, 0), seqH);
    assignSource(row(align, 0), seqV);

    seqan::Score<int, seqan::Simple> scoringScheme(2, -1, -2, -1);
    seqan::AlignConfig<> alignConfig;

    int result = globalAlignment(align, scoringScheme, alignConfig);

    std::cerr << "The resulting alignment is\n"
              << align;

    return 0;
}
<commit_msg>[FIX] Fixing include in the align demos.<commit_after>#include <iostream>

#include <seqan/basic.h>
#include <seqan/align.h>
#include <seqan/file.h>  // for printint strings

int main()
{
    seqan::Dna5String seqH = "CGATT";
    seqan::Dna5String seqV = "CGAAATT";

    seqan::Align<Dna5String> align;
    resize(rows(align), 2);
    assignSource(row(align, 0), seqH);
    assignSource(row(align, 0), seqV);

    seqan::Score<int, seqan::Simple> scoringScheme(2, -1, -2, -1);
    seqan::AlignConfig<> alignConfig;

    int result = globalAlignment(align, scoringScheme, alignConfig);

    std::cerr << "The resulting alignment is\n"
              << align;

    return 0;
}
<|endoftext|>


<commit_before>#pragma once

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <limits>

#include "acmacs-base/float.hh"
#include "acmacs-base/to-string.hh"

// ----------------------------------------------------------------------

namespace acmacs
{
    class PointCoordinates
    {
      public:
        enum create_copy { create_copy };
        enum with_nan_coordinates_2D { with_nan_coordinates_2D };

        PointCoordinates(enum with_nan_coordinates_2D) : data_(2, std::numeric_limits<double>::quiet_NaN()), begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(size_t number_of_dimensions) : data_(number_of_dimensions, std::numeric_limits<double>::quiet_NaN()), begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(double x, double y) : data_{x, y}, begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(double x, double y, double z) : data_{x, y, z}, begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(enum create_copy, const PointCoordinates& source) : data_{source.begin_, source.end_}, begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(std::vector<double>::const_iterator first, std::vector<double>::const_iterator last) : begin_{&const_cast<double&>(*first)}, end_{&const_cast<double&>(*last)} {}
        explicit PointCoordinates(const std::vector<double>& source) : PointCoordinates(std::begin(source), std::end(source)) {}

        PointCoordinates(const PointCoordinates& rhs) : data_(rhs.data_), begin_{rhs.data_.empty() ? rhs.begin_ : &*data_.begin()}, end_{rhs.data_.empty() ? rhs.end_ : &*data_.end()} {}
        PointCoordinates(PointCoordinates&& rhs) : data_(std::move(rhs.data_)), begin_{rhs.data_.empty() ? rhs.begin_ : &*data_.begin()}, end_{rhs.data_.empty() ? rhs.end_ : &*data_.end()} {}

        PointCoordinates& operator=(const PointCoordinates& rhs)
        {
            assert(number_of_dimensions() == rhs.number_of_dimensions());
            if (rhs.data_.empty()) {
                data_.clear();
                begin_ = rhs.begin_;
                end_ = rhs.end_;
            }
            else {
                data_ = rhs.data_;
                begin_ = &*data_.begin();
                end_ = &*data_.end();
            }
            return *this;
        }

        bool operator==(const PointCoordinates& rhs) const { return std::equal(begin_, end_, rhs.begin_, rhs.end_); }
        bool operator!=(const PointCoordinates& rhs) const { return !operator==(rhs); }

        constexpr size_t number_of_dimensions() const { return static_cast<size_t>(end_ - begin_); }
        double operator[](size_t dim) const { /* assert(dim < number_of_dimensions()); */ return *(begin_ + dim); }
        double& operator[](size_t dim) { /* assert(dim < number_of_dimensions()); */ return *(begin_ + dim); }

        constexpr double x() const { return operator[](0); }
        constexpr double y() const { return operator[](1); }
        constexpr double z() const { return operator[](2); }

        void x(double val) { operator[](0) = val; }
        void y(double val) { operator[](1) = val; }
        void z(double val) { operator[](2) = val; }

        PointCoordinates operator-() const { PointCoordinates result(create_copy, *this); std::transform(begin_, end_, begin_, [](double val) { return -val; }); return result; }
        PointCoordinates operator+=(const PointCoordinates& rhs) { std::transform(begin_, end_, rhs.begin_, begin_, [](double v1, double v2) { return v1 + v2; }); return *this; }
        PointCoordinates operator+=(double val) { std::transform(begin_, end_, begin_, [val](double v1) { return v1 + val; }); return *this; }
        PointCoordinates operator-=(const PointCoordinates& rhs) { std::transform(begin_, end_, rhs.begin_, begin_, [](double v1, double v2) { return v1 - v2; }); return *this; }
        PointCoordinates operator*=(double val) { std::transform(begin_, end_, begin_, [val](double v1) { return v1 * val; }); return *this; }
        PointCoordinates operator/=(double val) { std::transform(begin_, end_, begin_, [val](double v1) { return v1 / val; }); return *this; }

        constexpr const double* begin() const { return begin_; }
        constexpr const double* end() const { return end_; }
        constexpr double* begin() { return begin_; }
        constexpr double* end() { return end_; }

        bool not_nan() const
        {
            return std::all_of(begin(), end(), [](double value) -> bool { return !std::isnan(value); });
        }

       PointCoordinates mean_with(const PointCoordinates& another) const
       {
           PointCoordinates result(number_of_dimensions());
           std::transform(begin_, end_, another.begin_, result.begin_, [](double v1, double v2) { return (v1 + v2) / 2.0; });
           return result;
       }

      private:
        std::vector<double> data_;
        double* begin_;
        double* end_;

    }; // class PointCoordinates

    inline std::string to_string(const PointCoordinates& coord, size_t precision = 32)
    {
        auto result = '{' + acmacs::to_string(coord.x(), precision) + ", " + acmacs::to_string(coord.y(), precision);
        if (coord.number_of_dimensions() == 3)
            result += ", " + to_string(coord.z(), precision);
        return result + '}';
    }

    inline std::ostream& operator<<(std::ostream& out, const PointCoordinates& coord) { return out << to_string(coord); }

    inline double distance2(const PointCoordinates& p1, const PointCoordinates& p2)
    {
        std::vector<double> diff(p1.number_of_dimensions());
        std::transform(p1.begin(), p1.end(), p2.begin(), diff.begin(), [](double v1, double v2) { return v1 - v2; });
        return std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
    }

    inline double distance(const PointCoordinates& p1, const PointCoordinates& p2) { return std::sqrt(distance2(p1, p2)); }

    inline PointCoordinates operator+(const PointCoordinates& p1, const PointCoordinates& p2) { PointCoordinates result(PointCoordinates::create_copy, p1); result += p2; return result; }
    inline PointCoordinates operator+(const PointCoordinates& p1, double val) { PointCoordinates result(PointCoordinates::create_copy, p1); result += val; return result; }
    inline PointCoordinates operator-(const PointCoordinates& p1, const PointCoordinates& p2) { PointCoordinates result(PointCoordinates::create_copy, p1); result -= p2; return result; }
    inline PointCoordinates operator-(const PointCoordinates& p1, double val) { PointCoordinates result(PointCoordinates::create_copy, p1); result += -val; return result; }
    inline PointCoordinates operator*(const PointCoordinates& p1, double val) { PointCoordinates result(PointCoordinates::create_copy, p1); result *= val; return result; }
    inline PointCoordinates operator/(const PointCoordinates& p1, double val) { PointCoordinates result(PointCoordinates::create_copy, p1); result /= val; return result; }

} // namespace acmacs

// ----------------------------------------------------------------------
/// Local Variables:
/// eval: (if (fboundp 'eu-rename-buffer) (eu-rename-buffer))
/// End:
<commit_msg>PointCoordinates development<commit_after>#pragma once

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <limits>

#include "acmacs-base/float.hh"
#include "acmacs-base/to-string.hh"

// ----------------------------------------------------------------------

namespace acmacs
{
    class PointCoordinates
    {
      public:
        enum create_copy { create_copy };
        enum with_nan_coordinates_2D { with_nan_coordinates_2D };

        PointCoordinates(enum with_nan_coordinates_2D) : data_(2, std::numeric_limits<double>::quiet_NaN()), begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(size_t number_of_dimensions) : data_(number_of_dimensions, std::numeric_limits<double>::quiet_NaN()), begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(double x, double y) : data_{x, y}, begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(double x, double y, double z) : data_{x, y, z}, begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(enum create_copy, const PointCoordinates& source) : data_{source.begin_, source.end_}, begin_{&*data_.begin()}, end_{&*data_.end()} {}
        PointCoordinates(std::vector<double>::const_iterator first, std::vector<double>::const_iterator last) : begin_{&const_cast<double&>(*first)}, end_{&const_cast<double&>(*last)} {}
        explicit PointCoordinates(const std::vector<double>& source) : PointCoordinates(std::begin(source), std::end(source)) {}

        PointCoordinates(const PointCoordinates& rhs) : data_(rhs.data_), begin_{rhs.data_.empty() ? rhs.begin_ : &*data_.begin()}, end_{rhs.data_.empty() ? rhs.end_ : &*data_.end()} {}
        PointCoordinates(PointCoordinates&& rhs) : data_(std::move(rhs.data_)), begin_{rhs.data_.empty() ? rhs.begin_ : &*data_.begin()}, end_{rhs.data_.empty() ? rhs.end_ : &*data_.end()} {}

        PointCoordinates& operator=(const PointCoordinates& rhs)
        {
            assert(number_of_dimensions() == rhs.number_of_dimensions());
            if (rhs.data_.empty()) {
                data_.clear();
                begin_ = rhs.begin_;
                end_ = rhs.end_;
            }
            else {
                data_ = rhs.data_;
                begin_ = &*data_.begin();
                end_ = &*data_.end();
            }
            return *this;
        }

        bool operator==(const PointCoordinates& rhs) const { return std::equal(begin_, end_, rhs.begin_, rhs.end_); }
        bool operator!=(const PointCoordinates& rhs) const { return !operator==(rhs); }

        constexpr size_t number_of_dimensions() const { return static_cast<size_t>(end_ - begin_); }
        double operator[](size_t dim) const { /* assert(dim < number_of_dimensions()); */ return *(begin_ + dim); }
        double& operator[](size_t dim) { /* assert(dim < number_of_dimensions()); */ return *(begin_ + dim); }

        constexpr double x() const { return operator[](0); }
        constexpr double y() const { return operator[](1); }
        constexpr double z() const { return operator[](2); }

        void x(double val) { operator[](0) = val; }
        void y(double val) { operator[](1) = val; }
        void z(double val) { operator[](2) = val; }

        PointCoordinates operator-() const { PointCoordinates result(create_copy, *this); std::transform(begin_, end_, begin_, [](double val) { return -val; }); return result; }
        PointCoordinates operator+=(const PointCoordinates& rhs) { std::transform(begin_, end_, rhs.begin_, begin_, [](double v1, double v2) { return v1 + v2; }); return *this; }
        PointCoordinates operator+=(double val) { std::transform(begin_, end_, begin_, [val](double v1) { return v1 + val; }); return *this; }
        PointCoordinates operator-=(const PointCoordinates& rhs) { std::transform(begin_, end_, rhs.begin_, begin_, [](double v1, double v2) { return v1 - v2; }); return *this; }
        PointCoordinates operator*=(double val) { std::transform(begin_, end_, begin_, [val](double v1) { return v1 * val; }); return *this; }
        PointCoordinates operator/=(double val) { std::transform(begin_, end_, begin_, [val](double v1) { return v1 / val; }); return *this; }

        constexpr const double* begin() const { return begin_; }
        constexpr const double* end() const { return end_; }
        constexpr double* begin() { return begin_; }
        constexpr double* end() { return end_; }

        bool not_nan() const
        {
            return std::all_of(begin(), end(), [](double value) -> bool { return !std::isnan(value); });
        }

       PointCoordinates mean_with(const PointCoordinates& another) const
       {
           PointCoordinates result(number_of_dimensions());
           std::transform(begin_, end_, another.begin_, result.begin_, [](double v1, double v2) { return (v1 + v2) / 2.0; });
           return result;
       }

      private:
        std::vector<double> data_;
        double* begin_;
        double* end_;

    }; // class PointCoordinates

    inline std::string to_string(const PointCoordinates& coord, size_t precision = 32)
    {
        auto result = '{' + acmacs::to_string(coord.x(), precision) + ", " + acmacs::to_string(coord.y(), precision);
        if (coord.number_of_dimensions() == 3)
            result += ", " + to_string(coord.z(), precision);
        return result + '}';
    }

    inline std::ostream& operator<<(std::ostream& out, const PointCoordinates& coord) { return out << to_string(coord); }

    inline double distance2(const PointCoordinates& p1, const PointCoordinates& p2)
    {
        std::vector<double> diff(p1.number_of_dimensions());
        std::transform(p1.begin(), p1.end(), p2.begin(), diff.begin(), [](double v1, double v2) { return v1 - v2; });
        return std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
    }

    inline double distance(const PointCoordinates& p1, const PointCoordinates& p2) { return std::sqrt(distance2(p1, p2)); }

    inline PointCoordinates operator+(const PointCoordinates& p1, const PointCoordinates& p2) { PointCoordinates result(PointCoordinates::create_copy, p1); result += p2; return result; }
    inline PointCoordinates operator+(const PointCoordinates& p1, double val) { PointCoordinates result(PointCoordinates::create_copy, p1); result += val; return result; }
    inline PointCoordinates operator-(const PointCoordinates& p1, const PointCoordinates& p2) { PointCoordinates result(PointCoordinates::create_copy, p1); result -= p2; return result; }
    inline PointCoordinates operator-(const PointCoordinates& p1, double val) { PointCoordinates result(PointCoordinates::create_copy, p1); result += -val; return result; }
    inline PointCoordinates operator*(const PointCoordinates& p1, double val) { PointCoordinates result(PointCoordinates::create_copy, p1); result *= val; return result; }
    inline PointCoordinates operator/(const PointCoordinates& p1, double val) { PointCoordinates result(PointCoordinates::create_copy, p1); result /= val; return result; }

} // namespace acmacs

// ----------------------------------------------------------------------
/// Local Variables:
/// eval: (if (fboundp 'eu-rename-buffer) (eu-rename-buffer))
/// End:
<|endoftext|>


<commit_before>/*********************************************************************
*  ccea.h
*
*  CCEATest is a set of unit tests for the CCEA library.
*
*  Copyright (C) 2016 Eric Klinkhammer
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program 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 General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include <iostream>
#include <vector>

#include "ccea.h"

class CCEATest : public::testing::Test {
  protected:

  CCEA ccea;

  // Used for unit tests. Will increment first element in scores by 1
  static void callback(std::vector<FANN::neural_net*>& nets, std::vector<double>& scores) {
    for (const auto net : nets) {
      scores.push_back(0);
    }
  }
};

TEST_F(CCEATest,testRunGeneration_popSizeConstant) {
  std::vector<std::vector<FANN::neural_net*> > initalPop = ccea.getPopulation();
  int popSizeInit = initalPop.size();
  int poolSizeInit = initalPop[0].size();

  ccea.runGeneration(callback);

  std::vector<std::vector<FANN::neural_net*> > afterPop = ccea.getPopulation();
  int popSizeAfter = afterPop.size();
  int poolSizeAfter = afterPop[0].size();

  EXPECT_EQ(popSizeInit, popSizeAfter);
  EXPECT_EQ(poolSizeInit, poolSizeAfter);
}

TEST_F(CCEATest,testRunGeneration_callsCallback) {

}

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc,argv);
  return RUN_ALL_TESTS();
}
<commit_msg>Creating model branch.<commit_after>/*********************************************************************
*  ccea_test.h
*
*  CCEATest is a set of unit tests for the CCEA library.
*
*  Copyright (C) 2016 Eric Klinkhammer
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program 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 General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include <iostream>
#include <vector>

#include "ccea.h"

class CCEATest : public::testing::Test {
  protected:

  CCEA ccea;

  // Used for unit tests. Will increment first element in scores by 1
  static void callback(std::vector<FANN::neural_net*>& nets, std::vector<double>& scores) {
    for (const auto net : nets) {
      scores.push_back(0);
    }
  }
};

TEST_F(CCEATest,testRunGeneration_popSizeConstant) {
  std::vector<std::vector<FANN::neural_net*> > initalPop = ccea.getPopulation();
  int popSizeInit = initalPop.size();
  int poolSizeInit = initalPop[0].size();

  ccea.runGeneration(callback);

  std::vector<std::vector<FANN::neural_net*> > afterPop = ccea.getPopulation();
  int popSizeAfter = afterPop.size();
  int poolSizeAfter = afterPop[0].size();

  EXPECT_EQ(popSizeInit, popSizeAfter);
  EXPECT_EQ(poolSizeInit, poolSizeAfter);
}

TEST_F(CCEATest,testRunGeneration_callsCallback) {

}

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc,argv);
  return RUN_ALL_TESTS();
}
<|endoftext|>