Skip to content

Commit

Permalink
Added tests for timer
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Nov 24, 2017
1 parent 793583e commit 910c1a8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/b6b_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static enum b6b_res b6b_timer_proc_timer(struct b6b_interp *interp,
struct b6b_obj *f, *o;
int fd, err;

if (!b6b_proc_get_args(interp, args, "of", NULL, &f))
if (!b6b_proc_get_args(interp, args, "of", NULL, &f) || !f->f)
return B6B_ERR;

fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
Expand Down
86 changes: 86 additions & 0 deletions tests/b6b_test_timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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, "{$timer a}", 10) == B6B_ERR);
b6b_interp_destroy(&interp);

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

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$local t [$timer 1]} {$sleep 1} {$t read}", 42) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 1);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{[$timer 2] read}", 17) == 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, "{$local t [$timer 1]} {$sleep 1} {$local a [$t read]} {$sleep 1} {$list.new $a [$t read]}", 89) == B6B_OK);
assert(b6b_as_list(interp.fg->_));
assert(!b6b_list_empty(interp.fg->_));
assert(b6b_as_float(b6b_list_first(interp.fg->_)->o));
assert(b6b_list_first(interp.fg->_)->o->f == 1);
assert(b6b_list_next(b6b_list_first(interp.fg->_)));
assert(b6b_as_float(b6b_list_next(b6b_list_first(interp.fg->_))->o));
assert(b6b_list_next(b6b_list_first(interp.fg->_))->o->f == 1);
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, "{$local t [$timer 1]} {$sleep 1} {$list.new [$t read] [$t read]}", 64) == B6B_OK);
assert(b6b_as_list(interp.fg->_));
assert(!b6b_list_empty(interp.fg->_));
assert(b6b_as_float(b6b_list_first(interp.fg->_)->o));
assert(b6b_list_first(interp.fg->_)->o->f == 1);
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 == 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, "{$local t [$timer 1.5]} {$sleep 1} {$local a [$t read]} {$sleep 0.5} {$list.new $a [$t read]}", 93) == 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 == 0);
assert(b6b_list_next(b6b_list_first(interp.fg->_)));
assert(b6b_as_float(b6b_list_next(b6b_list_first(interp.fg->_))->o));
assert(b6b_list_next(b6b_list_first(interp.fg->_))->o->f == 1);
assert(!b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))));
b6b_interp_destroy(&interp);


return EXIT_SUCCESS;
}
3 changes: 2 additions & 1 deletion tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ tests = [
['math', 'quick', 5],
['bswap', 'quick', 5],
['rand', 'quick', 5],
['socket', 'quick', 5]
['socket', 'quick', 5],
['timer', 'quick', 10]
]

if get_option('with_threads')
Expand Down

0 comments on commit 910c1a8

Please sign in to comment.