Skip to content

Commit

Permalink
Added tests for b6b_interp procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Nov 30, 2017
1 parent 764e9e5 commit 2085128
Show file tree
Hide file tree
Showing 24 changed files with 867 additions and 52 deletions.
6 changes: 6 additions & 0 deletions src/b6b_exc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static enum b6b_res b6b_exc_proc_try(struct b6b_interp *interp,
{
struct b6b_obj *t, *e, *f, *o;
unsigned int argc;
int exit;
enum b6b_res res, eres = B6B_ERR, fres;

argc = b6b_proc_get_args(interp, args, "oo|oo", NULL, &t, &e, &f);
Expand Down Expand Up @@ -51,10 +52,15 @@ static enum b6b_res b6b_exc_proc_try(struct b6b_interp *interp,
}

if (argc == 4) {
exit = interp->exit;
/* if the try block triggered exit, postpone it and let the finally
* block run */
interp->exit = 0;
o = b6b_ref(interp->fg->_);
fres = b6b_call(interp, f);
b6b_unref(interp->fg->_);
interp->fg->_ = o;
interp->exit = exit;

if ((res == B6B_RET) || (res == B6B_EXIT))
return res;
Expand Down
68 changes: 17 additions & 51 deletions src/b6b_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define _GNU_SOURCE

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
Expand All @@ -29,10 +28,6 @@
#include <limits.h>
#include <time.h>
#include <stdio.h>
#include <sys/syscall.h>
#ifdef B6B_HAVE_VALGRIND
# include <valgrind/helgrind.h>
#endif

#undef _GNU_SOURCE

Expand Down Expand Up @@ -553,50 +548,6 @@ int b6b_offload(struct b6b_interp *interp,

#endif

static void b6b_do_syscall(void *arg)
{
struct b6b_syscall_data *data = (struct b6b_syscall_data *)arg;

data->rval = syscall(data->args[0],
data->args[1],
data->args[2],
data->args[3],
data->args[4],
data->args[5]);
if (data->rval < 0)
data->rerrno = errno;
}

int b6b_syscall(struct b6b_interp *interp,
int *ret,
const long nr,
...)
{
struct b6b_syscall_data data;
va_list ap;
int i;

va_start(ap, nr);

data.args[0] = nr;
for (i = 1; i < sizeof(data.args) / sizeof(data.args[0]); ++i)
data.args[i] = va_arg(ap, long);

va_end(ap);

#ifdef B6B_HAVE_VALGRIND
VALGRIND_HG_DISABLE_CHECKING(&data, sizeof(data));
#endif
if (!b6b_offload(interp, b6b_do_syscall, &data))
return 0;

*ret = data.rval;
if (data.rval < 0)
errno = data.rerrno;

return 1;
}

static enum b6b_res b6b_on_res(struct b6b_interp *interp,
const enum b6b_res res)
{
Expand Down Expand Up @@ -823,6 +774,17 @@ static enum b6b_res b6b_interp_proc_nop(struct b6b_interp *interp,
return B6B_OK;
}

static enum b6b_res b6b_interp_proc_echo(struct b6b_interp *interp,
struct b6b_obj *args)
{
struct b6b_obj *o;

if (!b6b_proc_get_args(interp, args, "oo", NULL, &o))
return B6B_ERR;

return b6b_return(interp, b6b_ref(o));
}

static enum b6b_res b6b_interp_proc_yield(struct b6b_interp *interp,
struct b6b_obj *args)
{
Expand Down Expand Up @@ -1001,8 +963,6 @@ static enum b6b_res b6b_interp_proc_repr(struct b6b_interp *interp,
static enum b6b_res b6b_interp_proc_call(struct b6b_interp *interp,
struct b6b_obj *args)
{


struct b6b_obj *stmts;

if (b6b_proc_get_args(interp, args, "oo", NULL, &stmts))
Expand Down Expand Up @@ -1090,6 +1050,12 @@ static const struct b6b_ext_obj b6b_interp[] = {
.val.s = "nop",
.proc = b6b_interp_proc_nop
},
{
.name = "echo",
.type = B6B_TYPE_STR,
.val.s = "echo",
.proc = b6b_interp_proc_echo
},
{
.name = "yield",
.type = B6B_TYPE_STR,
Expand Down
2 changes: 2 additions & 0 deletions src/b6b_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ static enum b6b_res b6b_loop_proc_range(struct b6b_interp *interp,
&stepo)) {
case 4:
step = stepo->i;
if (step <= 0)
return B6B_ERR;

case 3:
if (start->i > end->i)
Expand Down
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ endif

libb6b_srcs = [
'b6b_hash.c', 'b6b_obj.c', 'b6b_str.c', 'b6b_int.c', 'b6b_float.c', 'b6b_list.c', 'b6b_dict.c',
'b6b_frame.c', 'b6b_thread.c', 'b6b_interp.c',
'b6b_frame.c', 'b6b_thread.c', 'b6b_syscall.c', 'b6b_interp.c',
'b6b_math.c', 'b6b_logic.c', 'b6b_loop.c', 'b6b_exc.c', 'b6b_proc.c',
'b6b_strm.c', 'b6b_fdops.c', 'b6b_stdio.c', 'b6b_file.c', 'b6b_socket.c', 'b6b_timer.c', 'b6b_signal.c', 'b6b_sh.c', 'b6b_poll.c', 'b6b_evloop.c',
'b6b_time.c', 'b6b_path.c', 'b6b_rand.c'
Expand Down
72 changes: 72 additions & 0 deletions tests/b6b_test_b6b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* This file is part of b6b.
*
* Copyright 2017 Dima Krasner
*
* 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.
*/

#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <b6b.h>

int main()
{
struct b6b_interp interp;

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$b6b}", 6) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{[$b6b {}] call {{$echo 5}}}", 28) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 5);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{[$b6b {a b}] call {{$echo $@}}}", 32) == B6B_OK);
assert(b6b_as_list(interp.fg->_));
assert(!b6b_list_empty(interp.fg->_));
assert(b6b_as_str(b6b_list_first(interp.fg->_)->o));
assert(b6b_list_first(interp.fg->_)->o->slen == 1);
assert(strcmp(b6b_list_first(interp.fg->_)->o->s, "a") == 0);
assert(b6b_list_next(b6b_list_first(interp.fg->_)));
assert(b6b_as_str(b6b_list_next(b6b_list_first(interp.fg->_))->o));
assert(b6b_list_next(b6b_list_first(interp.fg->_))->o->slen == 1);
assert(strcmp(b6b_list_next(b6b_list_first(interp.fg->_))->o->s, "b") == 0);
assert(!b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))));
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{[$b6b {}] a {{$echo 5}}}", 25) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{[$b6b {}] call {{$exit 5}}}",
28) == B6B_EXIT);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$local a 1337} {[$b6b {}] call {{$global a 1338}}} {$echo $a}",
62) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 1337);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
37 changes: 37 additions & 0 deletions tests/b6b_test_break.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of b6b.
*
* Copyright 2017 Dima Krasner
*
* 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.
*/

