xref: /netbsd-src/external/mit/libuv/dist/test/test-getters-setters.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
1*5f2f4271Schristos /* Copyright libuv project contributors. All rights reserved.
2*5f2f4271Schristos  *
3*5f2f4271Schristos  * Permission is hereby granted, free of charge, to any person obtaining a copy
4*5f2f4271Schristos  * of this software and associated documentation files (the "Software"), to
5*5f2f4271Schristos  * deal in the Software without restriction, including without limitation the
6*5f2f4271Schristos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*5f2f4271Schristos  * sell copies of the Software, and to permit persons to whom the Software is
8*5f2f4271Schristos  * furnished to do so, subject to the following conditions:
9*5f2f4271Schristos  *
10*5f2f4271Schristos  * The above copyright notice and this permission notice shall be included in
11*5f2f4271Schristos  * all copies or substantial portions of the Software.
12*5f2f4271Schristos  *
13*5f2f4271Schristos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*5f2f4271Schristos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*5f2f4271Schristos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*5f2f4271Schristos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*5f2f4271Schristos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*5f2f4271Schristos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*5f2f4271Schristos  * IN THE SOFTWARE.
20*5f2f4271Schristos  */
21*5f2f4271Schristos 
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos #include <string.h>
250e552da7Schristos #include <sys/stat.h>
260e552da7Schristos 
270e552da7Schristos int cookie1;
280e552da7Schristos int cookie2;
290e552da7Schristos int cookie3;
300e552da7Schristos 
310e552da7Schristos 
TEST_IMPL(handle_type_name)320e552da7Schristos TEST_IMPL(handle_type_name) {
330e552da7Schristos   ASSERT(strcmp(uv_handle_type_name(UV_NAMED_PIPE), "pipe") == 0);
340e552da7Schristos   ASSERT(strcmp(uv_handle_type_name(UV_UDP), "udp") == 0);
350e552da7Schristos   ASSERT(strcmp(uv_handle_type_name(UV_FILE), "file") == 0);
36*5f2f4271Schristos   ASSERT_NULL(uv_handle_type_name(UV_HANDLE_TYPE_MAX));
37*5f2f4271Schristos   ASSERT_NULL(uv_handle_type_name(UV_HANDLE_TYPE_MAX + 1));
38*5f2f4271Schristos   ASSERT_NULL(uv_handle_type_name(UV_UNKNOWN_HANDLE));
390e552da7Schristos   return 0;
400e552da7Schristos }
410e552da7Schristos 
420e552da7Schristos 
TEST_IMPL(req_type_name)430e552da7Schristos TEST_IMPL(req_type_name) {
440e552da7Schristos   ASSERT(strcmp(uv_req_type_name(UV_REQ), "req") == 0);
450e552da7Schristos   ASSERT(strcmp(uv_req_type_name(UV_UDP_SEND), "udp_send") == 0);
460e552da7Schristos   ASSERT(strcmp(uv_req_type_name(UV_WORK), "work") == 0);
47*5f2f4271Schristos   ASSERT_NULL(uv_req_type_name(UV_REQ_TYPE_MAX));
48*5f2f4271Schristos   ASSERT_NULL(uv_req_type_name(UV_REQ_TYPE_MAX + 1));
49*5f2f4271Schristos   ASSERT_NULL(uv_req_type_name(UV_UNKNOWN_REQ));
500e552da7Schristos   return 0;
510e552da7Schristos }
520e552da7Schristos 
530e552da7Schristos 
TEST_IMPL(getters_setters)540e552da7Schristos TEST_IMPL(getters_setters) {
550e552da7Schristos   uv_loop_t* loop;
560e552da7Schristos   uv_pipe_t* pipe;
570e552da7Schristos   uv_fs_t* fs;
580e552da7Schristos   int r;
590e552da7Schristos 
600e552da7Schristos   loop = malloc(uv_loop_size());
61*5f2f4271Schristos   ASSERT_NOT_NULL(loop);
620e552da7Schristos   r = uv_loop_init(loop);
630e552da7Schristos   ASSERT(r == 0);
640e552da7Schristos 
650e552da7Schristos   uv_loop_set_data(loop, &cookie1);
660e552da7Schristos   ASSERT(loop->data == &cookie1);
670e552da7Schristos   ASSERT(uv_loop_get_data(loop) == &cookie1);
680e552da7Schristos 
690e552da7Schristos   pipe = malloc(uv_handle_size(UV_NAMED_PIPE));
700e552da7Schristos   r = uv_pipe_init(loop, pipe, 0);
710e552da7Schristos   ASSERT(uv_handle_get_type((uv_handle_t*)pipe) == UV_NAMED_PIPE);
720e552da7Schristos 
730e552da7Schristos   ASSERT(uv_handle_get_loop((uv_handle_t*)pipe) == loop);
740e552da7Schristos   pipe->data = &cookie2;
750e552da7Schristos   ASSERT(uv_handle_get_data((uv_handle_t*)pipe) == &cookie2);
760e552da7Schristos   uv_handle_set_data((uv_handle_t*)pipe, &cookie1);
770e552da7Schristos   ASSERT(uv_handle_get_data((uv_handle_t*)pipe) == &cookie1);
780e552da7Schristos   ASSERT(pipe->data == &cookie1);
790e552da7Schristos 
800e552da7Schristos   ASSERT(uv_stream_get_write_queue_size((uv_stream_t*)pipe) == 0);
810e552da7Schristos   pipe->write_queue_size++;
820e552da7Schristos   ASSERT(uv_stream_get_write_queue_size((uv_stream_t*)pipe) == 1);
830e552da7Schristos   pipe->write_queue_size--;
840e552da7Schristos   uv_close((uv_handle_t*)pipe, NULL);
850e552da7Schristos 
860e552da7Schristos   r = uv_run(loop, UV_RUN_DEFAULT);
870e552da7Schristos   ASSERT(r == 0);
880e552da7Schristos 
890e552da7Schristos   fs = malloc(uv_req_size(UV_FS));
900e552da7Schristos   uv_fs_stat(loop, fs, ".", NULL);
910e552da7Schristos 
920e552da7Schristos   r = uv_run(loop, UV_RUN_DEFAULT);
930e552da7Schristos   ASSERT(r == 0);
940e552da7Schristos 
950e552da7Schristos   ASSERT(uv_fs_get_type(fs) == UV_FS_STAT);
960e552da7Schristos   ASSERT(uv_fs_get_result(fs) == 0);
970e552da7Schristos   ASSERT(uv_fs_get_ptr(fs) == uv_fs_get_statbuf(fs));
980e552da7Schristos   ASSERT(uv_fs_get_statbuf(fs)->st_mode & S_IFDIR);
990e552da7Schristos   ASSERT(strcmp(uv_fs_get_path(fs), ".") == 0);
1000e552da7Schristos   uv_fs_req_cleanup(fs);
1010e552da7Schristos 
1020e552da7Schristos   r = uv_loop_close(loop);
1030e552da7Schristos   ASSERT(r == 0);
1040e552da7Schristos 
1050e552da7Schristos   free(pipe);
1060e552da7Schristos   free(fs);
1070e552da7Schristos   free(loop);
1080e552da7Schristos   return 0;
1090e552da7Schristos }
110