Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix, windows: make uv_is_*() always return 0 or 1
Browse files Browse the repository at this point in the history
Ensure that the following API functions always return either 0 or 1:

  * uv_is_active()
  * uv_is_closing()
  * uv_is_readable()
  * uv_is_writable()
  • Loading branch information
bnoordhuis committed Sep 12, 2013
1 parent ce3c38a commit 80a716b
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/unix/stream.c
Expand Up @@ -1384,12 +1384,12 @@ int uv_read_stop(uv_stream_t* stream) {


int uv_is_readable(const uv_stream_t* stream) {
return stream->flags & UV_STREAM_READABLE;
return !!(stream->flags & UV_STREAM_READABLE);
}


int uv_is_writable(const uv_stream_t* stream) {
return stream->flags & UV_STREAM_WRITABLE;
return !!(stream->flags & UV_STREAM_WRITABLE);
}


Expand Down
2 changes: 1 addition & 1 deletion src/win/handle.c
Expand Up @@ -149,5 +149,5 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) {


int uv_is_closing(const uv_handle_t* handle) {
return handle->flags & (UV__HANDLE_CLOSING | UV_HANDLE_CLOSED);
return !!(handle->flags & (UV__HANDLE_CLOSING | UV_HANDLE_CLOSED));
}
21 changes: 11 additions & 10 deletions test/test-active.c
Expand Up @@ -47,31 +47,32 @@ TEST_IMPL(active) {
r = uv_timer_init(uv_default_loop(), &timer);
ASSERT(r == 0);

ASSERT(!uv_is_active((uv_handle_t*) &timer));
ASSERT(!uv_is_closing((uv_handle_t*) &timer));
/* uv_is_active() and uv_is_closing() should always return either 0 or 1. */
ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));

r = uv_timer_start(&timer, timer_cb, 1000, 0);
ASSERT(r == 0);

ASSERT(uv_is_active((uv_handle_t*) &timer));
ASSERT(!uv_is_closing((uv_handle_t*) &timer));
ASSERT(1 == uv_is_active((uv_handle_t*) &timer));
ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));

r = uv_timer_stop(&timer);
ASSERT(r == 0);

ASSERT(!uv_is_active((uv_handle_t*) &timer));
ASSERT(!uv_is_closing((uv_handle_t*) &timer));
ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));

r = uv_timer_start(&timer, timer_cb, 1000, 0);
ASSERT(r == 0);

ASSERT(uv_is_active((uv_handle_t*) &timer));
ASSERT(!uv_is_closing((uv_handle_t*) &timer));
ASSERT(1 == uv_is_active((uv_handle_t*) &timer));
ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));

uv_close((uv_handle_t*) &timer, close_cb);

ASSERT(!uv_is_active((uv_handle_t*) &timer));
ASSERT(uv_is_closing((uv_handle_t*) &timer));
ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
ASSERT(1 == uv_is_closing((uv_handle_t*) &timer));

