Anonymous avatar Anonymous committed 5c6adfb

Comments (0)

Files changed (4)

 /* Here we define our menu operations */
 static int
 action_new (PedPartition **part) {
-	PedPartition *temp = (*part)->prev;
+	PedPartition *temp = disk_get_prev_nmd_partition (disk,*part);
 	static MenuItem part_position[] = {
 		{ 's', N_("Begining"), N_("Create the partition at the begining of the free space") },
 		{ 'e', N_("End"), N_("Create the partition at the end of the free space") },
 		return 0;
 
 	if (!do_mkpart(disk,start,end,type,NULL,part,opts,&uiquery)) {
-		if (!*part) 
-			*part = temp->next;
+		if (!*part) {
+			do {
+				temp = ped_disk_next_partition(disk,temp);
+			} while (temp && temp->type & PED_PARTITION_METADATA);
+			*part = temp;
+		}
 		warning_waitkey(N_("Can't create partition."));
 		return 0;
 	}

tests/check_common.c

 }
 END_TEST
 
+
+/* We will give the partitions below different sizes. I'll use rand */
 #define PART_LENGTH (2097151LL - ((unsigned int) rand() % 1048575))
 
 static void
-next_part_geometry(PedGeometry **geom, PedConstraint *constraint) {
+next_part_geometry(PedGeometry **geom, PedConstraint *constraint, int factor) {
 	PedSector start;
 	PedGeometry *temp;
 	if (!*geom)
 		start = (*geom)->end+1LL;
 		ped_geometry_destroy(*geom);
 	}
-	temp = ped_geometry_new(dev, start, PART_LENGTH);
+	temp = ped_geometry_new(dev, start, PART_LENGTH*(long long)factor);
 	*geom = ped_constraint_solve_nearest(constraint,temp);
 	ped_geometry_destroy(temp);
 }
 		case 2:
 			
 			string[0][BUFSIZE] = 2;
-			snprintf(string[0],BUFSIZE,"%llus",start);
-			snprintf(string[1],BUFSIZE,"%llus",end);
+			snprintf(string[0],BUFSIZE,"%llds",start);
+			snprintf(string[1],BUFSIZE,"%llds",end);
 			return do_mkpart(disk,0LL,0LL,type,fs_type,newpart,
 			                 UI_CUSTOM_VALUES,&uiquery);
 	}
 	PedPartition *part = NULL;
 	int i;
 	/* First we create an extended partition */
+	fail_unless(do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery),
+	            "We could create the label for juggling partitions");
 	fail_unless(do_mkpart(disk,0LL,20000LL,PED_PARTITION_EXTENDED,NULL,NULL,
 	                      UI_DEFAULT,&uiquery),
 	            "Failed to create an extended partition");
 	fail_unless(are_near(constraint->start_range->start,part->geom.start)
 	            && are_near(constraint->end_range->end,part->geom.end),
 	            "Maximize didn't work correctly.\n"
-	            "We wanted %llu-%llu, we got %llu-%llu",
+	            "We wanted %lld-%lld, we got %lld-%lld",
 	            constraint->start_range->start, constraint->end_range->end,
 	            part->geom.start, part->geom.end);
 	constraint = constraint_intersect_and_destroy(constraint,
 	           constraint_from_start_end(dev,&(part->geom),&(part->geom)));
 
-	/* We will give the partitions below different sizes. I'll use rand */
+	
 	srand(218872305);
 	walk = ped_file_system_type_get_next (NULL);
 	/* We first try to create 20 partitions */
 	for (i = 0; i < 20; i++) {
-		next_part_geometry(&geom, constraint);
+		next_part_geometry(&geom, constraint, 1);
 
 		fail_unless(_mkpart (i % 3, geom->start,  geom->end,
 				PED_PARTITION_LOGICAL, walk, &part),
 			"Failed to create %s partition. "
-			"Number %d, size %llus, start %llus, end %llus",
+			"Number %d, size %llds, start %llds, end %llds",
 			walk->name, i, geom->length, geom->start, geom->end);
 
 		fail_unless(are_near(geom->start,part->geom.start) &&
 			are_near(geom->end,part->geom.end),
 			"Failed to create %s partition where we want. "
-			"Number %d, we wanted %llu-%llu, we got %llu-%llu",
+			"Number %d, we wanted %lld-%lld, we got %lld-%lld",
 			walk->name, i, geom->start, geom->end,
 			part->geom.start, part->geom.end);
 		ped_geometry_set (geom,part->geom.start,part->geom.length);
 			part = part->prev;
 		} while (part && part->type & PED_PARTITION_METADATA);
 		if (part && (part->type & PED_PARTITION_FREESPACE))
