void test_concat_append_add_union_intersection_difference(void)
{     
     List list1 = list_random(MAX_LIST_SIZE, ELEMENT_MAX_VALUE);
     list_sort_by_item(&list1);
     list_unique(&list1);
     printf("Original List 1 ::: ");
     list_print(&list1);

     List list2 = list_random(MAX_LIST_SIZE, ELEMENT_MAX_VALUE);
     list_sort_by_item(&list2);
     list_unique(&list2);
     printf("%sOriginal List 2 ::: ", brown);
     list_print(&list2);

     List concat = list_concat(&list1, &list2);
     printf("%sConcatenation List ::: ", cyan);
     list_print(&concat);

     List append = list_duplicate(&list1);
     list_append(&append, &list2);
     printf("%sList 1 appended to list 2 ::: ", magenta);
     list_print(&append);
     
     List add = list_duplicate(&list1);
     list_add(&add, &list2);
     printf("%sList 1 added to list 2 ::: ", green);
     list_print(&add);

     List union_list = list_union(&list1, &list2);	
     printf("%sList Union ::: ", blue);
     list_print(&union_list);

     List intersection_list = list_intersection(&list1, &list2);
     printf("%sList Intersection ::: ", red);
     list_print(&intersection_list);

     List difference_list = list_difference(&list1, &list2);
     printf("%sDifference List ::: ", cyan);
     list_print(&difference_list);

     uint union_size = list_union_size(&list1, &list2);
     printf("%sList Union Size = %d\n", brown, union_size);

     uint intersection_size = list_intersection_size(&list1, &list2);
     printf("%sList Intersection Size = %d\n", black, intersection_size);

     uint difference_size = list_difference_size(&list1, &list2);
     printf("%sList Difference Size = %d\n", black, difference_size);
     
     printf("%s",none);

}
void test_insert_duplicate_copy(void)
{   
     List list = list_random(MAX_LIST_SIZE, ELEMENT_MAX_VALUE);
     printf("Random List ::: ");
     list_print(&list);

     List duplicate = list_duplicate(&list);
     printf("%sDuplicate List ::: ", cyan);
     list_print(&duplicate);

     uint low = rand() % list.size;
     uint high = (rand() % (list.size - low)) + low;
     List copy = list_copy_range(&list, low, high);
     printf("%sCopy List Range %d-%d::: ", red, low, high);
     list_print(&copy);
     
     Item random_item;
     random_item.item = rand() % ELEMENT_MAX_VALUE;
     random_item.freq = 1;
     uint random_position = rand() % list.size;
     list_insert(&list, random_item, random_position);
     printf("%sRandom List with %d inserted in %d ::: ", green, random_item.item, random_position);
     list_print(&list);
     printf("%s",none);
}
Exemple #3
0
ListData* list_touch(ListData* original)
{
    if (original == NULL)
        return NULL;

    if (!original->immutable)
        return original;

    ListData* copy = list_duplicate(original);
    list_decref(original);
    return copy;
}
void test_less_more_frequent(void)
{
     List list = list_random(MAX_LIST_SIZE, ELEMENT_MAX_VALUE);
     list_sort_by_item(&list);
     list_unique(&list);
     list_sort_by_frequency_back(&list);
     printf("Original List ::: ");
     list_print(&list);

     List duplicate = list_duplicate(&list);
     list_delete_less_frequent(&duplicate, 5);
     printf("%sList without less frequent items ::: ", cyan);
     list_print(&duplicate);
   
     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     list_delete_more_frequent(&duplicate, 8);	
     list_sort_by_frequency_back(&duplicate);
     printf("%sList without more frequent items ::: ", magenta);
     list_print(&duplicate);
     printf ("%s",none);
}
void test_pop_delete(void)
{
     List list = list_random(MAX_LIST_SIZE, ELEMENT_MAX_VALUE);
     list_sort_by_item(&list);
     list_unique(&list);
     printf("Original List ::: ");
     list_print(&list);

     List duplicate = list_duplicate(&list);
     list_pop(&duplicate);
     printf("%sList 1 without last item ::: ", cyan);
     list_print(&duplicate);
   
     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     uint random_value = rand() % duplicate.size;
     list_pop_multi(&duplicate, random_value);
     printf("%sList without last %d items ::: ", brown, random_value);
     list_print(&duplicate);
	      
     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     uint random_end = rand() % duplicate.size;
     list_pop_until(&duplicate, random_end);
     printf("%sList until %d ::: ", magenta, random_end);
     list_print(&duplicate);

     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     uint random_position = rand() % (duplicate.size);
     list_delete_position(&duplicate, random_position);
     printf("%sList wihout %d-d item ::: ", green, random_position);
     list_print(&duplicate);
     printf("%s", none);

     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     Item random_item;
     random_position = rand() % (duplicate.size);
     random_item = duplicate.data[random_position];
     list_delete_item(&duplicate, random_item);
     printf("%sList without %d ::: ", red, random_item.item);
     list_print(&duplicate);
     printf("%s", none);
     
     list_destroy(&duplicate);
     duplicate = list_duplicate(&list);
     uint low = rand() % duplicate.size;
     uint high = (rand() % (duplicate.size - low)) + low;
     list_delete_range(&duplicate, low, high);
     printf("%sList without range %d - %d ::: ", blue, low, high);
     list_print(&duplicate);
     printf("%s", none);
}
/* The descendant footprint of a node is defined as a balance between
 * the widest point of the children branches, while still maintaining
 * the existance of the sibling branches. The assumption is that by
 * knowing the larget size needed, all other branches can be executed
 * within that designated size, so we only need to add the residual
 * size of a branch to hold onto it while the heavier weights are
 * computed. */
