unix, windows: make uv_is_*() always return 0 or 1

Ensure that the following API functions always return either 0 or 1:

  * uv_is_active()
  * uv_is_closing()
  * uv_is_readable()
  * uv_is_writable()
This commit is contained in:
Ben Noordhuis 2013-09-12 12:25:58 +02:00
parent 05822a5507
commit d7115f0677
13 changed files with 40 additions and 39 deletions

View File

@ -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);
}

View File

@ -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));
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);
@ -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);

View File

@ -139,9 +139,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);

View File

@ -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));
}
}
@ -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);

View File

@ -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++;
}

View File

@ -169,7 +169,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);
@ -962,7 +962,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;
}

View File

@ -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;
}

View File

@ -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++;
}
@ -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++;
@ -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++;
@ -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;

View File

@ -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++;
}