int list_add (void) { ret_t ret; size_t s1, s2; chula_list_t list = LIST_HEAD_INIT(list); for (int i=0; i<10; i++) { /* Add a new list entry */ chula_list_get_len (&list, &s1); ret = chula_list_content_add (&list, INT_TO_POINTER(i)); chula_list_get_len (&list, &s2); /* Check return code */ assert (((ret == ret_ok) && (s2 > s1)) || ((ret == ret_nomem) && (s2 <= s1))); } chula_list_content_free (&list, NULL); for (int i=0; i<10; i++) { /* Add a new list entry */ chula_list_get_len (&list, &s1); ret = chula_list_content_add_tail (&list, INT_TO_POINTER(100 + i)); chula_list_get_len (&list, &s2); /* Check return code */ assert (((ret == ret_ok) && (s2 > s1)) || ((ret == ret_nomem) && (s2 <= s1))); } chula_list_content_free (&list, NULL); return 0; }
END_TEST START_TEST (len) { ret_t ret; size_t len; chula_avl_t avl; /* None */ chula_avl_init (&avl); ch_assert (chula_avl_is_empty (AVL_GENERIC(&avl))); ret = chula_avl_len (AVL_GENERIC(&avl), &len); ch_assert (ret == ret_ok); ch_assert (len == 0); /* Single */ chula_avl_add_ptr (&avl, "uno", INT_TO_POINTER(1)); ch_assert (ret == ret_ok); ch_assert (! chula_avl_is_empty (AVL_GENERIC(&avl))); chula_avl_len (AVL_GENERIC(&avl), &len); ch_assert (len == 1); /* Multiple */ chula_avl_add_ptr (&avl, "dos", INT_TO_POINTER(2)); ch_assert (ret == ret_ok); ch_assert (! chula_avl_is_empty (AVL_GENERIC(&avl))); chula_avl_len (AVL_GENERIC(&avl), &len); ch_assert (len == 2); chula_avl_mrproper (AVL_GENERIC(&avl), NULL); }
void test_list_foreach(void) { List *l = list_prepend(NULL, INT_TO_POINTER(1)); l = list_prepend(l, INT_TO_POINTER(2)); l = list_prepend(l, INT_TO_POINTER(3)); int sum = 0; list_foreach(l, foreach_cb, &sum); cut_assert_equal_int(6, sum); }
void test_two_appends(void) { List *l = list_append(NULL, INT_TO_POINTER(1)); cut_assert_not_null(l); cut_assert_null(l->next); cut_assert_equal_pointer(INT_TO_POINTER(1), l->data); l = list_append(l, INT_TO_POINTER(2)); cut_assert_equal_pointer(INT_TO_POINTER(1), l->data); cut_assert_not_null(l->next); cut_assert_equal_pointer(INT_TO_POINTER(2), l->next->data); cut_assert_null(l->next->next); }
static void sx9500_thread_main(int arg1, int unused) { struct device *dev = INT_TO_POINTER(arg1); struct sx9500_data *data = dev->driver_data; uint8_t reg_val; ARG_UNUSED(unused); while (1) { k_sem_take(&data->sem, K_FOREVER); if (i2c_reg_read_byte(data->i2c_master, data->i2c_slave_addr, SX9500_REG_IRQ_SRC, ®_val) < 0) { SYS_LOG_DBG("sx9500: error %d reading IRQ source register", ret); continue; } if ((reg_val & SX9500_CONV_DONE_IRQ) && data->handler_drdy) { data->handler_drdy(dev, &data->trigger_drdy); } if ((reg_val & SX9500_NEAR_FAR_IRQ) && data->handler_near_far) { data->handler_near_far(dev, &data->trigger_near_far); } } }
END_TEST START_TEST (_buf) { ret_t ret; void *p; chula_avl_t avl; chula_buffer_t uno = CHULA_BUF_INIT; chula_buffer_t dos = CHULA_BUF_INIT; chula_buffer_t tres = CHULA_BUF_INIT; chula_avl_init (&avl); chula_buffer_add_str (&uno, "uno"); chula_buffer_add_str (&dos, "dos"); chula_buffer_add_str (&tres, "tres"); ret = chula_avl_add (&avl, &uno, INT_TO_POINTER(1)); ch_assert (ret == ret_ok); ret = chula_avl_add (&avl, &dos, INT_TO_POINTER(2)); ch_assert (ret == ret_ok); ret = chula_avl_add (&avl, &tres, INT_TO_POINTER(3)); ch_assert (ret == ret_ok); chula_avl_get (&avl, &dos, &p); ch_assert (POINTER_TO_INT(p) == 2); chula_avl_get (&avl, &tres, &p); ch_assert (POINTER_TO_INT(p) == 3); chula_avl_get (&avl, &uno, &p); ch_assert (POINTER_TO_INT(p) == 1); chula_avl_del (&avl, &dos, NULL); chula_avl_get (&avl, &uno, &p); ch_assert (POINTER_TO_INT(p) == 1); p = NULL; ret = chula_avl_get (&avl, &dos, &p); ch_assert (ret != ret_ok); ch_assert (p == NULL); ret = chula_avl_check (AVL_GENERIC(&avl)); ch_assert (ret == ret_ok); chula_buffer_mrproper (&uno); chula_buffer_mrproper (&dos); chula_buffer_mrproper (&tres); chula_avl_mrproper (AVL_GENERIC(&avl), NULL); }
static void PerfxMain_AddPending(PerfxMainHandler *handler) /* IN */ { ASSERT(handler); gPending = PerfxList_Prepend(gPending, INT_TO_POINTER(handler->id)); PerfxMain_Wakeup(); }
END_TEST START_TEST (_ptr) { ret_t ret; void *p; chula_avl_t avl; chula_avl_init (&avl); ret = chula_avl_add_ptr (&avl, "uno", INT_TO_POINTER(1)); ch_assert (ret == ret_ok); ret = chula_avl_add_ptr (&avl, "dos", INT_TO_POINTER(2)); ch_assert (ret == ret_ok); ret = chula_avl_add_ptr (&avl, "tres", INT_TO_POINTER(3)); ch_assert (ret == ret_ok); chula_avl_get_ptr (&avl, "dos", &p); ch_assert (POINTER_TO_INT(p) == 2); chula_avl_get_ptr (&avl, "tres", &p); ch_assert (POINTER_TO_INT(p) == 3); chula_avl_get_ptr (&avl, "uno", &p); ch_assert (POINTER_TO_INT(p) == 1); chula_avl_set_case (&avl, true); chula_avl_get_ptr (&avl, "TrEs", &p); ch_assert (POINTER_TO_INT(p) == 3); chula_avl_set_case (&avl, false); chula_avl_del_ptr (&avl, "dos", NULL); chula_avl_get_ptr (&avl, "uno", &p); ch_assert (POINTER_TO_INT(p) == 1); p = NULL; ret = chula_avl_get_ptr (&avl, "dos", &p); ch_assert (ret != ret_ok); ch_assert (p == NULL); ret = chula_avl_check (AVL_GENERIC(&avl)); ch_assert (ret == ret_ok); chula_avl_mrproper (AVL_GENERIC(&avl), NULL); }
int net_context_tcp_send(struct net_buf *buf) { /* Prepare data to be sent */ process_post_synch(&ip_buf_context(buf)->tcp, tcpip_event, INT_TO_POINTER(TCP_WRITE_EVENT), buf); return ip_buf_sent_status(buf); }
static void tmp007_fiber(int dev_ptr, int unused) { struct device *dev = INT_TO_POINTER(dev_ptr); struct tmp007_data *drv_data = dev->driver_data; ARG_UNUSED(unused); while (1) { nano_fiber_sem_take(&drv_data->gpio_sem, TICKS_UNLIMITED); tmp007_fiber_cb(dev); } }
static void lis2ds12_thread(int dev_ptr, int unused) { struct device *dev = INT_TO_POINTER(dev_ptr); struct lis2ds12_data *data = dev->driver_data; ARG_UNUSED(unused); while (1) { k_sem_take(&data->trig_sem, K_FOREVER); lis2ds12_handle_int(dev); } }
static void lis2dw12_thread(int dev_ptr, int unused) { struct device *dev = INT_TO_POINTER(dev_ptr); struct lis2dw12_data *lis2dw12 = dev->driver_data; ARG_UNUSED(unused); while (1) { k_sem_take(&lis2dw12->gpio_sem, K_FOREVER); lis2dw12_handle_interrupt(dev); } }
static void mpu6050_thread(int dev_ptr, int unused) { struct device *dev = INT_TO_POINTER(dev_ptr); struct mpu6050_data *drv_data = dev->driver_data; ARG_UNUSED(unused); while (1) { k_sem_take(&drv_data->gpio_sem, K_FOREVER); mpu6050_thread_cb(dev); } }
const char * PerfxQuark_ToString(PerfxQuark quark) /* IN */ { PerfxQuarkEntry *entry; ASSERT(gQuarks); pthread_rwlock_rdlock(&gRWLock); entry = PerfxHashTable_Lookup(gQuarks, INT_TO_POINTER(quark)); pthread_rwlock_unlock(&gRWLock); return entry ? entry->string : NULL; }
static void _gpio_sch_poll_status(int data, int unused) { struct device *dev = INT_TO_POINTER(data); struct gpio_sch_config *info = dev->config->config_info; struct gpio_sch_data *gpio = dev->driver_data; ARG_UNUSED(unused); /* Cleaning up GTS first */ _write_gts(_read_gts(info->regs), info->regs); while (gpio->poll) { uint32_t status; if (!gpio->callback) { goto loop; } status = _read_gts(info->regs); if (!status) { goto loop; } if (gpio->port_cb) { gpio->callback(dev, status); goto ack; } if (gpio->cb_enabled) { uint32_t valid_int = status & gpio->cb_enabled; int i; for (i = 0; i < info->bits; i++) { if (valid_int & BIT(i)) { gpio->callback(dev, i); } } } ack: /* It's not documented but writing the same status value * into GTS tells to the controller it got handled. */ _write_gts(status, info->regs); loop: nano_fiber_timer_start(&gpio->poll_timer, GPIO_SCH_POLLING_TICKS); nano_fiber_timer_test(&gpio->poll_timer, TICKS_UNLIMITED); } }
int main (int argc, /* IN */ char *argv[]) /* IN */ { PerfxHashTable *hashtable; uint32_t i; hashtable = PerfxHashTable_Create(1024, perfx_direct_hash, perfx_direct_equal, NULL, NULL); for (i = 0; i < 100000; i++) { PerfxHashTable_Insert(hashtable, INT_TO_POINTER(i), INT_TO_POINTER(i)); } ASSERT_INT(PerfxHashTable_CountKeys(hashtable), ==, 100000); for (i = 0; i < 100000; i++) { ASSERT(PerfxHashTable_Contains(hashtable, INT_TO_POINTER(i))); } ASSERT(!PerfxHashTable_Contains(hashtable, INT_TO_POINTER(100001))); PerfxHashTable_Free(hashtable); return 0; }
static void arbiter_conn_cb(struct ev_loop *l,struct ev_io *watcher,int revents){ zero_arbiter_thread *th = watcher->data; int cfd; if((cfd = accept(th->sockfd,NULL,NULL)) == -1){ perror("accept error"); return; } sock_set_non_block(cfd); int idx; idx = (th->last_thread + 1) % (th->server->worker_len); th->last_thread = idx; zero_worker_thread *worker = th->server->ze_workers + idx; zero_queue_push(worker->queue,INT_TO_POINTER(cfd)); uint64_t n = 1; write(worker->notify_fd,&n,sizeof(n)); }
int fchdir(void *x, int fd) { void *ptr; if (hashtable_get(HT_FROM_P(x), INT_TO_POINTER(fd), &ptr)) { if (NULL == ptr) { // errno ? return -1; } else { return chdir(ptr); } } else { // errno ? return -1; } }
int main(int argc , char ** argv){ printf("test client begin!\n"); rpc_client *client = rpc_client_new(); rpc_client_connect(client,"127.0.0.1",8778); printf("test client started success!"); pointer output; size_t len; char input[256]; int i, result; rpc_code code; for (i = 0; i < 10000; i++) { len = sprintf(input, "%d %d", i, i + 1); printf("call!\n"); code = rpc_client_call(client, "TestService", "add", input, strlen( input) + 1, &output, &len); if (code != RPC_OK) { printf("call error %s\n", rpc_code_format(code)); exit(1); } result = *(int*) output; if (result != (2 * i + 1)) { printf("i is %d,result:%d should %d\n", i, result, 2 * i + 1); exit(1); } printf("result:%d\n", result); } int j; for (j = 0; j < 10; j++) { for (i = 0; i < 10000; i++) { len = sprintf(input, "%d %d", i, i + 1); rpc_client_call_async(client, "TestService", "add", strdup(input), strlen(input) + 1, cb_add, INT_TO_POINTER(i)); } rpc_sleep(1000); } printf("test ok\n"); for (;;) { rpc_sleep(1000); } return EXIT_SUCCESS; }
static void cb_dispatch_conn(struct ev_loop *l, struct ev_io *watcher, int revents) { rpc_dispatch_thread *th = watcher->data; int cfd; if ((cfd = accept(th->sock_fd, NULL, NULL)) == -1) { //check errno perror("accept conn error"); return; } rpc_set_non_block(cfd); //rrd thread int idx; idx = (th->last_thread + 1) % (th->server->worker_len); th->last_thread = idx; rpc_worker_thread *worker = th->server->th_workers + idx; //notify rpc_queue_push(worker->queue, INT_TO_POINTER(cfd)); uint64_t n = 1; write(worker->notify_fd, &n, sizeof(n)); }
void test_reverse_list(void) { List *l = list_prepend(NULL, INT_TO_POINTER(1)); l = list_prepend(l, INT_TO_POINTER(2)); l = list_prepend(l, INT_TO_POINTER(3)); l = list_reverse(l); cut_assert_not_null(l); cut_assert_equal_pointer(INT_TO_POINTER(1), l->data); cut_assert_not_null(l->next); cut_assert_equal_pointer(INT_TO_POINTER(2), l->next->data); cut_assert_not_null(l->next->next); cut_assert_equal_pointer(INT_TO_POINTER(3), l->next->next->data); cut_assert_null(l->next->next->next); }