Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #928 from tom-wa/fix_653
Browse files Browse the repository at this point in the history
mathcheck: fix #653 + many other things
  • Loading branch information
markus2330 authored Sep 15, 2016
2 parents 32e281f + 4bd39c6 commit 3c8e123
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
47 changes: 34 additions & 13 deletions src/plugins/mathcheck/mathcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,25 @@
#define str(s) #s
#define xstr(s) str (s)

typedef enum { ERROR, ADD, SUB, MUL, DIV, NOT, EQU, LT, GT, LE, GE, RES, VAL, END, SET, NA, EMPTY } Operation;
typedef enum {
ERROR = 0,
ADD = 1,
SUB = 2,
MUL = 3,
DIV = 4,
NOT = 5,
EQU = 6,
LT = 7,
GT = 8,
LE = 9,
GE = 10,
RES = 11,
VAL = 12,
END = 13,
SET = 14,
NA = 15,
EMPTY = 16
} Operation;
typedef struct
{
double value;
Expand Down Expand Up @@ -91,12 +109,12 @@ static PNElem doPrefixCalculation (PNElem * stack, PNElem * stackPtr)
}
while (stackPtr >= stack)
{
if (stackPtr->op == VAL && stackPtr != stack)
if ((stackPtr->op == VAL || stackPtr->op == NA) && stackPtr != stack)
{
--stackPtr;
continue;
}
else if (stackPtr->op == VAL && stackPtr == stack)
else if ((stackPtr->op == VAL || stackPtr->op == NA) && stackPtr == stack)
{
break;
}
Expand Down Expand Up @@ -128,7 +146,6 @@ static PNElem doPrefixCalculation (PNElem * stack, PNElem * stackPtr)
e2.op = VAL;
}
}