#include <stdlib.h>
#include <assert.h>

#include <b6b.h>

int main()
{
struct b6b_interp interp;

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$echo 1} {$break} {$echo 2}",
28) == B6B_BREAK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 0);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
55 changes: 55 additions & 0 deletions tests/b6b_test_call.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of b6b.
*
* Copyright 2017 Dima Krasner
*
* 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.
*/

#include <stdlib.h>
#include <assert.h>

#include <b6b.h>

int main()
{
struct b6b_interp interp;

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$call}", 7) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$echo 1} {$call {}}", 20) == B6B_OK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 0);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$echo 1} {$call {{$echo 2} {$echo 3}}}",
39) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 3);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$echo 1} {$call {{$echo 2} {$throw 3} {$echo 4}}}",
50) == B6B_ERR);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 3);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
37 changes: 37 additions & 0 deletions tests/b6b_test_continue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of b6b.
*
* Copyright 2017 Dima Krasner
*
* 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.
*/

#include <stdlib.h>
#include <assert.h>

#include <b6b.h>

int main()
{
struct b6b_interp interp;

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$echo 1} {$continue} {$echo 2}",
31) == B6B_CONT);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 0);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
41 changes: 41 additions & 0 deletions tests/b6b_test_echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of b6b.
*
* Copyright 2017 Dima Krasner
*
* 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.
*/

#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <b6b.h>

int main()
{
struct b6b_interp interp;

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$echo}", 7) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$echo abc}", 11) == B6B_OK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 3);
assert(strcmp(interp.fg->_->s, "abc") == 0);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}

0 comments on commit 2085128

Please sign in to comment.