Skip to content

Commit

Permalink
added unit test for #93
Browse files Browse the repository at this point in the history
added unit test for #93
cleanuo
  • Loading branch information
Ingo Berg committed Jun 7, 2021
1 parent a20d8a0 commit 6d8c4b8
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 165 deletions.
2 changes: 1 addition & 1 deletion include/muParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
9 changes: 5 additions & 4 deletions include/muParserBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down Expand Up @@ -176,6 +176,7 @@ namespace mu
const string_type& GetExpr() const;
const funmap_type& GetFunDef() const;
string_type GetVersion(EParserVersionInfo eInfo = pviFULL) const;
const ParserByteCode& GetByteCode() const;

const char_type** GetOprtDef() const;
void DefineNameChars(const char_type* a_szCharset);
Expand Down Expand Up @@ -214,9 +215,9 @@ namespace mu

explicit change_dec_sep(char_type cDecSep, char_type cThousandsSep = 0, int nGroup = 3)
:std::numpunct<TChar>()
, m_nGroup(nGroup)
, m_cDecPoint(cDecSep)
, m_cThousandsSep(cThousandsSep)
,m_nGroup(nGroup)
,m_cDecPoint(cDecSep)
,m_cThousandsSep(cThousandsSep)
{}

protected:
Expand Down
8 changes: 4 additions & 4 deletions include/muParserBytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down Expand Up @@ -50,14 +50,14 @@ namespace mu

