int main(int argc, char **argv) { BEGIN_TESTS(); { /* test when inserting more than array size */ struct core_queue queue; int i; core_queue_init(&queue, sizeof(int)); TEST_INT_EQUALS(core_queue_empty(&queue), 1); i = 16; while (i--) { TEST_INT_EQUALS(core_queue_enqueue(&queue, &i), 1); } i = 16; while (i--) { TEST_INT_EQUALS(core_queue_enqueue(&queue, &i), 1); } TEST_INT_EQUALS(core_queue_full(&queue), 0); i = 16; while (i--) { int item; TEST_INT_EQUALS(core_queue_dequeue(&queue, &item), 1); } i = 16; while (i--) { int item; TEST_INT_EQUALS(core_queue_dequeue(&queue, &item), 1); } core_queue_destroy(&queue); } { /* push 1000 elements, and verify them after */ struct core_queue queue; int i; core_queue_init(&queue, sizeof(int)); TEST_INT_EQUALS(core_queue_empty(&queue), 1); i = 1000; while (i--) { TEST_INT_EQUALS(core_queue_enqueue(&queue, &i), 1); } TEST_INT_EQUALS(core_queue_full(&queue), 0); i = 1000; while (i--) { int item; TEST_INT_EQUALS(core_queue_dequeue(&queue, &item), 1); TEST_INT_EQUALS(item, i); /* printf("%i %i\n", item, i); */ } TEST_INT_EQUALS(core_queue_empty(&queue), 1); core_queue_destroy(&queue); } { /* use array size of 1 and 2000 elements */ struct core_queue queue; int i; core_queue_init(&queue, sizeof(int)); TEST_INT_EQUALS(core_queue_empty(&queue), 1); i = 2000; while (i--) { TEST_INT_EQUALS(core_queue_enqueue(&queue, &i), 1); } TEST_INT_EQUALS(core_queue_full(&queue), 0); i = 2000; while (i--) { int item; TEST_INT_EQUALS(core_queue_dequeue(&queue, &item), 1); TEST_INT_EQUALS(item, i); /* printf("%i %i\n", item, i); */ } TEST_INT_EQUALS(core_queue_empty(&queue), 1); core_queue_destroy(&queue); } { /* stress test the code by inserting one element, and then removing. */ struct core_queue queue; int i; int expected; core_queue_init(&queue, sizeof(int)); TEST_INT_EQUALS(core_queue_empty(&queue), 1); i = 3000; while (i--) { expected = i; TEST_INT_EQUALS(core_queue_enqueue(&queue, &i), 1); TEST_INT_EQUALS(core_queue_dequeue(&queue, &i), 1); TEST_INT_EQUALS(i, expected); TEST_INT_EQUALS(core_queue_empty(&queue), 1); TEST_INT_EQUALS(core_queue_enqueue(&queue, &i), 1); TEST_INT_EQUALS(core_queue_dequeue(&queue, &i), 1); TEST_INT_EQUALS(i, expected); } TEST_INT_EQUALS(core_queue_full(&queue), 0); TEST_INT_EQUALS(core_queue_empty(&queue), 1); /* * At any time, there is 0 or 1 elements in the queue. */ /* core_queue_print(&queue); */ TEST_INT_IS_LOWER_THAN(core_queue_capacity(&queue), 100); core_queue_destroy(&queue); } { struct core_queue queue; int i; int value; core_queue_init(&queue, sizeof(int)); #if 0 printf("Test 3\n"); #endif TEST_INT_EQUALS(core_queue_empty(&queue), 1); i = 3000; while (i--) { value = i; TEST_INT_EQUALS(core_queue_enqueue(&queue, &value), 1); TEST_INT_EQUALS(core_queue_enqueue(&queue, &value), 1); TEST_INT_EQUALS(core_queue_dequeue(&queue, &value), 1); } #if 0 printf("Test 3, size %d\n", core_queue_size(&queue)); #endif core_queue_destroy(&queue); } { struct core_queue queue; int i; int value; core_queue_init(&queue, sizeof(int)); #if 0 printf("Test 4\n"); #endif TEST_INT_EQUALS(core_queue_empty(&queue), 1); i = 3000000; while (i--) { value = i; TEST_INT_EQUALS(core_queue_enqueue(&queue, &value), 1); TEST_INT_EQUALS(core_queue_enqueue(&queue, &value), 1); TEST_INT_EQUALS(core_queue_dequeue(&queue, &value), 1); TEST_INT_EQUALS(core_queue_dequeue(&queue, &value), 1); } #if 0 printf("Test 4, size %d\n", core_queue_size(&queue)); #endif core_queue_destroy(&queue); } END_TESTS(); return 0; }
void biosal_sequence_partitioner_receive(struct thorium_actor *actor, struct thorium_message *message) { int tag; int source; int count; void *buffer; int bytes; struct biosal_sequence_partitioner *concrete_actor; struct biosal_partition_command command; struct thorium_message response; int command_number; struct biosal_partition_command *active_command; int stream_index; struct biosal_partition_command *command_bucket; int i; thorium_message_get_all(message, &tag, &count, &buffer, &source); concrete_actor = (struct biosal_sequence_partitioner *)thorium_actor_concrete_actor(actor); if (tag == ACTION_SEQUENCE_PARTITIONER_SET_BLOCK_SIZE) { thorium_message_unpack_int(message, 0, &concrete_actor->block_size); thorium_actor_send_reply_empty(actor, ACTION_SEQUENCE_PARTITIONER_SET_BLOCK_SIZE_REPLY); biosal_sequence_partitioner_verify(actor); /* printf("DEBUG biosal_sequence_partitioner_receive received block size\n"); */ } else if (tag == ACTION_SEQUENCE_PARTITIONER_SET_ENTRY_VECTOR) { /* printf("DEBUG biosal_sequence_partitioner_receive unpacking vector, %d bytes\n", count); */ core_vector_init(&concrete_actor->stream_entries, 0); core_vector_unpack(&concrete_actor->stream_entries, buffer); /* printf("DEBUG after unpack\n"); */ thorium_actor_send_reply_empty(actor, ACTION_SEQUENCE_PARTITIONER_SET_ENTRY_VECTOR_REPLY); /* printf("DEBUG biosal_sequence_partitioner_receive received received entry vector\n"); */ biosal_sequence_partitioner_verify(actor); } else if (tag == ACTION_SEQUENCE_PARTITIONER_SET_ACTOR_COUNT) { thorium_message_unpack_int(message, 0, &concrete_actor->store_count); thorium_actor_send_reply_empty(actor, ACTION_SEQUENCE_PARTITIONER_SET_ACTOR_COUNT_REPLY); biosal_sequence_partitioner_verify(actor); /* printf("DEBUG biosal_sequence_partitioner_receive received received store count\n"); */ } else if (tag == ACTION_SEQUENCE_PARTITIONER_GET_COMMAND) { if (core_queue_dequeue(&concrete_actor->available_commands, &command)) { bytes = biosal_partition_command_pack_size(&command); /* printf("DEBUG partitioner has command, packing %d bytes!\n", bytes); */ buffer = thorium_actor_allocate(actor, bytes); biosal_partition_command_pack(&command, buffer); thorium_message_init(&response, ACTION_SEQUENCE_PARTITIONER_GET_COMMAND_REPLY, bytes, buffer); thorium_actor_send_reply(actor, &response); /* store the active command */ command_number = biosal_partition_command_name(&command); command_bucket = (struct biosal_partition_command *)core_map_add(&concrete_actor->active_commands, &command_number); *command_bucket = command; /* there may be other command available too ! */ } } else if (tag == ACTION_SEQUENCE_PARTITIONER_GET_COMMAND_REPLY_REPLY) { /* * take the name of the command, find it in the active * command, generate a new command, and send ACTION_SEQUENCE_PARTITIONER_COMMAND_IS_READY * as a reply */ thorium_message_unpack_int(message, 0, &command_number); active_command = core_map_get(&concrete_actor->active_commands, &command_number); if (active_command == NULL) { return; } stream_index = biosal_partition_command_stream_index(active_command); active_command = NULL; core_map_delete(&concrete_actor->active_commands, &command_number); biosal_sequence_partitioner_generate_command(actor, stream_index); if (core_map_size(&concrete_actor->active_commands) == 0 && core_queue_size(&concrete_actor->available_commands) == 0) { thorium_actor_send_reply_empty(actor, ACTION_SEQUENCE_PARTITIONER_FINISHED); } } else if (tag == ACTION_ASK_TO_STOP && source == thorium_actor_supervisor(actor)) { #ifdef BIOSAL_SEQUENCE_PARTITIONER_DEBUG printf("DEBUG biosal_sequence_partitioner_receive ACTION_ASK_TO_STOP\n"); #endif thorium_actor_send_to_self_empty(actor, ACTION_STOP); } else if (tag == ACTION_SEQUENCE_PARTITIONER_PROVIDE_STORE_ENTRY_COUNTS_REPLY) { /* generate commands */ for (i = 0; i < core_vector_size(&concrete_actor->stream_entries); i++) { biosal_sequence_partitioner_generate_command(actor, i); } } }