Creating pipeline for test case selection. Work in progress.
authorMarkus Kauppila <markus.kauppila@gmail.com>
Thu, 09 Jun 2011 16:37:51 +0300
changeset 5692 249e54c47b90
parent 5691 c0b4bcd901a0
child 5693 cd0110de8ad5
Creating pipeline for test case selection. Work in progress.
test/test-automation/runner.c
test/test-automation/testdummy/testdummy.c
--- a/test/test-automation/runner.c	Wed Jun 08 19:41:12 2011 +0300
+++ b/test/test-automation/runner.c	Thu Jun 09 16:37:51 2011 +0300
@@ -61,10 +61,22 @@
  * linked list. \todo write better doc
  */
 typedef struct TestSuiteReference {
-	char *name; //<! test suite name
+	char *name; //!< test suite name
+	void *library; //!< pointer to shared/dynamic library implemeting the suite
+
 	struct TestSuiteReference *next; //!< Pointer to next item in the list
 } TestSuiteReference;
 
+typedef struct TestCaseItem {
+	char *testName;
+	char *suiteName;
+
+	TestCaseInit testCaseInit;
+	TestCase testCase;
+	TestCaseQuit testCaseQuit;
+
+	struct TestCaseItem *next;
+} TestCaseItem; //!< \todo rename
 
 /*!
  * Scans the tests/ directory and returns the names
@@ -88,49 +100,49 @@
 	TestSuiteReference *suites = NULL;
 
 	Entry *entry = NULL;
-	if(directory) {
-		while(entry = readdir(directory)) {
-			if(entry->d_namlen > 2) { // discards . and ..
+	if(!directory) {
+		perror("Couldn't open directory: tests/");
+	}
+
+	while(entry = readdir(directory)) {
+		if(entry->d_namlen > 2) { // discards . and ..
+			const char *delimiters = ".";
+			char *name = strtok(entry->d_name, delimiters);
+			char *ext = strtok(NULL, delimiters);
+
+			// filter out all other suites but the selected test suite
+			int ok = 1;
+			if(only_selected_suite) {
+				ok = SDL_strncmp(selected_suite_name, name, NAME_BUFFER_SIZE) == 0;
+			}
+
+			if(ok && SDL_strcmp(ext, extension)  == 0) {
 				char buffer[NAME_BUFFER_SIZE];
 				memset(buffer, 0, NAME_BUFFER_SIZE);
 
-				char *name = strtok(entry->d_name, ".");
-				char *ext = strtok(NULL, ".");
+				strcat(buffer, directoryName);
+				strcat(buffer, name);
+				strcat(buffer, ".");
+				strcat(buffer, ext);
 
-				// filter out all other suites but the selected test suite
-				int ok = 1;
-				if(only_selected_suite) {
-					ok = SDL_strncmp(selected_suite_name, name, NAME_BUFFER_SIZE) == 0;
-				}
-
-				if(ok && SDL_strcmp(ext, extension)  == 0) {
-					strcat(buffer, directoryName);
-					strcat(buffer, name);
-					strcat(buffer, ".");
-					strcat(buffer, ext);
+				// create test suite reference
+				TestSuiteReference *reference = (TestSuiteReference *) SDL_malloc(sizeof(TestSuiteReference));
+				memset(reference, 0, sizeof(TestSuiteReference));
 
-					// create tes suite reference
-					TestSuiteReference *reference = (TestSuiteReference *) SDL_malloc(sizeof(TestSuiteReference));
-					memset(reference, 0, sizeof(TestSuiteReference));
+				int length = strlen(buffer) + 1; // + 1 for '\0'?
+				reference->name = SDL_malloc(length * sizeof(char));
 
-					int length = strlen(buffer) + 1; // + 1 for '\0'?
-					reference->name = SDL_malloc(length * sizeof(char));
+				strcpy(reference->name, buffer);
 
-					strcpy(reference->name, buffer);
-
-					reference->next = suites;
+				reference->next = suites;
+				suites = reference;
 
-					suites = reference;
-
-					printf("Reference added to: %s\n", buffer);
-				}
+				printf("Reference added to: %s\n", buffer);
 			}
 		}
+	}
 
-		closedir(directory);
-	} else {
-		perror("Couldn't open directory: tests/");
-	}
+	closedir(directory);
 
 	return suites;
 }
@@ -281,26 +293,22 @@
  * \return The return value of the test. Zero means success, non-zero failure.
  */
 int