if (e1.op == VAL && e2.op == VAL)
{
switch (stackPtr->op)
Expand Down Expand Up @@ -157,6 +174,8 @@ static PNElem doPrefixCalculation (PNElem * stack, PNElem * stackPtr)
default:
break;
}
result.op = stackPtr->op;
result.value = stackPtr->value;
}
else
{
Expand All @@ -176,7 +195,7 @@ static PNElem doPrefixCalculation (PNElem * stack, PNElem * stackPtr)
}
static PNElem parsePrefixString (const char * prefixString, Key * curKey, KeySet * ks, Key * parentKey)
{
const char * regexString = "(((\\.|\\.\\.|@|\\/)([[:alnum:]]*/)*[[:alnum:]]+))|('[0-9]*[.,]{0,1}[0-9]*')|([-+:/<>=!{*])";
const char * regexString = "((((\\.|\\.\\.|@|\\/)([[:alnum:]]*/)*[[:alnum:]]+))|('[0-9]*[.,]{0,1}[0-9]*')|(==)|([-+:/<>=!{*]))";
char * ptr = (char *)prefixString;
regex_t regex;
Key * key;
Expand All @@ -200,33 +219,37 @@ static PNElem parsePrefixString (const char * prefixString, Key * curKey, KeySet
char * searchKey = NULL;
while (1)
{
stackPtr->op = ERROR;
stackPtr->value = 0;
nomatch = regexec (&regex, ptr, 1, &match, 0);
if (nomatch)
{
break;
}
len = match.rm_eo - match.rm_so;
start = match.rm_so + (ptr - prefixString);
if (len == 1 && !isalpha (prefixString[start]))
if (!strncmp (prefixString + start, "==", 2))
{
resultOp = EQU;
}
else if (len == 1 && !isalpha (prefixString[start]) && prefixString[start] != '\'' && prefixString[start] != '.' &&
prefixString[start] != '@')
{

switch (prefixString[start])
{

case '+':
stackPtr->op = ADD;
++stackPtr;
break;
case '-':
stackPtr->op = SUB;
++stackPtr;
break;
case '/':
stackPtr->op = DIV;
++stackPtr;
break;
case '*':
stackPtr->op = MUL;
++stackPtr;
break;
case ':':
resultOp = SET;
Expand Down Expand Up @@ -281,8 +304,6 @@ static PNElem parsePrefixString (const char * prefixString, Key * curKey, KeySet
}
else
{
stackPtr->op = ERROR;
stackPtr->value = 0;
char * subString = elektraMalloc (len + 1);
strncpy (subString, prefixString + start, len);
subString[len] = '\0';
Expand Down Expand Up @@ -328,8 +349,8 @@ static PNElem parsePrefixString (const char * prefixString, Key * curKey, KeySet
elektraFree (subString);
}
if (stackPtr->op != NA) stackPtr->op = VAL;
++stackPtr;
}
++stackPtr;
int offset = stackPtr - stack;
stack = realloc (stack, (offset + 1) * sizeof (PNElem));
stackPtr = stack;
Expand Down
39 changes: 35 additions & 4 deletions src/plugins/mathcheck/testmod_mathcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
PLUGIN_CLOSE (); \
}

#define testSet(ks, value) \
{ \
Key * parentKey = keyNew ("user/tests/mathcheck", KEY_VALUE, "", KEY_END); \
KeySet * conf = ksNew (0, KS_END); \
PLUGIN_OPEN ("mathcheck"); \
ksRewind (ks); \
plugin->kdbSet (plugin, ks, parentKey); \
succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/mathcheck/sum", 0)), value), "error"); \
keyDel (parentKey); \
PLUGIN_CLOSE (); \
}

static KeySet * create_ks (const char * res, const char * meta)
{
return ksNew (5, keyNew ("user/tests/mathcheck/sum", KEY_VALUE, res, KEY_META, "check/math", meta, KEY_END),
Expand All @@ -52,6 +64,9 @@ int main (int argc, char ** argv)
ks = create_ks ("2", "== / @/bla/val1 @/bla/val2");
test (ks, 1);
ksDel (ks);
ks = create_ks ("", ":= / @/bla/val1 @/bla/val2");
testSet (ks, "2");
ksDel (ks);
ks = create_ks ("1", "== / ../bla/val1 ../bla/val3");
test (ks, (-1));
ksDel (ks);
Expand All @@ -61,6 +76,11 @@ int main (int argc, char ** argv)
ks = create_ks ("4.5", "== + '1.5' + '1.5' '1.5'");
test (ks, 1);
ksDel (ks);

ks = create_ks ("", ":= + '1.5' + '1.5' '1.5'");
testSet (ks, "4.5");
ksDel (ks);

ks = create_ks ("1", "== + '1.5' '1.5'");
test (ks, (-1));
ksDel (ks);
Expand All @@ -73,18 +93,29 @@ int main (int argc, char ** argv)
test (ks, 1);
ksDel (ks);

ks = create_ks ("", ":= + @/bla/nonExisting '7'");
testSet (ks, "7");
ksDel (ks);

ks = create_ks ("7", "== * @/bla/nonExisting '7'");
test (ks, 1);
ksDel (ks);

ks = create_ks ("3", "== + ../bla/nonExisting + ../bla/nonExistingToo ../bla/val3");
test (ks, 1) ksDel (ks);
test (ks, 1);
ksDel (ks);

ks = create_ks ("", ":= + ../bla/nonExisting + ../bla/nonExistingToo ../bla/val3");
testSet (ks, "3");
ksDel (ks);

ks = create_ks ("3", "== / @/bla/nonExisting / ../bla/nonExistingToo @/bla/val3");
test (ks, 1) ksDel (ks);
test (ks, 1);
ksDel (ks);

ks = create_ks ("3", "== + @/bla/nonExisting / ../bla/nonExistingToo ../bla/val3");
test (ks, 1) ksDel (ks);
ks = create_ks ("3", "== + @/bla/nonExisting / ../bla/val3 ../bla/nonExistingToo");
test (ks, 1);
ksDel (ks);

printf ("\ntestmod_mathcheck RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);

Expand Down

0 comments on commit 3c8e123

Please sign in to comment.