1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <functional>
#include <iostream>
#include <memory>
#include <cm3p/uv.h>
#include "cmGetPipes.h"
#include "cmUVHandlePtr.h"
static bool testBool()
{
cm::uv_async_ptr async;
cm::uv_handle_ptr handle;
cm::uv_idle_ptr idle;
cm::uv_pipe_ptr pipe;
cm::uv_process_ptr process;
cm::uv_signal_ptr signal;
cm::uv_stream_ptr stream;
cm::uv_timer_ptr timer;
cm::uv_tty_ptr tty;
return !async && !handle && !idle && !pipe && !process && !signal &&
!stream && !timer && !tty;
}
static bool testIdle()
{
bool idled = false;
cm::uv_loop_ptr loop;
loop.init();
auto cb = [](uv_idle_t* handle) {
auto idledPtr = static_cast<bool*>(handle->data);
*idledPtr = true;
uv_idle_stop(handle);
};
cm::uv_idle_ptr idle;
idle.init(*loop, &idled);
idle.start(cb);
uv_run(loop, UV_RUN_DEFAULT);
if (!idled) {
std::cerr << "uv_idle_ptr did not trigger callback" << std::endl;
return false;
}
idled = false;
idle.start(cb);
idle.stop();
uv_run(loop, UV_RUN_DEFAULT);
if (idled) {
std::cerr << "uv_idle_ptr::stop did not stop callback" << std::endl;
return false;
}
return true;
}
static bool testTimer()
{
bool timed = false;
cm::uv_loop_ptr loop;
loop.init();
auto cb = [](uv_timer_t* handle) {
auto timedPtr = static_cast<bool*>(handle->data);
*timedPtr = true;
uv_timer_stop(handle);
};
cm::uv_timer_ptr timer;
timer.init(*loop, &timed);
timer.start(cb, 10, 0, cm::uv_update_time::no);
uv_run(loop, UV_RUN_DEFAULT);
if (!timed) {
std::cerr << "uv_timer_ptr did not trigger callback" << std::endl;
return false;
}
timed = false;
timer.start(cb, 10, 0, cm::uv_update_time::no);
timer.stop();
uv_run(loop, UV_RUN_DEFAULT);
if (timed) {
std::cerr << "uv_timer_ptr::stop did not stop callback" << std::endl;
return false;
}
return true;
}
static bool testWriteCallback()
{
int pipe[] = { -1, -1 };
if (cmGetPipes(pipe) < 0) {
std::cout << "cmGetPipes() returned an error" << std::endl;
return false;
}
cm::uv_loop_ptr loop;
loop.init();
cm::uv_pipe_ptr pipeRead;
pipeRead.init(*loop, 0);
uv_pipe_open(pipeRead, pipe[0]);
cm::uv_pipe_ptr pipeWrite;
pipeWrite.init(*loop, 0);
uv_pipe_open(pipeWrite, pipe[1]);
char c = '.';
uv_buf_t buf = uv_buf_init(&c, sizeof(c));
int status = -1;
auto cb = std::make_shared<std::function<void(int)>>(
[&status](int s) { status = s; });
// Test getting a callback after the write is done.
cm::uv_write(pipeWrite, &buf, 1, cb);
uv_run(loop, UV_RUN_DEFAULT);
if (status != 0) {
std::cout << "cm::uv_write non-zero status: " << status << std::endl;
return false;
}
// Test deleting the callback before it is made.
status = -1;
cm::uv_write(pipeWrite, &buf, 1, cb);
cb.reset();
uv_run(loop, UV_RUN_DEFAULT);
if (status != -1) {
std::cout << "cm::uv_write callback incorrectly called with status: "
<< status << std::endl;
return false;
}
return true;
}
int testUVHandlePtr(int, char** const)
{
bool passed = true;
passed = testBool() && passed;
passed = testIdle() && passed;
passed = testTimer() && passed;
passed = testWriteCallback() && passed;
return passed ? 0 : -1;
}
|