Skip to contentSkip to conversation

x86: setjmp: check expected value of "i" to give better feedback

PATCH Applied Bill Wendling 4 participants 6 messages Sep 11, 2019 – Sep 24, 2019 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: setjmp: check expected value of "i" to give better feedback

Use a list of expected values instead of printing out numbers, which aren't very meaningful. This prints only if the expected and actual values differ.

---
 x86/setjmp.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
x86/setjmp.c +15 -4
index 976a632..c0b25ec 100644
@@ -1,19 +1,30 @@
 #include "libcflat.h"
 #include "setjmp.h"
 
+int expected[] = {
+	0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
+#define NUM_EXPECTED (sizeof(expected) / sizeof(int))
+
 int main(void)
 {
-    volatile int i;
+    volatile int i = -1, index = 0;
+    volatile bool had_errors = false;
     jmp_buf j;
 
     if (setjmp(j) == 0) {
 	    i = 0;
     }
-    printf("%d\n", i);
-    if (++i < 10) {
+    if (expected[index++] != i) {
+	    printf("FAIL: actual %d / expected %d\n", i, expected[index]);
+            had_errors = true;
+    }
+    if (index < NUM_EXPECTED) {
+	    i++;
 	    longjmp(j, 1);
     }
 
-    printf("done\n");
+    printf("Test %s\n", had_errors ? "FAILED" : "PASSED");
     return 0;
 }
Signature · 1 line
2.23.0.162.g0b9fbb3734-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 50 lines · Bill Wendling

Use a list of expected values instead of printing out numbers, which aren't very meaningful. This prints only if the expected and actual values differ.

---
 x86/setjmp.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
x86/setjmp.c +15 -5
index 976a632..c0b25ec 100644
@@ -1,19 +1,30 @@
 #include "libcflat.h"
 #include "setjmp.h"
 
+int expected[] = {
+       0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
+#define NUM_EXPECTED (sizeof(expected) / sizeof(int))
+
 int main(void)
 {
-    volatile int i;
+    volatile int i = -1, index = 0;
+    volatile bool had_errors = false;
     jmp_buf j;
 
     if (setjmp(j) == 0) {
            i = 0;
     }
-    printf("%d\n", i);
-    if (++i < 10) {
+    if (expected[index++] != i) {
+           printf("FAIL: actual %d / expected %d\n", i, expected[index]);
+            had_errors = true;
+    }
+    if (index < NUM_EXPECTED) {
+           i++;
            longjmp(j, 1);
     }
 
-    printf("done\n");
+    printf("Test %s\n", had_errors ? "FAILED" : "PASSED");
     return 0;
 }
--
Quoted 1 line

2.23.0.162.g0b9fbb3734-goog

#2 lore raw
In reply to Bill Wendling
Quoted 14 lines · Bill Wendling

Use a list of expected values instead of printing out numbers, which aren't very meaningful. This prints only if the expected and actual values differ.

---
 x86/setjmp.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
x86/setjmp.c +0 -0
index 976a632..c0b25ec 100644

x86/setjmp.c line 4

Quoted 4 lines
x86/setjmp.c +1 -0
@@ -1,19 +1,30 @@
 #include "libcflat.h"
 #include "setjmp.h"
 
+int expected[] = {

This should be 'static const'.

x86/setjmp.c line 8

Quoted 4 lines
x86/setjmp.c +4 -0
+	0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
+#define NUM_EXPECTED (sizeof(expected) / sizeof(int))

How about NUM_LONGJMPS? And you can use ARRAY_SIZE.

Quoted 5 lines
x86/setjmp.c +2 -1
+
 int main(void)
 {
-    volatile int i;
+    volatile int i = -1, index = 0;

x86/setjmp.c line 20

Quoted 10 lines
x86/setjmp.c +3 -2
+    volatile bool had_errors = false;
     jmp_buf j;
 
     if (setjmp(j) == 0) {
 	    i = 0;
     }
-    printf("%d\n", i);
-    if (++i < 10) {
+    if (expected[index++] != i) {
+	    printf("FAIL: actual %d / expected %d\n", i, expected[index]);

This will print the wrong expected value on failure since index was incremented above.

x86/setjmp.c line 22

Quoted 2 lines
x86/setjmp.c +2 -0
+            had_errors = true;
+    }

had_errors seems like overkill. If we fail once, what's the pointing of continuing on?

x86/setjmp.c line 26

Quoted 4 lines
x86/setjmp.c +2 -0
+    if (index < NUM_EXPECTED) {
+	    i++;
 	    longjmp(j, 1);
     }

And we should pass i to longjmp(), otherwise there isn't really any point to having separate index and i variables, e.g.:

	volatile int index = 0;
	jmp_buf j;
	int i;

	i = setjmp(j);

	if (expected[index] != i) {
		printf("FAIL: actual %d / expected %d\n", i, expected[index]);
		return -1;
	}
	index++;

	if (i < NUM_LONGJMPS)
		longjmp(jmp_buf, i + 1);	

	return 0;
Quoted 8 lines
x86/setjmp.c +1 -1
-    printf("done\n");
+    printf("Test %s\n", had_errors ? "FAILED" : "PASSED");
     return 0;
 }
Signature · 1 line
2.23.0.162.g0b9fbb3734-goog
#3 lore raw

PATCH 1/1 x86: setjmp: check expected value of "i" to give better feedback

Use a list of expected values instead of printing out numbers, which aren't very meaningful. This prints only if the expected and actual values differ.

---
 x86/setjmp.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
x86/setjmp.c +15 -8
index 976a632..1874944 100644
@@ -1,19 +1,26 @@
 #include "libcflat.h"
 #include "setjmp.h"
 
+static const int expected[] = {
+	0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
+#define NUM_LONGJMPS ARRAY_SIZE(expected)
+
 int main(void)
 {
-    volatile int i;
+    volatile int index = 0;
     jmp_buf j;
+    int i;
 
-    if (setjmp(j) == 0) {
-	    i = 0;
-    }
-    printf("%d\n", i);
-    if (++i < 10) {
-	    longjmp(j, 1);
+    i = setjmp(j);
+    if (expected[index] != i) {
+	    printf("FAIL: actual %d / expected %d\n", i, expected[index]);
+	    return -1;
     }
+    index++;
+    if (i + 1 < NUM_LONGJMPS)
+	    longjmp(j, i + 1);
 
-    printf("done\n");
     return 0;
 }
Signature · 1 line
2.23.0.351.gc4317032e6-goog
#4 lore raw

+ Paolo Bonzini, Radim Krčmář, Marc Orr

Quoted 52 lines · Bill Wendling

Use a list of expected values instead of printing out numbers, which aren't very meaningful. This prints only if the expected and actual values differ.

---
 x86/setjmp.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
x86/setjmp.c +15 -9
index 976a632..1874944 100644
@@ -1,19 +1,26 @@
 #include "libcflat.h"
 #include "setjmp.h"
 
+static const int expected[] = {
+       0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
+#define NUM_LONGJMPS ARRAY_SIZE(expected)
+
 int main(void)
 {
-    volatile int i;
+    volatile int index = 0;
     jmp_buf j;
+    int i;
 
-    if (setjmp(j) == 0) {
-           i = 0;
-    }
-    printf("%d\n", i);
-    if (++i < 10) {
-           longjmp(j, 1);
+    i = setjmp(j);
+    if (expected[index] != i) {
+           printf("FAIL: actual %d / expected %d\n", i, expected[index]);
+           return -1;
     }
+    index++;
+    if (i + 1 < NUM_LONGJMPS)
+           longjmp(j, i + 1);
 
-    printf("done\n");
     return 0;
 }
--

2.23.0.351.gc4317032e6-goog

maintainer Applied
#5 lore raw
In reply to Bill Wendling
Quoted 39 lines · Bill Wendling

Use a list of expected values instead of printing out numbers, which aren't very meaningful. This prints only if the expected and actual values differ.

---
 x86/setjmp.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
x86/setjmp.c +11 -3
index 976a632..c0b25ec 100644
@@ -1,19 +1,30 @@
 #include "libcflat.h"
 #include "setjmp.h"
 
+int expected[] = {
+	0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
+#define NUM_EXPECTED (sizeof(expected) / sizeof(int))
+
 int main(void)
 {
-    volatile int i;
+    volatile int i = -1, index = 0;
+    volatile bool had_errors = false;
     jmp_buf j;
 
     if (setjmp(j) == 0) {
 	    i = 0;
     }
-    printf("%d\n", i);
-    if (++i < 10) {
+    if (expected[index++] != i) {
+	    printf("FAIL: actual %d / expected %d\n", i, expected[index]);
+            had_errors = true;

x86/setjmp.c line 30

Quoted 10 lines
x86/setjmp.c +4 -1
+    }
+    if (index < NUM_EXPECTED) {
+	    i++;
 	    longjmp(j, 1);
     }
 
-    printf("done\n");
+    printf("Test %s\n", had_errors ? "FAILED" : "PASSED");
     return 0;
 }

Queued, thanks.

Paolo