[Mesa-dev] [PATCH 4/5] r300g/compiler/tests: Enable reading expected output from the .test files

Tom Stellard tom at stellard.net
Sun Jan 5 18:51:20 PST 2014


From: Tom Stellard <thomas.stellard at amd.com>

---
 .../drivers/r300/compiler/tests/README.test        | 26 ++++++++
 .../drivers/r300/compiler/tests/rc_test_helpers.c  | 77 ++++++++++++++++++++--
 .../drivers/r300/compiler/tests/rc_test_helpers.h  |  7 ++
 3 files changed, 105 insertions(+), 5 deletions(-)
 create mode 100644 src/gallium/drivers/r300/compiler/tests/README.test

diff --git a/src/gallium/drivers/r300/compiler/tests/README.test b/src/gallium/drivers/r300/compiler/tests/README.test
new file mode 100644
index 0000000..8a698a8
--- /dev/null
+++ b/src/gallium/drivers/r300/compiler/tests/README.test
@@ -0,0 +1,26 @@
+# This file demonstrates the syntax of 'test' input files.  It is preferred,
+# but not required that 'test' input files have a .test suffix
+# (e.g. input.test)
+
+# Any line that begins with a '#' character is ignored.  Empty lines and lines
+# that begin with whitespace are also ignored.
+
+# The first N lines must be assembly output in the format produced by the
+# radeon_program_print() family of functions.  This assembly represents the
+# program to be tested.
+#
+
+const[0] = {     2.0000     0.0000     0.0000     0.0000 }
+
+RCP temp[0].x, const[1].x___;
+RCP temp[0].y, const[1]._y__;
+MUL temp[1].xy, const[0].xx__, temp[0].xy__;
+MOV output[0].xy, temp[1].xy;
+
+# After the program assembly you may optionally add a line containing only a
+# '=' character followed by one or more lines of expected output.  The expected
+# output is specified using posix extended regular expressions
+=
+RCP temp\[[0-9]\]\.x \* 2, const\[1\]\.x___;
+RCP temp\[[0-9]\]\.y \* 2, const\[1\]\._y__;
+MOV output\[0\]\.xy, temp\[[0-9]\]\.xy__;
diff --git a/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.c b/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.c
index 725be4c..f307561 100644
--- a/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.c
+++ b/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.c
@@ -528,7 +528,6 @@ void init_compiler(
 }
 
 #define MAX_LINE_LENGTH 100
-#define MAX_PATH_LENGTH 100
 
 unsigned load_program(
 	struct radeon_compiler *c,
@@ -536,19 +535,19 @@ unsigned load_program(
 	const char *filename)
 {
 	char line[MAX_LINE_LENGTH];
-	char path[MAX_PATH_LENGTH];
 	FILE *file;
 	unsigned *count;
 	char **string_store;
 	unsigned i = 0;
 
 	memset(line, 0, sizeof(line));
-	snprintf(path, MAX_PATH_LENGTH, "compiler/tests/%s", filename);
-	file = fopen(path, "r");
+	memset(test, 0, sizeof(struct rc_test_file));
+
+	snprintf(test->path, MAX_PATH_LENGTH, "compiler/tests/%s", filename);
+	file = fopen(test->path, "r");
 	if (!file) {
 		return 0;
 	}
-	memset(test, 0, sizeof(struct rc_test_file));
 
 	count = &test->num_input_lines;
 
@@ -607,3 +606,71 @@ unsigned load_program(
 	}
 	return 1;
 }
+
+unsigned check_program(
+	const struct radeon_compiler *c,
+	const struct rc_test_file *test)
+{
+	FILE *output;
+	char path[MAX_PATH_LENGTH];
+	char line[MAX_LINE_LENGTH];
+	char *expected = NULL;
+	unsigned expected_index = 0;
+	unsigned ret = 0;
+
+	unsigned path_len = strlen(test->path);
+	/* +4 '.out'
+	 * +1 null byte at end
+	 */
+	if (path_len + 4 + 1 > MAX_PATH_LENGTH) {
+		fprintf(stderr, "File path is too long: %s\n", test->path);
+		return 0;
+	}
+	memcpy(path, test->path, path_len);
+	strcpy(path + path_len, ".out");
+	output = fopen(path, "w+");
+	if (!output) {
+		fprintf(stderr, "Failed to open file; %s\n", path);
+	}
+
+	rc_print_program_file(&c->Program, output);
+	rewind(output);
+
+	while (fgets(line, MAX_LINE_LENGTH, output)) {
+		char *newline;
+		expected = test->expected[expected_index];
+		/* Remove the newline character */
+		newline = strchr(expected, '\n');
+		if (newline) {
+			*newline = '\0';
+		}
+
+		char err_buf[REGEX_ERR_BUF_SIZE];
+		regex_t regex;
+		int err_code;
+
+		err_code = regcomp(&regex, expected, REG_EXTENDED | REG_NOSUB);
+		if (err_code) {
+			regerror(err_code, &regex, err_buf, REGEX_ERR_BUF_SIZE);
+			fprintf(stderr, "Failed to compile regex: %s\n",
+								err_buf);
+			ret = 0;
+			goto done;
+		}
+		if (!regexec(&regex, line, 0, NULL, 0)) {
+			/* Match found */
+			expected_index++;
+			if (expected_index == test->num_expected_lines) {
+				/* All matches have been found. */
+				ret = 1;
+				goto done;
+			}
+		}
+	}
+
+	fprintf(stderr, "Failed to match pattern: %s\n", expected);
+	ret = 0;
+done:
+	fclose(output);
+	return ret;
+}
diff --git a/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.h b/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.h
index 6cc8d9c..dd1c323 100644
--- a/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.h
+++ b/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.h
@@ -29,7 +29,10 @@
 
 #include "radeon_compiler.h"
 
+#define MAX_PATH_LENGTH 100
+
 struct rc_test_file {
+	char path[MAX_PATH_LENGTH];
 	unsigned num_input_lines;
 	char **input;
 	unsigned num_expected_lines;
@@ -69,3 +72,7 @@ unsigned load_program(
 	struct radeon_compiler *c,
 	struct rc_test_file *test,
 	const char *filename);
+
+unsigned check_program(
+	const struct radeon_compiler *c,
+	const struct rc_test_file *test);
-- 
1.8.1.5



More information about the mesa-dev mailing list