Skip to content

Commit

Permalink
fix bug for ($1+0)==$1 comparison
Browse files Browse the repository at this point in the history
add addcmp and fld1 tests
  • Loading branch information
James Parkinson committed Jul 4, 2021
1 parent 676d77d commit 3f15586
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 17 deletions.
24 changes: 14 additions & 10 deletions lib/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2333,17 +2333,8 @@ awka_doln(int idx, int set)
a_HSHNode *node;

if (!idx)
return awka_dol0(set);

if (!nullvar)
{
malloc( &nullvar, sizeof(a_VAR) );
nullvar->allc = malloc( &nullvar->ptr, 1 );
nullvar->ptr[0] = '\0';
nullvar->slen = 0;
nullvar->type = a_VARUNK;
nullvar->temp = nullvar->type2 = 0;
nullvar->dval = 0.0;
return awka_dol0(set);
}

if (_rebuildn == TRUE)
Expand All @@ -2363,7 +2354,19 @@ awka_doln(int idx, int set)
case 0:
/* if (idx >= array->nodeno) */
if (idx >= nf || idx >= array->nodeno)
{
if (!nullvar)
{
malloc( &nullvar, sizeof(a_VAR) );
nullvar->allc = malloc( &nullvar->ptr, 1 );
nullvar->ptr[0] = '\0';
nullvar->slen = 0;
nullvar->type = a_VARUNK;
nullvar->temp = nullvar->type2 = 0;
nullvar->dval = 0.0;
}
return nullvar;
}
break;

default:
Expand Down Expand Up @@ -2414,6 +2417,7 @@ awka_doln(int idx, int set)
}
else
array->slot[idx]->var->ptr = NULL;

array->slot[idx]->key = _a_SPLT_LOCALSTR;
return array->slot[idx]->var;
}
Expand Down
16 changes: 11 additions & 5 deletions lib/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ _awka_getdval( a_VAR *v, char *file, int line )
awka_error("runtime error: awka_getd in file %s, line %d - %s\n", file,line,"array used as scalar");
}

if (v->type2 != (char) -1)
if (v->type == a_VARDBL && v->type2 != (char) -1)
v->type2 = a_DBLSET;
return v;
}
Expand Down Expand Up @@ -724,9 +724,14 @@ _awka_checkunk(a_VAR *va)
if (!isalpha(va->ptr[0]) &&
_awka_isnumber(va->ptr) == TRUE)
{
va->type2 = a_DBLSET;
if(va->type != a_VARDBL)
va->type2 = a_DBLSET;
va->dval = strtod(va->ptr, NULL);
}
else {
va->dval = 0.0;
va->type2 = 0;
}
/*
else
va->type2 = -1;
Expand Down Expand Up @@ -798,8 +803,9 @@ awka_var2dblcmp( a_VAR *va, double d )
if (va->type == a_VARUNK && va->type2 == 0 && va->ptr)
_awka_checkunk(va);

if (va->type <= a_VARDBL || (va->type == a_VARUNK && va->type2 == a_DBLSET))
if (va->type == a_VARDBL || (va->type == a_VARUNK && va->type2 == a_DBLSET))
return (va->dval == d ? 0 : (va->dval < d ? -1 : 1));

if (!(i = strcmp(awka_gets1(va), awka_tmp_dbl2str(d))))
return 0;
return (i < 0 ? -1 : 1);
Expand All @@ -816,7 +822,7 @@ awka_dbl2varcmp( double d, a_VAR *va )
if (va->type2 == 0 && va->ptr && va->type == a_VARUNK)
_awka_checkunk(va);

if (va->type <= a_VARDBL || (va->type == a_VARUNK && va->type2 == a_DBLSET))
if (va->type == a_VARDBL || (va->type == a_VARUNK && va->type2 == a_DBLSET))
{
i = (d == va->dval ? 0 : (d < va->dval ? -1 : 1));
return (double) i;
Expand Down Expand Up @@ -944,7 +950,7 @@ awka_tmp_re2var(awka_regexp *r)
char *
awka_tmp_dbl2str(double d)
{
char *s, tmp[256];
char *s, tmp[25]; // max chars for %d on 64 bit machine should be 23
int i = (int) d, len;

if ((double) i == d)
Expand Down
27 changes: 27 additions & 0 deletions limits.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
The built in limits of AWKA - not necessarily limits but initial settings

AWKA
----
starting input file buf size 4096
number of field splits 255
string split buffer size 400
sprintf size combination of Nr splits and split buffer
memory page size 256
code warnings before warn 16
nr P fileds (NF,RS,RS,CONVFMT,OFMT 5
input file buf size 4096 (as defined above)
nr scan codes 256
eval stack size 256
max number of fields 32767 = 128*256-1

LIBAWKA
-------
variable arguments: 254
sprintf buffer size 2048
garbage collector bin size 30
temporary strings size 32
IO buffer size 16384
RE max dup size 32767
RE compiled pattern size 131072
RE match_2 reported regs size 30

4 changes: 2 additions & 2 deletions test/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ basic: msg \
bslash print cox gensub zeroflag getnr2tm getnr2tb \
numindex subslash opasnslf opasnidx arynocls getlnbuf arysubnm \
nlstrina nextfile next rsnul1nl beginexit mainexit fnrtest \
powtest begingetline extendedchars nofile sort \
powtest begingetline extendedchars nofile sort addcmp \
endbasic

revive-awka: rslen rsnull scgi
Expand Down Expand Up @@ -105,7 +105,7 @@ zeroflag asort subslash opasnslf opasnidx arysubnm nlstrina print gensub cox ret
nofile a_cat eiso3 divide conflict aloop dubois splitrem fun arraycat arrayref rand \
convfmt intprec gnureops pcntplus prmreuse math getlnhd backgsub dynlj substr eofsplit \
prt1eval prtoeval gsubtest splitwht foronsplit intest fnarydel delarprm \
beginexit powtest begingetline extendedchars sort mathextra::
beginexit powtest begingetline extendedchars sort addcmp mathextra::
@echo $@
@$(AWKA) -f $@.awk >x.c
@$(CC) x.c $(CFLAGS) -o xx
Expand Down
10 changes: 10 additions & 0 deletions test/addcmp.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BEGIN {
a[0] = 50
a[1] = "a"
a[2] = "test string"
a[3] = SUBSEP

for (i=0; i<4; i++) print a[i] + 0 == a[i]
print i + 0 == i
print "s" + 0 == "s"
}
6 changes: 6 additions & 0 deletions test/addcmp.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1
0
0
0
1
0
8 changes: 8 additions & 0 deletions test/fld1.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Test the comparison of numerical and string inputs and checks the
# type setting of input fields
#
# The expected input is lines containing either a string or a numeric value
#
{
print ($1 + 0) == $1
}

0 comments on commit 3f15586

Please sign in to comment.