void dag_node_footprint_determine_descendant(struct dag_node *n)
{
	struct dag_node *node1, *node2; //, *res_node;
	struct list *tmp_direct_children = list_create();
	struct set *footprint = set_create(0);
	uint64_t footprint_size = 0;

	/* Create a second list of direct children that allows us to
		sort on footprint properties. This is used
		when we compare footprint and the residual nodes. */
	set_first_element(n->footprint->direct_children);
	while((node1 = set_next_element(n->footprint->direct_children))){
		list_push_tail(tmp_direct_children, node1);
		list_first_item(node1->footprint->residual_nodes);
	}

	/* There are two cases for descendant nodes:
		1. Multiple direct_children indicating that multiple branches will
			need to be maintained concurrently and we need to account.
		2. One descendant indicating we want to continue the chain
			of residual and footprints that out child holds.
			create empty lists for this case.
	*/
	set_first_element(n->footprint->direct_children);
	if(set_size(n->footprint->direct_children) > 1){
		dag_node_footprint_determine_desc_residual_intersect(n);

		dag_node_footprint_set_desc_res_wgt_diff(n);

		set_insert_list(footprint, n->target_files);

		list_sort(tmp_direct_children, dag_node_footprint_comp_diff);
		list_first_item(tmp_direct_children);
		/* Loop over each child giving it the chance to be the largest footprint. */
		while((node1 = list_next_item(tmp_direct_children))){
			footprint_size = dag_file_set_size(footprint);
			if((footprint_size + node1->footprint->wgt) > n->footprint->delete_footprint){
				set_delete(n->footprint->delete_files);
				n->footprint->delete_files = set_duplicate(footprint);
				set_insert_set(n->footprint->delete_files, node1->footprint->wgt_files);
				n->footprint->delete_footprint = dag_file_set_size(n->footprint->delete_files);

			}
			// This is where we would remove an input file if it wasn't needed for other branches
			set_insert_set(footprint, node1->footprint->res_files);
			list_push_tail(n->footprint->delete_run_order, node1);
		}

		list_sort(tmp_direct_children, dag_node_footprint_comp_wgt_rev);
		list_first_item(tmp_direct_children);
		node1 = list_next_item(tmp_direct_children);

		set_insert_set(n->footprint->prog_max_files, node1->footprint->max_wgt_files);
		set_insert_set(n->footprint->prog_min_files, node1->footprint->wgt_files);
		list_push_tail(n->footprint->prog_run_order, node1);

		/* Find what the total space is needed to hold all residuals and
			the largest footprint branch concurrently. */
		while((node2 = list_next_item(tmp_direct_children))){
			set_insert_set(n->footprint->prog_max_files, node2->footprint->max_wgt_files);
			set_insert_set(n->footprint->prog_min_files, node2->footprint->res_files);
			list_push_tail(n->footprint->prog_run_order, node2);
		}

		n->footprint->prog_max_footprint = dag_file_set_size(n->footprint->prog_max_files);
		n->footprint->prog_min_footprint = dag_file_set_size(n->footprint->prog_min_files);
	} else {
		if(set_size(n->footprint->direct_children) == 1){
			node1 = set_next_element(n->footprint->direct_children);
			list_delete(n->footprint->residual_nodes);
			n->footprint->residual_nodes = list_duplicate(node1->footprint->residual_nodes);
		}

		set_insert_list(n->footprint->residual_files, n->target_files);
		set_insert_set(n->footprint->residual_files, n->footprint->terminal_files);
		n->footprint->residual_size = dag_file_set_size(n->footprint->residual_files);
	}

	/* Adding the current nodes list so parents can quickly access
		these decisions. */
	list_push_tail(n->footprint->residual_nodes, n);

	list_delete(tmp_direct_children);
	set_delete(footprint);
}
Exemple #7
0
int sign_message(struct s3_message* mesg, const char* user, const char * key) {
	int sign_str_len = 0;
	char *sign_str;
	char date[1024];
	struct s3_header_object *amz;
	struct list *amz_headers;
	char digest[SHA1_DIGEST_LENGTH];
	char string[SHA1_DIGEST_LENGTH*2];
	int result;
	memset(digest, 0, SHA1_DIGEST_LENGTH);
	memset(string, 0, SHA1_DIGEST_LENGTH*2);

	switch(mesg->type) {
		case S3_MESG_GET: 
			sign_str_len += 4;
			break;
		case S3_MESG_POST: 
			sign_str_len += 5;
			break;
		case S3_MESG_PUT:
			sign_str_len += 4;
			break;
		case S3_MESG_DELETE:
			sign_str_len += 7;
			break;
		case S3_MESG_HEAD:
			sign_str_len += 5;
			break;
		case S3_MESG_COPY:
			sign_str_len += 4;
			break;
		default:
			return -1;
	}

	if(mesg->content_md5) sign_str_len += strlen(mesg->content_md5);
	sign_str_len += 1;
	if(mesg->content_type) sign_str_len += strlen(mesg->content_type);
	sign_str_len += 1;

	strftime(date, 1024, "%a, %d %b %Y %H:%M:%S %Z", gmtime(&mesg->date));
	sign_str_len += strlen(date) + 1;

	if(mesg->amz_headers) {	
		list_first_item(mesg->amz_headers);
		while( (amz = (struct s3_header_object *)list_next_item(mesg->amz_headers)) ) {
			if(amz->type < S3_HEADER_CUSTOM) continue;
			switch(amz->type) {
				case S3_HEADER_CUSTOM:
					if(!amz->custom_type) return -1;
					sign_str_len += strlen(amz->custom_type) + 1;
					break;
				default:
					sign_str_len += strlen(s3_get_header_string(amz->type, amz->custom_type)) + 1;
					break;
			}
			if(!amz->value) return -1;
			sign_str_len += strlen(amz->value) + 1;
		}
	}
	if(!mesg->bucket || !mesg->path) {
		return -1;
	}
	sign_str_len += 1 + strlen(mesg->bucket) + 1 + strlen(mesg->path) + 1;
	
	sign_str = malloc(sign_str_len);
	if(!sign_str) return -1;
	memset(sign_str, 0, sign_str_len);

	switch(mesg->type) {
		case S3_MESG_GET: 
			sprintf(sign_str, "GET\n");
			break;
		case S3_MESG_POST: 
			sprintf(sign_str, "POST\n");
			break;
		case S3_MESG_PUT:
			sprintf(sign_str, "PUT\n");
			break;
		case S3_MESG_DELETE:
			sprintf(sign_str, "DELETE\n");
			break;
		case S3_MESG_HEAD:
			sprintf(sign_str, "HEAD\n");
			break;
		case S3_MESG_COPY:
			sprintf(sign_str, "PUT\n");
			break;
	}

	if(mesg->content_md5) sprintf(sign_str, "%s%s\n", sign_str,mesg->content_md5);
			else sprintf(sign_str, "%s\n", sign_str);
	if(mesg->content_type) sprintf(sign_str, "%s%s\n", sign_str, mesg->content_type);
			else sprintf(sign_str, "%s\n", sign_str);
	sprintf(sign_str, "%s%s", sign_str, date);

	if(mesg->amz_headers) {
		amz_headers = list_sort(list_duplicate(mesg->amz_headers), &s3_header_object_comp);
		list_first_item(amz_headers);
		{	int c_type = -1;
			char* c_ctype = NULL;
			while( (amz = (struct s3_header_object *)list_next_item(amz_headers)) ) {
				if(amz->type < S3_HEADER_CUSTOM) continue;
				if(c_type != amz->type) {
					c_type = amz->type;
					c_ctype = amz->custom_type;
					sprintf(sign_str, "%s\n%s:%s", sign_str, s3_get_header_string(amz->type, amz->custom_type), amz->value);

				} else if(c_type == S3_HEADER_CUSTOM && strcmp(c_ctype, amz->custom_type)) {
					c_ctype = amz->custom_type;
					sprintf(sign_str, "%s\n%s:%s", sign_str, amz->custom_type, amz->value);
				} else {
					sprintf(sign_str, "%s,%s", sign_str, amz->value);
				}
			}
		}
		list_delete(amz_headers);
	}

	sprintf(sign_str, "%s\n/%s%s", sign_str, mesg->bucket, mesg->path);
	if((result = hmac_sha1(sign_str, strlen(sign_str), key, strlen(key), (unsigned char*)digest))) return result;

	hmac_sha1(sign_str, strlen(sign_str), key, strlen(key), (unsigned char*)digest);
	b64_encode(digest, SHA1_DIGEST_LENGTH, string, SHA1_DIGEST_LENGTH*2);

	sprintf(mesg->authorization, "AWS %s:%s", user, string);
	free(sign_str);
	return 0;
}
Exemple #8
0
void atom_set_list(Atom* atom, const List* list) {
  atom_reset(atom);
  atom->type = ATOM_TYPE_LIST;
  atom->list = list_duplicate(list);
}