-ExecuteTest(void *suite, TestCaseReference *testReference) {
-	TestCaseInit testCaseInit = LoadTestCaseInit(suite);
-	TestCaseQuit testCaseQuit = LoadTestCaseQuit(suite);
-	TestCase test = (TestCase) LoadTestCase(suite, testReference->name);
-
+ExecuteTest(TestCaseItem *testItem) {
 	int retVal = 1;
 	if(execute_inproc) {
-		testCaseInit();
+		testItem->testCaseInit();
 
-		test(0x0);
+		testItem->testCase(0x0);
 
-		retVal = testCaseQuit();
+		retVal = testItem->testCaseQuit();
 	} else {
 		int childpid = fork();
 		if(childpid == 0) {
-			testCaseInit();
+			testItem->testCaseInit();
 
-			test(0x0);
+			testItem->testCase(0x0);
 
-			exit(testCaseQuit());
+			exit(testItem->testCaseQuit());
 		} else {
 			int stat_lock = -1;
 			int child = wait(&stat_lock);
@@ -388,6 +396,118 @@
 
 
 /*!
+ * \todo add comment
+ */
+TestCaseItem *
+LoadTestCases(TestSuiteReference *suites) {
+	TestCaseItem *testCases = NULL;
+
+	TestSuiteReference *suiteReference = NULL;
+	for(suiteReference = suites; suiteReference; suiteReference = suiteReference->next) {
+		TestCaseReference **tests = QueryTestCases(suiteReference->library);
+
+		TestCaseReference *testReference = NULL;
+		int counter = 0;
+		for(testReference = tests[counter]; testReference; testReference = tests[++counter]) {
+
+			void *suite = suiteReference->library;
+			// Load test case functions
+			TestCaseInit testCaseInit = LoadTestCaseInit(suiteReference->library);
+			TestCaseQuit testCaseQuit = LoadTestCaseQuit(suiteReference->library);
+			TestCase testCase = (TestCase) LoadTestCase(suiteReference->library, testReference->name);
+
+			// Do the filtering
+			if(FilterTestCase(testReference)) {
+				//!< \todo deallocate these
+				TestCaseItem *item = SDL_malloc(sizeof(TestCaseItem));
+				memset(item, 0, sizeof(TestCaseItem));
+
+				item->testCaseInit = testCaseInit;
+				item->testCase = testCase;
+				item->testCaseQuit = testCaseQuit;
+
+				// prepend the list
+				item->next = testCases;
+				testCases = item;
+
+				printf("Added test: %s\n", testReference->name);
+			}
+		}
+	}
+
+	return testCases;
+}
+
+/*!
+ * \todo add comment
+ */
+void
+UnloadTestCases(TestCaseItem *item) {
+	TestCaseItem *ref = item;
+	while(ref) {
+		TestCaseItem *temp = ref->next;
+		SDL_free(ref);
+		ref = temp;
+	}
+
+	item = NULL;
+}
+
+/*!
+ * \todo add comment
+ */
+int
+FilterTestCase(TestCaseReference *testReference) {
+	//int retVal = 1;
+
+	if(testReference->enabled == TEST_DISABLED) {
+		//retVal = 0;
+		return 0;
+	}
+
+	if(1 && strstr(testReference->name, "rect") != NULL) {
+		//retVal = 1;
+		return 1;
+	} else {
+		return 0;
+	}
+
+	return 1;
+}
+
+/*!
+ * \todo add comment
+ */
+TestSuiteReference *
+LoadTestSuites(TestSuiteReference *suites) {
+	TestSuiteReference *reference = NULL;
+	for(reference = suites; reference; reference = reference->next) {
+		reference->library = LoadTestSuite(reference->name);
+	}
+
+	return suites;
+}
+
+
+/*!
+ * \todo add comment
+ */
+void UnloadTestSuites(TestSuiteReference *suites) {
+	TestSuiteReference *ref = suites;
+	while(ref) {
+		SDL_free(ref->name);
+		SDL_UnloadObject(ref->library);
+
+		TestSuiteReference *temp = ref->next;
+		SDL_free(ref);
+		ref = temp;
+	}
+
+	suites = NULL;
+}
+
+
+/*!
  * Entry point for test runner
  *
  * \param argc Count of command line arguments
@@ -411,53 +531,37 @@
 #endif
 
 	const Uint32 startTicks = SDL_GetTicks();
+
 	TestSuiteReference *suites = ScanForTestSuites(DEFAULT_TEST_DIRECTORY, extension);
-
-	// load the suites
-	// load tests and filter them
-	// end result: list of tests to run
-
-	TestSuiteReference *suiteReference = NULL;
-	for(suiteReference = suites; suiteReference; suiteReference = suiteReference->next) {
-		char *testSuiteName = suiteReference->name;
+	suites = LoadTestSuites(suites);
 
-		// if the current suite isn't selected, go to next suite
-		void *suite = LoadTestSuite(testSuiteName);
-		TestCaseReference **tests = QueryTestCases(suite);
+	// load tests and filter them
+	TestCaseItem *testCases = LoadTestCases(suites);
 
-		TestCaseReference *reference = NULL;
-		int counter = 0;
-		for(reference = tests[counter]; reference; reference = tests[++counter]) {
-			if(only_selected_test && SDL_strncmp(selected_test_name, reference->name, NAME_BUFFER_SIZE) != 0) {
-				continue;
-			}
+	// end result: list of tests to run
+	TestCaseItem *testItem = NULL;
+	for(testItem = testCases; testItem; testItem = testItem->next) {
+		int retVal = ExecuteTest(testItem);
 
-			if(reference->enabled == TEST_DISABLED) {
-				printf("Test %s (in %s) disabled. Omitting...\n", reference->name, testSuiteName);
+		if(retVal) {
+			failureCount++;
+			if(retVal == 2) {
+				//printf("%s (in %s): FAILED -> No asserts\n", reference->name, testSuiteName);
+				printf("%s (in %s): FAILED -> No asserts\n", "<test name>", "<suite name>");
 			} else {
-				printf("Executing %s (in %s):\n", reference->name, testSuiteName);
-
-				int retVal = ExecuteTest(suite, reference);
-
-				if(retVal) {
-					failureCount++;
-					if(retVal == 2) {
-						printf("%s (in %s): FAILED -> No asserts\n", reference->name, testSuiteName);
-					} else {
-						printf("%s (in %s): FAILED\n", reference->name, testSuiteName);
-					}
-				} else {
-					passCount++;
-					printf("%s (in %s): ok\n", reference->name, testSuiteName);
-				}
+				printf("%s (in %s): FAILED\n", "<test name>", "<suite name>");
 			}
-
-			printf("\n");
+		} else {
+			passCount++;
+			printf("%s (in %s): ok\n", "<test name>", "<suite name>");
 		}
 
-		SDL_UnloadObject(suite);
+		printf("\n");
 	}
 
+	UnloadTestCases(testCases);
+	UnloadTestSuites(suites);
+
 	const Uint32 endTicks = SDL_GetTicks();
 
 	printf("Ran %d tests in %0.5f seconds.\n", (passCount + failureCount), (endTicks-startTicks)/1000.0f);
@@ -465,15 +569,6 @@
 	printf("%d tests passed\n", passCount);
 	printf("%d tests failed\n", failureCount);
 
-	// Deallocate the memory used by test suites
-	TestSuiteReference *ref = suites;
-	while(ref) {
-		SDL_free(ref->name);
-
-		TestSuiteReference *temp = ref->next;
-		SDL_free(ref);
-		ref = temp;
-	}
 
 	return 0;
 }
--- a/test/test-automation/testdummy/testdummy.c	Wed Jun 08 19:41:12 2011 +0300
+++ b/test/test-automation/testdummy/testdummy.c	Thu Jun 09 16:37:51 2011 +0300
@@ -60,6 +60,7 @@
 {
 	const char *revision = SDL_GetRevision();
 
+	printf("Dummycase 1\n");
 	printf("Revision is %s\n", revision);
 
 	AssertEquals(3, 5, "fails");
@@ -69,11 +70,13 @@
 {
 	char *msg = "eello";
 	//msg[0] = 'H';
+	printf("Dummycase 2\n");
 	AssertTrue(0, "fails");
 }
 
 void dummycase3(void *arg)
 {
+	printf("Dummycase 3\n");
 	AssertTrue(1, "passes");
 }