...
 
Commits (24)
############################################################################
# CMakeLists.txt
# Copyright (C) 2015 Belledonne Communications, Grenoble France
#
############################################################################
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
############################################################################
cmake_minimum_required(VERSION 3.0)
project(BELR VERSION 0.1.3 LANGUAGES C CXX)
set(BELR_SO_VERSION "1")
option(ENABLE_SHARED "Build shared library." ON)
option(ENABLE_STATIC "Build static library." ON)
option(ENABLE_STRICT "Build with strict compile options." YES)
option(ENABLE_TOOLS "Turn on or off compilation of tools." YES)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
if(NOT CPACK_GENERATOR AND NOT CMAKE_INSTALL_RPATH AND CMAKE_INSTALL_PREFIX)
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR})
message(STATUS "Setting install rpath to ${CMAKE_INSTALL_RPATH}")
endif()
include(GNUInstallDirs)
include(CheckSymbolExists)
include(CMakePushCheckState)
# find_package should be invoked here to check for libraries - however do NOT
# call include_directories here (see below)
if(LINPHONE_BUILDER_GROUP_EXTERNAL_SOURCE_PATH_BUILDERS)
set(BcToolbox_FIND_COMPONENTS tester)
include("${EP_bctoolbox_CONFIG_DIR}/BcToolboxConfig.cmake")
else()
find_package(BcToolbox 0.0.3 REQUIRED)
endif()
if(UNIX AND NOT APPLE)
include(CheckIncludeFiles)
check_include_files(libudev.h HAVE_LIBUDEV_H)
endif()
include_directories(
include/
src/
${CMAKE_CURRENT_BINARY_DIR}
)
if(MSVC)
include_directories(${MSVC_INCLUDE_DIR})
endif()
set(STRICT_OPTIONS_CPP )
set(STRICT_OPTIONS_C )
set(STRICT_OPTIONS_CXX )
set(STRICT_OPTIONS_OBJC )
if(MSVC)
if(ENABLE_STRICT)
list(APPEND STRICT_OPTIONS_CPP "/WX")
endif()
else()
list(APPEND STRICT_OPTIONS_CPP "-Wall" "-Wuninitialized" "-Wno-error=deprecated-declarations")
list(APPEND STRICT_OPTIONS_C "-Wdeclaration-after-statement" "-Wstrict-prototypes" "-Wno-error=strict-prototypes")
list(APPEND STRICT_OPTIONS_CXX "-std=c++11")
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
list(APPEND STRICT_OPTIONS_CPP "-Qunused-arguments" "-Wno-array-bounds")
endif()
if(APPLE)
list(APPEND STRICT_OPTIONS_CPP "-Wno-error=unknown-warning-option" "-Qunused-arguments" "-Wno-tautological-compare" "-Wno-unused-function" "-Wno-array-bounds")
list(APPEND STRICT_OPTIONS_CXX "-stdlib=libc++")
endif()
if(ENABLE_STRICT)
list(APPEND STRICT_OPTIONS_CPP "-Werror" "-Wextra" "-Wno-unused-parameter" "-fno-strict-aliasing")
endif()
endif()
if(STRICT_OPTIONS_CPP)
list(REMOVE_DUPLICATES STRICT_OPTIONS_CPP)
endif()
if(STRICT_OPTIONS_C)
list(REMOVE_DUPLICATES STRICT_OPTIONS_C)
endif()
set(BELR_CPPFLAGS ${BCTOOLBOX_CPPFLAGS})
if(ENABLE_STATIC)
list(APPEND BELR_CPPFLAGS "-DBELR_STATIC")
endif()
if(BELR_CPPFLAGS)
list(REMOVE_DUPLICATES BELR_CPPFLAGS)
add_definitions(${BELR_CPPFLAGS})
endif()
add_definitions("-DBELR_EXPORTS")
if(LINPHONE_BUILDER_GROUP_EXTERNAL_SOURCE_PATH_BUILDERS)
set(EXPORT_TARGETS_NAME "LinphoneBuilder")
else()
set(EXPORT_TARGETS_NAME "belr")
endif()
add_subdirectory(include)
add_subdirectory(src)
if(ENABLE_TOOLS)
add_subdirectory(tools)
endif()
include(CMakePackageConfigHelpers)
export(EXPORT ${EXPORT_TARGETS_NAME}Targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/BelrTargets.cmake"
)
configure_file(cmake/BelrConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/BelrConfig.cmake"
@ONLY
)
set(ConfigPackageLocation share/Belr/cmake)
install(EXPORT ${EXPORT_TARGETS_NAME}Targets
FILE BelrTargets.cmake
DESTINATION ${ConfigPackageLocation}
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/BelrConfig.cmake"
DESTINATION ${ConfigPackageLocation}
)
add_subdirectory(build)
############################################################################
# CMakeLists.txt
# Copyright (C) 2015 Belledonne Communications, Grenoble France
#
############################################################################
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
############################################################################
cmake_minimum_required(VERSION 3.0)
project(BELR VERSION 0.1.3 LANGUAGES C CXX)
set(BELR_SO_VERSION "1")
option(ENABLE_SHARED "Build shared library." ON)
option(ENABLE_STATIC "Build static library." ON)
option(ENABLE_STRICT "Build with strict compile options." YES)
option(ENABLE_TOOLS "Turn on or off compilation of tools." YES)
option(ENABLE_UNIT_TESTS "Enable compilation of unit tests." YES)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
if(NOT CPACK_GENERATOR AND NOT CMAKE_INSTALL_RPATH AND CMAKE_INSTALL_PREFIX)
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR})
message(STATUS "Setting install rpath to ${CMAKE_INSTALL_RPATH}")
endif()
include(GNUInstallDirs)
include(CheckSymbolExists)
include(CMakePushCheckState)
# find_package should be invoked here to check for libraries - however do NOT
# call include_directories here (see below)
if(LINPHONE_BUILDER_GROUP_EXTERNAL_SOURCE_PATH_BUILDERS)
set(BcToolbox_FIND_COMPONENTS tester)
include("${EP_bctoolbox_CONFIG_DIR}/BcToolboxConfig.cmake")
else()
find_package(BcToolbox 0.0.3 REQUIRED OPTIONAL_COMPONENTS tester) #DAMN POSTIVE
endif()
if(UNIX AND NOT APPLE)
include(CheckIncludeFiles)
check_include_files(libudev.h HAVE_LIBUDEV_H)
endif()
include_directories(
include/
src/
${CMAKE_CURRENT_BINARY_DIR}
)
if(MSVC)
include_directories(${MSVC_INCLUDE_DIR})
endif()
set(STRICT_OPTIONS_CPP )
set(STRICT_OPTIONS_C )
set(STRICT_OPTIONS_CXX )
set(STRICT_OPTIONS_OBJC )
if(MSVC)
if(ENABLE_STRICT)
list(APPEND STRICT_OPTIONS_CPP "/WX")
endif()
else()
list(APPEND STRICT_OPTIONS_CPP "-Wall" "-Wuninitialized" "-Wno-error=deprecated-declarations")
list(APPEND STRICT_OPTIONS_C "-Wdeclaration-after-statement" "-Wstrict-prototypes" "-Wno-error=strict-prototypes")
list(APPEND STRICT_OPTIONS_CXX "-std=c++11")
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
list(APPEND STRICT_OPTIONS_CPP "-Qunused-arguments" "-Wno-array-bounds")
endif()
if(APPLE)
list(APPEND STRICT_OPTIONS_CPP "-Wno-error=unknown-warning-option" "-Qunused-arguments" "-Wno-tautological-compare" "-Wno-unused-function" "-Wno-array-bounds")
list(APPEND STRICT_OPTIONS_CXX "-stdlib=libc++")
endif()
if(ENABLE_STRICT)
list(APPEND STRICT_OPTIONS_CPP "-Werror" "-Wextra" "-Wno-unused-parameter" "-fno-strict-aliasing")
endif()
endif()
if(STRICT_OPTIONS_CPP)
list(REMOVE_DUPLICATES STRICT_OPTIONS_CPP)
endif()
if(STRICT_OPTIONS_C)
list(REMOVE_DUPLICATES STRICT_OPTIONS_C)
endif()
set(BELR_CPPFLAGS ${BCTOOLBOX_CPPFLAGS})
if(ENABLE_STATIC)
list(APPEND BELR_CPPFLAGS "-DBELR_STATIC")
endif()
if(BELR_CPPFLAGS)
list(REMOVE_DUPLICATES BELR_CPPFLAGS)
add_definitions(${BELR_CPPFLAGS})
endif()
add_definitions("-DBELR_EXPORTS")
if(LINPHONE_BUILDER_GROUP_EXTERNAL_SOURCE_PATH_BUILDERS)
set(EXPORT_TARGETS_NAME "LinphoneBuilder")
else()
set(EXPORT_TARGETS_NAME "belr")
endif()
add_subdirectory(include)
add_subdirectory(src)
if(ENABLE_UNIT_TESTS AND BCTOOLBOX_TESTER_FOUND)
add_subdirectory(tester)
endif()
if(ENABLE_TOOLS)
add_subdirectory(tools)
endif()
include(CMakePackageConfigHelpers)
export(EXPORT ${EXPORT_TARGETS_NAME}Targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/BelrTargets.cmake"
)
configure_file(cmake/BelrConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/BelrConfig.cmake"
@ONLY
)
set(ConfigPackageLocation share/Belr/cmake)
install(EXPORT ${EXPORT_TARGETS_NAME}Targets
FILE BelrTargets.cmake
DESTINATION ${ConfigPackageLocation}
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/BelrConfig.cmake"
DESTINATION ${ConfigPackageLocation}
)
add_subdirectory(build)
......@@ -10,4 +10,4 @@ EXTRA_DIST= \
belr.pc.in \
README.md
SUBDIRS=src include tools
SUBDIRS=src include tools tester
......@@ -54,6 +54,19 @@ fi
AC_SUBST(BCTOOLBOX_LIBS)
PKG_CHECK_MODULES(BCTOOLBOX, [bctoolbox-tester],[libbctoolbox_tester_found=yes],foo=bar)
if test "$libbctoolbox_tester_found" != "yes" ; then
dnl Check the lib presence in case the PKG-CONFIG version is not found
AC_LANG_CPLUSPLUS
AC_CHECK_LIB(bctoolbox-tester, main, [BCTOOLBOXTESTER_LIBS+=" -lbctoolbox-tester"; libbctoolbox_tester_found=yes], [foo=bar])
AC_LANG_C
fi
if test "$libbctoolbox_tester_found" != "yes" ; then
AC_MSG_ERROR([libbctoolbox-tester not found. Install it and try again])
fi
AC_SUBST(BCTOOLBOXTESTER_LIBS)
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
......@@ -82,6 +95,7 @@ AC_CONFIG_FILES(
Makefile
src/Makefile
tools/Makefile
tester/Makefile
include/Makefile
include/belr/Makefile
belr.pc
......
......@@ -22,6 +22,8 @@
#include <list>
#include <map>
#include <memory>
#include <iostream>
#include <vector>
#include <string>
// =============================================================================
......@@ -54,11 +56,14 @@ struct TransitionMap{
bool mPossibleChars[256];
};
class RecognizerPointer;
class Recognizer : public std::enable_shared_from_this<Recognizer>{
public:
virtual ~Recognizer() = default;
void setName(const std::string &name);
void setOnlyName(const std::string& name);
void setId(int Id);
const std::string &getName()const;
BELR_PUBLIC size_t feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
unsigned int getId()const{
......@@ -67,6 +72,14 @@ public:
bool getTransitionMap(TransitionMap *mask);
void optimize();
void optimize(int recursionLevel);
virtual ~Recognizer() { }
virtual void save(std::ofstream& fichier, long &savePos)=0;
virtual bool equal(const std::shared_ptr<Recognizer> &CR)=0;
virtual void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd)=0;
virtual bool operator==(Recognizer &CR)=0;
protected:
Recognizer() = default;
......@@ -81,16 +94,29 @@ protected:
class CharRecognizer : public Recognizer{
public:
CharRecognizer(int to_recognize, bool caseSensitive=false);
private:
void save(std::ofstream& fichier, long &savePos);
bool equal(const std::shared_ptr<Recognizer> &CR);
static std::shared_ptr<CharRecognizer> loadVect(std::vector<std::string>::const_iterator &inIter);
void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool operator==(Recognizer &CR);
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
int mToRecognize;
bool mCaseSensitive;
};
class Selector : public Recognizer{
public:
std::shared_ptr<Selector> addRecognizer(const std::shared_ptr<Recognizer> &element);
void save(std::ofstream& fichier, long &savePos);
bool equal(const std::shared_ptr<Recognizer> &SEL);
static std::shared_ptr<Selector> loadVect(std::vector<std::string>::const_iterator &inIter, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool operator==(Recognizer &CR);
protected:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
......@@ -102,6 +128,15 @@ protected:
/**This is an optimization of the first one for the case where there can be only a single match*/
class ExclusiveSelector : public Selector{
public:
ExclusiveSelector();
void save(std::ofstream& fichier, long &savePos);
static std::shared_ptr<ExclusiveSelector> loadVect(std::vector<std::string>::const_iterator &inIter, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool equal(const std::shared_ptr<Recognizer> &CR);
void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool operator==(Recognizer &SEQ);
private:
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
};
......@@ -110,6 +145,13 @@ class Sequence : public Recognizer{
public:
std::shared_ptr<Sequence> addRecognizer(const std::shared_ptr<Recognizer> &element);
virtual bool _getTransitionMap(TransitionMap *mask);
void save(std::ofstream& fichier, long &savePos);
static std::shared_ptr<Sequence> loadVect(std::vector<std::string>::const_iterator &inIter, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool equal(const std::shared_ptr<Recognizer> &CR);
void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool operator==(Recognizer &SEQ);
protected:
virtual void _optimize(int recursionLevel);
private:
......@@ -121,6 +163,14 @@ class Loop : public Recognizer{
public:
std::shared_ptr<Loop> setRecognizer(const std::shared_ptr<Recognizer> &element, int min=0, int max=-1);
virtual bool _getTransitionMap(TransitionMap *mask);
void save(std::ofstream& fichier, long &savePos); //MODIFIE PAR IYED
bool equal(const std::shared_ptr<Recognizer> &CR); //MODIFIE PAR IYED
static std::shared_ptr<Loop> loadVect(std::vector<std::string>::const_iterator &inIter, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool operator==(Recognizer &SEQ);
protected:
virtual void _optimize(int recursionLevel);
private:
......@@ -143,6 +193,14 @@ public:
class CharRange : public Recognizer{
public:
CharRange(int begin, int end);
void save(std::ofstream& fichier, long &savePos);
static std::shared_ptr<CharRange> loadVect(std::vector<std::string>::const_iterator &inIter);
bool equal(const std::shared_ptr<Recognizer> &CR);
void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool operator==(Recognizer &SEQ);
private:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
......@@ -153,6 +211,13 @@ class Literal : public Recognizer{
public:
Literal(const std::string &lit);
virtual bool _getTransitionMap(TransitionMap *mask);
void save(std::ofstream& fichier, long &savePos);
static std::shared_ptr<Literal> loadVect(std::vector<std::string>::const_iterator &inIter);
bool equal(const std::shared_ptr<Recognizer> &CR);
void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool operator==(Recognizer &SEQ);
private:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
......@@ -168,8 +233,15 @@ public:
class RecognizerPointer : public Recognizer{
public:
RecognizerPointer();
std::shared_ptr<Recognizer> getPointed();
void setPointed(const std::shared_ptr<Recognizer> &r);
void save(std::ofstream& fichier, long &savePos);
static std::shared_ptr<RecognizerPointer> loadVect(std::vector<std::string>::const_iterator &inIter, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool equal(const std::shared_ptr<Recognizer> &CR);
void linkPointer(std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterBegin, std::list<std::shared_ptr<RecognizerPointer>>::iterator &rcptrIterEnd);
bool operator==(Recognizer &SEQ);
private:
virtual void _optimize(int recursionLevel);
virtual size_t _feed(const std::shared_ptr<ParserContextBase> &ctx, const std::string &input, size_t pos);
......@@ -248,6 +320,18 @@ public:
* Return the number of rules in this grammar.
**/
int getNumRules()const;
//Save functions
void createGrammarDump(std::string dumpFileName);
void saveRulesMap(std::ofstream &outFile);
void savePointersList(std::ofstream &outFile);
//Load function
BELR_PUBLIC static std::shared_ptr<Grammar> loadVectRulesMap(std::string fileName);
//comare functions
bool equal(std::shared_ptr<Grammar> &gramCompared);
private:
void assignRule(const std::string &name, const std::shared_ptr<Recognizer> &rule);
void _extendRule(const std::string &name, const std::shared_ptr<Recognizer> &rule);
......
//
// profiler.h
// Quick and dirty profiler
//
// Created by Niels Gabel on 9/1/08.
//
// Copyright 2008-2011 Niels Gabel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// To use:
// At function start, call ProfilerEnter() and pass your function name.
// Call ProfilerEnd() and pass your function name before your function returns
// ProfilerInit() installs an atexit() handler and will dump the profiler results to file
// when the program exits...
#if defined( PROFILING ) && PROFILING
# warning Profiling ON!
#import <pthread.h>
struct ProfilerDataCounter
{
struct ProfilerDataCounter * parent ;
clock_t func_time ;
unsigned long count ;
} ;
struct ProfilerData
{
char * name ;
struct ProfilerDataCounter counters[10] ;
} ;
extern pthread_key_t gProfilerDataPThreadKey ;
extern struct ProfilerData gProfilerTable[100] ;
extern struct ProfilerData * gProfilerTableLastUsedEntry ;
extern
#ifdef __cplusplus
"C"
#endif
void _ProfilerInit() ;
extern
#ifdef __cplusplus
"C"
#endif
void ProfilerOutputTree( FILE * file ) ;
//extern clock_t gProfilerOverhead ;
//extern unsigned long gProfilerCallCount ;
#define ProfilerGetCurrent() (struct ProfilerDataCounter*)pthread_getspecific( gProfilerDataPThreadKey )
#define ProfilerSetCurrent( __profiler_data ) pthread_setspecific( gProfilerDataPThreadKey, (const void*)__profiler_data )
#define ProfilerStackPush( ) \
struct ProfilerDataCounter * __profiler_parent = ProfilerGetCurrent() ; \
struct ProfilerDataCounter * __profiler_counter = & __profiler_data->counters[ 0 ] ; \
if ( __profiler_parent ) { \
while( __profiler_counter < & __profiler_data->counters[ 10 ] ) { \
++__profiler_counter ; \
if ( __profiler_counter->parent == __profiler_parent ) { break ; } \
else if ( !__profiler_counter->parent ) { \
__profiler_counter->parent = __profiler_parent ; \
__profiler_counter->func_time = 0 ; \
__profiler_counter->count = 0 ; \
break ; \
} \
} \
} \
ProfilerSetCurrent( __profiler_counter ) ;
#define ProfilerStackPop() ProfilerSetCurrent( __profiler_parent ) ;
#define ProfilerEnter( __function_name ) \
static struct ProfilerData * __profiler_data = NULL ; \
clock_t __profiler_start_time = clock() ; \
if ( !__profiler_data ) { __profiler_data = gProfilerTableLastUsedEntry++ ; __profiler_data->name = (char*)__function_name ; } \
ProfilerStackPush() ;
#define ProfilerExit( __function_name )\
__profiler_counter->func_time += clock() - __profiler_start_time ;\
++__profiler_counter->count ;\
ProfilerStackPop() ;
#define PSTART ProfilerEnter(__PRETTY_FUNCTION__) @try {{
#define PEND }} @finally { ProfilerExit( __PRETTY_FUNCTION__ ) ; }
#else
#define ProfilerEnter( __function_name ) {}
#define ProfilerExit( __function_name ) {}
#define PSTART
#define PEND
#endif
//
// profiler.m
// Quick and dirty profiler
//
// Created by Niels Gabel on 9/1/08.
//
// Copyright 208-2011 Niels Gabel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "profiler.h"
#if defined( PROFILING ) && PROFILING
#ifndef __has_feature
// not LLVM Compiler
#define __has_feature(x) 0
#endif
#if __has_feature(objc_arc)
# ifndef ARC
# define ARC 1
# endif
#else
#endif
/*
To use:
At function start, call ProfilerEnter() and pass your function name.
Call ProfilerExit() and pass your function name before your function returns
ProfilerInit() installs an atexit() handler and will dump the profiler results to file
when the program exits...
Define PROFILER_OUTPUT_TO_FILE if you want to append profiler output to "profiler_output.txt"
in your app's Documents folder. You can retrieve the file using the Organizer in Xcode.
(Click on your device, find it in the Applications list, turn down the disclosure triangle,
and click the download arrow on the right.)
If PROFILER_OUTPUT_TO_FILE is not defined, output written to stdout.
*/
//#import "utils.h"
//#import "debug.h"
#import <signal.h>
pthread_key_t gProfilerDataPThreadKey = 0 ;
struct ProfilerData gProfilerTable[100] ;
struct ProfilerData * gProfilerTableLastUsedEntry = & gProfilerTable[0] ;
static NSString * GetDocumentsFolderPath()
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
//clock_t gProfilerOverhead = 0 ;
//unsigned long gProfilerCallCount = 0 ;
static void ProfilerOutputTreeNode( FILE * file, struct ProfilerData * data, struct ProfilerDataCounter * counter, int indent )
{
{
float time = (float)counter->func_time / (float)CLOCKS_PER_SEC ;
fprintf( file, "%*s%-*s %15.5f %15lu %10.5f\n", indent * 4, "", 80 - (indent * 4 ), data->name, time, counter->count, time / (float)counter->count ) ;
}
for( struct ProfilerData * testData = & gProfilerTable[0]; testData < gProfilerTableLastUsedEntry; ++testData )
{
if ( !testData->name )
{
break ;
}
if ( testData != data )
{
for( struct ProfilerDataCounter * testCounter = & testData->counters[1] ; testCounter < &testData->counters[10]; ++testCounter )
{
if ( !testCounter->parent )
{
break ;
}
if ( testCounter->parent == counter )
{
ProfilerOutputTreeNode( file, testData, testCounter, indent + 1 ) ;
}
}
}
}
}
void
ProfilerOutputTree( FILE * file )
{
fprintf( file, "%-80s %15s %15s %10s\n", "FUNCTION", "TOTAL TIME", "COUNT", "AVG. TIME") ;
for( struct ProfilerData * data = & gProfilerTable[0]; data < gProfilerTableLastUsedEntry; ++data )
{
if ( data->counters[0].count > 0 )
{
ProfilerOutputTreeNode( file, data, & data->counters[0], 0 ) ;
}
}
}
static void
ProfilerExitFunction()
{
DebugLog(@"%s\n", __PRETTY_FUNCTION__ ) ;
@autoreleasepool {
#ifdef PROFILER_OUTPUT_TO_FILE
FILE * file = fopen( [ [ GetDocumentsFolderPath() stringByAppendingPathComponent:@"profiler_output.txt" ] UTF8String ], "a" ) ;
if ( !file )
{
file = stdout ;
}
#else
FILE * file = stdout ;
#endif
fprintf( file, "%s %s\n", __DATE__, __TIME__ ) ;
// {
// float time = (float)gProfilerOverhead / (float)CLOCKS_PER_SEC ;
//
// fprintf( file, "profiler time used: %.5f\n", time ) ;
// fprintf( file, "profiler avg. time: %.2f\n", time / (float)gProfilerCallCount ) ;
// }
ProfilerOutputTree( file ) ;
}
}
static void ProfilerInterruptSignalHandler( int signal )
{
DebugLog(@"%s\n", __PRETTY_FUNCTION__ ) ;
ProfilerExitFunction() ;
}
struct ProfilerInit
{
ProfilerInit()
{
@autoreleasepool {
DebugLog( @"Using profiling...\n" ) ;
DebugAssert( 0 == atexit( ProfilerExitFunction ) ) ;
DebugAssert( 0 == pthread_key_create( & gProfilerDataPThreadKey, NULL ) ) ;
{
const struct sigaction act =
{
{ ProfilerInterruptSignalHandler }, 0, 0
} ;
sigaction( SIGKILL, & act, NULL ) ;
sigaction( SIGINT, & act, NULL ) ;
}
bzero( gProfilerTable, sizeof( gProfilerTable ) ) ;
gProfilerTableLastUsedEntry = & gProfilerTable[ 0 ] ;
}
}
} ;
static struct ProfilerInit gProfilerInit = ProfilerInit() ;
#endif
\ No newline at end of file
This diff is collapsed.
############################################################################
# CMakeLists.txt
# Copyright (C) 2015 Belledonne Communications, Grenoble France
#
############################################################################
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
############################################################################
if(ENABLE_SHARED)
set(BELR_LIBRARIES_FOR_TESTER belr)
else()
set(BELR_LIBRARIES_FOR_TESTER belr-static)
endif()
set(GRAMMAR_FILES
grammars/basicgrammar.txt
grammars/vcardgrammar.txt
grammars/sipgrammar.txt
)
set(HEADER_FILES_CXX belr-tester.hpp)
set(SOURCE_FILES_CXX
belr-tester.cpp
belr-charrecognizer-tester.cpp
belr-selector-tester.cpp
belr-exclusiveselector-tester.cpp
belr-sequence-tester.cpp
belr-loop-tester.cpp
belr-charrange-tester.cpp
belr-literal-tester.cpp
belr-recognizerpointer-tester.cpp
belr-basicgrammar-tester.cpp
belr-vcardgrammar-tester.cpp
belr-sipgrammar-tester.cpp
)
bc_apply_compile_flags(SOURCE_FILES_CXX STRICT_OPTIONS_CPP STRICT_OPTIONS_CXX)
add_executable(belr_tester ${SOURCE_FILES_CXX} ${HEADER_FILES_CXX})
set_target_properties(belr_tester PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(belr_tester PUBLIC ${BCTOOLBOX_TESTER_INCLUDE_DIRS})
target_link_libraries(belr_tester ${BELR_LIBRARIES_FOR_TESTER} ${BCTOOLBOX_TESTER_LIBRARIES})
if(APPLE)
set_target_properties(belr_tester PROPERTIES LINK_FLAGS "-stdlib=libc++")
endif()
if(NOT IOS)
install(TARGETS belr_tester
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)
endif()
install(FILES ${GRAMMAR_FILES} DESTINATION "${CMAKE_INSTALL_DATADIR}/belr_tester/grammars")
EXTRA_DIST=grammars/basicgrammar.txt grammars/vcardgrammar.txt grammars/sipgrammar.txt
noinst_PROGRAMS=belr-tester
belr_tester_SOURCES=belr-tester.cpp \
belr-charrecognizer-tester.cpp \
belr-selector-tester.cpp \
belr-exclusiveselector-tester.cpp \
belr-sequence-tester.cpp \
belr-loop-tester.cpp \
belr-charrange-tester.cpp \
belr-literal-tester.cpp \
belr-recognizerpointer-tester.cpp \
belr-basicgrammar-tester.cpp \
belr-vcardgrammar-tester.cpp \
belr-sipgrammar-tester.cpp
belr_tester_LDADD=$(top_builddir)/src/libbelr.la -lbcunit $(BCTOOLBOX_LIBS) $(BCTOOLBOXTESTER_LIBS)
belr_tester_CFLAGS=\
-DBC_CONFIG_FILE=\"config.h\" \
$(BCUNIT_CFLAGS)
belr_tester_CXXFLAGS=$(CXXFLAGS)
belr_tester_AM_CPPFLAGS=-I$(top_srcdir)/include \
-I$(top_srcdir)/tester \
-I$(top_srcdir)/tester/common
AM_CPPFLAGS=-I$(top_srcdir)/include
/*
belr-basicgrammar-tester.cpp
Copyright (C) 2015 Belledonne Communications SARL
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 "belr-tester.hpp"
using namespace::std;
using namespace::belr;
static string openFile(const char *name) {
ifstream istr(name, std::ios::binary);
if (!istr.is_open()) {
BC_FAIL(name);
}
stringstream vcardStream;
vcardStream << istr.rdbuf();
string vcard = vcardStream.str();
return vcard;
}
static void basicgrammar_save(void) {
string grammarToParse = bc_tester_res("grammars/basicgrammar.txt");
string grammarDump = bc_tester_file("grammarDump.bin");
string grammarDump2 = bc_tester_file("grammarDump2.bin");
remove(grammarDump.c_str());
remove(grammarDump2.c_str());
ABNFGrammarBuilder builder;
//Read grammar put it in object grammar
auto start = std::chrono::high_resolution_clock::now();
shared_ptr<Grammar> grammar=builder.createFromAbnfFile(grammarToParse, make_shared<CoreRules>());
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsedFirst = finish - start;
BC_ASSERT_FALSE(!grammar);
//Save grammar
grammar->createGrammarDump(grammarDump);
//Load grammar
start = std::chrono::high_resolution_clock::now();
shared_ptr<Grammar> loadedGram = Grammar::loadVectRulesMap(grammarDump);
finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsedSecond = finish - start;
BC_ASSERT_FALSE(!loadedGram);
loadedGram->createGrammarDump(grammarDump2);
string g1 = openFile(grammarDump.c_str());
string g2 = openFile(grammarDump2.c_str());
shared_ptr<Grammar> loadedGram2 = Grammar::loadVectRulesMap(grammarDump2);
BC_ASSERT_STRING_EQUAL(g1.c_str(), g2.c_str());
BC_ASSERT_TRUE(grammar->equal(loadedGram));
BC_ASSERT_TRUE(loadedGram->equal(loadedGram2));
BC_ASSERT_TRUE(elapsedFirst.count() > elapsedSecond.count());
// cout << "Optimisation ratio : " << (elapsedFirst.count() / elapsedSecond.count())*100 << "%"<< endl;
}
static test_t tests[] = {
TEST_NO_TAG("BASICGRAMMAR", basicgrammar_save),
};
test_suite_t grammar_simple_test_suite = {
"BasicGrammar Save and Load",
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
/*
belr-charrange-tester.cpp
Copyright (C) 2015 Belledonne Communications SARL
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 "belr-tester.hpp"
using namespace::std;
using namespace::belr;
extern std::vector<std::string> split(const std::string &s, char delim);
static void char4_charrange(void) {
long savePosition = 0;
//Create two charrecognizers
const shared_ptr<CharRange> stringBasic = dynamic_pointer_cast<CharRange>(Utils::char_range(97, 100));
BC_ASSERT_EQUAL(stringBasic->feed(NULL, "a", 0), 1, int, "%d");
BC_ASSERT_EQUAL(stringBasic->feed(NULL, "b", 0), 1, int, "%d");
BC_ASSERT_EQUAL(stringBasic->feed(NULL, "c", 0), 1, int, "%d");
BC_ASSERT_EQUAL(stringBasic->feed(NULL, "d", 0), 1, int, "%d");
//SAVE AND LOAD
ofstream ofichier ("test-char_range.bin", ios::out | ios::app);
stringBasic->save(ofichier, savePosition);
ofichier.close();
ifstream ifichier("test-char_range.bin", ios::in);
string contenu; // déclaration d'une chaîne qui contiendra la ligne lue
getline(ifichier, contenu); // on met dans "contenu" la ligne
std::vector<std::string> result = split(contenu, ' ');
vector<string>::const_iterator i = result.begin();
const shared_ptr<CharRange> stringBasicLoaded = CharRange::loadVect(i);
ifichier.close();
remove("test-char_range.bin");
BC_ASSERT_TRUE(stringBasic->equal(stringBasicLoaded));
BC_ASSERT_EQUAL(stringBasicLoaded->feed(NULL, "a",0), 1, int, "%d");
BC_ASSERT_EQUAL(stringBasicLoaded->feed(NULL, "b",0), 1, int, "%d");
BC_ASSERT_EQUAL(stringBasicLoaded->feed(NULL, "c",0), 1, int, "%d");
BC_ASSERT_EQUAL(stringBasicLoaded->feed(NULL, "d",0), 1, int, "%d");
}
static test_t tests[] = {
TEST_NO_TAG("4CHAR", char4_charrange),
};
test_suite_t recognizer_charrange_test_suite = {
"CharRange",
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
/*
belr-charrecognizer-tester.cpp
Copyright (C) 2015 Belledonne Communications SARL
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 "belr-tester.hpp"
using namespace::std;
using namespace::belr;
extern std::vector<std::string> split(const std::string &s, char delim);
static void char2_charrecognizer(void) {
long savePosition = 0;
shared_ptr<CharRecognizer> character = Foundation::charRecognizer('b', false);
shared_ptr<CharRecognizer> character2 = Foundation::charRecognizer('a', false);
//feeding the original recognizer
BC_ASSERT_EQUAL(character->feed(NULL, "b", 0), 1, int, "%d");
BC_ASSERT_EQUAL(character2->feed(NULL, "a", 0), 1, int, "%d");
//SAVE
ofstream ofichier ("test-char.bin", ios::out | ios::app | ios::binary);
character->save(ofichier, savePosition);
character2->save(ofichier, savePosition);
ofichier.close();
//LOAD
ifstream ifichier ("test-char.bin", ios::in | ios::binary);
string content;
getline(ifichier, content);
std::vector<std::string> result = split(content, ' ');
vector<string>::const_iterator i = result.begin();
const shared_ptr<CharRecognizer> characterLoaded = CharRecognizer::loadVect(i);
const shared_ptr<CharRecognizer> characterLoaded2 = CharRecognizer::loadVect(i);
ifichier.close();
remove("test-char.bin");
//verify if the two recognizers are equal
BC_ASSERT_TRUE(character->equal(characterLoaded));
BC_ASSERT_TRUE(character2->equal(characterLoaded2));
//feeding the loaded recognizer
BC_ASSERT_EQUAL(characterLoaded->feed(NULL, "b", 0), 1, int, "%d");
BC_ASSERT_EQUAL(characterLoaded2->feed(NULL, "a", 0), 1, int, "%d");
}
static test_t tests[] = {
TEST_NO_TAG("2CHAR", char2_charrecognizer),
};
test_suite_t recognizer_char_test_suite = {
"CharRecognizer",
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
This diff is collapsed.
/*
belr-literal-tester.cpp
Copyright (C) 2015 Belledonne Communications SARL
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 "belr-tester.hpp"
using namespace::std;
using namespace::belr;
extern std::vector<std::string> split(const std::string &s, char delim);
static void char4_literal(void) {
long savePosition = 0;
const shared_ptr<Literal> chaine = dynamic_pointer_cast<Literal>(Utils::literal("abcd"));
BC_ASSERT_EQUAL(chaine->feed(NULL, "abcd", 0) ,4, int, "%d");
ofstream ofichier ("test-lit.bin", ios::out | ios::app);
chaine->save(ofichier ,savePosition);
ofichier.close();
ifstream ifichier ("test-lit.bin", ios::in);
string content;
getline(ifichier, content);
std::vector<std::string> result = split(content, ' ');
vector<string>::const_iterator i = result.begin();
const shared_ptr<Literal> chaine_loaded = Literal::loadVect(i);
ifichier.close();
remove("test-lit.bin");
BC_ASSERT_TRUE(chaine->equal(chaine_loaded));
BC_ASSERT_EQUAL(chaine_loaded->feed(NULL, "abcd",0) ,4,int ,"%d");
}
static test_t tests[] = {
TEST_NO_TAG("4CHAR", char4_literal),
};
test_suite_t recognizer_literal_test_suite = {
"Literal",
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*
belr-sipgrammar-tester.cpp
Copyright (C) 2015 Belledonne Communications SARL
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 "belr-tester.hpp"
using namespace::std;
using namespace::belr;
static string openFile(const char *name) {
ifstream istr(name, std::ios::binary);
if (!istr.is_open()) {
BC_FAIL(name);
}
stringstream vcardStream;
vcardStream << istr.rdbuf();
string vcard = vcardStream.str();
return vcard;
}
static void sipgrammar_save(void) {
string grammarToParse = bc_tester_res("grammars/sipgrammar.txt");
string grammarDump = bc_tester_file("grammarDump.bin");
string grammarDump2 = bc_tester_file("grammarDump2.bin");
remove(grammarDump.c_str());
remove(grammarDump2.c_str());
ABNFGrammarBuilder builder;
//Read grammar put it in object grammar
auto start = std::chrono::high_resolution_clock::now();
shared_ptr<Grammar> grammar=builder.createFromAbnfFile(grammarToParse, make_shared<CoreRules>());
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsedFirst = finish - start;
BC_ASSERT_FALSE(!grammar);
//Save grammar
grammar->createGrammarDump(grammarDump);
//Load grammar
start = std::chrono::high_resolution_clock::now();
shared_ptr<Grammar> loadedGram = Grammar::loadVectRulesMap(grammarDump);
finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsedSecond = finish - start;
BC_ASSERT_FALSE(!loadedGram);
loadedGram->createGrammarDump(grammarDump2);
string g1 = openFile(grammarDump.c_str());
string g2 = openFile(grammarDump2.c_str());
BC_ASSERT_STRING_EQUAL(g1.c_str(), g2.c_str());
BC_ASSERT_TRUE(grammar->equal(loadedGram));
BC_ASSERT_TRUE(elapsedFirst.count() > elapsedSecond.count());
// cout << "Optimisation ratio : " << (elapsedFirst.count() / elapsedSecond.count())*100 << "%"<< endl;
}
static test_t tests[] = {
TEST_NO_TAG("SIPGRAMMAR", sipgrammar_save),
};
test_suite_t grammar_sipgrammar_suite = {
"SipGrammar Save and Load",
NULL,
NULL,
NULL,
NULL,
sizeof(tests) / sizeof(tests[0]),
tests
};
/*
belcard-tester.cpp
Copyright (C) 2015 Belledonne Communications SARL
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 "belr-tester.hpp"
#include <bctoolbox/logging.h>
int main(int argc, char *argv[]) {
int i;
int ret;
belr_tester_init(NULL);
if (strstr(argv[0], ".libs")) {
int prefix_length = (int)(strstr(argv[0], ".libs") - argv[0]) + 1;
char prefix[200];
sprintf(prefix, "%s%.*s", argv[0][0] == '/' ? "" : "./", prefix_length, argv[0]);
bc_tester_set_resource_dir_prefix(prefix);
bc_tester_set_writable_dir_prefix(prefix);
}
for(i = 1; i < argc; ++i) {
int ret = bc_tester_parse_args(argc, argv, i);
if (ret>0) {
i += ret - 1;
continue;
} else if (ret<0) {
bc_tester_helper(argv[0], "");
}
return ret;
}
ret = bc_tester_start(argv[0]);
belr_tester_uninit();
return ret;
}
static void log_handler(int lev, const char *fmt, va_list args) {
#ifdef _WIN32
vfprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, fmt, args);
fprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, "\n");
#else
va_list cap;
va_copy(cap,args);
/* Otherwise, we must use stdio to avoid log formatting (for autocompletion etc.) */
vfprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, fmt, cap);
fprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, "\n");
va_end(cap);
#endif
}
void belr_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
if (ftester_printf == NULL) ftester_printf = log_handler;
bc_tester_init(ftester_printf, BCTBX_LOG_MESSAGE, BCTBX_LOG_ERROR, "grammars");
bc_tester_add_suite(&recognizer_char_test_suite);
bc_tester_add_suite(&recognizer_selector_test_suite);
bc_tester_add_suite(&recognizer_exclusiveselector_test_suite);
bc_tester_add_suite(&recognizer_sequence_test_suite);
bc_tester_add_suite(&recognizer_loop_test_suite);
bc_tester_add_suite(&recognizer_charrange_test_suite);
bc_tester_add_suite(&recognizer_literal_test_suite);
bc_tester_add_suite(&recognizer_recognizerpointer_test_suite);
bc_tester_add_suite(&grammar_simple_test_suite);
bc_tester_add_suite(&grammar_vcardgrammar_suite);
bc_tester_add_suite(&grammar_sipgrammar_suite);
}
void belr_tester_uninit(void) {
bc_tester_uninit();
}
template<typename Out>
void split(const std::string &s, char delim, Out result) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
/*
belr-tester.hpp
Copyright (C) 2015 Belledonne Communications SARL
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/>.
*/
#ifndef belr_tester_hpp
#define belr_tester_hpp
#include <bctoolbox/tester.h>
#include "belr/grammarbuilder.h"
#include "belr/abnf.h"
#include "belr/belr.h"
#include <fstream>
#include <string>
#include <memory>
#include <sstream>
#include <iostream>
#include <vector>
#include <chrono>
#ifdef __cplusplus
extern "C" {
#endif
extern test_suite_t recognizer_char_test_suite;
extern test_suite_t recognizer_selector_test_suite;
extern test_suite_t recognizer_exclusiveselector_test_suite;
extern test_suite_t recognizer_sequence_test_suite;
extern test_suite_t recognizer_loop_test_suite;
extern test_suite_t recognizer_charrange_test_suite;
extern test_suite_t recognizer_literal_test_suite;
extern test_suite_t recognizer_recognizerpointer_test_suite;
extern test_suite_t grammar_simple_test_suite;
extern test_suite_t grammar_vcardgrammar_suite;
extern test_suite_t grammar_sipgrammar_suite;
void belr_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args));
void belr_tester_uninit(void);
#ifdef __cplusplus
};
#endif
#endif
/*
belr-vcardgrammar-tester.cpp
Copyright (C) 2015 Belledonne Communications SARL
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 "belr-tester.hpp"
using namespace::std;
using namespace::belr;
static string openFile(const char *name) {
ifstream istr(name, std::ios::binary);
if (!istr.is_open()) {
BC_FAIL(name);
}
stringstream vcardStream;
vcardStream << istr.rdbuf();
string vcard = vcardStream.str();
return vcard;
}
static void vcardgrammar_save(void) {
string grammarToParse = bc_tester_res("grammars/vcardgrammar.txt");
string grammarDump = bc_tester_file("grammarDump.bin");
string grammarDump2 = bc_tester_file("grammarDump2.bin");
remove(grammarDump.c_str());
remove(grammarDump2.c_str());
ABNFGrammarBuilder builder;
//Read grammar put it in object grammar
auto start = std::chrono::high_resolution_clock::now();