r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT(r == 0);
Expand Down
2 changes: 1 addition & 1 deletion test/test-fs-poll.c
Expand Up @@ -81,7 +81,7 @@ static void poll_cb(uv_fs_poll_t* handle,
memset(&zero_statbuf, 0, sizeof(zero_statbuf));

ASSERT(handle == &poll_handle);
ASSERT(uv_is_active((uv_handle_t*)handle));
ASSERT(1 == uv_is_active((uv_handle_t*) handle));
ASSERT(prev != NULL);
ASSERT(curr != NULL);

Expand Down
6 changes: 3 additions & 3 deletions test/test-ipc-send-recv.c
Expand Up @@ -211,9 +211,9 @@ int ipc_send_recv_helper(void) {
ASSERT(r == 0);

uv_pipe_open(&ctx.channel, 0);
ASSERT(uv_is_readable((uv_stream_t*)&ctx.channel));
ASSERT(uv_is_writable((uv_stream_t*)&ctx.channel));
ASSERT(!uv_is_closing((uv_handle_t*)&ctx.channel));
ASSERT(1 == uv_is_readable((uv_stream_t*)&ctx.channel));
ASSERT(1 == uv_is_writable((uv_stream_t*)&ctx.channel));
ASSERT(0 == uv_is_closing((uv_handle_t*)&ctx.channel));

r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, read2_cb);
ASSERT(r == 0);
Expand Down
12 changes: 6 additions & 6 deletions test/test-ipc.c
Expand Up @@ -559,9 +559,9 @@ int ipc_helper(int listen_after_write) {

uv_pipe_open(&channel, 0);

ASSERT(uv_is_readable((uv_stream_t*) &channel));
ASSERT(uv_is_writable((uv_stream_t*) &channel));
ASSERT(!uv_is_closing((uv_handle_t*) &channel));
ASSERT(1 == uv_is_readable((uv_stream_t*) &channel));
ASSERT(1 == uv_is_writable((uv_stream_t*) &channel));
ASSERT(0 == uv_is_closing((uv_handle_t*) &channel));

r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT(r == 0);
Expand Down Expand Up @@ -609,9 +609,9 @@ int ipc_helper_tcp_connection(void) {

uv_pipe_open(&channel, 0);

ASSERT(uv_is_readable((uv_stream_t*)&channel));
ASSERT(uv_is_writable((uv_stream_t*)&channel));
ASSERT(!uv_is_closing((uv_handle_t*)&channel));
ASSERT(1 == uv_is_readable((uv_stream_t*) &channel));
ASSERT(1 == uv_is_writable((uv_stream_t*) &channel));
ASSERT(0 == uv_is_closing((uv_handle_t*) &channel));

r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT(r == 0);
Expand Down
6 changes: 3 additions & 3 deletions test/test-ping-pong.c
Expand Up @@ -135,9 +135,9 @@ static void pinger_on_connect(uv_connect_t *req, int status) {

ASSERT(status == 0);

ASSERT(uv_is_readable(req->handle));
ASSERT(uv_is_writable(req->handle));
ASSERT(!uv_is_closing((uv_handle_t *)req->handle));
ASSERT(1 == uv_is_readable(req->handle));
ASSERT(1 == uv_is_writable(req->handle));
ASSERT(0 == uv_is_closing((uv_handle_t *) req->handle));

pinger_write_ping(pinger);

Expand Down
6 changes: 3 additions & 3 deletions test/test-poll.c
Expand Up @@ -406,9 +406,9 @@ static void connection_poll_cb(uv_poll_t* handle, int status, int events) {

/* Assert that uv_is_active works correctly for poll handles. */
if (context->events != 0) {
ASSERT(uv_is_active((uv_handle_t*) handle));
ASSERT(1 == uv_is_active((uv_handle_t*) handle));
} else {
ASSERT(!uv_is_active((uv_handle_t*) handle));
ASSERT(0 == uv_is_active((uv_handle_t*) handle));
}
}

Expand All @@ -418,7 +418,7 @@ static void delay_timer_cb(uv_timer_t* timer, int status) {
int r;

/* Timer should auto stop. */
ASSERT(!uv_is_active((uv_handle_t*) timer));
ASSERT(0 == uv_is_active((uv_handle_t*) timer));

/* Add the requested events to the poll mask. */
ASSERT(context->delayed_events != 0);
Expand Down
4 changes: 2 additions & 2 deletions test/test-shutdown-close.c
Expand Up @@ -56,9 +56,9 @@ static void connect_cb(uv_connect_t* req, int status) {

r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
ASSERT(r == 0);
ASSERT(!uv_is_closing((uv_handle_t*) req->handle));
ASSERT(0 == uv_is_closing((uv_handle_t*) req->handle));
uv_close((uv_handle_t*) req->handle, close_cb);
ASSERT(uv_is_closing((uv_handle_t*) req->handle));
ASSERT(1 == uv_is_closing((uv_handle_t*) req->handle));

connect_cb_called++;
}
Expand Down
4 changes: 2 additions & 2 deletions test/test-spawn.c
Expand Up @@ -167,7 +167,7 @@ TEST_IMPL(spawn_fails) {
init_process_options("", exit_cb_expect_enoent);
options.file = options.args[0] = "program-that-had-better-not-exist";
ASSERT(0 == uv_spawn(uv_default_loop(), &process, &options));
ASSERT(0 != uv_is_active((uv_handle_t*)&process));
ASSERT(1 == uv_is_active((uv_handle_t*) &process));
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
ASSERT(1 == exit_cb_called);

Expand Down Expand Up @@ -946,7 +946,7 @@ TEST_IMPL(spawn_auto_unref) {
ASSERT(0 == uv_is_closing((uv_handle_t*) &process));
uv_close((uv_handle_t*) &process, NULL);
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
ASSERT(0 != uv_is_closing((uv_handle_t*) &process));
ASSERT(1 == uv_is_closing((uv_handle_t*) &process));
MAKE_VALGRIND_HAPPY();
return 0;
}
2 changes: 1 addition & 1 deletion test/test-timer-again.c
Expand Up @@ -78,7 +78,7 @@ static void repeat_2_cb(uv_timer_t* handle, int status) {
repeat_2_cb_called++;

if (uv_timer_get_repeat(&repeat_2) == 0) {
ASSERT(!uv_is_active((uv_handle_t*)handle));
ASSERT(0 == uv_is_active((uv_handle_t*) handle));
uv_close((uv_handle_t*)handle, close_cb);
return;
}
Expand Down
8 changes: 4 additions & 4 deletions test/test-timer.c
Expand Up @@ -38,7 +38,7 @@ static void once_close_cb(uv_handle_t* handle) {
printf("ONCE_CLOSE_CB\n");

ASSERT(handle != NULL);
ASSERT(!uv_is_active(handle));
ASSERT(0 == uv_is_active(handle));

once_close_cb_called++;
}
Expand All @@ -49,7 +49,7 @@ static void once_cb(uv_timer_t* handle, int status) {

ASSERT(handle != NULL);
ASSERT(status == 0);
ASSERT(!uv_is_active((uv_handle_t*)handle));
ASSERT(0 == uv_is_active((uv_handle_t*) handle));

once_cb_called++;

Expand All @@ -74,7 +74,7 @@ static void repeat_cb(uv_timer_t* handle, int status) {

ASSERT(handle != NULL);
ASSERT(status == 0);
ASSERT(uv_is_active((uv_handle_t*)handle));
ASSERT(1 == uv_is_active((uv_handle_t*) handle));

repeat_cb_called++;

Expand Down Expand Up @@ -163,7 +163,7 @@ TEST_IMPL(timer_init) {

ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
ASSERT(0 == uv_timer_get_repeat(&handle));
ASSERT(!uv_is_active((uv_handle_t*)&handle));
ASSERT(0 == uv_is_active((uv_handle_t*) &handle));

MAKE_VALGRIND_HAPPY();
return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/test-udp-send-and-recv.c
Expand Up @@ -54,7 +54,7 @@ static void alloc_cb(uv_handle_t* handle,

static void close_cb(uv_handle_t* handle) {
CHECK_HANDLE(handle);
ASSERT(uv_is_closing(handle));
ASSERT(1 == uv_is_closing(handle));
close_cb_called++;
}

Expand Down

0 comments on commit 80a716b

Please sign in to comment.