-			fail("We reached a hole at %llus-%llus, number %d",
+			fail("We reached a hole at %llds-%llds, number %d",
 			     part->geom.start, part->geom.end, i);
 		fail_unless (do_rm(disk,temp,&uiquery),
 		             "Failed to delete partition after %d",
 }
 END_TEST
 
+static void
+get_max_geometry(PedGeometry **geom, PedConstraint *constraint, 
+                 PedPartition *part)
+{
+	PedGeometry *temp = NULL;
+	if (*geom) ped_geometry_destroy(*geom);
+	PedSector start, end;
+	int i = 0;
 
+	if (part->prev && part->prev->type & PED_PARTITION_FREESPACE)
+		start = part->prev->geom.start;
+	else
+		start = part->geom.start;
+	if (part->next && part->next->type & PED_PARTITION_FREESPACE)
+		end = part->next->geom.end;
+	else
+		end = part->geom.end;	
+	temp = ped_geometry_new(dev, start, end-start+1LL);
+	*geom = ped_constraint_solve_nearest(constraint,temp);
+	ped_geometry_destroy(temp);
+
+}
+
+/* NOTE: If you create a rather big test disk, you can make this use llrand 
+         from functions.c instead. */
+static void
+randomize_position(PedSector *start, PedSector *end, PedGeometry *where, int i)
+{
+
+	if (i == 0) {
+		*start = where->start;
+		do {
+			*end = *start+((unsigned int)rand() % where->length)-1;
+		} while (are_near(*start,*end));
+	}
+	else if (i == 3) {
+		*end = where->end;
+		do {
+			*start = *end-((unsigned int)rand() % where->length)+1;
+		} while (are_near(*start,*end));
+	}
+	else {
+		PedSector mid = where->start +
+		                ((unsigned int) rand() % where->length)-1;
+		do {
+			*start = where->start+
+				((unsigned int) rand() % (mid - where->start));
+			*end = mid +
+				((unsigned int) rand() % (where->end - mid));
+		} while (are_near(*start,*end));
+	}
+}
+
+/* TODO: Make it check different partition table types !*/
 /* Here we test (*only*) the size changing with do_resize */
-/*START_TEST (test_resize_size) {
+START_TEST (test_resize_size) {
+	PedConstraint *constraint = ped_device_get_constraint(dev);
+	PedGeometry *geom = NULL;
+	PedSector start, end;
+	PedPartition *part = NULL;
+	PedFileSystemType *fs_type = ped_file_system_type_get("ext2");
+	int i;
+	fail_unless(do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery),
+	            "We could create the label for resizing partitions");
+	srand(82181281);
+	for (i = 0; i < 4; i++) {
+		next_part_geometry(&geom, constraint, 1);
+		string[0][BUFSIZE] = 0;
+		strncpy(string[0],"Label",BUFSIZE);
+		uiquery.getstring = getstring;
+		fail_unless(_mkpart (0, geom->start,  geom->end,
+				PED_PARTITION_NORMAL,
+ 				fs_type, &part),
+			"Failed to create partition for resize. "
+			"Number %d, size %llds, start %llds, end %llds",
+			i, geom->length, geom->start, geom->end);
+	}
+	while (part) {
+		get_max_geometry(&geom,constraint,part);
+		for (i = 0; i < 4; i++) {
+			randomize_position(&start,&end,geom,i);
+			fail_unless(do_resize (disk, part, start, end,
+			                       UI_NO_FS_RESIZE, &uiquery),
+			            "Could not do %d resize of partition %d "
+			            "from %llds-%llds to %llds-%llds "
+			            "in region %llds-%llds",
+			            i, part->num, part->geom.start,
+			            part->geom.end, start, end, geom->start,
+			            geom->end);
+			fail_unless(are_near(part->geom.start,start) &&
+			           are_near(part->geom.end,end),
+			           "The %d resize of partition %d was wrong "
+			           "we wanted %llds-%llds, we got %llds-%llds",
+			           i, part->num, start, end,
+			           part->geom.start, part->geom.end);
+		}
+		do {
+			part = part->prev;
+		} while (part && ((part->type & PED_PARTITION_METADATA) ||
+		         (part->type & PED_PARTITION_FREESPACE)));
+	}
+	
 
-}*/
+}
+END_TEST
 
 Suite *common_suite(void)
 {
 	tcase_add_test(tc_core, test_null);
 	tcase_add_test(tc_core, test_mklabel);
 	tcase_add_test(tc_core, test_jugglepart);
+	tcase_add_test(tc_core, test_resize_size);
 	return s;
 }
 
-
 int main (void)
 {
 	int nf;
 		unlink_tempfile;
 		return 1;
 	}
-	if (!do_mklabel(dev,&disk,ped_disk_type_get("msdos"),&uiquery))
-		return EXIT_FAILURE;
 	ped_exception_set_handler(exception_handler);
 	string[0][BUFSIZE] = 0;
 	Suite *s = common_suite();
 	nf = srunner_ntests_failed(sr);
 	srunner_free(sr);
 	unlink_tempfile;
+
 	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 }

tests/functions.c

 
 #include <stdlib.h>
 #include <stdio.h>
+#include <check.h>
 #include <parted/parted.h>
+/* #include <endian.h> */
 #include "../src/strlist.h"
 #include "functions.h"
 
 exception_handler (PedException* ex) {
 	ex_opts = ex->options;
 	ex_type = ex->type;
+	switch (ex->type) {
+		case PED_EXCEPTION_ERROR:
+		case PED_EXCEPTION_FATAL:
+		case PED_EXCEPTION_BUG:
+		case PED_EXCEPTION_NO_FEATURE:
+			fail(ex->message);
+			break;
+	}
 	if (ex_opts & PED_EXCEPTION_FIX)
 		return PED_EXCEPTION_FIX;
 	else if (ex_opts & PED_EXCEPTION_RETRY)
 	if (a < 0LL) a = -a;
 	return (a < FAR); 
 }
+
+/* I don't believe our tests require this now, but I leave this here */
+#if 0
+long long llrand() {
+	
+	/* We assume that long long is always 64 bits and that int is always 32 bits,
+	   as per /usr/include/bits/types.h */
+#define STEP 32;
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define D >>
+#else
+#define D <<
+#endif
+
+	long long result;
+	result = (long long) rand();
+	result = result D STEP;
+	result |= (long long) rand();
+	
+	return result;
+
+}
+#endif

tests/functions.h

 
 /* Other functions */
 extern int are_near(long long a, long long b);
-
+extern long long llrand();
 #endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.