union
{
struct //SValData
struct // SValData
{
value_type* ptr;
value_type data;
value_type data2;
} Val;

struct //SFunData
struct // SFunData
{
// Note: the type is erased in generic_callable_type and the signature of the
// function to call is tracked elsewhere in regard with the number of
Expand All @@ -68,7 +68,7 @@ namespace mu
int idx;
} Fun;

struct //SOprtData
struct // SOprtData
{
value_type* ptr;
int offset;
Expand Down
2 changes: 1 addition & 1 deletion include/muParserCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion include/muParserDLL.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion include/muParserDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion include/muParserError.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion include/muParserFixes.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion include/muParserInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion include/muParserTemplateMagic.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
47 changes: 37 additions & 10 deletions include/muParserTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down Expand Up @@ -179,35 +179,61 @@ namespace mu
}

// postfix operator callback
static value_type Mega(value_type a_fVal) { return a_fVal * (value_type)1e6; }
static value_type Micro(value_type a_fVal) { return a_fVal * (value_type)1e-6; }
static value_type Milli(value_type a_fVal) { return a_fVal / (value_type)1e3; }
static value_type Mega(value_type a_fVal)
{
return a_fVal * (value_type)1e6;
}

static value_type Micro(value_type a_fVal)
{
return a_fVal * (value_type)1e-6;
}

static value_type Milli(value_type a_fVal)
{
return a_fVal / (value_type)1e3;
}

// Custom value recognition
static int IsHexVal(const char_type* a_szExpr, int* a_iPos, value_type* a_fVal);

// With user data
static value_type FunUd0(void* data) { return reinterpret_cast<std::intptr_t>(data); }
static value_type FunUd1(void* data, value_type v) { return reinterpret_cast<std::intptr_t>(data) + v; }
static value_type FunUd2(void* data, value_type v1, value_type v2) { return reinterpret_cast<std::intptr_t>(data) + v1 + v2; }
static value_type FunUd10(void* data, value_type v1, value_type v2, value_type v3, value_type v4, value_type v5,
value_type v6, value_type v7, value_type v8, value_type v9, value_type v10)
static value_type FunUd0(void* data)
{
return reinterpret_cast<std::intptr_t>(data);
}

static value_type FunUd1(void* data, value_type v)
{
return reinterpret_cast<std::intptr_t>(data) + v;
}

static value_type FunUd2(void* data, value_type v1, value_type v2)
{
return reinterpret_cast<std::intptr_t>(data) + v1 + v2;
}

static value_type FunUd10(void* data, value_type v1, value_type v2, value_type v3, value_type v4, value_type v5, value_type v6, value_type v7, value_type v8, value_type v9, value_type v10)
{
return reinterpret_cast<std::intptr_t>(data) + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
}

static value_type StrFunUd3(void* data, const char_type* v1, value_type v2, value_type v3)
{
int val(0);
stringstream_type(v1) >> val;
return reinterpret_cast<std::intptr_t>(data) + val + v2 + v3;
}

static value_type SumUd(void* data, const value_type* a_afArg, int a_iArgc)
{
if (!a_iArgc)
throw mu::Parser::exception_type(_T("too few arguments for function sum."));

value_type fRes = 0;
for (int i = 0; i < a_iArgc; ++i) fRes += a_afArg[i];
for (int i = 0; i < a_iArgc; ++i)
fRes += a_afArg[i];

return reinterpret_cast<std::intptr_t>(data) + fRes;
}

Expand All @@ -225,6 +251,7 @@ namespace mu
int TestIfThenElse();
int TestBulkMode();
int TestOssFuzzTestCases();
int TestOptimizer();

void Abort() const;

Expand Down
72 changes: 47 additions & 25 deletions include/muParserToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down Expand Up @@ -68,64 +68,86 @@ namespace mu
{
// Note: we keep generic_callable_type a pod for the purpose of layout

erased_fun_type pRawFun_;
void* pUserData_;
erased_fun_type _pRawFun;
void* _pUserData;

template <std::size_t NbParams, typename... Args>
value_type call_fun(Args&&... args) const
{
static_assert(NbParams == sizeof...(Args), "mismatch between NbParams and Args");
if (pUserData_ == nullptr) {
auto fun_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::fun_type>(pRawFun_);
if (_pUserData == nullptr)
{
auto fun_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::fun_type>(_pRawFun);
return (*fun_typed_ptr)(std::forward<Args>(args)...);
} else {
auto fun_userdata_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::fun_userdata_type>(pRawFun_);
return (*fun_userdata_typed_ptr)(pUserData_, std::forward<Args>(args)...);
}
else
{
auto fun_userdata_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::fun_userdata_type>(_pRawFun);
return (*fun_userdata_typed_ptr)(_pUserData, std::forward<Args>(args)...);
}
}

template <std::size_t NbParams, typename... Args>
value_type call_bulkfun(Args&&... args) const
{
static_assert(NbParams == sizeof...(Args) - 2, "mismatch between NbParams and Args");
if (pUserData_ == nullptr) {
auto bulkfun_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::bulkfun_type>(pRawFun_);
if (_pUserData == nullptr) {
auto bulkfun_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::bulkfun_type>(_pRawFun);
return (*bulkfun_typed_ptr)(std::forward<Args>(args)...);
} else {
auto bulkfun_userdata_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::bulkfun_userdata_type>(pRawFun_);
return (*bulkfun_userdata_typed_ptr)(pUserData_, std::forward<Args>(args)...);
auto bulkfun_userdata_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::bulkfun_userdata_type>(_pRawFun);
return (*bulkfun_userdata_typed_ptr)(_pUserData, std::forward<Args>(args)...);
}
}

value_type call_multfun(const value_type* a_afArg, int a_iArgc) const
{
if (pUserData_ == nullptr) {
auto multfun_typed_ptr = reinterpret_cast<multfun_type>(pRawFun_);
if (_pUserData == nullptr) {
auto multfun_typed_ptr = reinterpret_cast<multfun_type>(_pRawFun);
return (*multfun_typed_ptr)(a_afArg, a_iArgc);
} else {
auto multfun_userdata_typed_ptr = reinterpret_cast<multfun_userdata_type>(pRawFun_);
return (*multfun_userdata_typed_ptr)(pUserData_, a_afArg, a_iArgc);
auto multfun_userdata_typed_ptr = reinterpret_cast<multfun_userdata_type>(_pRawFun);
return (*multfun_userdata_typed_ptr)(_pUserData, a_afArg, a_iArgc);
}
}

template <std::size_t NbParams, typename... Args>
value_type call_strfun(Args&&... args) const
{
static_assert(NbParams == sizeof...(Args), "mismatch between NbParams and Args");
if (pUserData_ == nullptr) {
auto strfun_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::strfun_type>(pRawFun_);
if (_pUserData == nullptr)
{
auto strfun_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::strfun_type>(_pRawFun);
return (*strfun_typed_ptr)(std::forward<Args>(args)...);
} else {
auto strfun_userdata_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::strfun_userdata_type>(pRawFun_);
return (*strfun_userdata_typed_ptr)(pUserData_, std::forward<Args>(args)...);
}
else
{
auto strfun_userdata_typed_ptr = reinterpret_cast<typename TplCallType<NbParams>::strfun_userdata_type>(_pRawFun);
return (*strfun_userdata_typed_ptr)(_pUserData, std::forward<Args>(args)...);
}
}

bool operator==(generic_callable_type other) const { return pRawFun_ == other.pRawFun_ && pUserData_ == other.pUserData_; }
explicit operator bool() const { return pRawFun_ != nullptr; }
bool operator==(std::nullptr_t) const { return pRawFun_ == nullptr; }
bool operator!=(std::nullptr_t) const { return pRawFun_ != nullptr; }
bool operator==(generic_callable_type other) const
{
return _pRawFun == other._pRawFun && _pUserData == other._pUserData;
}

explicit operator bool() const
{
return _pRawFun != nullptr;
}

bool operator==(std::nullptr_t) const
{
return _pRawFun == nullptr;
}

bool operator!=(std::nullptr_t) const
{
return _pRawFun != nullptr;
}
};

static_assert(std::is_trivial<generic_callable_type>::value, "generic_callable_type shall be trivial");
static_assert(std::is_standard_layout<generic_callable_type>::value, "generic_callable_type shall have standard layout");
// C++17: static_assert(std::is_aggregate<generic_callable_type>::value, "generic_callable_type shall be an aggregate");
Expand Down
2 changes: 1 addition & 1 deletion include/muParserTokenReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
6 changes: 3 additions & 3 deletions samples/example1/example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down Expand Up @@ -455,7 +455,7 @@ static void Calc()
parser.DefineFun(_T("strfun0"), StrFun0);
parser.DefineFun(_T("strfun2"), StrFun2);
parser.DefineFun(_T("ping"), Ping);
parser.DefineFun(_T("rnd"), Rnd); // Add an unoptimizeable function
parser.DefineFun(_T("rnd"), Rnd, false); // Add an unoptimizeable function
parser.DefineFun(_T("throw"), ThrowAnException);

parser.DefineOprt(_T("add"), Add, 0);
Expand All @@ -466,7 +466,7 @@ static void Calc()
parser.DefineFun(_T("selftest"), SelfTest);
parser.DefineFun(_T("help"), Help);
parser.DefineFun(_T("arg2of2"), Arg2Of2);
parser.DefineFun(_T("arg1of2"), Arg1Of2);
parser.DefineFun(_T("arg1of2"), Arg1Of2, false);

parser.DefinePostfixOprt(_T("{ft}"), Milli);
parser.DefinePostfixOprt(_T("ft"), Milli);
Expand Down
2 changes: 1 addition & 1 deletion samples/example2/example2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion samples/example3/example3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
|__|_| /____/| __(____ /__| /____ >\___ >__|
\/ |__| \/ \/ \/
Copyright (C) 2004 - 2020 Ingo Berg
Copyright (C) 2004 - 2021 Ingo Berg
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Expand Down
Loading

0 comments on commit 6d8c4b8

Please sign in to comment.